Dear Johan,
What I want to do is to optimize one problem, of which the constraints contain another optimization problem.
The code is as below:
========================================================
%% Two-layer optimization
clear;clc;
r = sdpvar(2,1);
x = sdpvar([2,2],[1,1]);
u = sdpvar(1,1);
% Optimization problem 1
Objective_1 = (x{2}-r)'*(x{2}-r);
Constraints_1 = [x{2} == 2*x{1}+[2;3]*u];
Opt_1 = optimizer(Constraints_1,Objective_1,[],{x{1},r},u);
optu_1 = Opt_1{{[0;0],[2;3]}}; % problem 1 can be solved very well
% Optimization problem 2
Objective_2 = (x{2}-r)'*(x{2}-r);
Constraints_2 = [x{2} == 2*x{1}+[2;3]*(u+Opt_1{{x{2},r}})]; % contraints of problem 2 contains optimizer 1
Opt_2 = optimizer(Constraints_2,Objective_2,[],{x{1},r},u);
optu_2 = Opt_2{{[0;0],[2;3]}};
========================================================
The error message is like this:
Adding NaN to an SDPVAR makes no sense.
Constraints_2 = [x{2} == 2*x{1}+[2;3]*(u+Opt_1{{x{2},r}})]; % contraints of problem 2 contains optimizer 1
I believe there is something wrong with the overloading of '+' between 'u' and 'Opt_1{{x{2},r}}'. Do you have any clue that I can make this two-layer optimization possible?
If I make x defined as a matrix rather than a cell array, it still doesn't help:
========================================================
%% Two-layer optimization 2
clear;clc;
r = sdpvar(2,1);
x = sdpvar(2,2,'full');
u = sdpvar(1,1);
% Optimization problem 1
Objective_1 = (x(:,2)-r)'*(x(:,2)-r);
Constraints_1 = [x(:,2) == 2*x(:,1)+[2;3]*u];
Opt_1 = optimizer(Constraints_1,Objective_1,[],{x(:,1),r},u);
optu_1 = Opt_1{{[0;0],[2;3]}}; % problem 1 can be solved very well
% Optimization problem 2
Objective_2 = (x(:,2)-r)'*(x(:,2)-r);
Constraints_2 = [x(:,2) == 2*x(:,1)+[2;3]*(u+Opt_1{{x(:,2),r}})]; % contraints of problem 2 contains optimizer 1
Opt_2 = optimizer(Constraints_2,Objective_2,[],{x(:,1),r},u);
optu_2 = Opt_2{{[0;0],[2;3]}};
========================================================
But if I make the two-layer optimization problem simpler, it would work:
========================================================
%% Two-layer optimization 3
clear;clc;
r = sdpvar(2,1);
x = sdpvar(2,1);
% Optimization problem 1
Objective_1 = (x-r)'*(x-r);
Constraints_1 = [x(2) == 2*x(1)];
Opt_1 = optimizer(Constraints_1,Objective_1,[],r,x(1));
optu_1 = Opt_1{[2;3]};
% Optimization problem 2
Objective_2 = (x-r)'*(x-r);
Constraints_2 = [x(2) == 2*x(1)+Opt_1{r}]; % contraints of problem 2 contains optimizer 1
Opt_2 = optimizer(Constraints_2,Objective_2,[],r,x);
optu_2 = Opt_2{[3;4]};
% Problem 2 can be solved
========================================================
Looking forward to your answer. I will appreciate it very much!
Renjie