数学建模:Yalmip求解线性与非线性优化问题

🔆 文章首发于我的个人博客:欢迎大佬们来逛逛

线性优化

使用 Yalmip 求解线性规划最优值:

m i n { − x 1 − 2 x 2 + 3 x 3 } x 1 + x 2 ⩾ 3 x 2 + x 3 ⩾ 3 x 1 + x 3 = 4 0 ≤ x 1 , x 2 , x 3 ≤ 2 \begin{gathered}min\{-x_1-2x_2+3x_3\} \\x_1+x_2\geqslant3 \\x_2+x_3\geqslant3 \\x_1+x_3=4 \\0\leq x_1,x_2,x_3\leq2 \end{gathered} min{−x1−2x2+3x3}x1+x2⩾3x2+x3⩾3x1+x3=40≤x1,x2,x3≤2

  • 使用 sdpvar 来构建实数解 ,如果要求解整数类型,则使用 intvar;如果是01类型,使用binvar
  • 构建目标函数
  • 构建约定与LB,UB上下限,其中 ≥ ≤ == 等约束直接照抄即可
  • optimize求解最优值
  • 打印结果...
matlab 复制代码
clc;clear;
%
p = sdpvar(3,1);
%% 构建目标函数
Objective = -p(1)-2*p(2)+3*p(3);
%% 构建约束
Constraints = [0<=p<=2,(p(1)+p(2)>=3),(p(2)+p(3)>=3),(p(1)+p(3)==4)];
%% 求解最优值
optimize(Constraints,Objective);
%%
% 三个变量的解
P=double(p);
% 目标优化值
Obj=double(Objective);
objstr=['目标函数最优值:',num2str(Obj)];
disp(objstr)
for i=1:length(P)
    xstr=['x',num2str(i),'的值为:',num2str(P(i))];
    disp(xstr)
end

整数规划

在 optimize 函数中添加如下参数来实现 01整数规划

  • options = sdpsettings('solver', 'bnb', 'bnb.solver', 'fmincon');
  • options = sdpsettings('solver','bmibnb', 'bmibnb.uppersolver', 'fmincon'); 全局最优,但是速度慢

案例(1)

matlab 复制代码
clc;clear;

p = binvar(6,4); % 01整数规划
%%
c = [4,2,3,4;
    6,4,5,5;
    7,6,7,6;
    7,8,8,6;
    7,9,8,6;
    7,10,8,6;
];
%%
f = -sum(sum(p.*c));
%% 构建约束:注意二维矩阵的构建方法
cons = [];
for j = 1:size(c,2)
    cons = [cons,(sum(p(:,j))>=1)];
end
%%
for i = 1:size(c,1)
    cons = [cons,(sum(p(i,:))==1)];
end
%% 整数规划
% options = sdpsettings('solver', 'bnb', 'bnb.solver', 'fmincon');
options = sdpsettings('solver','bmibnb', 'bmibnb.uppersolver', 'fmincon');
%%
optimize(cons,f,options);

%%
% 三个变量的解
P=double(p);
% 目标优化值
Obj=double(-f);
objstr=['目标函数最优值:',num2str(Obj)];
disp(objstr)
disp(P);

案例(2)

matlab 复制代码
clc;clear;

p = intvar(7,1);
%%
f = sum(p);
%%
% 必须写上限制范围
cons = [0<=p,(p(1)+2*p(2)+p(7)>=100),(3*p(3)+2*p(4)+p(5)+p(7)>=100),(4*p(1)+p(2)+2*p(4)+4*p(5)+6*p(6)+p(7)>=100)];
%% 整数规划
% options = sdpsettings('solver', 'bnb', 'bnb.solver', 'fmincon');
% options = sdpsettings('solver','bmibnb', 'bmibnb.uppersolver', 'fmincon');
%%
optimize(cons,f);

%%
% 三个变量的解
P=double(p);
% 目标优化值
Obj=double(f);
objstr=['目标函数最优值:',num2str(Obj)];
disp(objstr)
for i=1:length(P)
    xstr=['x',num2str(i),'的值为:',num2str(P(i))];
    disp(xstr)
end

案例(3)


matlab 复制代码
clc;clear;

p = binvar(5,8);
%%
c = [ 30 25 18 32 27 19 22 26;
    29 31 19 18 21 20 30 19;
    28 29 30 19 19 22 23 26;
    29 30 19 24 25 19 18 21;
    21 20 18 17 16 14 16 18];

