FOX/ObjCryst++  2022
ObjCryst::MonteCarloObj Class Reference

Base object for Monte-Carlo Global Optimization methods. More...

+ Inheritance diagram for ObjCryst::MonteCarloObj:
+ Collaboration diagram for ObjCryst::MonteCarloObj:

Public Member Functions

 MonteCarloObj ()
 Default Constructor.
 
 MonteCarloObj (const string name)
 Constructor.
 
 MonteCarloObj (const MonteCarloObj &old)
 Copy constructor.
 
 MonteCarloObj (const bool internalUseOnly)
 Constructor. More...
 
virtual ~MonteCarloObj ()
 Destructor.
 
void SetAlgorithmSimulAnnealing (const AnnealingSchedule scheduleTemp, const REAL tMax, const REAL tMin, const AnnealingSchedule scheduleMutation=ANNEALING_CONSTANT, const REAL mutMax=16., const REAL mutMin=.125, const long nbTrialRetry=0, const REAL minCostRetry=0.)
 Set the refinement method to simulated Annealing. More...
 
void SetAlgorithmParallTempering (const AnnealingSchedule scheduleTemp, const REAL tMax, const REAL tMin, const AnnealingSchedule scheduleMutation=ANNEALING_CONSTANT, const REAL mutMax=16., const REAL mutMin=.125)
 Set the refinement method to Parallel Tempering. More...
 
virtual void Optimize (long &nbSteps, const bool silent=false, const REAL finalcost=0, const REAL maxTime=-1)
 Launch optimization (a single run) for N steps. More...
 
virtual void MultiRunOptimize (long &nbCycle, long &nbSteps, const bool silent=false, const REAL finalcost=0, const REAL maxTime=-1)
 Launch optimization for multiple runs of N steps. More...
 
void RunSimulatedAnnealing (long &nbSteps, const bool silent=false, const REAL finalcost=0, const REAL maxTime=-1)
 
void RunParallelTempering (long &nbSteps, const bool silent=false, const REAL finalcost=0, const REAL maxTime=-1)
 
void RunRandomLSQMethod (long &nbCycle)
 
virtual void XMLOutput (ostream &os, int indent=0) const
 Output a description of the object in XML format to a stream. More...
 
virtual void XMLInput (istream &is, const XMLCrystTag &tag)
 Input in XML format from a stream, restoring the set of refined objects and the associated cost functions. More...
 
virtual const string GetClassName () const
 Get the name for this class type.
 
LSQNumObjGetLSQObj ()
 Access to the builtin LSQ optimization object.
 
const LSQNumObjGetLSQObj () const
 Access to the builtin LSQ optimization object.
 
virtual void InitLSQ (const bool useFullPowderPatternProfile=true)
 Prepare mLSQ for least-squares refinement during the global optimization. More...
 
virtual void UpdateDisplay () const
 Update Display (if any display is available), when a new 'relevant' configuration is reached. More...
 
- Public Member Functions inherited from ObjCryst::OptimizationObj
 OptimizationObj ()
 Default constructor.
 
 OptimizationObj (const string name)
 Constructor.
 
 OptimizationObj (const OptimizationObj &old)
 Copy constructor.
 
virtual ~OptimizationObj ()
 Destructor.
 
virtual void RandomizeStartingConfig ()
 Randomize starting configuration. More...
 
void FixAllPar ()
 Fix all parameters.
 
void SetParIsFixed (const string &parName, const bool fix)
 Fix one parameter.
 
void SetParIsFixed (const RefParType *type, const bool fix)
 Fix one family of parameters.
 
void UnFixAllPar ()
 UnFix All parameters.
 
void SetParIsUsed (const string &parName, const bool use)
 Set a parameter to be used.
 
void SetParIsUsed (const RefParType *type, const bool use)
 Set a family of parameters to be used.
 
void SetLimitsRelative (const string &parName, const REAL min, const REAL max)
 Change the relative limits for a parameter from its name.
 
void SetLimitsRelative (const RefParType *type, const REAL min, const REAL max)
 Change the relative limits for a family of parameter.
 
void SetLimitsAbsolute (const string &parName, const REAL min, const REAL max)
 Change the absolute limits for a parameter from its name.
 
