problem with implementing non linear constraints in a minimization problem using genetic algorithms

Hi, and sorry for my bad english. I have to minimize a function error(var,input):=(input=0 to N) max(error(var,input))
Basically I am designing a steering mechanism that needs to satisfy an input/output function relation called the Ackerman Law. By comparing the output of the actual mechanism and the output given by the Ackerman Law I define an objective error function for each input angle. For each iteration "var" vector is defined (it contains a set of 3 lenghts), I want a function that evaluates the error for each input angle. At first i had problems defining this function but by reading this thread: https://it.mathworks.com/matlabcentral/answers/16613-minimize-the-sum-of-functions-fmincon I solved it.
so my error is defined as
error=@(var) abs(max(arrayfun(@(input) error(var,input), deg2rad(0):deg2rad(1):deg2rad(25))));
By doing this at each iteration of the algorithm for the fixed index var, the function returns the maximum error.
The problem is that in order to return an acceptable set of lenghts for my mechanism, the vector var needs to satisfy 3 non linear inequality constraints (that basically allow the mechanism to work in the real world) for each input angle!!
I tried to vectorize and call the constraints as follows:
constrain1=@(var) (-deltainiziale(var));
constrain2=@(var) arrayfun(@(input) -deltab(var,input), deg2rad(0):deg2rad(1):deg2rad(40));
constrain3=@(var) arrayfun(@(input) -deltaout(var,input), deg2rad(0):deg2rad(1):deg2rad(40));
c=@(var) [constrain1(var), constrain2(var), constrain3(var)];
ceq=@(var) [];
nonlinearconstraints=@(var) [c(var),ceq(var)];
but when i call the ga
risultati=ga(error,nvars,[],[],[],[],lb,ub,nonlinearconstraints)
I keep getting errors. I tried to solve the nonconstrained problem and it seems to work, but I need to include those constraints. Thanks and Regards, Eskander Meddeb

 Accepted Answer

Hi,
the way to give nonlinear constraints to solvers like ga is described here:
A general documentation size to this topic is:
If you follow the examples and adapt them to your problem, it should work properly (except there are more errors beside this)
Best regards
Stephan

8 Comments

Thanks for your kind reply :) I read the documentation you linked me, but I still have some doubts: is it correct to use the arrayfunc function to define the constraints? If no, how can I get rid of the dependance on the variable input? And what are the other errors that you found? I defined the constraints on the same live script of the main, since those constraints depend upon parameters and other functions, I kinda don't know how to write a single function file that recalls the functions that it depends upon. This is my first time using Matlab and I am using it for a really specific problem so I am confused. Thank you and Best Regards
In general you define a function which gets the optimization variables as a vector (often x is used), then makes some calculations and returns c and ceq to the solver. The idea is to write the constraints in a way, that satisfy the requirements given in the documentation.
For example if
x(1) * x(2) < 5
is your constraint, write it as:
function [c, ceq] = nonlinear_constraint(x)
c = x(1) * x(2) - 5;
ceq = [];
end
This is because c <= 0 is the definition how c is written conform to the documentation.
Not completely correct, I'm sorry for not being clear, I'll try to explain myself better:
for each iteration the algorithm defines a vector var containing the 3 design variables that I am interested in finding.
The objective function error(var,input) is a function of var and input. Where input is the range of motion of the mechanism (from 0 to 40 degrees). When I define a set of lenghts (var) the behaviour of the mechanism is fully defined, so I want the objective function to evaluate the behaviour of the mechanism (which is fully defined by assigning a vector var) for each input angle and evaluate the maximum error, which happens at one of the 40 degrees of the range of motion, when such error is minimized, then the desired mechanism will be obtained. By vectorizing the error function with respect to input, I am able to define an objective function that depends only upon "var". (I am not interested in obtaining an "optimum input", i am interested in obtaining an optimum set of lenghts).
Now for a mechanism to be correctly defined, it needs to satisfy some geometrical constraints. These constraints depend on the lenghts of the members of the mechanism but they can be verified only for a certain range of motion (think of a door, at the end of it's range of motion you can't "open it more", this means that mathematically some constraints are verified only for a set of input angles, where input angles is how much you open the door). I want my mechanism to be working inside a pre determined range of motion, so the mathematical constraints that I define need to be respected (for a fixed set of lengths, contained in var) for each input angle. Thus for each iteration (for a fixed var) I want the mechanism to verify the constraints for each input angle. I tried to do that by vectorizing the constraints using array func and get rid of the dependance on input, like I did for the objective function, but to no avail. I hope this is more understandable, thanks for your interest, Eskander Meddeb
Yes I understand the syntax, but I have problems with the algorithm
I edited my comment - the challenge you have is to define your constraints in a way that meet the requirements. I think i can not help you with content details (thats why i changed my comment), but I can help getting started with how to write nonlinear constraints.
My suggestion is that you start writing as far as you can and come back with the specific errors/problems you have by doing so.
thank you for your replies. I managed to write the constraints in the way that meets the requirements, as you suggested that was the faulty part of the code.T https://it.mathworks.com/help/optim/ug/nonlinear-constraints.html#bsj90wb. This link helped me. I wrote the script on the Live Editor and used anonymous functions for almost every part of the code, since they are faster to write and I am new to MatLab and in a hurry. I simply thought that I could use the normal syntax for anonymous function too, while I actually can't since they cannot produce two outputs. The algorithm seems to work, even thought it's very slow (it takes 40 minutes for ga to produce a result, while for fmincon it takes only a few seconds). Thanks Stephan and kind Regards.
Glad to hear. If my answer was useful for you please accept it. I hope you have success with your project.
Best regards
Stephan

Sign in to comment.

More Answers (0)

Categories

Find more on Functions 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!