Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
genetic algorithm constraints

Subject: genetic algorithm constraints

From: kentavros babis

Date: 5 Mar, 2009 16:49:01

Message: 1 of 9

I have a initial population of 20 parents each of which has 77 variables.
parent1 = [x1,1 x1,2 ... x1,77]
parent2 = [x2,1 x2,2 ... x2,77]
     . . . .
     . . . .
     . . . .
parent20= [x20,1 x20,2 ... x20,77]


How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
                                                         xn,22<xn,42 n=1,2,...,20

thanks in advance

Subject: genetic algorithm constraints

From: Alan Weiss

Date: 5 Mar, 2009 17:31:28

Message: 2 of 9

doc ga

Use the syntax
[x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)

x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
and returns a scalar function value evaluated at x.

If the problem has m linear inequality constraints and n variables, then

A is a matrix of size m-by-n.

b is a vector of length m.

The matrix A multiplies your 77-variable vector x.
Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
The +1 corresponds to an xn,21 variable
The -1 corresponds to an xn,41 variable
similarly for xn,22 and xn,42
Your vector b should be all zeros.

Alan Weiss
MATLAB mathematical toolbox documentation

kentavros babis wrote:
> I have a initial population of 20 parents each of which has 77 variables.
> parent1 = [x1,1 x1,2 ... x1,77]
> parent2 = [x2,1 x2,2 ... x2,77]
> . . . .
> . . . .
> . . . .
> parent20= [x20,1 x20,2 ... x20,77]
>
>
> How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
> xn,22<xn,42 n=1,2,...,20
>
> thanks in advance

Subject: genetic algorithm constraints

From: kentavros babis

Date: 5 Mar, 2009 19:59:02

Message: 3 of 9

Alan Weiss <aweiss@mathworks.com> wrote in message <gop29g$se3$1@fred.mathworks.com>...
> doc ga
>
> Use the syntax
> [x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)
>
> x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
> subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
> and returns a scalar function value evaluated at x.
>
> If the problem has m linear inequality constraints and n variables, then
>
> A is a matrix of size m-by-n.
>
> b is a vector of length m.
>
> The matrix A multiplies your 77-variable vector x.
> Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
> The +1 corresponds to an xn,21 variable
> The -1 corresponds to an xn,41 variable
> similarly for xn,22 and xn,42
> Your vector b should be all zeros.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> kentavros babis wrote:
> > I have a initial population of 20 parents each of which has 77 variables.
> > parent1 = [x1,1 x1,2 ... x1,77]
> > parent2 = [x2,1 x2,2 ... x2,77]
> > . . . .
> > . . . .
> > . . . .
> > parent20= [x20,1 x20,2 ... x20,77]
> >
> >
> > How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
> > xn,22<xn,42 n=1,2,...,20
> >
> > thanks in advance


Thanks a lot, i managed to create the matrix A and vector b but now I get error ga only accepts inputs of data type double.

Subject: genetic algorithm constraints

From: Alan Weiss

Date: 5 Mar, 2009 20:14:41

Message: 4 of 9

Yes, if you want to use this type of constraint, you need to have your
data type to be double. If you have a custom or bitstream data type,
well, you'll have to do something else.

Once again, I recommend you look in the manual:
doc ga
or
http://www.mathworks.com/access/helpdesk/help/toolbox/gads/ga.html

Alan Weiss
MATLAB mathematical toolbox documentation

