I’m working on implementing a Particle Swarm Optimization routine in Julia and would like to define a “Particle” mutable struct with MVector fields (from StaticArrays.jl) but am running into some issues.
Here is my current struct:
mutable struct Particle{M,T} where {M <: Integer, T <: AbstractFloat}
x::MVector{M,T} # Current Position
v::MVector{M,T} # Current Velocity
p::MVector{M,T} # Best Position
fx::T # Current Objective Function Value
fp::T # Best Objective Function Value
function Particle(x::MVector{M,T}, v::MVector{M,T}) where {M <: Integer, T <: AbstractFloat}
xl = length(x)
vl = length(v)
if xl != vl
throw(ArgumentError("Particle's position and velocity must be the same length."))
else
new{M,T}(x, v, NaN*similar(x), NaN*x[1], NaN*x[1])
end
end
end
Using my defined constructor, I get the following error:
julia> a = MVector{3}(rand(3))
3-element MArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
0.6821892643778722
0.41842430858799906
julia> b = MVector{3}(rand(3))
3-element MArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
0.9403860675524811
0.163642437664691
0.1349743741370517
julia> Particle(a,b)
ERROR: MethodError: no method matching Particle(::MArray{Tuple{3},Float64,1,3}, ::MArray{Tuple{3},Float64,1,3})
Stacktrace:
[1] top-level scope at REPL[3]:1
I’ve primarily used MATLAB for all of my work and am relatively new to Julia and this error has me stumped. What confuses me most is, upon running the following command:
julia> methods(Particle)
# 1 method for type constructor:
[1] (::Type{Particle})(x::MArray{Tuple{M},T,1,M}, v::MArray{Tuple{M},T,1,M}) where {M<:Integer, T<:AbstractFloat} in Main at c:\Users\grant\.julia\dev\JuSwarm\src\particle.jl:36
it appears (to the best of my knowledge) that the correct method for constructing my Particle type does exist… Any and all help on this issue would be greatly appreciated!