Merge pull request '三人三个文件夹' (#2) from 盘荣博 into main

王朝群
p3itgm2rp 5 months ago
commit aa9c8889bd

@ -1,40 +1,40 @@
clc; clear; close all; format long g; clc; clear; close all; format long g;
prob=optimproblem; prob=optimproblem;
x=optimvar('x',9,1,'LowerBound',0,'Type','integer'); x=optimvar('x',9,1,'LowerBound',0,'Type','integer');
p=[0.25,0.35,0.50]; p=[0.25,0.35,0.50];
r=[1.25,2.00,2.80]; r=[1.25,2.00,2.80];
ot=[5,10,0;7,9,12;6,8,0;4,0,11;7,0,0]; ot=[5,10,0;7,9,12;6,8,0;4,0,11;7,0,0];
total=1000:200:3000; total=1000:200:3000;
odp=[300/6000,321/10000,250/4000,783/7000,200/4000]; odp=[300/6000,321/10000,250/4000,783/7000,200/4000];
X=[]; X=[];
Q=[]; Q=[];
for i=1:length(total) for i=1:length(total)
% prob.Objective=(ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7))*odp(1)+(ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9))*odp(2)+(ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8)))*odp(3)+(ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9))*odp(4)+(ot(5,1)*(x(3)+x(6)))*odp(5)-(r(1)-p(1))*ones(1,6)*x(1:6)-(r(2)-p(2))*ones(1,2)*x(7:8)-(r(3)-p(3))*x(9); % prob.Objective=(ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7))*odp(1)+(ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9))*odp(2)+(ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8)))*odp(3)+(ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9))*odp(4)+(ot(5,1)*(x(3)+x(6)))*odp(5)-(r(1)-p(1))*ones(1,6)*x(1:6)-(r(2)-p(2))*ones(1,2)*x(7:8)-(r(3)-p(3))*x(9);
dp1=(ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7))*odp(1); dp1=(ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7))*odp(1);
dp2=(ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9))*odp(2); dp2=(ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9))*odp(2);
dp3=(ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8)))*odp(3); dp3=(ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8)))*odp(3);
dp4=(ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9))*odp(4); dp4=(ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9))*odp(4);
dp5=(ot(5,1)*(x(3)+x(6)))*odp(5); dp5=(ot(5,1)*(x(3)+x(6)))*odp(5);
dp=dp1+dp2+dp3+dp4+dp5; dp=dp1+dp2+dp3+dp4+dp5;
prob.Objective=dp-(r(1)-p(1))*ones(1,6)*x(1:6)-(r(2)-p(2))*ones(1,2)*x(7:8)-(r(3)-p(3))*x(9); prob.Objective=dp-(r(1)-p(1))*ones(1,6)*x(1:6)-(r(2)-p(2))*ones(1,2)*x(7:8)-(r(3)-p(3))*x(9);
prob.Constraints.con1=p(1)*ones(1,6)*x(1:6)+p(2)*ones(1,2)*x(7:8)+p(3)*x(9)+dp<=total(i);%总费用 prob.Constraints.con1=p(1)*ones(1,6)*x(1:6)+p(2)*ones(1,2)*x(7:8)+p(3)*x(9)+dp<=total(i);%总费用
prob.Constraints.con2=ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7)<=6000;%A1 prob.Constraints.con2=ot(1,1)*ones(1,3)*x(1:3)+ot(1,2)*x(7)<=6000;%A1
prob.Constraints.con3=ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9)<=10000; prob.Constraints.con3=ot(2,1)*ones(1,3)*x(4:6)+ot(2,2)*x(8)+ot(2,3)*x(9)<=10000;
prob.Constraints.con4=ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8))<=4000; prob.Constraints.con4=ot(3,1)*(x(1)+x(4))+ot(3,2)*(x(7)+x(8))<=4000;
prob.Constraints.con5=ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9)<=7000; prob.Constraints.con5=ot(4,1)*(x(2)+x(5))+ot(4,3)*x(9)<=7000;
prob.Constraints.con6=ot(5,1)*(x(3)+x(6))<=4000; prob.Constraints.con6=ot(5,1)*(x(3)+x(6))<=4000;
[sol,fval,flag,out]=solve(prob); [sol,fval,flag,out]=solve(prob);
xx=sol.x; xx=sol.x;
X=[X,xx]; X=[X,xx];
Q=[Q,-fval]; Q=[Q,-fval];
end end
plot(total,Q,'*-'); plot(total,Q,'*-');

