Matlab: Error in copied and renamed function

1 次查看(过去 30 天)
Hello
I'm using the ga() function of Matlab for parameter optimization in the following way:
options = gaoptimset('CrossoverFraction',0.8,'FitnessScalingFcn',@fitscalingrank,'TolFun',1e-6,...
'Generations',50,'PopulationSize',20,'EliteCount',2);
minfn = @(z)myLossFunction(z);
[x,fval,~,output] = ga(minfn,8,[],[],[],[],LB,UB,[],1,options);
LB and UB are just matrices with the lower and upper bound for the variables.
The above code works very well. Now, I have made a local copy of the ga() function and named it ga2() because I want to make some modification to it. Now I'm calling it in the following way:
[x,fval,~,output] = ga2(minfn,8,[],[],[],[],LB,UB,[],1,options);
I did not do any modifications yet, I just renamed it to ga2().
Now I'm getting the following error at line 295 of the call gaminlpvalidateoptions(options); of ga2():
Undefined function 'gaminlpvalidateoptions' for input arguments of type 'struct'.
If I comment it out, I'm getting a new error on line 328 saying:
Undefined function 'gacommon' for input arguments of type 'function_handle'.
Why is this and how can I get rid off? I have just copied and renamed the ga() function.

采纳的回答

Star Strider
Star Strider 2016-6-19
You took it out of its natural environment (the toolbox directory where the original version of it used to live) and now it can’t find the functions it depends on, because it doesn’t know where to look for them.
It’s probably not good to modify MATLAB code anyway. Genetic algorithm functions aren’t difficult to write de novo, but aren’t as efficient or robust as the Global Optimization Toolbox code for them.
If the original function works with your code, just go with it!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by