Refrain from copying markMatrices to omxState children
[openmx:openmx.git] / src / omxState.h
1 /*
2  *  Copyright 2007-2013 The OpenMx Project
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 /***********************************************************
18 *
19 *  omxState.h
20 *
21 *  Created: Timothy R. Brick    Date: 2009-05-23
22 *
23 *       Contains header information for the omxState structure
24 *   omxStates keep the current optimization state.
25 *
26 **********************************************************/
27
28 #ifndef _OMXSTATE_H_
29 #define _OMXSTATE_H_
30
31 #include <R.h>
32 #include <Rinternals.h>
33 #include <Rdefines.h>
34 #include <R_ext/Rdynload.h>
35 #include <R_ext/BLAS.h>
36 #include <R_ext/Lapack.h>
37 #include <sys/types.h>
38
39 #ifdef WIN32
40
41 #else
42
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <netdb.h>
46
47 #endif
48
49 #include <time.h>
50 #include <unistd.h>
51 #include "omxDefines.h"
52
53 /* Forward declarations for later includes */
54 typedef struct omxState omxState;
55 typedef struct omxFreeVar omxFreeVar;
56 typedef struct omxConstraint omxConstraint;
57 typedef struct omxCheckpoint omxCheckpoint;
58 typedef struct omxConfidenceInterval omxConfidenceInterval;
59
60 #include "omxMatrix.h"
61 #include "omxAlgebra.h"
62 #include "omxExpectation.h"
63 #include "omxFitFunction.h"
64 #include "omxData.h"
65 //#include "omxOptimizer.h"                                                                                     // omxOptimizer objects coming soon
66
67 #include <vector>
68
69 struct omxFreeVarLocation {
70         int matrix;
71         int row, col;
72 };
73
74 struct omxFreeVar {                     // Free Variables
75         double lbound, ubound;  // Bounds
76         std::vector<omxFreeVarLocation> locations;
77         int numDeps;            // number of algebra/matrix dependencies
78         int *deps;              // indices of algebra/matrix dependencies
79         const char* name;
80 };
81
82 struct omxConstraint {          // Free Variable Constraints
83         int size;
84         int opCode;
85         double* lbound;
86         double* ubound;
87         omxMatrix* result;
88 };
89
90 enum omxCheckpointType {
91         OMX_FILE_CHECKPOINT,
92         OMX_CONNECTION_CHECKPOINT
93 };
94 typedef enum omxCheckpointType omxCheckpointType;
95
96 struct omxCheckpoint {
97         omxCheckpointType type;
98         time_t time;
99         int numIterations;
100         time_t lastCheckpoint;  // FIXME: Cannot update at sub-second times.
101         FILE* file;                                             // TODO: Maybe make the connection piece a union instead.
102         SEXP connection;
103         unsigned short int saveHessian;
104 };
105
106 struct omxConfidenceInterval {          // For Confidence interval request
107         omxMatrix* matrix;                              // The matrix
108         int row, col;                                   // Location of element to calculate
109         double ubound;                                  // Fit-space upper boundary
110         double lbound;                                  // Fit-space lower boundary
111         double max;                                             // Value at upper bound
112         double min;                                             // Value at lower bound
113         int lCode;                                              // Optimizer code at lower bound
114         int uCode;                                              // Optimizer code at upper bound
115         unsigned short calcLower;               // Are we currently calculating lbound?
116 };
117
118 #define MAX_STRING_LEN 250
119
120 struct omxState {
121         int numHessians;
122         int calculateStdErrors;
123         int ciMaxIterations;
124
125         int numThreads;
126         int numChildren;
127         std::vector< omxMatrix* > matrixList;
128         std::vector< omxMatrix* > algebraList;
129         std::vector< omxExpectation* > expectationList;
130         std::vector< omxData* > dataList;
131         omxState** childList;                                                                                   // List of child states
132         omxState* parentState;                                                                                  // Parent State
133
134         // TODO: Need a way to deal with unregistered matrices that have free vars
135
136         /* May want to farm these out to the omxFitFunction object. */
137         int numConstraints;
138         int nclin, ncnln;                                               // Number of linear and nonlinear constraints
139         omxConstraint* conList;                                                                                 // List of constraints
140         int numIntervals;
141         omxConfidenceInterval* intervalList;                                                    // List of confidence intervals requested
142
143         int numFreeParams;
144         omxFreeVar* freeVarList;                                                                                // List of Free Variables and where they go.
145
146 /* Data members for use by Fit Function and Algebra Calculations */
147         long int computeCount;                                                                                  // How many times have things been evaluated so far?
148         long int currentRow;                                                                                    // If we're calculating row-by-row, what row are we on?
149
150         /* For Checkpointing */
151         int majorIteration;                                                                                             // Major iteration number
152         int minorIteration;                                                                                             // Minor iteration within major iteration
153         time_t startTime;                                                                                               // Time of first computation
154         time_t endTime;                                                                                                 // 'Cause we might as well report it
155         omxCheckpoint* checkpointList;                                                                  // List of checkpoints
156         char *chkptText1, *chkptText2;                                                                  // Placeholders for checkpointing text
157         int numCheckpoints;                                                                                             // Number of checkpoints
158
159         char statusMsg[MAX_STRING_LEN];
160         int analyticGradients;
161 };
162
163 /* Initialize and Destroy */
164         void omxInitState(omxState* state, omxState *parentState);
165         void omxFillState(omxState* state, /*omxOptimizer *oo,*/ omxMatrix** matrixList, omxMatrix** algebraList, omxData** dataList, omxMatrix* fitFunction);
166         void omxFreeState(omxState *state);                                                                     // Destructor
167         void omxDuplicateState(omxState *tgt, omxState* src); 
168                                                                         // Duplicates the current state object
169         omxState* omxGetState(omxState *os, int stateNum);                                      // Retrieve a child by number
170
171         void omxSetMajorIteration(omxState *state, int value);                          // Recursively set major iteration number
172         void omxSetMinorIteration(omxState *state, int value);                          // Recursively set minor iteration number
173
174         omxMatrix* omxLookupDuplicateElement(omxState* os, omxMatrix* element);
175         omxExpectation* omxLookupDuplicateExpectation(omxState* os, omxExpectation* ox);
176
177         void omxResetStatus(omxState *state);    
178 inline bool isErrorRaised(omxState *state) { return state->statusMsg[0] != 0; }
179 void omxRaiseError(omxState *state, int errorCode, const char* errorMsg); // DEPRECATED
180 void omxRaiseErrorf(omxState *state, const char* errorMsg, ...);
181                                                                                                                                                 // TODO: Move RaiseError to omxOptimizer.
182
183 /* Advance a step */
184         void omxStateNextRow(omxState *state);                                                          // Advance Row
185         void omxStateNextEvaluation(omxState *state);                                           // Advance Evaluation count
186
187         void omxWriteCheckpointMessage(omxState *os, char *msg);
188         void omxSaveCheckpoint(omxState* os, double* x, double* f, int force);  // Save out checkpoints
189 void omxExamineFitOutput(omxState *state, omxMatrix *fitMatrix, int *mode);
190
191 #endif /* _OMXSTATE_H_ */
192
193