34 #ifndef __TASMANIAN_PARTICLE_SWARM_HPP
35 #define __TASMANIAN_PARTICLE_SWARM_HPP
136 inline void getParticlePositions(
double pp[])
const {std::copy_n(particle_positions.begin(), num_particles * num_dimensions, pp);}
140 inline void getParticleVelocities(
double pv[])
const {std::copy_n(particle_velocities.begin(), num_particles * num_dimensions, pv);}
149 inline void getBestParticlePositions(
double bpp[])
const {std::copy_n(best_particle_positions.begin(), (num_particles + 1) * num_dimensions, bpp);}
153 inline void getBestPosition(
double bp[])
const {std::copy_n(best_particle_positions.begin() + num_particles * num_dimensions, num_dimensions, bp);}
156 return std::vector<double>(best_particle_positions.begin() + num_particles * num_dimensions,
157 best_particle_positions.begin() + num_particles * num_dimensions + num_dimensions);
170 return {positions_initialized, velocities_initialized, best_positions_initialized, cache_initialized};
175 std::copy_n(pp, num_dimensions * num_particles, particle_positions.begin());
176 positions_initialized =
true;
180 checkVarSize(
"ParticleSwarmState::setParticlePositions",
"particle position", pp.size(), num_dimensions * num_particles);
181 particle_positions = pp;
182 positions_initialized =
true;
186 checkVarSize(
"ParticleSwarmState::setParticlePositions",
"particle positions", pp.size(), num_dimensions * num_particles);
187 particle_positions = std::move(pp);
188 positions_initialized =
true;
192 std::copy_n(pv, num_dimensions * num_particles, particle_velocities.begin());
193 velocities_initialized =
true;
197 checkVarSize(
"ParticleSwarmState::setParticleVelocities",
"particle velocities", pv.size(), num_dimensions * num_particles);
198 particle_velocities = pv;
199 velocities_initialized =
true;
203 checkVarSize(
"ParticleSwarmState::setParticleVelocities",
"particle velocities", pv.size(), num_dimensions * num_particles);
204 particle_velocities = std::move(pv);
205 velocities_initialized =
true;
209 std::copy_n(bpp, num_dimensions * (num_particles + 1), best_particle_positions.begin());
210 best_positions_initialized =
true;
214 checkVarSize(
"ParticleSwarmState::setBestParticlePositions",
"best particle positions", bpp.size(), num_dimensions * (num_particles + 1));
215 best_particle_positions = bpp;
216 best_positions_initialized =
true;
220 checkVarSize(
"ParticleSwarmState::setBestParticlePositions",
"best particle positions", bpp.size(), num_dimensions * (num_particles + 1));
221 best_particle_positions = std::move(bpp);
222 best_positions_initialized =
true;
227 best_positions_initialized =
false;
228 std::fill(best_particle_positions.begin(), best_particle_positions.end(), 0.0);
232 cache_initialized =
false;
233 std::fill(cache_particle_fvals.begin(), cache_particle_fvals.end(), 0.0);
234 std::fill(cache_particle_inside.begin(), cache_particle_inside.end(),
false);
235 std::fill(cache_best_particle_fvals.begin(), cache_best_particle_fvals.end(), 0.0);
236 std::fill(cache_best_particle_inside.begin(), cache_best_particle_inside.end(),
false);
253 const double cognitive_coeff,
const double social_coeff,
const int num_iterations,
257 bool positions_initialized, velocities_initialized, best_positions_initialized, cache_initialized;
258 int num_dimensions, num_particles;
259 std::vector<double> particle_positions, particle_velocities, best_particle_positions, cache_particle_fvals, cache_best_particle_fvals;
260 std::vector<bool> cache_particle_inside, cache_best_particle_inside;
285 const double cognitive_coeff,
const double social_coeff,
const int num_iterations,
Stores the information about a particle swarm.
Definition: tsgParticleSwarm.hpp:114
bool isCacheInitialized() const
Returns true if the cache has been initialized.
Definition: tsgParticleSwarm.hpp:167
void getBestParticlePositions(double bpp[]) const
Return the best known particle positions.
Definition: tsgParticleSwarm.hpp:149
int getNumParticles() const
Return the number of particles.
Definition: tsgParticleSwarm.hpp:134
void initializeParticlesInsideBox(const double box_lower[], const double box_upper[], const std::function< double(void)> get_random01=TasDREAM::tsgCoreUniform01)
Randomly initializes all of the particle positions and velocities inside of a box.
ParticleSwarmState(ParticleSwarmState &&source)=default
Move constructor.
ParticleSwarmState(const ParticleSwarmState &source)=default
Copy constructor.
void setParticlePositions(const double pp[])
Set the particle positions, raw-array variant.
Definition: tsgParticleSwarm.hpp:174
void setParticleVelocities(std::vector< double > &&pv)
Sets the best position, with a move.
Definition: tsgParticleSwarm.hpp:202
void getBestPosition(double bp[]) const
Loads the best known position in the swarm.
Definition: tsgParticleSwarm.hpp:153
int getNumDimensions() const
Return the number of dimensions.
Definition: tsgParticleSwarm.hpp:132
void clearBestParticles()
Clear the previously best known particle velocities.
Definition: tsgParticleSwarm.hpp:226
void clearCache()
Clear the particle swarm cache.
Definition: tsgParticleSwarm.hpp:231
bool isVelocityInitialized() const
Returns true if the particle velocities have been initialized.
Definition: tsgParticleSwarm.hpp:163
void setParticlePositions(std::vector< double > &&pp)
Set the particle positions, with a move.
Definition: tsgParticleSwarm.hpp:185
bool isBestPositionInitialized() const
Returns true if the best particle positions have been initialized.
Definition: tsgParticleSwarm.hpp:165
void setParticleVelocities(const std::vector< double > &pv)
Sets the best position.
Definition: tsgParticleSwarm.hpp:196
std::vector< double > getBestParticlePositions() const
Return the best known particle positions, vector overload.
Definition: tsgParticleSwarm.hpp:151
ParticleSwarmState(const int num_dimensions, const int num_particles)
Constructor for a particle swarm state with the number of particles and dimensions.
void setBestParticlePositions(const std::vector< double > &bpp)
Sets the best position per particle.
Definition: tsgParticleSwarm.hpp:213
void setBestParticlePositions(std::vector< double > &&bpp)
Sets the best position per particle, allows for a move.
Definition: tsgParticleSwarm.hpp:219
std::vector< bool > getStateVector() const
Return the complete state vector.
Definition: tsgParticleSwarm.hpp:169
std::vector< double > getBestPosition() const
Returns the best knows position in the swarm.
Definition: tsgParticleSwarm.hpp:155
void setBestParticlePositions(const double bpp[])
Set the previously best known particle velocities.
Definition: tsgParticleSwarm.hpp:208
friend void ParticleSwarm(const ObjectiveFunction f, const TasDREAM::DreamDomain inside, const double inertia_weight, const double cognitive_coeff, const double social_coeff, const int num_iterations, ParticleSwarmState &state, const std::function< double(void)> get_random01)
Applies the classic particle swarm algorithm to a particle swarm state.
ParticleSwarmState & operator=(ParticleSwarmState &&source)=default
Move assignment.
std::vector< double > getParticlePositions() const
Return the particle positions, vector overload.
Definition: tsgParticleSwarm.hpp:138
void initializeParticlesInsideBox(const std::vector< double > &box_lower, const std::vector< double > &box_upper, const std::function< double(void)> get_random01=TasDREAM::tsgCoreUniform01)
Randomly initializes all of the particles, vector API overload.
void setParticleVelocities(const double pv[])
Set the particle velocities.
Definition: tsgParticleSwarm.hpp:191
void getParticlePositions(double pp[]) const
Return the particle positions.
Definition: tsgParticleSwarm.hpp:136
ParticleSwarmState(const int num_dimensions, std::vector< double > &&pp, std::vector< double > &&pv)
Constructor for a particle swarm state with the number of dimensions and the number of particles infe...
void getParticleVelocities(double pv[]) const
Return the particle velocities.
Definition: tsgParticleSwarm.hpp:140
bool isPositionInitialized() const
Returns true if the particle positions have been initialized.
Definition: tsgParticleSwarm.hpp:161
std::vector< double > getParticleVelocities() const
Return the particle velocities, vector overload.
Definition: tsgParticleSwarm.hpp:142
ParticleSwarmState()=delete
The particle swarm state must be initialized with data that defines number of particles and dimension...
void setParticlePositions(const std::vector< double > &pp)
Set the particle positions, vector variant.
Definition: tsgParticleSwarm.hpp:179
double tsgCoreUniform01()
Default random sampler, using rand() divided by RAND_MAX.
Definition: tsgDreamCoreRandom.hpp:61
std::function< bool(std::vector< double > const &x)> DreamDomain
Generic test function whether a sample belongs in the domain.
Definition: tsgDreamSample.hpp:69
void ParticleSwarm(const ObjectiveFunction f, const TasDREAM::DreamDomain inside, const double inertia_weight, const double cognitive_coeff, const double social_coeff, const int num_iterations, ParticleSwarmState &state, const std::function< double(void)> get_random01=TasDREAM::tsgCoreUniform01)
Applies the classic particle swarm algorithm to a particle swarm state.
void checkVarSize(const std::string method_name, const std::string var_name, const int var_size, const int exp_size)
Definition: tsgOptimizationUtils.hpp:80
std::function< void(const std::vector< double > &x_batch, std::vector< double > &fval_batch)> ObjectiveFunction
Generic batched objective function signature.
Definition: tsgOptimizationUtils.hpp:124
Encapsulates the Tasmanian Optimization module.
Definition: TasmanianOptimization.hpp:86
Utility functions and aliases in the optimization module.