AMPL如何有一个求和约束

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

我需要根据以下指标总结约束:

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;
sum constraints indices ampl
2个回答
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];

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是非常大的,因为它避免了创建所有地方我都不j为t时的情况下,它可能是更有效的。

© www.soinside.com 2019 - 2024. All rights reserved.