kentavros babis wrote:
> Alan Weiss <aweiss@mathworks.com> wrote in message <gop29g$se3$1@fred.mathworks.com>...
>> doc ga
>>
>> Use the syntax
>> [x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)
>>
>> x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
>> subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
>> and returns a scalar function value evaluated at x.
>>
>> If the problem has m linear inequality constraints and n variables, then
>>
>> A is a matrix of size m-by-n.
>>
>> b is a vector of length m.
>>
>> The matrix A multiplies your 77-variable vector x.
>> Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
>> The +1 corresponds to an xn,21 variable
>> The -1 corresponds to an xn,41 variable
>> similarly for xn,22 and xn,42
>> Your vector b should be all zeros.
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation
>>
>> kentavros babis wrote:
>>> I have a initial population of 20 parents each of which has 77 variables.
>>> parent1 = [x1,1 x1,2 ... x1,77]
>>> parent2 = [x2,1 x2,2 ... x2,77]
>>> . . . .
>>> . . . .
>>> . . . .
>>> parent20= [x20,1 x20,2 ... x20,77]
>>>
>>>
>>> How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
>>> xn,22<xn,42 n=1,2,...,20
>>>
>>> thanks in advance
>
>
> Thanks a lot, i managed to create the matrix A and vector b but now I get error ga only accepts inputs of data type double.

Subject: genetic algorithm constraints

From: kentavros babis

Date: 5 Mar, 2009 23:50:25

Message: 5 of 9

I have read the ga doc. And even tried to write a constraint function

function [c]=constraint_y(Aineq,x)
x=x.';
c=Aineq*x;

but again I receive the same error.

Ga options are the default, double vector and so on.
I must mention that my fitness function is neural network with 77 inputs and one output.
fitness function
function objective=ann_y(eff_y,x)
x=x.';
objective=-eff_y(x);
eff_y :neural network
I have to transpose matrix for neural network to take input data from ga.
Any ideas about the data type double.

Alan Weiss <aweiss@mathworks.com> wrote in message <gopbrh$kq8$1@fred.mathworks.com>...
> Yes, if you want to use this type of constraint, you need to have your
> data type to be double. If you have a custom or bitstream data type,
> well, you'll have to do something else.
>
> Once again, I recommend you look in the manual:
> doc ga
> or
> http://www.mathworks.com/access/helpdesk/help/toolbox/gads/ga.html
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> kentavros babis wrote:
> > Alan Weiss <aweiss@mathworks.com> wrote in message <gop29g$se3$1@fred.mathworks.com>...
> >> doc ga
> >>
> >> Use the syntax
> >> [x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)
> >>
> >> x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
> >> subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
> >> and returns a scalar function value evaluated at x.
> >>
> >> If the problem has m linear inequality constraints and n variables, then
> >>
> >> A is a matrix of size m-by-n.
> >>
> >> b is a vector of length m.
> >>
> >> The matrix A multiplies your 77-variable vector x.
> >> Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
> >> The +1 corresponds to an xn,21 variable
> >> The -1 corresponds to an xn,41 variable
> >> similarly for xn,22 and xn,42
> >> Your vector b should be all zeros.
> >>
> >> Alan Weiss
> >> MATLAB mathematical toolbox documentation
> >>
> >> kentavros babis wrote:
> >>> I have a initial population of 20 parents each of which has 77 variables.
> >>> parent1 = [x1,1 x1,2 ... x1,77]
> >>> parent2 = [x2,1 x2,2 ... x2,77]
> >>> . . . .
> >>> . . . .
> >>> . . . .
> >>> parent20= [x20,1 x20,2 ... x20,77]
> >>>
> >>>
> >>> How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
> >>> xn,22<xn,42 n=1,2,...,20
> >>>
> >>> thanks in advance
> >
> >
> > Thanks a lot, i managed to create the matrix A and vector b but now I get error ga only accepts inputs of data type double.

Subject: genetic algorithm constraints

From: Alan Weiss

Date: 6 Mar, 2009 13:19:37

Message: 6 of 9

Hi, I am not sure what is going on here. Can you get GA to run with no
constraints? I suspect (but am not sure) that your fitness function
might not be formulated properly. How do you pass 'objective' to ga? Do
you call
ga(@(x)objective(eff_y,x),77)
or do you do something else? It appears (but again I am not sure) that
you are using eff_y to represent both a parameter and a function (you
call ann_y(eff_y,x), which has eff_y look like a parameter, but then you
  write objective = -eff_y(x), which has eff_y look like a function).