void SetLimitsAbsolute (const RefParType *type, const REAL min, const REAL max)
 Change the absolute limits for a family of parameter.
 
virtual REAL GetLogLikelihood () const
 The optimized (minimized, actually) function. More...
 
void StopAfterCycle ()
 Stop after the current cycle. USed for interactive refinement.
 
virtual void DisplayReport ()
 Show report to the user during refinement. Used for GUI update.
 
void AddRefinableObj (RefinableObj &)
 Add a refined object. All sub-objects are also added.
 
RefinableObjGetFullRefinableObj (const bool rebuild=true)
 Get the RefinableObj with all the parameters from all refined objects. More...
 
const string & GetName () const
 Get the name for this object.
 
void SetName (const string &)
 Set the name for this object.
 
virtual void Print () const
 Print some information about this object.
 
void RestoreBestConfiguration ()
 Restore the Best configuration.
 
bool IsOptimizing () const
 Are we busy optimizing ?
 
void TagNewBestConfig ()
 During a global optimization, tell all objects that the current config is the latest "best" config.
 
REAL GetLastOptimElapsedTime () const
 Get the elapsed time (in seconds) during the last optimization.
 
MainTrackerGetMainTracker ()
 Get the MainTracker.
 
const MainTrackerGetMainTracker () const
 Get the MainTracker.
 
RefObjOptGetXMLAutoSaveOption ()
 
const RefObjOptGetXMLAutoSaveOption () const
 
const REAL & GetBestCost () const
 Access to current best cost.
 
REAL & GetBestCost ()
 Access to current best cost.
 
virtual void BeginOptimization (const bool allowApproximations=false, const bool enableRestraints=false)
 Begin optimization for all objects.
 
virtual void EndOptimization ()
 End optimization for all objects.
 
virtual long & NbTrialPerRun ()
 Number of trial per run.
 
virtual const long & NbTrialPerRun () const
 Number of trial per run.
 
long GetTrial () const
 Current trial number (updated during a run)
 
long GetRun () const
 Current run number (updated during a run)
 
ObjRegistry< RefObjOpt > & GetOptionList ()
 Access to the options registry.
 
unsigned int GetNbOption () const
 Number of Options for this object.
 
RefObjOptGetOption (const unsigned int i)
 Access to the options.
 
RefObjOptGetOption (const string &name)
 Access to the options by name.
 
const RefObjOptGetOption (const unsigned int i) const
 const access to the options
 
const RefObjOptGetOption (const string &name) const
 const access to the options by name
 
const ObjRegistry< RefinableObj > & GetRefinedObjList () const
 Access the list of refined object.
 
unsigned int GetNbParamSet () const
 Get the number of saved parameters set.
 
long GetParamSetIndex (const unsigned int i) const
 Get the index of a saved parameters set in the compiled RefinableObj. More...
 
long GetParamSetCost (const unsigned int i) const
 Get the cost (log-likelihood) of a saved parameters set.
 
void RestoreParamSet (const unsigned int i, const bool update_display=true)
 Restore a given saved parameter set. More...
 

Protected Member Functions

virtual void NewConfiguration (const RefParType *type=gpRefParTypeObjCryst)
 Make a random change in the configuration. More...
 
virtual void InitOptions ()
 Initialization of options.
 
- Protected Member Functions inherited from ObjCryst::OptimizationObj
void PrepareRefParList ()
 
void BuildRecursiveRefObjList ()
 (Re)build OptimizationObj::mRecursiveRefinedObjList, if an object has been added or modified. More...
 
void AddOption (RefObjOpt *opt)
 

Protected Attributes

RefObjOpt mGlobalOptimType
 Method used for the global optimization. More...
 
REAL mCurrentCost
 Current value of the cost function.
 
RefObjOpt mSaveTrackedData
 Option to save the evolution of tracked data (cost functions, likelihhod, individual parameters,...)
 
REAL mTemperature
 Current temperature for annealing.
 
REAL mTemperatureMax
 Beginning temperature for annealing.
 
REAL mTemperatureMin
 Lower temperature.
 
RefObjOpt mAnnealingScheduleTemp
 Schedule for the annealing.
 
