LINGO初印象
LINGO的运算符
简单程序编写
Model:
Title:求解二次规划
!小程序可用,大程序不提倡
[opt] max = 98 * x1 + 277 * x2 - x1 ^ 2 - 0.3*x1*x2 - 2 * x2 ^ 2;
[st1] x1 + x2 < 100;
[st2] x1 < 2 * x2;
@gin(x1);@gin(x2);
end
段的概述: LINGO中建立的优化模型可以由六个部分组成,或称六“段”。
基本集合 与 属性变量
sets: ! 集合段
s/1..100/:x; !基本集合,集合名与属性变量
endsets
!目标与约束段;
@sum(s(i) : x(i)) < 90; !循坏求和函数
sets: ! 集合段
ss/1..10/:b; !基本集合,集合名与属性变量
endsets
data: !数据段;
b = 1 0 1 2 3 5 2 6 1 2;
enddata
sets: ! 集合段
a/1..100/:x; !基本集合,集合名与属性变量
b/1..200/:y; !基本集合,集合名与属性变量
endsets
!目标与约束段
@ for(b(j):@gin(y(j)));
@ for(a(i):@bin(x(i)));
实例:
程序
model:
TITLE 超市大赢家
SETS:
S/1..50/:W,V,X;
ENDSETS
DATA:
V = 220, 208, 198, 192, 180, 180, 165, 162, 160, 158,155, 130,125, 122, 120, 118, 115, 110, 105, 101, 100, 100, 98,96, 95, 90, 88,82, 80, 77, 75, 73, 72, 70, 69, 66, 65, 63, 60, 58,56, 50, 30, 20, 15,10, 8, 5, 3, 1
w = 80, 82, 85, 70, 72, 70, 66, 50, 55, 25, 50, 55, 40, 48,50, 32, 22, 60, 30, 32, 40, 38, 35, 32, 25, 28, 30, 22, 50, 30, 45,30, 60, 50, 20, 65, 20, 25, 30, 10, 20, 25, 15, 10, 10, 10, 4, 4, 2,1
ENDDATA
MAX = @SUM(S(I) : V(I) * X(I));
@SUM(S((I) : W(I)*X(I))) < 1000;
@FOR(S(I):@BIN(X(I)));
END
派生集合与多维数字型变量
sets:
a/1..100/:;
b/1..200/:;
C(a,b):x;
Endsets
! 目标与约束段
@for(b(j):@sum(a(i):x(i,j)) > 150.001);
!集合 元素循坏函数
逻辑运算符与过滤条件(无前面的/)
案例:
sets:
HANG/1..3/:B;
LIE/1..4/:x;
XISHU(HANG,LIE):A;
endsets
DATA:
A = 1 2 3 1
2 5 1 2
3 1 6 -2;
B = 4 5 7;
enddata
@ for(HANG(I) :
@sum(LIE(J) : A(I,J) * X(J)) > B(I));