%% 
f = sum(sum(c.*p));

%%
constraints = [];
for j = 1:size(c,2)
    constraints = [constraints,(sum(p(:,j))<=1)];
end

for i = 1:size(c,1)
    constraints = [constraints,(sum(p(i,:))==1)];
end
%%
options = sdpsettings('solver','bmibnb', 'bmibnb.uppersolver', 'fmincon');

%% 
optimize(constraints,f,options);

%%
% 三个变量的解
P=double(p);
% 目标优化值
Obj=double(f);
objstr=['目标函数最优值:',num2str(Obj)];
disp(objstr)
disp(P);

非线性规划

max ⁡ f ( x ) = 2 x 1 + 3 x 1 2 + 3 x 2 + x 2 2 + x 3 , s. t. { x 1 + 2 x 1 2 + x 2 + 2 x 2 2 + x 3 ⩽ 10 , x 1 + x 1 2 + x 2 + x 2 2 − x 3 ⩽ 50 , 2 x 1 + x 1 2 + 2 x 2 + x 3 ⩽ 40 , x 1 2 + x 3 = 2 , x 1 + 2 x 2 ⩾ 1 , x 1 ⩾ 0 , x 2 , x 3 不约束 . \max f(x)=2x_1+3x_1^2+3x_2+x_2^2+x_3,\\\text{s. t.}\quad\begin{cases}x_1+2x_1^2+x_2+2x_2^2+x_3\leqslant10,\\x_1+x_1^2+x_2+x_2^2-x_3\leqslant50,\\2x_1+x_1^2+2x_2+x_3\leqslant40,\\x_1^2+x_3=2,\\x_1+2x_2\geqslant1,\\x_1\geqslant0,x_2,x_3\text{ 不约束}.\end{cases} maxf(x)=2x1+3x12+3x2+x22+x3,s. t.⎩ ⎨ ⎧x1+2x12+x2+2x22+x3⩽10,x1+x12+x2+x22−x3⩽50,2x1+x12+2x2+x3⩽40,x12+x3=2,x1+2x2⩾1,x1⩾0,x2,x3 不约束.

matlab 复制代码
clc;clear;

p = sdpvar(3,1);
%% 
f = -2*p(1)-3*p(1)^2-3*p(2)-p(2)^2-p(3);

%%
Cons = [(p(1)>=0),(p(1)+2*p(1)^2+p(2)+2*p(2)^2+p(3)<=10),(p(1)+p(1)^2+p(2)+p(2)^2-p(3)<=50),(2*p(1)+p(1)^2+2*p(2)+p(3)<=40),(p(1)^2+p(3)==2),(p(1)+2*p(2)>=1)];
%%
% options = sdpsettings('solver','bmibnb', 'bmibnb.uppersolver', 'fmincon');
%% 
optimize(Cons,f);
%%
% 三个变量的解
P=double(p);
% 目标优化值
Obj=double(-f);
objstr=['目标函数最优值:',num2str(Obj)];
disp(objstr)
disp(P);
相关推荐
统计学小王子10 小时前
数学建模国赛倒计时8天——《搭建建模Word框架(内容篇)》
数学建模
统计学小王子10 小时前
数学建模国赛倒计时8天——《搭建建模Word框架(字体字号)》
数学建模·word
不正经的码狗2 天前
数学建模入门
数学建模
建模小刘3 天前
2026数学建模国赛准备----AI提示词!!!!!
人工智能·数学建模·2026数学建模国赛
kyle~5 天前
机器视觉---高动态范围成像HDR
数学建模·机器视觉
ECT-OS-JiuHuaShan6 天前
共轭互逆链路论,彻底打击庸俗辩证法和不可知论
数据库·人工智能·算法·机器学习·数学建模
疯狂小猫咪7 天前
排课算法初探:教培场景下的资源约束问题如何解
数学建模
kyle~8 天前
滤波---指数移动平均法(Exponential Moving Average, EMA)
数学建模·信号处理
民乐团扒谱机9 天前
【微科普】压缩感知(CS):违反了奈奎斯特采样定理?不先采集也能还原信号?大白话讲透稀疏采样、L1重建与OMP,一文吃透附代码
python·神经网络·线性代数·算法·数学建模·压缩感知·奈奎斯特
88号技师9 天前
2026年SCI-分数牛顿衍生优化算法Fractional Newton-derived optimizer-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法