Runtime creation of constraints and optimization variable

조회 수: 1 (최근 30일)
Sai Srikar
Sai Srikar 2023년 11월 17일
댓글: SOUMNATH PAUL 2023년 11월 30일
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.

채택된 답변

SOUMNATH PAUL
SOUMNATH PAUL 2023년 11월 30일
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
Sai Srikar
Sai Srikar 2023년 11월 30일
편집: Sai Srikar 2023년 11월 30일
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);
SOUMNATH PAUL
SOUMNATH PAUL 2023년 11월 30일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by