@ -1,61 +1,61 @@
%27页例题 %27页例题
clc; clear; close all; clc; clear; close all;
n = 10000; % 使用较小的 n 值以便更容易可视化 n = 10000; % 使用较小的 n 值以便更容易可视化
x = unifrnd(0, 12, [1, n]); x = unifrnd(0, 12, [1, n]);
y = unifrnd(0, 9, [1, n]); y = unifrnd(0, 9, [1, n]);
ans=sum(y < x.^2 & x <= 3)+sum(y < 12 - x & x >= 3); ans=sum(y < x.^2 & x <= 3)+sum(y < 12 - x & x >= 3);
ans=ans/n; ans=ans/n;
% 找出满足条件的点 % 找出满足条件的点
condition1 = y < x.^2 & x <= 3; condition1 = y < x.^2 & x <= 3;
condition2 = y < 12 - x & x >= 3; condition2 = y < 12 - x & x >= 3;
condition_met = condition1 | condition2; % 满足任一条件的点 condition_met = condition1 | condition2; % 满足任一条件的点
condition_not_met = ~condition_met; % 不满足任何条件的点 condition_not_met = ~condition_met; % 不满足任何条件的点
% 创建图形窗口 % 创建图形窗口
figure; figure;
hold on;%在同一张图上绘图 hold on;%在同一张图上绘图
% 绘制不满足任何条件的点 % 绘制不满足任何条件的点
scatter(x(condition_not_met), y(condition_not_met), 'k.'); % k----黑色 .----绘制样式 scatter(x(condition_not_met), y(condition_not_met), 'k.'); % k----黑色 .----绘制样式
%scatter绘制散点图 %scatter绘制散点图
%x(condition_not_met) 会返回一个新的向量,其中只包含 x 中对应 condition_not_met 为 true 的元素。 %x(condition_not_met) 会返回一个新的向量,其中只包含 x 中对应 condition_not_met 为 true 的元素。
% 绘制满足第一个条件的点 % 绘制满足第一个条件的点
scatter(x(condition1), y(condition1), 'r.'); % 红色 scatter(x(condition1), y(condition1), 'r.'); % 红色
% 绘制满足第二个条件的点 % 绘制满足第二个条件的点
scatter(x(condition2), y(condition2), 'b.'); % 蓝色 scatter(x(condition2), y(condition2), 'b.'); % 蓝色
% 添加图例和标签 % 添加图例和标签
legend('不满足任何条件的点', '满足 y < x^2 且 x <= 3 的点', '满足 y < 12 - x 且 x >= 3 的点'); legend('不满足任何条件的点', '满足 y < x^2 且 x <= 3 的点', '满足 y < 12 - x 且 x >= 3 的点');
xlabel('x'); xlabel('x');
ylabel('y'); ylabel('y');
title('随机生成的点和满足条件的点'); title('随机生成的点和满足条件的点');
hold off; hold off;
%蒙特卡洛法求圆周率qw %蒙特卡洛法求圆周率qw
clc;clear;close all; clc;clear;close all;
n=10^5; n=10^5;
x=unifrnd(-1,1,[1,n]); x=unifrnd(-1,1,[1,n]);
y=unifrnd(-1,1,[1,n]); y=unifrnd(-1,1,[1,n]);
con1=x.^2+y.^2<=1; con1=x.^2+y.^2<=1;
con2=~con1; con2=~con1;
ans=sum(x.^2+y.^2<=1); ans=sum(x.^2+y.^2<=1);
ans=ans/n*4; ans=ans/n*4;
figure ; figure ;
hold on; hold on;
scatter(x(con1),y(con1),'r.'); scatter(x(con1),y(con1),'r.');
scatter(x(con2),y(con2),'k.'); scatter(x(con2),y(con2),'k.');
hold off; hold off;

