mlpack  1.0.12
sa.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_CORE_OPTIMIZERS_SA_SA_HPP
15 #define __MLPACK_CORE_OPTIMIZERS_SA_SA_HPP
16 
17 #include <mlpack/prereqs.hpp>
18 
19 #include "exponential_schedule.hpp"
20 
21 namespace mlpack {
22 namespace optimization {
23 
63 template<
64  typename FunctionType,
65  typename CoolingScheduleType = ExponentialSchedule
66 >
67 class SA
68 {
69  public:
86  SA(FunctionType& function,
87  CoolingScheduleType& coolingSchedule,
88  const size_t maxIterations = 1000000,
89  const double initT = 10000.,
90  const size_t initMoves = 1000,
91  const size_t moveCtrlSweep = 100,
92  const double tolerance = 1e-5,
93  const size_t maxToleranceSweep = 3,
94  const double maxMoveCoef = 20,
95  const double initMoveCoef = 0.3,
96  const double gain = 0.3);
97 
106  double Optimize(arma::mat& iterate);
107 
109  const FunctionType& Function() const { return function; }
111  FunctionType& Function() { return function; }
112 
114  double Temperature() const { return temperature; }
116  double& Temperature() { return temperature; }
117 
119  size_t InitMoves() const { return initMoves; }
121  size_t& InitMoves() { return initMoves; }
122 
124  size_t MoveCtrlSweep() const { return moveCtrlSweep; }
126  size_t& MoveCtrlSweep() { return moveCtrlSweep; }
127 
129  double Tolerance() const { return tolerance; }
131  double& Tolerance() { return tolerance; }
132 
134  size_t MaxToleranceSweep() const { return maxToleranceSweep; }
136  size_t& MaxToleranceSweep() { return maxToleranceSweep; }
137 
139  double Gain() const { return gain; }
141  double& Gain() { return gain; }
142 
144  size_t MaxIterations() const { return maxIterations; }
146  size_t& MaxIterations() { return maxIterations; }
147 
149  arma::mat MaxMove() const { return maxMove; }
151  arma::mat& MaxMove() { return maxMove; }
152 
154  arma::mat MoveSize() const { return moveSize; }
156  arma::mat& MoveSize() { return moveSize; }
157 
159  std::string ToString() const;
160  private:
162  FunctionType& function;
164  CoolingScheduleType& coolingSchedule;
168  double temperature;
170  size_t initMoves;
174  double tolerance;
178  double gain;
179 
181  arma::mat maxMove;
183  arma::mat moveSize;
184 
200  void GenerateMove(arma::mat& iterate,
201  arma::mat& accept,
202  double& energy,
203  size_t& idx,
204  size_t& sweepCounter);
205 
224  void MoveControl(const size_t nMoves, arma::mat& accept);
225 };
226 
227 }; // namespace optimization
228 }; // namespace mlpack
229 
230 #include "sa_impl.hpp"
231 
232 #endif
size_t MaxToleranceSweep() const
Get the maxToleranceSweep.
Definition: sa.hpp:134
size_t MoveCtrlSweep() const
Get sweeps per move control.
Definition: sa.hpp:124
double gain
Proportional control in feedback move control.
Definition: sa.hpp:178
arma::mat MoveSize() const
Get move size of each parameter.
Definition: sa.hpp:154
arma::mat & MaxMove()
Modify the maximum move size of each parameter.
Definition: sa.hpp:151
void MoveControl(const size_t nMoves, arma::mat &accept)
MoveControl() uses a proportional feedback control to determine the size parameter to pass to the mov...
size_t & MaxToleranceSweep()
Modify the maxToleranceSweep.
Definition: sa.hpp:136
SA(FunctionType &function, CoolingScheduleType &coolingSchedule, const size_t maxIterations=1000000, const double initT=10000., const size_t initMoves=1000, const size_t moveCtrlSweep=100, const double tolerance=1e-5, const size_t maxToleranceSweep=3, const double maxMoveCoef=20, const double initMoveCoef=0.3, const double gain=0.3)
Construct the SA optimizer with the given function and parameters.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
double & Gain()
Modify the gain.
Definition: sa.hpp:141
Simulated Annealing is an stochastic optimization algorithm which is able to deliver near-optimal res...
Definition: sa.hpp:67
double Optimize(arma::mat &iterate)
Optimize the given function using simulated annealing.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double temperature
The current temperature.
Definition: sa.hpp:168
double & Tolerance()
Modify the tolerance.
Definition: sa.hpp:131
size_t MaxIterations() const
Get the maximum number of iterations.
Definition: sa.hpp:144
arma::mat MaxMove() const
Get the maximum move size of each parameter.
Definition: sa.hpp:149
arma::mat maxMove
Maximum move size of each parameter.
Definition: sa.hpp:181
void GenerateMove(arma::mat &iterate, arma::mat &accept, double &energy, size_t &idx, size_t &sweepCounter)
GenerateMove proposes a move on element iterate(idx), and determines if that move is acceptable or no...
double tolerance
Tolerance for convergence.
Definition: sa.hpp:174
std::string ToString() const
Return a string representation of this object.
const FunctionType & Function() const
Get the instantiated function to be optimized.
Definition: sa.hpp:109
double Tolerance() const
Get the tolerance.
Definition: sa.hpp:129
double Temperature() const
Get the temperature.
Definition: sa.hpp:114
arma::mat moveSize
Move size of each parameter.
Definition: sa.hpp:183
size_t initMoves
The number of initial moves before reducing the temperature.
Definition: sa.hpp:170
double Gain() const
Get the gain.
Definition: sa.hpp:139
size_t & MoveCtrlSweep()
Modify sweeps per move control.
Definition: sa.hpp:126
size_t InitMoves() const
Get the initial moves.
Definition: sa.hpp:119
size_t & InitMoves()
Modify the initial moves.
Definition: sa.hpp:121
size_t moveCtrlSweep
The number of sweeps before a MoveControl() call.
Definition: sa.hpp:172
FunctionType & Function()
Modify the instantiated function.
Definition: sa.hpp:111
CoolingScheduleType & coolingSchedule
The cooling schedule being used.
Definition: sa.hpp:164
arma::mat & MoveSize()
Modify move size of each parameter.
Definition: sa.hpp:156
size_t maxIterations
The maximum number of iterations.
Definition: sa.hpp:166
double & Temperature()
Modify the temperature.
Definition: sa.hpp:116
size_t & MaxIterations()
Modify the maximum number of iterations.
Definition: sa.hpp:146
size_t maxToleranceSweep
Number of sweeps in tolerance before system is considered frozen.
Definition: sa.hpp:176