chore: add comments to swarm.hpp

This commit is contained in:
2024-11-25 14:44:01 +01:00
parent 2b20e90a08
commit fe62617beb
2 changed files with 38 additions and 1 deletions

View File

@@ -8,13 +8,37 @@
#include <vec.hpp>
#include <vector>
/**
* An Agent in a Particle Swarm Optimisation simulation.
* #T is the type of the position vector the Agent uses.
*/
template<typename T>
struct Agent {
/**
* Type of the fitness function.
*/
using F = std::function<float(T)>;
/**
* Moves the Agent along its velocity by a factor of #dt.
*
* The parameter #dt is useful when drawing the Agent moving faster than it
* #tick()s.
*
* @see: step()
*/
virtual void move(float dt) = 0;
/**
* Steps the algorithm values one tick forward.
* Warning: Does not move the Agent. Use #move() for this
* @see: move()
*/
virtual void step() = 0;
/**
* The best position this Agent has seen during its lifetime.
*/
virtual std::pair<float, T> best() const = 0;
Agent(F f) : f(f) {}
@@ -24,6 +48,8 @@ protected:
template <typename A>
concept ParameterChangeAlgorithm = requires(A alg, float f, unsigned int i) {
// initial viscosity, initial nostalgia, initial peer pressure, current
// iteration, max iteration
{ alg(f, f, f, i, i) } -> std::same_as<vec<3>>;
};
@@ -62,13 +88,14 @@ namespace ParamChange {
};
};
template<std::size_t N, ParameterChangeAlgorithm A = ParamChange::MTanhAlg<>>
template<std::size_t N, ParameterChangeAlgorithm A = ParamChange::MLinAlg>
struct Particle : public Agent<vec<N>> {
std::pair<float, vec<N>> best() const override { return {pb, pb_pos}; };
void move(float dt=1) override { position = position + velocity * dt; }
void step() override {
// Get the new parameter values
vec<3> params = alg(kviscosity, knostalgia, kpeer_pressure,
curr_iter, max_iter);