运算符
x = 2;
y = 3*x^10 + 6/(15-x^(1/2));
关系运算符
B = 10;
e = 0.0001;
A - e > B;
逻辑运算符
model:
sets:
factory / 1..6 / : a;
endsets
data:
a = 6,5,4,3,2,1;
enddata
y = @sum( factory(i) | i#ge#5 : a(i) );
end
model:
sets:
factory / 1..6 / : a;
endsets
min = @sum(factory : a);
@for( factory(i) | i#lt#3 #or# i#ge#5 : a(i) > 5 );
end
model:
sets:
fac / 1..3 / : ;
coo(fac,fac) : a;
endsets
data:
a = 1,2,3
4,5,6
7,8,9;
enddata
y = @sum( coo(i,j) | i#le#j : a(i,j) );
end
【注】以上三个问题只是为了熟练逻辑运算符在 for 与 sum 中的使用。
lingo内置函数
if判断
① Lingo 默认所有变量不为负数,故应先进行定义域自由化。
② if 函数语法简单,看一眼即可学会。
@free(x);
@free(y);
x = -10; ! 给 x 一个随机的初值
y = @if( x#ge#0 , x+10 , x-10 );
if 函数的嵌套功能:
x = 1500;
y = @if( x#le#500 , 4*x , @if( x#gt#1000 , 1500+2*x , 500+3*x ));
① if 函数通常仅仅在分段函数处出现,一般其出现频率、使用次数十分之低。
② Lingo 中的 if 函数,必须自带一个 else。
变量定界函数
@free(x);
@free(y);
min = (x+2)^2 + (y-2)^2;
@bnd(1,x,3);
max = 2*x;
x >= 1;
x <= 3;
max =2*x;
第一句可写为 x>1;x<3;替代,但不论从速度还是约束条件数量,都不如用@bnd 函数。
model:
sets:
factory / 1..8 / : a,b,x;
endsets
data:
a = 2,9,3,8,10,6,4,10;
b = 1,3,4,3,3,1,5,10;
enddata
max = @sum(factory : a*x );
@sum(factory : b*x ) < 15;
@for( factory : @bin(x) );
end
model:
sets:
factory / 1..6 / : a,x;
plant / 1..5 / :b;
coo(factory,plant) : c;
endsets
data:
a=2.1 1.0 1.8 1.2 2.0 1.2;
b=6 125 12500 345 5;
c=0.45 20 415 22 0.3
0.45 28 4065 5 0.35
0.65 40 850 43 0.6
0.4 25 75 27 0.2
0.5 26 76 48 0.4
0.5 75 235 8 0.6;
enddata
max = @sum( factory : a*x );
@for( plant(j) : @sum(factory(i):c(i,j)*x(i)) <= b(j) );
@sum( factory : x )=14;
x(2)<=3;
x(4)<=2;
@for( factory(i) | i#ne#2 #and# i#ne#4 : @bnd(1,x(i),4) );
@for( factory : @gin(x) );
end
数学函数
y = @sin(3.14159) + @log(1024) / @log(2) + @abs(-10) + @exp(0);
集合操作函数
为测试函数而生的代码!
model:
sets:
factory / 1..6 / : a;
endsets
data:
a = 6,5,4,3,2,1;
enddata
prod = @prod(factory : a);
greater = @max(factory : a);
less = @min(factory : a);
in = @in(factory,5);
size = @size(factory);
end