#ifndef SCRIPTS2_H_INCLUDED
#define SCRIPTS2_H_INCLUDED

#include <vector>
#include <fstream>

#include "analysis.h"
#include "ParameterOptimizer.h"

void cmp_evolution(std::vector<SearchHeuristic*> algos, MasterMind& problem, int max_evolution, int nb_tests = 100) {
    int nb_algos = algos.size();
    Statistic** values = new Statistic*[nb_algos];
    for(int i=0; i<nb_algos; i++) {
        algos[i]->set_problem(&problem);
        values[i] = avg_evolution(algos[i], max_evolution, nb_tests);
    }
    std::ofstream statsfile("stats.csv");
    for(int i=0; i<max_evolution; i++) {
        statsfile << i << ";";
        for(int k=0; k<nb_algos; k++)
            statsfile << values[k][i].average << ";";
        statsfile << std::endl;
    }

    for(int i=0; i<nb_algos; i++)
        delete[] values[i];
    delete[] values;
}

/// Max Lambda
class LambdaOptimizer : public ParameterOptimizer {
    public:
        LambdaOptimizer(std::default_random_engine& randomizer) : ParameterOptimizer(randomizer) {};
        SearchHeuristic* get_context(unsigned int i, double parameter, std::default_random_engine& randomizer) {
            unsigned int n=20;
            EA* algo = new EA(1+i, (int)parameter, PLUS, 1./n, randomizer);
            MasterMind* mm = new MasterMind(n, n, randomizer);
            algo->set_problem(mm);
            return algo;
        }
        virtual std::string display_context(unsigned int context) {
           std::stringstream ss;
           ss << "mu=" << context+1;
           return ss.str();
        }
};

void optimal_lambda(int n, int mu_initial, int mu_final, std::default_random_engine& randomizer, int lambda_init, int lambda_final, int lambda_step=1, int nb_tests=10, double student_law_quartile=-1, bool displaying=true) {
    LambdaOptimizer lo(randomizer);
    lo.set_nb_contexts(50);
    lo.set_study_interval(lambda_init, lambda_final, lambda_step);
    lo.add_test_wave(nb_tests, student_law_quartile);
    lo.run();
}

class MuLambdaOptimizer : public ParameterOptimizer {
    public:
        MuLambdaOptimizer(int n_init, std::default_random_engine& randomizer) : ParameterOptimizer(randomizer), n_initial{n_init} {};
        SearchHeuristic* get_context(unsigned int i, double parameter, std::default_random_engine& randomizer) {
            EA* algo = new EA((int)parameter, (int)parameter, PLUS, 1./(n_initial+i), randomizer);
            MasterMind* mm = new MasterMind(n_initial+i, n_initial+i, randomizer);
            algo->set_problem(mm);
            return algo;
        }
        virtual std::string display_context(unsigned int context) {
           std::stringstream ss;
           ss << "n=" << n_initial+context;
           return ss.str();
        }
    private:
        int n_initial;
};

void optimal_mulambda(int initial_n, int final_n, std::default_random_engine& randomizer, int par_init, int par_final, int par_step=1, int nb_tests=10, double student_law_quartile=-1, bool displaying=true) {
    MuLambdaOptimizer lo(initial_n, randomizer);
    lo.set_nb_contexts(final_n - initial_n);
    lo.set_study_interval(par_init, par_final, par_step);
    lo.add_test_wave(nb_tests, student_law_quartile);
    lo.run();
}

class CrossoverProbaOptimizer : public ParameterOptimizer {
    public:
        CrossoverProbaOptimizer(int n_init, double obj, std::default_random_engine& randomizer) : ParameterOptimizer(randomizer), n_initial{n_init}, objective{obj} {};
        SearchHeuristic* get_context(unsigned int i, double parameter, std::default_random_engine& randomizer) {
            GA* algo = new GA(5, 5, PLUS, 1./(n_initial+i), parameter, UNIFORM_CROSSOVER, GA_V1, randomizer);
            MasterMind* mm = new MasterMind(n_initial+i, n_initial+i, randomizer);
            algo->set_problem(mm);
            algo->set_objective(objective);
            return algo;
        }
        std::string display_context(unsigned int context) {
           std::stringstream ss;
           ss << "n=" << n_initial+context;
           return ss.str();
        }
        std::string get_context_for_csvfile(unsigned int num_context) {
           return std::to_string(n_initial+num_context);
        }
    private:
        int n_initial;
        double objective;
};

void script_crossover_proba_optim(std::default_random_engine& randomizer) {
    std::string namefile = "crossover-proba.csv";
    ofstream savefile(namefile);
    {
        int n_init=3, n_final=21;
        double p_init=0.01, p_final=0.99, p_step=0.02;
        CrossoverProbaOptimizer cpo(n_init, 1.00, randomizer);
        cpo.set_nb_contexts(n_final - n_init);
        cpo.set_study_interval(p_init, p_final, p_step);
        cpo.add_test_wave(40, 2.5419828669, 0);
        cpo.add_test_wave(400, 2.4429370465, 1);
        cpo.add_test_wave(2000, 2.4344837209, 2);
        cpo.save_run(namefile);
        cpo.run();
    }{
        int n_init=21, n_final=40;
        double p_init=0.01, p_final=0.3, p_step=0.02;
        CrossoverProbaOptimizer cpo(n_init, 1.00, randomizer);
        cpo.set_nb_contexts(n_final - n_init);
        cpo.set_study_interval(p_init, p_final, p_step);
        cpo.add_test_wave(80, 2.4860491262, 0);
        cpo.add_test_wave(200, 2.4535822177, 1);
        cpo.add_test_wave(400, 2.4429370465, 2);
        cpo.save_run(namefile);
        cpo.run();
    }
}

#endif // SCRIPTS2_H_INCLUDED
