MATLAB Answers

Adding two linear inequality constraints in Optimization toolbox

20 views (last 30 days)
Hussain Ja
Hussain Ja on 1 Jul 2020
Commented: Matt J on 5 Jul 2020
I have the following code for maximizing revenue for a plant:
function varargout = EbsOptimize5_3(para)
para = reshape(para, 2, []);
% para = [x11,x12,x13;x21,x22,x23];
%para = reshape(para, 2, []);
global asmInfo app oc model objects Sun SunDNI SunDNImeasm M1 M2 M1measm M2measm TotalmassFlow;
if (~(size(asmInfo)))
disp('call EbsOpen.dll')
asmInfo = NET.addAssembly('C:\Program Files\Ebsilon\EBSILONProfessional 14 P3\EbsOpen.dll');
app = EbsOpen.ApplicationClass;
oc = app.ObjectCaster;
model = app.Open('C:\Users\Hassan Bukhari\Desktop\Priority Study\New EXP CSP 3_with_TimeSeries.ebs');
model.ActivateProfile('Charging');
objects = model.Objects;
%%%%%%%%%%%%
Sun = oc.CastToComp117(objects.Item('Sun'));
SunDNI = Sun.DNI;
%SunDNImeasm = Sun.DNI.Value;
M1 = oc.CastToComp33(objects.Item('Start_value_4'));
M2 = oc.CastToComp33(objects.Item('Start_value_5'));
M1measm = M1.M;
M2measm = M2.M;
end
SunDNImeasm = [600; 700];
powerPrice = [100; 150];
Power = zeros(1,length(SunDNImeasm));
Revenue = zeros(1,length(SunDNImeasm));
M1v = zeros(1,length(SunDNImeasm));
M2v = zeros(1,length(SunDNImeasm));
massFlow = zeros(1,length(SunDNImeasm));
for i=1:length(SunDNImeasm)
Sun.DNI.Value = SunDNImeasm(i);
M1measm.Value = para(1,i);
M2measm.Value = para(2,i);
errors = app.NewCalculationErrors();
model.Simulate(errors);
Gen = oc.CastToComp11(objects.Item('Generator'));
%Power = (Gen.QREAL.Value);
Power(i) = Gen.QREAL.Value/1000 ;
M1v(i) = M1measm.Value;
M2v(i) = M2measm.Value;
massFlow(i) = 3600*(M1v(i)+M2v(i));
Revenue(i) = -(Power(i)*powerPrice(i));
end
TotalRevenue = sum(Revenue);
TotalmassFlow = sum(massFlow);
varargout{1} = TotalRevenue;
if nargout > 1
varargout{2} = Power;
varargout{3} = Revenue;
varargout{4} = TotalmassFlow;
varargout{5} = M1v;
varargout{6} = M2v;
end
%TotalPower = sum(Power);
%TotalRevenue = Power*powerPrice;
%TotalRevenue = sum(Revenue);% Aineq=-ones(1,nvars);
% bineq=-9.36e3/3600;
end
I have added a linear inquality constraint on total mass flow as:
If I want to add another constraint on "Power" being calculated by the black box model attached to MATLAB, how can I do that?

Answers (3)

Alan Weiss
Alan Weiss on 1 Jul 2020
You say that your new constraints are linear. In that case, you add one row to A and to b for each new constraint. For example, if there are two new constraints, x(1) + 2*x(2) <= 17 and x(3) - 4*x(4) <= -3, you would have the following:
A = -ones(1,4);
A = [A;1 2 0 0];
A = [A;0 0 1 -4];
b = -9.36e6/3600;
b = [b;17;-3];
Alan Weiss
MATLAB mathematical toolbox documentation
  9 Comments
Catalytic
Catalytic on 2 Jul 2020
But I think what Catalytic meant is, can we really know that to be the case? Absolutely no detail about how Power is computed is visible to us, so we do not know whether the calculations are linear combinations or not.
Yes, exactly. If it turns out that Power is a linear function of the para(i), we could conceivably replace the black box calculation of the constraints with matrices..

Sign in to comment.


Walter Roberson
Walter Roberson on 1 Jul 2020
Your power is not a linear constraint: your power is being calculated based upon values created by your external process, and to put a constraint upon it you need your nonlinear constraint function to call the same external process and go through the same power calculation. I discussed the efficient way to do that in my Answer, and discussed why you need to do things that way.

Matt J
Matt J on 2 Jul 2020
Edited: Matt J on 2 Jul 2020
But the optimization algorithm should still be able to handle it, right? Can it be accomodated in Aineq and bineq?
One way you can test if a black box function is linear is using my func2mat File Exchange submission,
It will generate a matrix representation A*para(:) of the function Power(para) under the hypothesis that the function is linear. However, you would then need to verify the hypothesis by testing whether A*para(:) is in agreement with the black box calculation on lots of different choices of para.
  30 Comments
Matt J
Matt J on 5 Jul 2020
Who can say,without sitting next to you to see what you are running? The use of global variables is prone to causing accidents like that, though. It's highly discouraged
in favor of other fixed parameter passing methods described here

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!