// File: /workspace/github_cpp_makefile_star2_O1/tanakh[P]icfpc2015/main.cpp
// Function: annealing(problem const&, int, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, double, double, int, int)
pair<string, int> annealing(const problem &p, int seed, int tle, int mle, const string &tag,
                            double init_temp, double temp_decay, int rounds, int beam_width)
{
  int best_score = -1;
  string best_move;

  vector<double> pp(NUM_FEATURES);
  for (int i = 0; i < (int)pp.size(); i++)
    pp[i] = genrand_real1() - 0.5;
  double score = -1;

  double temp = init_temp;
  for (int t = 0; t < rounds; t++, temp *= temp_decay) {
    if (t % 10 == 0)
      cerr << "round: [" << t << "/" << rounds << "]: "
           << temp << ", " << score << "        \r" << flush;

    if (temp < 1){
      cerr << "REBOOT                                   " << endl;
      temp = best_score / 10;
    }

    auto bkup = pp;

    if (genrand_int31() % 2 == 0) {
      pp[genrand_int31() % pp.size()] = genrand_real1() - 0.5;
    }
    else {
      pp[genrand_int31() % pp.size()] += genrand_real1() * 0.5 - 0.25;
      // pp[genrand_int31() % pp.size()] = genrand_real1() - 0.5;
    }

    // auto rr = solve(p, seed, tle, mle, pp, false);
    auto rr =
      beam_width == 1 ? solve(p, seed, tle, mle, pp, false)
      : beam_search(p, seed, tle, mle, pp, beam_width);

    if (score <= rr.second ||
        genrand_real2() <= exp((rr.second - score) / temp)) {
      score = rr.second;

      if (best_score < rr.second) {
        best_score = rr.second;
        best_move = rr.first;

        cerr << "*BEST* " << p.id << "-" << seed << ", round " << t << ": " << best_score
             << "                      " << endl;
        // cerr << "param[] = [";
        // for (int i = 0; i < (int)pp.size(); i++)
        //   cerr << pp[i] << ", ";
        // cerr << "]" << endl;;

        output_solution(p.id, seed, best_score, best_move, tag);
      }
    }
    else {
      pp = bkup;
    }
  }

  return make_pair(best_move, best_score);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movsd %xmm1, 0x68(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %r9, 0x60(%rsp)
movl %edx, 0xc(%rsp)
movq %rsi, %r15
movq %rdi, %r12
leaq 0x48(%rsp), %r14
movq %r14, -0x10(%r14)
xorl %eax, %eax
movq %rax, -0x8(%r14)
movb $0x0, (%r14)
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movl $0x50, %edi
callq 0x3310
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, %rcx
addq $0x50, %rcx
movq %rcx, 0x30(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rax)
movupd %xmm0, 0x30(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, (%rax)
movq %rcx, 0x28(%rsp)
subq 0x20(%rsp), %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x82d3
xorl %ebx, %ebx
callq 0x3a63
movq %rax, %xmm0
punpckldq 0xcd82(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xcd8a(%rip), %xmm0     # 0x15020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xce16(%rip), %xmm1     # 0x150c0
addsd 0xce46(%rip), %xmm1     # 0x150f8
movq 0x20(%rsp), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %rbx
movq 0x28(%rsp), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rax
cmpq %rax, %rbx
jl 0x827c
movq %r12, 0x58(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, 0x100(%rsp)
jle 0x8703
movsd 0xcdfd(%rip), %xmm0     # 0x150f0
movsd %xmm0, 0x18(%rsp)
xorl %r13d, %r13d
movq 0x14ce5(%rip), %rbx     # 0x1cfe8
movl %r13d, %eax
movl $0xcccccccd, %ecx       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x23, %rax
addl %eax, %eax
leal (%rax,%rax,4), %eax
cmpl %r13d, %eax
jne 0x83cf
movl $0x8, %edx
movq %rbx, %rdi
leaq 0xd1fb(%rip), %rsi      # 0x1552b
callq 0x3350
movq %rbx, %rdi
movl %r13d, %esi
callq 0x34a0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xd49e(%rip), %rsi      # 0x157f0
callq 0x3350
movq %r14, %rdi
movl 0x100(%rsp), %esi
callq 0x34a0
movq %rax, %r14
movl $0x3, %edx
movq %rax, %rdi
leaq 0xd1bc(%rip), %rsi      # 0x15534
callq 0x3350
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3450
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0xcfb2(%rip), %rsi      # 0x1534f
callq 0x3350
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x3450
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0xd176(%rip), %rsi      # 0x15538
callq 0x3350
movq %r14, %rdi
callq 0x3200
movsd 0xcd11(%rip), %xmm0     # 0x150e8
ucomisd 0x10(%rsp), %xmm0
jbe 0x843e
movl $0x29, %edx
movq %rbx, %rdi
leaq 0xd154(%rip), %rsi      # 0x15542
callq 0x3350
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movslq %ebp, %rax
imulq $0x66666667, %rax, %rax # imm = 0x66666667
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x22, %rax
addl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xc428
callq 0x3a63
movq %rax, %r12
callq 0x3a63
movq %rax, %r14
callq 0x3a63
shrq %rax
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
movq %r14, %xmm1
punpckldq 0xcb85(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xcb8d(%rip), %xmm1     # 0x15020
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xcc19(%rip), %xmm0     # 0x150c0
testb $0x2, %r12b
jne 0x84b7
addsd 0xcc43(%rip), %xmm0     # 0x150f8
jmp 0x84cc
mulsd 0xcc11(%rip), %xmm0     # 0x150d0
addsd 0xcc39(%rip), %xmm0     # 0x15100
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movl 0x108(%rsp), %eax
cmpl $0x1, %eax
jne 0x84fc
movl $0x0, (%rsp)
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
movl 0xc(%rsp), %edx
leaq 0x20(%rsp), %r9
callq 0x74ca
jmp 0x8515
movl %eax, (%rsp)
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
movl 0xc(%rsp), %edx
leaq 0x20(%rsp), %r9
callq 0x7c35
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x85a9
movsd %xmm1, 0x18(%rsp)
callq 0x3a63
movq %rax, %xmm0
punpckldq 0xcacb(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xcad3(%rip), %xmm0     # 0x15020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xcb67(%rip), %xmm1     # 0x150c8
movapd %xmm1, 0xb0(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x90(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x34d0
ucomisd 0xb0(%rsp), %xmm0
jae 0x85a9
leaq 0x20(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xc260
jmp 0x86b7
movl 0x90(%rsp), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x18(%rsp)
cmpl %r12d, %ebp
jge 0x86b7
leaq 0x38(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3180
movl $0x7, %edx
movq %rbx, %rdi
leaq 0xcf86(%rip), %rsi      # 0x1556c
callq 0x3350
movl (%r15), %esi
movq %rbx, %rdi
callq 0x34a0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xcefd(%rip), %rsi      # 0x15505
callq 0x3350
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x34a0
movq %rax, %r14
movl $0x8, %edx
movq %rax, %rdi
leaq 0xcf49(%rip), %rsi      # 0x15574
callq 0x3350
movq %r14, %rdi
movl %r13d, %esi
callq 0x34a0
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0xcf33(%rip), %rsi      # 0x15580
callq 0x3350
movq %r14, %rdi
movl %r12d, %esi
callq 0x34a0
movq %rax, %r14
movl $0x16, %edx
movq %rax, %rdi
leaq 0xcee6(%rip), %rsi      # 0x15555
callq 0x3350
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movl (%r15), %edi
movl 0xc(%rsp), %esi
movl %r12d, %edx
leaq 0x38(%rsp), %rcx
movq 0x60(%rsp), %r8
callq 0x69f2
movl %r12d, %ebp
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x86ce
callq 0x32f0
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x86e0
callq 0x32f0
incl %r13d
movsd 0x10(%rsp), %xmm0
mulsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl %r13d, 0x100(%rsp)
jne 0x8303
movq 0x58(%rsp), %r14
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0xeda4
movl %ebp, 0x20(%r14)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
leaq 0x48(%rsp), %rbx
je 0x873c
callq 0x32f0
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x874b
callq 0x32f0
movq %r14, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x87a1
movq %rax, %rbx
jmp 0x87b8
jmp 0x87a1
movq %rax, %rbx
jmp 0x8788
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x8788
callq 0x32f0
leaq 0x48(%rsp), %r14
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x87a9
callq 0x32f0
jmp 0x87a9
movq %rax, %rbx
leaq 0x48(%rsp), %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x87b8
callq 0x32f0
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x87c7
callq 0x32f0
movq %rbx, %rdi
callq 0x34b0