Runtime creation of constraints and optimization variable

I am coding an optimization problem, where my optimization variable is denoted by Q which is a TxN matrix, and ith row of Q is of the form g*P_i, here g is a 1xN vector which is already known, and P_i is the ith NxN diagonal matrix and ith constraint is norm(P_i,'fro')<=N. How to code Q and and its constraints in MATLAB, when T and N are given. I would like to find the optimum value of my objective function for different values of T. I am using optimvar and optimproblem functions.

 Accepted Answer

Hi,
To my understanding you are seeking help to code this optimization problem using MATLAB’s “optimvar” and “optimproblem” functions.
Here are some steps that you can follow:
  • Kindly define the optimization variable ‘Q’
T = 10; % Example value for T
N = 5; % Example value for N
g = rand(1, N); % Replace with your known vector g
% Create an optimization variable Q of size T x N
Q = optimvar('Q', T, N);
  • Second step is to set up the constraints, since each row of “Q” is “g*P_i” and “P_i” is an “NxN” diagonal matrix, you can express “P_i” as a vector (since it's diagonal) and then multiply by g to get the row of “Q”. The Frobenius norm constraint on “P_i” is equivalent to constraining the 2-norm of the vector since “P_i” is diagonal.
% Create an optimization problem
prob = optimproblem;
% Add constraints to the problem
for i = 1:T
P_i = optimvar(['P_i' num2str(i)], N, 'LowerBound', 0, 'UpperBound', N);
% Define the ith constraint for P_i
prob.Constraints.(['P_i' num2str(i) '_constraint']) = sum(P_i.^2) <= N;
% Define the ith row of Q to be g*diag(P_i)
prob.Constraints.(['Q_row' num2str(i)]) = Q(i,:) == g .* P_i';
end
  • Then we need to define the objective function, Since you have not specified the objective function, I am considering a placeholder function(“myObjectiveFunction”).
% Define an example objective function (replace with your own)
prob.Objective = sum(Q, 'all');
% Alternatively, if the objective function is more complex, define it like this:
% prob.Objective = myObjectiveFunction(Q);
  • So we have our problem set up now, we will need to loop over different values of “T” to solve for each scenario.
% Loop over different values of T and solve the optimization problem
T_values = [5, 10, 15]; % Example values for T
optimal_Q = cell(length(T_values), 1); % Preallocate cell array for solutions
for idx = 1:length(T_values)
T = T_values(idx);
% Update the size of Q and the constraints accordingly
% (You'll need to redefine Q and the constraints for each T)
% Solve the optimization problem
[sol, fval, exitflag, output] = solve(prob);
% Store the optimal Q matrix
optimal_Q{idx} = sol.Q;
end
Kindly replace “myObjectiveFunction” with your actual objective function and adjust the constraints and variable definitions as needed for your specific problem. The code example assumes that the “g” vector and the “N” value are constant across different “T” values. If they change, you'll need to update them in the loop as well.
Here are some resources you can go through for more examples and references:
  1. https://in.mathworks.com/help/optim/ug/optimvar.html%22
  2. https://in.mathworks.com/help/optim/ug/optimproblem.html%22
  3. https://in.mathworks.com/help/optim/examples.html?category=problem-based-basics&s_tid=CRUX_topnav%22
Hope it helps!
Regards,
Soumnath

4 Comments

Hi Soumnath,
Thanks for helping.
can you help in setting initial condition for each P_i in a loop?
Yes, Sure
To set initial conditions for each P_i within a loop, you'll need to create an initial guess for the optimization problem in MATLAB. Kindly do this by creating a structure that holds initial values for each P_i and then pass this structure to the solver when you call the solve function.
% Loop over different values of T and solve the optimization problem
T_values = [5, 10, 15]; % Example values for T
optimal_Q = cell(length(T_values), 1); % Preallocate cell array for solutions
for idx = 1:length(T_values)
T = T_values(idx);
% Define the optimization variable Q of size T x N
Q = optimvar('Q', T, N);
% Create an optimization problem
prob = optimproblem;
% Define initial guess structure
x0 = struct;
% Add constraints and initial guess for each P_i to the problem
for i = 1:T
P_i_name = ['P_i' num2str(i)];
P_i = optimvar(P_i_name, N, 'LowerBound', 0, 'UpperBound', N);
% Add the Frobenius norm constraint for P_i
prob.Constraints.([P_i_name '_constraint']) = sum(P_i.^2) <= N;
% Set the initial condition for P_i
x0.(P_i_name) = ones(N, 1) * sqrt(N/N); % Example initial condition
% Define the ith row of Q to be g*diag(P_i)
prob.Constraints.([P_i_name '_Q_row']) = Q(i,:) == g .* P_i';
end
% Define the objective function (replace with your own)
prob.Objective = sum(Q, 'all');
% Solve the optimization problem with the initial guess
[sol, fval, exitflag, output] = solve(prob, x0);
% Store the optimal Q matrix
optimal_Q{idx} = sol.Q;
end
  • We loop over the array 'T_values' to solve the optimization problem for different values of 'T'.
  • We define 'Q' and 'P_i' variables for each 'T' within the loop.
  • We create a structure 'x0' to hold the initial conditions for each 'P_i'.
  • We set an example initial condition for each 'P_i' as a vector of ones multiplied by sqrt(N/N). This is a simple starting point, and you might want to choose a more appropriate initial condition based on your specific problem.
  • We pass the initial guess 'x0' to the 'solve' function to provide a starting point for the optimization.
Please replace the placeholder initial conditions with values that make sense for your problem. The initial conditions can have a significant impact on the optimization, especially in non-convex problems where there might be multiple local optima.
Thank you very much Soumnath,
It worked. I had to provide an intial guess for Q as well since I was getting an error to provide intial guess for Q, and now I get the below error:
Nonlinear constraint function is undefined at initial point.
Fmincon cannot continue.
can you help with this? Below is my code
load("R.mat")
load("g.mat")
N=64;
P_t=100;
PL=(norm(g)^2)/N;
iter=9e5;
T=12;
sigma2=10^(-4);
prob=optimproblem("Description","MSE min");
x0=struct;
Q = optimvar('Q', T, N);
for i = 1:T
phi_i_name=['phi_i' num2str(i)];
phi_i = optimvar(phi_i_name, [1,N], 'LowerBound', 0, 'UpperBound', N);
prob.Constraints.([phi_i_name '_constraint']) = sum(phi_i.^2) <= N;
gphi_i=@(g,phi_i) g .* phi_i;
x0.(phi_i_name) = ones(N, 1);
prob.Constraints.(['Q_row' num2str(i)]) = Q(i,:) == fcn2optimexpr(gphi_i,g,phi_i) ;
end
P=optimvar("P",[T,1],"Type",'continuous','LowerBound',0);
f=@(P,Q) trace(R - R*Q'*diag(P)*inv(diag(P)*Q*R*Q'*diag(P) + sigma2*eye(T))*diag(P)*Q*R);
prob.Objective = fcn2optimexpr(f,P,Q,'OutputSize',[1,1]);
prob.Constraints.power= norm(diag(P),'fro')^2 <= P_t;
x0.P = sqrt(P_t / T) * ones(T, 1);
QIG = randn(T, N);
trace_Q_Q_transpose = trace(QIG * QIG');
while trace_Q_Q_transpose > P_t*T*N
scaling_factor = sqrt(P_t / trace_Q_Q_transpose);
QIG = scaling_factor * QIG;
trace_Q_Q_transpose = trace(QIG * QIG');
end
x0.Q=QIG;
opts=optimoptions('fmincon','MaxFunctionEvaluations',iter);
[sol,optval]=solve(prob,x0,'Options',opts);
Since you mentioned that you had to provide an initial guess for 'Q' as well, kindly make sure that the initial guess for Q is consistent with the constraints you have defined. The initial guess 'x0.Q' should satisfy the power constraint 'prob.Constraints.power'.
The error can be because of the initial guess, which may be outside the feasible region defined by the constraints, or may be there is a mismatch in the dimensions or types of variables used in the function.

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!