Originally posted by: vishnu02saj
I have the following tsp.mod file which is one of the example in the cplex studio, the 'travelling salesman problem'.
/*****************************************************************************
*
* OPL model and script for Symmetric Travelling Salesman Problem
*
*****************************************************************************/
/*****************************************************************************
*
* DATA
*
*****************************************************************************/
// Cities
int n = ...;
range Cities = 1..n;
// Edges -- sparse set
tuple edge {int i; int j;}
setof(edge) Edges = {<i,j> | ordered i,j in Cities};
int dist[Edges] = ...;
// Decision variables
dvar boolean x[Edges];
tuple Subtour { int size; int subtour[Cities]; }
{Subtour} subtours = ...;
/*****************************************************************************
*
* MODEL
*
*****************************************************************************/
// Objective
minimize sum (<i,j> in Edges) dist[<i,j>]*x[<i,j>];
subject to {
// Each city is linked with two other cities
forall (j in Cities)
sum (<i,j> in Edges) x[<i,j>] + sum (<j,k> in Edges) x[<j,k>] == 2;
// Subtour elimination constraints.
forall (s in subtours)
sum (i in Cities : s.subtour[i] != 0)
x[<minl(i, s.subtour[i]), maxl(i, s.subtour[i])>]
<= s.size-1;
};
// POST-PROCESSING to find the subtours
// Solution information
int thisSubtour[Cities];
int newSubtourSize;
int newSubtour[Cities];
// Auxiliary information
int visited[i in Cities] = 0;
setof(int) adj[j in Cities] = {i | <i,j> in Edges : x[<i,j>] == 1} union
{k | <j,k> in Edges : x[<j,k>] == 1};
execute {
newSubtourSize = n;
for (var i in Cities) { // Find an unexplored node
if (visited[i]==1) continue;
var start = i;
var node = i;
var thisSubtourSize = 0;
for (var j in Cities)
thisSubtour[j] = 0;
while (node!=start || thisSubtourSize==0) {
visited[node] = 1;
var succ = start;
for (i in adj[node])
if (visited[i] == 0) {
succ = i;
break;
}
thisSubtour[node] = succ;
node = succ;
++thisSubtourSize;
}
// writeln("Found subtour of size : ", thisSubtourSize);
if (thisSubtourSize < newSubtourSize) {
for (i in Cities)
newSubtour[i] = thisSubtour[i];
newSubtourSize = thisSubtourSize;
}
}
//if (newSubtourSize != n)
// writeln("Best subtour of size ", newSubtourSize);
}
/*****************************************************************************
*
* SCRIPT
*
*****************************************************************************/
main {
var opl = thisOplModel
var mod = opl.modelDefinition;
var dat = opl.dataElements;
var status = 0;
var it =0;
while (1) {
var cplex1 = new IloCplex();
opl = new IloOplModel(mod,cplex1);
opl.addDataSource(dat);
opl.generate();
it++;
// writeln("Iteration ",it, " with ", opl.subtours.size, " subtours.");
if (!cplex1.solve()) {
// writeln("ERROR: could not solve");
status = 1;
opl.end();
break;
}
opl.postProcess();
//writeln("Current solution : ", cplex1.getObjValue());
if (opl.newSubtourSize == opl.n) {
// This simply prints the selected edges.
for (var e in opl.Edges) {
if (opl.x[e] > 0.5) {
// writeln(e.i, " -> ", e.j);
}
}
// This prints the tour as a cycle
var c = 1; // current city
var lastc = -1; // city visited right before C
write(c);
while (true) {
var nextc = -1; // next city to visit
// Find the next city to visit. To this end we
// find the edge that leaves city C and does not
// end in city LASTC. We know that exactly one such
// edge exists, otherwise the solution would be infeasible.
for (var e in opl.Edges) {
if (opl.x[e] > 0.5) {
if (e.i == c && e.j != lastc) {
nextc = e.j;
break;
}
else if (e.j == c && e.i != lastc) {
nextc = e.i;
break;
}
}
}
// Write next city and update current and last city.
write(" -> ", nextc);
lastc = c;
c = nextc;
// Stop if we are back at the origin.
if (c == 1) {
break;
}
}
opl.end();
cplex1.end();
break; // not found
}
dat.subtours.add(opl.newSubtourSize, opl.newSubtour);
opl.end();
cplex1.end();
}
status;
}
I got the following as output in the scripting log:
1 -> 4 -> 13 -> 7 -> 8 -> 6 -> 17 -> 14 -> 15 -> 3 -> 11 -> 10 -> 2 -> 5 -> 9 -> 12 -> 16 -> 1
But I want the output in solution pool. My solution is pool is blank now. Is there any way to write the solution in the solution pool from the main block?
#DecisionOptimization#OPLusingCPLEXOptimizer