One more thing: it is bad practice to formulate linear inequalities in a
nonlinear constraint function. You didn't include the nonlinear
equalities, so the syntax was wrong, but even if you fix the syntax, it
is much less efficient to use a nonlinear constraint function than to
pass a linear constraint matrix A.

Good luck,

Alan Weiss
MATLAB mathematical toolbox documentation

kentavros babis wrote:
> I have read the ga doc. And even tried to write a constraint function
>
> function [c]=constraint_y(Aineq,x)
> x=x.';
> c=Aineq*x;
>
> but again I receive the same error.
>
> Ga options are the default, double vector and so on.
> I must mention that my fitness function is neural network with 77 inputs and one output.
> fitness function
> function objective=ann_y(eff_y,x)
> x=x.';
> objective=-eff_y(x);
> eff_y :neural network
> I have to transpose matrix for neural network to take input data from ga.
> Any ideas about the data type double.
>
> Alan Weiss <aweiss@mathworks.com> wrote in message <gopbrh$kq8$1@fred.mathworks.com>...
>> Yes, if you want to use this type of constraint, you need to have your
>> data type to be double. If you have a custom or bitstream data type,
>> well, you'll have to do something else.
>>
>> Once again, I recommend you look in the manual:
>> doc ga
>> or
>> http://www.mathworks.com/access/helpdesk/help/toolbox/gads/ga.html
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation
>>
>> kentavros babis wrote:
>>> Alan Weiss <aweiss@mathworks.com> wrote in message <gop29g$se3$1@fred.mathworks.com>...
>>>> doc ga
>>>>
>>>> Use the syntax
>>>> [x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)
>>>>
>>>> x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
>>>> subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
>>>> and returns a scalar function value evaluated at x.
>>>>
>>>> If the problem has m linear inequality constraints and n variables, then
>>>>
>>>> A is a matrix of size m-by-n.
>>>>
>>>> b is a vector of length m.
>>>>
>>>> The matrix A multiplies your 77-variable vector x.
>>>> Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
>>>> The +1 corresponds to an xn,21 variable
>>>> The -1 corresponds to an xn,41 variable
>>>> similarly for xn,22 and xn,42
>>>> Your vector b should be all zeros.
>>>>
>>>> Alan Weiss
>>>> MATLAB mathematical toolbox documentation
>>>>
>>>> kentavros babis wrote:
>>>>> I have a initial population of 20 parents each of which has 77 variables.
>>>>> parent1 = [x1,1 x1,2 ... x1,77]
>>>>> parent2 = [x2,1 x2,2 ... x2,77]
>>>>> . . . .
>>>>> . . . .
>>>>> . . . .
>>>>> parent20= [x20,1 x20,2 ... x20,77]
>>>>>
>>>>>
>>>>> How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
>>>>> xn,22<xn,42 n=1,2,...,20
>>>>>
>>>>> thanks in advance
>>>
>>> Thanks a lot, i managed to create the matrix A and vector b but now I get error ga only accepts inputs of data type double.

Subject: genetic algorithm constraints

From: kentavros babis

Date: 6 Mar, 2009 14:10:19

Message: 7 of 9

The ga runs very well without the constraints
[x2, fval2,scores2] = ga(@(x) ann_y(eff_y,x),77,options_y);
My objective function is this

function objective=ann_y(eff_y,x)
> > x=x.';
> > objective=-eff_y(x); ( - because I want to maximize function)
> > eff_y :neural network
I passed eff_y into input variables because I was getting error of undefined eff_y variable(eff_y neural network: 77in, 1out). But as I said the ga runs ok. The problem is that except the maximization of the fitness function I am interested in the final population as they represent y coordinates that must obey to these linear constraints.

From what I understand the ga creates new populations that are
parent = [x1,1 x1,2 ... x1,77](x2 is of this type) and then judges them by my constraints and if they pass, they continue to produce generations.

