network optimization when constraint calls a function
Show older comments

I am trying to optimize a network using GA. The objective function is to minimize x(3). The formulation is as follows:
x(0) is known.
Constraint file:
% equality
c_eq(1) = x(0) + x(4) - x(1);
res_fun = f(x(1));
c_eq(2) = x(2) - res_fun;
c_eq(3) = x(2) - x(4) - x(3);
% inequality
c(1) = x(0) - x(3);
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ConstraintFunction);
And my question is that the formulation is correct?
Thank you.
10 Comments
John D'Errico
on 27 May 2017
Edited: John D'Errico
on 27 May 2017
How can we know it is correct? We don't actually see your functions.
More importantly, there is NO need (absolutely NONE) to formulate most of those constraints as nonlinear constraints. They are purely LINEAR constraints.
LINEAR: c_eq(1) = x(0) + x(4) - x(1)
However, note that x(0) is invalid MATLAB syntax. But this is linear because it is a purely linear combination of the variables.
Similarly, this is linear:
c_eq(3) = x(2) - x(4) - x(3);
as is this:
% inequality c(1) = x(0) - x(3);
I have no idea what f is, so I cannot tell if that is linear or not.
sina
on 27 May 2017
John D'Errico
on 27 May 2017
Edited: John D'Errico
on 27 May 2017
This is not linear, but the product of two unknowns:
ceq = a*b - x(1)*x(2)
It cannot go into Aeq. As for the other case,
ceq = x(2) - f(x(1))
Again, if f is a nonlinear function, then this is a nonlinear constraint. But I don't see what is your problem. Functions can call other functions! If f is an m-file, then WTP? If f is a function handle, defined somewhere, then you can either pass it around as an argument, or learn to use nested functions. nested functions can see variables in the workspace above them, and a function handle is just a variable. Again, WTP? Or make f an m-file sub-function. Again, no problem. The constraint function will be able to see sub-functions.
Of course, f must be differentiable for fmincon to work properly, but you never told us what is inside f.
Use the linear equality and inequality constraints to the extent that you can. So some of your constraints should go there. Best would be if all of your constraints are linear.
sina
on 28 May 2017
Edited: Walter Roberson
on 28 May 2017
Walter Roberson
on 28 May 2017
ceq = a*b - x(1)*x(2) should be replaced by reducing the number of variables by one and then at the beginning of your objective function define
x2 = a*b / x(1);
Walter Roberson
on 28 May 2017
You have not shown ConstraintFunction (the variable) or the function declaration of your nonlinear constraint function ?
sina
on 28 May 2017
Walter Roberson
on 28 May 2017
You have the line
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ConstraintFunction);
If ObjectiveFunction is the actual name of a function, then it is going to call the function immediately, with no arguments, and pass the return value in as the first argument to ga. If ConstraintFunction is the actual name of a function, then it is going to call the function immediately, with no arguements, and pass the return value in as the ninth argument to ga .
If ObjectiveFunction and ConstraintFunction are function handles then what are they function handles to ?
What is the "function" line of whatever function is being called by ConstraintFunction ?
sina
on 28 May 2017
Walter Roberson
on 29 May 2017
You need to post all of your code.
Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
