Decision Optimization

 View Only
Expand all | Collapse all

OPL - change objective coefficient of an indexed decision variable

  • 1.  OPL - change objective coefficient of an indexed decision variable

    Posted Thu August 20, 2020 04:08 AM
    Hello, I am very new to OPL. After solving the original model, I want to change the coefficient of indexed decision variable, and change RHS of one of the constraints, and the solve model with new parameters. However, I am not able to do so. Primary challenge I am facing is that my decision variables and constraints are indexed. Any help is much appreciated. Thanks.
    // .dat file Products = { "chair", "table" }; Rescources = { "mahogany", "labor" }; Price = [45, 80]; Rescource_capacity = [400, 450]; Product_rescources = [[5, 20], [10, 15]]; // .mod file {string} Products = ...; int Price[Products] = ...; {string} Rescources = ...; int Rescource_capacity[Rescources] = ...; int Product_rescources[Rescources][Products] = ...; dvar float+ Produce[Products]; maximize sum( p in Products ) Produce[p] * Price[p]; subject to { forall( r in Rescources) ctCapacity: sum( p in Products ) Product_rescources[r][p] * Produce[p] <= Rescource_capacity[r]; } // Solution tuple ProduceSolutionT{ string Products; float value; }; {ProduceSolutionT} ProduceSolution = {<p, Produce[p]> | p in Products}; execute{ writeln(" Maximum profit = " , cplex.getObjValue()); writeln(ProduceSolution); // writeln(Produce["table"]); } // Above code works absolutely fine and returns // solution (optimal) with objective 2200 // Maximum profit = 2200 // {<"chair" 24> <"table" 14>} // **********************BELOW IS MY QUESTION************************** // Now I want to update the objective coefficient of lets say the first product // 'chair', and say, i want to update the RHS of the first constraint. How to do this // i know cplex.setObjCoef(thisOplModel.chair, 60) will work, if i have defined chair // as a decision variable, but i have indexed it as Produce["chair"] .. // after updations, i again i want to solve the model. // I tried something on below lines, but doesn't work main { cplex.setObjCoef(thisOplModel.Produce["chair"], 80); // at present RHS of first constraint = 400, // how to change it to 500 (say). I am not sure how to pick the // first constraint (there are 2 in total) cplex.solve(); thisOplModel.postProcess(); } ​

    ------------------------------
    Bhartendu Awasthi
    ------------------------------

    #DecisionOptimization


  • 2.  RE: OPL - change objective coefficient of an indexed decision variable

    Posted Thu August 20, 2020 07:33 AM
    I was able to figure out - 

    thisOplModel.generate(); cplex.setObjCoef(thisOplModel.Produce["table"], 60); thisOplModel.ctCapacity["mahogany"].UB = 500;​

    ------------------------------
    Bhartendu Awasthi
    ------------------------------