Cleaned up omxRAMObjective, added a few new helper functions to prettify things....
[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 #include "omxDefines.h"
38
39 /* Forward declarations for later includes */
40 typedef struct omxState omxState;
41 typedef struct omxFreeVar omxFreeVar;
42 typedef struct omxConstraint omxConstraint;
43
44 #include "omxMatrix.h"
45 #include "omxAlgebra.h"
46 #include "omxObjective.h"
47 #include "omxData.h"
48 //#include "omxOptimizer.h"                                                                                     // omxOptimizer objects coming soon
49
50 /* Structure definitions for object evaluation */  // Might be cleaner to give these their own files.
51 struct omxFreeVar {                     // Free Variables
52         double lbound, ubound;  // Bounds
53         int numLocations;
54         double** location;              // And where they go.
55         int* matrices;                  // Matrix numbers for dirtying.
56 };
57
58 struct omxConstraint {          // Free Variable Constraints
59         int size;
60         int opCode;
61         double* lbound;
62         double* ubound;
63         omxMatrix* result;
64 };
65
66 struct omxOptimizerState {                      // For hessian or confidence interval computation
67         int currentParameter;                   // Which parameter is being examined?
68         double offset;                                  // Current offset of optimization
69 };
70
71 #define MAX_STRING_LEN 250
72
73 struct omxState {                                                                                                       // The Current State of Optimization
74
75 /* Model and Optimizer Pointers */
76
77 //      omxOptimizer* optimizer;                                                                                // Current Optimizer
78         int numMats, numAlgs, numData;                                                                  // Number of matrices, algebras, and data elements
79         omxMatrix** matrixList;                                                                                 // Model Matrices
80         omxMatrix** algebraList;                                                                                // Model Algebras
81         omxData** dataList;                                                                                             // Data Objects
82         omxMatrix* objectiveMatrix;                                                                             // Objective Algebra
83
84         /* May want to farm these out to the omxObjective object. */
85         int numConstraints;
86         omxConstraint* conList;                                                                                 // List of constraints
87
88         int numFreeParams;
89         omxFreeVar* freeVarList;                                                                                // List of Free Variables and where they go.
90         double* optimalValues;                                                                                  // Values of the free parameters at the optimum value
91         double optimum;                                                                                                 // Objective value at optimum
92
93 /* Current Optimization State (optimizer-specific) */
94         void* optimizerState;                                                                                   // Optimizer specific state storage
95
96 /* Data members for use by Objective Function and Algebra Calculations */
97         long int computeCount;                                                                                  // How many times have things been evaluated so far?
98         long int currentRow;                                                                                    // If we're calculating row-by-row, what row are we on?
99         
100         int statusCode;                                                                                                 // Status code, if appropriate
101         char statusMsg[250];                                                                                    // Status/Error message to report
102         double saturatedModel;                                                                                  // Saturated model likelihood, where applicable
103
104 };
105
106 /* Initialize and Destroy */
107         void omxInitState(omxState* state);                                                                     // Null Constructor
108         void omxFillState(omxState* state, /*omxOptimizer *oo,*/ omxMatrix** matrixList, omxMatrix** algebraList, omxData** dataList, omxMatrix* objective); 
109         void omxFreeState(omxState *oo);                                                                        // Destructor
110         
111         void omxRaiseError(omxState *oo, int errorCode, char* errorMsg);        // Raise an Error 
112                                                                                                                                                 // TODO: Move RaiseError to omxOptimizer.
113         
114 /* Advance a step */
115         void omxRaiseError(omxState *oo, int errorCode, char* errorStr);
116         void omxStateNextRow(omxState *oo);                                                                     // Advance Row
117         void omxStateNextEvaluation(omxState *oo);                                                      // Advance Evaluation count
118         
119 #endif /* _OMXSTATE_H_ */
120
121