Please how can I use this fitness function "@sumk^dj(1+wj)" in optimization tool without getting errors like undefined function or variable 'dj'
2 views (last 30 days)
Show older comments
I'm doing my project on designing a genetic algorithm for robot path planning but I'm having problem with computing a fitness function.
0 Comments
Answers (1)
Geoff Hayes
on 15 Sep 2014
Ibrahim - look closely at the error message: undefined function or variable 'dj'. It is telling you that your code is trying to use dj before it has been defined and it is unclear whether dj is a function or a variable. Now look at your code
@sumk^dj(1+wj)
Have you written this code before you have defined dj? Is dj supposed to be a matrix/array or a function? Why have you included the @ symbol?
You indicate that you are using this code as the fitness function for the genetic algorithm, which may mean you are passing this an input to ga. Is this the case? If so, it may be easier to define your fitness function in a separate file as
function [fitness] = myFitnessFunction(inputData)
where myFitnessFunction is the function you are trying to optimize (minimize), inputData is your array of input variables, and fitness is the scalar fitness/score for your input data. Then you would pass this function to ga as
x = ga(@myFitnessFunction,…);
2 Comments
Geoff Hayes
on 16 Sep 2014
Ibrahim - please post code that is readable (formatted as code) using the {} Code button. It is not clear how you are using the above lines of code. Are they in a function body or a script or..?? The line number of variables=3; is not valid MATLAB code.
Please read the documentation for the ga function (I provided the link in my answer) and create a fitness function (like I suggested) in a separate file called myFitnessFunction.m or as a nested function within your main block of code.
Since you have three variables, then the body for your function should be something like
function [fitness] = myFitnessFunction(inputData)
var1 = inputData(1);
var2 = inputData(2);
var3 = inputData(3);
% do something with these three variables to produce the fitness
fitness = ...;
end
I don't know how you computed sumk, dj, or wj, and you have not described what the three variables that act as inputs to the fitness function.
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!