@ -1,135 +1,135 @@
%非线性规划 %非线性规划
%P42例题 %P42例题
%求解 %求解
clc,clear,format long g,close all; clc,clear,format long g,close all;
syms x1 x2;%符号变量(用于定义函数、求解方程、计算导数和积分) syms x1 x2;%符号变量(用于定义函数、求解方程、计算导数和积分)
f=(339-0.01*x1-0.003*x2)*x1+(399-0.004*x1-0.01*x2)*x2-(195*x1+225*x2+400000); f=(339-0.01*x1-0.003*x2)*x1+(399-0.004*x1-0.01*x2)*x2-(195*x1+225*x2+400000);
f=simplify(f); f=simplify(f);
f1=diff(f,x1),f2=diff(f,x2); f1=diff(f,x1),f2=diff(f,x2);
[x10,x20]=solve(f1,f2);%求驻点即使f1,f2为0的点 [x10,x20]=solve(f1,f2);%求驻点即使f1,f2为0的点
x10=round(double(x10));%四舍五入 x10=round(double(x10));%四舍五入
x20=round(double(x20)); x20=round(double(x20));
f0=subs(f,{x1,x2},{x10,x20}); f0=subs(f,{x1,x2},{x10,x20});
f0=double(f0); f0=double(f0);
subplot(121); subplot(121);
fmesh(f,[0,10000,0,10000]);%3维 fmesh(f,[0,10000,0,10000]);%3维
%xlabel('x1','Interpreter','latex'); %xlabel('x1','Interpreter','latex');
%ylabel('y1','Interpreter','latex'); %ylabel('y1','Interpreter','latex');
xlabel('x1'); xlabel('x1');
ylabel('y1'); ylabel('y1');
subplot(122); subplot(122);
L=fcontour(f,[0,10000,0,10000]); L=fcontour(f,[0,10000,0,10000]);
contour(L.XData,L.YData,L.ZData,'ShowText','on'); contour(L.XData,L.YData,L.ZData,'ShowText','on');
xlabel('x1'); xlabel('x1');
ylabel('y1'); ylabel('y1');
p1=339-0.01*x10-0.003*x20; p1=339-0.01*x10-0.003*x20;
p2=399-0.004*x10-0.01*x20; p2=399-0.004*x10-0.01*x20;
c=195*x10+225*x20+400000; c=195*x10+225*x20+400000;
rate=f0/c; rate=f0/c;
%灵敏性检验 分析 最优解 对 19村彩电价格下降幅度 的灵敏性 %灵敏性检验 分析 最优解 对 19村彩电价格下降幅度 的灵敏性
clc,clear,close all,format long g; clc,clear,close all,format long g;
syms x1 x2 a; syms x1 x2 a;
X1=4735,X2=7043,AA=0.01,FF=553641; X1=4735,X2=7043,AA=0.01,FF=553641;
f=(339-a*x1-0.003*x2)*x1+(399-0.004*x1-0.01*x2)*x2-(195*x1+225*x2+400000); f=(339-a*x1-0.003*x2)*x1+(399-0.004*x1-0.01*x2)*x2-(195*x1+225*x2+400000);
f1=diff(f,x1),f2=diff(f,x2); f1=diff(f,x1),f2=diff(f,x2);
[x10,x20]=solve(f1,f2); [x10,x20]=solve(f1,f2);
subplot(121); subplot(121);
fplot(x10,[0.002,0.02]); fplot(x10,[0.002,0.02]);
xlabel('a'); xlabel('a');
ylabel('x1'); ylabel('x1');
subplot(122); subplot(122);
fplot(x20,[0.002,0.02]); fplot(x20,[0.002,0.02]);
xlabel('a'); xlabel('a');
ylabel('x2'); ylabel('x2');
dx1=diff(x10,a); dx1=diff(x10,a);
dx10=subs(dx1,a,0.01),dx10=double(dx10); dx10=subs(dx1,a,0.01),dx10=double(dx10);
sx1a=dx10*AA/X1; sx1a=dx10*AA/X1;
dx2=diff(x20,a); dx2=diff(x20,a);
dx20=subs(dx2,a,0.01),dx20=double(dx20); dx20=subs(dx2,a,0.01),dx20=double(dx20);
sx2a=dx20*AA/X2; sx2a=dx20*AA/X2;
F=subs(f,[x1,x2],[x10,x20]); F=subs(f,[x1,x2],[x10,x20]);
F=simplify(F); F=simplify(F);
figure,fplot(F,[0.002,0.02]); figure,fplot(F,[0.002,0.02]);
xlabel('a'); xlabel('a');
ylabel('profit'); ylabel('profit');
dpro=diff(F,a); dpro=diff(F,a);
dpro0=subs(F,a,0.01); dpro0=subs(F,a,0.01);
sya=dpro0*AA/FF; sya=dpro0*AA/FF;
%当a提高10% %当a提高10%
f3=subs(f,[x1,x2,a],[X1,X2,0.011]),f3=double(f3); f3=subs(f,[x1,x2,a],[X1,X2,0.011]),f3=double(f3);
f4=subs(F,[a],0.011); f4=subs(F,[a],0.011);
%相对误差 %相对误差
delta=(f4-f3)/f4; delta=(f4-f3)/f4;
delta=double(delta); delta=double(delta);
%相对误差很小说明在19寸的价格下降幅度在一定范围内发生变化时仍然按照不变时的最优解来确定生产方案仍然可以获得几乎最优的利润。 %相对误差很小说明在19寸的价格下降幅度在一定范围内发生变化时仍然按照不变时的最优解来确定生产方案仍然可以获得几乎最优的利润。
%使用fmincon的简单例子 %使用fmincon的简单例子
%蒙特卡罗确定初始值 %蒙特卡罗确定初始值
clc,clear,close all; clc,clear,close all;
n=10^7; n=10^7;
x1=unifrnd(0,12,[1,n]); x1=unifrnd(0,12,[1,n]);
x2=unifrnd(0,12,[1,n]); x2=unifrnd(0,12,[1,n]);
fmin=+inf; fmin=+inf;
for i=1:n for i=1:n
x=[x1(i),x2(i)]; x=[x1(i),x2(i)];
if ((x1(i)-1)^2-x2(i)-6<=0) & (-2*x1(i)+3*x2(i)-6<=0) if ((x1(i)-1)^2-x2(i)-6<=0) & (-2*x1(i)+3*x2(i)-6<=0)
temp=x(1)^2+x(2)^2-x(1)*x(2)-2*x(1)-5*x(2); temp=x(1)^2+x(2)^2-x(1)*x(2)-2*x(1)-5*x(2);
if temp<fmin if temp<fmin
fmin=temp; fmin=temp;
x0=x; x0=x;
end end
end end
end end
%[x,fval]= fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) %[x,fval]= fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
A=[-2,3]; A=[-2,3];
b=6; b=6;
[x,fval]=fmincon(@fun1,x0,A,b,[],[],[],[],@nonlfun1); [x,fval]=fmincon(@fun1,x0,A,b,[],[],[],[],@nonlfun1);
%option=optimoptions('fmincon','Algorithm','sqp'); %option=optimoptions('fmincon','Algorithm','sqp');
%[x,fval]=fmincon(@fun1,x0,A,b,[],[],[],[],@nonlfun1,option); %[x,fval]=fmincon(@fun1,x0,A,b,[],[],[],[],@nonlfun1,option);
disp('Thw optimal solution is:'); disp('Thw optimal solution is:');
disp(x); disp(x);
disp('The minimum value is:'); disp('The minimum value is:');
disp(fval); disp(fval);
figure; figure;
fmesh(@(x1, x2) arrayfun(@(x1, x2) fun1([x1, x2]), x1, x2), [-4, 4, -4, 4]);%用法 fmesh(@(x1, x2) arrayfun(@(x1, x2) fun1([x1, x2]), x1, x2), [-4, 4, -4, 4]);%用法
xlabel('x1'); xlabel('x1');
ylabel('x2'); ylabel('x2');
zlabel('f(x1,x2)'); zlabel('f(x1,x2)');
%非线性规划---料场选址 %非线性规划---料场选址
clc,clear,close all; clc,clear,close all;
a=[1.25,8.75,0.5,5.75,3,7.25]; a=[1.25,8.75,0.5,5.75,3,7.25];
b=[1.25,0.75,4.75,5,6.5,7.25]; b=[1.25,0.75,4.75,5,6.5,7.25];
A=[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0]; A=[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0];
bb=[20;20]; bb=[20;20];
Aeq=[eye(6),eye(6),zeros(6,4)]; Aeq=[eye(6),eye(6),zeros(6,4)];
beq=[3;5;4;7;6;11;]; beq=[3;5;4;7;6;11;];
lb=[zeros(12,1);-inf;-inf;-inf;-inf]; lb=[zeros(12,1);-inf;-inf;-inf;-inf];
x0=[3,5,0,7,0,1,0,0,4,0,6,10,5,1,2,7]; x0=[3,5,0,7,0,1,0,0,4,0,6,10,5,1,2,7];
[xans,fvans]=fmincon(@fun2,x0,A,bb,Aeq,beq,lb,[]); [xans,fvans]=fmincon(@fun2,x0,A,bb,Aeq,beq,lb,[]);