Any ideas for the data type double error.
Thanks a lot, you 've been great help so far.


Alan Weiss <aweiss@mathworks.com> wrote in message
<gor7ta$mam$1@fred.mathworks.com>...
> Hi, I am not sure what is going on here. Can you get GA to run with no
> constraints? I suspect (but am not sure) that your fitness function
> might not be formulated properly. How do you pass 'objective' to ga? Do
> you call
> ga(@(x)objective(eff_y,x),77)
> or do you do something else? It appears (but again I am not sure) that
> you are using eff_y to represent both a parameter and a function (you
> call ann_y(eff_y,x), which has eff_y look like a parameter, but then you
> write objective = -eff_y(x), which has eff_y look like a function).
>
> One more thing: it is bad practice to formulate linear inequalities in a
> nonlinear constraint function. You didn't include the nonlinear
> equalities, so the syntax was wrong, but even if you fix the syntax, it
> is much less efficient to use a nonlinear constraint function than to
> pass a linear constraint matrix A.
>
> Good luck,
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> kentavros babis wrote:
> > I have read the ga doc. And even tried to write a constraint function
> >
> > function [c]=constraint_y(Aineq,x)
> > x=x.';
> > c=Aineq*x;
> >
> > but again I receive the same error.
> >
> > Ga options are the default, double vector and so on.
> > I must mention that my fitness function is neural network with 77 inputs and one output.
> > fitness function
> > function objective=ann_y(eff_y,x)
> > x=x.';
> > objective=-eff_y(x);
> > eff_y :neural network
> > I have to transpose matrix for neural network to take input data from ga.
> > Any ideas about the data type double.
> >
> > Alan Weiss <aweiss@mathworks.com> wrote in message <gopbrh$kq8$1@fred.mathworks.com>...
> >> Yes, if you want to use this type of constraint, you need to have your
> >> data type to be double. If you have a custom or bitstream data type,
> >> well, you'll have to do something else.
> >>
> >> Once again, I recommend you look in the manual:
> >> doc ga
> >> or
> >> http://www.mathworks.com/access/helpdesk/help/toolbox/gads/ga.html
> >>
> >> Alan Weiss
> >> MATLAB mathematical toolbox documentation
> >>
> >> kentavros babis wrote:
> >>> Alan Weiss <aweiss@mathworks.com> wrote in message <gop29g$se3$1@fred.mathworks.com>...
> >>>> doc ga
> >>>>
> >>>> Use the syntax
> >>>> [x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)
> >>>>
> >>>> x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
> >>>> subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
> >>>> and returns a scalar function value evaluated at x.
> >>>>
> >>>> If the problem has m linear inequality constraints and n variables, then
> >>>>
> >>>> A is a matrix of size m-by-n.
> >>>>
> >>>> b is a vector of length m.
> >>>>
> >>>> The matrix A multiplies your 77-variable vector x.
> >>>> Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
> >>>> The +1 corresponds to an xn,21 variable
> >>>> The -1 corresponds to an xn,41 variable
> >>>> similarly for xn,22 and xn,42
> >>>> Your vector b should be all zeros.
> >>>>
> >>>> Alan Weiss
> >>>> MATLAB mathematical toolbox documentation
> >>>>
> >>>> kentavros babis wrote:
> >>>>> I have a initial population of 20 parents each of which has 77 variables.
> >>>>> parent1 = [x1,1 x1,2 ... x1,77]
> >>>>> parent2 = [x2,1 x2,2 ... x2,77]
> >>>>> . . . .
> >>>>> . . . .
> >>>>> . . . .
> >>>>> parent20= [x20,1 x20,2 ... x20,77]
> >>>>>
> >>>>>
> >>>>> How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
> >>>>> xn,22<xn,42 n=1,2,...,20
> >>>>>
> >>>>> thanks in advance
> >>>
> >>> Thanks a lot, i managed to create the matrix A and vector b but now I get error ga only accepts inputs of data type double.

