How fmincon really work?

45 Ansichten (letzte 30 Tage)
imene. B
imene. B am 25 Nov. 2024 um 9:23
Beantwortet: Hitesh am 25 Nov. 2024 um 10:33
Hello everyone,
So fmicnon in matlab minimize the objective function, technically if we have an objective function that returns a negative values (let's say: objective = cost_negative), then the fmincon will try to minimize it by sending it further to the - infinity right? (for example objective = -1, then fmicnon will try to send it to a number lower than -1 which means -10 or -100 as example)?
if the first statement is correct, then my second question is: let's say we have 2 types of costs (objective = cost_negative + cost_positive), if we give - objective to fmincon this would technically minimize the negative costs to zero and maximize the positive costs?
(I'm using sqp algorithm in order to respect my constraints)
Thank you in advance.

Antworten (2)

Walter Roberson
Walter Roberson am 25 Nov. 2024 um 10:11
fmincon() always tries to minimize the result of the objective function. If the objective function returns negative values, then fmincon() tries to see if it can be driven even more negative.
objective = cost_negative + cost_positive
It will try to minimize the overall value. It doesn't care how the value is composed. It doesn't know (or care) whether an objective of 10 is composed of 1 negative + 9 positive, or 5 negative and 5 positive, or 9 negative and 1 positive. So no it will not maximize the positive costs.
If you want to minimize the negative costs and maximize the positive costs then use an objective of cost_negative - cost_positive -- assuming that cost_negative is "magnitude by which it would be negative"

Hitesh
Hitesh am 25 Nov. 2024 um 10:33
"fmincon" is designed to minimize the objective function. If your objective function returns negative values (for example, objective = -1), "fmincon" will indeed try to make it more negative, potentially driving it toward negative infinity, unless constrained by other factors in the problem such as bounds.
If you have an objective function that is a combination of a negative cost and a positive cost, like objective = cost_negative + cost_positive, and you provide -objective to fmincon, the optimizer will try to maximize the original objective function because minimizing -objective is equivalent to maximizing objective.
  • Negative Costs: The optimizer will attempt to increase its components since they contribute negatively to -objective.
  • Positive Costs: The optimizer will attempt to increase its components as well since they contribute positively to -objective.
Using the Sequential Quadratic Programming (SQP) algorithm is generally a good choice for problems with constraints, kindly ensure to verify that it is suitable for your specific problem.
Refer to the following code as an example:
% Define the objective function
objective = @(x) -(-x.^2 + 2*x); % This is -objective, equivalent to x^2 - 2x
% Initial guess
x0 = 0;
% No constraints in this simple example
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
% Options for fmincon
options = optimoptions('fmincon', 'Algorithm', 'sqp', 'Display', 'iter');
% Run the optimization
[x_opt, fval] = fmincon(objective, x0, A, b, Aeq, beq, lb, ub, [], options);
Iter Func-count Fval Feasibility Step Length Norm of First-order step optimality 0 2 -0.000000e+00 0.000e+00 1.000e+00 0.000e+00 2.000e+00 1 5 -8.400000e-01 0.000e+00 7.000e-01 1.400e+00 8.000e-01 2 7 -1.000000e+00 0.000e+00 1.000e+00 4.000e-01 7.451e-09 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 results
fprintf('Optimal x: %.4f\n', x_opt);
Optimal x: 1.0000
fprintf('Objective function value at optimal x: %.4f\n', fval);
Objective function value at optimal x: -1.0000
For more information regarding the "fmincon", kindly refer to the following MATLAB documentation:

Community Treasure Hunt

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

Start Hunting!

Translated by