@ -1,3 +1,3 @@
function f=fun1(x) function f=fun1(x)
f=x(1)*x(1)+x(2)*x(2)-x(1)*x(2)-2*x(1)-5*x(2); f=x(1)*x(1)+x(2)*x(2)-x(1)*x(2)-2*x(1)-5*x(2);
end end

@ -1,15 +1,15 @@
function f=fun2(x) function f=fun2(x)
a=[1.25,8.75,0.5,5.75,3,7.25]; a=[1.25,8.75,0.5,5.75,3,7.25];
b=[1.25,0.75,4.75,5,6.5,7.25]; b=[1.25,0.75,4.75,5,6.5,7.25];
f1 = 0; % 初始化f1 f1 = 0; % 初始化f1
f2 = 0; % 初始化f2 f2 = 0; % 初始化f2
for i=1:6 for i=1:6
s=sqrt((a(i)-x(13))^2+(b(i)-x(14))^2); s=sqrt((a(i)-x(13))^2+(b(i)-x(14))^2);
f1=x(i)*s+f1; f1=x(i)*s+f1;
end end
for i=7:12 for i=7:12
s=sqrt((a(i-6)-x(15))^2+(b(i-6)-x(16))^2); s=sqrt((a(i-6)-x(15))^2+(b(i-6)-x(16))^2);
f2=s*x(i)+f2; f2=s*x(i)+f2;
end end
f=f1+f2; f=f1+f2;
end end

