Adding two linear inequality constraints in Optimization toolbox
11 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(3 个)
Alan Weiss
2020-7-1
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 个评论
Matt J
2020-7-2
编辑:Matt J
2020-7-2
Constraints based on values that are not linear combinations of variables must be handled in the nonlinear constraint function.
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.
One question that I would add as well is, how fast is the Power computation? Are we seeing slow convergence, or is it simply that the black box calculations are slow and burdensome.
Catalytic
2020-7-2
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..
Walter Roberson
2020-7-1
I refer you to https://www.mathworks.com/matlabcentral/answers/557419-constraint-function-in-optimization-toolbox#answer_459217 where I explained what you need to do.
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.
0 个评论
Matt J
2020-7-2
编辑:Matt J
2020-7-2
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 个评论
Matt J
2020-7-5
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!