AMPL如何具有求和约束

时间:2019-02-06 18:13:32

标签: sum constraints indices ampl

我需要一个根据以下索引求和的约束:

subject to shift[1]: x[1,2] + x[1,3] + x[1,4] + x[1,5] + x[1,6] >= 8;
subject to shift[2]: x[1,2] + x[2,3] + x[2,4] + x[2,5] + x[2,6] >= 7;
subject to shift[3]: x[1,3] + x[2,3] + x[3,4] + x[3,5] + x[3,6] >= 12;
subject to shift[4]: x[1,4] + x[2,4] + x[3,4] + x[4,5] + x[4,6] >= 9;
subject to shift[5]: x[1,5] + x[2,5] + x[3,5] + x[4,5] + x[5,6] >= 6;
subject to shift[6]: x[1,6] + x[2,6] + x[3,6] + x[4,6] + x[5,6] >= 10;

我拥有的是:

param n;                        # number of shifts possible
param demand {i in 1..n};       # demand of workers at each shift
var x {1..n, 1..n} >= 0;        # number of workers per shift
# minimize function
subject to shift {t in 1..n}: sum{j in 1..(n)} x[t,j] >= demand[t];

这是错误的,因为它给出以下内容:

subject to shift[1]: x[1,1] + x[1,2] + x[1,3] + x[1,4] + x[1,5] + x[1,6] >= 8;
subject to shift[2]: x[2,1] + x[2,2] + x[2,3] + x[2,4] + x[2,5] + x[2,6] >= 7;
subject to shift[3]: x[3,1] + x[3,2] + x[3,3] + x[3,4] + x[3,5] + x[3,6] >= 12;
subject to shift[4]: x[4,1] + x[4,2] + x[4,3] + x[4,4] + x[4,5] + x[4,6] >= 9;
subject to shift[5]: x[5,1] + x[5,2] + x[5,3] + x[5,4] + x[5,5] + x[5,6] >= 6;
subject to shift[6]: x[6,1] + x[6,2] + x[6,3] + x[6,4] + x[6,5] + x[6,6] >= 10;

2 个答案:

答案 0 :(得分:1)

我找到了答案:

param n;                        # number of shifts possible
param demand {i in 1..n};       # demand of workers at each shift
var x {i in 1..n-1, j in 1..n} >= 0;        # number of workers per shift
#minimize function
subject to shift {t in 1..n}: sum {i in 1..n-1, j in i+1..n} (if i=t || j=t then 1 else 0)*x[i,j] >= demand[t];

答案 1 :(得分:0)

您已经发布了一个很好的解决方案,但是出于多样性的考虑,以下是获得相同结果的其他几种方法:

s.t. shift{t in 1..n}: sum{i in 1..n-1, j in i+1..n: i=t || j=t} x[i,j] >= demand[t]; 

s.t. shift{t in 1..n}: sum{i in 1..t-1, j=t} x[i,j] + sum{i = t, j in t+1..n} x[i,j] >= demand[t];

第二个不太优雅,但是如果n很大,则可能会更有效,因为它避免了创建所有i或j都不等于t的情况。