@ -1,49 +1,49 @@
%整数规划 %整数规划
%P23 ej2.5 %P23 ej2.5
%optimproblem解法 %optimproblem解法
clc; clear; close all; format long g; clc; clear; close all; format long g;
prob=optimproblem; prob=optimproblem;
x=optimvar('x',6,'LowerBound',0,'Type','integer'); x=optimvar('x',6,'LowerBound',0,'Type','integer');
prob.Objective=sum(x); prob.Objective=sum(x);
cnt=[35,40,50,45,55,30]; cnt=[35,40,50,45,55,30];
con=optimconstr(6); con=optimconstr(6);
con(1)=x(1)+x(6)>=35; con(1)=x(1)+x(6)>=35;
for i=1:5 for i=1:5
con(i+1)=x(i)+x(i+1)>=cnt(i+1); con(i+1)=x(i)+x(i+1)>=cnt(i+1);
end end
prob.Constraints.con=con; prob.Constraints.con=con;
[sol,fval,flag,out]=solve(prob); [sol,fval,flag,out]=solve(prob);
X=sol.x; X=sol.x;
%linprog解法 %linprog解法
clc; clear; close all; format long g; clc; clear; close all; format long g;
f=[1,1,1,1,1,1]; f=[1,1,1,1,1,1];
intcon=[1:6]; intcon=[1:6];
A=zeros(6,6); A=zeros(6,6);
A(1,1)=-1; A(1,1)=-1;
A(1,6)=-1; A(1,6)=-1;
for i=1:5 for i=1:5
A(i+1,i)=-1; A(i+1,i)=-1;
A(i+1,i+1)=-1; A(i+1,i+1)=-1;
end end
lb=zeros(6,1);%注意不是lb=0 lb=zeros(6,1);%注意不是lb=0
b=[-35;-40;-50;-45;-55;-30]; b=[-35;-40;-50;-45;-55;-30];
[x,fval]=intlinprog(f,intcon,A,b,[],[],lb); [x,fval]=intlinprog(f,intcon,A,b,[],[],lb);
%01规划 %01规划
%背包问题 %背包问题
f=-[540,200,180,350,60,150,280,450,320,120]; f=-[540,200,180,350,60,150,280,450,320,120];
intcon=1:10; intcon=1:10;
lb=zeros(10); lb=zeros(10);
ub=ones(10); ub=ones(10);
A=[6,3,4,5,1,2,3,5,4,2]; A=[6,3,4,5,1,2,3,5,4,2];
b=30; b=30;
[x,fval]=intlinprog(f,intcon,A,b,[],[],lb,ub); [x,fval]=intlinprog(f,intcon,A,b,[],[],lb,ub);

