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

# Constraint function not working in genetic algorithm 'GA' toolbox

Asked by Stewie on 6 Oct 2012

I'm trying to solve the Jiles-Atherton model for ferromagnetic hysteresis using the genetic algorithm optimisation toolbox.

The JA model is a first order ODE that has 5 parameters for the ferromagnetic material, which will then allow you to plot B as a function of H.

What I’m doing is simulating a run with a particular set of the five parameters, to get some test data, and then trying to take this test data and see if I can find what those 5 values were to begin with.

Using various techniques I’m able to get in the ‘ball-park’ but then I need to get the exact value.

In this example, I know my values are P=[1e6, 1e3, 0.1, 2e3, 1e-3]

and my lower and upper ‘ball-park’ estimates from the simulated data are

PL=[0.9e6, 750, .082, 1e3, 1e-6]; PU=[1.1e6, 1500, 0.15, 4e3, 10e-3];

I wrote a function to give me the mean-squared error between each simulated run and my test data. This is called “fitness_complete” and takes the five parameters and puts out one fitness value. This is working fine.

The problem is that there are many local minima, but only one global, whose value is zero. So I need to use the GA optimisation so that I don’t accidentally find one of the local minima that the linear searches have been giving me. As a result, I cannot simply linearly optimise one parameter at a time.

When I run my program at both extremes of the bounds I get a nice respectable answer, however you cannot get a valid answer from some of the combinations or ‘children’ that the GA generates.

So as is my understanding, the constraint function tells the GA whether the child is valid or not. Am I correct? If not then I expect the GA to ignore that ‘child’ and generate another one.

I wrote a function, ‘constraint_complete’ to tell the GA whether the child is valid or not by basically trying to run it and issuing a catch if an error occurs, not the most elegant solution, I know, but it works, and is listed at the end of my post if you’re interested.

As I understand, the GA should ignore any children who don’t meet the requirement C(P)<0 Ceq(P)=0

My function returns C as [] (empty), and Ceq as either 0 (for valid), or +1 (for invalid).

My problem is, is that the GA doesn’t seem to be ignoring these infeasible children, and just keeps on running until it grinds to a halt.

I am running the following command for the GA:

X = ga(@fitness_complete,5,[],[],[],[],PL,PU,@constraint_complete)

Any ideas guys? (or gals?)

I’ve tried searching for examples but this is exactly what they tell me to do.

This is my first post so any and all Lyapunov stable feedback would be appreciated :)

Thanks,

-Stewie

The code:

invalid=0;

try

```    for amp=1:N:7
Amplitude=Amplitudes(amp);```
```        Ht=data{amp,per}(1,:);
Mt=data{amp,per}(2,:);```
`        constraint(Amplitude,T,Ht,Mt,P)`
```    end
catch
invalid=1;
end```

if(invalid==1)

```    invalid
c=[];```
`    ceq=1;`

else

`    invalid`
`    c=[];`
`    ceq=0;`

end

## Products

No products are associated with this question.

Answer by Alan Weiss on 10 Oct 2012

Perhaps this explanation of the nonlinear constraint algorithm answers your question sufficiently well.

If not, feel free to ask again.

Alan Weiss

MATLAB mathematical toolbox documentation

## 1 Comment

Stewie on 11 Oct 2012

Yeah I tried that using ceq, as shown in my code, but it just seems to ignore it.