简体   繁体   English

如何为 CP Optimizer 提供初始可行解

[英]How to give to CP Optimizer an initial feasible solution

I need to solve a hard scheduling problem.我需要解决一个困难的调度问题。 For that purpose, I get an initial feasible solution using a greedy heuristic.为此,我使用贪婪的启发式方法获得了初始可行的解决方案。 This heuristic gives me the starting time of every job in every machine.这种启发式方法为我提供了每台机器上每项工作的开始时间。

How can I initialize CP Optimizer with this initial solution?如何使用此初始解决方案初始化 CP Optimizer? ("warmstart") (“热启动”)

Thanks in advance提前致谢

With opl Cplex CPo you May See an example in使用 opl Cplex CPo 你可能会看到一个例子

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

//we saw how to warm start in CPLEX. //我们看到了如何在 CPLEX 中热启动。 Let s see how we can do the same with CPO in 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
     
     */

and with scheduling并有日程安排

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();
}

This is called a "starting point" in CP and is explained in the manual in CP Optimizer > CP Optimizer C++ API Reference Manual > Concepts > Starting point in CP Optimizer in the manual.这在 CP 中称为“起点”,并在手册中的CP Optimizer > CP Optimizer C++ API 参考手册 > 概念 > CP Optimizer中的起点中的手册中进行了解释。 The function to use is IloCP::setStartingPoint(IloSolution) .要使用的函数是IloCP::setStartingPoint(IloSolution)

In the CPLEX distriubtion you have examples for this in the plantlocation.cpp , sched_goalprog.cpp examples and their variants for other APIs.在 CPLEX 发行版中,您在plantlocation.cppsched_goalprog.cpp示例及其其他 API 的变体中提供了sched_goalprog.cpp示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM