Example #1
0
Solution Solver::solve(const Instance& tsp, Population& init_pop)
{
    //set_phase(1);
    bool stop = false;
    Population& training_population = init_pop;
    Solution best_sol = init_pop.best_solution();
    int max_no_imp = tsp.nodes_card*2;

    while (!stop && iterations < max_tot_iterations) {
        Population next_gen = advance_generation(best_sol, training_population, tsp);
//        cout << "### "<< iterations << "-th population ###" << endl;
//        next_gen.print();
        Solution local_best_sol = next_gen.best_solution();

//        cout << "best sol:\t\t";
//        best_sol.print();
//        cout << "current best sol:\t";
//        local_best_sol.print();

        if (local_best_sol.evaluate() < best_sol.evaluate()) {
            best_sol = local_best_sol;
            set_phase(1);
            iterations_without_improvement = 0;
        }
        else {
            iterations_without_improvement++;
        }
        iterations++;

        training_population = next_gen;


        if (iterations_without_improvement > max_no_imp)
            stop = true;

        if (iterations_without_improvement > max_no_imp-1)
            set_phase(0);
    }

//    cout << "### "<< iterations << "-th population ###" << endl;
    return best_sol;
}