我是编程的初学者,我正在尝试为DIAL A RIDE问题编写Cplex的Java代码。这是模型和代码。 我无法就容量进行最后的成本评估,也许在代码中还有其他错误。 你能帮我吗?
公共类Dialaride { 公共静态void main(String [] args){
int P= ;
int V=;
double M = ; //big M
int [] q= ;
int []T= ;
float[] d =;
double [] e=;
double[] l=;
int G=300;
int [][][] c= new int[P][P][V];
for(int i = 0; i < P; i++) {
for(int j = 0; j < P; j++) {
for(int k = 0; k < V; k++) {
if (i==j) {
c[i][j][k]=0;
}else {
c[i][j][k]=12;
}
}
}
}
double[][] t = new double[P][P];
for(int i = 0; i < P; i++) {
for(int j = 0; j < P; j++) {
if (i==j) {
t[i][j]=0;
}else {
t[i][j] = 10+ d[i];
}
}
}
try {
//model def
IloCplex cplex = new IloCplex();
IloIntVar [][][] x = new IloIntVar [P][][];
for (int i = 0; i < P; i++) {
x[i] = new IloIntVar [P][];
for (int j = 0; j < P; j++) {
x[i][j] = cplex.boolVarArray(V);
}
}
//arrival time of vehicle k at customer i
IloNumVar [][]B = new IloNumVar [P][];
for(int i = 0; i < P; i++) {
B[i] =cplex.numVarArray(V, 0, Double.MAX_VALUE);
}
//load of vehicle k after visiting customer i
IloNumVar [][]Q = new IloNumVar [P][];
for(int i = 0; i < P; i++) {
Q[i] =cplex.numVarArray(V, 0, Double.MAX_VALUE);
}
//ride time of user i on vehicle k
IloNumVar [][]L = new IloNumVar [P][];
for(int i = 0; i < P; i++) {
L[i] =cplex.numVarArray(V, 0, Double.MAX_VALUE);
}
//Objective Expression
IloLinearNumExpr objective = cplex.linearNumExpr();
for (int i = 0; i < P; i++) {
for (int j = 0; j < P; j++) {
for (int k = 0; k < V; k++) {
objective.addTerm(c[i][j][k], x[i][j][k]);
}
}
}
//Define objective
cplex.addMinimize(objective);
// constraint 1: Each customer is visited only once
for(int i = 1; i < P/2; i++) {
IloLinearNumExpr expr1 = cplex.linearNumExpr();
for(int j = 1; j < P; j++) {
for(int k = 0; k < V; k++) {
if(i != j) {
expr1.addTerm(1.0, x[i][j][k]);
}
}
}
cplex.addEq(expr1, 1.0);
}
//constraint 2: the pickup and delivery nodes are visited by the same vehicle
for(int k = 0; k < V; k++) {
for (int i =1; i <P/2; i++) {
IloLinearNumExpr expr2 = cplex.linearNumExpr();
for(int j = 1; j < P; j++) {
for(int h = 0; h < P; h++) {
if(i != j) {
expr2.addTerm(1.0, x[i][j][k]);
if(h!=(P/2)+i) {
expr2.addTerm(-1.0, x[(P/2)+i][h][k]);
}
}
}
}
cplex.addEq(expr2, 0);
}
}
//Constraint 3: Each vehicle must start from depot 0
for(int k = 0; k < V; k++) {
IloLinearNumExpr expr3 = cplex.linearNumExpr();
for(int j = 0; j < (P/2)+1; j++) {
expr3.addTerm(1.0, x[0][j][k]);
}
cplex.addEq(expr3, 1.0);
}
//Constraint 4: Each vehicle must finish at the final depot
for(int k = 0; k < V; k++) {
IloLinearNumExpr expr4 = cplex.linearNumExpr();
for(int i = 0; i < P; i++) {
expr4.addTerm(1.0, x[i][P-1][k]);
}
cplex.addEq(expr4, 1.0);
}
//constraint 5: after arriving to customer vehicle must leave for next
for(int h = 1; h <P/2 ; h++) {
for(int k = 0; k <V ; k++) {
IloLinearNumExpr expr5 = cplex.linearNumExpr();
for(int i = 0; i <P; i++) {
for(int j = 1; j <P ; j++) {
if(i != j) {
expr5.addTerm(1.0, x[h][i][k]);
expr5.addTerm(-1.0, x[i][j][k]);
}
}
}
cplex.addEq(expr5, 0);
}
}
//constraint 6: time
for(int i = 0; i < P; i++) {
for(int j = 0; j < P; j++) {
for(int k = 0; k < V; k++) {
if(i != j) {
IloLinearNumExpr expr6 = cplex.linearNumExpr();
expr6.addTerm(1.0, B[i][k]);
expr6.setConstant(t[i][j]);
expr6.setConstant(d[i]);
expr6.addTerm(-1.0, B[j][k]);
expr6.addTerm(M, x[i][j][k]);
cplex.addLe(expr6, M);
}
}
}
}
//constraint 7: load
for(int i = 0; i < P; i++) {
for(int j = 0; j < P; j++) {
for(int k = 0; k < V; k++) {
if(i != j) {
IloLinearNumExpr expr7 = cplex.linearNumExpr();
expr7.addTerm(1.0, Q[i][k]);
expr7.setConstant(q[j]);
expr7.addTerm(-1.0, Q[j][k]);
expr7.addTerm(M, x[i][j][k]);
cplex.addLe(expr7, M);
}
}
}
}
//constraint 8: time window-earliest arrival time
for(int i = 0; i < P; i++) {
for(int k = 0; k < V; k++) {
IloLinearNumExpr expr8 = cplex.linearNumExpr();
expr8.addTerm(1.0, B[i][k]);
cplex.addGe(expr8, e[i]);
}
}
//constraint 9: time window-latest arrival time
for(int i = 0; i < P; i++) {
for(int k = 0; k < V; k++) {
IloLinearNumExpr expr9 = cplex.linearNumExpr();
expr9.addTerm(1.0, B[i][k]);
cplex.addLe(expr9, l[i]);
}
}
//constraint 10:
for(int i = 0; i < P/2; i++) {
for(int k = 0; k < V; k++) {
IloLinearNumExpr expr10 = cplex.linearNumExpr();
expr10.addTerm(1.0, L[i][k]);
cplex.addGe(expr10,t[i][(P/2)+i] );
}
}
//constraint 11:
for(int i = 0; i < P/2; i++) {
for(int k = 0; k < V; k++) {
IloLinearNumExpr expr11 = cplex.linearNumExpr();
expr11.addTerm(1.0, L[i][k]);
cplex.addLe(expr11,G);
}
}
//constraint 12
for(int i = 0; i <P/2 ; i++) {
for(int k = 0; k <V ; k++) {
IloLinearNumExpr expr12 = cplex.linearNumExpr();
expr12.addTerm(1.0, L[i][k]);
expr12.addTerm(1.0, B[i][k]);
expr12.setConstant(d[i]);
cplex.addEq(expr12, B[(P/2)+i][k]);
}
}
//constraint 13
for(int k = 0; k <V ; k++) {
IloLinearNumExpr expr13 = cplex.linearNumExpr();
expr13.addTerm(1.0, B[(P/2)+1][k]);
expr13.addTerm(-1.0, B[0][k]);
cplex.addEq(expr13, T[k]);
}[enter link description here][1]
答案 0 :(得分:0)
您说过要添加约束max{0, qi} ≤ Qki ≤ min{Qk,Qk + qi}
。您没有提到您的符号是什么,所以我假设Q
是变量,而q
是事先已知的常量。
对于任何变量Qki
,您可以计算值max{0, qi}
并将其设置为变量的下限。
对于上限,您的意思还不清楚。但是似乎您想指定一个给定变量(我叫它X
)必须小于其他两个变量(Y
和Z
)的最小值。这等效于说明X ≤ Y
和X ≤ Z
。您只需声明这两个约束。
如果我误解或假设不正确,也许这会有所帮助:https://www.leandro-coelho.com/how-to-linearize-max-min-and-abs-functions/介绍了如何线性化 min 和 max 。