Energy storage optimisation problem - separate charge and discharge constraint

12 views (last 30 days)
hussain askar
hussain askar on 23 Nov 2021
Commented: Alan Weiss on 26 Nov 2021
Hi
I'm currently working on an optimization problem to minimize the cost of energy dispatch where we have solar generation and energy storage system. My fmincon function is working, however after i added the constraints for the battery charging and discharging I'm getting an error :
Unable to perform assignment because value of type 'optim.problemdef.OptimizationExpression' is not convertible to 'double'.
Caused by:
Error using double
Conversion to double from optim.problemdef.OptimizationExpression is not possible.
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
I'm not sure what is causing the issue, any help would be great in fixing this.
options = optimoptions('fmincon',...
'Algorithm','sqp','Display', 'iter', 'ConstraintTolerance', 1e-10);
[p, C] = fmincon (@myfun, p0, [], [], [], [], lb, ub, @mycon, options);
% Cost Function that needs to be minimized..
function C = myfun (p)
C =0;
for t = 1:24
for i = 1:4
cost = ce(i) + b(i)*p(t,i) + a(i)*p(t,i)^2;
C = C + cost;
end
end
end
function [c, ceq] = mycon(p) % function [ceq] = mycon(p) in Octave
global Pd P_solar
N= 24;
dt = 1;
BattEnergy = 650;
Einit = 0.5 * BattEnergy;
Emax = 0.8*BattEnergy;
Emin = 0.2*BattEnergy;
Pch = optimvar('Pch',N,'LowerBound',0,'UpperBound',Emax);
Pdisch = optimvar('Pdisch',N,'LowerBound',0,'UpperBound',Emin);
c =0;
c1 =zeros(24,1);
c2 =zeros(24,1);
c3 =zeros(24,1);
c4 =zeros(24,1);
c5 =zeros(24,1);
c6 =zeros(24,1);
for t = 1:24
c1(t) = ( 0 - Pdisch(t));
c2(t) = ( Pdisch(t) - Pdischmax(t));
c3(t) = ( 0 - Pch(t));
c4(t) = ( Pch(t) - Pchmax(t));
c5(t) = Emin - (sum(Pch(t) - Pdisch(t)) * dt) ;
c6(t) = (sum(Pch(t) - Pdisch(t)) * dt) - Emax;
end
c = [c1;c2;c3;c4;c5;c6];
c=c(:);
ceq=0;
for t = 1:24
ceq1(t) = Pch(t) * Pdisch(t);
ceq2(t) = sum(Pch(t) - Pdisch(t)) * dt;
ceq3(t) = sum(p(t,:)) + Pdisch(t) - Pch(t) - Pd(t);
ceq = [ceq1;ceq2;ceq3];
ceq=ceq(:);
end
end

Accepted Answer

Alan Weiss
Alan Weiss on 24 Nov 2021
You are mixing up the problem-based and solver-based formulations. You need to choose one or the other. If you are using the solver-based formulation so that you can call fmincon, then do not create any optimization variables using optimvar. Alternatively, if you are using the problem-based formulation, do not call fmincon. In all cases, don't create optimization variables in the nonlinear constraint function. Create them, if they are needed, in the main workspace.
If you want to create your constraints using the problem-based formulation and then export to the solver-based, use prob2struct. But I think that you will be happier and more successful if you choose just one approach rather than a hybrid.
For documentation of the problem-based approach, see Problem-Based Optimization Workflow. For a simple example, see Solve a Constrained Nonlinear Problem, Problem-Based. For an example related to your application, see Optimal Dispatch of Power Generators: Problem-Based.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  4 Comments
Alan Weiss
Alan Weiss on 26 Nov 2021
Sorry, you are going to have to reformulate your if statements along the lines of Integer and Logical Modeling. This requires you to create binary variables and link them to your optimization variables. It takes some thought and possibly experimentation. Take it one step at a time, you'll be pleased with the results.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!