Skip to content

Commit aef3019

Browse files
committed
ml: fix SimulatedAnnealingSolver interface
1 parent 0a43957 commit aef3019

File tree

4 files changed

+174
-159
lines changed

4 files changed

+174
-159
lines changed

modules/ml/include/opencv2/ml.hpp

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,9 @@ class CV_EXPORTS_W ANN_MLP : public StatModel
15281528
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
15291529
CV_WRAP void setAnnealItePerStep(int val);
15301530

1531+
/** @brief Set/initialize anneal RNG */
1532+
void setAnnealEnergyRNG(const RNG& rng);
1533+
15311534
/** possible activation functions */
15321535
enum ActivationFunctions {
15331536
/** Identity function: \f$f(x)=x\f$ */
@@ -1875,86 +1878,104 @@ class CV_EXPORTS_W ANN_MLP_ANNEAL : public ANN_MLP
18751878
{
18761879
public:
18771880
/** @see setAnnealInitialT */
1878-
CV_WRAP virtual double getAnnealInitialT() const;
1881+
CV_WRAP virtual double getAnnealInitialT() const = 0;
18791882
/** @copybrief getAnnealInitialT @see getAnnealInitialT */
1880-
CV_WRAP virtual void setAnnealInitialT(double val);
1883+
CV_WRAP virtual void setAnnealInitialT(double val) = 0;
18811884

18821885
/** ANNEAL: Update final temperature.
18831886
It must be \>=0 and less than initialT. Default value is 0.1.*/
18841887
/** @see setAnnealFinalT */
1885-
CV_WRAP virtual double getAnnealFinalT() const;
1888+
CV_WRAP virtual double getAnnealFinalT() const = 0;
18861889
/** @copybrief getAnnealFinalT @see getAnnealFinalT */
1887-
CV_WRAP virtual void setAnnealFinalT(double val);
1890+
CV_WRAP virtual void setAnnealFinalT(double val) = 0;
18881891

18891892
/** ANNEAL: Update cooling ratio.
18901893
It must be \>0 and less than 1. Default value is 0.95.*/
18911894
/** @see setAnnealCoolingRatio */
1892-
CV_WRAP virtual double getAnnealCoolingRatio() const;
1895+
CV_WRAP virtual double getAnnealCoolingRatio() const = 0;
18931896
/** @copybrief getAnnealCoolingRatio @see getAnnealCoolingRatio */
1894-
CV_WRAP virtual void setAnnealCoolingRatio(double val);
1897+
CV_WRAP virtual void setAnnealCoolingRatio(double val) = 0;
18951898

18961899
/** ANNEAL: Update iteration per step.
18971900
It must be \>0 . Default value is 10.*/
18981901
/** @see setAnnealItePerStep */
1899-
CV_WRAP virtual int getAnnealItePerStep() const;
1902+
CV_WRAP virtual int getAnnealItePerStep() const = 0;
19001903
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
1901-
CV_WRAP virtual void setAnnealItePerStep(int val);
1902-
1903-
1904-
/** @brief Creates empty model
1905-
1906-
Use StatModel::train to train the model, Algorithm::load\<ANN_MLP\>(filename) to load the pre-trained model.
1907-
Note that the train method has optional flags: ANN_MLP::TrainFlags.
1908-
*/
1909-
// CV_WRAP static Ptr<ANN_MLP> create();
1904+
CV_WRAP virtual void setAnnealItePerStep(int val) = 0;
19101905

1906+
/** @brief Set/initialize anneal RNG */
1907+
virtual void setAnnealEnergyRNG(const RNG& rng) = 0;
19111908
};
19121909

1910+
19131911
/****************************************************************************************\
19141912
* Simulated annealing solver *
19151913
\****************************************************************************************/
19161914

1915+
/** @brief The class defines interface for system state used in simulated annealing optimization algorithm.
1916+
1917+
@cite Kirkpatrick83 for details
1918+
*/
1919+
class CV_EXPORTS SimulatedAnnealingSolverSystem
1920+
{
1921+
protected:
1922+
inline SimulatedAnnealingSolverSystem() {}
1923+
public:
1924+
virtual ~SimulatedAnnealingSolverSystem() {}
1925+
1926+
/** Give energy value for a state of system.*/
1927+
virtual double energy() const = 0;
1928+
/** Function which change the state of system (random pertubation).*/
1929+
virtual void changeState() = 0;
1930+
/** Function to reverse to the previous state. Can be called once only after changeState(). */
1931+
virtual void reverseState() = 0;
1932+
};
1933+
19171934
/** @brief The class implements simulated annealing for optimization.
1935+
*
19181936
@cite Kirkpatrick83 for details
19191937
*/
19201938
class CV_EXPORTS SimulatedAnnealingSolver : public Algorithm
19211939
{
19221940
public:
1923-
SimulatedAnnealingSolver() { init(); }
1924-
~SimulatedAnnealingSolver();
1925-
/** Give energy value for a state of system.*/
1926-
virtual double energy() =0;
1927-
/** Function which change the state of system (random pertubation).*/
1928-
virtual void changedState() = 0;
1929-
/** Function to reverse to the previous state.*/
1930-
virtual void reverseChangedState() = 0;
1931-
/** Simulated annealing procedure. */
1941+
SimulatedAnnealingSolver(const Ptr<SimulatedAnnealingSolverSystem>& system);
1942+
inline ~SimulatedAnnealingSolver() { release(); }
1943+
1944+
/** Simulated annealing procedure. */
19321945
int run();
1933-
/** Set intial temperature of simulated annealing procedure.
1934-
*@param x new initial temperature. x\>0
1946+
/** Set/initialize RNG (energy).
1947+
@param rng new RNG
1948+
*/
1949+
void setEnergyRNG(const RNG& rng);
1950+
/** Set initial temperature of simulated annealing procedure.
1951+
@param x new initial temperature. x\>0
19351952
*/
19361953
void setInitialTemperature(double x);
19371954
/** Set final temperature of simulated annealing procedure.
1938-
*@param x new final temperature value. 0\<x\<initial temperature
1955+
@param x new final temperature value. 0\<x\<initial temperature
19391956
*/
19401957
void setFinalTemperature(double x);
1958+
/** Get final temperature of simulated annealing procedure. */
19411959
double getFinalTemperature();
19421960
/** Set setCoolingRatio of simulated annealing procedure : T(t) = coolingRatio * T(t-1).
1943-
* @param x new cooling ratio value. 0\<x\<1
1961+
@param x new cooling ratio value. 0\<x\<1
19441962
*/
19451963
void setCoolingRatio(double x);
19461964
/** Set number iteration per temperature step.
1947-
* @param ite number of iteration per temperature step ite \> 0
1965+
@param ite number of iteration per temperature step ite \> 0
19481966
*/
19491967
void setIterPerStep(int ite);
19501968

1951-
protected:
1952-
void init();
1969+
void release();
1970+
SimulatedAnnealingSolver(const SimulatedAnnealingSolver&);
1971+
SimulatedAnnealingSolver& operator=(const SimulatedAnnealingSolver&);
19531972

1954-
private:
1955-
struct Impl;
1973+
struct Impl; friend struct Impl;
1974+
protected:
19561975
Impl* impl;
19571976
};
1977+
1978+
19581979
//! @} ml
19591980

19601981
}

0 commit comments

Comments
 (0)