MATLAB: How to use ga optimization function with 3 variables (2 cell arrays, 1 array of different dimensions)


I am generally new to matlab's optimization toolbox and I am trying to optimize a function which has as arguments 3 variables of different sizes and types. To be more specific, for the first 2 variables I am trying to use a cell array (1,5) whose cells has (2,5) arrays and for the third one I am trying to use an array (3,5). I have a problem declaring the following:
1. the number of variables of the problem.
2. the upper bound and lower bound matrices and
3. the way the objective function and the constraints take them as arguments, for example if F the objective function do I have to write:
function y = F(x,y,z) % where x = the first cell array, y the second one and z the (3,5) array?
Thank you for answering in advance,

Best Answer

The documentation for objective functions states that you need to put all of your variables onto one row vector for ga. Basically, MATLAB passes a row vector x to your fitness function, and it is up to your fitness function to process the point x as needed.
For your specific case, let's talk about matrices rather than cell arrays. Your variable x (which I'll call x1) can be represented as a 5-by-2-by-5 array, as can y. It sounds like z is a 3-by-5 array. So your fitness function could look as follows:
function f = myfitness(x)
x1 = x(1:5*2*5);
y = x(5*2*5+1:2*5*2*5);
z = x(2*5*2*5+1:end);
x1 = reshape(x1,5,2,5)
y = reshape(y,5,2,5);
z = reshape(z,3,5);
% Now compute the fitness using x1, y, and z
When you call ga you have the number of variables is 2*5*2*5 + 3*5 = 115. I hope that is clear. And, of course, inside your fitness function you are free to turn x1 and y into cell arrays, if you like.
Alan Weiss
MATLAB mathematical toolbox documentation