24 #include <boost/format.hpp>
25 #include "ObjCryst/RefinableObj/RefinableObj.h"
26 #include "ObjCryst/Quirks/VFNStreamFormat.h"
27 #include "ObjCryst/Quirks/VFNDebug.h"
29 #include "ObjCryst/wxCryst/wxRefinableObj.h"
34 #define POSSIBLY_UNUSED(expr) (void)(expr)
44 mpParent(0),mName(name),mId(0)
50 mpParent(parent),mName(name),mId(0)
59 VFN_DEBUG_MESSAGE(
"RefParType::IsDescendantFromOrSameAs(RefParType*): "<<
this<<
" : "<<
mpParent,1)
60 if(type->
mId==
mId)
return true;
67 if(
this==type)
return true;
74 static unsigned long nbRefParType=0;
79 long NiftyStaticGlobalObjectsInitializer_RefinableObj::mCount=0;
86 unsigned long RefinableObjClock::msTick0=0;
87 unsigned long RefinableObjClock::msTick1=0;
88 RefinableObjClock::RefinableObjClock()
94 RefinableObjClock::~RefinableObjClock()
97 set<const RefinableObjClock*> vChild=
mvChild;
98 set<RefinableObjClock*> vParent=
mvParent;
101 for(std::set<const RefinableObjClock*>::iterator pos=vChild.begin();
102 pos!=vChild.end();++pos) (*pos)->RemoveParent(*
this);
103 for(std::set<RefinableObjClock*>::iterator pos=vParent.begin();
104 pos!=vParent.end();++pos) (*pos)->RemoveChild(*
this);
107 bool RefinableObjClock::operator< (
const RefinableObjClock &rhs)
const
109 if(mTick1<rhs.mTick1)
return true;
110 if(mTick1==rhs.mTick1) {
if(mTick0<rhs.mTick0)
return true;}
113 bool RefinableObjClock::operator<=(
const RefinableObjClock &rhs)
const
115 if(mTick1<rhs.mTick1)
return true;
116 if(mTick1==rhs.mTick1)
if(mTick0<=rhs.mTick0)
return true;
119 bool RefinableObjClock::operator> (
const RefinableObjClock &rhs)
const
121 if(mTick1>rhs.mTick1)
return true;
122 if(mTick1==rhs.mTick1)
if(mTick0>rhs.mTick0)
return true;
125 bool RefinableObjClock::operator>=(
const RefinableObjClock &rhs)
const
127 if(mTick1>rhs.mTick1)
return true;
128 if(mTick1==rhs.mTick1)
if(mTick0>=rhs.mTick0)
return true;
134 if(++msTick0==0) ++msTick1;
137 for(std::set<RefinableObjClock*>::iterator pos=
mvParent.begin();
138 pos!=
mvParent.end();++pos) (*pos)->Click();
139 VFN_DEBUG_MESSAGE(
"RefinableObjClock::Click():"<<mTick1<<
":"<<mTick0<<
"(at "<<
this<<
")",0)
149 cout <<
"Clock():"<<mTick1<<
":"<<mTick0;
150 VFN_DEBUG_MESSAGE_SHORT(
" (at "<<
this<<
")",4)
155 cout <<
"RefinableObj class Clock():"<<msTick1<<
":"<<msTick0<<endl;
161 const unsigned int i =
mvChild.erase(&clock); POSSIBLY_UNUSED(i);
162 VFN_DEBUG_MESSAGE(
"RefinableObjClock::RemoveChild():"<<i,5)
171 if(clock.HasParent(*
this)==
true)
172 throw ObjCrystException(
"RefinableObjClock::AddParent(..) Loop in clock tree !!");
178 const unsigned int i =
mvParent.erase(&clock); POSSIBLY_UNUSED(i);
179 VFN_DEBUG_MESSAGE(
"RefinableObjClock::RemoveParent():"<<i,5)
186 for(std::set<RefinableObjClock*>::iterator pos=
mvParent.begin();
187 pos!=
mvParent.end();++pos)
if( (*
this) > (**pos) ) **pos = *
this;
192 for(std::set<RefinableObjClock*>::iterator pos=
mvParent.begin();
195 if((*pos)==&clock)
return true;
196 if((*pos)->HasParent(clock))
return true;
211 Restraint::~Restraint()
214 const RefParType* Restraint::GetType()
const{
return mpRefParType;}
216 void Restraint::SetType(
const RefParType *type){mpRefParType=type;}
226 mName(
""),mpValue(0),mMin(0),mMax(0),
227 mHasLimits(false),mIsFixed(true),mIsUsed(true),mIsPeriodic(false),
228 mPeriod(0.),mGlobalOptimStep(1.),mDerivStep(1e-5),mRefParDerivStepModel(REFPAR_DERIV_STEP_ABSOLUTE),
229 mSigma(0.),mHumanScale(1.),mHasAssignedClock(false),mpClock(0)
241 const bool hasLimits,
244 const bool isPeriodic,
245 const REAL humanScale,
248 mName(name),mpValue(refPar),mMin(min),mMax(max),
249 mHasLimits(hasLimits),mIsFixed(isFixed),mIsUsed(isUsed),mIsPeriodic(isPeriodic),mPeriod(period),
250 mGlobalOptimStep((max-min)/100.),mDerivStep(1e-5),mRefParDerivStepModel(derivMode),
251 mSigma(0.),mHumanScale(humanScale),
253 mUseEquation(false),mEquationNbRefPar(0),mEquationCoeff(0),
255 mHasAssignedClock(false),mpClock(0)
261 RefinablePar::~RefinablePar()
274 const bool hasLimits,
277 const bool isPeriodic,
278 const REAL humanScale,
285 Restraint::SetType(type);
332 mUseEquation=old.mUseEquation;
333 mEquationNbRefPar=old.mEquationNbRefPar;
334 mEquationCoeff=old.mEquationCoeff;
341 if(
true==mUseEquation)
343 VFN_DEBUG_MESSAGE(
"RefinablePar::Value():Evaluating Equation",0)
344 REAL tmp=mEquationCoeff(0);
345 for(
int i=0;i<mEquationNbRefPar;i++)
346 tmp += mEquationCoeff(i+1) * mEquationRefPar[i]->GetValue();
362 VFN_DEBUG_MESSAGE(
"RefinablePar::SetValue()",2)
364 if(
true==mUseEquation)
366 cout <<
"RefinablePar::SetValue(): this parameter is defined by an equation !!" <<endl;
386 if(this->IsLimited() ==
true)
391 else if(
true==this->IsPeriodic())
409 VFN_DEBUG_MESSAGE(
"RefinablePar::SetHumanValue()",2)
411 if(
true==mUseEquation)
413 cout <<
"RefinablePar::SetValue(): this parameter is defined by an equation !!" <<endl;
433 if(this->IsLimited() ==
true)
438 else if(
true==this->IsPeriodic())
447 if(0==mutateValue)
return;
448 VFN_DEBUG_MESSAGE(
"RefinablePar::Mutate():"<<this->
GetName(),1)
452 if(
true==mUseEquation)
454 cout <<
"RefinablePar::Mutate(): this parameter is defined by an equation !!" <<endl;
474 if(this->IsLimited() ==
true)
479 else if(
true==this->IsPeriodic())
485 VFN_DEBUG_MESSAGE(
"RefinablePar::Mutate():End",0)
490 VFN_DEBUG_MESSAGE(
"RefinablePar::MutateTo()",2)
492 if(*
mpValue == mutateValue)
return;
495 if(
true==mUseEquation)
497 cout <<
"RefinablePar::Mutate(): this parameter is defined by an equation !!" <<endl;
517 if(this->IsLimited() ==
true)
522 else if(
true==this->IsPeriodic())
529 REAL RefinablePar::GetSigma()
const {
return mSigma;}
531 void RefinablePar::SetSigma(
const REAL sigma) {
mSigma=sigma; this->
Click();}
533 void RefinablePar::Print()
const
548 bool RefinablePar::IsFixed()
const {
return mIsFixed;}
549 void RefinablePar::SetIsFixed(
const bool b)
551 VFN_DEBUG_MESSAGE(
"RefinablePar::SetIsFixed():"<<this->
GetName(),1)
556 void RefinablePar::SetIsLimited(
const bool b) {
mHasLimits=b;this->
Click();}
561 bool RefinablePar::IsPeriodic()
const {
return mIsPeriodic;}
562 void RefinablePar::SetIsPeriodic(
const bool b,REAL period)
586 if(d == 0.)
return 1e-8;
602 void RefinablePar::SetUseEquation(
const bool useItOrNot,
const REAL c0)
605 mUseEquation=useItOrNot;
606 if(
true==mUseEquation)
608 mEquationCoeff.resize(mEquationMaxRefPar);
609 mEquationCoeff(0)=c0;
613 void RefinablePar::SetUseEquation(
const bool useItOrNot,
const REAL c0,
617 mUseEquation=useItOrNot;
618 if(
true==mUseEquation)
620 mEquationCoeff.resize(mEquationMaxRefPar);
621 mEquationCoeff(0)=c0;
622 mEquationCoeff(1)=c1;
623 mEquationRefPar[0]=&refpar1;
627 void RefinablePar::SetUseEquation(
const bool useItOrNot,
const REAL c0,
632 mUseEquation=useItOrNot;
633 if(
true==mUseEquation)
635 mEquationCoeff.resize(mEquationMaxRefPar);
636 mEquationCoeff(0)=c0;
637 mEquationCoeff(1)=c1;
638 mEquationCoeff(2)=c2;
639 mEquationRefPar[0]=&refpar1;
640 mEquationRefPar[1]=&refpar2;
644 void RefinablePar::SetUseEquation(
const bool useItOrNot,
const REAL c0,
650 mUseEquation=useItOrNot;
651 if(
true==mUseEquation)
653 mEquationCoeff.resize(mEquationMaxRefPar);
654 mEquationCoeff(0)=c0;
655 mEquationCoeff(1)=c1;
656 mEquationCoeff(2)=c2;
657 mEquationCoeff(3)=c2;
658 mEquationRefPar[0]=&refpar1;
659 mEquationRefPar[1]=&refpar2;
660 mEquationRefPar[2]=&refpar2;
666 VFN_DEBUG_MESSAGE(
"RefinablePar::AssignClock() for "<<this->
GetName()<<
"at "<<&clock,4)
673 VFN_DEBUG_MESSAGE(
"RefinablePar::Click():"<<this->
GetName(),1)
688 VFN_DEBUG_MESSAGE(
"RefinablePar::SetLimitsRelative():"<<this->
GetName(),1)
692 this->SetIsLimited(true);
699 this->SetIsLimited(
true);
704 VFN_DEBUG_MESSAGE(
"RefinablePar::WXCreate()",8)
705 if(mpWXFieldRefPar!=0)
716 void RefinablePar::WXDelete()
718 if(0!=mpWXFieldRefPar)
720 VFN_DEBUG_MESSAGE(
"RefinablePar::WXDelete():"<<
mName,5)
721 delete mpWXFieldRefPar;
727 VFN_DEBUG_MESSAGE(
"RefinablePar::WXNotifyDelete():"<<
mName,5)
742 RefObjOpt::~RefObjOpt()
745 void RefObjOpt::Init(
const int nbChoice,
747 const string *choiceNames)
749 VFN_DEBUG_MESSAGE(
"RefObjOpt::Init()"<<*name,5)
753 mpChoiceName=choiceNames;
756 int RefObjOpt::GetNbChoice()
const
759 int RefObjOpt::GetChoice()
const
762 void RefObjOpt::SetChoice(
const int choice)
764 if(mChoice==choice)
return;
765 VFN_DEBUG_MESSAGE(
"RefObjOpt::SetChoice()"<<this->
GetName()<< \
766 " to "<<this->GetChoiceName(choice),5)
770 void RefObjOpt::SetChoice(const
string &choiceName)
773 for(choice=0;choice<mNbChoice;choice++)
if(choiceName==*(mpChoiceName+choice))
break;
774 if(choice==mNbChoice) choice=0;
775 this->SetChoice(choice);
778 const string& RefObjOpt::GetName()
const
783 const string& RefObjOpt::GetClassName()
const
785 static string className=
"Option";
789 const string& RefObjOpt::GetChoiceName(
const int i)
const
791 return *(mpChoiceName+i);
794 const RefinableObjClock& RefObjOpt::GetClock()
const{
return mClock;}
797 WXCrystObjBasic* RefObjOpt::WXCreate(wxWindow *parent)
799 VFN_DEBUG_MESSAGE(
"RefObjOpt::WXCreate()",8)
800 mpWXFieldOption=new WXFieldOption (parent,-1,this);
801 return mpWXFieldOption;
803 WXCrystObjBasic* RefObjOpt::WXGet()
805 return mpWXFieldOption;
807 void RefObjOpt::WXDelete()
809 if(0!=mpWXFieldOption)
811 VFN_DEBUG_MESSAGE(
"RefObjOpt::WXDelete()",5)
812 delete mpWXFieldOption;
816 void RefObjOpt::WXNotifyDelete()
818 VFN_DEBUG_MESSAGE(
"RefObjOpt::WXNotifyDelete()",5)
833 template<
class T>
void RefObjOption<T>::SetChoice(
const int choice)
835 if(mChoice==choice)
return;
836 VFN_DEBUG_MESSAGE(
"RefObjOption<T>::SetChoice()"<<this->GetName()<< \
837 " to "<<this->GetChoiceName(choice),5)
840 if(mfpSetNewValue !=0) (mpObj->*mfpSetNewValue)(choice);
843 template<class T>
void RefObjOption<T>::Init(const
int nbChoice,
845 const
string *choiceNames,
846 void (T::*fp)(const
int))
848 this->RefObjOpt::Init(nbChoice,name,choiceNames);
856 bool operator==(
const wxString&wx,
const string&str)
858 return wx==str.c_str();
860 bool operator==(
const string&str,
const wxString&wx)
862 return wx==str.c_str();
866 template<
class T> ObjRegistry<T>::ObjRegistry():
867 mName(
""),mAutoUpdateUI(true)
872 VFN_DEBUG_MESSAGE(
"ObjRegistry::ObjRegistry()",5)
875 template<
class T> ObjRegistry<T>::ObjRegistry(
const string &name):
876 mName(name),mAutoUpdateUI(true)
881 VFN_DEBUG_MESSAGE(
"ObjRegistry::ObjRegistry(name):"<<mName,5)
885 template<
class T> ObjRegistry<T>::~ObjRegistry()
887 VFN_DEBUG_MESSAGE(
"ObjRegistry::~ObjRegistry():"<<mName,5)
895 VFN_DEBUG_ENTRY(
"ObjRegistry("<<mName<<
")::Register():"<<obj.GetName(),2)
896 typename vector<T*>::iterator pos=find(mvpRegistry.begin(),mvpRegistry.end(),&obj);
897 if(pos!=mvpRegistry.end())
899 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::Register():"<<obj.GetName()<<
"Already registered!",2)
902 mvpRegistry.push_back(&obj);
903 mvpRegistryList.push_back(&obj);
906 if((0!=mpWXRegistry) && mAutoUpdateUI)
907 mpWXRegistry->Add(obj.WXCreate(mpWXRegistry));
910 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::Register():"<<obj.GetName(),2)
915 VFN_DEBUG_ENTRY(
"ObjRegistry("<<mName<<
")::Deregister(&obj)"<<mvpRegistry.size(),2)
916 if (mvpRegistry.size() == 0)
920 VFN_DEBUG_EXIT(
"ObjRegistry(" << mName <<
")::Deregister(&obj): EMPTY registry", 2)
924 typename vector<T*>::iterator pos=find(mvpRegistry.begin(),mvpRegistry.end(),&obj);
925 if(pos==mvpRegistry.end())
927 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::Deregister(&obj):NOT FOUND !!!",2)
931 if(0!=mpWXRegistry) mpWXRegistry->Remove(obj.WXGet());
933 mvpRegistry.erase(pos);
935 typename list<T*>::iterator pos2=find(mvpRegistryList.begin(),mvpRegistryList.end(),&obj);
936 mvpRegistryList.erase(pos2);
939 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::Deregister(&obj)",2)
944 VFN_DEBUG_ENTRY(
"ObjRegistry("<<mName<<
")::Deregister(name):"<<objName,2)
946 const long i=this->Find(objName);
949 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::Deregister(name): NOT FOUND !!!",2)
953 typename vector<T*>::iterator pos=find(mvpRegistry.begin(),mvpRegistry.end(),mvpRegistry[i]);
956 if(0!=mpWXRegistry) mpWXRegistry->Remove((*pos)->WXGet());
958 mvpRegistry.erase(pos);
960 typename list<T*>::iterator pos2=find(mvpRegistryList.begin(),mvpRegistryList.end(),mvpRegistry[i]);
961 mvpRegistryList.erase(pos2);
964 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::Deregister(name):",2)
969 VFN_DEBUG_ENTRY(
"ObjRegistry("<<mName<<
")::DeRegisterAll():",5)
973 typename vector<T*>::iterator pos;
974 for(pos=mvpRegistry.begin();pos!=mvpRegistry.end();++pos)
975 mpWXRegistry->Remove((*pos)->WXGet());
979 mvpRegistryList.clear();
981 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::DeRegisterAll():",5)
986 VFN_DEBUG_ENTRY(
"ObjRegistry("<<mName<<
")::DeleteAll():",5)
987 vector<T*> reg=mvpRegistry;
988 typename vector<T*>::iterator pos;
989 for(pos=reg.begin();pos!=reg.end();++pos)
delete *pos;
991 mvpRegistryList.clear();
993 VFN_DEBUG_EXIT(
"ObjRegistry("<<mName<<
")::DeleteAll():",5)
998 if(i>=this->GetNb())
throw ObjCrystException(
"ObjRegistry<T>::GetObj(i): i >= nb!");
999 return *(mvpRegistry[i]);
1004 if(i>=this->GetNb())
throw ObjCrystException(
"ObjRegistry<T>::GetObj(i): i >= nb!");
1005 return *(mvpRegistry[i]);
1010 const long i=this->Find(objName);
1011 return *(mvpRegistry[i]);
1016 const long i=this->Find(objName);
1017 return *(mvpRegistry[i]);
1021 const string& className)
1023 const long i=this->Find(objName,className);
1024 return *(mvpRegistry[i]);
1028 const string& className)
const
1030 const long i=this->Find(objName,className);
1031 return *(mvpRegistry[i]);
1038 VFN_DEBUG_MESSAGE(
"ObjRegistry::Print():",2)
1039 cout <<mName<<" :"<<this->GetNb()<<"
object registered:" <<endl;
1041 for(
long i=0;i<this->GetNb();++i)
1042 cout << boost::format("
#%3d:%s(%s)") %i %this->GetObj(i).GetClassName() %this->GetObj(i).GetName()<<endl;
1045 template<class T> void ObjRegistry<T>::SetName(const string &name){ mName=name;}
1047 template<class T> const string& ObjRegistry<T>::GetName()const { return mName;}
1049 template<class T> long ObjRegistry<T>::Find(const string &objName) const
1051 VFN_DEBUG_MESSAGE("ObjRegistry::Find(objName)",2)
1054 for(long i=this->GetNb()-1;i>=0;i--)
1055 if( mvpRegistry[i]->GetName() == objName) return i;
1056 // if(-1 != index) error=true ;else index=i;
1059 // cout << "ObjRegistry::Find(name) : ";
1060 // cout << "found duplicate name ! This *cannot* be !!" ;
1061 // cout << objName <<endl;
1065 cout << "ObjRegistry<T>::Find("<<objName<<"): Not found !!"<<endl;
1067 throw ObjCrystException("ObjRegistry<T>::Find("+objName+"): Not found !!");
1071 template<class T> long ObjRegistry<T>::Find(const string &objName,
1072 const string &className,
1073 const bool nothrow) const
1075 VFN_DEBUG_MESSAGE("ObjRegistry::Find(objName,className)",2)
1078 for(long i=this->GetNb()-1;i>=0;i--)
1079 if( mvpRegistry[i]->GetName() == objName)
1080 if(className==mvpRegistry[i]->GetClassName()) return i;
1081 // if(-1 != index) error=true ;else index=i;
1084 // cout << "ObjRegistry::Find(name) : ";
1085 // cout << "found duplicate name ! This *cannot* be !!" ;
1086 // cout << objName <<endl;
1090 cout << "ObjRegistry<T>::Find("<<objName<<","<<className<<"): Not found !!"<<endl;
1093 throw ObjCrystException("ObjRegistry<T>::Find("+objName+","+className+"): Not found !!");
1097 template<class T> long ObjRegistry<T>::Find(const T &obj) const
1099 VFN_DEBUG_MESSAGE("ObjRegistry::Find(&obj)",2)
1100 for(long i=this->GetNb()-1;i>=0;i--)
1101 if( mvpRegistry[i]== &obj) return i;
1102 //:TODO: throw something
1106 template<class T> long ObjRegistry<T>::Find(const T *pobj) const
1108 VFN_DEBUG_MESSAGE("ObjRegistry::Find(&obj)",2)
1109 for(long i=this->GetNb()-1;i>=0;i--)
1110 if( mvpRegistry[i]== pobj) return i;
1111 //:TODO: throw something
1115 template<class T> const RefinableObjClock& ObjRegistry<T>::GetRegistryClock()const{return mListClock;}
1117 template<class T> void ObjRegistry<T>::AutoUpdateUI(const bool autoup)
1119 mAutoUpdateUI=autoup;
1122 template<class T> void ObjRegistry<T>::UpdateUI()
1124 #ifdef __WX__CRYST__
1125 for(unsigned int i=0;i<this->GetNb();i++)
1127 if((this->GetObj(i).WXGet()==NULL) && (0!=mpWXRegistry))
1128 mpWXRegistry->Add(this->GetObj(i).WXCreate(mpWXRegistry));
1133 template<class T> std::size_t ObjRegistry<T>::size() const
1135 return (std::size_t) mvpRegistry.size();
1138 template<class T> typename vector<T*>::const_iterator ObjRegistry<T>::begin() const
1140 return mvpRegistry.begin();
1143 template<class T> typename vector<T*>::const_iterator ObjRegistry<T>::end() const
1145 return mvpRegistry.end();
1148 template<class T> typename list<T*>::const_iterator ObjRegistry<T>::list_begin() const
1150 return mvpRegistryList.begin();
1153 template<class T> typename list<T*>::const_iterator ObjRegistry<T>::list_end() const
1155 return mvpRegistryList.end();
1158 #ifdef __WX__CRYST__
1159 template<class T> WXRegistry<T>* ObjRegistry<T>::WXCreate(wxWindow *parent)
1161 VFN_DEBUG_MESSAGE("ObjRegistry<T>::WXCreate()",2)
1162 mpWXRegistry=new WXRegistry<T> (parent,this);
1163 for(int i=0;i<this->GetNb();i++)
1164 mpWXRegistry->Add(this->GetObj(i).WXCreate(mpWXRegistry));
1165 return mpWXRegistry;
1167 template<class T> void ObjRegistry<T>::WXDelete()
1171 VFN_DEBUG_MESSAGE("ObjRegistry<T>::WXDelete()",2)
1172 delete mpWXRegistry;
1176 template<class T> void ObjRegistry<T>::WXNotifyDelete()
1178 VFN_DEBUG_MESSAGE("ObjRegistry<T>::WXNotifyDelete()",2)
1183 //######################################################################
1184 // function RefObjRegisterRecursive
1185 //######################################################################
1186 template<class T> void RefObjRegisterRecursive(T &obj,ObjRegistry<T> ®)
1188 VFN_DEBUG_MESSAGE("RefObjRegisterRecursive()",3)
1190 ObjRegistry<T> *pObjReg=&(obj.GetSubObjRegistry());
1191 for(int i=0;i<pObjReg->GetNb();i++)
1192 RefObjRegisterRecursive(pObjReg->GetObj(i),reg);
1195 //######################################################################
1196 // function RefObjRegisterRecursive
1197 //######################################################################
1199 void GetSubRefObjListClockRecursive(ObjRegistry<RefinableObj> ®,RefinableObjClock &clock)
1201 if(reg.GetRegistryClock()>clock) clock=reg.GetRegistryClock();
1202 for(int i=0;i<reg.GetNb();i++)
1203 GetSubRefObjListClockRecursive(reg.GetObj(i).GetSubObjRegistry(),clock);
1206 //######################################################################
1208 //######################################################################
1210 ObjRegistry<RefinableObj> gRefinableObjRegistry("Global RefinableObj registry");
1211 ObjRegistry<RefinableObj> gTopRefinableObjRegistry("Global Top RefinableObj registry");
1213 RefinableObj::RefinableObj():
1215 mNbRefParNotFixed(-1),mOptimizationDepth(0),mDeleteRefParInDestructor(true)
1216 #ifdef __WX__CRYST__
1220 VFN_DEBUG_MESSAGE("RefinableObj::RefinableObj()",3)
1221 gRefinableObjRegistry.Register(*this);
1222 mSubObjRegistry.SetName("Registry for sub-objects");
1223 mClientObjRegistry.SetName("Registry for Clients");
1225 VFN_DEBUG_MESSAGE("RefinableObj::RefinableObj():End",2)
1227 RefinableObj::RefinableObj(const bool internalUseOnly):
1229 mNbRefParNotFixed(-1),mOptimizationDepth(0),mDeleteRefParInDestructor(true)
1230 #ifdef __WX__CRYST__
1234 VFN_DEBUG_MESSAGE("RefinableObj::RefinableObj(bool)",3)
1235 if(false==internalUseOnly) gRefinableObjRegistry.Register(*this);
1236 mSubObjRegistry.SetName("Registry for sub-objects");
1237 mClientObjRegistry.SetName("Registry for Clients");
1239 VFN_DEBUG_MESSAGE("RefinableObj::RefinableObj(bool):End",2)
1242 RefinableObj::RefinableObj(const RefinableObj &old) {}
1244 RefinableObj::RefinableObj(const RefinableObj &old):
1245 mName(old.mName),mMaxNbRefPar(old.mMaxNbRefPar),mSavedValuesSetIsUsed(mMaxNbSavedSets),
1246 mOptimizationDepth(0),mDeleteRefParInDestructor(true)
1247 #ifdef __WX__CRYST__
1251 VFN_DEBUG_MESSAGE("RefinableObj::RefinableObj(RefinableObj&)",3)
1252 mpRefPar = new RefinablePar*[mMaxNbRefPar];
1253 mpSavedValuesSet = new CrystVector_REAL* [mMaxNbSavedSets];
1254 mpSavedValuesSetName = new string* [mMaxNbSavedSets];
1255 mSavedValuesSetIsUsed=false;
1257 mSubObjRegistry.SetName("Registry for sub-objects of "+mName);
1258 mClientObjRegistry.SetName("Registry for Clients of "+mName);
1259 gRefinableObjRegistry.Register(*this);
1262 RefinableObj::~RefinableObj()
1264 VFN_DEBUG_MESSAGE("RefinableObj::~RefinableObj():"<<this->GetName(),5)
1265 if(mvpRefPar.size()>0)
1267 if(true==mDeleteRefParInDestructor)
1269 vector<RefinablePar*>::iterator pos;
1270 for(pos=mvpRefPar.begin();pos!=mvpRefPar.end();pos++) delete *pos;
1273 gRefinableObjRegistry.DeRegister(*this);
1274 for(int i=0;i<mSubObjRegistry.GetNb();i++)
1275 mSubObjRegistry.GetObj(i).DeRegisterClient(*this);
1276 VFN_DEBUG_MESSAGE("RefinableObj::~RefinableObj():End",4)
1277 #ifdef __WX__CRYST__
1282 const string& RefinableObj::GetClassName() const
1284 const static string className="RefinableObj";
1288 const string& RefinableObj::GetName() const {return mName;}
1289 void RefinableObj::SetName(const string &name)
1291 VFN_DEBUG_MESSAGE("RefinableObj::SetName()to :"<<name,6)
1293 mSubObjRegistry.SetName("Registry for sub-objects of "+mName);
1296 void RefinableObj::operator=(const RefinableObj &old)
1298 VFN_DEBUG_MESSAGE("RefinableObj::operator=(RefinableObj&)",3)
1299 this->ResetParList();
1300 //this->AddPar(old);
1301 // Do not copy old saved sets
1302 //... but erase any that may be stored
1303 for(long i=0;i<mMaxNbSavedSets;i++)
1304 if(true==mSavedValuesSetIsUsed(i))
1306 delete *(mpSavedValuesSetName+i);
1307 delete *(mpSavedValuesSet+i);
1309 mSavedValuesSetIsUsed=false;
1312 void RefinableObj::PrepareForRefinement() const
1314 VFN_DEBUG_MESSAGE("RefinableObj::PrepareForRefinement()",5)
1315 mNbRefParNotFixed=0;
1316 mRefparNotFixedIndex.resize(this->GetNbPar());
1317 for(long i=0;i<this->GetNbPar();i++)
1318 if ( (this->GetPar(i).IsFixed() == false) && (this->GetPar(i).IsUsed() == true))
1320 mRefparNotFixedIndex(mNbRefParNotFixed) = i;
1321 mNbRefParNotFixed++;
1324 VFN_DEBUG_MESSAGE("RefinableObj::PrepareForRefinement():End",5)
1327 void RefinableObj::FixAllPar()
1329 VFN_DEBUG_ENTRY("RefinableObj("<<this->GetClassName()<<":"
1330 <<this->GetName()<<")::FixAllPar()",4)
1331 for(long i=0;i<this->GetNbPar();i++) this->GetPar(i).SetIsFixed(true);
1332 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1333 this->GetSubObjRegistry().GetObj(i).FixAllPar();
1334 VFN_DEBUG_EXIT("RefinableObj("<<this->GetName()<<")::FixAllPar()",4)
1337 void RefinableObj::UnFixAllPar()
1339 for(long i=0;i<this->GetNbPar();i++) this->GetPar(i).SetIsFixed(false);
1340 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1341 this->GetSubObjRegistry().GetObj(i).UnFixAllPar();
1344 void RefinableObj::SetParIsFixed(const long parIndex,const bool fix)
1346 this->GetPar(parIndex).SetIsFixed(fix);
1349 void RefinableObj::SetParIsFixed(const string& name,const bool fix)
1351 for(long i=this->GetNbPar()-1;i>=0;i--)
1352 if( this->GetPar(i).GetName() == name)
1353 this->GetPar(i).SetIsFixed(fix);
1356 void RefinableObj::SetParIsFixed(const RefParType *type,const bool fix)
1358 for(long i=0;i<this->GetNbPar();i++)
1359 if( this->GetPar(i).GetType()->IsDescendantFromOrSameAs(type))
1361 //cout << " Fixing ..." << this->GetPar(i).Name()<<endl;
1362 this->GetPar(i).SetIsFixed(fix);
1364 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1365 this->GetSubObjRegistry().GetObj(i).SetParIsFixed(type,fix);
1368 void RefinableObj::SetParIsUsed(const string& name,const bool use)
1370 for(long i=this->GetNbPar()-1;i>=0;i--)
1371 if( this->GetPar(i).GetName() == name)
1372 this->GetPar(i).SetIsUsed(use);
1375 void RefinableObj::SetParIsUsed(const RefParType *type,const bool use)
1377 for(long i=0;i<this->GetNbPar();i++)
1378 if( this->GetPar(i).GetType()->IsDescendantFromOrSameAs(type))
1380 //cout << " Now used (Waow!) : ..." << this->GetPar(i).Name()<<endl;
1381 this->GetPar(i).SetIsUsed(use);
1383 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1384 this->GetSubObjRegistry().GetObj(i).SetParIsUsed(type,use);
1387 long RefinableObj::GetNbPar()const { return mvpRefPar.size();}
1389 long RefinableObj::GetNbParNotFixed()const {return mNbRefParNotFixed;}
1391 RefinablePar& RefinableObj::GetPar(const long i)
1393 return *(mvpRefPar[i]);
1396 const RefinablePar& RefinableObj::GetPar(const long i) const
1398 return *(mvpRefPar[i]);
1401 RefinablePar& RefinableObj::GetPar(const string & name)
1403 const long i=this->FindPar(name);
1407 throw ObjCrystException("RefinableObj::GetPar(): cannot find parameter: "+name+" in object:"+this->GetName());
1409 return *(mvpRefPar[i]);
1412 const RefinablePar& RefinableObj::GetPar(const string & name) const
1414 const long i=this->FindPar(name);
1418 throw ObjCrystException("RefinableObj::GetPar(): cannot find parameter: "+name+" in object:"+this->GetName());
1420 return *(mvpRefPar[i]);
1423 RefinablePar& RefinableObj::GetPar(const REAL *p)
1425 const long i=this->FindPar(p);
1429 throw ObjCrystException("RefinableObj::GetPar(*p): cannot find parameter in object:"+this->GetName());
1431 return *(mvpRefPar[i]);
1434 const RefinablePar& RefinableObj::GetPar(const REAL *p) const
1436 const long i=this->FindPar(p);
1440 throw ObjCrystException("RefinableObj::GetPar(*p): cannot find parameter in object:"+this->GetName());
1442 return *(mvpRefPar[i]);
1445 RefinablePar& RefinableObj::GetParNotFixed(const long i)
1447 return *(mvpRefPar[mRefparNotFixedIndex(i)]);
1450 const RefinablePar& RefinableObj::GetParNotFixed(const long i) const
1452 return *(mvpRefPar[mRefparNotFixedIndex(i)]);
1455 void RefinableObj::AddPar(const RefinablePar &newRefPar)
1457 VFN_DEBUG_MESSAGE("RefinableObj::AddPar(RefPar&)",2)
1458 string name=newRefPar.GetName();
1460 if(this->FindPar(name)>=0)
1461 while(this->FindPar(name)!=-1)
1462 {// KLUDGE ? Extend name if another parameter already exists with the same name
1463 VFN_DEBUG_MESSAGE("RefinableObj::AddPar(): need to change name ?! -> "<<name<<","<<this->FindPar(name),10)
1465 if(++ct==100) break;// KLUDGE, let go and hope for the best...
1468 mvpRefPar.push_back(new RefinablePar(newRefPar));
1469 mvpRefPar.back()->SetName(name);
1470 mRefParListClock.Click();
1473 void RefinableObj::AddPar(RefinablePar *newRefPar)
1475 VFN_DEBUG_MESSAGE("RefinableObj::AddPar(RefPar&)",2)
1476 string name=newRefPar->GetName();
1478 if(this->FindPar(name)>=0)
1479 while(this->FindPar(name)!=-1)
1480 {// KLUDGE ? Extend name if another parameter already exists with the same name
1481 VFN_DEBUG_MESSAGE("RefinableObj::AddPar(): need to change name ?! -> "<<name<<","<<this->FindPar(name),10)
1483 if(++ct==100) break;// KLUDGE, let go and hope for the best...
1485 mvpRefPar.push_back(newRefPar);
1486 mvpRefPar.back()->SetName(name);
1487 mRefParListClock.Click();
1490 void RefinableObj::AddPar(RefinableObj &newRefParList,const bool copyParam)
1492 VFN_DEBUG_MESSAGE("RefinableObj::AddPar(RefParList&)" <<newRefParList.GetNbPar() ,2)
1494 for(long i=0;i<newRefParList.GetNbPar();i++)
1496 if(copyParam) p=new RefinablePar(newRefParList.GetPar(i));
1497 else p=&(newRefParList.GetPar(i));
1502 vector<RefinablePar *>::iterator RefinableObj::RemovePar(RefinablePar *refPar)
1504 VFN_DEBUG_MESSAGE("RefinableObj::RemovePar(RefPar&)",2)
1505 vector<RefinablePar *>::iterator pos=find(mvpRefPar.begin(),mvpRefPar.end(),refPar);
1506 if(pos==mvpRefPar.end())
1508 throw ObjCrystException("RefinableObj::RemovePar():"+refPar->GetName()
1509 +"is not in this object:"+this->GetName());
1511 return mvpRefPar.erase(pos);
1514 void RefinableObj::Print() const
1516 VFN_DEBUG_ENTRY("RefinableObj::Print()",2)
1517 cout << "Refinable Object:"<<this->GetName()
1518 <<", with " << this->GetNbPar() << " parameters" <<endl;
1519 for(int i=0;i<this->GetNbPar();i++)
1521 if(this->GetPar(i).IsUsed() == false) continue;
1522 cout << "#"<<i<<"#" << this->GetPar(i).GetName() << ": " ;
1523 cout << FormatFloat(this->GetPar(i).GetHumanValue(),18,12) << " ";
1524 if(true == this->GetPar(i).IsFixed()) cout << "Fixed";
1526 if(true == this->GetPar(i).IsLimited())
1528 cout << "Limited (" << this->GetPar(i).GetHumanMin()<<","
1529 <<this->GetPar(i).GetHumanMax()<<")";
1530 if(true == this->GetPar(i).IsPeriodic()) cout << ",Periodic" ;
1532 VFN_DEBUG_MESSAGE_SHORT(" (at "<<this->GetPar(i).mpValue<<")",5)
1533 if(true == this->GetPar(i).mHasAssignedClock)
1535 VFN_DEBUG_MESSAGE_SHORT(" (Clock at "<<this->GetPar(i).mpClock<<")",5)
1539 VFN_DEBUG_EXIT("RefinableObj::Print()",2)
1542 unsigned long RefinableObj::CreateParamSet(const string name) const
1544 VFN_DEBUG_ENTRY("RefinableObj::CreateParamSet()",3)
1546 for(id=0;id<=mvpSavedValuesSet.size();id++)
1547 if(mvpSavedValuesSet.end()==mvpSavedValuesSet.find(id)) break;
1549 pair< CrystVector_REAL ,string> p;
1551 mvpSavedValuesSet.insert(make_pair(id,p));
1553 this->SaveParamSet(id);
1554 VFN_DEBUG_MESSAGE("RefinableObj::CreateParamSet(): new parameter set with id="<<id<<" and name:"<<name,2)
1555 VFN_DEBUG_EXIT("RefinableObj::CreateParamSet()",3)
1559 void RefinableObj::ClearParamSet(const unsigned long id)const
1561 VFN_DEBUG_ENTRY("RefinableObj::ClearParamSet()",2)
1562 mvpSavedValuesSet.erase(this->FindParamSet(id));
1563 VFN_DEBUG_EXIT("RefinableObj::ClearParamSet()",2)
1566 void RefinableObj::SaveParamSet(const unsigned long id)const
1568 VFN_DEBUG_MESSAGE("RefinableObj::SaveRefParSet()",2)
1569 map<unsigned long,pair<CrystVector_REAL,string> >::iterator pos=this->FindParamSet(id);
1570 pos->second.first.resize(mvpRefPar.size());
1571 REAL *p=pos->second.first.data();
1572 for(long i=0;i<this->GetNbPar();i++) *p++ = this->GetPar(i).GetValue();
1575 void RefinableObj::RestoreParamSet(const unsigned long id)
1577 VFN_DEBUG_MESSAGE("RefinableObj::RestoreRefParSet()",2)
1578 map<unsigned long,pair<CrystVector_REAL,string> >::iterator pos=this->FindParamSet(id);
1579 REAL *p=pos->second.first.data();
1580 for(long i=0;i<this->GetNbPar();i++)
1582 //if( !this->GetPar(i).IsFixed() && this->GetPar(i).IsUsed())
1583 if(this->GetPar(i).IsUsed())
1584 this->GetPar(i).SetValue(*p);
1589 const CrystVector_REAL & RefinableObj::GetParamSet(const unsigned long id)const
1591 VFN_DEBUG_MESSAGE("RefinableObj::GetParamSet() const",2)
1592 map<unsigned long,pair<CrystVector_REAL,string> >::const_iterator pos=this->FindParamSet(id);
1593 return pos->second.first;
1596 CrystVector_REAL & RefinableObj::GetParamSet(const unsigned long id)
1598 VFN_DEBUG_MESSAGE("RefinableObj::GetParamSet()",2)
1599 map<unsigned long,pair<CrystVector_REAL,string> >::iterator pos=this->FindParamSet(id);
1600 return pos->second.first;
1603 REAL RefinableObj::GetParamSet_ParNotFixedHumanValue(const unsigned long id,
1604 const long par)const
1606 VFN_DEBUG_MESSAGE("RefinableObj::RefParSetNotFixedHumanValue()",0)
1607 map<unsigned long,pair<CrystVector_REAL,string> >::iterator pos=this->FindParamSet(id);
1608 return pos->second.first(mRefparNotFixedIndex(par));
1611 const void RefinableObj::EraseAllParamSet()
1613 mvpSavedValuesSet.clear();
1616 const string& RefinableObj::GetParamSetName(const unsigned long id)const
1618 VFN_DEBUG_MESSAGE("RefinableObj::GetParamSetName()",2)
1619 map<unsigned long,pair<CrystVector_REAL,string> >::const_iterator pos=this->FindParamSet(id);
1620 return pos->second.second;
1623 void RefinableObj::SetLimitsAbsolute(const string &name,const REAL min,const REAL max)
1625 for(long i=this->GetNbPar()-1;i>=0;i--)
1626 if( this->GetPar(i).GetName() == name)
1627 this->GetPar(i).SetLimitsAbsolute(min,max);
1629 void RefinableObj::SetLimitsAbsolute(const RefParType *type,
1630 const REAL min,const REAL max)
1632 for(long i=0;i<this->GetNbPar();i++)
1633 if(this->GetPar(i).GetType()->IsDescendantFromOrSameAs(type))
1634 this->GetPar(i).SetLimitsAbsolute(min,max);
1635 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1636 this->GetSubObjRegistry().GetObj(i).SetLimitsAbsolute(type,min,max);
1638 void RefinableObj::SetLimitsRelative(const string &name, const REAL min, const REAL max)
1640 for(long i=this->GetNbPar()-1;i>=0;i--)
1641 if( this->GetPar(i).GetName() == name)
1642 this->GetPar(i).SetLimitsRelative(min,max);
1644 void RefinableObj::SetLimitsRelative(const RefParType *type,
1645 const REAL min, const REAL max)
1647 VFN_DEBUG_MESSAGE("RefinableObj::SetLimitsRelative(RefParType*):"<<this->GetName(),2)
1648 for(long i=0;i<this->GetNbPar();i++)
1650 VFN_DEBUG_MESSAGE("RefinableObj::SetLimitsRelative(RefParType*):par #"<<i,2)
1651 if(this->GetPar(i).GetType()->IsDescendantFromOrSameAs(type))
1652 this->GetPar(i).SetLimitsRelative(min,max);
1654 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1655 this->GetSubObjRegistry().GetObj(i).SetLimitsRelative(type,min,max);
1657 void RefinableObj::SetLimitsProportional(const string &name,const REAL min,const REAL max)
1659 for(long i=this->GetNbPar()-1;i>=0;i--)
1660 if( this->GetPar(i).GetName() == name)
1661 this->GetPar(i).SetLimitsProportional(min,max);
1663 void RefinableObj::SetLimitsProportional(const RefParType *type,
1664 const REAL min, const REAL max)
1666 for(long i=0;i<this->GetNbPar();i++)
1667 if(this->GetPar(i).GetType()->IsDescendantFromOrSameAs(type))
1668 this->GetPar(i).SetLimitsProportional(min,max);
1669 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1670 this->GetSubObjRegistry().GetObj(i).SetLimitsProportional(type,min,max);
1672 void RefinableObj::SetGlobalOptimStep(const RefParType *type, const REAL step)
1674 for(long i=0;i<this->GetNbPar();i++)
1675 if(this->GetPar(i).GetType()->IsDescendantFromOrSameAs(type))
1676 this->GetPar(i).SetGlobalOptimStep(step);
1677 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1678 this->GetSubObjRegistry().GetObj(i).SetGlobalOptimStep(type,step);
1681 ObjRegistry<RefinableObj>& RefinableObj::GetSubObjRegistry()
1682 {return mSubObjRegistry;}
1684 const ObjRegistry<RefinableObj>& RefinableObj::GetSubObjRegistry()const
1685 {return mSubObjRegistry;}
1687 void RefinableObj::RegisterClient(RefinableObj &obj)const
1688 {mClientObjRegistry.Register(obj);}
1690 void RefinableObj::DeRegisterClient(RefinableObj &obj)const
1691 {mClientObjRegistry.DeRegister(obj);}
1693 const ObjRegistry<RefinableObj>& RefinableObj::GetClientRegistry()const{return mClientObjRegistry;}
1694 ObjRegistry<RefinableObj>& RefinableObj::GetClientRegistry() {return mClientObjRegistry;}
1696 bool RefinableObj::IsBeingRefined()const {return mOptimizationDepth>0;}
1698 extern const long ID_WXOBJ_ENABLE; //These are defined in wxCryst/wxCryst.cpp
1699 extern const long ID_WXOBJ_DISABLE;
1700 void RefinableObj::BeginOptimization(const bool allowApproximations,
1701 const bool enableRestraints)
1703 mOptimizationDepth++;
1704 if(mOptimizationDepth>1) return;
1706 for(int i=0;i<mSubObjRegistry.GetNb();i++)
1707 mSubObjRegistry.GetObj(i).BeginOptimization(allowApproximations);
1708 #ifdef __WX__CRYST__
1711 if(true==wxThread::IsMain()) mpWXCrystObj->Enable(false);
1714 wxUpdateUIEvent event(ID_WXOBJ_DISABLE);
1715 wxPostEvent(mpWXCrystObj,event);
1721 void RefinableObj::EndOptimization()
1723 mOptimizationDepth--;
1724 if(mOptimizationDepth<0) throw ObjCrystException("RefinableObj::EndOptimization(): mOptimizationDepth<0 !!");
1726 if(mOptimizationDepth>0) return;
1727 for(int i=0;i<mSubObjRegistry.GetNb();i++)
1728 mSubObjRegistry.GetObj(i).EndOptimization();
1729 #ifdef __WX__CRYST__
1732 if(true==wxThread::IsMain()) mpWXCrystObj->Enable(true);
1735 wxUpdateUIEvent event(ID_WXOBJ_ENABLE);
1736 wxPostEvent(mpWXCrystObj,event);
1742 void RefinableObj::SetApproximationFlag(const bool allow)
1744 for(int i=0;i<mSubObjRegistry.GetNb();i++)
1745 mSubObjRegistry.GetObj(i).SetApproximationFlag(allow);
1748 void RefinableObj::RandomizeConfiguration()
1750 VFN_DEBUG_ENTRY("RefinableObj::RandomizeConfiguration():"<<mName,5)
1751 this->PrepareForRefinement();
1752 for(int j=0;j<this->GetNbParNotFixed();j++)
1754 if(true==this->GetParNotFixed(j).IsLimited())
1756 const REAL min=this->GetParNotFixed(j).GetMin();
1757 const REAL max=this->GetParNotFixed(j).GetMax();
1758 this->GetParNotFixed(j).MutateTo(min+(max-min)*(rand()/(REAL)RAND_MAX) );
1761 if(true==this->GetParNotFixed(j).IsPeriodic())
1764 this->GetParNotFixed(j).MutateTo((rand()/(REAL)RAND_MAX)
1765 * this->GetParNotFixed(j).GetPeriod());
1768 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
1769 this->GetSubObjRegistry().GetObj(i).RandomizeConfiguration();
1770 VFN_DEBUG_EXIT("RefinableObj::RandomizeConfiguration():Finished",5)
1773 void RefinableObj::GlobalOptRandomMove(const REAL mutationAmplitude,
1774 const RefParType *type)
1776 if(mRandomMoveIsDone) return;
1777 VFN_DEBUG_ENTRY("RefinableObj::GlobalOptRandomMove()",2)
1778 for(int j=0;j<this->GetNbParNotFixed();j++)
1780 if(this->GetParNotFixed(j).GetType()->IsDescendantFromOrSameAs(type))
1781 this->GetParNotFixed(j).Mutate( this->GetParNotFixed(j).GetGlobalOptimStep()
1782 *2*(rand()/(REAL)RAND_MAX-0.5)*mutationAmplitude);
1784 for(int i=0;i<mSubObjRegistry.GetNb();i++)
1785 mSubObjRegistry.GetObj(i).GlobalOptRandomMove(mutationAmplitude,type);
1786 mRandomMoveIsDone=true;
1787 VFN_DEBUG_EXIT("RefinableObj::GlobalOptRandomMove()",2)
1789 void RefinableObj::BeginGlobalOptRandomMove()
1791 mRandomMoveIsDone=false;
1792 for(int i=0;i<mSubObjRegistry.GetNb();i++)
1793 mSubObjRegistry.GetObj(i).BeginGlobalOptRandomMove();
1796 unsigned int RefinableObj::GetNbLSQFunction()const{return 0;}
1798 REAL RefinableObj::GetLogLikelihood()const
1800 VFN_DEBUG_ENTRY("RefinableObj::GetLogLikelihood()",3)
1801 if(0==mvpRestraint.size())
1803 VFN_DEBUG_EXIT("RefinableObj::GetLogLikelihood()=0",3)
1806 VFN_DEBUG_MESSAGE("RefinableObj::GetLogLikelihood()there are restraints...",2)
1808 vector< Restraint * >::const_iterator pos;
1809 for(pos=mvpRestraint.begin();pos!=mvpRestraint.end();pos++)
1811 VFN_DEBUG_MESSAGE("RefinableObj::GetLogLikelihood()Restraint: "<<*pos,2)
1812 loglike+= (*pos)->GetLogLikelihood();
1814 VFN_DEBUG_EXIT("RefinableObj::GetLogLikelihood()="<<loglike,3)
1818 // std::map<RefinablePar*, REAL>& RefinableObj::GetLogLikelihood_FullDeriv(std::set<RefinablePar *> &vPar)
1821 // return mLogLikelihood_FullDeriv;
1824 const CrystVector_REAL& RefinableObj::GetLSQCalc(const unsigned int) const
1826 throw ObjCrystException("Error: called RefinableObj::GetLSQCalc()");
1827 CrystVector_REAL *noWarning=new CrystVector_REAL;
1831 const CrystVector_REAL& RefinableObj::GetLSQObs(const unsigned int) const
1833 throw ObjCrystException("Error: called RefinableObj::GetLSQObs()");
1834 CrystVector_REAL *noWarning=new CrystVector_REAL;
1838 const CrystVector_REAL& RefinableObj::GetLSQWeight(const unsigned int) const
1840 throw ObjCrystException("Error: called RefinableObj::GetLSQWeight()");
1841 CrystVector_REAL *noWarning=new CrystVector_REAL;
1845 const CrystVector_REAL& RefinableObj::GetLSQDeriv(const unsigned int n, RefinablePar&par)
1847 // By default, use numerical derivatives
1848 const REAL step=par.GetDerivStep();
1850 mLSQDeriv =this->GetLSQCalc(n);
1851 par.Mutate(-2*step);
1852 mLSQDeriv -=this->GetLSQCalc(n);
1854 mLSQDeriv /= step*2;
1858 std::map<RefinablePar*, CrystVector_REAL> & RefinableObj::GetLSQ_FullDeriv(const unsigned int n,std::set<RefinablePar *> &vPar)
1860 mLSQ_FullDeriv[n].clear();
1861 mLSQ_FullDeriv[n][(RefinablePar*)0]=this->GetLSQCalc(n);
1862 for(std::set<RefinablePar *>::const_iterator pos=vPar.begin();pos!=vPar.end();pos++)
1863 mLSQ_FullDeriv[n][*pos]=this->GetLSQDeriv(n,**pos);
1865 return mLSQ_FullDeriv[n];
1868 void RefinableObj::ResetParList()
1870 VFN_DEBUG_MESSAGE("RefinableObj::ResetParList()",3)
1871 if(mvpRefPar.size()>0)
1873 if(true==mDeleteRefParInDestructor)
1875 vector<RefinablePar*>::iterator pos;
1876 for(pos=mvpRefPar.begin();pos!=mvpRefPar.end();pos++) delete *pos;
1880 mNbRefParNotFixed=-1;
1882 VFN_DEBUG_MESSAGE("RefinableObj::ResetParList():Deleting Saved Sets....",2)
1883 this->EraseAllParamSet();
1884 mRefParListClock.Click();
1885 VFN_DEBUG_MESSAGE("RefinableObj::ResetParList():End.",3)
1888 ObjRegistry<RefObjOpt>& RefinableObj::GetOptionList()
1890 return mOptionRegistry;
1893 unsigned int RefinableObj::GetNbOption()const
1895 return mOptionRegistry.GetNb();
1898 RefObjOpt& RefinableObj::GetOption(const unsigned int i)
1900 VFN_DEBUG_MESSAGE("RefinableObj::GetOption()"<<i,3)
1902 return mOptionRegistry.GetObj(i);
1905 RefObjOpt& RefinableObj::GetOption(const string & name)
1907 VFN_DEBUG_MESSAGE("RefinableObj::GetOption()"<<name,3)
1908 const long i=mOptionRegistry.Find(name);
1912 throw ObjCrystException("RefinableObj::GetOption(): cannot find option: "+name+" in object:"+this->GetName());
1914 return mOptionRegistry.GetObj(i);
1917 const RefObjOpt& RefinableObj::GetOption(const unsigned int i)const
1920 return mOptionRegistry.GetObj(i);
1923 const RefObjOpt& RefinableObj::GetOption(const string & name)const
1925 VFN_DEBUG_MESSAGE("RefinableObj::GetOption()"<<name,3)
1926 const long i=mOptionRegistry.Find(name);
1930 throw ObjCrystException("RefinableObj::GetOption(): cannot find option: "+name+" in object:"+this->GetName());
1932 return mOptionRegistry.GetObj(i);
1935 void RefinableObj::GetGeneGroup(const RefinableObj &obj,
1936 CrystVector_uint & groupIndex,
1937 unsigned int &first) const
1939 VFN_DEBUG_MESSAGE("RefinableObj::GetGeneGroup()",4)
1940 for(long i=0;i<obj.GetNbPar();i++)
1941 for(long j=0;j<this->GetNbPar();j++)
1942 if(&(obj.GetPar(i)) == &(this->GetPar(j))) groupIndex(i)= first++;
1944 void RefinableObj::SetDeleteRefParInDestructor(const bool b) {mDeleteRefParInDestructor=b;}
1946 const RefinableObjClock& RefinableObj::GetRefParListClock()const{return mRefParListClock;}
1948 REAL RefinableObj::GetRestraintCost()const
1950 vector<Restraint*>::const_iterator pos;
1952 for(pos=mvpRestraint.begin();pos != mvpRestraint.end();++pos)
1953 cost += (*pos)->GetLogLikelihood();
1957 void RefinableObj::AddRestraint(Restraint *pNewRestraint)
1959 VFN_DEBUG_MESSAGE("RefinableObj::AddRestraint(Restraint*)",2)
1960 mvpRestraint.push_back(pNewRestraint);
1963 vector<Restraint*>::iterator RefinableObj::RemoveRestraint(Restraint *pRestraint)
1965 VFN_DEBUG_MESSAGE("RefinableObj::RemoveRestraint(Restraint*)",2)
1966 vector<Restraint*>::iterator pos=find(mvpRestraint.begin(),mvpRestraint.end(),pRestraint);
1967 if(mvpRestraint.end() != pos)
1969 return mvpRestraint.erase(pos);
1973 cout <<"RefinableObj::RemoveRestraint(..)"
1974 <<" Whoops... tried to remove a Restraint which does not exist..."<<endl;
1979 void RefinableObj::TagNewBestConfig()const
1982 const RefinableObjClock& RefinableObj::GetClockMaster()const{return mClockMaster;}
1984 size_t RefinableObj::int_ptr() const {return (size_t)this;}
1986 void RefinableObj::UpdateDisplay()const
1988 #ifdef __WX__CRYST__
1989 VFN_DEBUG_ENTRY("RefinableObj::UpdateDisplay()",3)
1990 if(0!=mpWXCrystObj) mpWXCrystObj->CrystUpdate(true,true);
1991 VFN_DEBUG_EXIT("RefinableObj::UpdateDisplay()",3)
1995 long RefinableObj::FindPar(const string &name) const
1999 for(long i=this->GetNbPar()-1;i>=0;i--)
2000 if( this->GetPar(i).GetName() == name)
2002 if(-1 != index) warning=true ;else index=i;
2006 throw ObjCrystException("RefinableObj::FindPar("+name+"): found duplicate refinable variable name in object:"+this->GetName());
2011 long RefinableObj::FindPar(const REAL *p) const
2015 for(long i=this->GetNbPar()-1;i>=0;i--)
2016 if( p == mvpRefPar[i]->mpValue ) //&(this->GetPar(i).GetValue())
2018 if(-1 != index) warning=true ;else index=i;
2022 throw ObjCrystException("RefinableObj::FindPar(*p): Found duplicate parameter in object:"+this->GetName());
2028 void RefinableObj::AddSubRefObj(RefinableObj &obj)
2030 VFN_DEBUG_MESSAGE("RefinableObj::AddSubRefObj()",3)
2031 mSubObjRegistry.Register(obj);
2032 mClockMaster.AddChild(obj.GetClockMaster());
2035 void RefinableObj::RemoveSubRefObj(RefinableObj &obj)
2037 VFN_DEBUG_MESSAGE("RefinableObj::RemoveSubRefObj()",3)
2038 mSubObjRegistry.DeRegister(obj);
2039 mClockMaster.RemoveChild(obj.GetClockMaster());
2042 void RefinableObj::AddOption(RefObjOpt *opt)
2044 VFN_DEBUG_MESSAGE("RefinableObj::AddOption()",5)
2045 //:TODO: automagically resize the option array if necessary
2046 mOptionRegistry.Register(*opt);
2047 mClockMaster.AddChild(opt->GetClock());
2048 VFN_DEBUG_MESSAGE("RefinableObj::AddOption():End",5)
2051 void RefinableObj::Prepare()
2053 VFN_DEBUG_MESSAGE("RefinableObj::Prepare()",5)
2054 for(int i=0;i<this->GetSubObjRegistry().GetNb();i++)
2055 this->GetSubObjRegistry().GetObj(i).Prepare();
2058 map<unsigned long,pair<CrystVector_REAL,string> >::iterator
2059 RefinableObj:: FindParamSet(const unsigned long id)const
2061 VFN_DEBUG_ENTRY("RefinableObj::FindParamSet()",2)
2062 map<unsigned long,pair<CrystVector_REAL,string> >::iterator pos;
2063 pos=mvpSavedValuesSet.find(id);
2064 if(mvpSavedValuesSet.end() == pos)
2066 throw ObjCrystException("RefinableObj::FindParamSet(long): Unknown saved set ! In object:"+this->GetName());
2068 VFN_DEBUG_EXIT("RefinableObj::FindParamSet()",2)
2072 #ifdef __WX__CRYST__
2073 WXCrystObjBasic* RefinableObj::WXCreate(wxWindow *parent)
2075 VFN_DEBUG_MESSAGE("RefinableObj::WXCreate()",8)
2076 mpWXCrystObj=new WXRefinableObj (parent,this);
2077 return mpWXCrystObj;
2079 WXCrystObjBasic* RefinableObj::WXGet()
2081 return mpWXCrystObj;
2083 void RefinableObj::WXDelete()
2087 VFN_DEBUG_MESSAGE("RefinableObj::WXDelete()",5)
2088 delete mpWXCrystObj;
2092 void RefinableObj::WXNotifyDelete()
2094 VFN_DEBUG_MESSAGE("RefinableObj::WXNotifyDelete():"<<mName,5)
2098 //######################################################################
2099 // function GetRefParListClockRecursive
2100 //######################################################################
2102 void GetRefParListClockRecursive(ObjRegistry<RefinableObj> ®,RefinableObjClock &clock)
2104 for(int i=0;i<reg.GetNb();i++)
2106 if(reg.GetObj(i).GetRefParListClock()>clock)
2107 clock=reg.GetObj(i).GetRefParListClock();
2108 GetRefParListClockRecursive(reg.GetObj(i).GetSubObjRegistry(),clock);
2112 //***********EXPLICIT INSTANTIATION*******************//
2113 template void RefObjRegisterRecursive(RefinableObj &obj,ObjRegistry<RefinableObj> ®);
2115 #include "ObjCryst/ObjCryst/Crystal.h"
2116 #include "ObjCryst/ObjCryst/Scatterer.h"
2117 #include "ObjCryst/ObjCryst/ScatteringPower.h"
2118 #include "ObjCryst/ObjCryst/ZScatterer.h"
2119 #include "ObjCryst/ObjCryst/PowderPattern.h"
2120 #include "ObjCryst/ObjCryst/DiffractionDataSingleCrystal.h"
2121 #include "ObjCryst/ObjCryst/ScatteringCorr.h"
2122 #include "ObjCryst/RefinableObj/GlobalOptimObj.h"
2123 #include "ObjCryst/RefinableObj/IO.h"
2124 #include "ObjCryst/ObjCryst/ReflectionProfile.h"
2126 namespace ObjCryst {
2128 template class ObjRegistry<RefObjOpt>;
2129 template class ObjRegistry<RefinableObj>;
2130 template class ObjRegistry<Crystal>;
2131 template class ObjRegistry<Scatterer>;
2132 template class ObjRegistry<ScatteringPower>;
2133 template class ObjRegistry<ScatteringPowerAtom>;
2134 template class ObjRegistry<PowderPattern>;
2135 template class ObjRegistry<PowderPatternComponent>;
2136 template class ObjRegistry<DiffractionDataSingleCrystal>;
2137 template class ObjRegistry<OptimizationObj>;
2138 //template class ObjRegistry<IOCrystTag>;//to be removed
2139 template class ObjRegistry<XMLCrystTag>;
2140 template class ObjRegistry<ZAtom>;
2141 template class ObjRegistry<TexturePhaseMarchDollase>;
2142 template class ObjRegistry<TextureEllipsoid>;
2143 template class ObjRegistry<ReflectionProfile>;
2145 template class RefObjOption<RefinableObj>;
2146 template class RefObjOption<Crystal>;
2147 template class RefObjOption<Radiation>;
2148 template class RefObjOption<Scatterer>;
2149 template class RefObjOption<ScatteringPower>;
2150 template class RefObjOption<ScatteringPowerAtom>;
2151 template class RefObjOption<PowderPattern>;
2152 template class RefObjOption<PowderPatternComponent>;
2153 template class RefObjOption<PowderPatternBackground>;
2154 template class RefObjOption<DiffractionDataSingleCrystal>;
2155 template class RefObjOption<PowderPatternDiffraction>;
2156 //template class RefObjOption<GlobalOptimObj>;
2157 //template class RefObjOption<IOCrystTag>;
2159 } // namespace ObjCryst
The namespace which includes all objects (crystallographic and algorithmic) in ObjCryst++.
const RefParType * gpRefParTypeObjCryst
Top RefParType for the ObjCryst++ library.
RefParDerivStepModel
How do we compute steps h for numerical derivative calculation : d=f(x+h)-f(x-h)/h/2 either h is fixe...
Exception class for ObjCryst++ library.
class of refinable parameter types.
RefParType(const string &name)
Create a top parameter type.
void InitId()
Get a Unique id (RefParType::mId)
bool operator==(const RefParType *parent) const
returns true if the two types are the same.
const string & GetName() const
Get the name for this parameter.
unsigned long mId
The unique number identifying this type.
const RefParType * mpParent
the parent for this RefParType (we could easily allow several...)
bool IsDescendantFromOrSameAs(const RefParType *type) const
Returns true if the parameter is a descendant of 'type'.
const string mName
The name/description for this parameter type.
We need to record exactly when refinable objects have been modified for the last time (to avoid re-co...
std::set< RefinableObjClock * > mvParent
List of parent clocks, which will be clicked whenever this one is.
void Reset()
Reset a Clock to 0, to force an update.
void Print() const
Print clock value. Only for debugging purposes.
std::set< const RefinableObjClock * > mvChild
List of 'child' clocks, which will click this clock whenever they are clicked.
void AddChild(const RefinableObjClock &)
Add a 'child' clock.
void AddParent(RefinableObjClock &) const
Add a 'parent' clock.
void RemoveChild(const RefinableObjClock &)
remove a child clock. This also tells the child clock to remove the parent.
void RemoveParent(RefinableObjClock &) const
remove a parent clock
void operator=(const RefinableObjClock &rhs)
This will (i) set the clock to the same values as the rhs clock, but will not change the list of chil...
void Click()
Record an event for this clock (generally, the 'time' an object has been modified,...
void PrintStatic() const
Print current general clock value. Only for debugging purposes.
Restraint: generic class for a restraint of a given model.
virtual REAL GetLogLikelihood() const
Get -ln(likelihood) for this restraint.
Restraint()
Default constructor, sets RefParType to gpRefParTypeObjCryst.
Generic class for parameters of refinable objects.
void SetHumanScale(const REAL)
Human scale for this parameter : for angles, this is equal to 180/pi.
REAL GetDerivStep() const
Fixed step to use to compute numerical derivative.
REAL mDerivStep
Step to use for numerical derivative calculation.
REAL GetHumanMin() const
Get the minimum value allowed (if limited)
bool mIsPeriodic
Is the parameter periodic ? If this is the case, then when using the RefinablePar::Mutate() function,...
REAL mHumanScale
Scale to be used to display 'human' value.
string GetName() const
Get the parameter's name.
REAL mMin
Hard lower and upper limits.
void SetDerivStep(const REAL)
Fixed step to use to compute numerical derivative.
bool mHasAssignedClock
Is there a clock associated with this parameter ? If yes, then it must Click() it each time it is mod...
void SetMax(const REAL)
Get the maximum value allowed (if limited)
REAL GetMin() const
Minimum value allowed (if limited or periodic)
REAL mGlobalOptimStep
Step to use for global method search (simulated annealing,...)
void Click()
Click the Clock ! to telle the RefinableObj it has been modified.
REAL GetHumanScale() const
Human scale for this parameter : for angles, this is equal to 180/pi.
REAL GetValue() const
of the parameter.
void SetValue(const REAL value)
of the parameter.
void CopyAttributes(const RefinablePar &)
Copy all attributes (limits, flags, etc...) from another RefinablePar object.
void Init(const string &name, REAL *refPar, const REAL min, const REAL max, const RefParType *type, RefParDerivStepModel derivMode=REFPAR_DERIV_STEP_RELATIVE, const bool hasLimits=true, const bool isFixed=false, const bool isUsed=true, const bool isPeriodic=false, const REAL humanScale=1., REAL period=1.)
Constructor.
const REAL * GetPointer() const
Access to a const pointer to the refined value.
void MutateTo(const REAL newValue)
Change the current value to the given one.
void SetHumanValue(const REAL &)
Current value of parameter, scaled if necessary (for angles) to a human-understandable value.
void SetMin(const REAL)
Set the Minimum value allowed (if limited)
bool mHasLimits
Does the refinable parameter need limits (min and max) ?
REAL * mpValue
Pointer to the refinable value.
REAL mSigma
Calculated sigma on value.
void SetPeriod(const REAL)
Set the period value (if periodic)
bool mIsUsed
Is the parameter currently used ?
REAL GetHumanMax() const
Get the maximum value allowed (if limited)
void SetGlobalOptimStep(const REAL)
Maximum step to use during Global Optimization algorithms.
void SetLimitsAbsolute(const REAL min, const REAL max)
Change the limits for this object, giving absolute new limits.
bool IsUsed() const
Is the parameter used (if not, it is simply irrelevant in the model) ?
void AssignClock(RefinableObjClock &clock)
REAL GetMax() const
Get the maximum value allowed (if limited)
const REAL & GetHumanValue() const
Current value of parameter, scaled if necessary (for angles) to a human-understandable value.
void Mutate(const REAL mutateValue)
Add the given amount to the parameter current value.
void SetName(const string &)
Set the name of the parameter. It should be unique in the RefinableObj.
bool mIsFixed
is the parameter currently fixed ?
void SetLimitsRelative(const REAL min, const REAL max)
Change the limits for this object, giving relative new limits (eg giving -.1 and +....
void SetHumanMin(const REAL)
Set the minimum value allowed (if limited)
RefParDerivStepModel mRefParDerivStepModel
Model followed for derivation.
void SetHumanMax(const REAL)
Get the maximum value allowed (if limited)
REAL mPeriod
Period value (if relevant)
RefinablePar()
Default Constructor.
REAL GetGlobalOptimStep() const
Maximum step to use during Global Optimization algorithms.
void SetIsUsed(const bool)
Is the parameter used (if not, it is simply irrelevant in the model) ?
REAL GetPeriod() const
Get the period (if periodic)
void SetLimitsProportional(const REAL min, const REAL max)
Change the limits for this object, proportionnaly to the current value.
string mName
name of the refinable parameter
RefObjOpt()
Constructor for the option.
Class for options of RefinableObj, templated so that we can warn the object that something has been c...
RefObjOption(T *obj)
Constructor for the option.
void DeRegisterAll()
De-register all objects from the list.
void DeRegister(T &obj)
De-register an object.
T & GetObj(const unsigned int i)
Get object #i in the registry.
long GetNb() const
Get the index of an object in the registry, from its name Warning: it can change if an object is remo...
void Register(T &obj)
Register a new object. Already registered objects are skipped.
void DeleteAll()
Delete all objects in the registry.. Use with caution !!
Abstract base class for all objects in wxCryst.
A field for a RefinablePar.