How to solve the optimization problem where constraint is calculated thorough the loop?

조회 수: 10 (최근 30일)
Anup 2024년 3월 25일
댓글: Anup 2024년 3월 25일
I have a matlab code which contains loop and has a structure similar to following (ignore the syntax). I am trying to use matlab optimization toolbox to minimize the objective_funtion. The problem I am facing is defining the constraint. It seems that the matlab optimization toolbox doesn't allow constraint variable to be calculated through the loop. How do I solve problem like this?
% optimization variables
a % it is the array of data with hourly resolution
b
c
% Loop to calculate constraint variables
for i=1:length(a)
%%% here some calculation is done using a, b, and c
if (Certain Condition) satisfies
calculation(i) = funtion1(a, b, c, other variables)
else
calculation(i) = funtion2(a, b, c, other variables)
...
...
sum_calculation = sum(calculation);
end
objective_funtion = funtion3(a,b,c, other variables including sum_calculaiton)
cosntraint = sum_calculation>= 4000

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

답변 (1개)

Sudarsanan A K 2024년 3월 25일
Hi Anup,
When dealing with optimization problems in MATLAB, especially with constraints that depend on calculations within a loop, the key is to encapsulate your loop and any dependent calculations within a function that can be passed to the optimization solver as a constraint. MATLAB's optimization toolbox solvers, such as "fmincon", allow you to define both equality and inequality constraints through function handles that return the values of those constraints.
Here is a simple example where we aim to minimize a quadratic objective function subject to a constraint that involves a sum calculated in a loop:
• Set up and call the optimization solver.
% Initial guesses for a and b
initialGuess = [0, 0];
% No linear inequality or equality constraints
A = [];
b = [];
Aeq = [];
beq = [];
% No bounds
lb = [];
ub = [];
% Options
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
% Solve
[x, fval] = fmincon(@objectiveFunction, initialGuess, A, b, Aeq, beq, lb, ub, @constraintFunction, options);
Iter Func-count Fval Feasibility Step Length Norm of First-order step optimality 0 3 0.000000e+00 4.000e+02 1.000e+00 0.000e+00 1.490e-08 1 6 3.200000e+02 0.000e+00 1.000e+00 1.789e+01 1.600e+01 2 9 3.200000e+02 5.684e-14 1.000e+00 4.553e-07 5.638e-07 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
% Display the optimized variables and objective function value
disp(['Optimized a: ', num2str(x(1))]);
Optimized a: 8
disp(['Optimized b: ', num2str(x(2))]);
Optimized b: 16
disp(['Minimum objective function value: ', num2str(fval)]);
Minimum objective function value: 320
• Create the objective function "objectiveFunction" (e.g., ):
function obj = objectiveFunction(x)
% x is a vector where x(1) = a and x(2) = b
a = x(1);
b = x(2);
% Objective function
obj = a^2 + b^2;
end
• Create the constraint function "constraintFunction" (The constraint will be that the sum of over iterations is greater than or equal to ):
function [c, ceq] = constraintFunction(x)
a = x(1);
b = x(2);
% Initialize the sum calculation
sumCalculation = 0;
% Example loop that could represent more complex calculations
for i = 1:10
sumCalculation = sumCalculation + (a + 2*b);
end
% Inequality constraint (should be <= 0 to be satisfied)
c = 400 - sumCalculation; % This constraint expects sumCalculation >= 400 for satisfaction
% No equality constraints
ceq = [];
end
This example demonstrates how to set up and solve a constrained optimization problem in MATLAB, where the constraint involves calculations that are represented by a loop.
For understanding the use-cases of anonymous functions, refer to this resource:
I hope this helps!
댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Anup 2024년 3월 25일
Hi @Sudarsanan A K, Thank you for the response. I have edited the problem above. In my code here, there are lots of conditional statements just like the edited code above. Apparantly, I now think that the problem is not the loop but the conditional statements present inside of the loop. Is there a way to address this?

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

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by