MLPACK  1.0.10
sa.hpp
Go to the documentation of this file.
1 
22 #ifndef __MLPACK_CORE_OPTIMIZERS_SA_SA_HPP
23 #define __MLPACK_CORE_OPTIMIZERS_SA_SA_HPP
24 
25 #include <mlpack/prereqs.hpp>
26 
27 #include "exponential_schedule.hpp"
28 
29 namespace mlpack {
30 namespace optimization {
31 
71 template<
72  typename FunctionType,
73  typename CoolingScheduleType = ExponentialSchedule
74 >
75 class SA
76 {
77  public:
94  SA(FunctionType& function,
95  CoolingScheduleType& coolingSchedule,
96  const size_t maxIterations = 1000000,
97  const double initT = 10000.,
98  const size_t initMoves = 1000,
99  const size_t moveCtrlSweep = 100,
100  const double tolerance = 1e-5,
101  const size_t maxToleranceSweep = 3,
102  const double maxMoveCoef = 20,
103  const double initMoveCoef = 0.3,
104  const double gain = 0.3);
105 
114  double Optimize(arma::mat& iterate);
115 
117  const FunctionType& Function() const { return function; }
119  FunctionType& Function() { return function; }
120 
122  double Temperature() const { return temperature; }
124  double& Temperature() { return temperature; }
125 
127  size_t InitMoves() const { return initMoves; }
129  size_t& InitMoves() { return initMoves; }
130 
132  size_t MoveCtrlSweep() const { return moveCtrlSweep; }
134  size_t& MoveCtrlSweep() { return moveCtrlSweep; }
135 
137  double Tolerance() const { return tolerance; }
139  double& Tolerance() { return tolerance; }
140 
142  size_t MaxToleranceSweep() const { return maxToleranceSweep; }
144  size_t& MaxToleranceSweep() { return maxToleranceSweep; }
145 
147  double Gain() const { return gain; }
149  double& Gain() { return gain; }
150 
152  size_t MaxIterations() const { return maxIterations; }
154  size_t& MaxIterations() { return maxIterations; }
155 
157  arma::mat MaxMove() const { return maxMove; }
159  arma::mat& MaxMove() { return maxMove; }
160 
162  arma::mat MoveSize() const { return moveSize; }
164  arma::mat& MoveSize() { return moveSize; }
165 
167  std::string ToString() const;
168  private:
170  FunctionType& function;
172  CoolingScheduleType& coolingSchedule;
176  double temperature;
178  size_t initMoves;
182  double tolerance;
186  double gain;
187 
189  arma::mat maxMove;
191  arma::mat moveSize;
192 
208  void GenerateMove(arma::mat& iterate,
209  arma::mat& accept,
210  double& energy,
211  size_t& idx,
212  size_t& sweepCounter);
213 
232  void MoveControl(const size_t nMoves, arma::mat& accept);
233 };
234 
235 }; // namespace optimization
236 }; // namespace mlpack
237 
238 #include "sa_impl.hpp"
239 
240 #endif
size_t MaxToleranceSweep() const
Get the maxToleranceSweep.
Definition: sa.hpp:142
size_t MoveCtrlSweep() const
Get sweeps per move control.
Definition: sa.hpp:132
double gain
Proportional control in feedback move control.
Definition: sa.hpp:186
arma::mat MoveSize() const
Get move size of each parameter.
Definition: sa.hpp:162
arma::mat & MaxMove()
Modify the maximum move size of each parameter.
Definition: sa.hpp:159
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:144
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:31
double & Gain()
Modify the gain.
Definition: sa.hpp:149
Simulated Annealing is an stochastic optimization algorithm which is able to deliver near-optimal res...
Definition: sa.hpp:75
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:176
double & Tolerance()
Modify the tolerance.
Definition: sa.hpp:139
size_t MaxIterations() const
Get the maximum number of iterations.
Definition: sa.hpp:152
arma::mat MaxMove() const
Get the maximum move size of each parameter.
Definition: sa.hpp:157
arma::mat maxMove
Maximum move size of each parameter.
Definition: sa.hpp:189
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:182
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:117
double Tolerance() const
Get the tolerance.
Definition: sa.hpp:137
double Temperature() const
Get the temperature.
Definition: sa.hpp:122
arma::mat moveSize
Move size of each parameter.
Definition: sa.hpp:191
size_t initMoves
The number of initial moves before reducing the temperature.
Definition: sa.hpp:178
double Gain() const
Get the gain.
Definition: sa.hpp:147
size_t & MoveCtrlSweep()
Modify sweeps per move control.
Definition: sa.hpp:134
size_t InitMoves() const
Get the initial moves.
Definition: sa.hpp:127
size_t & InitMoves()
Modify the initial moves.
Definition: sa.hpp:129
size_t moveCtrlSweep
The number of sweeps before a MoveControl() call.
Definition: sa.hpp:180
FunctionType & Function()
Modify the instantiated function.
Definition: sa.hpp:119
CoolingScheduleType & coolingSchedule
The cooling schedule being used.
Definition: sa.hpp:172
arma::mat & MoveSize()
Modify move size of each parameter.
Definition: sa.hpp:164
size_t maxIterations
The maximum number of iterations.
Definition: sa.hpp:174
double & Temperature()
Modify the temperature.
Definition: sa.hpp:124
size_t & MaxIterations()
Modify the maximum number of iterations.
Definition: sa.hpp:154
size_t maxToleranceSweep
Number of sweeps in tolerance before system is considered frozen.
Definition: sa.hpp:184