Major back-end changes, with front-end tweaks to keep up. 1) Data frames are now...
[openmx:openmx.git] / src / omxState.h
1 /*
2  *  Copyright 2007-2009 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
38 /* Forward declarations for later includes */
39 typedef struct omxState omxState;
40 typedef struct omxFreeVar omxFreeVar;
41 typedef struct omxConstraint omxConstraint;
42
43 #include "omxMatrix.h"
44 #include "omxAlgebra.h"
45 #include "omxObjective.h"
46 #include "omxData.h"
47 //#include "omxOptimizer.h"                                                                                     // omxOptimizer objects coming soon
48
49 /* Structure definitions for object evaluation */  // Might be cleaner to give these their own files.
50 struct omxFreeVar {                     // Free Variables
51         double lbound, ubound;  // Bounds
52         int numLocations;
53         double** location;              // And where they go.
54         int* matrices;                  // Matrix numbers for dirtying.
55 };
56
57 struct omxConstraint {          // Free Variable Constraints
58         int size;
59         int opCode;
60         double* lbound;
61         double* ubound;
62         omxMatrix* result;
63 };
64
65 #define MAX_STRING_LEN 250
66
67 #ifdef DEBUGMX
68 #define OMX_DEBUG 1
69 #else
70 #define OMX_DEBUG 0
71 #endif /* DEBUGMX */
72
73
74 struct omxState {                                                                                                       // The Current State of Optimization
75
76 /* Model and Optimizer Pointers */
77
78 //      omxOptimizer* optimizer;                                                                                // Current Optimizer
79         int numMats, numAlgs, numData;                                                                  // Number of matrices, algebras, and data elements
80         omxMatrix** matrixList;                                                                                 // Model Matrices
81         omxMatrix** algebraList;                                                                                // Model Algebras
82         omxData** dataList;                                                                                             // Data Objects
83         omxMatrix* objectiveMatrix;                                                                             // Objective Algebra
84
85         /* May want to farm these out to the omxObjective object. */
86         int numConstraints;
87         omxConstraint* conList;                                                                                 // List of constraints
88
89         int numFreeParams;
90         omxFreeVar* freeVarList;                                                                                // List of Free Variables and where they go.
91
92         unsigned short int allowShortcut;                                                               // See below:
93         /* This is primarily included to allow special case objective functions and optimizers to disable it.
94                 All matrices are initialized at compute count 0.  The optimizer is requested to run the objective
95                 function once at compute count 1 to trace dependencies.  All matrices that are updated at compute
96                 count 1 (that is, are non-static) will advance their lastCompute to 1, and those that are computed
97                 at each row will advance their lastRow to the total number of rows.
98                 If Shortcut evaluation is enabled, any matrix/algebra with a lastCompute of 0 will not check to see 
99                 if it needs updating, ever.  Any matrix/algebra with a lastRow of 0 will not check row-by-row to see
100                 if it need re-evaluation.  This can be disabled in cases where the dependency tree might change during
101                 optimization.  NOT YET IMPLEMENTED. */
102
103 /* Current Optimization State (optimizer-specific) */
104         void* optimizerState;                                                                                   // Optimizer specific state storage
105
106 /* Data members for use by Objective Function and Algebra Calculations */
107         long int computeCount;                                                                                  // How many times have things been evaluated so far?
108         long int currentRow;                                                                                    // If we're calculating row-by-row, what row are we on?
109         
110         int statusCode;                                                                                                 // Status code, if appropriate
111         char statusMsg[250];                                                                                    // Status/Error message to report
112         double saturatedModel;                                                                                  // Saturated model likelihood, where applicable
113
114 };
115
116 /* Initialize and Destroy */
117         void omxInitState(omxState* state);                                                                     // Null Constructor
118         void omxFillState(omxState* state, /*omxOptimizer *oo,*/ omxMatrix** matrixList, omxMatrix** algebraList, omxData** dataList, omxMatrix* objective); 
119         void omxFreeState(omxState *oo);                                                                        // Destructor
120         
121         void omxRaiseError(omxState *oo, int errorCode, char* errorMsg);        // Raise an Error 
122                                                                                                                                                 // TODO: Move RaiseError to omxOptimizer.
123         
124 /* Advance a step */
125         void omxRaiseError(omxState *oo, int errorCode, char* errorStr);
126         void omxStateNextRow(omxState *oo);                                                                     // Advance Row
127         void omxStateNextEvaluation(omxState *oo);                                                      // Advance Evaluation count
128         
129 #endif /* _OMXSTATE_H_ */
130
131