Subject: genetic algorithm constraints

From: kentavros babis

Date: 7 Mar, 2009 00:40:20

Message: 8 of 9

Sorry to bother you, but I managed to get somewhere. I inserted the same options in gatool and the constaints and it worked. Also I exported the problem and created a file optimproblem containing the fitness function and the inequality constaints Aineq,Bineq, So why can't I insert them from a script?


Alan Weiss <aweiss@mathworks.com> wrote in message
<gop29g$se3$1@fred.mathworks.com>...
> doc ga
>
> Use the syntax
> [x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)
>
> x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
> subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
> and returns a scalar function value evaluated at x.
>
> If the problem has m linear inequality constraints and n variables, then
>
> A is a matrix of size m-by-n.
>
> b is a vector of length m.
>
> The matrix A multiplies your 77-variable vector x.
> Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
> The +1 corresponds to an xn,21 variable
> The -1 corresponds to an xn,41 variable
> similarly for xn,22 and xn,42
> Your vector b should be all zeros.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> kentavros babis wrote:
> > I have a initial population of 20 parents each of which has 77 variables.
> > parent1 = [x1,1 x1,2 ... x1,77]
> > parent2 = [x2,1 x2,2 ... x2,77]
> > . . . .
> > . . . .
> > . . . .
> > parent20= [x20,1 x20,2 ... x20,77]
> >
> >
> > How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
> > xn,22<xn,42 n=1,2,...,20
> >
> > thanks in advance

Subject: genetic algorithm constraints

From: Alan Weiss

Date: 9 Mar, 2009 12:24:40

Message: 9 of 9

I'm glad to hear that you managed to solve your problem using the
Optimization Tool. I guess you had a syntax error in your command-line
attempts. If you are certain that you did not have a syntax error, then
please report your problem to tech support as a bug. But, since the
Optimization Tool simply calls the command-line functions "under the
hood," it seems likely that there was a syntax error at the command line.

Alan Weiss
MATLAB mathematical toolbox documentation

kentavros babis wrote:
> Sorry to bother you, but I managed to get somewhere. I inserted the same options in gatool and the constaints and it worked. Also I exported the problem and created a file optimproblem containing the fitness function and the inequality constaints Aineq,Bineq, So why can't I insert them from a script?
>
>
> Alan Weiss <aweiss@mathworks.com> wrote in message
> <gop29g$se3$1@fred.mathworks.com>...
>> doc ga
>>
>> Use the syntax
>> [x,fval,exitflag] = ga(fitnessfcn,nvars,A,b)
>>
>> x = ga(fitnessfcn,nvars,A,b) finds a local minimum x to fitnessfcn,
>> subject to the linear inequalities A*x <= b. fitnessfcn accepts input x
>> and returns a scalar function value evaluated at x.
>>
>> If the problem has m linear inequality constraints and n variables, then
>>
>> A is a matrix of size m-by-n.
>>
>> b is a vector of length m.
>>
>> The matrix A multiplies your 77-variable vector x.
>> Create A with rows of the form 0,...,0,1,0,...,0,-1,0,...,0
>> The +1 corresponds to an xn,21 variable
>> The -1 corresponds to an xn,41 variable
>> similarly for xn,22 and xn,42
>> Your vector b should be all zeros.
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation
>>
>> kentavros babis wrote:
>>> I have a initial population of 20 parents each of which has 77 variables.
>>> parent1 = [x1,1 x1,2 ... x1,77]
>>> parent2 = [x2,1 x2,2 ... x2,77]
>>> . . . .
>>> . . . .
>>> . . . .
>>> parent20= [x20,1 x20,2 ... x20,77]
>>>
>>>
>>> How can i insert a constraint stating that xn,21<xn,41 n=1,2,...,20
>>> xn,22<xn,42 n=1,2,...,20
>>>
>>> thanks in advance

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us