Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/bermejus/gopt
Browse files Browse the repository at this point in the history
  • Loading branch information
david-bermejo committed Apr 7, 2021
2 parents de8900c + a0aa9a5 commit 19341e3
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions math/particleswarm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace gopt
};

template <typename F, typename V, typename T = typename V::type>
V particleswarm(F&& f, const V& lb, const V& ub, const unsigned int n_particles, const unsigned int max_iter = 1000, const PSOptions<T>& opts = {}, const std::vector<V>& x0 = std::vector<V>(0))
V particleswarm(F&& f, const V& lb, const V& ub, const unsigned int n_particles, const unsigned int max_iter = 1000, const PSOptions<T>& opts = {})
{
static constexpr bool is_dynamic = std::is_same_v<std::remove_cvref_t<V>, Vector<T>>;
const unsigned int size = lb.size();
Expand Down Expand Up @@ -65,22 +65,16 @@ namespace gopt
{
for (auto& p : particles)
{
if (!x0.size())
p.position = V(size);

p.position = V(size);
p.velocity = V(size);
p.best_position = V(size);
}
}

for (int i = 0; auto& p : particles)
{
if (x0.size())
p.position = x0[i];
else {
for (int j = 0; j < size; j++)
p.position[j] = lb[j] + (ub[j] - lb[j]) * dist(gen);
}
for (int j = 0; j < size; j++)
p.position[j] = lb[j] + (ub[j] - lb[j]) * dist(gen);

if constexpr (is_dynamic) {
p.velocity.fill(0.0);
Expand Down

0 comments on commit 19341e3

Please sign in to comment.