mlpack  2.2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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;
162  size_t maxIterations;
164  double temperature;
166  size_t initMoves;
168  size_t moveCtrlSweep;
170  double tolerance;
172  size_t maxToleranceSweep;
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
FunctionType & Function()
Modify the instantiated function.
Definition: sa.hpp:109
size_t & MaxIterations()
Modify the maximum number of iterations.
Definition: sa.hpp:144
Simulated Annealing is an stochastic optimization algorithm which is able to deliver near-optimal res...
Definition: sa.hpp:65
double & Gain()
Modify the gain.
Definition: sa.hpp:139
The core includes that mlpack expects; standard C++ includes and Armadillo.
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.
const FunctionType & Function() const
Get the instantiated function to be optimized.
Definition: sa.hpp:107
double Gain() const
Get the gain.
Definition: sa.hpp:137
size_t & MoveCtrlSweep()
Modify sweeps per move control.
Definition: sa.hpp:124
size_t MoveCtrlSweep() const
Get sweeps per move control.
Definition: sa.hpp:122
size_t InitMoves() const
Get the initial moves.
Definition: sa.hpp:117
double Optimize(arma::mat &iterate)
Optimize the given function using simulated annealing.
double Tolerance() const
Get the tolerance.
Definition: sa.hpp:127
arma::mat & MaxMove()
Modify the maximum move size of each parameter.
Definition: sa.hpp:149
arma::mat MoveSize() const
Get move size of each parameter.
Definition: sa.hpp:152
size_t MaxToleranceSweep() const
Get the maxToleranceSweep.
Definition: sa.hpp:132
double & Tolerance()
Modify the tolerance.
Definition: sa.hpp:129
size_t & InitMoves()
Modify the initial moves.
Definition: sa.hpp:119
arma::mat MaxMove() const
Get the maximum move size of each parameter.
Definition: sa.hpp:147
size_t & MaxToleranceSweep()
Modify the maxToleranceSweep.
Definition: sa.hpp:134
size_t MaxIterations() const
Get the maximum number of iterations.
Definition: sa.hpp:142
double Temperature() const
Get the temperature.
Definition: sa.hpp:112
arma::mat & MoveSize()
Modify move size of each parameter.
Definition: sa.hpp:154
double & Temperature()
Modify the temperature.
Definition: sa.hpp:114