Originally posted by: SystemAdmin
I am learning ILOG OPL.I found a problem in the demo TSP.
There is a brace mismatch problem in the TSP problem and somehow,the code can be run and gave me a result.Of course I don't know if it is the correct answer or not.The amount of the "{" and "}" does match-the amount of "{" is the same as the amount of "}",but the position doesn't match(there is a "{" which IDE cann't find a "}" for and there is a "}" which IDE cann't find a "{" for).After I corrected the position,no grammar error,the code can be run as well and I was given a result which showed that the problem cann't be solved. Is there anyone who have test the result?
Looking forward to anyone's reply.
The following is the mod and the data is in the attachment.You can also find this demo in ILOG 12.4 demo directory.
// 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};//setof(edge)等价于{edge}
int dist
Edges = ...;
// Decision variables
dvar boolean x
Edges;
tuple Subtour { int size; int subtour
Cities; }//size?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;
//subtour的size 记录包含的城市的
};
// POST-PROCESSING to find the subtours
// Solution information
int thisSubtour
Cities;
int newSubtourSize;
int newSubtour
Cities;
// Auxiliar 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};//记录每个城市的邻接城市。
//每个城市的邻接城市是个集合,共有n的集合。
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;//通过循环找到node的其中一个邻接点就可以,并且记录在succ中。
}
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) {
opl.end();
cplex1.end();
break; // not found
}
dat.subtours.add(opl.newSubtourSize, opl.newSubtour);
opl.end();
cplex1.end();
}
status;
}
#CPLEXOptimizers#DecisionOptimization