25 #ifndef _VFN_REFINABLE_OBJ_H_ 
   26 #define _VFN_REFINABLE_OBJ_H_ 
   34 #include "ObjCryst/CrystVector/CrystVector.h" 
   35 #include "ObjCryst/ObjCryst/General.h" 
   36 #include "ObjCryst/RefinableObj/IO.h" 
   42    template<
class T> 
class ObjRegistry;
 
   44    template<
class T> 
class RefObjOption;
 
   47 #include "ObjCryst/wxCryst/wxRefinableObj.h" 
   56    REFPAR_DERIV_STEP_ABSOLUTE,
 
   57    REFPAR_DERIV_STEP_RELATIVE
 
  114             gpRefParTypeObjCryst=
new RefParType(
"ObjCryst++");
 
  122             gpRefParTypeObjCryst=0;
 
  172       unsigned long mTick0, mTick1;
 
  173       static unsigned long msTick0,msTick1;
 
  259                      const bool hasLimits=
true,
 
  260                      const bool isFixed=
false,
 
  261                      const bool isUsed=
true,
 
  262                      const bool isPeriodic=
false,
 
  263                      const REAL humanScale=1.,
 
  291       void Init(     
const string &name,
 
  297                      const bool hasLimits=
true,
 
  298                      const bool isFixed=
false,
 
  299                      const bool isUsed=
true,
 
  300                      const bool isPeriodic=
false,
 
  301                      const REAL humanScale=1.,
 
  345          void Mutate(
const REAL mutateValue);
 
  354          REAL GetSigma()
const;
 
  355          REAL GetHumanSigma()
const;
 
  356          void SetSigma(
const REAL);
 
  369          void SetIsFixed(
const bool);
 
  371          bool IsLimited()
const;
 
  372          void SetIsLimited(
const bool);
 
  379          bool IsPeriodic()
const;
 
  380          void SetIsPeriodic(
const bool,REAL period=1);
 
  434          void SetUseEquation(
const bool useItOrNot,
const REAL c0=0.);
 
  435          void SetUseEquation(
const bool useItOrNot,
const REAL c0,
 
  438          void SetUseEquation(
const bool useItOrNot,
const REAL c0,
 
  441          void SetUseEquation(
const bool useItOrNot,
const REAL c0,
 
  473       void XMLOutput(ostream &os,
const string &name,
int indent=0)
const;
 
  479       void XMLOutput(ostream &os,
int indent=0)
const;
 
  522          static const int mEquationMaxRefPar=10;
 
  524          int mEquationNbRefPar;
 
  526          CrystVector_REAL mEquationCoeff;
 
  541       void             WXNotifyDelete();
 
  558       void Init(
const int nbChoice,
const string *name,
 
  559                 const string *choiceNames);
 
  560       int GetNbChoice()
const;
 
  561       int GetChoice()
const;
 
  562       virtual void SetChoice(
const int choice);
 
  563       void SetChoice(
const string &choiceName);
 
  564       const string& GetName()
const;
 
  565       const string& GetClassName()
const;
 
  566       const string& GetChoiceName(
const int i)
const;
 
  572       void XMLOutput(ostream &os,
int indent=0)
const;
 
  595       void             WXNotifyDelete();
 
  614       void Init(
const int nbChoice,
const string *name,
 
  615                 const string *choiceNames,
 
  616                 void (T::*fp)(
const int));
 
  617       virtual void SetChoice(
const int choice);
 
  664       T& 
GetObj(
const unsigned int i);
 
  670       const T& 
GetObj(
const unsigned int i)
const;
 
  673       T& 
GetObj(
const string &objName);
 
  676       const T& 
GetObj(
const string &objName)
const;
 
  680       T& 
GetObj(
const string &objName, 
const string& className);
 
  684       const T& 
GetObj(
const string &objName, 
const string& className)
const;
 
  689       void SetName(
const string &);
 
  690       const string& GetName()
const;
 
  693       long Find(
const string &objName)
const;
 
  699       long Find(
const string &objName, 
const string& className,
 
  700                 const bool nothrow=
false)
const;
 
  703       long Find(
const T &obj)
const;
 
  706       long Find(
const T *pobj)
const;
 
  737       void             WXNotifyDelete();
 
  773       virtual const string& 
GetName() 
const;
 
  775       virtual void SetName(
const string &name);
 
  794       void SetParIsUsed(
const string& parName,
const bool use);
 
  867       virtual void Print() 
const;
 
  898       const CrystVector_REAL& 
GetParamSet(
const unsigned long setId)
const;
 
  903       CrystVector_REAL& 
GetParamSet(
const unsigned long setId);
 
  984                                      const bool enableRestraints=
false);
 
 1029                                        const RefParType *type=gpRefParTypeObjCryst);
 
 1077          virtual const CrystVector_REAL& 
GetLSQCalc(
const unsigned int) 
const;
 
 1079          virtual const CrystVector_REAL& 
GetLSQObs(
const unsigned int) 
const;
 
 1081          virtual const CrystVector_REAL& 
GetLSQWeight(
const unsigned int) 
const;
 
 1106          virtual std::map<RefinablePar*, CrystVector_REAL> & 
GetLSQ_FullDeriv(
const unsigned int,std::set<RefinablePar *> &vPar);
 
 1117       virtual void XMLOutput(ostream &os,
int indent=0)
const;
 
 1164                                    CrystVector_uint & groupIndex,
 
 1165                                    unsigned int &firstGroup) 
const;
 
 1206       long FindPar(
const string &name) 
const;
 
 1208       long FindPar(
const REAL*) 
const;
 
 1221       map<unsigned long,pair<CrystVector_REAL,string> >::iterator 
FindParamSet(
unsigned long id)
const;
 
 1282       mutable std::map< unsigned int,std::map<RefinablePar*, CrystVector_REAL> > 
mLSQ_FullDeriv;
 
 1292    #ifdef __WX__CRYST__ 
 1298       void WXNotifyDelete();
 
 1319 #endif// _VFN_REFINABLE_OBJ_H_ 
void UpdateUI()
Manually update the UI, making sure all objects in the registry are displayed This is useful when the...
REAL mGlobalOptimStep
Step to use for global method search (simulated annealing,...) 
void SetDerivStep(const REAL)
Fixed step to use to compute numerical derivative. 
T & GetObj(const unsigned int i)
Get object #i in the registry. 
REAL GetHumanMin() const 
Get the minimum value allowed (if limited) 
void Print() const 
Print clock value. Only for debugging purposes. 
virtual REAL GetRestraintCost() const 
Get the restraint cost (overall penalty of all restraints) 
void AddSubRefObj(RefinableObj &)
CrystVector_long mRefparNotFixedIndex
Index of not-fixed parameters. 
void AddPar(const RefinablePar &newRefPar)
Add a refinable parameter. 
virtual const ObjRegistry< RefinableObj > & GetClientRegistry() const 
Get the list of clients. 
RefinableObjClock mRefParListClock
Last time the RefinableParList was modified (a parameter added or removed). 
virtual void UpdateDisplay() const 
If there is an interface, this should be automatically be called each time there is a 'new...
REAL GetMin() const 
Minimum value allowed (if limited or periodic) 
void SetLimitsRelative(const string &parName, const REAL min, const REAL max)
Change the limits for a given parameter, giving relative new limits (eg giving -.1 and +...
bool mIsFixed
is the parameter currently fixed ? 
const RefinableObjClock & GetRegistryClock() const 
Last time an object was added or removed from the registry. 
void SetParIsUsed(const string &parName, const bool use)
Set whether a parameter is used. 
WX representation of a RefObj option. This displays the names of the different choices. 
void AutoUpdateUI(const bool autoup=true)
Enable the UI automatic update, so that objects in the registry are automatically added to the UI...
long GetNb() const 
Get the index of an object in the registry, from its name Warning: it can change if an object is remo...
string GetName() const 
Get the parameter's name. 
void Register(T &obj)
Register a new object. Already registered objects are skipped. 
We need to record exactly when refinable objects have been modified for the last time (to avoid re-co...
bool IsBeingRefined() const 
Is the object being refined ? (Can be refined by one algorithm at a time only.) 
virtual void GlobalOptRandomMove(const REAL mutationAmplitude, const RefParType *type=gpRefParTypeObjCryst)
Make a random move of the current configuration. 
REAL * mpValue
Pointer to the refinable value. 
const REAL * GetPointer() const 
Access to a const pointer to the refined value. 
void FixAllPar()
Fix All parameters. 
virtual void BeginOptimization(const bool allowApproximations=false, const bool enableRestraints=false)
This should be called by any optimization class at the begining of an optimization. 
const string * mpName
(short) Name for this option. 
void SaveParamSet(const unsigned long id) const 
Save the current set of refined values over a previously-created set of saved values. 
void RemoveParent(RefinableObjClock &) const 
remove a parent clock 
A field for a RefinablePar. 
const string & GetName() const 
Get the name for this parameter. 
map< unsigned long, pair< CrystVector_REAL, string > >::iterator FindParamSet(unsigned long id) const 
Find a parameter set with a given id (and check if it is there) 
vector< T * > mvpRegistry
The registry of objects. 
void SetParIsFixed(const long parIndex, const bool fix)
Fix/un-fix one parameter from its #. 
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...
long Find(const string &objName) const 
Find the number of an object in the registry from its name (slow !) The search starts at the end of t...
RefParType(const string &name)
Create a top parameter type. 
void RefObjRegisterRecursive(T &obj, ObjRegistry< T > ®)
Register a new object in a registry, and recursively include all included (sub)objects. 
void Click()
Record an event for this clock (generally, the 'time' an object has been modified, or some computation has been made) 
vector< Restraint * >::iterator RemoveRestraint(Restraint *pRestraint)
Remove a restraint from the list of known restraints. 
RefinableObjClock mClock
The clock associated to this option. 
void RemoveSubRefObj(RefinableObj &)
long GetNbPar() const 
Total number of refinable parameter in the object. 
void AddChild(const RefinableObjClock &)
Add a 'child' clock. 
long mNbRefParNotFixed
Total of not-fixed parameters. 
Class for options of RefinableObj, templated so that we can warn the object that something has been c...
const CrystVector_REAL & GetParamSet(const unsigned long setId) const 
Access one save refpar set. 
RefinablePar & GetParNotFixed(const long i)
Access all parameters in the order they were inputted, skipping fixed parameters. ...
void SetLimitsRelative(const REAL min, const REAL max)
Change the limits for this object, giving relative new limits (eg giving -.1 and +.1 will set new limits at the current value + min and current value + max) Thus min should logically be <0 and max >0. 
void XMLInput(istream &is, const XMLCrystTag &tag)
XMLInput From stream. 
long GetNbParNotFixed() const 
Total number of non-fixed parameters. Is initialized by PrepareForRefinement() 
RefinablePar & GetPar(const long i)
Access all parameters in the order they were inputted. 
std::set< RefinableObjClock * > mvParent
List of parent clocks, which will be clicked whenever this one is. 
unsigned long mId
The unique number identifying this type. 
void SetIsUsed(const bool)
Is the parameter used (if not, it is simply irrelevant in the model) ? 
void PrintStatic() const 
Print current general clock value. Only for debugging purposes. 
void XMLInput(istream &is, const XMLCrystTag &tag)
XMLInput From stream. 
REAL mDerivStep
Step to use for numerical derivative calculation. 
void InitId()
Get a Unique id (RefParType::mId) 
void(T::* mfpSetNewValue)(const int)
The pointer to the member function to be used when the choice is changed, to notify immediately the o...
void BeginGlobalOptRandomMove()
Raise a flag, to be sure not to make a random change more than once in each RefinableObj. 
void Mutate(const REAL mutateValue)
Add the given amount to the parameter current value. 
virtual void RegisterClient(RefinableObj &) const 
Register a new object using this object. 
This displays all components of a ObjCryst++ Registry. 
virtual REAL GetLogLikelihood() const 
Get -ln(likelihood) for this restraint. 
virtual void GetGeneGroup(const RefinableObj &obj, CrystVector_uint &groupIndex, unsigned int &firstGroup) const 
Get the gene group assigned to each parameter. 
void SetLimitsProportional(const string &parName, const REAL min, const REAL max)
Change the limits for a given parameter, proportionnaly to the current value. 
ObjRegistry< RefinableObj > gTopRefinableObjRegistry("Global Top RefinableObj registry")
This is a special registry for 'top' object for an optimization. 
RefinableObjClock mClockMaster
Master clock, which is changed whenever the object has been altered. 
virtual void SetApproximationFlag(const bool allow)
Enable or disable numerical approximations. 
unsigned int GetNbOption() const 
Number of Options for this object. 
const REAL & GetHumanValue() const 
Current value of parameter, scaled if necessary (for angles) to a human-understandable value...
RefObjOpt & GetOption(const unsigned int i)
Access to the options. 
RefParDerivStepModel
How do we compute steps h for numerical derivative calculation : d=f(x+h)-f(x-h)/h/2 either h is fixe...
REAL mHumanScale
Scale to be used to display 'human' value. 
Generic Refinable Object. 
void SetMin(const REAL)
Set the Minimum value allowed (if limited) 
REAL mPeriod
Period value (if relevant) 
void SetLimitsProportional(const REAL min, const REAL max)
Change the limits for this object, proportionnaly to the current value. 
void ResetParList()
Re-init the list of refinable parameters, removing all parameters. 
const RefinableObjClock & GetClockMaster() const 
This clocks records any change in the object. See refinableObj::mClockMaster. 
virtual std::map< RefinablePar *, CrystVector_REAL > & GetLSQ_FullDeriv(const unsigned int, std::set< RefinablePar * > &vPar)
Get the first derivative for the LSQ function for each parameter supplied in a list. 
ObjRegistry< RefinableObj > gRefinableObjRegistry("Global RefinableObj registry")
Global Registry for all RefinableObj. 
RefObjOption(T *obj)
Constructor for the option. 
Abstract base class for all objects in wxCryst. 
map< unsigned long, pair< CrystVector_REAL, string > > mvpSavedValuesSet
Map of (index,pointers to arrays) used to save sets of values for all parameters. ...
void GetRefParListClockRecursive(ObjRegistry< RefinableObj > ®, RefinableObjClock &clock)
Get the last time any RefinablePar was added in a recursive list of objects. 
string mName
Name for this RefinableObject. Should be unique, at least in the same scope.+. 
void PrepareForRefinement() const 
Find which parameters are used and not fixed, for a refinement /optimization. 
string mName
Name of this registry. 
virtual const CrystVector_REAL & GetLSQDeriv(const unsigned int, RefinablePar &)
Get the first derivative values for the LSQ function, for a given parameter. 
bool mHasLimits
Does the refinable parameter need limits (min and max) ? 
const void EraseAllParamSet()
Erase all saved refpar sets. 
const string * mpChoiceName
Names corresponding to each possible value of this option (Human-understandable). ...
bool mDeleteRefParInDestructor
If true (the default), then all RefinablePar will be deleted when the the object is deleted...
void RestoreParamSet(const unsigned long id)
Restore a saved set of values. 
void AssignClock(RefinableObjClock &clock)
REAL GetMax() const 
Get the maximum value allowed (if limited) 
vector< Restraint * > mvpRestraint
Vector of pointers to the restraints for this object. 
void XMLOutput(ostream &os, int indent=0) const 
XMLOutput to stream in well-formed XML. 
REAL GetPeriod() const 
Get the period (if periodic) 
void RemoveChild(const RefinableObjClock &)
remove a child clock. This also tells the child clock to remove the parent. 
void operator=(const RefinableObj &old)
Defined not implemented... 
RefParDerivStepModel mRefParDerivStepModel
Model followed for derivation. 
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. 
RefinablePar()
Default Constructor. 
bool mIsPeriodic
Is the parameter periodic ? If this is the case, then when using the RefinablePar::Mutate() function...
virtual const CrystVector_REAL & GetLSQCalc(const unsigned int) const 
Get the current calculated value for the LSQ function. 
const string & GetParamSetName(const unsigned long setId) const 
Get the name associated to a refpar set. 
const RefinableObjClock & GetRefParListClock() const 
What was the last time a RefinablePar was added/removed ? 
void DeRegister(T &obj)
De-register an object. 
void Click()
Click the Clock ! to telle the RefinableObj it has been modified. 
void AddParent(RefinableObjClock &) const 
Add a 'parent' clock. 
ObjRegistry< RefinableObj > & GetSubObjRegistry()
Access to the registry of RefinableObj used by this object. 
void CopyAttributes(const RefinablePar &)
Copy all attributes (limits, flags, etc...) from another RefinablePar object. 
bool mAutoUpdateUI
Enable the user interface update. 
virtual const string & GetClassName() const 
Name for this class ("RefinableObj", "Crystal",...). 
bool mIsUsed
Is the parameter currently used ? 
void DeleteAll()
Delete all objects in the registry.. Use with caution !! 
virtual void RandomizeConfiguration()
Randomize Configuration (before a global optimization). 
ObjRegistry< RefinableObj > mClientObjRegistry
Registry of RefinableObject using this object. 
virtual void XMLOutput(ostream &os, int indent=0) const 
Output to stream in well-formed XML. 
int mNbChoice
Number of different choice possible for this option. 
RefinableObj()
Constructor. 
void SetHumanValue(const REAL &)
Current value of parameter, scaled if necessary (for angles) to a human-understandable value...
Restraint: generic class for a restraint of a given model. 
ObjRegistry< RefinableObj > mSubObjRegistry
Registry of RefinableObject needed for this object (owned by this object or not) 
CrystVector_REAL mLSQDeriv
Temporary array used to return derivative values of the LSQ function for given parameters. 
const string mName
The name/description for this parameter type. 
void MutateTo(const REAL newValue)
Change the current value to the given one. 
void ClearParamSet(const unsigned long id) const 
Erase the param set with the given id, releasing memory. 
virtual const CrystVector_REAL & GetLSQWeight(const unsigned int) const 
Get the weight values for the LSQ function. 
void SetValue(const REAL value)
of the parameter. 
REAL GetHumanScale() const 
Human scale for this parameter : for angles, this is equal to 180/pi. 
bool operator==(const RefParType *parent) const 
returns true if the two types are the same. 
bool IsDescendantFromOrSameAs(const RefParType *type) const 
Returns true if the parameter is a descendant of 'type'. 
void Reset()
Reset a Clock to 0, to force an update. 
REAL GetHumanMax() const 
Get the maximum value allowed (if limited) 
RefinableObjClock mListClock
Last time an object was added or removed. 
virtual void EndOptimization()
This should be called by any optimization class at the end of an optimization. 
long FindPar(const string &name) const 
Find a refinable parameter with a given name. 
virtual unsigned int GetNbLSQFunction() const 
Number of LSQ functions. 
The namespace which includes all objects (crystallographic and algorithmic) in ObjCryst++. 
void SetHumanMin(const REAL)
Set the minimum value allowed (if limited) 
void SetMax(const REAL)
Get the maximum value allowed (if limited) 
const RefParType * mpParent
the parent for this RefParType (we could easily allow several...) 
REAL GetDerivStep() const 
Fixed step to use to compute numerical derivative. 
virtual REAL GetLogLikelihood() const 
Get -log(likelihood) of the current configuration for the object. 
Generic class for parameters of refinable objects. 
T * mpObj
The object which uses this option. 
RefObjOpt()
Constructor for the option. 
virtual const string & GetName() const 
Name of the object. 
REAL GetGlobalOptimStep() const 
Maximum step to use during Global Optimization algorithms. 
bool mHasAssignedClock
Is there a clock associated with this parameter ? If yes, then it must Click() it each time it is mod...
string mName
name of the refinable parameter 
void SetHumanMax(const REAL)
Get the maximum value allowed (if limited) 
void GetSubRefObjListClockRecursive(ObjRegistry< RefinableObj > ®, RefinableObjClock &clock)
Get the last time any object was added in the recursive list of objects. 
void AddRestraint(Restraint *pNewRestraint)
Add a new restraint. 
REAL GetParamSet_ParNotFixedHumanValue(const unsigned long setId, const long parNumber) const 
Access the (human) value of one refined parameter in a saved set of parameters. 
REAL GetValue() const 
of the parameter. 
vector< RefinablePar * >::iterator RemovePar(RefinablePar *refPar)
Remove a refinable parameter. 
Restraint()
Default constructor, sets RefParType to gpRefParTypeObjCryst. 
virtual const CrystVector_REAL & GetLSQObs(const unsigned int) const 
Get the observed values for the LSQ function. 
void SetGlobalOptimStep(const RefParType *type, const REAL step)
Change the maximum step to use during Global Optimization algorithms. 
void SetLimitsAbsolute(const string &parName, const REAL min, const REAL max)
Change the limits for a given parameter, giving absolute new limits. 
vector< RefinablePar * > mvpRefPar
Vector of pointers to the refinable parameters. 
class to input or output a well-formatted xml beginning or ending tag. 
ObjRegistry< RefObjOpt > mOptionRegistry
List of options for this object. 
int mChoice
Current value. 
void SetDeleteRefParInDestructor(const bool b)
Set this object not to delete its list of parameters when destroyed. 
void SetPeriod(const REAL)
Set the period value (if periodic) 
std::map< unsigned int, std::map< RefinablePar *, CrystVector_REAL > > mLSQ_FullDeriv
Temporary map to return the derivative of the LSQ function versus a list of parameters. 
void DeRegisterAll()
De-register all objects from the list. 
virtual void TagNewBestConfig() const 
During a global optimization, tells the object that the current config is the latest "best" config...
void SetHumanScale(const REAL)
Human scale for this parameter : for angles, this is equal to 180/pi. 
bool IsUsed() const 
Is the parameter used (if not, it is simply irrelevant in the model) ? 
class of refinable parameter types. 
virtual ~RefinableObj()
Destructor. 
virtual void DeRegisterClient(RefinableObj &) const 
Deregister an object (which not any more) using this object. 
unsigned long CreateParamSet(const string name="") const 
Save the current set of refined values in a new set. 
void SetName(const string &)
Set the name of the parameter. It should be unique in the RefinableObj. 
std::set< const RefinableObjClock * > mvChild
List of 'child' clocks, which will click this clock whenever they are clicked. 
int mOptimizationDepth
Is the object being refined or optimized ? if mOptimizationDepth=0, no optimization is taking place...
void UnFixAllPar()
UnFix All parameters. 
const RefParType * gpRefParTypeObjCryst
Top RefParType for the ObjCryst++ library. 
void AddOption(RefObjOpt *opt)
void SetGlobalOptimStep(const REAL)
Maximum step to use during Global Optimization algorithms. 
REAL mSigma
Calculated sigma on value. 
virtual void SetName(const string &name)
Name of the object. 
void SetLimitsAbsolute(const REAL min, const REAL max)
Change the limits for this object, giving absolute new limits. 
REAL mMin
Hard lower and upper limits. 
void XMLOutput(ostream &os, const string &name, int indent=0) const 
XMLOutput to stream in well-formed XML. 
virtual void XMLInput(istream &is, const XMLCrystTag &tag)
Input From stream.