How fmincon really work?
45 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (2)
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"
0 Kommentare
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);
% Display the results
fprintf('Optimal x: %.4f\n', x_opt);
fprintf('Objective function value at optimal x: %.4f\n', fval);
For more information regarding the "fmincon", kindly refer to the following MATLAB documentation:
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!