Doxygen 1.9.8
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
 
Loading...
Searching...
No Matches
tsgParticleSwarm.hpp
Go to the documentation of this file.
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
38
50namespace TasOptimization {
51
115public:
119 ParticleSwarmState(const int num_dimensions, const int num_particles);
122 ParticleSwarmState(const int num_dimensions, std::vector<double> &&pp, std::vector<double> &&pv);
124 ParticleSwarmState(const ParticleSwarmState &source) = default;
127
130
132 inline int getNumDimensions() const {return num_dimensions;}
134 inline int getNumParticles() const {return num_particles;}
136 inline void getParticlePositions(double pp[]) const {std::copy_n(particle_positions.begin(), num_particles * num_dimensions, pp);}
138 inline std::vector<double> getParticlePositions() const {return particle_positions;}
140 inline void getParticleVelocities(double pv[]) const {std::copy_n(particle_velocities.begin(), num_particles * num_dimensions, pv);}
142 inline std::vector<double> getParticleVelocities() const {return particle_velocities;}
149 inline void getBestParticlePositions(double bpp[]) const {std::copy_n(best_particle_positions.begin(), (num_particles + 1) * num_dimensions, bpp);}
151 inline std::vector<double> getBestParticlePositions() const {return best_particle_positions;}
153 inline void getBestPosition(double bp[]) const {std::copy_n(best_particle_positions.begin() + num_particles * num_dimensions, num_dimensions, bp);}
155 inline std::vector<double> getBestPosition() const {
156 return std::vector<double>(best_particle_positions.begin() + num_particles * num_dimensions,
157 best_particle_positions.begin() + num_particles * num_dimensions + num_dimensions);
158 }
159
161 inline bool isPositionInitialized() const {return positions_initialized;}
163 inline bool isVelocityInitialized() const {return velocities_initialized;}
165 inline bool isBestPositionInitialized() const {return best_positions_initialized;}
167 inline bool isCacheInitialized() const {return cache_initialized;}
169 inline std::vector<bool> getStateVector() const {
170 return {positions_initialized, velocities_initialized, best_positions_initialized, cache_initialized};
171 }
172
174 void setParticlePositions(const double pp[]) {
175 std::copy_n(pp, num_dimensions * num_particles, particle_positions.begin());
176 positions_initialized = true;
177 }
179 void setParticlePositions(const std::vector<double> &pp) {
180 checkVarSize("ParticleSwarmState::setParticlePositions", "particle position", pp.size(), num_dimensions * num_particles);
181 particle_positions = pp;
182 positions_initialized = true;
183 }
185 void setParticlePositions(std::vector<double> &&pp) {
186 checkVarSize("ParticleSwarmState::setParticlePositions", "particle positions", pp.size(), num_dimensions * num_particles);
187 particle_positions = std::move(pp);
188 positions_initialized = true;
189 }
191 void setParticleVelocities(const double pv[]) {
192 std::copy_n(pv, num_dimensions * num_particles, particle_velocities.begin());
193 velocities_initialized = true;
194 }
196 void setParticleVelocities(const std::vector<double> &pv) {
197 checkVarSize("ParticleSwarmState::setParticleVelocities", "particle velocities", pv.size(), num_dimensions * num_particles);
198 particle_velocities = pv;
199 velocities_initialized = true;
200 }
202 void setParticleVelocities(std::vector<double> &&pv) {
203 checkVarSize("ParticleSwarmState::setParticleVelocities", "particle velocities", pv.size(), num_dimensions * num_particles);
204 particle_velocities = std::move(pv);
205 velocities_initialized = true;
206 }
208 void setBestParticlePositions(const double bpp[]) {
209 std::copy_n(bpp, num_dimensions * (num_particles + 1), best_particle_positions.begin());
210 best_positions_initialized = true;
211 }
213 void setBestParticlePositions(const std::vector<double> &bpp) {
214 checkVarSize("ParticleSwarmState::setBestParticlePositions", "best particle positions", bpp.size(), num_dimensions * (num_particles + 1));
215 best_particle_positions = bpp;
216 best_positions_initialized = true;
217 }
219 void setBestParticlePositions(std::vector<double> &&bpp) {
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;
223 }
224
227 best_positions_initialized = false;
228 std::fill(best_particle_positions.begin(), best_particle_positions.end(), 0.0);
229 }
231 void clearCache() {
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);
237 }
238
246 void initializeParticlesInsideBox(const double box_lower[], const double box_upper[],
247 const std::function<double(void)> get_random01 = TasDREAM::tsgCoreUniform01);
249 void initializeParticlesInsideBox(const std::vector<double> &box_lower, const std::vector<double> &box_upper,
250 const std::function<double(void)> get_random01 = TasDREAM::tsgCoreUniform01);
251
252 friend void ParticleSwarm(const ObjectiveFunction f, const TasDREAM::DreamDomain inside, const double inertia_weight,
253 const double cognitive_coeff, const double social_coeff, const int num_iterations,
254 ParticleSwarmState &state, const std::function<double(void)> get_random01);
255
256private:
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;
261};
262
263// Forward declarations.
264
284void ParticleSwarm(const ObjectiveFunction f, const TasDREAM::DreamDomain inside, const double inertia_weight,
285 const double cognitive_coeff, const double social_coeff, const int num_iterations,
286 ParticleSwarmState &state, const std::function<double(void)> get_random01 = TasDREAM::tsgCoreUniform01);
287
288}
289#endif
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
std::vector< double > getBestPosition() const
Returns the best knows position in the swarm.
Definition tsgParticleSwarm.hpp:155
std::vector< double > getParticleVelocities() const
Return the particle velocities, vector overload.
Definition tsgParticleSwarm.hpp:142
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
ParticleSwarmState & operator=(ParticleSwarmState &&source)=default
Move assignment.
void setParticleVelocities(const std::vector< double > &pv)
Sets the best position.
Definition tsgParticleSwarm.hpp:196
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
void setBestParticlePositions(const double bpp[])
Set the previously best known particle velocities.
Definition tsgParticleSwarm.hpp:208
std::vector< double > getBestParticlePositions() const
Return the best known particle positions, vector overload.
Definition tsgParticleSwarm.hpp:151
std::vector< bool > getStateVector() const
Return the complete state vector.
Definition tsgParticleSwarm.hpp:169
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.
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
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.