MATLAB: Fuzzy controller using genetic algorithm

genetic algorithmoptimization

I am trying to implement a fuzzy logic controller for manipulator control using genetic algorithm. I have seven membership function defined for each of the following parameters, position of link 1, velocity of link 1, position of link 2 and velocity of link 2. I am trying to use genetic algorithm to change the value obtained from my membership function in order to provide better control. I am adding a certain value 'x' to the degree of membership for each of these values. That makes a total of 7×4, 28 variables in 'x'.
1. I only want to impose upper and lower bounds on the values of x and no other non-linear constraints. What should be the format of my contrarians function? and is there an easier way to do it without making a constraints function? Here is how I am trying to implement it. Please let me know if its correct.
%%constraint function
function [c_eq] = myConstraints(x)
c_eq = [];
end
ObjFcn = @myFitness;
nvars = 28;
LB = [-0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2];
UB = [0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2];
ConsFcn = @myConstraints;
[x, fval] = ga(ObjFcn,nvars);
2. I have a global variables, position1 and position2 declared inside my fitness function which are matrices storing the values of positions for every passing iteration. These variables are not getting reflected in my workspace. How can I get them to show up there? I tried initialising it inside my main function using:
global n position1 position2
n=1;
position1(n)=0;
and then my n gets incremented inside my fitness function and the new value of position is supposed to be added to position1 and position2 inside my fitness function. Now these new values are not getting reflected in my workspace and the only value contained by these variables in my workspace is the one I initialised it to have.
3. If I have some extra variables (which I have declared as global in both, main and the fitness function) and I make changes to them inside my fitness function (for the purpose of calculation of the parameter I wish to minimise), will these changes be reflected in my main function when I call ga or will ga simply tell me the values of 'x' that I should use inside main to make these respective changes to my extra variables?
Some extra information about my code that might help you understand my questions better- I am slowly increasing the value of position in roughly 600 iterations in my main program. I am calling ga in every iteration. I am using ga to minimise the total error in position (abs(desired_val_position1-actual_val_position1) + abs(desired_val_position2-actual_val_position2). The value of the current positions are being updated only inside the fitness function. (I have presumed that the changes I make to some extra variables inside my fitness function will be reflected in my main function and the workspace.

Best Answer

1) For bounds constraints, you do not need any constraints functions.
ObjFcn = @myFitness;
nvars = 28;
LB = [-0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2 -0.2];
UB = [0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2];
A = []; b = []; Aeq = []; beq = [];
[x, fval] = ga(ObjFcn, nvars, A, b, Aeq, beq, LB, UB);
2) With respect to global variable:
better would be to use nested functions with shared variables. global is the slowest form of variable access.
But if you do use global, then make sure that you declare the variables as global in the workspace where you want to view them. If your "main function" is a function rather than a script and you are viewing from the command window without the debugger, then you would want to also give the global command at the command line.
3) If you are making changes to a variable "for the purpose of calculation of the parameter I wish to minimise" then you are introducing discontinuities in your calculation. ga() can survive discontinuities where most other routines cannot (particleswarm can survive discontinuities as well), but you lose efficiency and end up hoping that a mutation will drop you in the right spot. But in any case, if you have declared them as global and modify them, then you can retrieve them afterwards by access them from the global workspace.
4) "The value of the current positions are being updated only inside the fitness function."
Anything that you are changing inside your fitness function with hopes of returning should probably be part of your variables being minimized.
5) "I am slowly increasing the value of position in roughly 600 iterations in my main program."
I wonder if Simulated Annealing, sa(), might be better for your purpose?