// File: /workspace/github_cpp_makefile_star2_O3/louisabraham[P]cage/example.cpp
// Function: main
int main()
{
    srand(time(NULL));

    const int n = 1000;
    const int dim = 2;

    vector<vector<float_t>> X(n);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < dim; j++)
            X[i].push_back(drand());

    using elem_t = vector<float_t>;

    const function<elem_t()> init = []() {
        elem_t ans;
        for (int i = 0; i < dim + 1; i++)
            ans.push_back(drand());
        return ans;
    };

    const function<float_t(elem_t, elem_t)> eval = [&](elem_t coefs, elem_t x) {
        float_t ans = coefs[dim];
        for (int i = 0; i < dim; i++)
            ans += coefs[i] * x[i];
        return ans;
    };

    vector<float_t> y(n);
    auto true_coefs = init();
    cout << "real coeffs ";
    for (auto c : true_coefs)
        cout << setprecision(3) << c << ' ';
    cout << endl;
    for (int i = 0; i < n; i++)
        y[i] = eval(true_coefs, X[i]);

    float mean = 0;
    for (int i = 0; i < n; i++)
        mean += y[i];
    mean /= n;
    float var = 0;
    for (int i = 0; i < n; i++)
    {
        float tmp = y[i] - mean;
        var += tmp * tmp;
    }
    var /= n;

    cout << "std " << sqrt(var) << endl;

    const function<float_t(const elem_t &)> score = [&](const elem_t &coefs) {
        float_t error = 0;
        for (int i = 0; i < n; i++)
        {
            float_t e = eval(coefs, X[i]) - y[i];
            error += e * e;
        }
        error /= n;
        if (error != error)
            return (float)1e6;
        return error;
    };

    const function<void(elem_t &)> mutate = [&](elem_t &coefs) {
        float_t &x = coefs[rand() % (dim + 1)];
        auto ptr = reinterpret_cast<uint32_t *>(&x);
        *ptr ^= (1 << (rand() % 32));
    };

    auto ans = genetic_algorithm(
        init,   // generate a new item
        score,  // score function
        true,   // minimize or maximize
        mutate, // in-place mutation function
        10,     // population size
        100000, // number of optimization steps
        50      // if not -1, use optimal Las Vegas restarts
    );

    cout << "solution coeffs ";
    for (auto c : ans.second)
        cout << setprecision(3) << c << ' ';
    cout << endl;

    cout << "rmse " << sqrt(ans.first) << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x10e0
