// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movsd %xmm1, 0x50(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %r9, 0x48(%rsp)
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x20(%rsp), %rdi
pushq $0xa
popq %rsi
leaq 0x90(%rsp), %rdx
callq 0x9ffa
movq 0x20(%rsp), %rax
xorl %ebx, %ebx
movq 0x28(%rsp), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rax
cmpq %rax, %rbx
jge 0x7024
callq 0x3a22
addsd 0xb0ab(%rip), %xmm0     # 0x120c0
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
jmp 0x6ff4
movq %r14, 0x40(%rsp)
xorl %r13d, %r13d
movl 0xf0(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movl %ecx, 0x3c(%rsp)
movsd 0xb067(%rip), %xmm0     # 0x120b0
movsd %xmm0, 0x18(%rsp)
pushq $-0x1
popq %r14
leaq 0x90(%rsp), %rbx
movl %r13d, %eax
xorl %edx, %edx
pushq $0xa
popq %rcx
divl %ecx
cmpl %r13d, 0x3c(%rsp)
je 0x7360
imull $0xa, %eax, %eax
cmpl %r13d, %eax
jne 0x7109
movq 0x14f65(%rip), %rdi     # 0x1bfe8
leaq 0xb479(%rip), %rsi      # 0x12503
callq 0x32f0
movq %rax, %rdi
movl %r13d, %esi
callq 0x3490
movq %rax, %rdi
leaq 0xb724(%rip), %rsi      # 0x127c8
callq 0x32f0
movq %rax, %rdi
movl 0xf0(%rsp), %esi
callq 0x3490
movq %rax, %rdi
leaq 0xb44a(%rip), %rsi      # 0x1250c
callq 0x32f0
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3440
movq %rax, %rdi
leaq 0xb248(%rip), %rsi      # 0x12327
callq 0x32f0
movq %rax, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x3440
movq %rax, %rdi
leaq 0xb414(%rip), %rsi      # 0x12510
callq 0x32f0
movq %rax, %rdi
callq 0x31f0
movsd 0xaf97(%rip), %xmm0     # 0x120a8
ucomisd 0x10(%rsp), %xmm0
jbe 0x7147
movq 0x14ec8(%rip), %rdi     # 0x1bfe8
leaq 0xb3f3(%rip), %rsi      # 0x1251a
callq 0x32f0
movq %rax, %rdi
callq 0x3130
movl %r14d, %eax
cltd
pushq $0xa
popq %rcx
idivl %ecx
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xa04c
callq 0x3a17
movq %rax, %r12
callq 0x3a22
movsd %xmm0, 0x8(%rsp)
callq 0x3a17
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
testb $0x1, %r12b
jne 0x719a
movsd 0x8(%rsp), %xmm0
addsd 0xaf28(%rip), %xmm0     # 0x120c0
jmp 0x71b5
movsd 0x8(%rsp), %xmm0
mulsd 0xaee8(%rip), %xmm0     # 0x12090
addsd 0xaf08(%rip), %xmm0     # 0x120b8
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movl 0xf8(%rsp), %eax
cmpl $0x1, %eax
jne 0x71de
andl $0x0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x20(%rsp), %r9
callq 0x645d
jmp 0x71f3
movl %eax, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
leaq 0x20(%rsp), %r9
callq 0x6a94
movl 0xb0(%rsp), %r12d
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x8(%rsp)
ucomisd 0x18(%rsp), %xmm0
jae 0x7271
callq 0x3a53
movsd %xmm0, 0x8(%rsp)
cvtsi2sdl 0xb0(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x34c0
ucomisd 0x8(%rsp), %xmm0
jae 0x725e
leaq 0x20(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x9e2a
movl %r14d, %r12d
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x7325
movl 0xb0(%rsp), %r12d
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x8(%rsp)
cmpl %r12d, %r14d
jge 0x7322
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x3170
movq 0x14d5a(%rip), %rdi     # 0x1bfe8
leaq 0xb2af(%rip), %rsi      # 0x12544
callq 0x32f0
movl (%r15), %esi
movq %rax, %rdi
callq 0x3490
movq %rax, %rdi
leaq 0xb22e(%rip), %rsi      # 0x124dd
callq 0x32f0
movq %rax, %rdi
movl %ebp, %esi
callq 0x3490
movq %rax, %rdi
leaq 0xb284(%rip), %rsi      # 0x1254c
callq 0x32f0
movq %rax, %rdi
movl %r13d, %esi
callq 0x3490
movq %rax, %rdi
leaq 0xb276(%rip), %rsi      # 0x12558
callq 0x32f0
movq %rax, %rdi
movl %r12d, %esi
callq 0x3490
movq %rax, %rdi
leaq 0xb231(%rip), %rsi      # 0x1252d
callq 0x32f0
movq %rax, %rdi
callq 0x3130
movl (%r15), %edi
movl %ebp, %esi
movl %r12d, %edx
leaq 0x58(%rsp), %rcx
movq 0x48(%rsp), %r8
callq 0x5d50
jmp 0x7325
movl %r14d, %r12d
movq %rbx, %rdi
callq 0x3550
leaq 0x78(%rsp), %rdi
callq 0xdf34
incl %r13d
movsd 0x10(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movl %r12d, %r14d
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x705b
leaq 0x58(%rsp), %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x33a0
movl %r14d, 0x20(%rbx)
leaq 0x20(%rsp), %rdi
callq 0xdf34
leaq 0x58(%rsp), %rdi
callq 0x3550
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x73c9
movq %rax, %rbx
jmp 0x73d6
jmp 0x73c9
movq %rax, %rbx
jmp 0x73bd
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x3550
leaq 0x78(%rsp), %rdi
callq 0xdf34
jmp 0x73cc
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xdf34
leaq 0x58(%rsp), %rdi
callq 0x3550
movq %rbx, %rdi
callq 0x34a0