Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

RefinableObj.h

00001 /*
00002 * LibCryst++ : a Crystallographic computing library in C++
00003 *
00004 *  (c) 2000 Vincent FAVRE-NICOLIN
00005 *           Laboratoire de Cristallographie
00006 *           24, quai Ernest-Ansermet, CH-1211 Geneva 4, Switzerland
00007 *  Contact: Vincent.Favre-Nicolin@cryst.unige.ch
00008 *           Radovan.Cerny@cryst.unige.ch
00009 *
00010 */
00011 /*
00012 *  header file for the RefinablePar and RefinableObj classes
00013 *
00014 * This is still in early development stages !! Not secure !
00015 *
00016 */
00017 
00018 #ifndef _VFN_REFINABLE_OBJ_H_
00019 #define _VFN_REFINABLE_OBJ_H_
00020 
00021 #include <string>
00022 #include <iostream>
00023 #include "CrystVector/CrystVector.h"
00024 #include "ObjCryst/General.h"
00025 #include "RefinableObj/IO.h"
00026 
00027 #ifdef __WX__CRYST__
00028    class wxWindow;
00029 namespace ObjCryst
00030 {
00031 template<class T> class ObjRegistry;
00032 class RefObjOpt;
00033 template<class T> class RefObjOption;
00034 }
00035 #include "wxCryst/wxRefinableObj.h"
00036 #endif
00037 
00038 namespace ObjCryst
00039 {
00042 enum  RefParDerivStepModel
00043 {
00044    REFPAR_DERIV_STEP_ABSOLUTE,
00045    REFPAR_DERIV_STEP_RELATIVE
00046 };
00047 
00066 class RefParType
00067 {
00068    public:
00071       RefParType(const string &name);
00073       RefParType(const RefParType *parent,const string &name);
00075       ~RefParType();
00077       bool IsDescendantFromOrSameAs(const RefParType *type) const;
00079       bool operator==(const RefParType *parent) const;
00081       const string& GetName() const;
00082    private:
00084        const RefParType* mpParent;
00086       const string mName;
00087 };
00088 
00090 extern const RefParType *gpRefParTypeObjCryst;
00091 
00100 class RefinableObjClock
00101 {
00102    public:
00103       RefinableObjClock();
00104       ~RefinableObjClock();
00105       bool operator< (const RefinableObjClock &rhs)const;
00106       bool operator<=(const RefinableObjClock &rhs)const;
00107       bool operator> (const RefinableObjClock &rhs)const;
00108       bool operator>=(const RefinableObjClock &rhs)const;
00111       void Click();
00113       void Reset();
00115       void Print()const;
00117       void PrintStatic()const;
00118    private:
00119       unsigned long mTick0, mTick1;
00120       static unsigned long msTick0,msTick1;
00121 };
00122 
00130 class RefinablePar
00131 {
00132    public:
00134 
00135 
00137       RefinablePar();
00160       RefinablePar(  const string &name,
00161                      double *refPar,
00162                      const double min,
00163                      const double max,
00164                      const RefParType *type,
00165                      RefParDerivStepModel derivMode=REFPAR_DERIV_STEP_RELATIVE,
00166                      const bool hasLimits=true,
00167                      const bool isFixed=false,
00168                      const bool isUsed=true,
00169                      const bool isPeriodic=false,
00170                      const double humanScale=1.,
00171                      double period=1.);
00172       ~RefinablePar();
00195       void Init(     const string &name,
00196                      double *refPar,
00197                      const double min,
00198                      const double max,
00199                      const RefParType *type,
00200                      RefParDerivStepModel derivMode=REFPAR_DERIV_STEP_RELATIVE,
00201                      const bool hasLimits=true,
00202                      const bool isFixed=false,
00203                      const bool isUsed=true,
00204                      const bool isPeriodic=false,
00205                      const double humanScale=1.,
00206                      double period=1.);
00207                      
00209       
00211 
00212 
00215          double GetValue()const;
00216 
00220          void SetValue(const double value);
00221 
00225          const double& GetHumanValue() const;
00226 
00230          void SetHumanValue(const double&) ;
00231 
00239          void Mutate(const double mutateValue);
00246          void MutateTo(const double newValue);
00247 
00248          double GetSigma()const;
00249          double GetHumanSigma()const;
00250          void SetSigma(const double);
00252       
00254 
00255 
00256          const RefParType* GetType()const;
00258          void SetType(const RefParType*);
00260          string GetName()const;
00261       
00262          void Print() const;
00263          
00264          bool IsFixed()const;
00265          void SetIsFixed(const bool);
00266 
00267          bool IsLimited()const;
00268          void SetIsLimited(const bool);
00269 
00271          bool IsUsed()const;
00273          void SetIsUsed(const bool);
00274 
00275          bool IsPeriodic()const;
00276          void SetIsPeriodic(const bool,double period=1);
00277 
00279          double GetHumanScale()const;
00281          void SetHumanScale(const double);
00283       
00284 
00286 
00287 
00288          double GetMin()const;
00290          void  SetMin(const double);
00291          
00293          double GetHumanMin()const;
00295          void  SetHumanMin(const double);
00296 
00298          double GetMax()const;
00300          void  SetMax(const double);
00301          
00303          double GetHumanMax()const;
00305          void  SetHumanMax(const double);
00306          
00308          double GetPeriod()const;
00310          void  SetPeriod(const double);
00312       
00314 
00315 
00316          double GetDerivStep()const;
00318          void  SetDerivStep(const double);
00319 
00321          double GetGlobalOptimStep()const;
00323          void  SetGlobalOptimStep(const double);
00325 
00326       
00327       
00329 
00330          void SetUseEquation(const bool useItOrNot,const double c0=0.);
00331          void SetUseEquation(const bool useItOrNot,const double c0,
00332                              const double c1, const RefinablePar &refpar1);
00333 
00334          void SetUseEquation(const bool useItOrNot,const double c0,
00335                              const double c1, const RefinablePar &refpar1,
00336                              const double c2, const RefinablePar &refpar2);
00337          void SetUseEquation(const bool useItOrNot,const double c0,
00338                              const double c1, const RefinablePar &refpar1,
00339                              const double c2, const RefinablePar &refpar2,
00340                              const double c3, const RefinablePar &refpar3);
00342              
00344 
00345 
00346 
00347 
00348       void AssignClock(RefinableObjClock &clock);
00350       
00352 
00353 
00354          void SetLimitsAbsolute(const double min, const double max);
00358          void SetLimitsRelative(const double min, const double max);
00361          void SetLimitsProportional(const double min, const double max);
00363 
00369       void XMLOutput(ostream &os,const string &name,int indent=0)const;
00375       void XMLOutput(ostream &os,int indent=0)const;
00379       void XMLInput(istream &is,const XMLCrystTag &tag);
00380    private:
00382       void Click();
00384       string mName;
00386       const RefParType *mpRefParType;
00388       double *mValue;
00390       double mMin;
00392       double mMax;
00394       bool mHasLimits;
00396       bool mIsFixed;
00398       bool mIsUsed;
00402       bool mIsPeriodic;
00404       double mPeriod;
00406       double mGlobalOptimStep;
00408       double mDerivStep;
00410       RefParDerivStepModel mRefParDerivStepModel;
00412       double mSigma;
00416       double mHumanScale;
00417       
00418       // Parameter defined by equations ?
00420          bool mUseEquation;
00422          static const int mEquationMaxRefPar=10;
00424          int mEquationNbRefPar;
00426          CrystVector_double mEquationCoeff;
00428          const RefinablePar *mEquationRefPar[10];
00429       
00432          bool mHasAssignedClock;
00433          RefinableObjClock* mpClock;
00434          
00435    friend class RefinableObj;
00436 };
00440 class RefObjOpt
00441 {
00442    public:
00446       RefObjOpt();
00447       virtual ~RefObjOpt();
00448       void Init(const int nbChoice,const string *name,
00449                 const string *choiceNames);
00450       int GetNbChoice()const;
00451       int GetChoice()const;
00452       virtual void SetChoice(const int choice);
00453       void SetChoice(const string &choiceName);
00454       const string& GetName()const;
00455       const string& GetClassName()const;
00456       const string& GetChoiceName(const int i)const;
00457       const RefinableObjClock& GetClock()const;
00462       void XMLOutput(ostream &os,int indent=0)const;
00466       void XMLInput(istream &is,const XMLCrystTag &tag);
00467    protected:
00469       int mNbChoice;
00471       int mChoice;
00474       const string* mpName;
00477       const string* mpChoiceName;
00479       RefinableObjClock mClock;
00480    #ifdef __WX__CRYST__
00481    public:
00482       WXCrystObjBasic* WXCreate(wxWindow *parent);
00483       WXCrystObjBasic* WXGet();
00484       void             WXDelete();
00485       void             WXNotifyDelete();
00486    private:
00487       WXFieldOption * mpWXFieldOption;
00488    #endif
00489 };
00490 
00491 
00496 template<class T> class RefObjOption:public RefObjOpt
00497 {
00498    public:
00502       RefObjOption(T* obj);
00503       ~RefObjOption();
00504       void Init(const int nbChoice,const string *name,
00505                 const string *choiceNames,
00506                 void (T::*fp)(const int));
00507       virtual void SetChoice(const int choice);
00508    protected:
00509    private:
00511       T* mpObj;
00515       void (T::*mfpSetNewValue)(const int);
00516 };
00517 
00533 template<class T> class ObjRegistry
00534 {
00535    public:
00536       ObjRegistry();
00537       ObjRegistry(const string &name);
00538       ~ObjRegistry();
00540       void Register(T &obj);
00542       void DeRegister(T &obj);
00544       void DeRegister(const string &objName);
00546       void DeleteAll();
00552       T& GetObj(const unsigned int i);
00558       const T& GetObj(const unsigned int i)const;
00561       T& GetObj(const string &objName);
00564       const T& GetObj(const string &objName)const;
00568       T& GetObj(const string &objName, const string& className);
00572       const T& GetObj(const string &objName, const string& className)const;
00575       long GetNb()const;
00576       void Print()const;
00577       void SetName(const string &);
00578       const string& GetName()const;
00581       long Find(const string &objName)const;
00585       long Find(const string &objName, const string& className)const;
00588       long Find(const T &obj)const;
00589    private:
00591       T** mpRegistry;
00593       unsigned long mNbRegistered;
00595       unsigned long mMaxNbRegistered;
00597       string mName;
00598    #ifdef __WX__CRYST__
00599    public:
00600       WXRegistry<T>* WXCreate(wxWindow *parent);
00601       void             WXDelete();
00602       void             WXNotifyDelete();
00603    private:
00604       WXRegistry<T> * mpWXRegistry;
00605    #endif
00606 };
00607 
00610 template<class T> void RefObjRegisterRecursive(T &obj,ObjRegistry<T> &reg);
00611 
00612 
00622 class RefinableObj
00623 {
00624    public:
00626       RefinableObj();
00628       RefinableObj(const RefinableObj &old);
00630       virtual ~RefinableObj();
00634       virtual const string GetClassName() const;
00636       virtual const string& GetName() const;
00638       virtual void SetName(const string &name);
00640       void operator=(const RefinableObj &old);
00641       
00644       void PrepareForRefinement() const;
00646       void FixAllPar();
00648       void UnFixAllPar();
00650       void SetParIsFixed(const long parIndex,const bool fix);
00652       void SetParIsFixed(const string& parName,const bool fix);
00654       void SetParIsFixed(const RefParType *type,const bool fix);
00656       void SetParIsUsed(const string& parName,const bool use);
00658       void SetParIsUsed(const RefParType *type,const bool use);
00663       long GetNbPar()const;
00665       long GetNbParNotFixed()const;
00666       
00668       RefinablePar& GetPar(const long i);
00670       const RefinablePar& GetPar(const long i) const;
00671       
00673       RefinablePar& GetPar(const string & name);
00675       const RefinablePar& GetPar(const string & name) const;
00676       
00678       RefinablePar& GetPar(const double*);
00680       const RefinablePar& GetPar(const double*) const;
00681       
00684       RefinablePar& GetParNotFixed(const long i);
00687       const RefinablePar& GetParNotFixed(const long i)const;
00688 
00689       void AddPar(const RefinablePar &newRefPar);
00690       
00691       void AddPar(const RefinableObj &newRefParList);
00692       
00693       virtual void Print() const;
00694       
00700       long CreateParamSet(const string name="") const;
00705       void SaveParamSet(const long id)const;
00712       void RestoreParamSet(const long id);
00717       const CrystVector_double& GetParamSet(const long setId)const;
00727       double GetParamSet_ParNotFixedHumanValue(const long setId,const long parNumber)const;
00731       const void EraseAllParamSet();
00732       
00734       void SetLimitsAbsolute(const string &parName, const double min, const double max);
00736       void SetLimitsAbsolute(const RefParType *type, const double min, const double max);
00740       void SetLimitsRelative(const string &parName, const double min, const double max);
00744       void SetLimitsRelative(const RefParType *type, const double min, const double max);
00747       void SetLimitsProportional(const string &parName, const double min, const double max);
00750       void SetLimitsProportional(const RefParType *type, const double min, const double max);
00751       
00753       ObjRegistry<RefinableObj>& GetSubObjRegistry();
00755       const ObjRegistry<RefinableObj>& GetSubObjRegistry()const;
00756       
00759       virtual void RegisterClient(RefinableObj &)const;
00761       virtual void DeRegisterClient(RefinableObj &)const;
00762       
00764       bool IsBeingRefined()const;
00765       
00773       virtual void BeginOptimization();
00780       virtual void EndOptimization();
00781       
00785       virtual void RandomizeConfiguration();
00786       
00804       virtual void GlobalOptRandomMove(const double mutationAmplitude);
00805       
00806       //Cost functions
00808          virtual unsigned int GetNbCostFunction()const;
00810          virtual const string& GetCostFunctionName(const unsigned int)const;
00812          virtual const string& GetCostFunctionDescription(const unsigned int)const;
00815          virtual double GetCostFunctionValue(const unsigned int);
00816 
00819       void ResetParList();
00820       
00825       virtual void XMLOutput(ostream &os,int indent=0)const;
00834       virtual void XMLInput(istream &is,const XMLCrystTag &tag);
00835       //virtual void XMLInputOld(istream &is,const IOCrystTag &tag);
00838       virtual void UpdateDisplay()const;
00839       //Options
00841          unsigned int GetNbOption()const;
00843          RefObjOpt& GetOption(const unsigned int i);
00845          const RefObjOpt& GetOption(const unsigned int i)const;
00846    protected:
00848       long FindPar(const string &name) const;
00850       long FindPar(const double*) const;
00851       
00853       void AddSubRefObj(RefinableObj &);
00855       void RemoveSubRefObj(RefinableObj &);
00856       
00858       void InitRandomSeedFromTime()const;
00860       void AddOption(RefObjOpt *opt);
00862       virtual void Prepare();
00863       
00865       string mName;
00867       RefinablePar **mpRefPar;
00869       long mNbRefPar;
00871       long mMaxNbRefPar;
00872       
00873       //Saved sets of parameters
00875          static const int mMaxNbSavedSets=1000;
00877          mutable CrystVector_double **mpSavedValuesSet;
00879          mutable string **mpSavedValuesSetName;
00881          mutable CrystVector_bool mSavedValuesSetIsUsed;
00882       
00883       // Used during refinements, initialized by PrepareForRefinement()
00885          mutable long mNbRefParNotFixed;
00887          mutable CrystVector_long mRefparNotFixedIndex;
00889          bool mIsbeingRefined;
00890       
00892          ObjRegistry<RefinableObj> mSubObjRegistry;
00895          mutable ObjRegistry<RefinableObj> mClientObjRegistry;
00896       
00897       // Options for this object      
00901          ObjRegistry<RefObjOpt> mOptionRegistry;
00902       
00903    #ifdef __WX__CRYST__
00904    public:
00906       virtual WXCrystObjBasic* WXCreate(wxWindow*);
00907       WXCrystObjBasic* WXGet();
00908       void WXDelete();
00909       void WXNotifyDelete();
00910    protected:
00911       WXCrystObjBasic *mpWXCrystObj;
00912    #endif
00913 };
00914 
00916 extern ObjRegistry<RefinableObj> gRefinableObjRegistry;
00920 extern ObjRegistry<RefinableObj> gTopRefinableObjRegistry;
00921 }//namespace
00922 
00923 #endif// _VFN_REFINABLE_OBJ_H_

Generated on Wed Nov 14 19:48:36 2001 for ObjCryst++ by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001