Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PSO bound type #124

Merged
merged 2 commits into from Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions include/ensmallen_bits/pso/pso.hpp
Expand Up @@ -40,19 +40,17 @@ namespace ens {
* For more information, refer to:
*
* @inproceedings{Kennedy,
* doi = {10.1109/icnn.1995.488968},
* url = {https://doi.org/10.1109/icnn.1995.488968},
* publisher = {{IEEE}},
* author = {J. Kennedy and R. Eberhart},
* title = {Particle swarm optimization},
* booktitle = {Proceedings of {ICNN}{\textquotesingle}95 -
* International Conference on Neural Networks}
* author = {J. Kennedy and R. Eberhart},
* booktitle = {Proceedings of {ICNN}{\textquotesingle}95 -
* International Conference on Neural Networks},
* publisher = {{IEEE}}
* }
*
* PSO can optimize arbitrary functions. For more details, see the documentation
* on function types included with this distribution or on the ensmallen
* website.
*
*
* For PSO to work, the function being optimized must implement an
* ArbitraryFunctionType template parameter. The respective class must implement
* the following function:
Expand Down Expand Up @@ -159,16 +157,16 @@ class PSOType
size_t& NumParticles() { return numParticles; }

//! Retrieve value of lowerBound.
size_t LowerBound() const { return lowerBound; }
const arma::vec& LowerBound() const { return lowerBound; }

//! Modify value of lowerBound.
size_t& LowerBound() { return lowerBound; }
arma::vec& LowerBound() { return lowerBound; }

//! Retrieve value of upperBound.
size_t UpperBound() const { return upperBound; }
const arma::vec& UpperBound() const { return upperBound; }

//! Modify value of upperBound.
size_t& UpperBound() { return upperBound; }
arma::vec& UpperBound() { return upperBound; }

//! Retrieve value of maxIterations.
size_t MaxIterations() const { return maxIterations; }
Expand Down
10 changes: 5 additions & 5 deletions include/ensmallen_bits/pso/pso_impl.hpp
Expand Up @@ -20,7 +20,7 @@
namespace ens {
/* After the velocity of each particle is updated at the end of each iteration
* in PSO, the position of particle i (in iteration j) is updated as:
*
*
* \f[
* p_{i, j + 1} = p_{i, j} + v_{i, j}
* \f]
Expand All @@ -42,7 +42,7 @@ double PSOType<VelocityUpdatePolicy, InitPolicy>::Optimize(
*/
typedef Function<FunctionType> ArbitraryFunctionType;
ArbitraryFunctionType& f(static_cast<ArbitraryFunctionType&>(function));

// Make sure we have the methods that we need.
traits::CheckEvaluate<ArbitraryFunctionType>();

Expand Down Expand Up @@ -70,14 +70,14 @@ double PSOType<VelocityUpdatePolicy, InitPolicy>::Optimize(
particleFitnesses(i) = f.Evaluate(particlePositions.slice(i));
particleBestFitnesses(i) = particleFitnesses(i);
}

// Declare queue to keep track of improvements over a number of iterations.
queue <double> performanceHorizon;
// Variable to store the position of the best particle.
size_t bestParticle = 0;
// Find the best fitness.
double bestFitness = std::numeric_limits<double>::max();

// Run PSO for horizonSize number of iterations.
// This will allow the performanceHorizon to be updated.
// With some initial values in this, we may proceed with the remaining steps
Expand Down Expand Up @@ -128,7 +128,7 @@ double PSOType<VelocityUpdatePolicy, InitPolicy>::Optimize(
// If there is no significant improvement, terminate.
if (performanceHorizon.front() - performanceHorizon.back() < impTolerance)
break;

// Calculate fitness and evaluate personal best.
for (size_t j = 0; j < numParticles; j++)
{
Expand Down