Пример #1
0
/** Allocates an array of solution
* nsol is the number of solutions. Its number will be population size
* nbj is the number of objectives
*/
solution_t * allocate_solution(const int *nsol, const int *nbj){
	solution_t * solution_aux;	
	solution_aux = Malloc(solution_t,*nsol);
	//Each solution
	for (int s = 0; s < *nsol; s++){
		initialize_solution(&solution_aux[s], nbj);
	}
	return solution_aux;
}
Пример #2
0
main()
{
	tsp_instance t;			/* tsp points */
	tsp_solution s;			/* tsp solution */
	double initCost, finCost;

	read_tsp(&t);
	initialize_solution(t.n, &s);
	initCost=solution_cost(&s,&t);
	printf("solution_cost = %7.1f\n",initCost);
	print_solution(&s);


	solution_count=0;
	repeated_annealing(&t,5,&s);
	finCost = solution_cost(&s,&t);
	printf("repeated annealing %d iterations, cost = %7.1f    improvement %%%2.0f\n", solution_count, finCost, (finCost/initCost)*100);
	print_solution(&s);

}
Пример #3
0
main()
{
	tsp_instance t;			/* tsp points */
	tsp_solution s;			/* tsp solution */
	int i;				/* counter*/
	double solution_cost();

	read_tsp(&t);
	/*print_tsp(&t);*/

	read_solution(&s);
	printf("OPTIMAL SOLUTION COST = %7.1f\n",solution_cost(&s,&t));
        print_solution(&s);


	initialize_solution(t.n, &s);
	printf("solution_cost = %7.1f\n",solution_cost(&s,&t));
	print_solution(&s);

	
/*
	solution_count=0;
	random_sampling(&t,1500000,&s);
        printf("random sampling %d iterations, cost = %7.1f\n",
			solution_count,solution_cost(&s,&t));
       	print_solution(&s);

	solution_count=0;
        repeated_hill_climbing(&t,195,&s);
        printf("repeated hill climbing %d iterations, cost = %7.1f\n",
                        solution_count,solution_cost(&s,&t));
        print_solution(&s);
*/

	solution_count=0;
	repeated_annealing(&t,3,&s);
	printf("repeated annealing %d iterations, cost = %7.1f\n",
                        solution_count,solution_cost(&s,&t));
	print_solution(&s);

}
void anneal(instance *t, solution *s)
{
    double temperature = INITIAL_TEMPERATURE;

    initialize_solution(t->n, s);
    double current_value = solution_cost(s, t);

    for (int i = 1; i <= COOLING_STEPS; i++)
    {
        temperature *= COOLING_FRACTION;

        double start_value = current_value;

        for (int j = 1; j <= STEPS_PER_TEMP; j++)
        {
            int i1 = random_int(1, t->n);
            int i2 = random_int(2, t->n);

            double flip = random_float(0, 1);

            double delta = transition(s, t, i1, i2);
            double exponent = (-delta / current_value) / (K * temperature);
            double merit = pow(E, exponent);

            if (delta < 0)
                current_value = current_value + delta;
            else
                if (merit > flip)
                    current_value = current_value + delta;
                else
                    transition(s, t, i1, i2);
        }

        if ((current_value - start_value) < 0.0)
            temperature = temperature / COOLING_FRACTION;
    }
}