Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  about the demo TSP

    Posted 07/14/12 11:33 AM

    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 distEdges = ...;

    // Decision variables
    dvar boolean xEdges;

    tuple Subtour { int size; int subtourCities; }//size?subtourcities记录各个城市的
    {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.subtouri])><= s.size-1;
    //subtour的size 记录包含的城市的
    };

    // POST-PROCESSING to find the subtours

    // Solution information
    int thisSubtourCities;
    int newSubtourSize;
    int newSubtourCities;

    // Auxiliar information
    int visitedi in Cities = 0;
    setof(int) adjj 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) {
    visitednode = 1;
    var succ = start;
    for (i in adjnode)
    if (visited[i] == 0) {
    succ = i;//
    break;//通过循环找到node的其中一个邻接点就可以,并且记录在succ中。
    }

    thisSubtournode = 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


  • 2.  Re: about the demo TSP

    Posted 07/14/12 03:21 PM

    Originally posted by: SystemAdmin


    There's a separate forum for OPL; you might do better posting OPL questions there.

    I just opened the project in Studio 12.4 (the copy that I have installed, not the one you posted -- you need to surround code with {code} tags) and saw no problem with braces -- all present and accounted for, all correctly paired, all correctly indented.

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: about the demo TSP

    Posted 07/15/12 03:09 AM

    Originally posted by: SystemAdmin


    I looked at the example in the CPLEX distribution and could not find any problem with the braces.
    Can you please tell us which braces you think are wrong and which are the ones you changed (and how)?
    Your code is not readable since you did not include it into '{code}' tags.
    #CPLEXOptimizers
    #DecisionOptimization