Introduction to double-layer optimization (2) - double-layer optimization solution based on yalmip (with matlab code)

        The previous blog introduced the basic principles of double-layer optimization and the method of using KKT conditions to solve double-layer optimization:

Introduction to double-layer optimization (1)—Basic principles and solution methods

        This blog will introduce how to solve the two-layer optimization problem using yalmip.

1.KKT function

        By calling the KKT function in the yalmip toolbox, you can directly find the KKT conditions of the optimization problem, eliminating the need to manually write the steps yourself. The function usage is as follows:

[KKTsystem, details] = kkt(Constraint,Objective,z)

        Among them, z represents the optimization variable, KKTsystem stores the constraint expression of KKT conditions, and details is a structure variable used to store the details of KKT conditions. Take the lower-level optimization problem of the two-level optimization problem in the previous blog as an example:

 matlab code:

%% 目标函数和约束条件
x=sdpvar(1);
y=sdpvar(1);
Constraints=[-3*x+y <= -3 , 3*x+y <= 30];
objective=-y;
[KKTsystem, details] = kkt(Constraints,objective,x);

operation result:

        By adding the KKT conditions of the lower-level optimization as constraints to the upper-level optimization, the result of this two-level optimization can be obtained:

%% 清空
clc
clear
close all
warning off
%% 目标函数和约束条件
x=sdpvar(1);
y=sdpvar(1);
Constraints_down=[-3*x+y <= -3 , 3*x+y <= 30];
objective_down=-y;
[KKTsystem , details] = kkt(Constraints_down,objective_down,x);
Constraints_up=[2*x-3*y >= -12 , x+y <= 14];
objective_up=-x-2*y;
ops=sdpsettings('verbose', 0 , 'solver', 'gurobi');
result=optimize([KKTsystem,Constraints_up,boundingbox([Constraints_up,Constraints_down])],objective_up,ops);
%% 输出模型
saveampl(KKTsystem,objective_down,'KKT_model');
%% 输出结果
disp(['最优解:x=',num2str(value(x)),',y=',num2str(value(y))])
disp(['最优函数值=',num2str(value(objective_up))])

        The solution results are as follows:

        The solution results of manually writing KKT in the previous blog are consistent.

        The above example is a simple linear two-layer optimization problem. The official yalmip document gives an example of using the KKT function to solve nonlinear two-layer optimization. This two-level optimization problem is as follows:

It can also be solved using the KKT function. The code is as follows (this is the code provided by the official website):

sdpvar x1 x2 y1 y2 y3

OO = -8*x1-4*x2+4*y1-40*y2-4*y3;
OO = OO+OO^2;
CO = [x1>=0, x2>=0];

OI = (x1+2*x2+y1+y2+2*y3)^2;
CI = [[y1 y2 y3] >= 0,
       -y1+y2+y3 <= 10,
      2*x1-y1+2*y2-0.5*y3 <= 10,
      2*x2+2*y1-y2-0.5*y3 <= 9.7];

[K,details] = kkt(CI,OI,[x1 x2])
optimize([K,CO,boundingbox([CI,CO]),details.dual<=100],OO)

The solution result is:

The optimal objective function is -0.25

x1=0.0625,x2=0,y1=0,y2=0,y3=0。

2.solvebilevel function

        solvebilevel is a function built into the yalmip toolbox to solve bilevel optimization problems. That is to say, through this function, we do not need to manually write KKT conditions, nor do we need to use the KKT function. We can directly put the objective function and constraint conditions of the upper and lower layer optimization into it to get the result.

        code show as below:

%% 清空
clc
clear
close all
warning off
%% 目标函数和约束条件
x=sdpvar(1);
y=sdpvar(1);
Constraints_down=[-3*x+y <= -3 , 3*x+y <= 30];
objective_down=-y;
Constraints_up=[2*x-3*y >= -12 , x+y <= 14];
objective_up=-x-2*y;
solvebilevel(Constraints_up,objective_up,Constraints_down,objective_down,y)
%% 输出结果
disp(['最优解:x=',num2str(value(x)),',y=',num2str(value(y))])
disp(['最优函数值=',num2str(value(objective_up))])

The solution results are as follows:

         Compared with the previous blog about manually writing KKT conditions, the results of this blog using the KKT function are the same, which is indeed more trouble-free. But it should also be noted that this function is only suitable for smaller problems. If the problem is larger, you still need to solve the double-layer problem manually.

The complete code is available here:

Introduction to double-layer optimization (2) - double-layer optimization solution based on yalmip

The reference materials are all from yalmip official documents:

[1] Introduction to the usage of KKT function

[2]Solution of double-layer optimization

[3] Alternate method for double-layer optimization solution

[4] Introduction to the usage of solvebilevel function

Guess you like

Origin blog.csdn.net/weixin_44209907/article/details/130480309