How to solve the optimization problem where constraint is calculated thorough the loop?
Mostrar comentarios más antiguos
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
Respuestas (1)
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);
% Display the optimized variables and objective function value
disp(['Optimized a: ', num2str(x(1))]);
disp(['Optimized b: ', num2str(x(2))]);
disp(['Minimum objective function value: ', num2str(fval)]);
- 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 more information about the "fmilncon" function, refer to this documentation link:
For understanding the use-cases of anonymous functions, refer to this resource:
I hope this helps!
1 comentario
Anup
el 25 de Mzo. de 2024
Categorías
Más información sobre Surrogate Optimization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!