簡體   English   中英

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

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

我需要解決一個困難的調度問題。 為此,我使用貪婪的啟發式方法獲得了初始可行的解決方案。 這種啟發式方法為我提供了每台機器上每項工作的開始時間。

如何使用此初始解決方案初始化 CP Optimizer? (“熱啟動”)

提前致謝

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

這在 CP 中稱為“起點”,並在手冊中的CP Optimizer > CP Optimizer C++ API 參考手冊 > 概念 > CP Optimizer中的起點中的手冊中進行了解釋。 要使用的函數是IloCP::setStartingPoint(IloSolution)

在 CPLEX 發行版中,您在plantlocation.cppsched_goalprog.cpp示例及其其他 API 的變體中提供了sched_goalprog.cpp示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM