void solution_count_update(FPTREE * solution, NET* net_arr, MODULE * module_arr) { static solution_count ; solution_count = solution_count+1; if ((solution_count % PRINT_FREQUENCY) == 0){ printf("%d %ld\n",solution_count,solution_cost(solution, net_arr, module_arr)); } }
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); }
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; } }
void anneal(NET * net_arr, MODULE * module_arr ){ int i1, i2; /* pair of items to swap */ int i,j; /* counters */ double temperature; /* the current system temp */ long current_value; /* value of current state */ long start_value; /* value at start of loop */ int delta; /* value after swap */ double merit, flip; /* hold swap accept conditions*/ double exponent; /* exponent for energy funct*/ FPTREE * tempnodes[3 * OPERATOR_LIMIT]; // double random_float(); //double solution_cost(); //double transition(); temperature = INITIAL_TEMPERATURE; current_value = solution_cost(solution, net_arr, module_arr); for (i=1; i<=COOLING_STEPS; i++) { temperature *= COOLING_FRACTION; start_value = current_value; for (j=1; j<=STEPS_PER_TEMP; j++) { current_value = solution_cost(solution, net_arr, module_arr); fprintf(stderr, "current value: %ld\n", current_value); int randint1 = random_int(0, 196); int type = (nodes[randint1]->operator == 0 ? 1 : 0); delta = transition( net_arr, module_arr, randint1 ); // fprintf(stderr, "delta: %d\n", delta); flip = rand()/(RAND_MAX - 1.0);//random_float(0.0,1.0); exponent = (-delta)/(K * temperature); merit = pow(E,exponent); /*printf("merit = %f flip=%f exponent=%f\n",merit,flip,exponent); */ /*if (merit >= 1.0) merit = 0.0;*/ /* don't do unchanging swaps*/ if(delta < 0) { /*ACCEPT-WIN choose a better solution*/ //current_value = current_value + delta; if (TRACE_OUTPUT) { fprintf(stderr, "swap WIN %d value %ld temp=%f \n", delta, solution_cost(solution,net_arr, module_arr),temperature); fprintf(stderr, "\n\n"); } }else{if(merit >= flip){ /*ACCEPT-LOSS choose a worse solution*/ //current_value = current_value+delta; if (TRACE_OUTPUT) { fprintf(stderr, "swap LOSS %d value %ld merit=%f flip=%f\n", delta,solution_cost(solution,net_arr, module_arr) , merit, flip) ; fprintf(stderr, "\n\n") ; } }else{ /* REJECT */ if(type == 0){ int i ; for(i = randint1; ; i--){ if(nodes[i] == NULL || nodes[i]->operator == 0){ break; }else{ if(nodes[i]->operator == 'H'){ nodes[i]->operator = 'V'; }else{if(nodes[i]->operator == 'V'){ nodes[i]->operator = 'H'; }} } } for(i = randint1; ; i++){ if(nodes[i] == NULL || nodes[i]->operator == 0){ break; }else{ if(nodes[i]->operator == 'H'){ nodes[i]->operator = 'V'; }else{if(nodes[i]->operator == 'V'){ nodes[i]->operator = 'H'; }} } } }else{if(type == 1){ FPTREE * temp ; temp = nodes[randint1] ; nodes[randint1] = nodes[randint1 + 1]; nodes[randint1 + 1] = temp ; }} solution = list2tree(nodes); iter_update_tree(solution); iter_update_module(0, 0, solution, module_arr); }} solution_count_update(solution, net_arr, module_arr) ; } if((current_value-start_value) > 0.0){ // rerun at this temp temperature /= COOLING_FRACTION; if (TRACE_OUTPUT) {printf("rerun at temperature %f\n",temperature);} } } }