Constraint function in optimization toolbox

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 Comments

Why are you using so many globals? They should be avoided as much as possible anyway, and the function you posted is ignoring most of them, as well as the input.
Comment sent as email:
Can I send you an email with more details as the program is proprietary?
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.
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?

Sign in to comment.

 Accepted Answer

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 Comments

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?
When I run the code you have shown above I get the following error:
Optimization running.
Error running optimization.
Output argument "c" (and maybe others) not assigned during call to "con_EbsOptimize4".
I also get the following warning:
Warning: The value of local variables may have been changed to match the globals. Future versions of MATLAB will require
that you declare a variable to be global before you use that variable.
I'm sorry, I canot tell what you did. Please include your code. Feel free to not include all the code that calculates the values, but I do need to see the fmincon call and the constraint function definition, at least the first few lines of that function.
Alan Weiss
MATLAB mathematical toolbox documentation
The main program is running fine. The constraint function doesn't appear to be working as the output I get is violating the constraint.
I see the objective function calculation and the constraint function calculation. I also need to see the fmincon call, and any options settings.
Alan Weiss
MATLAB mathematical toolbox documentation
I call it as follows:
code11(2,[1.8,0.1],[3,0.9],15,3)
Things look OK to me. Try setting MaxGenerations_Data to 1e5 and see if that helps. The nonlinear constraint solver takes many, many generations to work.
I suggest that you might want to try patternsearch on this problem, and maybe surrogateopt if you have a recent MATLAB version, as ga is not very efficient when handling nonlinear constraints.
Alan Weiss
MATLAB mathematical toolbox documentation
My concern is not the optimality of the solution. It's just that the solution that it returns clearly violates the supplied constraint by a significant margin. So I have a hunch that somehow it's not calculating the constraint violation correctly and giving it as zero when clearly the violation is much much larger
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
Thanks Mr. Weiss. I figured out the problem. The code is working fine now. I had written the wrong generator name.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 30 Jun 2020

Edited:

on 30 Jun 2020

Community Treasure Hunt

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

Start Hunting!