# How do I use GA to optimize the variables, while keeping the objective function at a certain point?

12 views (last 30 days)
Harvey Rael on 18 May 2018
Commented: Harvey Rael on 24 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?
Harvey Rael on 19 May 2018
Geoff - Yeah it makes absolute sense, I had a brain fart and forgot that's what the GA was going to return. Being new to the game on GA's, how would I go about "punishing" values less than zero?
Walter - If I have understood you correctly, I have the variables constrained in such a way that they are all between certain values that I know I want them to be between, so all I want is to find the ones now that satisfy the case where y=0.
Walter Roberson on 19 May 2018
If you want to aim for zero, minimize the square of the function.

John D'Errico on 19 May 2018
Edited: 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
I.e, a spiral that satisfies the equation, with optimum windiness, going around the center point the most optimum times. The application of the question I am posing is the beginning of a trajectory optimisation problem.

### Categories

Find more on Solver Outputs and Iterative Display in Help Center and File Exchange

R2015b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!