Constraint function in optimization toolbox

조회 수: 1 (최근 30일)
Hussain Ja
Hussain Ja 2020년 6월 30일
편집: Hussain Ja 2020년 6월 30일
One of the variables in my model is Measured Power which is being calculated by an external black box model connected with MATLAB. The way its calculated is below:
  댓글 수: 4
Rik
Rik 2020년 6월 30일
Since this isn't really my field I don't know if I can solve the actual problem. I just noticed the globals, which tend to be a bad sign.
Also, this forum is a hobby, private consultation is a job, and it isn't my job. There are plenty of people you could hire.
Hussain Ja
Hussain Ja 2020년 6월 30일
I have skipped all the lines of code to show my constraint which is "Powermeasm". I am using the same code for the main program and the constraint function. The constraint function program has just 2 additional lines declaring the two constraints; c and ceq. I would like to know if this procedure is correct? Can I caclulate "Powermeasm" in the main program and provide a constraint on it through another constraint program? If so, why isn't it working?

댓글을 달려면 로그인하십시오.

채택된 답변

Alan Weiss
Alan Weiss 2020년 6월 30일
I think that you missed the step where you call the external function to calculate the nonlinear constraint.
function [c,ceq] = con_EbsOptimize4(para)
global asmInfo app oc model objects Pr Hu Pmeasm Hmeasm Power Powermeasm;
model.Simulate(errors);
Power = oc.CastToComp11(objects.Item('Generator_1'));
Powermeasm = Power.QREAL.Value;
c = Powermeasm-192000;
ceq = [];
end
There are two other things that you might want to keep in mind for this:
  1. Often, the function that calculates the objective function is the same as the function that calculates the nonlinear constraint. If this is true for your function, consider using the technique in Objective and Nonlinear Constraints in the Same Function.
  2. If you are using fmincon as your solver, keep in mind the problems in finite differences mentioned in Optimizing a Simulation or Ordinary Differential Equation.
Alan Weiss
MATLAB mathematical toolbox documentation
  댓글 수: 9
Alan Weiss
Alan Weiss 2020년 6월 30일
You didn't say before that "...it's not calculating the constraint violation correctly and giving it as zero when clearly the violation is much much larger." Can you give iterative display data showing what you mean, or something else to help me understand what might be happening? And did you try my previous suggestion, to set MaxGenerations_Data to 1e5?
Alan Weiss
MATLAB mathematical toolbox documentation
Hussain Ja
Hussain Ja 2020년 6월 30일
Thanks Mr. Weiss. I figured out the problem. The code is working fine now. I had written the wrong generator name.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by