Search code examples
tuplescplexopl

Retrieving Reduced Cost of Dvar composed of "tuples" in CPLEX OPL


I want to retrieve the reduced cost of a decision variable in CPLEX OPL. However, the dvar indices are tuples, and cplex cannot iterate over a tuple to print the reduced costs. Is it impossible with tuples, or is there a way for it? For example there is a decision variable as below:

dvar float+ Production[ProductionLocations][TimePeriods];

tuple timeperiod {
  string TimePeriodID;
  string TimePeriodName;
  float CurrencyRate;
  float Rank;
  string ActivePeriod;
}
{timeperiod} TimePeriods = ...;
tuple productionlocation { 
  string ProductID;
  string ProductName;
  string LocationID;
  string LocationName;
}
{productionlocation} ProductionLocations = ...;

Thanks in advance.


Solution

  • Let me expand on Alex's answer: it seems you attempt to query dual values for a MIP. Dual values are however not defined for MIPs. You have two options here:

    1. You can relax all the variables and solve the relaxed problem. That is an LP and will thus provide duals. However, this may not be too helpful.
    2. You can compute the optimal solution to the MIP, fix all integer variables and then solve the remaining problem. This is again an LP and will thus provide dual values. These dual values may be more meaningful than the values you get from relaxing everything.

    In order to do the second thing, you can still use scripting:

    main {
      thisOplModel.generate();
      cplex.solve(); // Compute integer optimal solution
      cplex.solveFixed(); // Fix integer variables to optimal values, solve LP
      // query your dual values:
      for (var g in Gasolines) {
        writeln("a[",g,"].reducedCost = ",a[g].reducedCost);
      }
    }