REAL mTemperatureGamma
 Gamma for the 'gamma' temperature schedule.
 
REAL mMutationAmplitude
 Mutation amplitude. More...
 
REAL mMutationAmplitudeMax
 Mutation amplitude at the beginning of the optimization.
 
REAL mMutationAmplitudeMin
 Mutation amplitude at the end of the optimization.
 
RefObjOpt mAnnealingScheduleMutation
 Schedule for the annealing.
 
REAL mMutationAmplitudeGamma
 Gamma for the 'gamma' Mutation amplitude schedule.
 
long mNbTrialRetry
 Number of trials before testing if we are below the given minimum cost. More...
 
REAL mMinCostRetry
 Cost to reach unless an automatic randomization and retry is done.
 
LSQNumObj mLSQ
 Least squares object.
 
RefObjOpt mAutoLSQ
 Option to run automatic least-squares refinements.
 
- Protected Attributes inherited from ObjCryst::OptimizationObj
RefinableObj mRefParList
 The refinable par list used during refinement. More...
 
string mName
 Name of the GlobalOptimization object.
 
string mSaveFileName
 File name where refinement info is saved (NOT USED so far...)
 
long mNbTrialPerRun
 Number of trial per run, to be saved/restored in XML output.
 
long mNbTrial
 Current trial number.
 
long mRun
 Current run number (during multiple runs)
 
REAL mBestCost
 Best value of the cost function so far.
 
long mBestParSavedSetIndex
 Index of the 'best' saved parameter set.
 
unsigned long mContext
 The current 'context', in the case the optimization is run in different parallel contexts.
 
map< unsigned long, map< const RefinableObj *, LogLikelihoodStats > > mvContextObjStats
 Statistics for each context (mutable for dynamic update during optimization)
 
map< const RefinableObj *, DynamicObjWeightmvObjWeight
 Weights for each objects in each context (mutable for dynamic update during optimization)
 
std::vector< pair< long, REAL > > mvSavedParamSet
 List of saved parameter sets. More...
 
bool mIsOptimizing
 True if a refinement is being done. For multi-threaded environment.
 
bool mStopAfterCycle
 If true, then stop at the end of the cycle. Used in multi-threaded environment.
 
ObjRegistry< RefinableObjmRefinedObjList
 The refined objects. More...
 
ObjRegistry< RefinableObjmRecursiveRefinedObjList
 The refined objects, recursively including all sub-objects. More...
 
RefObjOpt mXMLAutoSave
 Periodic save of complete environment as an xml file.
 
REAL mLastOptimTime
 The time elapsed after the last optimization, in seconds.
 
MainTracker mMainTracker
 MainTracker object to track the evolution of cost functions, likelihood, and individual parameters.
 
ObjRegistry< RefObjOptmOptionRegistry
 List of options for this object. More...
 

Detailed Description

Base object for Monte-Carlo Global Optimization methods.

The algorithm is quite simple, whith two type of optimizations, either simulated Annealing or Parallel Tempering, the latter being recommanded for most real-world optimizations.

Definition at line 380 of file GlobalOptimObj.h.

Constructor & Destructor Documentation

◆ MonteCarloObj()

ObjCryst::MonteCarloObj::MonteCarloObj ( const bool  internalUseOnly)

Constructor.

Using internalUseOnly=true will avoid registering the the object to any registry, and thus (for example) no display will be created, nor will this object be automatically be saved.

Definition at line 624 of file GlobalOptimObj.cpp.

Member Function Documentation

◆ InitLSQ()

void ObjCryst::MonteCarloObj::InitLSQ ( const bool  useFullPowderPatternProfile = true)
virtual

Prepare mLSQ for least-squares refinement during the global optimization.

Parameters
useFullPowderPatternProfileif true, the refinement will use the full profile version of powder patterns, otherwise only the integrated powder pattern will be used (faster).

Definition at line 2293 of file GlobalOptimObj.cpp.

◆ MultiRunOptimize()

void ObjCryst::MonteCarloObj::MultiRunOptimize ( long &  nbCycle,
long &  nbSteps,
const bool  silent = false,
const REAL  finalcost = 0,
const REAL  maxTime = -1 
)
virtual

