Merge pull request '三人三个文件夹' (#2) from 盘荣博 into main
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,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,'*-');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
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…
Reference in new issue