network optimization when constraint calls a function

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

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.
You're right, I made mistake. But I change the code.
This is one of the nonlinear constriants: for example: ceq(1) = a*b + x(1)*x(2);
a and b are constant. The result of function needs to be set as follows:
ceq(2) = x(2) - res_fun -> absolutly it's linear but the question is how it can be formulated when its input is x(1). I can not put in Aeq, beq, Am I right?
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.
Thank you for your time. You asked me about the function. It is a function that is a part of complex algorithm but the result is for example <c> and in equality section
c_eq(...) = c - x(2);
I cannot put this in linear equality part as I can not call a function with argument x(1) in <linear equality m.file>.
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);
You have not shown ConstraintFunction (the variable) or the function declaration of your nonlinear constraint function ?
Let me explain it better,
c_eq(1) = x(a)*x(b) + x(7)*x(8) - x(1)*x(2);
y = f(x(1));
c_eq(2) = x(1)*x(2) - y(1)*y(2);
c_eq(3) = x(3) - y(1);
c_eq(4) = x(4) - y(2);
c_eq(5) = x(3)*x(4) - x(5)*x(6) - x(7)*x(8);
objective function:
y = x(5);
what is f(x)? it's a function with a complex algorithm to calculate y(1) and y(2).
y(1) < x(1);
y(2) < x(2);
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 ?
I followed exactly what you mentioned but I don't know why no generation loaded
It's a function calculate something, I think it's not a problem as I run this function many times without any problem.
You need to post all of your code.

Sign in to comment.

Answers (0)

Asked:

on 27 May 2017

Commented:

on 29 May 2017

Community Treasure Hunt

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

Start Hunting!