1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
assignment problem by simulated annealing.
*/
#include "wnlib.h"
#include "wnswap.h"
#include "wnabs.h"
#include "wnmat.h"
#define SIZE 200
/*
#define SIZE 50
*/
local int *permutation;
local double objective,**matrix;
main()
{
wn_gpmake("no_free");
initialize();
printf("size = %d\n",SIZE);
printf("start objective = %f\n",objective);
solve();
printf("end objective = %f\n",objective);
wn_gpfree();
}
local initialize()
{
extern double wn_flat_distribution();
int i,j;
wn_make_raw_matrix(&matrix,SIZE,SIZE);
permutation = (int *)wn_zalloc(SIZE*wn_sizeof(int));
wn_random_permutation(permutation,SIZE);
for(i=0;i<SIZE;++i)
{
for(j=0;j<SIZE;++j)
{
matrix[i][j] = wn_flat_distribution();
}
}
objective = 0;
for(i=0;i<SIZE;++i)
{
objective += matrix[i][permutation[i]];
}
}
/*
#define TIME 20000000000.0
#define TIME 10000000000.0
#define TIME 1800000000.0
#define TIME 360000000.0
#define TIME 1000000.0
*/
#define TIME 10000000000.0
local solve()
{
double delta_of_mutation(),temperature_function();
void accept_mutation();
double temperature;
printf("time = %f\n",(float)TIME);
wn_measure_anneal_temperature(&temperature,(delta_of_mutation),10000.0);
printf("temperature = %f\n",(float)temperature);
wn_anneal(
(delta_of_mutation),(accept_mutation),
(temperature_function),
TIME);
wn_measure_anneal_temperature(&temperature,(delta_of_mutation),10000.0);
printf("temperature = %f\n",(float)temperature);
}
local double temperature_function(time)
double time;
{
/*
return(1.00*wn_anneal_time*time);
return(0.20*wn_anneal_time*time);
return(0.20*wn_anneal_time*time + 0.05);
return(0.05);
*/
return(0.20*time*time);
}
local int index1,index2;
local double delta_score;
local double delta_of_mutation()
{
double eval_num_at_index();
index1 = wn_random_mod_int(SIZE);
index2 = wn_random_mod_int(SIZE);
compute_delta(&delta_score);
return(delta_score);
}
local compute_delta(pdelta_score)
double *pdelta_score;
{
*pdelta_score = matrix[index1][permutation[index2]] +
matrix[index2][permutation[index1]] -
matrix[index1][permutation[index1]] -
matrix[index2][permutation[index2]];
}
local accept_mutation()
{
wn_swap(permutation[index1],permutation[index2],int);
objective += delta_score;
}
|