1 // $Id: forcesummary.h,v 1.33 2011/03/07 06:08:49 bobgian Exp $
2 
3 /*
4   Copyright 2002  Peter Beerli, Mary Kuhner, Jon Yamato and Joseph Felsenstein
5 
6   This software is distributed free of charge for non-commercial use
7   and is copyrighted.  Of course, we do not guarantee that the software
8   works, and are not responsible for any damage you may cause or have.
9 */
10 
11 /********************************************************************
12  ForceSummary is the governing object for polymorphism along force lines.
13  It creates the basic tree and manages other force-specific objects.
14 
15  It is not polymorphic along force lines, but it contains forcevec, a
16  vector of pointers to Force objects, which are polymorphic and
17  can be used as a run-down of which forces are in effect in the program.
18  This information is used in maximization and in output reporting.
19 
20  ForceSummary owns the things that its forcevec points to, and will
21  delete them in its destructor.  No one else deletes these objects!
22  When a ForceSummary is copied, a new set of Force objects is
23  created for it to point to.
24 
25  ForceSummary is also the factory for TimeManager objects (see force.h).
26 
27  Written by Jim Sloan, heavily revised by Mary Kuhner
28 
29 Jon Yamato:
30  added SummarizeData(datapack)
31 Peter Beerli:
32  added parameterlist class variable and
33  several Getters and Setters for it (8/30/01)
34 Mary Kuhner:
35  took them out again, created the ParamVector class to remove
36  the need for them (October 2001)
37 ********************************************************************/
38 
39 #ifndef FORCESUMMARY_H
40 #define FORCESUMMARY_H
41 
42 #include <cassert>  // May be needed for inline definitions.
43 #include <map>
44 #include <stdlib.h>
45 #include <string>
46 #include <vector>
47 
48 #include "constants.h"
49 #include "defaults.h"
50 #include "forceparam.h"
51 #include "types.h"
52 #include "vectorx.h"
53 
54 //------------------------------------------------------------------------------------
55 
56 class Random;
57 class Force;
58 class Tree;
59 class TreeSummary;
60 class ChainPack;
61 class DataPack;
62 class Parameter;
63 class ParamVector;
64 class ChainOut;
65 class Summary;
66 class UIVars;
67 class StickForce;
68 class Event;    // for CreateEventVec(), we're the callthrough factory
69 class TimeManager;
70 
71 //------------------------------------------------------------------------------------
72 
73 class ForceSummary
74 {
75   private:
76 
77     ForceVec m_forcevec;  // contains one instance of each active force
78 
79     ForceParameters m_startParameters;// Starting force parameter values.
80 
81     DoubleVec1d m_llikemles;          // likelihoods at the MLEs per region.
82     double      m_overallLlikemle;
83 
84     // A vector of indices for the groups which are constrained to be identical
85     ULongVec2d m_identicalGroups;
86 
87     // A vector of indices for the groups which are constrained to
88     // be multiplicative
89     ULongVec2d m_multiplicativeGroups;
90 
91     // The multiplier of each group in the same order as
92     // m_multiplicativeGroups.
93     DoubleVec1d m_multgroupmultiplier;
94 
95     // mechanism for accessing the Parameters as a linearized vector using
96     // a check-in/check-out scheme
97     friend class ParamVector;
98     std::vector<Parameter>   GetAllParameters();
99     void                     SetAllParameters(const std::vector<Parameter>& src);
100 
101     // There should only be one ForceSummary object
102     ForceSummary(const ForceSummary& src);              // undefined
103     ForceSummary& operator=(const ForceSummary& src);   // undefined
104 
105 
106     ULongVec1d GetParamsIdenticalGroupedWith(unsigned long pindex) const;
107     ULongVec1d
108     GetParamsMultiplicativelyGroupedWith(unsigned long pindex) const;
109     double     GetGroupMultiplier(unsigned long pindex) const;
110 
111   public:
112 
113     ForceSummary(ForceVec fvec, ForceParameters fparams, DataPack& dpac);
114     ~ForceSummary();
115 
116     // is this force active?
117     bool          CheckForce(force_type tag)   const;
118 
119     // Posterior output setup
120     void          SetupPosteriors(const ChainPack& chpack,
121                                   const DataPack& dpack) const;
122 
123     // Factory Functions
124     Tree*          CreateProtoTree() const;
125     TreeSummary*   CreateProtoTreeSummary() const;
126     vector<Event*> CreateEventVec() const;  // used by ResimArranger::ctor
127 
128     // Many Set Functions removed because now that we have separation between front and back end,
129     // we don't have to turn any forces on or off after the forcesummary is created.
130     // ewalkup 8/17/2004
131     void SetRegionMLEs(const ChainOut& chout, long region);
132     void SetOverallMLE(const ChainOut& chout);
133 
134     bool  IsMissingForce(force_type tag) const;
135     bool  AnyForcesOtherThanGrowCoal() const;
136     bool  UsingStick() const;
HasGrowth()137     bool  HasGrowth() const { return CheckForce(force_GROW) || CheckForce(force_EXPGROWSTICK); };
HasSelection()138     bool  HasSelection() const { return CheckForce(force_LOGISTICSELECTION) || CheckForce(force_LOGSELECTSTICK); };
HasLogisticSelection()139     bool  HasLogisticSelection() const { return CheckForce(force_LOGISTICSELECTION); };
140     // Get Functions
GetNForces()141     long        GetNForces()       const { return m_forcevec.size(); };
142     long        GetNLocalPartitionForces() const;
143     ForceVec::const_iterator GetForceByTag(force_type tag) const;
144     const StickForce& GetStickForce() const;
145     LongVec1d GetLocalPartitionIndexes() const;
146     LongVec1d GetNonLocalPartitionIndexes() const;
147     force_type GetNonLocalPartitionForceTag() const;
148 
149     long        GetMaxEvents(force_type tag) const;
150     MethodTypeVec1d GetMethods(force_type tag) const;
151     const std::vector<Epoch>* GetEpochs() const;
152 
153     StringVec1d ToXML(unsigned long nspaces) const;
154 
GetStartParameters()155     const ForceParameters& GetStartParameters() const { return m_startParameters; };
156 
157     bool ConstrainParameterValues(ForceParameters& fp) const;
158 
GetLlikeMle(long region)159     double GetLlikeMle(long region) const   {
160         assert(static_cast<unsigned>(region) < m_llikemles.size());
161         return m_llikemles[region]; };
162 
GetOverallLlikeMle()163     double GetOverallLlikeMle()     const    { double mle =
164             ((m_llikemles.size() == 1)
165              ? m_llikemles[0] : m_overallLlikemle);
166         return mle; };
167 
168     // It is a ghastly error to call the next function unless the underlying model actually is 2D
169     // (and square).  It will throw an exception if you try.
170 
171     MethodTypeVec2d  Get2DMethods(force_type tag) const;
172 
GetAllForces()173     const ForceVec&    GetAllForces()  const  { return m_forcevec; };
174     vector<long>       GetForceSizes() const;
175     vector<force_type> GetForceTags()  const;
GetIdenticalGroupedParams()176     ULongVec2d       GetIdenticalGroupedParams() const { return m_identicalGroups; };
GetMultiplicativeGroupedParams()177     ULongVec2d       GetMultiplicativeGroupedParams() const { return m_multiplicativeGroups; };
178     void   SetParamWithConstraints(long pindex, double newval, DoubleVec1d& pvec, bool islog) const;
179 
180     const ForceVec   GetPartitionForces() const;
181     const ForceVec   GetLocalPartitionForces() const;
182 
183     long             GetNParameters(force_type tag) const;
184 
185     long             GetAllNParameters() const;
186     DoubleVec1d      GetModifiers(long posinparamvec) const;
187     DoubleVec1d      GetModifiersByForce(force_type tag) const;
188     proftype         GetOverallProfileType() const;
189 
190     double GetLowParameter(long posinparamvec) const;
191     double GetHighParameter(long posinparamvec) const;
192     double GetHighMult(long posinparamvec) const;
193     double GetLowMult(long posinparamvec) const;
194 
195     // If you need to check out a set of Parameters as a linearized vector, declare a variable of type
196     // ParamVector.  That constructor uses the singleton ForceSummary object from the registry and can
197     // give you a read-only or read-write version of the parameters.  See parameter.h
198 
199     // GetPartIndex() is a call through function to handle dynamic
200     // casting
201     long             GetPartIndex(force_type forcename) const;
202 
203     // Used by the code that constructs proto-ranges
204     LongVec1d        GetSelectedSites() const;
205 
206     TimeManager*     CreateTimeManager() const;
207 
208     // Validation
209     bool IsValidForceSummary() const;
210     // This is used in summary file reading, to make sure we are
211     // reading a file created with the same forces as we now have.
212     void ValidateForceParamOrBarf(const ForceParameters& fp);
213 };
214 
215 #endif // FORCESUMMARY_H
216 
217 //____________________________________________________________________________________
218