fmincon get the wrong answer
1 view (last 30 days)
Show older comments
i have a optimization problem that fmincon get the wrong answer, there are a demand power(2) that can be generate or buy with different price, the codes are as follow:
a = 0.005;
b = 6;
c = 100;
% Define the cost function for generating energy (quadratic cost function) for the current microgrid
Cg_i = @(Ec, a, b, c) a * Ec^2 + b * Ec + c;
% Given prices
prices = [33, 26];
mprice = 27; % Given value of mprice
% Define the cost function for buying energy
Cb_i = @(Ec, price) price * Ec;
% Given total energy constraint
Ec_total = 2; % Given value of Ec_total
% Define the objective function
objective = @(X) Cg_i(X(1), a, b, c) + Cb_i(X(2), prices(1)) + Cb_i(X(3), prices(2)) + Cb_i(X(4), mprice);
% Initial guess for Ec_g, Ec1, Ec2
initial_guess = [Ec_total / 3, Ec_total / 3, Ec_total / 3, Ec_total / 3];
% initial_guess = [0, 0, Ec_total, 0];
% Linear equality constraint for the sum of Ec_g, Ec1, Ec2
Aeq = [1, 1, 1, 1];
beq = Ec_total;
% Options for fmincon
options = optimoptions('fmincon', 'Display', 'iter');
% Define solver options
% Call fmincon with specified options
X = fmincon(objective, initial_guess, [], [], Aeq, beq, [0, 0, 0,0], [], [], options)
the matlab answer is X=[2 0 0 0]; that means generat 2 and no buy, that obviously is wrong because cost of buy with price 26 is 26*2=52 while generating cost is 112!
0 Comments
Accepted Answer
Torsten
on 12 May 2024
Edited: Torsten
on 12 May 2024
Cost of buy with price 26 is 2*26 + c = 152 in your setting. Note that although you don't generate energy, Cg_i(0,a,b,c) = c - thus you always add c to the cost of buying energy which obviously is not correct. Or do you want to treat c as fixed costs that always have to be paid - independent of whether energy is generated or not ?
3 Comments
Torsten
on 13 May 2024
Edited: Torsten
on 14 May 2024
One way is to use "ga": it can handle discontinuous objective functions:
rng("default")
a = 0.005;
b = 6;
c = 100;
% Define the cost function for generating energy (quadratic cost function) for the current microgrid
Cg_i = @(Ec, a, b, c) (a * Ec^2 + b * Ec + c)*(Ec>0);
% Given prices
prices = [33, 26];
mprice = 27; % Given value of mprice
% Define the cost function for buying energy
Cb_i = @(Ec, price) price * Ec;
% Given total energy constraint
Ec_total = 2; % Given value of Ec_total
% Define the objective function
objective = @(X) Cg_i(X(1), a, b, c) + Cb_i(X(2), prices(1)) + Cb_i(X(3), prices(2)) + Cb_i(X(4), mprice);
% Initial guess for Ec_g, Ec1, Ec2
initial_guess = [Ec_total / 3, Ec_total / 3, Ec_total / 3, Ec_total / 3];
% initial_guess = [0, 0, Ec_total, 0];
% Linear equality constraint for the sum of Ec_g, Ec1, Ec2
Aeq = [1, 1, 1, 1];
beq = Ec_total;
X = ga(objective,4,[],[],Aeq,beq,zeros(4,1),inf(4,1))
objective(X)
Torsten
on 14 May 2024
Edited: Torsten
on 14 May 2024
Maybe somebody sees the problem - I don't know why "fmincon" has to shift the initial guess to [1 1 2 1] . In my opinion, [0 0 2 0] as given should satisfy lower and upper bounds as well as the linear equality constraint.
% Given total energy constraint
Ec_total = 2; % Given value of Ec_total
% Initial guess for Ec_g, Ec1, Ec2
initial_guess = [0, 0, Ec_total, 0];
% Linear equality constraint for the sum of Ec_g, Ec1, Ec2
Aeq = [1, 1, 1, 1];
beq = Ec_total;
% Options for fmincon
options = optimoptions('fmincon', 'Display', 'iter');
% Define solver options
% Call fmincon with specified options
[X,fval,flag] = fmincon(@objective, initial_guess, [], [], Aeq, beq, zeros(1,4), inf(1,4), [], options)
objective(X)
function value = objective(X)
X
a = 0.005;
b = 6;
c = 100;
% Define the cost function for generating energy (quadratic cost function) for the current microgrid
Cg_i = @(Ec, a, b, c) a * Ec^2 + b * Ec + c;
% Given prices
prices = [33, 26];
mprice = 27; % Given value of mprice
% Define the cost function for buying energy
Cb_i = @(Ec, price) price * Ec;
% Define the objective function
value = Cg_i(X(1), a, b, c) + Cb_i(X(2), prices(1)) + Cb_i(X(3), prices(2)) + Cb_i(X(4), mprice);
end
More Answers (0)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!