Please how can I use this fitness function "@sumk^dj(1+wj)" in optimization tool without getting errors like undefined function or variable 'dj'

1 visualizzazione (ultimi 30 giorni)
I'm doing my project on designing a genetic algorithm for robot path planning but I'm having problem with computing a fitness function.

Risposte (1)

Geoff Hayes
Geoff Hayes il 15 Set 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 Commenti
ibrahim
ibrahim il 16 Set 2014
Hello Geoff, Could u please tell me whats wrong with my codes? i tried this: f(x)=sumk^dj(1+wj); y=sumk^dj(1+wj); Function y= @myfitness_function(k,dj,wj); number of variables=3; [x,fval]=ga(@myfitnessfunction,3); but i got an error displaying the messsage below.
Thanks. Optimization running. Error running optimization. Attempt to execute SCRIPT myfitness_function as a function: C:\Users\IBRAHIM\Documents\MATLAB\myfitness_function.m
Also, i tried this: f(x)=sumk^dj(1+wj); y=myfitnessfunction(k,dj,wj); x=ga(@myfitnessfunction) and it gave error message.
Geoff Hayes
Geoff Hayes il 16 Set 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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by