@ -1,140 +1,140 @@
%投资利益与风险1998A题 %投资利益与风险1998A题
%模型一 给定风险承受程度,求最大利益 %模型一 给定风险承受程度,求最大利益
f=[-0.05,-0.27,-0.19,-0.185,-0.185]; f=[-0.05,-0.27,-0.19,-0.185,-0.185];
%A矩阵 %A矩阵
%A=[0,0.025,0.015,0.055,0.026]; %错误 %A=[0,0.025,0.015,0.055,0.026]; %错误
%b=[1,1,1,1,1]; %b=[1,1,1,1,1];
A=[zeros(4,1),diag([0.025,0.015,0.055,0.026])];%不等式约束条件矩阵 A=[zeros(4,1),diag([0.025,0.015,0.055,0.026])];%不等式约束条件矩阵
%Aeq、beq %Aeq、beq
Aeq=[1,1.01,1.02,1.045,1.065]; Aeq=[1,1.01,1.02,1.045,1.065];
beq=1; beq=1;
%lb %lb
%lb=0; %错误 %lb=0; %错误
lb=zeros(5,1); lb=zeros(5,1);
%可承担风险率a %可承担风险率a
a=(0:0.001:0.05); a=(0:0.001:0.05);
%保存最优解 %保存最优解
Q=zeros(1,length(a)); Q=zeros(1,length(a));
xx=[];%空矩阵存放最优解对应x的值 xx=[];%空矩阵存放最优解对应x的值
for i=1:length(a) for i=1:length(a)
b=a(i)*ones(4,1); b=a(i)*ones(4,1);
[x,y]=linprog(f,A,b,Aeq,beq,lb); [x,y]=linprog(f,A,b,Aeq,beq,lb);
Q(i)=-y;%注意取负!!! Q(i)=-y;%注意取负!!!
xx=[xx;x']; xx=[xx;x'];
end end
plot(a,Q,'*r'); plot(a,Q,'*r');
xlabel("风险率"); xlabel("风险率");
ylabel("最大收益"); ylabel("最大收益");
%模型二 收益、风险按权重组合 %模型二 收益、风险按权重组合
% f0=[-0.05,-0.27,-0.19,-0.185,-0.185]; % f0=[-0.05,-0.27,-0.19,-0.185,-0.185];
% w=(0:0.1:1); % w=(0:0.1:1);
% Aeq=[1,1.01,1.02,1.045,1.065,0]; % Aeq=[1,1.01,1.02,1.045,1.065,0];
% beq=1; % beq=1;
% lb=0; % lb=0;
% xx=[]; % xx=[];
% Q=zeros(1,length(w)); % Q=zeros(1,length(w));
% A=[zeros(5,1),diag([0.025,0.025,0.055,0.065,0])]; % A=[zeros(5,1),diag([0.025,0.025,0.055,0.065,0])];
% b=ones(5,1); % b=ones(5,1);
% for i=1:length(w) % for i=1:length(w)
% f=[-w(i)*f0,1-w(i)]; % f=[-w(i)*f0,1-w(i)];
% b=x(end)*b; % b=x(end)*b;
% [x,y]=linprog(f,A,b,Aeq,beq,lb); % [x,y]=linprog(f,A,b,Aeq,beq,lb);
% Q(i)=-y; % Q(i)=-y;
% xx=[xx,x']; % xx=[xx,x'];
% end % end
% plot(w,Q,'*r'); % plot(w,Q,'*r');
%模型二 收益、风险按权重组合 %模型二 收益、风险按权重组合
clc; clear; close all; format long g; clc; clear; close all; format long g;
M = 10000; M = 10000;
prob = optimproblem; prob = optimproblem;
x = optimvar('x', 6, 1, 'LowerBound', 0); x = optimvar('x', 6, 1, 'LowerBound', 0);
r = [0.05, 0.28, 0.21, 0.23, 0.25]; r = [0.05, 0.28, 0.21, 0.23, 0.25];
p = [0, 0.01, 0.02, 0.045, 0.065]; p = [0, 0.01, 0.02, 0.045, 0.065];
q = [0, 0.025, 0.015, 0.055, 0.026]'; q = [0, 0.025, 0.015, 0.055, 0.026]';
%w = 0:0.1:1; %w = 0:0.1:1;
w = 0.7:0.03:1; w = 0.7:0.03:1;
V = []; V = [];
Q = []; Q = [];
X = []; X = [];
prob.Constraints.con1 = (1 + p) * x(1:end-1) == M; prob.Constraints.con1 = (1 + p) * x(1:end-1) == M;
prob.Constraints.con2 = (q(2:end).* x(2:end-1))<= x(end); %下标从1开始 prob.Constraints.con2 = (q(2:end).* x(2:end-1))<= x(end); %下标从1开始
for i = 1:length(w) for i = 1:length(w)
prob.Objective = w(i) * x(end) - (1 - w(i)) * (r - p) * x(1:end-1); %注意大小写 prob.Objective = w(i) * x(end) - (1 - w(i)) * (r - p) * x(1:end-1); %注意大小写
[sol, fval, flag, out] = solve(prob); [sol, fval, flag, out] = solve(prob);
xx = sol.x; xx = sol.x;
V = [V, max(q.* xx(1:end-1))]; V = [V, max(q.* xx(1:end-1))];
Q = [Q, (r - p) * xx(1:end-1)]; Q = [Q, (r - p) * xx(1:end-1)];
X = [X, xx]; X = [X, xx];
plot(V, Q, '*-'); plot(V, Q, '*-');
grid on; grid on;
xlabel('风险'); xlabel('风险');
ylabel('收益'); ylabel('收益');
end end
%模型三:达到一定盈利水平,极小化风险 %模型三:达到一定盈利水平,极小化风险
clc; clear; close all; format long g; clc; clear; close all; format long g;
M=10000; M=10000;
k=1500:100:3000; k=1500:100:3000;
prob = optimproblem; prob = optimproblem;
x = optimvar('x', 5, 1, 'LowerBound', 0);%下界为0 x = optimvar('x', 5, 1, 'LowerBound', 0);%下界为0
r = [0.05, 0.28, 0.21, 0.23, 0.25]; r = [0.05, 0.28, 0.21, 0.23, 0.25];
p = [0, 0.01, 0.02, 0.045, 0.065]; p = [0, 0.01, 0.02, 0.045, 0.065];
q = [0, 0.025, 0.015, 0.055, 0.026]'; q = [0, 0.025, 0.015, 0.055, 0.026]';
V = []; V = [];
Q = []; Q = [];
X = []; X = [];
t = optimvar('t', 'LowerBound', 0); t = optimvar('t', 'LowerBound', 0);
%prob.Objective=max(q.*x);%极小化风险 %prob.Objective=max(q.*x);%极小化风险
for i=1:length(k) for i=1:length(k)
prob.Objective = t; % 极小化风险 prob.Objective = t; % 极小化风险
prob.Constraints.con1 = (1 + p) * x == M; prob.Constraints.con1 = (1 + p) * x == M;
prob.Constraints.con2=((r-p)*x>=k(i));%达到一定盈利水平 prob.Constraints.con2=((r-p)*x>=k(i));%达到一定盈利水平
prob.Constraints.con3=(q.*x<=t); prob.Constraints.con3=(q.*x<=t);
[sol,fval,flag,out]=solve(prob); [sol,fval,flag,out]=solve(prob);
if flag==1 if flag==1
xx=sol.x; xx=sol.x;
X=[X,xx]; X=[X,xx];
Q=[Q,(r-p)*xx]; Q=[Q,(r-p)*xx];
V=[V,fval]; V=[V,fval];
else else
xx=-1*ones(5,1); xx=-1*ones(5,1);
X=[X,xx]; X=[X,xx];
Q=[Q,-1]; Q=[Q,-1];
V=[V,-1]; V=[V,-1];
end end
end end
plot(k,V,'*-'); plot(k,V,'*-');

