本片将会接着上篇实例来写,如要參照能够看下上篇的http://blog.csdn.net/yzu_120702117/article/details/38453791
如有句法上的问题能够參照我之前总结的基础篇,传送门:http://blog.csdn.net/yzu_120702117/article/details/38444485和http://blog.csdn.net/yzu_120702117/article/details/38415153
6、最优选择问题
某钻井队要从10个可供选择的井位中确定5个钻井探油,使总的钻探费用为最小。若10个井位的代号为s1,s2,…,s10,对应的钻探费用c1,c2,…,c10为5,8,10,6,9,5,7,6,10,8.而且井位选择上要满足下列限制条件:
(1) 或选择s1和s7,或选择钻探s9;
(2) 选择了s3或s4就不能选s5,或反过来也一样;
(3) 在s5,s6,s7,s8中最多仅仅能选两个.
试建立这个问题的整数规划模型,确定选择的井位。
取0-1变量s_i,若s_i=1,则表示选取第i个井。若s_i=0,则表示不选取第i个井。
建立数学模型例如以下:
解决的代码例如以下
model:
sets:
variables/1..10/:s,cost;
endsets
data:
cost=5 8 10 6 9 5 7 6 10 8;
enddata
min=@sum(variables:cost*s);
(s(1)+s(7)-2)*(s(9)-1)=0; !约束条件
s(3)*s(5)+s(4)*s(5)=0;
@sum(variables(i)|i#ge#5 #and# i#le#8:s(i))<=2;
@sum(variable:s)=5;
@for(variables:@bin(s));
end
7.运输加选址问题
某公司有六个建筑工地,位置坐标(ai,bi)(单位:公里),水泥日用量di(单位:吨)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXp1XzEyMDcwMjExNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
(1)现有2个料场。位于A(5,1),B(2,7),记(xj。yj)。及,2,日存储量ej各有20吨。
如果工地和料场之间有直线道路。制定每天的供应计划,即从A,B两料场分别向工地运送水泥,是得总的吨公里数最小,当中Cij表示i工地从j料场运来的水泥量。则能够建立模型
这个模型能够这样解答
model:
sets:
demand/1..6/:a,b,d;
supply/1..2/:x,y,e;
link(demand,supply):c;
endsets
data:
a=1.25 8.75 0.5 5.75 3 7.25;
b=1.25 0.75 4.75 5 6.5 7.75;
d=3 5 4 7 6 11;
x=5 2;
y=1 7;
e=20 20;
enddata
min=@sum(link(i,j):c(i,j)*@sqrt((a(i)-x(j))^2+(b(i)-y(j))^2)); !目标函数
@for(demand(i):@sum(supply(j):c(i,j))=d(i));
@for(supply(j):@sum(demand(i):c(i,j))<=e(j));
end
(2)改建两个新料场。须要确定新料场位置(xj,yj)和运量cij。在其它条件不变下使总公里数最小。模型与上面的一样,位置变量变为料场位置(xj,yj),变为非线性优化问题。
model:
sets:
demand/1..6/:a,b,d;
supply/1..2/:x,y,e;
link(demand,supply):c;
endsets
data:
a=1.25 8.75 0.5 5.75 3 7.25;
b=1.25 0.75 4.75 5 6.5 7.75;
d=3 5 4 7 6 11;
e=20 20;
enddata
init: !这里对x,y赋初值
x=5 2;
y=1 7;
endinit
[obj]min=@sum(link(i,j):c(i,j)*@sqrt((a(i)-x(j))^2+(b(i)-y(j))^2));!目标函数;
@for(demand(i):@sum(supply(j):c(i,j))=d(i));
@for(supply(j):@sum(demand(i):c(i,j))<=e(j));
@for(supply:@free(x);@free(y));
end
7.选址问题
某海岛上有12个基本的居民点,每一个居民点的位置(用平面坐标x,y表示。单位km)和居住人数(r)例如以下表所看到的。如今准备在海岛上建一个服务中心为居民提供各种服务。那么服务中心应该建在那里?
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXp1XzEyMDcwMjExNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="666" height="274">
如果建在(a,b)处最合理。
建立模型
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXp1XzEyMDcwMjExNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
求解这个模型:
MODEL:
SETS:
VAR/1..12/:X,Y,R;
ENDSETS
DATA:
X=0 8.20 0.50 5.70 0.77 2.87 4.43 2.58 0.72 9.76 3.19 5.55;
Y=0 0.50 4.90 5.00 6.49 8.76 3.26 9.32 9.96 3.16 7.20 7.88;
R=600 1000 800 1400 1200 700 600 800 1000 1200 1000 1100;
ENDDATA
MIN=@SUM(VAR:@SQRT((X-A)^2+(Y-B)^2)*R);
END
8.非线性整数规划:
这里给出求解
model:
sets:
row/1..4/:b;
col/1..5/:c1,c2,x;
link(row,col):a;
endsets
data:
c1=1,1,3,4,2;
c2=-8,-2,-3,-1,-2;
a=1 1 1 1 1
1 2 2 1 6
2 1 6 0 0
0 0 1 1 5;
b=400,800,200,200;
enddata
max=@sum(col:c1*x^2+c2*x);
@for(row(i):@sum(col(j):a(i,j)*x(j))<b(i));
@for(col:@gin(x));
@for(col:@bnd(0,x,99));
End
9.婚配问题
10对年龄相当的青年,随意一对男女青年配对的概率pij见下表。
试给出一个配对方案。使总的配对概率最大。
w1 w2 w3 w4 w5
w6 w7 w8 w9 w10
m1 0.5828 0.2091 0.4154 0.2140 0.6833 0.4514 0.6085 0.0841 0.1210 0.2319
m2 0.4235 0.3798 0.3050 0.6435 0.2126 0.0439 0.0158 0.4544 0.4508 0.2393
m3 0.5155 0.7833 0.8744 0.3200 0.8392 0.0272 0.0164 0.4418 0.7159 0.0498
m4 0.3340 0.6808 0.0150 0.9601 0.6288 0.3127 0.1901 0.3533 0.8928 0.0784
m5 0.4329 0.4611 0.7680 0.7266 0.1338 0.0129 0.5869 0.1536 0.2731 0.6408
m6 0.2259 0.5678 0.9708 0.4120 0.2071 0.3840 0.0576 0.6756 0.2548 0.1909
m7 0.5798 0.7942 0.9901 0.7446 0.6072 0.6831 0.3676 0.6992 0.8656 0.8439
m8 0.7604 0.0592 0.7889 0.2679 0.6299 0.0928 0.6315 0.7275 0.2324 0.1739
m9 0.5298 0.6029 0.4387 0.4399 0.3705 0.0353 0.7176 0.4784 0.8049 0.1708
m10 0.6405 0.0503 0.4983 0.9334 0.5751 0.6124 0.6927 0.5548 0.9084 0.9943
取xx_ij为0-1型决策变量。
模型为:
这里给出求解
model:
sets:
man/m1..m10/;
woman/w1..w10/;
link(man,woman):p,x;
endsets
data:
p=0.5828 0.2091 0.4154 0.2140 0.6833 0.4514 0.6085 0.0841 0.1210 0.2319
0.4235 0.3798 0.3050 0.6435 0.2126 0.0439 0.0158 0.4544 0.4508 0.2393
0.5155 0.7833 0.8744 0.3200 0.8392 0.0272 0.0164 0.4418 0.7159 0.0498
0.3340 0.6808 0.0150 0.9601 0.6288 0.3127 0.1901 0.3533 0.8928 0.0784
0.4329 0.4611 0.7680 0.7266 0.1338 0.0129 0.5869 0.1536 0.2731 0.6408
0.2259 0.5678 0.9708 0.4120 0.2071 0.3840 0.0576 0.6756 0.2548 0.1909
0.5798 0.7942 0.9901 0.7446 0.6072 0.6831 0.3676 0.6992 0.8656 0.8439
0.7604 0.0592 0.7889 0.2679 0.6299 0.0928 0.6315 0.7275 0.2324 0.1739
0.5298 0.6029 0.4387 0.4399 0.3705 0.0353 0.7176 0.4784 0.8049 0.1708
0.6405 0.0503 0.4983 0.9334 0.5751 0.6124 0.6927 0.5548 0.9084 0.9943;
enddata
max=@prod(man(i):@sum(woman(j):p(i,j)*x(i,j)));
@for(woman(j):@sum(link(i,j):x(i,j))=1);
@for(man(i):@sum(link(i,j):x(i,j))=1);
@for(link:@bin(x));
end
10.填数问题
分别把1,2,…,16填到图示的16个圈内,使得每一个三角形上的全部圈内的数的和为81(共4个三角形)。
决策变量:e_ij=1,第i个圈填数a_j;e_ij=0,第i个圈不填数a_j。
a_j=j,j=1,2,3,…,16。
模型:
这里给出求解
model:
sets:
number/1..16/:a;
link(number,number):e;
tri1(number)/1 2 3 4 5 6 7 8 9/;
tri2(number)/1 2 3 4 16 15 12 11 10/;
tri3(number)/4 5 6 7 14 13 12 15 16/;
tri4(number)/7 8 9 1 10 11 12 13 14/;
endsets
data:
a=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16;
enddata
[obj]max=@sum(link(i,j):e(i,j)*a(j));
@for(number(i):@sum(link(i,j):e(i,j))=1);
@for(number(j):@sum(link(i,j):e(i,j))=1);
@for(link(i,j):@bin(e(i,j)));
@sum(number(j):@sum(tri1(i):e(i,j)*a(j)))=81;
@sum(number(j):@sum(tri2(i):e(i,j)*a(j)))=81;
@sum(number(j):@sum(tri3(i):e(i,j)*a(j)))=81;
@sum(number(j):@sum(tri4(i):e(i,j)*a(j)))=81;
@sum(link(i,j):e(i,j)*a(j))=136;
end
红色的那句程序能够去掉,也能够为:min=@sum(link(i,j):e(i,j)*a(j)),但求的结果不同。结果都符合要求。
编号1~16的圆圈的填数结果至少有3种:
(1)12 11 1 10 7 8 14 13 5 9 4 16 2 6 15 3
(2)14 3 5 15 8 7 13 4 12 6 11 10 9 2 16 1
(3)14 11 4 15 9 8 13 2 5 16 3 10 12 6 1 7
为了求得很多其它的解,能够约束编号1~16的圆圈的填数结果不为以上3种结果。
model:
sets:
number/1..16/:a;
link(number,number):e;
tri1(number)/1 2 3 4 5 6 7 8 9/;
tri2(number)/1 2 3 4 16 15 12 11 10/;
tri3(number)/4 5 6 7 14 13 12 15 16/;
tri4(number)/7 8 9 1 10 11 12 13 14/;
yueshu1:c1;
yueshu2:c2;
yueshu3:c3;
endsets
data:
a=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16;
c1=12 11 1 10 7 8 14 13 5 9 4 16 2 6 15 3;
c2=14 3 5 15 8 7 13 4 12 6 11 10 9 2 16 1;
c3=14 11 4 15 9 8 13 2 5 16 3 10 12 6 1 7;
enddata
[obj]min=@sum(number(i):@sum(number(j):e(i,j)*a(j)));
@for(number(i):@sum(number(j):e(i,j))=1);
@for(number(j):@sum(link(i,j):e(i,j))=1);
@for(link(i,j):@bin(e(i,j)));
@sum(number(j):@sum(tri1(i):e(i,j)*a(j)))=81;
@sum(number(j):@sum(tri2(i):e(i,j)*a(j)))=81;
@sum(number(j):@sum(tri3(i):e(i,j)*a(j)))=81;
@sum(number(j):@sum(tri4(i):e(i,j)*a(j)))=81;
@sum(link(i,j):e(i,j)*a(j))=136;
@sum(yueshu1(j):@sum(link(i,j):e(i,j)))<16;
@sum(yueshu2(j):@sum(link(i,j):e(i,j)))<16;
@sum(yueshu3(j):@sum(link(i,j):e(i,j)))<16;
end
这里给出五个模型,能够与我之前总结的相相应,传送门:http://blog.csdn.net/yzu_120702117/article/details/38453791
手机扫一扫
移动阅读更方便
你可能感兴趣的文章