feat: add tanh transition curve particle parameter change algorithm
This commit is contained in:
parent
cf19cc7183
commit
2b20e90a08
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
|
||||
#include <functional>
|
||||
@ -45,15 +46,38 @@ namespace ParamChange {
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
template<auto steepness = 3>
|
||||
struct MTanhAlg {
|
||||
vec<3> operator()(
|
||||
float visc, float nostal, float peerp, int curr, int max) {
|
||||
float pct_done = (float)curr / (float)max;
|
||||
float scale_factor = (std::tanh(steepness * (pct_done - 0.5)) + 1) / 2;
|
||||
return {
|
||||
visc - (0.9f - 0.4f) * scale_factor,
|
||||
nostal - (2.5f - 0.5f) * scale_factor,
|
||||
peerp + (2.5f - 0.5f) * scale_factor,
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template<std::size_t N, ParameterChangeAlgorithm A = ParamChange::MLinAlg>
|
||||
template<std::size_t N, ParameterChangeAlgorithm A = ParamChange::MTanhAlg<>>
|
||||
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 {
|
||||
vec<3> params = alg(kviscosity, knostalgia, kpeer_pressure,
|
||||
curr_iter, max_iter);
|
||||
|
||||
viscosity = params.x;
|
||||
nostalgia = params.y;
|
||||
peer_pressure = params.z;
|
||||
|
||||
curr_iter++;
|
||||
|
||||
velocity = viscosity * velocity
|
||||
+ nostalgia * (pb_pos - position)
|
||||
+ peer_pressure * (peer.best().second - position);
|
||||
@ -63,15 +87,6 @@ struct Particle : public Agent<vec<N>> {
|
||||
pb = y;
|
||||
pb_pos = position;
|
||||
}
|
||||
|
||||
vec<3> params = alg(kviscosity, knostalgia, kpeer_pressure,
|
||||
curr_iter, max_iter);
|
||||
|
||||
viscosity = params.x;
|
||||
nostalgia = params.y;
|
||||
peer_pressure = params.z;
|
||||
|
||||
curr_iter++;
|
||||
};
|
||||
|
||||
const float kviscosity = 0.9f;
|
||||
@ -146,7 +161,7 @@ struct Swarm : public Agent<vec<N>> {
|
||||
|
||||
const std::vector<Particle<N>>& get_particles() { return particles; };
|
||||
|
||||
Swarm(Agent<vec<N>>::F f, unsigned max_iter = 150)
|
||||
Swarm(Agent<vec<N>>::F f, unsigned max_iter)
|
||||
: Agent<vec<N>>(f), max_iter(max_iter) {}
|
||||
private:
|
||||
unsigned max_iter;
|
||||
|
Loading…
Reference in New Issue
Block a user