# How is genetic algorithm function's ga optimfunctions used? How do I use that? What is the difference between optimset and optimfunction? Why do I receive error in this code?

1 view (last 30 days)
Devansh Shukla on 3 Aug 2023
Edited: Sam Chak on 4 Aug 2023
% Genetic Algorithm to tune PID parameters
nos_var= 3;
lb = [0 0 0];
ub= [200 200 200];
% GA optionsga_opt = optimoption('ga','Display', 'off', 'Generations', 25, 'PopulationSize',50,'PlotFcns',@gaplotbestf);---- this line has error
obj_fcn= @(k) optimizationofPID(k);
[k,best]= ga((obj_fcn),no_var,[],[],[],[],lb,ub,[],ga_opt);
Output:
Error in GAlgorithmPID (line 9)
ga_opt = optimoption('ga','Display', 'off', 'Generations', 25, 'PopulationSize',50,'PlotFcns',@gaplotbestf);
>> GAlgorithmPID
Unrecognized function or variable 'optimoption'.
Error in GAlgorithmPID (line 9)
ga_opt = optimoption('ga','Display', 'off', 'Generations', 25, 'PopulationSize',50,'PlotFcns',@gaplotbestf);
>>
Sam Chak on 4 Aug 2023
Edited: Sam Chak on 4 Aug 2023
Two questions:
1. Can you show the math model of the system?
2. How does your cost function in obj_fcn look like? This function usually tells about the desired performance like fast/slow output convergence, and high/low control effort.
The searching range [0 200] for each control gain is also relatively large. Probably most of the iterations lead to instability. Having a model allows you to run some tests and analysis so that the range can be tightened, especially with Ki and Kd. Ideally, the ranges should be set up such that GA searches within the stability regime.

Alan Weiss on 3 Aug 2023
The correct function name is optimoptions:
ga_opt = optimoptions('ga','Display', 'off', 'Generations', 25, 'PopulationSize',50,'PlotFcns',@gaplotbestf)
ga_opt =
ga options: Set properties: Display: 'off' MaxGenerations: 25 PlotFcn: @gaplotbestf PopulationSize: 50 Default properties: ConstraintTolerance: 1.0000e-03 CreationFcn: [] CrossoverFcn: [] CrossoverFraction: 0.8000 EliteCount: '0.05*PopulationSize' FitnessLimit: -Inf FitnessScalingFcn: @fitscalingrank FunctionTolerance: 1.0000e-06 HybridFcn: [] InitialPopulationMatrix: [] InitialPopulationRange: [] InitialScoresMatrix: [] MaxStallGenerations: 50 MaxStallTime: Inf MaxTime: Inf MutationFcn: [] NonlinearConstraintAlgorithm: 'auglag' OutputFcn: [] PopulationType: 'doubleVector' SelectionFcn: [] UseParallel: 0 UseVectorized: 0
Alan Weiss
MATLAB mathematical toolbox documentation

### Categories

Find more on Genetic Algorithm in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!