将程序从 CPLEX 重写为 CP 的正确方法是什么?

问题描述 投票:0回答:1

我解决了同时放置工厂和仓库的问题。起初,我使用标准 CPLEX 工具编写了一个程序,但后来我面临着使用 CP 重写该程序的任务。 这是我原来的项目: .mod 文件:

int l = ...;
int n = ...;
int t = ...;
int m = ...;

range j = 1..m;
range i = 1..n;
range h = 1..l;
range e = 1..t;

int D[j] = ...;
int K[i] = ...;
int S[h] = ...;
int W[e] = ...;
float F[i] = ...;
float f[e] = ...;
float c1[h][i] = ...;
float c2[i][e] = ...;
float c3[e][j] = ...;

dvar boolean y1[i];
dvar boolean y2[e];
dvar int+ x1[e][j];
dvar int+ x2[i][e];
dvar int+ x3[h][i];

minimize
  sum(I in i) (F[I] * y1[I]) + 
  sum(E in e) (f[E] * y2[E]) + 
  sum(H in h, I in i) (c1[H][I] * x3[H][I]) +
  sum(I in i, E in e) (c2[I][E] * x2[I][E]) +
  sum(E in e, J in j) (c3[E][J] * x1[E][J]);
 
 subject to {
    forall(H in h)
      sum(I in i) x3[H][I] <= S[H];
    forall(I in i)
      sum(H in h) (x3[H][I]) - sum(E in e) (x2[I][E]) >= 0;
    forall(I in i)
      sum(E in e) x2[I][E] <= K[I] * y1[I];
    forall(E in e)
      sum(I in i) (x2[I][E]) - sum(J in j) (x1[E][J]) >= 0;
    forall(E in e)
      sum(J in j) x1[E][J] <= W[E] * y2[E];
    forall(J in j)
      sum(E in e) x1[E][J] == D[J];
}

.dat 文件:

l = 3; //number of suppliers
n = 4; //number of potential factory locations
t = 5; //number of potential warehouse locations
m = 7; //number of markets or demand points
SheetConnection sheet("Task.xlsx");
D from SheetRead(sheet, "PWL!b26:h26"); //annual demand from customer
K from SheetRead(sheet, "PWL!h12:h15"); //potential capacity of factory at site
S from SheetRead(sheet, "PWL!f4:f6"); //supply capacity at supplier
W from SheetRead(sheet, "PWL!j21:j25"); //potential warehouse capacity at site
F from SheetRead(sheet, "PWL!g12:g15"); //fixed cost of locating a plant at site
f from SheetRead(sheet, "PWL!i21:i25"); //fixed cost of locating a warehouse at site
c1 from SheetRead(sheet, "PWL!b4:e6"); //cost of shipping one unit from supply source to factory 
c2 from SheetRead(sheet, "PWL!b12:f15"); //cost of producing and shipping one unit from factory to warehouse 
c3 from SheetRead(sheet, "PWL!b21:h25"); // cost of shipping one unit from warehouse to customer

x3 to SheetWrite(sheet, "PWL!m4:p6"); //quantity shipped from warehouse to market
x2 to SheetWrite(sheet, "PWL!m12:q15"); //quantity shipped from factory to warehouse
x1 to SheetWrite(sheet, "PWL!m21:s25"); //quantity shipped from supplier to factory at site
y1 to SheetWrite(sheet, "PWL!r12:r15"); //1 if factory is located at site i, 0 otherwise
y2 to SheetWrite(sheet, "PWL!t21:t25"); //1 if warehouse is located at site e, 0 otherwise

我不知道CP的很多复杂之处,任何帮助将不胜感激

到目前为止,我已经编写了一个.dat 文件:

nSuppliers = 3;
nFactories = 4;
nWarehouses = 5;
nMarkets = 7;

D = [150, 150, 100, 100, 100, 150, 100];
K = [350, 280, 400, 300];
S = [350, 290, 310];
W = [350, 350, 350, 350, 350];
F = [450000, 500000, 450000, 450000];
f = [30000, 40000, 35000, 20000, 35000];

c1 = [
    [1.9, 1.9, 1.8, 1.9],
    [2.0, 2.0, 1.9, 1.8],
    [1.8, 2.0, 1.9, 2.0]
];

c2 = [
    [6.1, 7.8, 7.9, 7.9, 7.8],
    [7.7, 7.1, 6.9, 7.8, 7.7],
    [6.7, 6.0, 7.1, 6.9, 6.7],
    [7.0, 6.0, 6.5, 7.6, 6.6]
];

c3 = [
    [4.5, 5.0, 4.6, 5.5, 4.8, 4.1, 4.9],
    [4.8, 4.6, 4.9, 5.7, 5.7, 5.9, 4.0],
    [5.2, 5.1, 5.1, 4.2, 4.7, 4.0, 5.0],
    [5.2, 5.9, 4.3, 5.9, 5.7, 4.1, 4.7],
    [4.9, 5.3, 4.1, 4.5, 4.7, 5.6, 4.0]
];
mathematical-optimization cplex constraint-programming opl
1个回答
0
投票

在您的模型中,您只需添加

using CP;

execute
{
  cp.param.timelimit=10;
}

在模型的开头

在我的机器上我看到

! Time = 9,98s, Average fail depth = 34, Memory usage = 26,2 MB
 ! Current bound is 475520.7 (gap is 67,11%)
 !          Best Branches  Non-fixed    W       Branch decision
    1.445820e+06     155k         12    8         1 != x3(2)(3)
    1.445820e+06     159k         29    9   F    24  = x1(3)(5)
 ! ----------------------------------------------------------------------------
 ! Search terminated by limit, 178 solutions found.
 ! Best objective         : 1.445820e+06 (gap is 67,11%)
 ! Best bound             : 475520.7
© www.soinside.com 2019 - 2024. All rights reserved.