hello everyone
i have been doing a problem on quadratic assignment problem
i got a problem on the answer of the coding..
i have a set of answer, the first answer it appear correct however the 2nd until 10 answer its calculate double..
i already set the result to 0 in the array however it still calculate wrong..
please help me
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
|
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdio>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//declare function
void input(); //assign all inout data into array
void initial_solution();
//declare variables & files
//const int m = 12; //no of flow n distant
const int pop_size = 12;
const int no_of_facloc = 12;
vector< vector <long double>> flow (no_of_facloc, vector <long double>
(no_of_facloc,0));
vector< vector <long double>> dist (no_of_facloc, vector <long double>
(no_of_facloc,0));
vector< vector <long double>> Result (pop_size, vector <long double>
(no_of_facloc,0));
vector< vector <long double>> x (no_of_facloc, vector <long double>
(no_of_facloc,0));
vector <long double> facility (no_of_facloc);
//vector <long double> objFunction (no_of_facloc);
//vector <long double> loc (no_of_facloc);
int i, k, j, q, p, z;
int temp_loc, same_temp_loc, loc;
long double objFunction;
ofstream outResult ("Result12rou.txt"); //write result
void input()
{
//open input file
ifstream inpFlow ("flow12rou.txt"); //read flow data
ifstream inpDist ("dist12rou.txt"); //read dist data
//assign flow into array
for ( i = 0; i < no_of_facloc; i++ )
{
for ( k = 0; k < no_of_facloc; k++ )
{
inpFlow >> flow[i][k];
//cout << "flow ["<<i<<"]["<<k<<"]="<<flow [i][k]
<<endl;
}
}
//assign distant into array
for ( j = 0; j < no_of_facloc; j++ )
{
for ( q = 0; q < no_of_facloc; q++ )
{
inpDist >> dist[j][q];
//cout << "dist ["<<j<<"]["<<q<<"]="<<dist [j][q]
<<endl;
}
}
}
//find possible x
void pointer_x()
{
for (i = 0; i<no_of_facloc; i++)
{
loc = facility[i];
x[i][loc]= 1; // assign the facility i and loc i to
x
}
}
//generate objective function
void obj_function()
{
objFunction=0;
int sum=0;
for (i = 0; i<no_of_facloc; i++)
{
for ( k = 0; k < no_of_facloc; k++ )
{
for ( j = 0; j < no_of_facloc; j++ )
{
for (q = 0; q < no_of_facloc;
q++)
{
sum = flow[i][k] * dist[j][q] * x[i][j] * x[k][q];
objFunction = objFunction + sum;
//objFunction = sum;
//if (sum > 0)
// objFunction = objFunction;
}
}
}
}
cout << objFunction<< ' ';
cout << endl;
//objFunction=0;
}
// generate inital solution
void initial_solution()
{
for ( p = 0; p < pop_size; p++ ) //for each population size 1 until n
{
for ( i = 0; i<no_of_facloc; i++) //for each facility [i]
until size
{
//obj_function;
{
check:
temp_loc = rand() % no_of_facloc; //assignt
facility to temporary random location
same_temp_loc = 0;
//chek for each facility whitch has been assign to
for (j = 0; j<i; j++)
{
if(facility[j] == temp_loc)
{
same_temp_loc = 1;
goto check;
}
}
//check if same, repeat; if not, assign
if (same_temp_loc == 0)
{
facility[i] = temp_loc; //assign facility
to temp_lock
}
cout << setw(3) << facility[i] << ' ';
//outResult << setw(3) << facility[i] << ' ';
}
}
pointer_x();
obj_function();
//outResult << endl;
cout<< endl;
}
//obj_function;
//cout << setw(3) << obj_function()<< ' ';
//cout<< endl;
}
int main()
{
input();
srand(time(NULL));
initial_solution();
//pointer_x();
//obj_function();
//Read_Result();
system("pause");
return 0;
}
| |
the objective function is where it calculate the result
this is the example of the result i get..
only the 1st one is correct
10 3 4 9 2 11 7 8 0 1 5 6 =52410
5 11 10 7 1 0 2 6 4 8 9 3 =145936
10 3 6 7 2 11 5 0 4 9 8 1 =228342
9 10 11 8 6 1 0 2 7 3 5 4 =466138
9 1 7 3 11 6 2 4 10 5 0 8 =747670
9 10 6 8 2 11 1 0 4 5 7 3 =768470
11 7 2 8 4 10 9 6 1 5 3 0 =1.03283e+006
5 6 7 2 0 3 8 11 4 10 9 1 =1.31354e+006
0 4 5 3 2 7 10 6 1 8 11 9 =1.63161e+006
4 6 10 9 3 7 5 8 11 1 0 2 =1.80093e+006
2 10 4 3 6 1 8 9 7 11 5 0 =2.08034e+006
9 1 3 0 2 4 10 11 5 8 6 7 =2.24092e+006