'Using a solution from a model as an input to another one and Outputting each Solution Separately

I'm solving an optimization problem in which I need the result from one model to be used as a input in another model for 180 iterations. I'm using CPLEX with OPL language without any addon.

I tried to save the values from one model into an Excel file and reading those into the next model but since I'm going to do this 180 times I am worried I will make an error and have to restart or not even know I made an error.

Is it possible to have this run for 180 iterations and input each iteration's solution separately?



Solution 1:[1]

You can rely on warmstart for that.

2 simple examples in easy OPL

Warm start from a file:

include "zoo.mod";

    main {
     var filename = "c:/temp/mipstart.mst";
      thisOplModel.generate();
      cplex.readMIPStarts(filename);
      cplex.solve();
      writeln("Objective: " + cplex.getObjValue());
     
    }

or with API

int nbKids=300;

    // a tuple is like a struct in C, a class in C++ or a record in Pascal
    tuple bus
    {
     key int nbSeats;
     float cost;
    }


    // This is a tuple set
    {bus} pricebuses=...;

    // asserts help make sure data is fine
    assert forall(b in pricebuses) b.nbSeats>0;assert forall(b in pricebuses) b.cost>0;


    // To compute the average cost per kid of each bus
    // you may use OPL modeling language

    float averageCost[b in pricebuses]=b.cost/b.nbSeats;

    // Let us try first with a naïve computation, use the cheapest bus

    float cheapestCostPerKid=min(b in pricebuses) averageCost[b];
    int cheapestBusSize=first({b.nbSeats | b in pricebuses : averageCost[b]==cheapestCostPerKid});
    int nbBusNeeded=ftoi(ceil(nbKids/cheapestBusSize));

    float cost0=item(pricebuses,<cheapestBusSize>).cost*nbBusNeeded;
    execute DISPLAY_Before_SOLVE
    {
      writeln("The naïve cost is ",cost0);
      writeln(nbBusNeeded," buses ",cheapestBusSize, " seats");
      writeln();
    }

    int naiveSolution[b in pricebuses]=
      (b.nbSeats==cheapestBusSize)?nbBusNeeded:0;


    // decision variable array
    dvar int+ nbBus[pricebuses];

    // objective
    minimize
      sum(b in pricebuses) b.cost*nbBus[b];
         
    // constraints
    subject to
    {
      sum(b in pricebuses) b.nbSeats*nbBus[b]>=nbKids;
    }

    float cost=sum(b in pricebuses) b.cost*nbBus[b];
    execute DISPLAY_After_SOLVE
    {
     writeln("The minimum cost is ",cost);
     for(var b in pricebuses) writeln(nbBus[b]," buses ",b.nbSeats, " seats");

    }


    main
    {
    thisOplModel.generate();
    // Warm start the naïve solution
    cplex.addMIPStart(thisOplModel.nbBus,thisOplModel.naiveSolution);
    cplex.solve();
    thisOplModel.postProcess();
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Fleischer