Skip to content

Commit 096002e

Browse files
committed
Add default and copy constructors to OptimizationObj and MonteCarloObj. Small const-correctness fixes
1 parent ee30fa0 commit 096002e

File tree

2 files changed

+127
-10
lines changed

2 files changed

+127
-10
lines changed

ObjCryst/RefinableObj/GlobalOptimObj.cpp

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ void CompareWorlds(const CrystVector_long &idx,const CrystVector_long &swap, con
6565
//#################################################################################
6666
ObjRegistry<OptimizationObj> gOptimizationObjRegistry("List of all Optimization objects");
6767

68+
OptimizationObj::OptimizationObj():
69+
mName(""),mSaveFileName("GlobalOptim.save"),
70+
mNbTrialPerRun(10000000),mNbTrial(0),mRun(0),mBestCost(-1),
71+
mBestParSavedSetIndex(-1),
72+
mContext(0),
73+
mIsOptimizing(false),mStopAfterCycle(false),
74+
mRefinedObjList("OptimizationObj: "+mName+" RefinableObj registry"),
75+
mRecursiveRefinedObjList("OptimizationObj: "+mName+" recursive RefinableObj registry"),
76+
mLastOptimTime(0)
77+
{
78+
VFN_DEBUG_ENTRY("OptimizationObj::OptimizationObj()",5)
79+
// This must be done in a real class to avoid calling a pure virtual method
80+
// if a graphical representation is automatically called upon registration.
81+
// gOptimizationObjRegistry.Register(*this);
82+
83+
static bool need_initRandomSeed=true;
84+
if(need_initRandomSeed==true)
85+
{
86+
srand(time(NULL));
87+
need_initRandomSeed=false;
88+
}
89+
// We only copy parameters, so do not delete them !
90+
mRefParList.SetDeleteRefParInDestructor(false);
91+
VFN_DEBUG_EXIT("OptimizationObj::OptimizationObj()",5)
92+
}
6893

6994
OptimizationObj::OptimizationObj(const string name):
7095
mName(name),mSaveFileName("GlobalOptim.save"),
@@ -92,6 +117,36 @@ mLastOptimTime(0)
92117
VFN_DEBUG_EXIT("OptimizationObj::OptimizationObj()",5)
93118
}
94119

120+
OptimizationObj::OptimizationObj(const OptimizationObj &old):
121+
mName(old.mName),mSaveFileName(old.mSaveFileName),
122+
mNbTrialPerRun(old.mNbTrialPerRun),mNbTrial(old.mNbTrial),mRun(old.mRun),mBestCost(old.mBestCost),
123+
mBestParSavedSetIndex(-1),
124+
mContext(0),
125+
mIsOptimizing(false),mStopAfterCycle(false),
126+
mRefinedObjList("OptimizationObj: "+mName+" RefinableObj registry"),
127+
mRecursiveRefinedObjList("OptimizationObj: "+mName+" recursive RefinableObj registry"),
128+
mLastOptimTime(0)
129+
{
130+
VFN_DEBUG_ENTRY("OptimizationObj::OptimizationObj(&old)",5)
131+
// This must be done in a real class to avoid calling a pure virtual method
132+
// if a graphical representation is automatically called upon registration.
133+
// gOptimizationObjRegistry.Register(*this);
134+
135+
static bool need_initRandomSeed=true;
136+
if(need_initRandomSeed==true)
137+
{
138+
srand(time(NULL));
139+
need_initRandomSeed=false;
140+
}
141+
// We only copy parameters, so do not delete them !
142+
mRefParList.SetDeleteRefParInDestructor(false);
143+
144+
for(unsigned int i=0;i<old.mRefinedObjList.GetNb();i++)
145+
this->AddRefinableObj(old.mRefinedObjList.GetObj(i));
146+
147+
VFN_DEBUG_EXIT("OptimizationObj::OptimizationObj(&old)",5)
148+
}
149+
95150
OptimizationObj::~OptimizationObj()
96151
{
97152
VFN_DEBUG_ENTRY("OptimizationObj::~OptimizationObj()",5)
@@ -366,7 +421,7 @@ long OptimizationObj::GetParamSetIndex(const unsigned int i) const
366421
{
367422
if(i>=mvSavedParamSet.size())
368423
throw ObjCrystException("OptimizationObj::GetSavedParamSetIndex(i): i > nb saved param set");
369-
424+
370425
return mvSavedParamSet[i].first;
371426
}
372427

@@ -463,16 +518,14 @@ void OptimizationObj::InitOptions()
463518
VFN_DEBUG_MESSAGE("OptimizationObj::InitOptions():End",5)
464519
}
465520

