How to code a cost function for a Genetic Algorithm?

4 views (last 30 days)
I am reading the book Practical Genetic Algorithms by Randy Haupt and Sue Haupt. The book gives example Matlab code for a simple binary genetic algorithm (GA). I've attached a copy of the the script for the code. The only line I've added to the code is line 19 where I'd like to know how many generations are being performed before the program is terminated. The code requires that the user provides a cost function in order for the GA to perform calculations. I'm trying to learn the syntax of the GA and understand how to write the code for a cost function. I want to use the GA to find the minimum of the function below:
The cost function that I worte is provided below:
function y = costfunction(x)
y = abs(x)+sin(x)+5
end
I want to check and make sure that I'm coding the proper syntax for the cost function to be implemented with the GA. Also, the output that I'm getting provided below:
ans =
1 5
Index exceeds matrix dimensions.
Error in GA_Book (line 37)
pop(ix,iy)=1-pop(ix,iy);
I know that the minmumim to my function is 5, but why am I geting two values for the answer? Also, I'm not sure why I'm getting an error for line 37. Any help or clarification with this topic would be appreciated as I am a beginer to learning GA

Answers (0)

Community Treasure Hunt

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

Start Hunting!