[mlpack-git] master,mlpack-1.0.x: Minor cleanups; no functionality changes. Make some things const where possible. (ae88e2a)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:51:01 EST 2015


Repository : https://github.com/mlpack/mlpack

On branches: master,mlpack-1.0.x
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit ae88e2a42b7e64ac58c2939ca0f5846492d40f46
Author: Ryan Curtin <ryan at ratml.org>
Date:   Thu Jul 3 14:10:14 2014 +0000

    Minor cleanups; no functionality changes.  Make some things const where
    possible.


>---------------------------------------------------------------

ae88e2a42b7e64ac58c2939ca0f5846492d40f46
 .../core/optimizers/sa/exponential_schedule.hpp    | 52 +++++++++++++---------
 src/mlpack/core/optimizers/sa/sa.hpp               | 21 +++++----
 src/mlpack/core/optimizers/sa/sa_impl.hpp          | 25 +++++------
 3 files changed, 54 insertions(+), 44 deletions(-)

diff --git a/src/mlpack/core/optimizers/sa/exponential_schedule.hpp b/src/mlpack/core/optimizers/sa/exponential_schedule.hpp
index c3d6019..8c4cfbd 100644
--- a/src/mlpack/core/optimizers/sa/exponential_schedule.hpp
+++ b/src/mlpack/core/optimizers/sa/exponential_schedule.hpp
@@ -1,49 +1,61 @@
-/*
+/**
  * @file exponential_schedule.hpp
  * @author Zhihao Lou
  *
- * Exponential (geometric) cooling schedule used in SA
+ * Exponential (geometric) cooling schedule used in SA.
  */