466-
void OptimizationObj::UpdateDisplay()
521+
void OptimizationObj::UpdateDisplay() const
467522
{
468523
Chronometer chrono;
469-
#ifdef __WX__CRYST__
470-
if(0!=this->WXGet()) this->WXGet()->CrystUpdate(true,true);
471-
#endif
472524
for(int i=0;i<mRefinedObjList.GetNb();i++)
473525
mRefinedObjList.GetObj(i).UpdateDisplay();
474526
mMainTracker.UpdateDisplay();
475527
}
528+
476529
void OptimizationObj::BuildRecursiveRefObjList()
477530
{
478531
// First check if anything has changed (ie if a sub-object has been
@@ -501,6 +554,27 @@ void OptimizationObj::AddOption(RefObjOpt *opt)
501554
// MonteCarloObj
502555
//
503556
//#################################################################################
557+
MonteCarloObj::MonteCarloObj():
558+
OptimizationObj(""),
559+
mCurrentCost(-1),
560+
mTemperatureMax(1e6),mTemperatureMin(.001),mTemperatureGamma(1.0),
561+
mMutationAmplitudeMax(8.),mMutationAmplitudeMin(.125),mMutationAmplitudeGamma(1.0),
562+
mNbTrialRetry(0),mMinCostRetry(0)
563+
#ifdef __WX__CRYST__
564+
,mpWXCrystObj(0)
565+
#endif
566+
{
567+
VFN_DEBUG_ENTRY("MonteCarloObj::MonteCarloObj()",5)
568+
this->InitOptions();
569+
mGlobalOptimType.SetChoice(GLOBAL_OPTIM_PARALLEL_TEMPERING);
570+
mAnnealingScheduleTemp.SetChoice(ANNEALING_SMART);
571+
mAnnealingScheduleMutation.SetChoice(ANNEALING_EXPONENTIAL);
572+
mXMLAutoSave.SetChoice(5);//Save after each Run
573+
mAutoLSQ.SetChoice(0);
574+
gOptimizationObjRegistry.Register(*this);
575+
VFN_DEBUG_EXIT("MonteCarloObj::MonteCarloObj()",5)
576+
}
577+
504578
MonteCarloObj::MonteCarloObj(const string name):
505579
OptimizationObj(name),
506580
mCurrentCost(-1),
@@ -522,6 +596,27 @@ mNbTrialRetry(0),mMinCostRetry(0)
522596
VFN_DEBUG_EXIT("MonteCarloObj::MonteCarloObj()",5)
523597
}
524598

599+
MonteCarloObj::MonteCarloObj(const MonteCarloObj &old):
600+
OptimizationObj(old),
601+
mCurrentCost(old.mCurrentCost),
602+
mTemperatureMax(old.mTemperatureMax),mTemperatureMin(old.mTemperatureMin),
603+
mTemperatureGamma(old.mTemperatureGamma),
604+
mMutationAmplitudeMax(old.mMutationAmplitudeMax),mMutationAmplitudeMin(old.mMutationAmplitudeMin),
605+
mMutationAmplitudeGamma(old.mMutationAmplitudeGamma),
606+
mNbTrialRetry(old.mNbTrialRetry),mMinCostRetry(old.mMinCostRetry)
607+
#ifdef __WX__CRYST__
608+
,mpWXCrystObj(0)
609+
#endif
610+
{
611+
VFN_DEBUG_ENTRY("MonteCarloObj::MonteCarloObj(&old)",5)
612+
this->InitOptions();
613+
for(unsigned int i=0;i<this->GetNbOption();i++)
614+
this->GetOption(i).SetChoice(old.GetOption(i).GetChoice());
615+
616+
gOptimizationObjRegistry.Register(*this);
617+
VFN_DEBUG_EXIT("MonteCarloObj::MonteCarloObj(&old)",5)
618+
}
619+
525620
MonteCarloObj::MonteCarloObj(const bool internalUseOnly):
526621
OptimizationObj(""),
527622
mCurrentCost(-1),
@@ -2221,6 +2316,15 @@ void MonteCarloObj::InitLSQ(const bool useFullPowderPatternProfile)
22212316
mLSQ.SetParIsFixed(gpRefParTypeRadiation,true);
22222317
}
22232318

