运筹学实验报告Word文档格式.docx
《运筹学实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《运筹学实验报告Word文档格式.docx(21页珍藏版)》请在冰豆网上搜索。
-1
PleaseinputthecoefficientmatrixoftheconstraintsA(m,n)=[2,5;
1,1;
3,1;
0,1;
-1,0;
0,-1]
A=
25
11
31
01
-10
0-1
Pleaseinputtheresourcearrayoftheprogramb(m)_T=[60,18,44,10,0,0]'
b=
60
18
44
10
0
Optimizationterminated.
Theoptimizationsolutionoftheprogrammingis:
x=
13.0000
5.0000
Theoptimizationvalueoftheprogrammingis:
opt_value=
-31.0000
LINDO程序
在命令窗口键入以下容:
max-2x-y
subjectto
2x+5y<
=60
x+y<
=18
3x+y<
=44
y<
=10
end
按solve键在reportswindow出现:
Globaloptimalsolutionfound.
Objectivevalue:
0.000000
Totalsolveriterations:
VariableValueReducedCost
X0.0000002.000000
Y0.0000001.000000
RowSlackorSurplusDualPrice
10.0000001.000000
260.000000.000000
318.000000.000000
444.000000.000000
510.000000.000000
2、整数规划
课本第二章79页1题
Maxz=100x1+180x2+70x3
s.t.40x1+50x2+60x3≤10000
3x1+6x2+2x3≤600
x1≤130
X2≤80
x3≤200
x1x2x3≥0
程序运行及结果:
biprogram
the
program
is
with
binary
linear
programming
Please
input
constraints
number
of
programming
m=5
m
=
5
variant
n=5
n
cost
array
objective
function
c(n)_T=[100,180,70]'
c
100
180
70
coefficient
matrix
A(m,n)=[40,50,60;
3,6,2;
1,0,0;
0,1,0;
0,0,1]
A
40
50
60
3
6
2
1
0
1
resource
b(m)_T=[10000;
600;
130;
80;
200]
b
10000
600
130
80
200
Optimization
terminated.
The
optimization
solution
is:
x
value
opt_value
程序名:
intprogramb程序说明:
%
programm
integer
use
branch
and
bound
method!
%这个程序是用分支定界法解决整数规划问题
please
parameters
in
main
mand
winows
%请在命令窗口输入这个主要定义函数的参数
[x,f]=ILp(c,A,b,vlb,vub,x0,neqcstr,pre)
min
f=c'
*x,s.t.
A*x<
=b,vlb<
=x<
=vub
f的最小值等于c的转置乘以x,A乘以x小于等于b,x大于等于vlb小于等于vub
vectors
required
as
integers
whole
x是整个的整数需要%
x0
initialization,'
[]'
also
ok
x0是初始值,"
[]"
也可以是。
neqcstr
equational
constraints,when
can
be
delete
neqcstr是平均约束条件的数目,当0能删除时%
pre
concise
rate
pre是简明率%
f
optimal
value
x是整数规划,f
是最优值
%%%%%%%%%%%%%%%%
if
nargin<
8,pre=0;
nargin
factually
variants
〔这个参数是实际输入的变量个数〕
7,neqcstr=0;
6,x0=[];
5,vub=[];
4,vlb=[];
%%%%%%%%%%%%%%%%%%
set
to
column
vectors
%建立列向量x0=x0(:
);
c=c(:
b=b(:
vlb=vlb(:
vub=vub(:
mm=1;
j=1;
nvars=length(c'
variants〔变量的个数〕
fvub=inf;
xall=[];
fall=[];
x_f_b=[];
[xtemp,ztemp,how]=lp(c,A,b,vlb,vub,x0,neqcstr,-1);
ftemp=c'
*xtemp;
%%%%%%%%%%%%%%%%%%%%%%%
strcmp(how,'
ok'
)
pare
between
how
ok〔how和ok之间的比拟〕
temp0=round(xtemp);
%临时变量四舍五入
temp1=floor(xtemp);
%取比其小的整数
temp2=find(abs(xtemp-temp0)>
pre);
mtemp=length(temp2);
~isempty(temp2)
x_f_b=[xtemp;
ftemp;
vlb;
vub];
while
j<
=mm
i=1;
i<
=mtemp
%%%%%%%%%%%%%%%%%%%%%
x_f_b(nvars+1,j)<
=fvub
vlbl=x_f_b(nvars+2:
2*nvars+1,j);
vubl=x_f_b(2*nvars+2:
3*nvars+1,j);
vubl(temp2(i))=temp1(temp2(i));
[xtemp,z,how]=lp(c,[A;
c'
],[b;
fvub],vlbl,vubl,x0,neqcstr,-1);
)
templ0=round(xtemp);
templ1=floor(xtemp);
templ2=find(abs(xtemp-templ0)>
isempty(templ2)
xall=[xall,xtemp];
fall=[fall,ftemp];
fvub=min([fvub,fall]);
elseif
ftemp<
x_f_b=[x_f_b,[xtemp;
vlbl;
vubl]];
vlbr=x_f_b(nvars+2:
vlbr(temp2(i))=temp1(temp2(i))+1;
vubr=x_f_b(2*nvars+2:
fvub],vlbr,vubr,x0,neqcstr,-1);
tempr0=round(xtemp);
tempr1=floor(xtemp);
tempr2=find(abs(xtemp-tempr0)>
isempty(tempr2)
vlbr;
vubr]];
i=i+1;
end
second
xint=x_f_b(1:
nvars,:
[m,mm]=size(xint);
j=j+1;
j>
mm
break
because
break
〔因为中断而完毕〕
temp0=round(xint(:
j));
temp1=floor(xint(:
temp2=find(abs(xint(:
j)-temp0)>
while〔完毕当前〕
else
correspond
if〔符合第一个如果〕
x=xtemp;
f=ftemp;
if〔第二个如果的完毕〕
%%%%%%%%%%%%%%%%%%5
~isempty(fall)
fmin=min(fall);
nmin=find(fall==fmin);
x=xall(:
nmin);
f=fmin;
first
x=nan*ones(1,nvars);
end
例99页第6题第二问
max-11x1-4x2
st
-x1+2x2<
=4
5x1+2x2<
=16
2x1-x2<
ginx1
ginx2
Extendedsolversteps:
X10.00000011.00000
X20.0000004.000000
24.0000000.000000
316.000000.000000
44.0000000.000000
3、非线性规划
unpfun1函数
unpfun1函数执行实例:
〔课本第四章152页16题〕
min4x1+6x2-2x1x2-2x^2,取初始点x0=〔1;
1〕
f=4*x
(1)-2*x
(1)*x
(2)-x
(2)^2-6*x
(2);
[x,fval]=fminunc(unpfun1,x0)〔调用无约束线性规划函数〕
按运行按钮在solutionreport窗口得到以下结果:
Warning:
Gradientmustbeprovidedfortrust-regionmethod;
usingline-searchmethodinstead.
>
Infminuncat265
Optimizationterminated:
relativeinfinity-normofgradientlessthanoptions.TolFun.
1.0e-006*
0.2541-0.2029
fval=
1.3173e-013
4、动态规划
dynamic;
dynfun1_1,dynfun1_2,dynfun1_3;
例180页第一题
程序说明:
dynamic程序:
%theprogrammiswiththedynamicprogrammingusetherecurisivemethodforthelasttofirst
%thisisthemainfunctionofthemethod
function[p_opt,fval,u]=dynprog(x,DecisFun,ObjFun,TransFun)
%thefunctionistosolvethedynamicexampleinthetextbook
%xisthesituationvariantanditscolumnnumberrepresentthestagesituation
%subfunctionDecisFun(k,x)istosolvethedecisionvariantofkstagevariantx
%subfunctionObjFun(k,x,u)istostageindexfunction
%subfunctionTransFun(k,x,u)isthestagetransformationfunction,uisthecorrespondingdecisionvariant
%p_opthasfouroutput,thefirstisthenumberofthestage,thesecondistheoptimalroadofdecision
%thethirdistheoptimalstategiesofthedecision,theforthistheindexfunctiongroup.
%fvalisacolumnvector,istorepresenttheoptimalvaluecorrespendtotheinitialstageisx
%
k=length(x(1,:
));
f_opt=nan*ones(size(x));
d_opt=f_opt;
t_vubm=inf*ones(size(x));
x_isnan=~isnan(x);
t_vub=inf;
%%%%%%%%%%%%%%%%%
%tocaculatetheteminatevalues
tmp1=find(x_isnan(:
k));
tmp2=length(tmp1);
fori=1:
tmp2
u=feval(DecisFun,k,x(i,k));
tmp3=length(u);
forj=1:
tmp3
tmp=feval(ObjFun,k,x(tmp1(i),k),u(j));
iftmp<
=t_vub
f_opt(i,k)=tmp;
d_opt(i,k)=u(j);
t_vub=tmp;
%recurisive
forii=k-1:
-1:
tmp10=find(x_isnan(:
ii));
tmp20=length(tmp10);
tmp20
u=feval(DecisFun,ii,x(i,ii));
tmp30=length(u);
tmp30
tmp00=feval(ObjFun,ii,x(tmp10(i),ii),u(j));
tmp40=feval(TransFun,ii,x(tmp10(i),ii),u(j));
tmp50=x(:
ii+1)-tmp40;
tmp60=find(tmp50==0);
if~isempty(tmp60),
ifnargin<
tmp00=tmp00+f_opt(tmp60
(1),ii+1);
iftmp00<
=t_vubm(i,ii)
f_opt(i,ii)=tmp00;
d_opt(i,ii)=u(j);
t_vubm(i,ii)=tmp00;
fval=f_opt(tmp1,1);
%towritetheindexandparameterorresult
p_opt=[];
tmpx=[];
tmpd=[];
tmpf=[];
tmp0=find(x_isnan(:
1));
tmp01=length(tmp0);
tmp01
tmpd(i)=d_opt(tmp0(i),1);
tmpx(i)=x(tmp0(i),1);
tmpf(i)=feval(ObjFun,1,tmpx(i),tmpd(i));
p_opt(k*(i-1)+1,[1,2,3,4])=[1,tmpx(i),tmpd(i),tmpf(i)];
forii=2:
k
tmpx(i)=feval(TransFun,ii-1,tmpx(i),tmpd(i));
tmp1=x(:
ii)-tmpx(i);
tmp2=find(tmp1==0);
if~isempty(tmp2)
tmpd(i)=d_opt(tmp2