@ -1,94 +1,94 @@
%ej.4.10仓库选址 %ej.4.10仓库选址
clc; clear; close all; format long g; clc; clear; close all; format long g;
a=zeros(6); a=zeros(6);
a(1,[2,5])=[20,15]; a(1,[2,5])=[20,15];
a(2,[1,3,4,5])=[20,20,60,25]; a(2,[1,3,4,5])=[20,20,60,25];
a(3,[2,4,5])=[20,30,18]; a(3,[2,4,5])=[20,30,18];
a(4,[2,3])=[60,30]; a(4,[2,3])=[60,30];
a(5,[1,2,3,6])=[15,25,18,15]; a(5,[1,2,3,6])=[15,25,18,15];
a(6,5)=15; a(6,5)=15;
s = cellstr(strcat('销售点',int2str([1:6]'))); s = cellstr(strcat('销售点',int2str([1:6]')));
G=graph(a,s); G=graph(a,s);
P = plot(G,'layout','force','EdgeColor','k','NodeFontSize',12); P = plot(G,'layout','force','EdgeColor','k','NodeFontSize',12);
D=[]; D=[];
for i=1:6 for i=1:6
temp=0; temp=0;
for j=1:6 for j=1:6
[path,d]=shortestpath(G,i,j); [path,d]=shortestpath(G,i,j);
if d>temp; if d>temp;
temp=d; temp=d;
end end
end end
D=[D,temp]; D=[D,temp];
end end
[minVal,minInd]=min(D); [minVal,minInd]=min(D);
disp('仓库应建在销售点'); disp('仓库应建在销售点');
disp(minInd); disp(minInd);
%答案版 %答案版
clc; clear; close all; clc; clear; close all;
% 初始化邻接矩阵 % 初始化邻接矩阵
a = zeros(6); a = zeros(6);
a(1, [2, 5]) = [20, 15]; a(1, [2, 5]) = [20, 15];
a(2, 3:5) = [20, 60, 25]; a(2, 3:5) = [20, 60, 25];
a(3, [4, 5]) = [30, 18]; a(3, [4, 5]) = [30, 18];
a(5, 6) = 15; a(5, 6) = 15;
% 创建节点标签 % 创建节点标签
s = cellstr(strcat('V', int2str([1:6]'))); s = cellstr(strcat('V', int2str([1:6]')));
% 创建图 % 创建图
G = graph(a, s, 'upper'); G = graph(a, s, 'upper');
% 计算所有节点之间的最短路径距离矩阵 % 计算所有节点之间的最短路径距离矩阵
d = distances(G); d = distances(G);
% 计算从每个节点到其他节点的最大最短路径距离 % 计算从每个节点到其他节点的最大最短路径距离
D = max(d, [], 2)'; %第三个参数 2 指定沿着矩阵的第二个维度(即列)进行操作。 D = max(d, [], 2)'; %第三个参数 2 指定沿着矩阵的第二个维度(即列)进行操作。
% 找到最大最短路径距离中的最小值及其下标 % 找到最大最短路径距离中的最小值及其下标
[minD, minIndex] = min(D); [minD, minIndex] = min(D);
% 绘制图 % 绘制图
plot(G, 'EdgeLabel', G.Edges.Weight, 'Layout', 'force');%设置边的标签 plot(G, 'EdgeLabel', G.Edges.Weight, 'Layout', 'force');%设置边的标签
% 显示结果 % 显示结果
fprintf('从每个节点到任意其他节点的最大最短路径距离: \n'); fprintf('从每个节点到任意其他节点的最大最短路径距离: \n');
disp(D); disp(D);
fprintf('最大最短路径距离中的最小值为: %f\n', minD); fprintf('最大最短路径距离中的最小值为: %f\n', minD);
fprintf('对应的节点为: V%d\n', minIndex); fprintf('对应的节点为: V%d\n', minIndex);
%设备更新问题; %设备更新问题;
clc;clear;close all; clc;clear;close all;
a=zeros(6); a=zeros(6);
a(1,[2:6])=[15,20,27,37,54]; a(1,[2:6])=[15,20,27,37,54];
a(2,[3:6])=[15,20,27,37]; a(2,[3:6])=[15,20,27,37];
a(3,[4:6])=[16,21,28]; a(3,[4:6])=[16,21,28];
a(4,[5:6])=[16,21]; a(4,[5:6])=[16,21];
a(5,6)=17; a(5,6)=17;
s=cellstr(strcat(int2str((1:6)'),'年初')); s=cellstr(strcat(int2str((1:6)'),'年初'));
G=digraph(a,s); G=digraph(a,s);
P = plot(G,'layout','force','EdgeColor','k','NodeFontSize',12); P = plot(G,'layout','force','EdgeColor','k','NodeFontSize',12);
[path,d]=shortestpath(G,1,6);%注意参数个数 [path,d]=shortestpath(G,1,6);%注意参数个数
highlight(P,path,"EdgeColor","red",'LineWidth',3.5); highlight(P,path,"EdgeColor","red",'LineWidth',3.5);
%最小生成树 %最小生成树
clc;clear; clc;clear;
a=zeros(9); a=zeros(9);
a(1,[2:9])=[2 1 3 4 4 2 5 4]; % 顶点1到其他顶点的边的权重 a(1,[2:9])=[2 1 3 4 4 2 5 4]; % 顶点1到其他顶点的边的权重
a(2,[3 9])=[4 1]; % 顶点2到顶点3、顶点9的边的权重 a(2,[3 9])=[4 1]; % 顶点2到顶点3、顶点9的边的权重
a(3,4)=1; % 同上。因为写过1到3和2到3的边的权重无需重复设 a(3,4)=1; % 同上。因为写过1到3和2到3的边的权重无需重复设
a(4,5)=1; a(4,5)=1;
a(5,6)=5; a(5,6)=5;
a(6,7)=2; a(6,7)=2;
a(7,8)=3; a(7,8)=3;
a(8,9)=5; a(8,9)=5;
s=cellstr(strcat('节点',int2str([1:9]'))); s=cellstr(strcat('节点',int2str([1:9]')));
G=graph(a,s,'upper'); G=graph(a,s,'upper');
P=plot(G,'layout','force'); P=plot(G,'layout','force');
T=minspantree(G,'Method','sparse'); T=minspantree(G,'Method','sparse');
L=sum(T.Edges.Weight); L=sum(T.Edges.Weight);
highlight(P,T,"EdgeColor","red",'LineWidth',2.5); highlight(P,T,"EdgeColor","red",'LineWidth',2.5);

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

