openModeller  Version 1.4.0
Algorithm.hh
Go to the documentation of this file.
00001 
00029 #ifndef _ALGORITHMHH_
00030 #define _ALGORITHMHH_
00031 
00032 #include <map>
00033 #include <string>
00034 
00035 #include <openmodeller/om_defs.hh>
00036 #include <openmodeller/CallbackWrapper.hh>
00037 #include <openmodeller/Sampler.hh>
00038 #include <openmodeller/AlgMetadata.hh>
00039 #include <openmodeller/ignorecase_traits.hh>
00040 
00041 #include <openmodeller/Configurable.hh>
00042 #include <openmodeller/Configuration.hh>
00043 
00044 #include <openmodeller/refcount.hh>
00045 
00046 #include <openmodeller/Model.hh>
00047 
00048 #include <openmodeller/Normalizer.hh>
00049 
00050 class AlgParameter;
00051 
00052 // Forward Decl.
00053 class AlgorithmImpl;
00054 
00055 typedef ReferenceCountedPointer< AlgorithmImpl > AlgorithmPtr;
00056 typedef ReferenceCountedPointer< const AlgorithmImpl > ConstAlgorithmPtr;
00057 
00063 typedef AlgorithmImpl *(*TAlgFactory)();
00064 typedef AlgMetadata *(*TAlgMetadata)();
00065 extern "C"
00066 {
00067 OM_ALG_DLL_EXPORT AlgorithmImpl *algorithmFactory();
00068 OM_ALG_DLL_EXPORT AlgMetadata const *algorithmMetadata();
00069 }
00070 
00076 class dllexp AlgorithmImpl : public Configurable, private ReferenceCountedObject
00077 {
00078 
00079   friend class ReferenceCountedPointer<AlgorithmImpl>;
00080   friend class ReferenceCountedPointer<const AlgorithmImpl>;
00081 
00082 public:
00083 
00084   typedef std::map< icstring, std::string > ParamSetType;
00085 
00086   // This constructor should only be used by default constructors
00087   // of derived classes.
00088   explicit AlgorithmImpl( AlgMetadata const *metadata );
00089 
00090   virtual ~AlgorithmImpl();
00091   
00097   void setParameters( int nparam, AlgParameter const *param );
00098   void setParameters( const ParamSetType& );
00099   
00100   std::string const getID() const { return _metadata ? _metadata->id : 0; }
00101   
00102   AlgMetadata const *getMetadata() const { return _metadata; }
00103 
00107   AlgorithmPtr getFreshCopy();
00108   
00113   virtual int supportsModelProjection() const { return 1; }
00114   
00115   /*
00116    * Training Methods
00117    */
00118   Model createModel( const SamplerPtr& samp, CallbackWrapper *func = 0 );
00119 
00123   void setSampler( const SamplerPtr& samp );
00124   
00127   virtual int initialize() = 0;
00128   
00132   virtual int iterate() { return 1; };
00133   
00137   virtual int finalize() { return 1; }
00138   
00140   virtual int done() const { return 1; }
00141   
00143   virtual int getConvergence( Scalar * const val ) const { return 0; }
00144   
00146   virtual float getProgress() const { return (float) done(); }
00147   
00148   /*
00149    * Model Implementation.
00150    */
00151   
00155   virtual int needNormalization() { return ( _normalizerPtr == 0 ) ? 0 : 1; }
00156 
00162   Normalizer * getNormalizer() const; 
00163 
00167   void setNormalization( const SamplerPtr& samp ) const;
00168   
00172   void setNormalization( const EnvironmentPtr& env ) const;
00173   
00182   virtual Scalar getValue( const Sample& x ) const = 0;
00183   
00184   /*
00185    * Extract the Model from the Algorithm
00186    */
00187   virtual Model getModel() const;
00188   
00189   /*
00190    * Extract and set configuration
00191    */
00192   
00193   ConfigurationPtr getConfiguration() const;
00194   
00195   void setConfiguration( const ConstConfigurationPtr & );
00196   
00197 protected:
00198 
00199   virtual void _getConfiguration( ConfigurationPtr& ) const { return; }
00200   virtual void _setConfiguration( const ConstConfigurationPtr & ) { return; }
00201   
00212   int dimDomain()  { return _samp ? _samp->numIndependent() : 0; }
00213   
00221   int getParameter( std::string const &name, std::string* value );
00222   
00230   int getParameter( std::string const &name, double *value );
00231   int getParameter( std::string const &name, float  *value );
00232   
00240   int getParameter( std::string const &name, int *value );
00241   
00242 
00243 protected:
00244   
00245   SamplerPtr _samp;
00246 
00247   Normalizer * _normalizerPtr;
00248 
00249   ParamSetType _param;
00250 
00251 private:
00252 
00253   AlgMetadata const *_metadata;
00254 
00255 private:
00256   
00257   typedef ParamSetType::value_type ParamSetValueType;
00258 
00259 };
00260 
00261 #endif