quadratic assignment problem
Jan 25, 2017 at 7:04am UTC
i got this coding but i cannot run it because of localserver.h, then i search for it and found but, but it doesn't run either..does anyone here know about local server?
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
/********** qap.cpp **********/
#include <iostream>
#include <fstream>
#include <vector>
#include "localsolver.h"
using namespace localsolver;
using namespace std;
class Qap {
public :
/* Number of points */
int n;
// Distance between locations
vector<vector<int > > A;
// Flow between facilites
vector<vector<lsint> > B;
/* Solver. */
LocalSolver localsolver;
/* LS Program variables */
LSExpression p;
/* Objective */
LSExpression obj;
/* Reads instance data */
void readInstance(const string& fileName){
ifstream infile(fileName.c_str());
if (!infile.is_open()) {
cerr << "File " << fileName << " cannot be opened." << endl;
exit(1);
}
infile >> n;
A.resize(n);
for (int i = 0; i < n; i++){
A[i].resize(n);
for (int j = 0; j < n; j++){
infile >> A[i][j];
}
}
B.resize(n);
for (int i = 0; i < n; i++){
B[i].resize(n);
for (int j = 0; j < n; j++){
infile >> B[i][j];
}
}
infile.close();
}
void solve(int limit){
try {
/* Declares the optimization model. */
LSModel model = localsolver.getModel();
// Permutation such that p[i] is the facility on the location i
p = model.listVar(n);
// The list must be complete
model.constraint(model.count(p) == n);
// Create B as an array to be accessed by an at operator
LSExpression arrayB = model.array();
for (int i = 0; i < n; i++) {
arrayB.addOperand(model.array(B[i].begin(), B[i].end()));
}
// Minimize the sum of product distance*flow
obj = model.sum();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
obj += A[i][j] * model.at(arrayB, p[i], p[j]);
}
}
model.minimize(obj);
model.close();
/* Parameterizes the solver. */
LSPhase phase = localsolver.createPhase();
phase.setTimeLimit(limit);
localsolver.solve();
}catch (const LSException& e){
cout << "LSException:" << e.getMessage() << endl;
exit(1);
}
}
/* Writes the solution in a file with the following format:
* - n objValue
* - permutation p */
void writeSolution(const string& fileName){
ofstream outfile(fileName.c_str());
if (!outfile.is_open()) {
cerr << "File " << fileName << " cannot be opened." << endl;
exit(1);
}
outfile << n << " " << obj.getValue() << "\n" ;
LSCollection pCollection = p.getCollectionValue();
for (int i = 0; i < n; i++) {
outfile << pCollection.get(i) << " " ;
}
outfile << endl;
outfile.close();
}
};
int main(int argc, char ** argv) {
if (argc < 2) {
cout << "Usage: qap inputFile [outputFile] [timeLimit]" << endl;
exit(1);
}
const char * instanceFile = argv[1];
const char * solFile = argc > 2 ? argv[2] : NULL;
const char * strTimeLimit = argc > 3 ? argv[3] : "300" ;
Qap model;
model.readInstance(instanceFile);
model.solve(atoi(strTimeLimit));
if (solFile != NULL)
model.writeSolution(solFile);
return 0;
}
Last edited on Jan 29, 2017 at 1:42pm UTC
Jan 25, 2017 at 7:20am UTC
Topic archived. No new replies allowed.