// File: louisabraham[P]cage/./cage.hpp
// Function: std::pair<float, std::vector<float, std::allocator<float>>> genetic_algorithm<std::vector<float, std::allocator<float>>, float>(std::function<std::vector<float, std::allocator<float>> ()>, std::function<float (std::vector<float, std::allocator<float>> const&)>, bool, std::function<void (std::vector<float, std::allocator<float>>&)>, int, int, int)
pair<S, T> genetic_algorithm(
    function<T()> init,           // generate a new item
    function<S(const T &)> score, // score function
    bool minimize,                // minimize or maximize
    function<void(T &)> mutate,   // in-place mutation function
    int pop_size,                 // population size
    int steps,                    // number of optimization steps
    int luby_basis = -1           // if not -1, use optimal Las Vegas restarts
                                  // heuristic: diameter of the search space in number of mutations
)
{
    srand(time(NULL));

    // restarts

    int cur_luby = 1;
    int luby_count = 0;
    int cur_count = 0;
    auto restart = [&]() {
        if (luby_basis == -1)
            return false;
        if (cur_count++ < cur_luby)
            return false;
        cur_count = 0;
        cur_luby = luby_basis * luby(luby_count++);
        return true;
    };

    using pair_t = pair<double, T>;

    auto comp = minimize ? mini<pair_t> : maxi<pair_t>;

    auto rank = [&](pair_t &e) {
        e.first = score(e.second);
    };

    pair_t global_best, best;

    auto reset = [&]() {
        if (comp(best, global_best))
            global_best = best;
        best.second = init();
        rank(best);
        //        cerr << best.first << endl;
    };

    reset();
    global_best = best;

    while (true)
    {
        auto cand = best;
        for (int i = 0; i < pop_size; i++)
        {
            mutate(cand.second);
            rank(cand);
            if (comp(cand, best))
                best = cand;
            steps--;
            if (!steps)
            {
                reset();
                return global_best;
            }
            if (restart())
                reset();
        }
    }
}

// Assembly:
subq $0x198, %rsp            # imm = 0x198
movq %r8, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
movb %cl, %al
movq %rdi, %rcx
movq %rcx, 0x40(%rsp)
movl 0x1a8(%rsp), %ecx
movl 0x1a0(%rsp), %ecx
movq %rdi, 0x190(%rsp)
movq %rsi, 0x188(%rsp)
movq %rdx, 0x180(%rsp)
andb $0x1, %al
movb %al, 0x17f(%rsp)
movq %r8, 0x170(%rsp)
movl %r9d, 0x16c(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x10d0
movl %eax, %edi
callq 0x10e0
movl $0x1, 0x168(%rsp)
movl $0x0, 0x164(%rsp)
movl $0x0, 0x160(%rsp)
leaq 0x1a8(%rsp), %rax
movq %rax, 0x140(%rsp)
leaq 0x160(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x168(%rsp), %rax
movq %rax, 0x150(%rsp)
leaq 0x164(%rsp), %rax
movq %rax, 0x158(%rsp)
testb $0x1, 0x17f(%rsp)
je 0x35cd
leaq 0x205a(%rip), %rax      # 0x5620
movq %rax, 0x18(%rsp)
jmp 0x35db
leaq 0x206c(%rip), %rax      # 0x5640
movq %rax, 0x18(%rsp)
jmp 0x35db
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x138(%rsp)
movq %rax, 0x130(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x5660
leaq 0xf0(%rsp), %rdi
callq 0x5660
jmp 0x3611
movq 0x30(%rsp), %rax
leaq 0x138(%rsp), %rcx
movq %rcx, 0xb8(%rsp)
leaq 0xf0(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
leaq 0x110(%rsp), %rcx
movq %rcx, 0xc8(%rsp)
movq %rax, 0xd0(%rsp)
leaq 0x130(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x56a0
jmp 0x366d
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x57c0
jmp 0x3684
jmp 0x3686
jmp 0x3688
leaq 0x98(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x5800
jmp 0x369f
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x388e
movq 0x20(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x5840
jmp 0x36d2
leaq 0x130(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x5880
jmp 0x36e9
movq 0x138(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x5800
jmp 0x370a
leaq 0x50(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x5800
jmp 0x371e
movq 0x10(%rsp), %rax
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq *%rax
movb %al, 0xf(%rsp)
jmp 0x3735
leaq 0x50(%rsp), %rdi
callq 0x58c0
leaq 0x70(%rsp), %rdi
callq 0x58c0
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x3756
jmp 0x3800
leaq 0xf0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x57c0
jmp 0x376d
jmp 0x3800
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x38f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x38e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x38b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x37f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x58c0
leaq 0x70(%rsp), %rdi
callq 0x58c0
jmp 0x38b2
movl 0x1a0(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1a0(%rsp)
cmpl $0x0, 0x1a0(%rsp)
jne 0x3848
leaq 0xb8(%rsp), %rdi
callq 0x56a0
jmp 0x382a
movq 0x38(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x58e0
jmp 0x383e
movl $0x1, 0x4c(%rsp)
jmp 0x3896
leaq 0x140(%rsp), %rdi
callq 0x5920
movb %al, 0xe(%rsp)
jmp 0x385b
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0x3865
jmp 0x3876
leaq 0xb8(%rsp), %rdi
callq 0x56a0
jmp 0x3874
jmp 0x3876
jmp 0x3878
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x36aa
movl $0x0, 0x4c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x58c0
movl 0x4c(%rsp), %eax
testl %eax, %eax
jne 0x38c1
jmp 0x38ad
jmp 0x3686
leaq 0x98(%rsp), %rdi
callq 0x58c0
jmp 0x38e8
leaq 0xf0(%rsp), %rdi
callq 0x58c0
leaq 0x110(%rsp), %rdi
callq 0x58c0
movq 0x40(%rsp), %rax
addq $0x198, %rsp            # imm = 0x198
retq
leaq 0xf0(%rsp), %rdi
callq 0x58c0
leaq 0x110(%rsp), %rdi
callq 0x58c0
movq 0xe8(%rsp), %rdi
callq 0x1190
nop