using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;
namespace TPOpt
{
class QAP
{ /* Taboo search procedure for asymmetrical quadratic assignment problems */
/* Author : Eric Taillard */
/* Date : 94/11/23 */
/* Implementation used in : E. Taillard, "Comparison of iterative searches */
/* for the quadratic assignment problem", Publication 989, Center for */
/* research on transportation, University of Montreal, Montreal, 1994 */
const int infinite = 999999999;
int n_max;
int[,] a , b ;
int[] solution, best_solution;
double scout,cpu,cpu1,cpu2,cpusum,cpusum2,t2;
int cost, i, n,init_sol_seed, meil_cout,
no_res, nb_res, optimum, nb_iterations; /* problem size */
int best_cost, aspirating_iteration, nr_iteration_before_aspiration;
int taboo_list_seed,current_taboo_list_size,lower_taboo_list_size, higher_taboo_list_size;
int[,] taboo_list;
DateTime t0;
public QAP()
{
}
public QAP (int n, int[,] d, int[,] f)
{ this.n = n;
a = d;
b = f;
optimum = 0;
n_max = n;
solution = new int[n];
best_solution = new int[n];
nb_iterations=n*2048; /* di TS */
nb_res=1; /* ripetizioni sullo stesso prob */
}
///
/// Distribuzione uniforme fra low e high
///
///
///
///
///
private int unif(int seed, int low, int high)
{ int m = 2147483647, a = 16807, b = 127773, c = 2836;
int kl;
double value_0_1; // real double precision (coded on 64 bits)
kl = seed / b;
seed = a * (seed % b) - kl * c ;
if (seed < 0) seed = seed + m ;
value_0_1 = seed/(1.0*m);
int res = low + (int) Math.Truncate((high - low + 1) * value_0_1);
return res;
}
private void read_problem()
{
int i,j;
StreamReader F = new StreamReader("NUG08.DAT");
string line;
string[] elem;
char[] separators = new char[]{' '};
optimum = Convert.ToInt32( F.ReadLine() );
F.ReadLine();
n_max = n = Convert.ToInt32( F.ReadLine() );
a = new int[n_max, n_max];
b = new int[n_max, n_max];
solution = new int[n_max];
best_solution = new int[n_max];
F.ReadLine();
nb_iterations=n*2048; /* di TS */
Trace.WriteLine(n+" "+optimum+" "+nb_res+" "+nb_iterations);
nb_res=1; /* ripetizioni sullo stesso prob */
for (i = 0; i best solution */
ref int cost, /* -> cost of the best solution */
int optimum,
int lower_taboo_list_size,
int higher_taboo_list_size,
int nr_iteration_before_aspiration, /* parameters */
int nr_iterations)
{ int current_iteration, current_taboo_list_size,
aspirating_iteration, taboo_list_seed;
taboo_list = new int[n_max,n_max];
int[,] delta = new int[n_max,n_max]; /* value of the difference between two solutions */
/* improve qap solution */
initialize(taboo_list,p,delta);
current_iteration = 0;
do
{ current_iteration = current_iteration+1;
perform_one_move(current_iteration,delta,p,lower_taboo_list_size,higher_taboo_list_size);
t2 = clock();
} while ((best_cost > optimum) && (t2<15));
cost = best_cost;
p = best_solution;
}
/* computes the difference of solution values if units u and v are permuted */
private void delta_full_computation(int i, int j, int[] p, int[,] delta)
{ int k, sum;
sum = 0;
for (k = 0;k= current_iteration) &&
(taboo_list[j,p[i]] >= current_iteration);
aspired = ((taboo_list[i,p[j]] < aspirating_iteration) &&
(taboo_list[j,p[i]] < aspirating_iteration)) ||
((cost + delta[i,j] < best_cost) && taboo);
if (((delta[i,j] < delta_min) && !taboo) || aspired)
{
u = i;
v = j;
if (aspired)
delta_min = -infinite;
else
delta_min = delta[i,j];
}
}
}
private void perform_one_move(int current_iteration, int[,] delta, int[] p, int lower_taboo_list_size, int higher_taboo_list_size)
{
int i, j, u=0, v=0;
find_best_move(ref u,ref v,current_iteration,delta,p);
if (u != infinite)
{
cost = cost + delta[u,v];
taboo_list[u,p[u]] = current_iteration + current_taboo_list_size;
taboo_list[v,p[v]] = current_iteration + current_taboo_list_size;
swap(ref p[u], ref p[v]);
}
/* else trace.WriteLine('no allowed move !'); */
if (cost < best_cost)
{
best_cost = cost;
for(i=0;i Repetition n."+no_res);
cpu1=clock();
Trace.WriteLine("cpu1="+cpu1);
for (i = 0;i0)
Trace.WriteLine("(1) cpu="+cpu+" cpu2="+cpu2+" cpu1="+cpu1+" cost:"+cost);
cpusum=cpusum+cpu;
scout = scout + cost;
if(cost