123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- #ifndef _OPTIMIZATIONPROBLEMFIRST_OPTIMIZATION_H
- #define _OPTIMIZATIONPROBLEMFIRST_OPTIMIZATION_H
- #include <iostream>
- #include <core/basics/NonCopyable.h>
- #include <core/vector/VectorT.h>
- namespace NICE {
- class OptimizationProblemFirst : private NonCopyable {
- public:
-
- inline OptimizationProblemFirst(unsigned int dimension)
- : m_gradientCache(dimension, 0), m_positionCache(dimension,0) {
- init();
- }
- virtual ~OptimizationProblemFirst();
-
- inline void init() {
- m_objectiveCached = false;
- m_positionCached = false;
- m_gradientCached = false;
- m_gradientNormCached = false;
- }
-
- inline unsigned int dimension() const {
- return m_gradientCache.size();
- }
-
- inline double objective() {
- if (!m_objectiveCached) {
- m_objectiveCache = computeObjective();
- m_objectiveCached = true;
- }
- return m_objectiveCache;
- }
-
- inline const Vector& position() {
- if (!m_positionCached) {
- m_positionCache.resize(dimension());
- computePosition(m_positionCache);
- m_positionCached = true;
- }
- return m_positionCache;
- }
-
- inline void computeGradient() {
- m_gradientNormCached = false;
- computeGradient(m_gradientCache);
- m_gradientCached = true;
- }
-
- inline const Vector& gradientCached() {
- return m_gradientCache;
- }
-
- inline const Vector& gradientCurrent() {
- if (!m_gradientCached) {
- computeGradient();
- }
- return m_gradientCache;
- }
-
- inline double gradientNormCached() {
- if (!m_gradientNormCached) {
- m_gradientNormCache = gradientCached().normL2();
- m_gradientNormCached = true;
- }
- return m_gradientNormCache;
- }
-
- inline double gradientNormCurrent() {
- if (!m_gradientNormCached || !m_gradientCached) {
- m_gradientNormCache = gradientCurrent().normL2();
- m_gradientNormCached = true;
- }
- return m_gradientNormCache;
- }
-
- inline void applyStep(const Vector& step) {
- invalidateCaches();
- doApplyStep(step);
- }
-
- inline void unapplyStep(const Vector& step) {
- invalidateCaches();
- doUnapplyStep(step);
- }
-
- virtual void invalidateCaches() {
- m_objectiveCached = false;
- m_positionCached = false;
- m_gradientCached = false;
- }
- protected:
-
- inline Vector& parameters() {
- m_positionCache.resize(dimension());
- return m_positionCache;
- }
-
- inline const Vector& parametersConst() const {
- if (m_positionCache.size() != dimension()) {
- fthrow(Exception,
- "parametersConst(): data not initialized. Call nonconst version of parameters() first");
- }
- return m_positionCache;
- }
-
- virtual double computeObjective() = 0;
-
- virtual void computeGradient(Vector& newGradient) = 0;
-
- virtual void computePosition(Vector& pos);
-
- virtual void doApplyStep(const Vector& step);
-
- virtual void doUnapplyStep(const Vector& step);
- private:
- bool m_positionCached;
- bool m_objectiveCached;
- bool m_gradientCached;
- bool m_gradientNormCached;
- double m_objectiveCache;
- Vector m_positionCache;
- Vector m_previousPosition;
- Vector m_gradientCache;
- double m_gradientNormCache;
- friend class OptimizationProblemSecond;
- };
- }; // namespace NICE
- #endif /* _OPTIMIZATIONPROBLEMFIRST_OPTIMIZATION_H */
|