Asked by Harvey Rael
on 18 May 2018

So, I have a function that contains 3 variables (for arguments sake x1,x2,x3) and I want to find the optimal values for the function using a genetic algorithm while ensuring the objective function remains at zero.

For example, y = f(x1,x2,x3)-5 (where the f(x1,x2,x3) is some messy algebraic equation with the three variables).

The GA keeps returning the optimal values where y = -5; where as I want the optimal values where y = 0. Any ideas?

Answer by John D'Errico
on 19 May 2018

Edited by John D'Errico
on 19 May 2018

There are still significant problems in what you want to do. Can you use GA to solve it? Well, no, not really in any rational way. It simply is not a case of optimization.

You say that you want to optimize some function f(x1,x1,x3), in the sense that

y = f(x1,x2,x3)-5 = 0

The problem is, there are infinitely many such solutions in general. Not just one. Yes, f is some messy algebraic thing. But there are still infinitely many solutions almost always, if any solutions do exist. In a rare case, you might have a unique solution. For example, suppose you defined f as:

f(x1,x2,x3) = x1^2 + x2^2 + x3^2 + 5

Clearly, there is only one possible set of values for x1,x2,x3, such that f(x1,x2,x3)-5=0. So it is trivially easy to have a problem where only one solutions exists. But in the real world, on any kind of real world problem, we won't get that lucky that a solution will be unique. And of course, you have not given us the function.

You also say that you have bounds on the variables but they change nothing of any significance, except make the "search" easier. But what is there in this in terms of a search?

So, now lets understand what form the solution locus will take. You have a 3 dimensional space for the problem, One equation, in the form of an EQUALITY. The general form for the solution locus will be what I like to think of as a 2-manifold, embedded in the R^3 space of our unknowns.

You can think of this 2-manifold assort of the surface of a potato. Thus, visualize some general possibly bumpy surface in 3-dimensions as the solution set, of all values such that the target is achieved.

If you had only two variables, the solution method I usually recommend is a simple contour plot! Think of it, what does a contour plot show you? Pick any single contour line, and that is essentially what you are trying to find. A contour line would be a 1-manifold in a 2-dimensional space. So we have simply onestep up from that. If you think of the 2-manifold as sort of the surface of a sphere, you will have the idea.

Now, how can we locate the solution set in 3 dimensions? The simple answer is an iso-surface, which is just the same idea of a level set, but in a 3-dimensional space. Let us make one as an example. I'll just pick some general thing, and see how it works.

[X,Y,Z] = meshgrid(-3:.1:3,-3:.1:3,-3:.1:3);

W = X + X.*Y + Y.^2 - 0.5*Z.^2 - 1;

Ph = patch(isosurface(X,Y,Z,W,0));

Ph.FaceColor = 'red';

Ph.EdgeColor = 'none';

daspect([1 1 1])

view(3)

camlight; lighting phong

grid on

box on

So here the solution locus is sort of a hyperbolic surface, within the domain of the chosen bounds. It is again a 2-manifold, but this time a bit more complicated in shape than a vaguely spherical simple potato.

If you want the solution itself, you can get it as a triangulated set of facets.

FV = isosurface(X,Y,Z,W,0)

FV =

struct with fields:

vertices: [12031×3 double]

faces: [23484×3 double]

Your problem should be similar, although since we don't have the relation you want to solve, that is where I must give up.

As I said above, this is simply not a problem of optimization, where GA would be of any value. Now, had you some reason for preferring some combinations of the variables, then you would formulate it all differently. So perhaps you have some true objective, thus some way to designate the desirability for any set of the variables? Then that would be your objective for an optimization, and the equality you have posed would be an equality constraint. Then you could use GA, or fmincon, or any optimizer that would solve a nonlinear optimization subject to nonlinear equality and bound constraints.

Harvey Rael
on 24 May 2018

Hi! Thanks for the in depth explanation and apologies for the late reply I was out of the country!

That was really a great answer, which I think I understood. To answer the questions at the end.. in terms of your example answer, I know I want x1 to be between 0 and 2 say, and x2 to be between 0 and 1, and x3 to be between 2 and 7. The desire is essentially to draw a spiral from known start point A to known end point B. x1, x2, x3 are shaping parameters of the spiral. To me, what I am trying to do is to "optimise" the shaping parameters of the spiral (ie find the best combination within these search bounds) that satisfy the equation. I do also know that the absolute value of x1*x2^2 has to be between 0 and 1 so that is a constraint I have applied. I hope that helps, though judging from your answer all the info I have supplied just reduces the search space

Harvey Rael
on 24 May 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Geoff Hayes (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401449-how-do-i-use-ga-to-optimize-the-variables-while-keeping-the-objective-function-at-a-certain-point#comment_569470

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401449-how-do-i-use-ga-to-optimize-the-variables-while-keeping-the-objective-function-at-a-certain-point#comment_569483

## Harvey Rael (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401449-how-do-i-use-ga-to-optimize-the-variables-while-keeping-the-objective-function-at-a-certain-point#comment_569639

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401449-how-do-i-use-ga-to-optimize-the-variables-while-keeping-the-objective-function-at-a-certain-point#comment_569663

Sign in to comment.