Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Vector.Attach in main() block

    Posted Sun September 26, 2010 10:15 PM

    Originally posted by: AnthonyPANG


    Dear all,

    I know we can use the IloOplCplexVectors to implement the warmstart function. However, the examples that I can found in ILOG, define this vector.attach function in the example.mod, that both the values and decision variables are defined in the model. May I have your help to show me how I can do it within the main() block because I want to use the result obtained from solving a model P1, and attach the solution to a vector for another model P2. Any simple example codes are highly appreciated.

    Thanks,
    Anthony
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Vector.Attach in main() block

    Posted Mon September 27, 2010 11:11 AM

    Originally posted by: Laval


    Hi,
    Please find a simple code that may answer your question. This example uses the same warmstart example in the documentation:
    m2.mod (m1.mod is below)
    
    
    // -------------------------------------------------------------------------- 
    // Licensed Materials - Property of IBM 
    // (c) Copyright IBM Corporation 1998, 2009. All Rights Reserved. 
    // 
    // Note to U.S. Government Users Restricted Rights: 
    // Use, duplication or disclosure restricted by GSA ADP Schedule 
    // Contract with IBM Corp. 
    // --------------------------------------------------------------------------   range r = 1..10; dvar int+ x[r]; dvar int+ y[r]; 
    // The following array of values (defined as data) will be used as  
    // a starting solution to warm-start the CPLEX search. 
    
    float values[i in r] = (i==5)? 10 : 0;       minimize sum( i in r ) x[i] + sum( j in r ) y[j]; subject to
    { ctSum: sum( i in r ) x[i] >= 10; forall( j in r ) ctEqual: y[j] == j; 
    }   main
    { 
    //model m1 var sourcem1 = 
    
    new IloOplModelSource(
    "m1.mod"); var cplexm1 = 
    
    new IloCplex(); var defm1 = 
    
    new IloOplModelDefinition(sourcem1); var oplm1 = 
    
    new IloOplModel(defm1,cplexm1); oplm1.generate(); 
    
    if (cplexm1.solve()) 
    { writeln(
    "OBJ = " + cplexm1.getObjValue()); 
    } 
    
    else 
    { writeln(
    "No solution"); 
    } 
    // Setting initial solution writeln(
    "Setting initial solution"); writeln(oplm1.x);   
    // model m2 thisOplModel.generate(); var def = thisOplModel.modelDefinition; var opl1 = 
    
    new IloOplModel(def, cplex); opl1.generate(); cplex.solve(); writeln(opl1.printSolution()); var cplex2 = 
    
    new IloCplex; var opl2 = 
    
    new IloOplModel(def, cplex2); opl2.generate();   
    // You need to copy your solution into this array values and then next attach this array to the x or y variables- see below  
    // pay attention to the size of the x var and values array- same size 
    
    for (var w in opl2.r) opl2.values[w]= oplm1.x[w];   var vectors = 
    
    new IloOplCplexVectors(); 
    // We attach the values (defined as data) as starting solution 
    // for the variables x.   vectors.attach(opl2.x, opl2.values); 
    // initialize x var vectors.attach(opl2.y,opl2.values);  
    // initialize y var vectors.setVectors(cplex2); cplex2.solve(); writeln(opl2.printSolution());   opl1.end(); opl2.end(); oplm1.end(); 0; 
    }
    


    m1.mod
    
    range r1 = 1..10; dvar int+ x[r1];     minimize sum( i in r1 ) x[i] ; subject to
    { ctSum: sum( i in r1 ) x[i] >= 1; 
    }
    


    This is the cplex when you solve the models: as you see he tries to use an initial solution but
    Tried aggregator 1 time.
    MIP Presolve eliminated 1 rows and 11 columns.
    All rows and columns eliminated.
    Presolve time = 0.00 sec.
    Tried aggregator 1 time.
    MIP Presolve eliminated 11 rows and 21 columns.
    All rows and columns eliminated.
    Presolve time = 0.00 sec.
    Warning: No solution found from 1 MIP starts.
    Retaining values of one MIP start for possible repair.
    Tried aggregator 1 time.
    MIP Presolve eliminated 11 rows and 21 columns.
    All rows and columns eliminated.
    Presolve time = 0.00 sec.
    The solution:
    OBJ = 1
    Setting initial solution
    1 0 0 0 0 0 0 0 0 0
    // solution (optimal) with objective 65
    x = 10 0 0 0 0 0 0 0 0 0;
    y = 1 2 3 4 5 6 7 8 9 10;

    // solution (optimal) with objective 65
    x = 10 0 0 0 0 0 0 0 0 0;
    y = 1 2 3 4 5 6 7 8 9 10;
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Vector.Attach in main() block

    Posted Tue September 28, 2010 07:43 AM

    Originally posted by: AnthonyPANG


    Dear Laval,

    Thanks for your reply, I got your idea. Means that I should make the main() block within the m2.mod file, so that the variables defined is within the model, in order for me to attach the vectors in m2.mod for warmstart.

    In fact, is it possible for me to have a main() block (say as m3.mod) that just used for creating two cplex models by calling two model files, such that I can use the solution obtained in m1.mod and attach the solution to m2.mod for warmstart.

    Nevermind if my above method is not work, as I can modify my model files as shown in your reply below.

    Thanks,
    Anthony
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Vector.Attach in main() block

    Posted Tue September 28, 2010 09:02 AM

    Originally posted by: Laval


    Hi,

    Yes you can use a third model (m3.mod)and call the two models m1 and m2 and use the warmstart method as shown in the code before....
    Just one more important thing, pay attention to the size of the array and the vectors(decision variables)otherwise you will get the same error (-3).
    Good luck
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Vector.Attach in main() block

    Posted Tue September 28, 2010 10:29 PM

    Originally posted by: AnthonyPANG


    Dear Laval,

    Thanks for your clarification. I think I am very clear about how it works now. Thanks alot.

    Anthony
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Vector.Attach in main() block

    Posted Thu September 13, 2012 10:47 AM

    Originally posted by: Mondano


    I have tried this to assign values to a multidimensional array in the main() block, but it does not work.

    I have three problems to solve: oplm1, oplm2 and oplm3 which is an integration of oplm1 and oplm2. From each problem oplm1 and oplm2 I need the values of the decision variables (x and q) and I want to hand them over to oplm3 and use it there for the decision variables. In all models the concerning dcision variables are called x and/or q and have three dimensions.

    Now I want to transfer x:

    If I do

    
    
    
    for (var w in oplm1.rangeOrders) 
    { 
    
    for (var v in oplm1.rangeDates) 
    { 
    
    for (var u in oplm1.rangeLines) 
    { oplm3.values[w][v][u] = oplm1.x[w][v][u]; 
    } 
    } 
    }
    

    The error is "Element "values" does not exist in OPL model". But in your version you did not define it either. Or did I miss something?

    If I want to assign it directly to the decision variable...
    
    
    
    for (var w in oplm1.rangeOrders) 
    { 
    
    for (var v in oplm1.rangeDates) 
    { 
    
    for (var u in oplm1.rangeLines) 
    { oplm3.x[w][v][u] = oplm1.x[w][v][u]; 
    } 
    } 
    }
    

    ... I get "Can not assign to decision variable in "x#0#0"

    I do not quite understand this second error message for two reasons:
    1) The variable has three dimension - why does CPLEX mention only two indices?
    2) Why is it not possible to assign values to decision variables? Or does this only work through vector attach?

    Question
    Can you tell me, how I can assign the solution of oplm1 and oplm2 to oplm3 when both are multidimensional arrays?
    #DecisionOptimization
    #OPLusingCPLEXOptimizer