Launch optimization for multiple runs of N steps.

Parameters
nbCyclethe number of runs (cycles) to perform. The structure is randomized at the beginning of each cycle. If nbCycle==-1, this will run indefinitely. The nbCycle parameter is decreased after each run.
nbStepsthe number of steps to go. This number is modified (decreases!) as the refinement goes on.
silent: if true, absolutely no message should be printed (except debugging)
finalcostthe optimization will stop if overall cost fallse below this value
maxTimethe optimization will stop after the given number of seconds has been spent optimizing (ignored if <0).

Implements ObjCryst::OptimizationObj.

Definition at line 771 of file GlobalOptimObj.cpp.

◆ NewConfiguration()

void ObjCryst::MonteCarloObj::NewConfiguration ( const RefParType type = gpRefParTypeObjCryst)
protectedvirtual

Make a random change in the configuration.

This just generates a new configuration with random changes (according to current parameters). The new configuration is not tested in this function vs temperature: this should be done in the OptimizationObj::Optimize() function, which also chooses whether to revert to the previous configuration.

Random moves are made by the objects and not by this function, because the new configuration can be specific (like, for example, permutations between some of the parameters (atoms)).

Parameters
typecan be used to restrict the move to a given category of parameters.

Definition at line 2223 of file GlobalOptimObj.cpp.

◆ Optimize()

void ObjCryst::MonteCarloObj::Optimize ( long &  nbSteps,
const bool  silent = false,
const REAL  finalcost = 0,
const REAL  maxTime = -1 
)
virtual

Launch optimization (a single run) for N steps.

Parameters
nbStepsthe number of steps to go. This number is modified (decreases!) as the refinement goes on.
silent: if true, absolutely no message should be printed (except debugging)
finalcostthe optimization will stop if overall cost fallse below this value
maxTimethe optimization will stop after the given number of seconds has been spent optimizing (ignored if <0).

Implements ObjCryst::OptimizationObj.

Definition at line 690 of file GlobalOptimObj.cpp.

◆ RunParallelTempering()

void ObjCryst::MonteCarloObj::RunParallelTempering ( long &  nbSteps,
const bool  silent = false,
const REAL  finalcost = 0,
const REAL  maxTime = -1 
)

Do a single Parallel Tempering run. This is called by Optimize(...) and MultiRunOptimize(), which must also prepare the optimization (PrepareRefParList(), etc..).

Definition at line 1346 of file GlobalOptimObj.cpp.

◆ RunSimulatedAnnealing()

void ObjCryst::MonteCarloObj::RunSimulatedAnnealing ( long &  nbSteps,
const bool  silent = false,
const REAL  finalcost = 0,
const REAL  maxTime = -1 
)

Do a single simulated annealing run. This is called by Optimize(...) and MultiRunOptimize(), which must also prepare the optimization (PrepareRefParList(), etc..).

Definition at line 915 of file GlobalOptimObj.cpp.

◆ SetAlgorithmParallTempering()

void ObjCryst::MonteCarloObj::SetAlgorithmParallTempering ( const AnnealingSchedule  scheduleTemp,
const REAL  tMax,
const REAL  tMin,
const AnnealingSchedule  scheduleMutation = ANNEALING_CONSTANT,
const REAL  mutMax = 16.,
const REAL  mutMin = .125 
)

Set the refinement method to Parallel Tempering.

The refinement begins at max and finishes at min temperature.

Parameters
scheduleTemptemperature schedule
tMax,tMinMax and Min temperatures. See AnnealingSchedule.
scheduleMutationthe mutation schedule. For each new configuration, the variation of each refinable parameter is less than its RefinablePar::GlobalOptimStep(), multiplied by the current mutation amplitude. By default this mutation is equal to 1., but making bigger steps can be a good idea at the beginning of the refinement. Thus you can choose a schedule for the amplitude, exactly like for the temperature.
mutMax,mutMinMax and Min mutation amplitudes.
Warning
do not use the 'smart' option for the temperature schedule, it is not yet implemented. Later it will be used to set the temperatures as a function of the amplitude schedule, so that we keep accepted move between 30% and 70%.
Note
this will be removed when we separate the different algorithms in different classes.

