Ctrl+鼠标滚轮可以放大代码
;
结束+、-、*、/
@free
函数,将变量定义域变为Rmax=200*x1+300*x2;
x1<100;
x2<=120;
x1+2*x2<=160;
a1=1;
a2=2;
a3=3;
a4=4;
a5=5;
max=s;
s=a1*x1;
s=a2*x2;
s=a3*x3;
s=a4*x4;
s=a5*x5;
x1+x2+x3+x4+x5=5000;
用暴力枚举法比较笨拙,所以要使用结合(矩阵工厂)
例子:
sets:
factory /1..6/:a,b;
plant /1..3/:x,y;
endsets
!A、B矩阵有6个元素,X,Y有3个元素。
矩阵工厂不能只生产矩阵,还要给矩阵赋初值才行,例子如下:
sets:
factory /1..6/:a,b;
plant /1..3/:c,x;
endsets
data:
a=1,2,3,4,5,6;
b=6.0,5.0,4.0,3.0,2.0,1.0;
c=10,20,30;
enddata
sets:
gc /1..5/:a,x;
endsets
data:
a=1,2,3,4,5;
enddata
max=s;
题目中第一个约束条件可以用for
循环一步到位。
@for(gc(i):s=a(i)*x(i));
@sum(gc(i):x(i))=5000;
使用矩阵工厂创建矩阵后,整个程序要用model和end包起来:
model:
sets:
gc /1..5/:a,x;
endsets
data:
a=1,2,3,4,5;
enddata
max=s;
@for(gc(i):s=a(i)*x(i));
@sum(gc(i):x(i))=5000;
end
生成6行8列矩阵c,x:
sets:
factory /1..6/:a;
plant /1..8/:d;
Cooperation(factory,plant):c,x;
endsets
model:
sets:
factory /1..6/:a;
plant /1..8/:d;
Coo(factory,plant):c,x;
endsets
data:
a=60,55,51,43,41,52;
d=35,37,22,32,41,32,43,38;
c=6,2,6,7,4,2,5,8
4,9,5,3,8,5,8,2
5,2,1,9,7,4,3,3
7,6,7,3,9,2,7,1
2,3,9,5,7,2,6,5
5,5,2,2,8,1,4,3;
enddata
min=@sum(coo(i,j):c(i,j)*x(i,j));
!或:min=@sum(factory(i):@sum(plant(j):c(i,j)*x(i,j)));
@for(factory(i):@sum(plant(j):x(i,j))<=a(i));
@for(plant(j):@sum(factory(i):x(i,j))=d(j));
end
x=2;
y=3*x^10+6/(15-x^(1/2));
B=10;
e=0.0001;
A-e>B;
model:
sets:
fac /1..6/:a;
endsets
data:
a=6,5,4,3,2,1;
enddata
y=@sum(fac(i)|i#ge#5:a(i)); ! i>=5;
end
i#ge#5
是对sum
求和进行了限制,当然也可以写成#gt4#
b站:https://space.bilibili.com/358229576
model:
sets:
fac /1..6/:a;
endsets
min=@sum(fac(i):a(i));
@for(fac(i)| i#le#2 #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