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

(Quick & dirty) Least-Squares Refinement Object with Numerical derivatives More...

+ Collaboration diagram for ObjCryst::LSQNumObj:

Public Member Functions

 LSQNumObj (std::string objName="Unnamed LSQ object")
 
void SetParIsFixed (const std::string &parName, const bool fix)
 Fix one parameter. More...
 
void SetParIsFixed (const RefParType *type, const bool fix)
 Fix one family of parameters. More...
 
void SetParIsFixed (RefinablePar &par, const bool fix)
 Fix one parameter. More...
 
void SetParIsFixed (RefinableObj &obj, const bool fix)
 Fix all parameters within an object. More...
 
void UnFixAllPar ()
 UnFix All parameters. More...
 
void SetParIsUsed (const std::string &parName, const bool use)
 Set a parameter to be used. More...
 
void SetParIsUsed (const RefParType *type, const bool use)
 Set a family of parameters to be used. More...
 
void Refine (int nbCycle=1, bool useLevenbergMarquardt=false, const bool silent=false, const bool callBeginEndOptimization=true, const float minChi2var=0.01)
 Do the refinement. More...
 
bool SafeRefine (std::list< RefinablePar * > vnewpar, std::list< const RefParType * > vnewpartype, REAL maxChi2factor=1.01, int nbCycle=1, bool useLevenbergMarquardt=false, const bool silent=false, const bool callBeginEndOptimization=true, const float minChi2var=0.01)
 Run a refinement in a 'safe' way: if the Chi2 value increases by more that a given factor the parameters are reverted to their initial values. More...
 
CrystVector_REAL Sigma () const
 
CrystMatrix_REAL CorrelMatrix () const
 
void CalcRfactor () const
 
REAL Rfactor () const
 
void CalcRwFactor () const
 
REAL RwFactor () const
 
void CalcChiSquare () const
 
REAL ChiSquare () const
 
void SetRefinedObj (RefinableObj &obj, const unsigned int LSQFuncIndex=0, const bool init=true, const bool recursive=false)
 Choose the object to refine. More...
 
const std::map< RefinableObj *, unsigned int > & GetRefinedObjMap () const
 Get the map of refined objects - this is a recursive list of all the objects that are taken into account for the refinement. More...
 
std::map< RefinableObj *, unsigned int > & GetRefinedObjMap ()
 Get the map of refined objects - this is a recursive list of all the objects that are taken into account for the refinement. More...
 
RefinableObjGetCompiledRefinedObj ()
 Access to the RefinableObj which is the compilation of all parameters from the object supplied for optimization and its sub-objects. More...
 
const RefinableObjGetCompiledRefinedObj () const
 Access to the RefinableObj which is the compilation of all parameters from the object supplied for optimization and its sub-objects. More...
 
void SetUseSaveFileOnEachCycle (bool yesOrNo=true)
 
void SetSaveFile (std::string fileName="refine.save")
 
void PrintRefResults () const
 
void SetDampingFactor (const REAL newDampFact)
 
void PurgeSaveFile ()
 
void WriteReportToFile () const
 
void OptimizeDerivativeSteps ()
 
const std::map< pair< const RefinablePar *, const RefinablePar * >, REAL > & GetVarianceCovarianceMap () const
 
void PrepareRefParList (const bool copy_param=false)
 Prepare the full parameter list for the refinement. More...
 
const CrystVector_REAL & GetLSQCalc () const
 Get the LSQ calc vector (using either only the top or the hierarchy of object)
 
const CrystVector_REAL & GetLSQObs () const
 Get the LSQ obs vector (using either only the top or the hierarchy of object)
 
const CrystVector_REAL & GetLSQWeight () const
 Get the LSQ weight vector (using either only the top or the hierarchy of object)
 
const CrystVector_REAL & GetLSQDeriv (RefinablePar &par)
 Get the LSQ deriv vector (using either only the top or the hierarchy of object)
 
const std::map< RefinablePar *, CrystVector_REAL > & GetLSQ_FullDeriv ()
 
void BeginOptimization (const bool allowApproximations=false, const bool enableRestraints=false)
 Tell all refined object that the refinement is beginning.
 
void EndOptimization ()
 Tell all refined object that the refinement is finished.
 

Private Attributes

ObjRegistry< RefinableObjmRecursiveRefinedObjList
 The recursive list of all refined sub-objects.
 
RefinableObj mRefParList
 The refinable par list used during refinement. More...
 
REAL mDampingFactor
 Damping factor for the refinement (unused yet...)
 
bool mSaveReportOnEachCycle
 Save result to file after each cycle ?
 
std::string mName
 Name of the refined object.
 
std::string mSaveFileName
 File name where refinement info is saved.
 
REAL mR
 
REAL mRw
 
REAL mChiSq
 
CrystMatrix_REAL mCorrelMatrix
 Correlation matrix between all refined parameters.
 
std::map< pair< const RefinablePar *, const RefinablePar * >, REAL > mvVarCovar
 Variance-Covariance matrix, as a std::map.
 
CrystVector_REAL mObs
 Observed values.
 
