Doxygen 1.17.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.3
Loading...
Searching...
No Matches
tsgParticleSwarm.hpp
1/*
2 * Copyright (c) 2022, Miroslav Stoyanov & Weiwei Kong
3 *
4 * This file is part of
5 * Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
13 * and the following disclaimer in the documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 *
26 * UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND
27 * IMPLIED. THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF
28 * THE SOFTWARE WILL NOT INFRINGE ANY PATENT, COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL
29 * ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE. THE USER ASSUMES
30 * RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING
31 * FROM OR ARISING OUT OF, IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.
32 */
33
34#ifndef __TASMANIAN_PARTICLE_SWARM_HPP
35#define __TASMANIAN_PARTICLE_SWARM_HPP
36
37#include "tsgOptimizationUtils.hpp"
38
49
50namespace TasOptimization {
51
56enum class NumParticles : int {};
57
63
127public:
131 ParticleSwarmState(const int num_dimensions, const int num_particles);
134 : ParticleSwarmState(static_cast<int>(dims), static_cast<int>(particles)) {}
135
137 ParticleSwarmState(const int num_dimensions, std::vector<double> &&pp, std::vector<double> &&pv);
139 ParticleSwarmState(const ParticleSwarmState &source) = default;
142
145
147 inline int getNumDimensions() const {return num_dimensions;}
149 inline int getNumParticles() const {return num_particles;}
151 inline void getParticlePositions(double pp[]) const {std::copy_n(particle_positions.begin(), num_particles * num_dimensions, pp);}
153 inline std::vector<double> getParticlePositions() const {return particle_positions;}
155 inline void getParticleVelocities(double pv[]) const {std::copy_n(particle_velocities.begin(), num_particles * num_dimensions, pv);}
157 inline std::vector<double> getParticleVelocities() const {return particle_velocities;}
164 inline void getBestParticlePositions(double bpp[]) const {std::copy_n(best_particle_positions.begin(), (num_particles + 1) * num_dimensions, bpp);}
166 inline std::vector<double> getBestParticlePositions() const {return best_particle_positions;}
168 inline void getBestPosition(double bp[]) const {std::copy_n(best_particle_positions.begin() + num_particles * num_dimensions, num_dimensions, bp);}
170 inline std::vector<double> getBestPosition() const {
171 return std::vector<double>(best_particle_positions.begin() + num_particles * num_dimensions,
172 best_particle_positions.begin() + num_particles * num_dimensions + num_dimensions);
173 }
174
176 inline bool isPositionInitialized() const {return positions_initialized;}
178 inline bool isVelocityInitialized() const {return velocities_initialized;}
180 inline bool isBestPositionInitialized() const {return best_positions_initialized;}
182 inline bool isCacheInitialized() const {return cache_initialized;}
184 inline std::vector<bool> getStateVector() const {
185 return {positions_initialized, velocities_initialized, best_positions_initialized, cache_initialized};
186 }
187
189 void setParticlePositions(const double pp[]) {
190 std::copy_n(pp, num_dimensions * num_particles, particle_positions.begin());
191 positions_initialized = true;
192 }
193
194 void setParticlePositions(const std::vector<double> &pp) {
195 checkVarSize("ParticleSwarmState::setParticlePositions", "particle position", pp.size(), num_dimensions * num_particles);
196 particle_positions = pp;
197 positions_initialized = true;
198 }
199
200 void setParticlePositions(std::vector<double> &&pp) {
201 checkVarSize("ParticleSwarmState::setParticlePositions", "particle positions", pp.size(), num_dimensions * num_particles);
202 particle_positions = std::move(pp);
203 positions_initialized = true;
204 }
205
206 void setParticleVelocities(const double pv[]) {
207 std::copy_n(pv, num_dimensions * num_particles, particle_velocities.begin());
208 velocities_initialized = true;
209 }
210
211 void setParticleVelocities(const std::vector<double> &pv) {
212 checkVarSize("ParticleSwarmState::setParticleVelocities", "particle velocities", pv.size(), num_dimensions * num_particles);
213 particle_velocities = pv;
214 velocities_initialized = true;
215 }
216
217 void setParticleVelocities(std::vector<double> &&pv) {
218 checkVarSize("ParticleSwarmState::setParticleVelocities", "particle velocities", pv.size(), num_dimensions * num_particles);
219 particle_velocities = std::move(pv);
220 velocities_initialized = true;
221 }
222
223 void setBestParticlePositions(const double bpp[]) {
224 std::copy_n(bpp, num_dimensions * (num_particles + 1), best_particle_positions.begin());
225 best_positions_initialized = true;
226 }
227
228 void setBestParticlePositions(const std::vector<double> &bpp) {
229 checkVarSize("ParticleSwarmState::setBestParticlePositions", "best particle positions", bpp.size(), num_dimensions * (num_particles + 1));
230 best_particle_positions = bpp;
231 best_positions_initialized = true;
232 }
233
234 void setBestParticlePositions(std::vector<double> &&bpp) {
235 checkVarSize("ParticleSwarmState::setBestParticlePositions", "best particle positions", bpp.size(), num_dimensions * (num_particles + 1));
236 best_particle_positions = std::move(bpp);
237 best_positions_initialized = true;
238 }
239
242 best_positions_initialized = false;
243 std::fill(best_particle_positions.begin(), best_particle_positions.end(), 0.0);
244 }
245
246 void clearCache() {
247 cache_initialized = false;
248 std::fill(cache_particle_fvals.begin(), cache_particle_fvals.end(), 0.0);
249 std::fill(cache_particle_inside.begin(), cache_particle_inside.end(), false);
250 std::fill(cache_best_particle_fvals.begin(), cache_best_particle_fvals.end(), 0.0);
251 std::fill(cache_best_particle_inside.begin(), cache_best_particle_inside.end(), false);
252 }
253
261 void initializeParticlesInsideBox(const double box_lower[], const double box_upper[],
262 const std::function<double(void)> get_random01 = TasDREAM::tsgCoreUniform01);
264 void initializeParticlesInsideBox(const std::vector<double> &box_lower, const std::vector<double> &box_upper,
265 const std::function<double(void)> get_random01 = TasDREAM::tsgCoreUniform01);
266
267 friend void ParticleSwarm(const ObjectiveFunction f, const TasDREAM::DreamDomain inside, const double inertia_weight,
268 const double cognitive_coeff, const double social_coeff, const int num_iterations,
269 ParticleSwarmState &state, const std::function<double(void)> get_random01);
270
271private:
272 bool positions_initialized, velocities_initialized, best_positions_initialized, cache_initialized;
273 int num_dimensions, num_particles;
274 std::vector<double> particle_positions, particle_velocities, best_particle_positions, cache_particle_fvals, cache_best_particle_fvals;
275 std::vector<bool> cache_particle_inside, cache_best_particle_inside;
276};
277
278// Forward declarations.
279
299void ParticleSwarm(const ObjectiveFunction f, const TasDREAM::DreamDomain inside, const double inertia_weight,
300 const double cognitive_coeff, const double social_coeff, const int num_iterations,
301 ParticleSwarmState &state, const std::function<double(void)> get_random01 = TasDREAM::tsgCoreUniform01);
302
303}
304#endif
Stores the information about a particle swarm.
Definition tsgParticleSwarm.hpp:126
bool isCacheInitialized() const
Returns true if the cache has been initialized.
Definition tsgParticleSwarm.hpp:182
void getBestParticlePositions(double bpp[]) const
Return the best known particle positions.
Definition tsgParticleSwarm.hpp:164
int getNumParticles() const
Return the number of particles.
Definition tsgParticleSwarm.hpp:149
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(NumDimensions dims, NumParticles particles)
Constructor for a particle swarm state with the number of particles and dimensions.
Definition tsgParticleSwarm.hpp:133
ParticleSwarmState(const ParticleSwarmState &source)=default
Copy constructor.
void setParticlePositions(const double pp[])
Set the particle positions, raw-array variant.
Definition tsgParticleSwarm.hpp:189
std::vector< double > getBestPosition() const
Returns the best knows position in the swarm.
Definition tsgParticleSwarm.hpp:170
std::vector< double > getParticleVelocities() const
Return the particle velocities, vector overload.
Definition tsgParticleSwarm.hpp:157
void setParticleVelocities(std::vector< double > &&pv)
Sets the best position, with a move.
Definition tsgParticleSwarm.hpp:217
void getBestPosition(double bp[]) const
Loads the best known position in the swarm.
Definition tsgParticleSwarm.hpp:168
int getNumDimensions() const
Return the number of dimensions.
Definition tsgParticleSwarm.hpp:147
void clearBestParticles()
Clear the previously best known particle velocities.
Definition tsgParticleSwarm.hpp:241
void clearCache()
Clear the particle swarm cache.
Definition tsgParticleSwarm.hpp:246
bool isVelocityInitialized() const
Returns true if the particle velocities have been initialized.
Definition tsgParticleSwarm.hpp:178
void setParticlePositions(std::vector< double > &&pp)
Set the particle positions, with a move.
Definition tsgParticleSwarm.hpp:200
bool isBestPositionInitialized() const
Returns true if the best particle positions have been initialized.
Definition tsgParticleSwarm.hpp:180
ParticleSwarmState & operator=(ParticleSwarmState &&source)=default
Move assignment.
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.
void setParticleVelocities(const std::vector< double > &pv)
Sets the best position.
Definition tsgParticleSwarm.hpp:211
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:228
void setBestParticlePositions(std::vector< double > &&bpp)
Sets the best position per particle, allows for a move.
Definition tsgParticleSwarm.hpp:234
void setBestParticlePositions(const double bpp[])
Set the previously best known particle velocities.
Definition tsgParticleSwarm.hpp:223
std::vector< double > getBestParticlePositions() const
Return the best known particle positions, vector overload.
Definition tsgParticleSwarm.hpp:166
std::vector< bool > getStateVector() const
Return the complete state vector.
Definition tsgParticleSwarm.hpp:184
std::vector< double > getParticlePositions() const
Return the particle positions, vector overload.
Definition tsgParticleSwarm.hpp:153
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:206
void getParticlePositions(double pp[]) const
Return the particle positions.
Definition tsgParticleSwarm.hpp:151
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:155
bool isPositionInitialized() const
Returns true if the particle positions have been initialized.
Definition tsgParticleSwarm.hpp:176
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:194
std::function< bool(std::vector< double > const &x)> DreamDomain
Generic test function whether a sample belongs in the domain.
Definition tsgDreamSample.hpp:69
NumParticles
Strong type for number of chains in TasDREAM::TasmanianDREAM.
Definition tsgParticleSwarm.hpp:56
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.
std::function< void(const std::vector< double > &x_batch, std::vector< double > &fval_batch)> ObjectiveFunction
Generic batched objective function signature.
Definition tsgOptimizationUtils.hpp:124
NumDimensions
Strong type for the number of dimensions.
Definition TasmanianSparseGrid.hpp:2450
double tsgCoreUniform01()
Generates random numbers uniformly distributed in (0, 1), uses the rand() command.
Definition tsgDreamCoreRandom.hpp:61
Encapsulates the Tasmanian Optimization module.
Definition TasmanianOptimization.hpp:86