MATLAB Answers

## Genetic algorithm and linear constraints

Asked by PJ

### PJ (view profile)

on 18 Apr 2013
Latest activity Commented on by Akshay Jain

### Akshay Jain (view profile)

on 31 Jan 2017

Hello,

I'm running a genetic algorithm with linear constraints:

```options=gaoptimset('PopulationSize',50,'Generations',10,'InitialPopulation',para0,'Display','iter','PlotFcns',@gaplotbestf,'UseParallel', 'always');
[x,fval,exitflag,output]=ga(@(para)foptim5(N,f,para),6,[0,-1,0,0,1,1;0,-1,0,1,0,0;-1,0,1,0,0,0;0,0,-1,0,0,0;0,0,0,-1,0,0],[-b;-2*b;-2*b;-b;-b],[],[],[W_l,H_l,w_l,h_l,P_l,L_l],[W_u,H_u,w_u,h_u,P_u,L_u],[],options)
```

It turns out, however, that these constraints are ignored (at least for individuals), which leads to errors in my objective function.

I do not see why, as far as I know the linear constraints should be considered by the optimization for each individual.

Matt J

### Matt J (view profile)

on 18 Apr 2013

How can we reproduce it?

Log in to comment.

## 1 Answer

### Alan Weiss (view profile)

Answer by Alan Weiss

### Alan Weiss (view profile)

on 18 Apr 2013

GA is a strictly feasible solver with respect to bounds and linear constraints. This means that, assuming the initial population is feasible with respect to these constraints, then all future individuals are feasible as well.

Therefore, I conclude that your initial population, para0, is not feasible with respect to bounds or linear constraints.

To test this conclusion, run your optimization again without giving an initial population. But I do suggest that you give an initial population range.

Good luck,

Alan Weiss

MATLAB mathematical toolbox documentation

Averina

### Averina (view profile)

on 18 Jun 2013

Hello!

I have encountered the same problem with GA. Though I generate an initial population that satisfies constraints (I have a loop for checking that out), they are not respected in the next generation. I use linear inequality constraints and boundary constraints. The boundaries are broken by almost every new individual, though weirdly the inequality constraints are satisfied.

My syntax is:

gaoptions = gaoptimset('TolCon', 1e-8, 'TolFun', 1e-2, 'CrossoverFcn', @crossoverheuristic, 'MutationFcn',@mutationgaussian, ... 'Display','iter','PopulationSize', sizepopulation, 'Generations', nrgenerations, 'MigrationFraction',0.2, 'InitialPopulation', GApopulation, ... 'EliteCount', floor(0.03*sizepopulation),'CrossoverFraction', 0.5);

[estimresultGA, estimfvalGA] = ga(@fitfunction,length(param_init), Aineqparam,bineq,[],[],param_min',param_max',[],[],gaoptions);

One way I mitigate this is to generate new values when GA produces unfeasible ones with another algorithm. But that kind of defeats the purpose of using a consistent algorithm.

Alan Weiss

### Alan Weiss (view profile)

on 18 Jun 2013

By forcing ga to use the Gaussian mutation function, you allow mutation to produce infeasible individuals. Don't do that! Use the default for constrained problems, mutationadaptfeasible.

Actually, I find many of your chosen options to be unhelpful. For example, it is often good to take a larger population size than the default, but it is never worthwhile to set a migration fraction. I suggest that you remove all of them except the ones you are certain that you need.

Alan Weiss

MATLAB mathematical toolbox documentation

Akshay Jain

### Akshay Jain (view profile)

on 31 Jan 2017

I am using mutationadaptfeasible, but still some of the population individuals are not following the linear inequalities. My chromosome size is 4 and I want x1<=x2 and x3<=x4, so my A and b are [1 -1 0 0; 0 0 1 -1] and [0;0]

Currently I am using an if condition to reject such individuals, but it would be nice if I don't have to. Any ideas what might be wrong ?

Akshay

Log in to comment.

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

### MATLAB Academy

New to MATLAB?

Learn MATLAB today!