首页 > 解决方案 > CPLEX - 匹配供需的约束

问题描述

我对 CPLEX 中的约束函数有疑问。

我有一种情况,需要从 8 艘船上为许多营地提供库存。船只需要停泊在两个港口之一。两种运输方式,即船舶运输(基于吨位运输)和从港口到营地的运输(基于船舶数量)都需要额外付费。

我需要添加一个约束条件,即供应量至少等于需求量,而目标是最小化成本。但是,我的约束没有给出正确的输出。

有人可以帮我纠正我的约束并解释为什么它不起作用吗?

 /// Indices 
int nrofships = 8;
range ships = 1..nrofships;
int nrofports = 2;
range ports = 1..nrofports;
int nrofmonths = 5; 
range months = 1..nrofmonths;
int nrofcamps = 6;
range camps = 1..nrofcamps;
// Parameters 
float capacity[ships] = [1.250, 2.740, 1.890, 4.980, 2.630, 16.000, 9.610, 3.540];
int demand[camps][months]= [[11, 5, 1, 1, 1],[3, 0, 3, 0, 2],[6, 3, 4, 3, 7],[5, 5, 5, 2, 6],[7, 1, 7, 1, 4],[0, 3, 5, 0, 0]];
int preparation[ships] = [250000, 655000, 412300, 876900, 470025, 3655000, 1914500, 700000];
int transportationcosts[ports][ships] = [[129, 132, 129, 132, 178, 160, 161, 144],[134, 155, 130, 139, 142, 139, 154, 122]];
int distributioncosts[ports][camps] = [[130, 81, 77, 83, 89, 116],[71, 125, 114, 85, 86, 86]];
// decision variable
dvar float+ x[ships][ports][months];
dvar float+ y[ports][camps][months];
dvar boolean binx[ships][ports][months];


//Expression of Decision 
// dexpr float Costs = sum(s in ships, c in camps, p in ports, m in months) preparation[s]*binx[s][p][m];

dexpr float TotalCost = sum(s in ships, m in months, c in camps, p in ports) preparation[s]*binx[s][p][m] + 
         sum(s in ships, m in months, c in camps, p in ports) transportationcosts[p][s]*x[s][p][m] + 
         sum(s in ships, m in months, c in camps, p in ports) distributioncosts[p][c]*y[p][c][m];
 
//Objective function
minimize TotalCost;

//Constraints
constraint ctdemand[ships][camps][months];

subject to {

forall(s in ships, c in camps, m in months) ctdemand[s][c][m]:     
 sum(p in ports) x[s][p][m]*capacity[s] >= demand[c][m];
     
}

标签: constraintscplex

解决方案


binx 在目标中,但没有约束。

你不需要像这样的约束吗

forall(s in ships, m in months,p in ports) binx[s][p][m]==(x[s][p][m]>=0.001);

?

加上 y 也不受约束


推荐阅读