-
 #ifndef __MLPACK_CORE_OPTIMIZERS_SA_EXPONENTIAL_SCHEDULE_HPP
 #define __MLPACK_CORE_OPTIMIZERS_SA_EXPONENTIAL_SCHEDULE_HPP
 
 namespace mlpack {
 namespace optimization {
 
-/* 
+/**
  * The exponential cooling schedule cools the temperature T at every step
+ * according to the equation
+ *
  * \f[
- * T_{n+1}=(1-\lambda)T_{n}
+ * T_{n+1} = (1-\lambda) T_{n}
  * \f]
+ *
  * where \f$ 0<\lambda<1 \f$ is the cooling speed. The smaller \f$ \lambda \f$
  * is, the slower the cooling speed, and better the final result will be. Some
- * literature uses \f$ \alpha=(-1\lambda) \f$ instead. In practice, \f$ \alpha \f$
- * is very close to 1 and will be awkward to input (e.g. alpha=0.999999 vs
- * lambda=1e-6).
+ * literature uses \f$ \alpha = (-1 \lambda) \f$ instead. In practice,
+ * \f$ \alpha \f$ is very close to 1 and will be awkward to input (e.g.
+ * alpha = 0.999999 vs lambda = 1e-6).
  */
 class ExponentialSchedule
 {
  public:
   /*
-   * Construct the ExponentialSchedule with the given parameter
+   * Construct the ExponentialSchedule with the given parameter.
+   *
+   * @param lambda Cooling speed.
+   */
+  ExponentialSchedule(const double lambda = 0.001) : lambda(lambda) { }
+
+  /**
+   * Returns the next temperature given current status.  The current system's
+   * energy is not used in this calculation.
    *
-   * @param lambda Cooling speed
+   * @param currentTemperature Current temperature of system.
+   * @param currentEnergy Current energy of system (not used).
    */
-  ExponentialSchedule(const double lambda = 0.001) : lambda(lambda){};
+  double NextTemperature(
+      const double currentTemperature,
+      const double /* currentEnergy */)
+  {
+    return (1 - lambda) * currentTemperature;
+  }
 
-  //! returns the next temperature given current status
-  double nextTemperature(const double currentTemperate, const double)
-  {return (1-lambda) * currentTemperate;}
+  //! Get the cooling speed, lambda.
+  double Lambda() const { return lambda; }
+  //! Modify the cooling speed, lambda.
+  double& Lambda() { return lambda; }
 
-  //! Get the cooling speed lambda
-  double Lambda() const {return lambda;}
-  //! Modify the cooling speed lambda
-  double& Lambda() {return lambda;}
  private:
+  //! The cooling speed.
   double lambda;
-
-
 };
 
 }; // namespace optimization
diff --git a/src/mlpack/core/optimizers/sa/sa.hpp b/src/mlpack/core/optimizers/sa/sa.hpp
index c2a6422..e21167d 100644
--- a/src/mlpack/core/optimizers/sa/sa.hpp
+++ b/src/mlpack/core/optimizers/sa/sa.hpp
@@ -19,18 +19,18 @@ namespace optimization {
  * schedule can be changed via a template parameter.
  *
  * The algorithm keeps the temperature at initial temperature for initMove
- * steps to get rid of the dependency of initial condition. After that, it
+ * steps to get rid of the dependency on the initial condition. After that, it
  * cools every step until the system is considered frozen or maxIterations is
  * reached.
  *
- * At each step, SA only perturbs one parameter at a time. The process that SA
- * perturbed all parameters in a problem is called a sweep. Every moveCtrlSweep
- * the algorithm does feedback move control to change the average move size
- * depending on the responsiveness of each parameter. Parameter gain controls
- * the proportion of the feedback control.
+ * At each step, SA only perturbs one parameter at a time. When SA has perturbed
+ * all parameters in a problem, a sweep has been completed. Every moveCtrlSweep
+ * sweeps, the algorithm does feedback move control to change the average move
+ * size depending on the responsiveness of each parameter. Parameter gain
+ * controls the proportion of the feedback control.
  *
- * The system is considered "frozen" when its score failed to change more then
- * tolerance for consecutive maxToleranceSweep sweeps.
+ * The system is considered "frozen" when its score fails to change more then
+ * tolerance for maxToleranceSweep consecutive sweeps.
  *
  * For SA to work, the FunctionType parameter must implement the following
  * two methods:
@@ -53,7 +53,7 @@ template<typename FunctionType, typename CoolingScheduleType>
 class SA
 {
  public:
-  /*
+  /**
    * Construct the SA optimizer with the given function and parameters.
    *
    * @param function Function to be minimized.
@@ -81,8 +81,7 @@ class SA
      const double initMoveCoef = 0.3,
      const double gain = 0.3);
 
-
-  /*&
+  /**
    * Optimize the given function using simulated annealing. The given starting
    * point will be modified to store the finishing point of the algorithm, and
    * the final objective value is returned.
diff --git a/src/mlpack/core/optimizers/sa/sa_impl.hpp b/src/mlpack/core/optimizers/sa/sa_impl.hpp
index 7841c1b..fdab618 100644
--- a/src/mlpack/core/optimizers/sa/sa_impl.hpp
+++ b/src/mlpack/core/optimizers/sa/sa_impl.hpp
@@ -1,4 +1,4 @@
-/*
+/**
  * @file sa_impl.hpp
  * @auther Zhihao Lou
  *
@@ -57,7 +57,6 @@ double SA<FunctionType, CoolingScheduleType>::Optimize(arma::mat &iterate)
   const size_t rows = function.GetInitialPoint().n_rows;
   const size_t cols = function.GetInitialPoint().n_cols;
 
-  size_t i;
   size_t frozenCount = 0;
   double energy = function.Evaluate(iterate);
   size_t oldEnergy = energy;
@@ -69,16 +68,16 @@ double SA<FunctionType, CoolingScheduleType>::Optimize(arma::mat &iterate)
   arma::mat accept(rows, cols);
   accept.zeros();
 
-  // Initial Moves to get rid of dependency of initial states.
-  for (i = 0; i < initMoves; ++i)
+  // Initial moves to get rid of dependency of initial states.
+  for (size_t i = 0; i < initMoves; ++i)
     GenerateMove(iterate, accept, energy, idx, sweepCounter);
 
   // Iterating and cooling.
-  for (i = 0; i != maxIterations; ++i)
+  for (size_t i = 0; i != maxIterations; ++i)
   {
     oldEnergy = energy;
     GenerateMove(iterate, accept, energy, idx, sweepCounter);
-    temperature = coolingSchedule.nextTemperature(temperature, energy);
+    temperature = coolingSchedule.NextTemperature(temperature, energy);
 
     // Determine if the optimization has entered (or continues to be in) a
     // frozen state.
@@ -121,8 +120,8 @@ void SA<FunctionType, CoolingScheduleType>::GenerateMove(
     size_t& idx,
     size_t& sweepCounter)
 {
-  double prevEnergy = energy;
-  double prevValue = iterate(idx);
+  const double prevEnergy = energy;
+  const double prevValue = iterate(idx);
 
   // It is possible to use a non-Laplace distribution here, but it is difficult
   // because the acceptance ratio should be as close to 0.44 as possible, and
@@ -135,11 +134,11 @@ void SA<FunctionType, CoolingScheduleType>::GenerateMove(
 
   iterate(idx) += move;
   energy = function.Evaluate(iterate);
-  // According to Metropolis criterion, accept the move with probability
+  // According to the Metropolis criterion, accept the move with probability
   // min{1, exp(-(E_new - E_old) / T)}.
-  double xi = math::Random();
-  double delta = energy - prevEnergy;
-  double criterion = std::exp(-delta / temperature);
+  const double xi = math::Random();
+  const double delta = energy - prevEnergy;
+  const double criterion = std::exp(-delta / temperature);
   if (delta <= 0. || criterion > xi)
   {
     accept(idx) += 1.;
@@ -164,7 +163,7 @@ void SA<FunctionType, CoolingScheduleType>::GenerateMove(
   }
 }
 
-/*
+/**
  * MoveControl() uses a proportional feedback control to determine the size
  * parameter to pass to the move generation distribution. The target of such
  * move control is to make the acceptance ratio, accept/nMoves, be as close to



More information about the mlpack-git mailing list