我遇到了交叉背包问题,通过最小化成本,需要将一组项目中的多个项目的最大数量放入一个箱子中。我能够解决CPLEX中的优化问题。
但是,当问题由两个容器(具有不同容量)组成时,我发现在CPLEX中实现时遇到了困难。
问题:
Bin = [B1, B2]
Capacity = [7,5]
Item = [I1, I2, I3, I4]
Weight = [6,3,1,4]
Price = [2,8,2,4]
目标是放置最大数量的项目并最小化总价格。
如何在CPLEX中实现此客观问题?
以下是我的代码片段:
// ITEMS
int n=4; // no of items
range items = 1..n; // range of items
int p[items] = [2,8,2,6]; //price
int w[items] = [6,3,1,4]; //weight
// BINS
int m=2; // no of bins
range bins=1..m; // range of bin
int capacity[bins] = [7,5]; // capacity of each bin
dvar boolean x[items][bins];
// model ; max the profit
maximize sum(i in items, j in bins) p[i]*x[i][j];
subject to {
forall (j in bins)
cons1 : sum(i in items) w[i]*x[i][j] <= capacity[j];
forall (i in items)
cons2 : sum(j in bins) x[i][j] == 1;
}
-谢谢
如果你添加
断言sum(i in items)w [i] <= sum(b in bin)capacity [b];
然后这个断言被违反了,这就解释了为什么你没有得到解决方案。您没有足够的容量。
但是如果你转过身来
int capacity [bins] = [7,5]; //每个垃圾箱的容量
成
int capacity [bins] = [7,7]; //每个垃圾箱的容量
然后你会得到一个解决方案
问候
您可以在CPLEX_Studio1271 \ opl \ examples \ opl \ knapsack中找到背包示例
问候