如何给 CP Optimizer 一个初始的可行解

How to give to CP Optimizer an initial feasible solution

我需要解决一个很难安排的问题。为此,我使用贪心启发式方法得到了一个初始可行的解决方案。这种启发式方法为我提供了每台机器中每项工作的开始时间。

如何使用此初始解决方案初始化 CP Optimizer? (“热启动”)

提前致谢

这在 CP 中称为“起点”,在 CP Optimizer > CP Optimizer C++ API Reference Manual > Concepts > Starting point in CP Optimizer in the manual. The function to use is IloCP::setStartingPoint(IloSolution) 的手册中有解释。

在 CPLEX 发行版中,您在 plantlocation.cppsched_goalprog.cpp 示例及其其他 API 的变体中有这方面的示例。

使用 opl Cplex CPo,您可以在

中查看示例

https://github.com/AlexFleischerParis/zooopl/blob/master/zoowarmstartapicpo.mod

//我们看到了如何在 CPLEX 中进行热启动。让我们看看如何在 CPLEX 中对 CPO 执行相同的操作:

    using CP;

    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
    var sol=new IloOplCPSolution();
    for(var b in thisOplModel.pricebuses)
       sol.setValue(thisOplModel.nbBus[b],thisOplModel.naiveSolution[b]);
    cp.setStartingPoint(sol);

    cp.solve();
    thisOplModel.postProcess();
    }
/*
.dat
 
    pricebuses={<40,500>,<30,400>};
which gives
 
    The naïve cost is 4000
    8 buses 40 seats
    The minimum cost is 3800
    6 buses 40 seats
    2 buses 30 seats
and in the log we see
 
    ! ----------------------------------------------------------------------------
     ! Minimization problem - 2 variables, 1 constraint
     ! Using starting point solution
     
     */

并有日程安排

using CP;

range r = 1..10;
dvar interval x[r] size 1;

dvar interval y[r] size 1;
// The following array of values (defined as data) will be used as  
// a starting solution to warm-start the CP Optimizer search.

int values[i in r] = (i==5)? 10 : 0;     

minimize sum( i in r ) startOf(x[i]) + sum( j in r ) startOf(y[j]);
subject to
{
ctSum: sum( i in r ) startOf(x[i]) >= 10;
forall( j in r ) ctEqual: startOf(y[j]) == j;
}   

execute
{
for(i in r) write(Opl.startOf(x[i])," ");
writeln();
}

main
{
thisOplModel.generate();
var sol=new IloOplCPSolution();

for(var i=1;i<=10;i++) sol.setStart(thisOplModel.x[i],thisOplModel.values[i]);
cp.solve();
thisOplModel.postProcess();
cp.setStartingPoint(sol);
cp.solve();
thisOplModel.postProcess();
}