求教lingo整数规划问题
本帖最后由 daoliyouyuan 于 2015-7-16 11:24 编辑我用lingo写了一个求解背包问题的程序,但运行时提示:
Improper use of attribute name.
23] @bin(y)
请教这个问题怎么解决? 多谢...
model:
sets:
cheque/1..2/: y, c;
order/1..2/: o;
links(cheque, order):x;
endsets
data:
c = 2500, 1500;
o = 1000, 700;
enddata
min = @sum(cheque: y);
@sum(order(j): o(j) * x(1, j)) <= c(1) * y(1);
@sum(order(j): o(j) * x(2, j)) <= c(2) * y(2);
@for(cheque(i) : @sum(order(j): o(j) * x(i, j)) <= c(i) * y(i) );
@for(order(j) : @sum(cheque(i): x(i,j)) = 1);
@for(links: @bin(x));
@bin(y);
end
数学上讲,y是一个向量,你需要用一个@for来表示对每一个y的分量都限定为0-1变量。
改为:
model:
sets:
cheque/1..2/: y, c;
order/1..2/: o;
links(cheque, order):x;
endsets
data:
c = 2500, 1500;
o = 1000, 700;
enddata
min = @sum(cheque: y);
@sum(order(j): o(j) * x(1, j)) <= c(1) * y(1);
@sum(order(j): o(j) * x(2, j)) <= c(2) * y(2);
@for(cheque(i) : @sum(order(j): o(j) * x(i, j)) <= c(i) * y(i) );
@for(order(j) : @sum(cheque(i): x(i,j)) = 1);
@for(links: @bin(x));
@for(cheque:@bin(y));
end
运行结果:
Global optimal solution found.
Objective value: 1.000000
Objective bound: 1.000000
Infeasibilities: 0.000000
Extended solver steps: 0
Total solver iterations: 0
Model Class: PILP
Total variables: 6
Nonlinear variables: 0
Integer variables: 6
Total constraints: 7
Nonlinear constraints: 0
Total nonzeros: 18
Nonlinear nonzeros: 0
Variable Value Reduced Cost
Y( 1) 1.000000 1.000000
Y( 2) 0.000000 1.000000
C( 1) 2500.000 0.000000
C( 2) 1500.000 0.000000
O( 1) 1000.000 0.000000
O( 2) 700.0000 0.000000
X( 1, 1) 1.000000 0.000000
X( 1, 2) 1.000000 0.000000
X( 2, 1) 0.000000 0.000000
X( 2, 2) 0.000000 0.000000
Row Slack or Surplus Dual Price
1 1.000000 -1.000000
2 800.0000 0.000000
3 0.000000 0.000000
4 800.0000 0.000000
5 0.000000 0.000000
6 0.000000 0.000000
7 0.000000 0.000000
稍等会有有人给你解答问题的
!我用lingo写了一个求解背包问题的程序,但运行时提示:
Improper use of attribute name.
23] @bin(y)
请教这个问题怎么解决? 多谢...;
model:
sets:
cheque/1..2/: y, c;
order/1..2/: o;
links(cheque, order):x;
endsets
data:
c = 2500, 1500;
o = 1000, 700;
enddata
min = @sum(cheque: y);
@sum(order(j): o(j) * x(1, j)) <= c(1) * y(1);
@sum(order(j): o(j) * x(2, j)) <= c(2) * y(2);
@for(cheque(i) : @sum(order(j): o(j) * x(i, j)) <= c(i) * y(i) );
@for(order(j) : @sum(cheque(i): x(i,j)) = 1);
@for(links: @bin(x));
!@bin(y);
!用这种方法,限制y的取值为0或1;
@for(cheque: @bin(y));
!或者这种。
!y(1)*(y(1)-1)=0;
!y(2)*(y(2)-1)=0;
end
楼上惊险大神~~~ 楼主有福了
就是需要在结尾处加一个for 语句循环。。因为你的Y定义了2个值 明白了么?
二楼给的解答是值得推荐的,言简意赅。
四楼给的注释中的另一种方案不好,指定决策变量为binary不必整成复杂的complementary constraints,能简化模型一般经验以简化模型为先,而不是复杂化。
真的有福·····
大神················
来刷个体力
页:
[1]