Definition at line 672 of file GlobalOptimObj.cpp.

◆ SetAlgorithmSimulAnnealing()

void ObjCryst::MonteCarloObj::SetAlgorithmSimulAnnealing ( const AnnealingSchedule  scheduleTemp,
const REAL  tMax,
const REAL  tMin,
const AnnealingSchedule  scheduleMutation = ANNEALING_CONSTANT,
const REAL  mutMax = 16.,
const REAL  mutMin = .125,
const long  nbTrialRetry = 0,
const REAL  minCostRetry = 0. 
)

Set the refinement method to simulated Annealing.

Note that Parellel Tempering is more efficient to get out of local minima, so you sould rather use that method.

The refinement begins at max and finishes at min temperature.

Parameters
scheduleTemptemperature schedule. See AnnealingSchedule.
tMax,tMinMax and Min temperatures.
scheduleMutationthe mutation schedule. For each new configuration, the variation of each refinable parameter is less than its RefinablePar::GlobalOptimStep(), multiplied by the current mutation amplitude. By default this mutation is equal to 1., but making bigger steps is a good idea at the beginning of the refinement (for higher temperatures). See AnnealingSchedule. See AnnealingSchedule.
mutMax,mutMinMax and Min mutation amplitudes.
minCostRetry,nbTrialRetryif after nbTrialRetry, the cost function is still above minCostRetry, then start again from a random configuration. No randomization is made if nbTrialRetry <= 0.
maxNbTrialSinceBestif more than maxNbTrialSinceBest trials have been made since the best configuration was recorded, then revert to that configuration. This should be large enough to have an ergodic search (the default is never to revert..)
Warning
do not use the 'smart' option for the temperature schedule, it is not yet implemented. Later it will be used to set the temperatures as a function of the amplitude schedule, so that we accept between 30% and 70% moves.
Note
this will be removed when we separate the different algorithms in different classes.

Definition at line 651 of file GlobalOptimObj.cpp.

◆ UpdateDisplay()

void ObjCryst::MonteCarloObj::UpdateDisplay ( ) const
virtual

Update Display (if any display is available), when a new 'relevant' configuration is reached.

This calls all RefinableObj::UpdateDisplay()

Reimplemented from ObjCryst::OptimizationObj.

Definition at line 2323 of file GlobalOptimObj.cpp.

◆ XMLInput()

void ObjCryst::MonteCarloObj::XMLInput ( istream &  is,
const XMLCrystTag tag 
)
virtual

Input in XML format from a stream, restoring the set of refined objects and the associated cost functions.

Note that the corresponding objects must have been loaded in memory before, else shit happens.

Implements ObjCryst::OptimizationObj.

Definition at line 2129 of file GlobalOptimObj.cpp.

◆ XMLOutput()

void ObjCryst::MonteCarloObj::XMLOutput ( ostream &  os,
int  indent = 0 
) const
virtual

Output a description of the object in XML format to a stream.

This saves the list of refined object and the cost functions, as well as options for the refinement. The refined objects are not saved, so this must be done somewhere else (they must be reloaded before this object).

Implements ObjCryst::OptimizationObj.

Definition at line 2068 of file GlobalOptimObj.cpp.

Member Data Documentation

◆ mGlobalOptimType

RefObjOpt ObjCryst::MonteCarloObj::mGlobalOptimType
protected

Method used for the global optimization.

Should be removed when we switch to using several classes for different algorithms.

Definition at line 516 of file GlobalOptimObj.h.

◆ mMutationAmplitude

REAL ObjCryst::MonteCarloObj::mMutationAmplitude
protected

Mutation amplitude.

From .25 to 64. Random moves will have a maximum amplitude equal to this amplitude multiplied by the Global optimization step defined for each RefinablePar. Large amplitude should be used at the beginning of the refinement (high temeratures).

Definition at line 543 of file GlobalOptimObj.h.

◆ mNbTrialRetry

long ObjCryst::MonteCarloObj::mNbTrialRetry
protected

Number of trials before testing if we are below the given minimum cost.

If <=0, this will be ignored.

Definition at line 555 of file GlobalOptimObj.h.


The documentation for this class was generated from the following files: