// File: /workspace/github_cpp_makefile_star2_O2/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 %ebx, %ebx
xorl %edi, %edi
callq 0x10d0
movl %eax, %edi
callq 0x10e0
leaq 0x88(%rsp), %rdi
leaq 0x30(%rsp), %r14
movl $0x3e8, %esi            # imm = 0x3E8
movq %r14, %rdx
callq 0x1ae8
pushq $0x2
popq %rbp
cmpq $0x3e8, %rbx            # imm = 0x3E8
je 0x1344
imulq $0x18, %rbx, %r12
movl %ebp, %r13d
subl $0x1, %r13d
jb 0x133f
movq 0x88(%rsp), %r15
addq %r12, %r15
callq 0x182c
movss %xmm0, 0x30(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2238
jmp 0x1316
incq %rbx
jmp 0x1306
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x4ed(%rip), %rax       # 0x1840
movq %rax, 0x48(%rsp)
leaq 0x53b(%rip), %rax       # 0x189a
movq %rax, 0x40(%rsp)
movaps %xmm0, 0xc0(%rsp)
leaq 0x53f(%rip), %rax       # 0x18b2
movq %rax, 0xd8(%rsp)
leaq 0x5ae(%rip), %rax       # 0x1930
movq %rax, 0xd0(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x1b3a
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1b8c
movq 0x4c24(%rip), %r15      # 0x5fd8
leaq 0x1ca5(%rip), %rsi      # 0x3060
movq %r15, %rdi
callq 0x10f0
movq 0x58(%rsp), %rbx
movq 0x60(%rsp), %r14
pushq $0x20
popq %rbp
cmpq %r14, %rbx
je 0x1401
movss (%rbx), %xmm0
movq (%r15), %rax
movq -0x18(%rax), %rax
movq $0x3, 0x8(%r15,%rax)
movq %r15, %rdi
callq 0x10a0
movq %rax, %rdi
movl %ebp, %esi
callq 0x1120
addq $0x4, %rbx
jmp 0x13d0
movq 0x4bd0(%rip), %rdi      # 0x5fd8
callq 0x1080
xorl %ebx, %ebx
leaq 0x180(%rsp), %r12
leaq 0x58(%rsp), %r13
leaq 0x168(%rsp), %rbp
leaq 0xc0(%rsp), %r15
xorl %r14d, %r14d
cmpq $0x3e8, %r14            # imm = 0x3E8
je 0x1488
movq %r12, %rdi
movq %r13, %rsi
callq 0x1bb8
movq 0x88(%rsp), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0x1bb8
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1ba4
movq 0x70(%rsp), %rax
movss %xmm0, (%rax,%r14,4)
movq %rbp, %rdi
callq 0x23cc
movq %r12, %rdi
callq 0x23cc
incq %r14
addq $0x18, %rbx
jmp 0x142f
xorps %xmm0, %xmm0
movq 0x70(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x3e8, %rcx            # imm = 0x3E8
je 0x14a5
addss (%rax,%rcx,4), %xmm0
incq %rcx
jmp 0x1492
divss 0x1b57(%rip), %xmm0     # 0x3004
xorps %xmm1, %xmm1
movss %xmm1, 0xc(%rsp)
xorl %ecx, %ecx
cmpq $0x3e8, %rcx            # imm = 0x3E8
je 0x14e3
movss (%rax,%rcx,4), %xmm1
subss %xmm0, %xmm1
mulss %xmm1, %xmm1
movss 0xc(%rsp), %xmm2
addss %xmm1, %xmm2
movss %xmm2, 0xc(%rsp)
incq %rcx
jmp 0x14b8
movq 0x4aee(%rip), %rdi      # 0x5fd8
leaq 0x1b7c(%rip), %rsi      # 0x306d
callq 0x10f0
movq %rax, %r12
movss 0xc(%rsp), %xmm0
divss 0x1afd(%rip), %xmm0     # 0x3004
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x1515
sqrtss %xmm0, %xmm0
jmp 0x151a
callq 0x1160
movq %r12, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1080
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
pushq $0x18
popq %rdi
callq 0x1100
movq %r15, (%rax)
leaq 0x88(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, 0x10(%rsp)
leaq 0x3e5(%rip), %rax       # 0x1948
movq %rax, 0x28(%rsp)
leaq 0x4d1(%rip), %rax       # 0x1a40
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
leaq 0x51a(%rip), %rax       # 0x1aa0
movq %rax, 0xb8(%rsp)
leaq 0x53b(%rip), %rax       # 0x1ad0
movq %rax, 0xb0(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1ffc
leaq 0x108(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2042
leaq 0xe8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2088
leaq 0x148(%rsp), %rdi
leaq 0x128(%rsp), %rsi
leaq 0x108(%rsp), %rdx
pushq $0x1
popq %rcx
leaq 0xe8(%rsp), %r8
pushq $0xa
popq %r9
pushq $0x32
pushq $0x186a0                # imm = 0x186A0
callq 0x1c05
popq %rax
popq %rcx
leaq 0xe8(%rsp), %rdi
callq 0x20ce
leaq 0x108(%rsp), %rdi
callq 0x20ce
leaq 0x128(%rsp), %rdi
callq 0x20ce
movq 0x499f(%rip), %r14      # 0x5fd8
leaq 0x1a32(%rip), %rsi      # 0x3072
movq %r14, %rdi
callq 0x10f0
leaq 0x150(%rsp), %rbx
movq (%rbx), %r15
movq 0x8(%rbx), %r12
pushq $0x20
popq %rbp
cmpq %r12, %r15
je 0x168c
movss (%r15), %xmm0
movq (%r14), %rax
movq -0x18(%rax), %rax
movq $0x3, 0x8(%r14,%rax)
movq %r14, %rdi
callq 0x10a0
movq %rax, %rdi
movl %ebp, %esi
callq 0x1120
addq $0x4, %r15
jmp 0x165a
movq 0x4945(%rip), %rdi      # 0x5fd8
callq 0x1080
movq 0x4939(%rip), %rdi      # 0x5fd8
leaq 0x19dd(%rip), %rsi      # 0x3083
callq 0x10f0
movq %rax, %r14
movss 0x148(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x16c5
sqrtss %xmm0, %xmm0
jmp 0x16ca
callq 0x1160
movq %r14, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x23cc
leaq 0xa0(%rsp), %rdi
callq 0x20ce
leaq 0x10(%rsp), %rdi
callq 0x20ce
leaq 0x58(%rsp), %rdi
callq 0x23cc
leaq 0x70(%rsp), %rdi
callq 0x23cc
leaq 0xc0(%rsp), %rdi
callq 0x20ce
leaq 0x30(%rsp), %rdi
callq 0x20ce
leaq 0x88(%rsp), %rdi
callq 0x20ea
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x20ce
jmp 0x175a
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x20ce
jmp 0x176c
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x20ce
jmp 0x17cb
movq %rax, %rbx
jmp 0x17cb
movq %rax, %rbx
jmp 0x17d8
movq %rax, %rbx
jmp 0x17f1
movq %rax, %rbx
jmp 0x17fb
jmp 0x17e4
jmp 0x17e4
jmp 0x17bb
jmp 0x17e4
movq %rax, %rbx
jmp 0x17ac
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x23cc
leaq 0x180(%rsp), %rdi
callq 0x23cc
jmp 0x17e7
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x23cc
leaq 0xa0(%rsp), %rdi
callq 0x20ce
leaq 0x10(%rsp), %rdi
callq 0x20ce
jmp 0x17e7
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x23cc
leaq 0x70(%rsp), %rdi
callq 0x23cc
leaq 0xc0(%rsp), %rdi
callq 0x20ce
leaq 0x30(%rsp), %rdi
callq 0x20ce
jmp 0x1817
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x20ea
movq %rbx, %rdi
callq 0x1150