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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cassert>
#include <ctime>
using namespace std;
long int **read_matrix(ifstream &input, long int n);
double ran01(long int *idum);
long int *generate_random_vector(long int n);
unsigned long int compute_evaluation_function(long int **, long int **, long int *, long int n);
void first_2_opt_symmetric ( long int *, long int, long int **, long int **);
void swap(long int pos_1, long int pos_2, long int *);
void perturbation(long int *p, long int pert_size, long int **, long int **);
long int *p;
long int **d;
long int **f;
long int n;
long int i;
long int j;
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
int main()
{
string name;
long int optimum;
ifstream infile;
long int current_best_eval;
long int *best_p;
long int new_eval;
long int current_eval;
long int actual_solution_value;
current_best_eval = INT_MAX;
long int k;
infile.open("nug12.dat");
cout << "Name: ";
infile >> name;
cout << name << "\n";
cout << "Rows and Columns: ";
infile >> n;
cout << n << "\n";
cout << "Optimum solution: ";
infile >> optimum;
cout << optimum<<"\n";
long int **d=read_matrix(infile, n);
cout << endl;
cout << endl;
long int **f=read_matrix(infile, n);
cout << endl;
p=generate_random_vector(n);
cout << "Objective function value= ";
compute_evaluation_function(d, f, p, n);//initial solution
cout <<"\n";
first_2_opt_symmetric (p, n, d, f);//permutation
cout <<"\n";
compute_evaluation_function(d, f, p, n);
cout <<"\n";
for (i = 0; i < 100; i++) //ILS loop
{
cout << "iteration = " << i << " ";
perturbation(p, 3, d, f);
cout << endl;
first_2_opt_symmetric (p, n, d, f);
cout << endl;
new_eval = compute_evaluation_function(d, f, p, n);
cout << endl << "new eval = " << new_eval << endl;
cout <<"\n";
if (new_eval < current_best_eval)
{
current_best_eval = new_eval;
*best_p=*p;
}
}
cout << "current best eval = " << current_best_eval << endl;
return 0;
}
long int **read_matrix(ifstream &input, long int n)
{
long int **matrix = new long int *[n];
for (i = 0; i < n; i++)
{
matrix[i] = new long int[n];
for (j = 0; j < n; j++)
{
if( !(input >> matrix[i][j]) )
{
cerr << "Error reading at " << i << j << endl;
exit(1);
}
}
}
return matrix;
}
double ran01(long int *idum)
{
long k;
double ans;
k =(*idum)/IQ;
*idum = IA * (*idum - k * IQ) - IR * k;
if (*idum < 0 )
{
*idum += IM;
}
ans = AM * (*idum);
return ans;
}
long int *generate_random_vector(long int n)
{
long int i, j, help;
long int *v;
srand(time(0));
long int seed=rand();
v = new long int( n * sizeof(long int) );
for ( i = 0 ; i < n; i++ )
{
v[i] = i;
}
for ( i = 0 ; i < n-1 ; i++)
{
j = (long int) ( ran01( &seed ) * (n - i));
assert( i + j < n );
help = v[i];
v[i] = v[i+j];
v[i+j] = help;
}
return v;
}
unsigned long int compute_evaluation_function(long int **d, long int **f, long int *p, long int n)
{
unsigned long obj_f_value;
obj_f_value = 0;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
obj_f_value += d[i][j] * f[p[i]][p[j]];
}
}
cout << obj_f_value;
return obj_f_value;
}
void first_2_opt_symmetric (long int *q, long int n, long int **d, long int **f)
{
long int improvement = 1;
long int improve_item = 0;
register long int u, v, k;
register long int tmp;
long int *x;
improvement = 1;
x = generate_random_vector(n);
while (improvement)
{
improvement = 0;
for ( i = 0 ; i < n ; i++ )
{
u = x[i];
improve_item = 0;
for ( j = 0 ; j < n ; j++ )
{
v = x[j];
if (u == v)
continue;
tmp = 0;
for ( k = 0 ; k < n ; k++ )
{
if ( (k != u) && (k != v) )
{
tmp += (d[u][k] - d[v][k]) * (f[q[v]][q[k]] - f[q[u]][q[k]]);
}
}
if (tmp < 0)
{
//cout <<"improve"<<"\n";
improvement = 1;
improve_item = 1;
swap (u, v, q);
}
}
}
}
delete (x);
}
void swap(long int pos_1, long int pos_2, long int *p)
{
long int tmp;
tmp = p[pos_1];
p[pos_1] = p[pos_2];
p[pos_2] = tmp;
}
void perturbation(long int *p, long int pert_size, long int **d, long int **f)
{
long int hold_value[n]; //allocate memory for items to be chosen in move/*
int chosen[pert_size]; //*allocate temporary memory to determine items to be moved */*
for(i = 0; i < n; i++)
{
hold_value[i] = p[i]; //initialize hold_value with the same value from local search/*
}
int j = n - 1;
int rand_number;
int rand_no;
for(i = 1; i <= pert_size; i++)
{
rand_number = rand();
rand_no = rand_number % j; //choose random number from 1 to size - 1/
chosen[i - 1] = rand_no + i; //copy the value to chosen[i]
}
long int temp;
for(i = 0; i < pert_size; i++)
{
temp = chosen[i];
swap(i, temp, hold_value); //swap chosen[i] and hold_value[i]; n first item in hold_value[i] is the index array that will be included in perturbation/
}
long int temp1;
long int temp2;
temp1 = p[hold_value[1]];//
temp2 = p[hold_value[0]];
for(i = 0; i < pert_size - 1; i++)
{
p[hold_value[i + 1]] = temp2;
temp2 = temp1;
temp1 = p[hold_value[i + 2]];
}
p[hold_value[0]] = temp2;
cout <<endl;
}
|