Class Undefined Function or Variable

8 ビュー (過去 30 日間)
Ali Ali
Ali Ali 2018 年 2 月 18 日
コメント済み: Ali Ali 2018 年 2 月 18 日
in the class shown .. I got the errors as below:
1.
"Invalid default value for property 'VarSize' in class 'particle':
Undefined function or variable 'nVar'."
2.
"particle Undefined function or variable 'VarMin'.
Error in particle (line 45)
obj.position = unifrnd(VarMin, VarMax, VarSize); "
3.
"particle
Undefined function or variable 'CostFunction'.
Error in particle (line 51)
obj.cost = CostFunction(obj.position); "
code is:
classdef particle
%PARTICLES Summary of this class goes here
% Detailed explanation goes here
properties
CostFunction = @(x) Sphere(x); % Cost Function
nVar = 2; % Number of Unknown (Decision) Variables
VarSize = nVar; % Matrix Size of Decision Variables
VarMin = -10; % Lower Bound of Decision Variables
VarMax = 10; % Upper Bound of Decision Variables
MaxIt = 100; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
w = 1; % Inertia Coefficient
c1 = 2; % Personal Acceleration Coefficient
c2 = 2; % Social (Global) Acceleration Coefficient
position = [];
velocity = [];
cost = [];
bestPosition = [];
bestCost = [];
end
methods
function z = sphere(x)
z = sum(x.^2);
end
function [ obj ] = particle( varargin )
% Generate Random Solution
obj.position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
obj.velocity = zeros([1 2]);
% Evaluation
obj.cost = CostFunction(obj.position);
% Update the Personal Best
obj.bestPosition = obj.position;
obj.bestCost = obj.cost;
end
end
end

採用された回答

Walter Roberson
Walter Roberson 2018 年 2 月 18 日
You need to change
obj.position = unifrnd(VarMin, VarMax, VarSize);
to
obj.position = unifrnd(obj.VarMin, obj.VarMax, obj.VarSize);
  1 件のコメント
Ali Ali
Ali Ali 2018 年 2 月 18 日
I appreciated this, thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by