2319+
void MonteCarloObj::UpdateDisplay() const
2320+
{
2321+
Chronometer chrono;
2322+
#ifdef __WX__CRYST__
2323+
if(0!=mpWXCrystObj) mpWXCrystObj->CrystUpdate(true,true);
2324+
#endif
2325+
this->OptimizationObj::UpdateDisplay();
2326+
}
2327+
22242328
#ifdef __WX__CRYST__
22252329
WXCrystObjBasic* MonteCarloObj::WXCreate(wxWindow *parent)
22262330
{

ObjCryst/RefinableObj/GlobalOptimObj.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ enum GlobalOptimType
102102
class OptimizationObj
103103
{
104104
public:
105+
/// Default constructor
106+
OptimizationObj();
105107
/// Constructor
106-
OptimizationObj(const string name="");
108+
OptimizationObj(const string name);
109+
/// Copy constructor
110+
OptimizationObj(const OptimizationObj &old);
107111
/// Destructor
108112
virtual ~OptimizationObj();
109113

@@ -243,7 +247,7 @@ class OptimizationObj
243247
const ObjRegistry<RefinableObj>& GetRefinedObjList() const;
244248
/// Update Display (if any display is available), when a new 'relevant' configuration
245249
/// is reached. This calls all RefinableObj::UpdateDisplay()
246-
virtual void UpdateDisplay();
250+
virtual void UpdateDisplay() const;
247251
/// Get the number of saved parameters set
248252
unsigned int GetNbParamSet() const;
249253
/// Get the index of a saved parameters set in the compiled RefinableObj.
@@ -328,8 +332,10 @@ class OptimizationObj
328332
bool mStopAfterCycle;
329333

330334
// Refined objects
331-
/// The refined objects
332-
ObjRegistry<RefinableObj> mRefinedObjList;
335+
/// The refined objects. This is mutable to allow a copy constructor for
336+
/// the OptimizationObj, and because the objects are not owned
337+
/// but rather referenced here.
338+
mutable ObjRegistry<RefinableObj> mRefinedObjList;
333339
/// The refined objects, recursively including all sub-objects.
334340
/// This is mutable, since it is a function of mRefinedObjList only.
335341
mutable ObjRegistry<RefinableObj> mRecursiveRefinedObjList;
@@ -374,8 +380,12 @@ class OptimizationObj
374380
class MonteCarloObj:public OptimizationObj
375381
{
376382
public:
383+
/// Default Constructor
384+
MonteCarloObj();
377385
/// Constructor
378-
MonteCarloObj(const string name="");
386+
MonteCarloObj(const string name);
387+
/// Copy constructor
388+
MonteCarloObj(const MonteCarloObj &old);
379389
/// Constructor. Using internalUseOnly=true will avoid registering the
380390
/// the object to any registry, and thus (for example) no display will be created,
381391
/// nor will this object be automatically be saved.
@@ -478,6 +488,9 @@ class MonteCarloObj:public OptimizationObj
478488
* will be used (faster).
479489
*/
480490
virtual void InitLSQ(const bool useFullPowderPatternProfile=true);
491+
/// Update Display (if any display is available), when a new 'relevant' configuration
492+
/// is reached. This calls all RefinableObj::UpdateDisplay()
493+
virtual void UpdateDisplay() const;
481494
protected:
482495

483496
/** \brief Make a random change in the configuration.

0 commit comments

Comments
 (0)