CrystVector_REAL mWeight
 Weight corresponding to all observed values.
 
int mIndexValuesSetInitial
 Index of the set of saved values for all refinable parameters, before refinement and before the last cycle.
 
int mIndexValuesSetLast
 
bool mStopAfterCycle
 If true, then stop at the end of the cycle. Used in multi-threading environment.
 
std::map< RefinableObj *, unsigned int > mvRefinedObjMap
 Map of the recursive list of the objects to be refined. More...
 
std::map< RefinableObj *, unsigned int > mvRefinedObjLSQSize
 Size of each object LSQ data. This is initialized in LSQNumObj::GetLSQObs()
 
bool mCopyRefPar
 If true, then parameters to be refined will be copied instead of referenced. More...
 
CrystVector_REAL mLSQObs
 Temporary arrays for LSQ functions evaluation - used when using recursive LSQ function.
 
CrystVector_REAL mLSQCalc
 
CrystVector_REAL mLSQWeight
 
CrystVector_REAL mLSQDeriv
 
std::map< RefinablePar *, CrystVector_REAL > mLSQ_FullDeriv
 

Detailed Description

(Quick & dirty) Least-Squares Refinement Object with Numerical derivatives

This is still highly experimental !

Definition at line 38 of file LSQNumObj.h.

Member Function Documentation

◆ GetCompiledRefinedObj() [1/2]

RefinableObj & ObjCryst::LSQNumObj::GetCompiledRefinedObj ( )

Access to the RefinableObj which is the compilation of all parameters from the object supplied for optimization and its sub-objects.

Since this compilation is only updated from the suplied refinableobj and its sub-objects when SetRefinedObj() and PrepareRefParList() are called, it is possible to alter the fixed/limited status of parameters here without affecting the parameters in the refined objects.

Definition at line 848 of file LSQNumObj.cpp.

◆ GetCompiledRefinedObj() [2/2]

const RefinableObj & ObjCryst::LSQNumObj::GetCompiledRefinedObj ( ) const

Access to the RefinableObj which is the compilation of all parameters from the object supplied for optimization and its sub-objects.

Since this compilation is only updated from the suplied refinableobj and its sub-objects when SetRefinedObj() and PrepareRefParList() are called, it is possible to alter the fixed/limited status of parameters here without affecting the parameters in the refined objects.

Definition at line 850 of file LSQNumObj.cpp.

◆ GetRefinedObjMap() [1/2]

map< RefinableObj *, unsigned int > & ObjCryst::LSQNumObj::GetRefinedObjMap ( )

Get the map of refined objects - this is a recursive list of all the objects that are taken into account for the refinement.

The key is a pointer to the object and the value is the LSQ function index for that object.

Definition at line 842 of file LSQNumObj.cpp.

◆ GetRefinedObjMap() [2/2]

const map< RefinableObj *, unsigned int > & ObjCryst::LSQNumObj::GetRefinedObjMap ( ) const

Get the map of refined objects - this is a recursive list of all the objects that are taken into account for the refinement.

The key is a pointer to the object and the value is the LSQ function index for that object.

Definition at line 837 of file LSQNumObj.cpp.

◆ PrepareRefParList()

void ObjCryst::LSQNumObj::PrepareRefParList ( const bool  copy_param = false)

Prepare the full parameter list for the refinement.

Parameters
copy_paramif false (the default), then the lsq algorithm will work directly on the parameters of the refined object and sub-object. So that any modification to the fixed/used/limited status applies permanently to the parameters. if true, then the parameters are copied and therefore only the value of the parameter is changed (and the clocks are ticked).
Note
: if copy_param==true, then any modification to the parameters (fixed, limited, used status) only affects the copy and not the original. Also, calling again PrepareRefParList cancels any such modification.
This will be called automatically before starting the refinement only if the parameter list is empty. Otherwise it should be called before refinement.

Definition at line 913 of file LSQNumObj.cpp.

◆ Refine()

void ObjCryst::LSQNumObj::Refine ( int  nbCycle = 1,
bool  useLevenbergMarquardt = false,
const bool  silent = false,
const bool  callBeginEndOptimization = true,
const float  minChi2var = 0.01 
)

Do the refinement.

Parameters
nbCyclenumber of LSQ cycles - if negative, the algorithm will continue until it reaches (-nbcycle) or until the relative variation in Chi2 is less than minChi2var
useLevenbergMarquardtenable Levenberg-Marquardt algorithm to ensure that a decrease of Chi^2 will be obtained (actually a 1% increase is allowed)
callBeginEndOptimizationif true, will call RefinableObj::BeginOptimization(true,...) and RefinableObj::EndOptimization(). You may not want this if the LSQ is done during another (e.g. monte-carlo) optimization - but then the calling function must ensure that approximations are disabled (using RefinableObj::SetApproximationFlag) for objects where that would render derivative calculations imprecise.
minChi2varused for termination of the refinement if the relative variation of Chi2 between two successive cyles is less than minChi2var

Applying new computed values :TODO: & Check if a limit has been hit

Definition at line 106 of file LSQNumObj.cpp.

◆ SafeRefine()

