[mlpack-svn] r16755 - mlpack/trunk/src/mlpack/core/optimizers/sa

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Jul 3 10:10:14 EDT 2014


Author: rcurtin
Date: Thu Jul  3 10:10:14 2014
New Revision: 16755

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


Modified:
   mlpack/trunk/src/mlpack/core/optimizers/sa/exponential_schedule.hpp
   mlpack/trunk/src/mlpack/core/optimizers/sa/sa.hpp
   mlpack/trunk/src/mlpack/core/optimizers/sa/sa_impl.hpp

Modified: mlpack/trunk/src/mlpack/core/optimizers/sa/exponential_schedule.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sa/exponential_schedule.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/optimizers/sa/exponential_schedule.hpp	Thu Jul  3 10:10:14 2014
@@ -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;
+  }
+
+  //! Get the cooling speed, lambda.
+  double Lambda() const { return lambda; }
+  //! Modify the cooling speed, lambda.
+  double& Lambda() { return lambda; }
 
-  //! 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;}
  private:
+  //! The cooling speed.
   double lambda;
-
-
 };
 
 }; // namespace optimization

Modified: mlpack/trunk/src/mlpack/core/optimizers/sa/sa.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sa/sa.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/optimizers/sa/sa.hpp	Thu Jul  3 10:10:14 2014
@@ -19,18 +19,18 @@
  * 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 @@
 class SA
 {
  public:
-  /*
+  /**
    * Construct the SA optimizer with the given function and parameters.
    *
    * @param function Function to be minimized.
@@ -81,8 +81,7 @@
      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.

Modified: mlpack/trunk/src/mlpack/core/optimizers/sa/sa_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/sa/sa_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/optimizers/sa/sa_impl.hpp	Thu Jul  3 10:10:14 2014
@@ -1,4 +1,4 @@
-/*
+/**
  * @file sa_impl.hpp
  * @auther Zhihao Lou
  *
@@ -57,7 +57,6 @@
   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 @@
   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 @@
     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 @@
 
   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 @@
   }
 }
 
-/*
+/**
  * 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-svn mailing list