movl %eax, %edi
callq 0x10f0
movl $0x5dc0, %edi           # imm = 0x5DC0
callq 0x1110
movq %rax, 0x88(%rsp)
leaq 0x5dc0(%rax), %rbx
movq %rbx, 0x98(%rsp)
movl $0x5dc0, %edx           # imm = 0x5DC0
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %rbx, 0x90(%rsp)
leaq 0x30(%rsp), %rbx
movb $0x1, %bpl
leaq (%r15,%r15,2), %r12
movq 0x88(%rsp), %rax
leaq (%rax,%r12,8), %r14
callq 0x1040
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x1c74(%rip), %xmm0     # 0x3004
movss %xmm0, 0x30(%rsp)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x13ae
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x13b9
movq %r14, %rdi
movq %rbx, %rdx
callq 0x2658
testb $0x1, %bpl
movl $0x0, %ebp
jne 0x1370
incq %r15
cmpq $0x3e8, %r15            # imm = 0x3E8
jne 0x1369
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x7db(%rip), %rax       # 0x1bba
movq %rax, 0x48(%rsp)
leaq 0x85f(%rip), %rax       # 0x1c4a
movq %rax, 0x40(%rsp)
movaps %xmm0, 0xa0(%rsp)
leaq 0x863(%rip), %rax       # 0x1c62
movq %rax, 0xb8(%rsp)
leaq 0x8c8(%rip), %rax       # 0x1cd6
movq %rax, 0xb0(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl $0xfa0, %edi            # imm = 0xFA0
callq 0x1110
movq %rax, 0x10(%rsp)
movq %rax, %rbx
addq $0xfa0, %rbx            # imm = 0xFA0
movq %rbx, 0x20(%rsp)
movl $0xfa0, %edx            # imm = 0xFA0
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %rbx, 0x18(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0x19ae
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq *0x18(%rsi)
movq 0x3b62(%rip), %rbx      # 0x4fd8
leaq 0x1be3(%rip), %rsi      # 0x3060
movl $0xc, %edx
movq %rbx, %rdi
callq 0x1130
movq 0x70(%rsp), %r15
movq 0x78(%rsp), %r12
cmpq %r12, %r15
je 0x14dc
leaq 0x50(%rsp), %r14
movq (%rbx), %rax
movq -0x18(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%r15), %xmm0
movq $0x3, 0x8(%rbx,%rax)
movq %rbx, %rdi
callq 0x1150
movb $0x20, 0x50(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1130
addq $0x4, %r15
cmpq %r12, %r15
jne 0x149e
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq 0x3ade(%rip), %rdi      # 0x4fd8
callq 0x1030
movq %rax, %rdi
callq 0x10b0
xorl %r13d, %r13d
leaq 0x120(%rsp), %r14
leaq 0x70(%rsp), %r15
leaq 0x108(%rsp), %r12
leaq 0xa0(%rsp), %rbx
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
callq 0x1f0a
movq 0x88(%rsp), %rsi
addq %r13, %rsi
movq %r12, %rdi
callq 0x1f0a
cmpq $0x0, 0xb0(%rsp)
je 0x19a9
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq *0xb8(%rsp)
movq 0x10(%rsp), %rax
movss %xmm0, (%rax,%rbp,4)
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x158d
movq 0x118(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x120(%rsp), %rdi
testq %rdi, %rdi
je 0x15aa
movq 0x130(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
incq %rbp
addq $0x18, %r13
cmpq $0x3e8, %rbp            # imm = 0x3E8
jne 0x1529
xorps %xmm0, %xmm0
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
addss (%rax,%rcx,4), %xmm0
incq %rcx
cmpq $0x3e8, %rcx            # imm = 0x3E8
jne 0x15c8
divss 0x1a27(%rip), %xmm0     # 0x3008
xorps %xmm2, %xmm2
xorl %ecx, %ecx
movss (%rax,%rcx,4), %xmm1
subss %xmm0, %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm2
incq %rcx
cmpq $0x3e8, %rcx            # imm = 0x3E8
jne 0x15e6
movss %xmm2, 0x2c(%rsp)
movq 0x39c8(%rip), %rdi      # 0x4fd8
leaq 0x1a56(%rip), %rsi      # 0x306d
movl $0x4, %edx
callq 0x1130
movss 0x2c(%rsp), %xmm0
divss 0x19d9(%rip), %xmm0     # 0x3008
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x163d
sqrtss %xmm0, %xmm0
jmp 0x1642
callq 0x1180
cvtss2sd %xmm0, %xmm0
movq 0x398b(%rip), %rdi      # 0x4fd8
callq 0x1150
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10b0
movq $0x0, 0x58(%rsp)
movl $0x18, %edi
callq 0x1110
movq %rbx, (%rax)
leaq 0x88(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, 0x50(%rsp)
leaq 0x63b(%rip), %rax       # 0x1cee
movq %rax, 0x68(%rsp)
leaq 0x787(%rip), %rax       # 0x1e46
movq %rax, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
leaq 0x7d4(%rip), %rax       # 0x1eaa
movq %rax, 0xd8(%rsp)
leaq 0x80d(%rip), %rax       # 0x1ef2
movq %rax, 0xd0(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x24e0
leaq 0x158(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x253e
leaq 0x138(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x259c
leaq 0xe8(%rsp), %rdi
leaq 0x178(%rsp), %rsi
leaq 0x158(%rsp), %rdx
leaq 0x138(%rsp), %r8
movl $0x1, %ecx
movl $0xa, %r9d
pushq $0x32
pushq $0x186a0                # imm = 0x186A0
callq 0x1f8a
addq $0x10, %rsp
movq 0x148(%rsp), %rax
testq %rax, %rax
je 0x1780
leaq 0x138(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x168(%rsp), %rax
testq %rax, %rax
je 0x179f
leaq 0x158(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x188(%rsp), %rax
testq %rax, %rax
je 0x17be
leaq 0x178(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x3813(%rip), %rbx      # 0x4fd8
leaq 0x18a6(%rip), %rsi      # 0x3072
movl $0x10, %edx
movq %rbx, %rdi
callq 0x1130
movq 0xf0(%rsp), %r15
movq 0xf8(%rsp), %r12
cmpq %r12, %r15
je 0x1831
leaq 0xf(%rsp), %r14
movq (%rbx), %rax
movq -0x18(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%r15), %xmm0
movq $0x3, 0x8(%rbx,%rax)
movq %rbx, %rdi
callq 0x1150
movb $0x20, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1130
addq $0x4, %r15
cmpq %r12, %r15
jne 0x17f3
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq 0x3789(%rip), %rdi      # 0x4fd8
callq 0x1030
movq %rax, %rdi
callq 0x10b0
movq 0x3775(%rip), %rdi      # 0x4fd8
leaq 0x1819(%rip), %rsi      # 0x3083
movl $0x5, %edx
callq 0x1130
movss 0xe8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x188b
sqrtss %xmm0, %xmm0
jmp 0x1890
callq 0x1180
cvtss2sd %xmm0, %xmm0
movq 0x373d(%rip), %rdi      # 0x4fd8
callq 0x1150
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10b0
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x18e7
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x1906
leaq 0xc0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x191f
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x1939
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1950
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x196f
leaq 0xa0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rax
testq %rax, %rax
je 0x1988
leaq 0x30(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x88(%rsp), %rdi
callq 0x25fa
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1070
callq 0x1070
jmp 0x1b9a
jmp 0x1b9a
jmp 0x1b9a
jmp 0x1b9a
jmp 0x1b9a
jmp 0x1b9a
jmp 0x1b9a
movq %rax, %rbx
movq 0x148(%rsp), %rax
testq %rax, %rax
je 0x1a02
leaq 0x138(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1a02
jmp 0x1b9a
movq %rax, %rbx
movq 0x168(%rsp), %rax
testq %rax, %rax
je 0x1a2b
leaq 0x158(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1a2b
jmp 0x1b9a
movq %rax, %rbx
movq 0x188(%rsp), %rax
testq %rax, %rax
je 0x1aec
leaq 0x178(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1aec
jmp 0x1b9a
movq %rax, %rbx
jmp 0x1aec
jmp 0x1b2a
movq %rax, %rbx
jmp 0x1b5e
movq %rax, %rbx
jmp 0x1b47
jmp 0x1b2a
jmp 0x1b2a
jmp 0x1acc
jmp 0x1b2a
movq %rax, %rbx
jmp 0x1aad
jmp 0x1a8d
movq %rax, %rbx
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x1aad
movq 0x118(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x120(%rsp), %rdi
testq %rdi, %rdi
je 0x1b2d
movq 0x130(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1b2d
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x1aec
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x1b0b
leaq 0xc0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x1b2d
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1b2d
jmp 0x1b9a
jmp 0x1b9a
movq %rax, %rbx
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x1b47
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1b5e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x1b7d
leaq 0xa0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x40(%rsp), %rax
testq %rax, %rax
je 0x1ba5
leaq 0x30(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1ba5
jmp 0x1b9a
movq %rax, %rdi
callq 0x264d
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x25fa
movq %rbx, %rdi
callq 0x1170