bool ObjCryst::LSQNumObj::SafeRefine ( std::list< RefinablePar * >  vnewpar,
std::list< const RefParType * >  vnewpartype,
REAL  maxChi2factor = 1.01,
int  nbCycle = 1,
bool  useLevenbergMarquardt = false,
const bool  silent = false,
const bool  callBeginEndOptimization = true,
const float  minChi2var = 0.01 
)

Run a refinement in a 'safe' way: if the Chi2 value increases by more that a given factor the parameters are reverted to their initial values.

Moreover, the listed 'new' parameters or parameter types are then fixed.

Parameters
vnewparlist of parameters added for this optimization, which will be unfixed at the beginning, and will be fixed at the end if Chi2 increases. This can be empty.
vnewpartypelist of parameter types, which will be unfixed at the beginning, and will be fixed at the end if Chi2 increases. This can be empty.
maxChi2factorif Chi2_end> maxChi2factor * Chi2_begin, parameters are reverted to their initial value, and new parameters are fixed. All the other parameters are the same as in LSQNumObj::Refine.
Returns
: true if the minimization was succesful, false otherwise.

Definition at line 706 of file LSQNumObj.cpp.

◆ SetParIsFixed() [1/4]

void ObjCryst::LSQNumObj::SetParIsFixed ( const RefParType type,
const bool  fix 
)

Fix one family of parameters.

LSQNumObj::PrepareRefParList() must be called first!

Definition at line 69 of file LSQNumObj.cpp.

◆ SetParIsFixed() [2/4]

void ObjCryst::LSQNumObj::SetParIsFixed ( const std::string &  parName,
const bool  fix 
)

Fix one parameter.

LSQNumObj::PrepareRefParList() must be called first!

◆ SetParIsFixed() [3/4]

void ObjCryst::LSQNumObj::SetParIsFixed ( RefinableObj obj,
const bool  fix 
)

Fix all parameters within an object.

Note that this will fix the copied parameters, not the one in the original objects.

LSQNumObj::PrepareRefParList() must be called first!

Definition at line 81 of file LSQNumObj.cpp.

◆ SetParIsFixed() [4/4]

void ObjCryst::LSQNumObj::SetParIsFixed ( RefinablePar par,
const bool  fix 
)

Fix one parameter.

Note that this will fix the copied parameter, not the one in the original object. The supplied RefinablePar may be either the copied one or the original.

LSQNumObj::PrepareRefParList() must be called first!

Definition at line 75 of file LSQNumObj.cpp.

◆ SetParIsUsed() [1/2]

void ObjCryst::LSQNumObj::SetParIsUsed ( const RefParType type,
const bool  use 
)

Set a family of parameters to be used.

LSQNumObj::PrepareRefParList() must be called first!

Definition at line 100 of file LSQNumObj.cpp.

◆ SetParIsUsed() [2/2]

void ObjCryst::LSQNumObj::SetParIsUsed ( const std::string &  parName,
const bool  use 
)

Set a parameter to be used.

LSQNumObj::PrepareRefParList() must be called first!

◆ SetRefinedObj()

void ObjCryst::LSQNumObj::SetRefinedObj ( RefinableObj obj,
const unsigned int  LSQFuncIndex = 0,
const bool  init = true,
const bool  recursive = false 
)

Choose the object to refine.

The minimization will be done against its LSQ function and its parameters, as well as the LSQ functions and parameters of its sub-objects (if recursive==true)

Parameters
LSQFuncIndexone object can have a choice of several LSQ functions to minimize- this allows to choose which one to minimize.
initif true, the list of refined objects is first cleared. otherwise the new object (and its sub-objects) is just added to the list.
recursiveif false, only the supplied object is added, and not its sub-objects

Definition at line 824 of file LSQNumObj.cpp.

◆ UnFixAllPar()

void ObjCryst::LSQNumObj::UnFixAllPar ( )

UnFix All parameters.

LSQNumObj::PrepareRefParList() must be called first!

Definition at line 89 of file LSQNumObj.cpp.

Member Data Documentation

◆ mCopyRefPar

bool ObjCryst::LSQNumObj::mCopyRefPar
private

If true, then parameters to be refined will be copied instead of referenced.

Therefore only their values and the parameter's clocks are affected when working on the copy.

Definition at line 263 of file LSQNumObj.h.

◆ mRefParList

RefinableObj ObjCryst::LSQNumObj::mRefParList
mutableprivate

The refinable par list used during refinement.

It is only a compilation of the parameters in RefinableObj and its sub-objects

This list is only updated from the suplied refinableobj and its sub-objects when SetRefinedObj() and PrepareRefParList() are called, so it is possible to alter the fixed/limited status of parameters here without affecting the parameters in the refined objects.

Definition at line 223 of file LSQNumObj.h.

◆ mvRefinedObjMap

std::map<RefinableObj*,unsigned int> ObjCryst::LSQNumObj::mvRefinedObjMap
private

Map of the recursive list of the objects to be refined.

The key is the pointer to the object and the value the LSQ function index

Individual LSQ functions can be changed using GetRefinedObjMap().

Definition at line 256 of file LSQNumObj.h.


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