How to constrain the Genetic Algorithm so that the variables are in the form of an n by m matrix

My cost function will only accept a 25 * 51 matrix as an input (1275 variables) and then it returns a single number. How will I set up the GA to minimise this cost function?
Thanks!

 Accepted Answer

Insert the following as the first line in your cost function
x=reshape(x,25,51);
Now it doesn't matter what shape the original input x, is.

3 Comments

This will mess up the cost function, it needs to be a matrix because there is a function inside which needs it to be that shape. The function is a homemade neural network function, where the shape of the matrix defines the network architecture and the values inside represent the synaptic weights. I want the GA algorithm to change the values inside the matrix. The way I have done this is to create a cost function that inputs the weight matrix, and then a (homemade) feed forward function returns the output of the neural network. This is compared to the training data (stored as a global variable), and the mean squared error of the training data and neural net output are returned from the cost function.
I want to minimise this cost function with the GA is this possible?
So the problem is that the current cost function yourfunc(x) doesn't know the matrix dimensions in advance? You will have to wrap it in another function that does know the architecture and which reshapes the input accordingly, e.g.,
fitnessFunc=@(x) yourfunc(reshape(x,[M,N]))
Surely, you have something outside of yourfunc() that does know the architecture. How else does the matrix get into its intended shape in the first place?
Sorry, your original suggestion works. I just misunderstood what it would do. Thanks for your answer!

Sign in to comment.

More Answers (0)

Products

Asked:

on 15 May 2014

Commented:

on 15 May 2014

Community Treasure Hunt

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

Start Hunting!