Vectorization of a Non-Linear Fitness Function
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I have a set of targets, a 30x2 double, which I would like to 'hit' with a projectile. To do so, I would like to pass the vector of targets through a genetic algorithm which minimises the distance between projectile and target at some point during its trajectory.
However, when I have tried to vectorize my code, I get the following error:
"When 'Vectorized' is 'on', your fitness function must return a vector of length equal to the size of the population."
My original code, which works for a single input is as follows:
%Calling genetic algorithm solver for target (x,y)
[xga,fga,flga,oga]=target(x,y);
Where:
function [xga,fga,flga,oga]=target(target_x, target_y)
%GA to minimise the distance to the target by changing the initial speed and angle of the
%projectile.
rng default
object=@(initialSpeedAngle)objective(initialSpeedAngle,target_x,target_y);
%Initial guess for speed and angle.
startAtCentre=[450 45];
%Defining a randomised initial population
initpop=10*randn(20,2)+repmat(startAtCentre,20,1);
%Defining GA options - initial population and function tolerance.
optns=optimoptions('ga','InitialPopulationMatrix',initpop,'TolFun',0);
%Running the GA
[xga,fga,flga,oga]=ga(object,2,[],[],[],[],[100 10],[800 80],[],optns);
end
And:
function out=objective(initialSpeedAngle,target_x,target_y)
%Projectile trajectory
ode45solns=solve_ode45(initialSpeedAngle(1),initialSpeedAngle(2),0,0.01,100);
%Initialising cell
DistToTarget=zeros(size(ode45solns,1),3);
%Column 1 is difference in x between trajectory and target, Column 2 difference in y. Column 3 is
%total 2D distance
DistToTarget(:,1)=target_x-ode45solns(:,4);
DistToTarget(:,2)=target_y-ode45solns(:,5);
DistToTarget(:,3)=sqrt(DistToTarget(:,1).^2+DistToTarget(:,2).^2);
out=min(DistToTarget(:,3));
end
I have tried to vectorize the problem for x_targets=targets(:,1) and y_targets=targets(:,2), giving;
%Calling the genetic algorithm for a vector of targets:
[xga,fga,flga,oga]=targetVectorized(x_targets,y_targets);
Where:
function [xga,fga,flga,oga]=targetVectorized(target_x, target_y)
%Using a genetic algorithm (GA) to minimise the distance to the target by changing the initial
%speed and angle of the projectile.
rng default
%Defining what the GA should change.
object=@(initialSpeedAngle)objectiveVectorized(initialSpeedAngle, target_x(:,1),target_y(:,1));
%Initial guess for speed and angle.
startAtCentre=[450 45];
%Defining a randomised initial population
initpop=10*randn(size(20,1),2)+repmat(startAtCentre,size(20,1),1);
%Defining GA options - initial population and function tolerance.
optns=optimoptions('ga','InitialPopulationMatrix',initpop,'TolFun',0,'UseVectorized', true);
%Running the GA
[xga,fga,flga,oga]=ga(object,2,[],[],[],[],[100 10],[800 80],[],optns);
end
And:
function out=objective(initialSpeedAngle,target_x,target_y)
%Projectile trajectory
ode45solns=solve_ode45(initialSpeedAngle(1),initialSpeedAngle(2),0,0.01,...
100);
%Size of loop, initialising cells
NumTargets=size(target_x,1);
xDistToTarget=zeros(size(ode45solns,1),NumTargets);
yDistToTarget=zeros(size(ode45solns,1),NumTargets);
%Calculating differences between the target and ode45solns
for ii=1:1:NumTargets
%xDistToTarget difference in x between trajectory and target.
xDistToTarget(:,ii)=target_x(ii)-ode45solns(:,4);
%yDistToTarget difference in y between trajectory and target.
yDistToTarget(:,ii)=target_y(ii)-ode45solns(:,5);
end
%totDistToTarget is total 2D distance.
totDistToTarget=sqrt(xDistToTarget.^2+yDistToTarget.^2);
%Smallest distance between target and projectile
out=min(totDistToTarget)';
end
Thanks in advance for any help!
0 Commenti
Risposte (1)
Jorge Martinez
il 17 Dic 2019
Modificato: Jorge Martinez
il 17 Dic 2019
I had a similar issue, the problem is that I was using 'ga' with the vectorized fitness function. The actual function is 'gamultiobj'.
Matlab R2014a
0 Commenti
Vedere anche
Categorie
Scopri di più su Genetic Algorithm in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!