@ -0,0 +1,28 @@
import networkx as nx
import pylab as plt
import numpy as np
p=[25,26,28,31] ;a=[10,14,18,26] ; r=[20,16,13,11]
b= np.zeros((5,5))
for i in range(5):
for j in range(i+1,5):
b[i,j]=p[i]+np.sum(a[0:j-i])-r[j-i-1]
G=nx.DiGraph(b)
print(G)
p=nx.dijkstra_path(G,source=0,target=4,weight='weight')
print("最短路径为:",np.array(p)+1)#下标从零开始
d=nx.dijkstra_path_length(G,source=0,target=4,weight="weight")
print("所求的费用最小值为:",d)
s=dict(zip(range(5),range(1,6)))
plt.rc("font",size=16)
pos=nx.shell_layout((G))#设置布局
print(type(pos),'\npos=',pos)
w=nx.get_edge_attributes(G,"weight")
print(type(w),'\nw=',w)
nx.draw(G,pos,font_weight='bold',labels=s,node_color='r')#绘制点和边
nx.draw_networkx_edge_labels(G,pos,edge_labels=w)#绘制标签
#绘制最短路径
path_edges=list(zip(p,p[1:]))
print(type(path_edges),"\npath_edges=",path_edges)
nx.draw_networkx_edges(G,pos,edgelist=path_edges,edge_color="r",width=1)
plt.savefig("figure10_9.png");plt.show()
Loading…
Cancel
Save