mlpack  master
sa.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_OPTIMIZERS_SA_SA_HPP
13 #define MLPACK_CORE_OPTIMIZERS_SA_SA_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 #include "exponential_schedule.hpp"
18 
19 namespace mlpack {
20 namespace optimization {
21 
61 template<
62  typename FunctionType,
63  typename CoolingScheduleType = ExponentialSchedule
64 >
65 class SA
66 {
67  public:
84  SA(FunctionType& function,
85  CoolingScheduleType& coolingSchedule,
86  const size_t maxIterations = 1000000,
87  const double initT = 10000.,
88  const size_t initMoves = 1000,
89  const size_t moveCtrlSweep = 100,
90  const double tolerance = 1e-5,
91  const size_t maxToleranceSweep = 3,
92  const double maxMoveCoef = 20,
93  const double initMoveCoef = 0.3,
94  const double gain = 0.3);
95 
104  double Optimize(arma::mat& iterate);
105 
107  const FunctionType& Function() const { return function; }
109  FunctionType& Function() { return function; }
110 
112  double Temperature() const { return temperature; }
114  double& Temperature() { return temperature; }
115 
117  size_t InitMoves() const { return initMoves; }
119  size_t& InitMoves() { return initMoves; }
120 
122  size_t MoveCtrlSweep() const { return moveCtrlSweep; }
124  size_t& MoveCtrlSweep() { return moveCtrlSweep; }
125 
127  double Tolerance() const { return tolerance; }
129  double& Tolerance() { return tolerance; }
130 
132  size_t MaxToleranceSweep() const { return maxToleranceSweep; }
134  size_t& MaxToleranceSweep() { return maxToleranceSweep; }
135 
137  double Gain() const { return gain; }
139  double& Gain() { return gain; }
140 
142  size_t MaxIterations() const { return maxIterations; }
144  size_t& MaxIterations() { return maxIterations; }
145 
147  arma::mat MaxMove() const { return maxMove; }
149  arma::mat& MaxMove() { return maxMove; }
150 
152  arma::mat MoveSize() const { return moveSize; }
154  arma::mat& MoveSize() { return moveSize; }
155 
156  private:
158  FunctionType& function;
160  CoolingScheduleType& coolingSchedule;
164  double temperature;
166  size_t initMoves;
170  double tolerance;
174  double gain;
175 
177  arma::mat maxMove;
179  arma::mat moveSize;
180 
196  void GenerateMove(arma::mat& iterate,
197  arma::mat& accept,
198  double& energy,
199  size_t& idx,
200  size_t& sweepCounter);
201 
220  void MoveControl(const size_t nMoves, arma::mat& accept);
221 };
222 
223 } // namespace optimization
224 } // namespace mlpack
225 
226 #include "sa_impl.hpp"
227 
228 #endif
size_t MaxToleranceSweep() const
Get the maxToleranceSweep.
Definition: sa.hpp:132
size_t MoveCtrlSweep() const
Get sweeps per move control.
Definition: sa.hpp:122
double gain
Proportional control in feedback move control.
Definition: sa.hpp:174
arma::mat MoveSize() const
Get move size of each parameter.
Definition: sa.hpp:152
arma::mat & MaxMove()
Modify the maximum move size of each parameter.
Definition: sa.hpp:149
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:134
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: binarize.hpp:18
double & Gain()
Modify the gain.
Definition: sa.hpp:139
Simulated Annealing is an stochastic optimization algorithm which is able to deliver near-optimal res...
Definition: sa.hpp:65
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:164
double & Tolerance()
Modify the tolerance.
Definition: sa.hpp:129
size_t MaxIterations() const
Get the maximum number of iterations.
Definition: sa.hpp:142
arma::mat MaxMove() const
Get the maximum move size of each parameter.
Definition: sa.hpp:147
arma::mat maxMove
Maximum move size of each parameter.
Definition: sa.hpp:177
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:170
const FunctionType & Function() const
Get the instantiated function to be optimized.
Definition: sa.hpp:107
double Tolerance() const
Get the tolerance.
Definition: sa.hpp:127
double Temperature() const
Get the temperature.
Definition: sa.hpp:112
arma::mat moveSize
Move size of each parameter.
Definition: sa.hpp:179
size_t initMoves
The number of initial moves before reducing the temperature.
Definition: sa.hpp:166
double Gain() const
Get the gain.
Definition: sa.hpp:137
size_t & MoveCtrlSweep()
Modify sweeps per move control.
Definition: sa.hpp:124
size_t InitMoves() const
Get the initial moves.
Definition: sa.hpp:117
size_t & InitMoves()
Modify the initial moves.
Definition: sa.hpp:119
size_t moveCtrlSweep
The number of sweeps before a MoveControl() call.
Definition: sa.hpp:168
FunctionType & Function()
Modify the instantiated function.
Definition: sa.hpp:109
CoolingScheduleType & coolingSchedule
The cooling schedule being used.
Definition: sa.hpp:160
arma::mat & MoveSize()
Modify move size of each parameter.
Definition: sa.hpp:154
size_t maxIterations
The maximum number of iterations.
Definition: sa.hpp:162
double & Temperature()
Modify the temperature.
Definition: sa.hpp:114
size_t & MaxIterations()
Modify the maximum number of iterations.
Definition: sa.hpp:144
size_t maxToleranceSweep
Number of sweeps in tolerance before system is considered frozen.
Definition: sa.hpp:172