Error existent field - classes

I've coded PSO Simulink, and make particle as a class, main coding as below:
clc;
clear;
close all;
%%Initialization
% Parameters
nPop = 50;
MaxIt = 100;
w = 1;
c1 = 2;
c2 = 2;
% Generate Particle Template
sample_particle = particle();
% Create Population Array
particles(nPop) = particle();
% Array to Hold Best Cost Value on Each Iteration
BestCosts = zeros(MaxIt, 1);
%%Main Loop of PSO
for it=1:MaxIt
for i=1:nPop
% Update Velocity
particle(i).velocity = w*particle(i).velocity ...
+ c1*rand(particle(i).VarSize).*(particle(i).bestPosition - particle(i).position) ...
+ c2*rand(particle(i).VarSize).*(particle(i).globalbest - particle(i).position);
% Update Position
particle(i).position = particle(i).position + particle(i).velocity;
% Evaluation
particle(i).Cost = CostFunction(particle(i).position);
% Update Personal Best
if particle.Cost < particle.Best.Cost
particle.Best.Position = particle.position;
particle.Best.Cost = particle.Cost;
% Update Global Best
if particle.Best.Cost < GlobalBest.Cost
GlobalBest = particle.Best;
end
end
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;
% Display Iteration Information
disp(['Iteration' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
end
And Class coding:
classdef particle
%PARTICLES Summary of this class goes here
% Detailed explanation goes here
properties
% Parameters
VarMin = -10;
VarMax = 10;
VarSize = 2;
% Template
position = [];
velocity = [];
cost = [];
bestPosition = [];
bestCost = [];
% Initialize Global Best
globalbest = inf;
end
methods
function [ obj ] = particle( varargin )
function circle = CostFunction(x,y)
circle = (x.^2)+(y.^2);
end
% Generate Random Solution
obj.position = unifrnd(obj.VarMin, obj.VarMax, obj.VarSize);
% Initialize Velocity
obj.velocity = zeros(obj.VarSize);
% Evaluation
obj.cost = CostFunction(obj.position,obj.position);
% Update the Personal Best
obj.bestPosition = obj.position;
obj.bestCost = obj.cost;
% Update Global Best
if obj.bestCost < obj.globalbest
obj.globalbest = obj.bestCost;
end
end
end
end
then, after run got an error:
Reference to non-existent field 'position'.
Error in main (line 35)
particle(i).position = particle(i).position + particle(i).velocity;
and I want to plot swarm particles on its moving motion.

 Respuesta aceptada

Steven Lord
Steven Lord el 20 de Feb. de 2018

1 voto

Don't define a variable in your code with the same name as a function or class you want to use in that code.
Change the variable name particle in your PSO code to particles (which I would avoid as too easy for a human to mistype or misread as particle) or something like listOfParticles and make that an array of particle objects.

6 comentarios

Ali Ali
Ali Ali el 20 de Feb. de 2018
I did, but, I had another error.. please, check:
clc; clear; close all; %% Initialization
% Parameters nPop = 50; MaxIt = 100; w = 1; c1 = 2; c2 = 2;
% Generate Particle Template sample_particle = particle();
% Create Population Array particles(nPop) = particle();
% Array to Hold Best Cost Value on Each Iteration BestCosts = zeros(MaxIt, 1);
%% Main Loop of PSO
for it=1:MaxIt
for i=1:nPop
% Update Velocity
particles(i).velocity = w*particles(i).velocity ...
+ c1*rand(particles(i).VarSize).*(particles(i).bestPosition - particles(i).position) ...
+ c2*rand(particles(i).VarSize).*(particles(i).globalbest - particles(i).position);
% Update Position
particles(i).position = particles(i).position + particles(i).velocity;
% Evaluation
particles(i) = CostFunction(particles(i).position,particles(i).position);
% Update Personal Best
if particles(i).Cost < particles(i).Best.Cost
particles(i).Best.Position = particles(i).position;
particles.Best.Cost = particles.Cost;
% Update Global Best
if particles(i).Best.Cost < GlobalBest.Cost
GlobalBest = particles.Best;
end
end
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;
% Display Iteration Information
disp(['Iteration' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
end
....................
class
classdef particle %PARTICLES Summary of this class goes here % Detailed explanation goes here
properties
% Parameters
VarMin = -10;
VarMax = 10;
VarSize = 2;
% Template
position = [];
velocity = [];
cost = [];
bestPosition = [];
bestCost = [];
% Initialize Global Best
globalbest = inf;
end
methods
function [ obj ] = particle( varargin )
function z = CostFunction(x,y)
z = (x.^2)+(y.^2);
end
% Generate Random Solution
obj.position = unifrnd(obj.VarMin, obj.VarMax, obj.VarSize);
% Initialize Velocity
obj.velocity = zeros(obj.VarSize);
% Evaluation
obj.cost = CostFunction(obj.position,obj.position);
% Update the Personal Best
obj.bestPosition = obj.position;
obj.bestCost = obj.cost;
% Update Global Best
if obj.bestCost < obj.globalbest
obj.globalbest = obj.bestCost;
end
end
end
end
.....................
function
function z = CostFunction(x,y)
z = (x.^2)+(y.^2);
end
Steven Lord
Steven Lord el 20 de Feb. de 2018
What is the full text (that means everything displayed in red) of the error message you receive when you execute your code?
Also I recommend eliminating this line from your code:
clc; clear; close all; %%Initialization
That makes it much more difficult to debug your code or compare the results from a run before you make a change to the results after you make the change.
Ali Ali
Ali Ali el 20 de Feb. de 2018
>> main No appropriate method, property, or field 'Cost' for class 'particle'. Error in main (line 38) if particles(i).Cost < particles(i).Best.Cost
Steven Lord
Steven Lord el 20 de Feb. de 2018
That makes sense. The particle class does not have a property Cost. It has a property cost (lower-case) but 'cost' is not the same as 'Cost'. Case matters.
Ali Ali
Ali Ali el 20 de Feb. de 2018
I appreciated this, thank you.
Ali Ali
Ali Ali el 20 de Feb. de 2018
Undefined function or variable 'globalbest'. Error in main (line 55) BestCosts(it) = globalbest;

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 20 de Feb. de 2018

Comentada:

el 20 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by