Implemention (untested) of hessian/std. error calculation and likelihood-based confid...
[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 typedef struct omxOptimizerState omxOptimizerState;
44
45 #include "omxMatrix.h"
46 #include "omxAlgebra.h"
47 #include "omxObjective.h"
48 #include "omxData.h"
49 //#include "omxOptimizer.h"                                                                                     // omxOptimizer objects coming soon
50
51 /* Structure definitions for object evaluation */  // Might be cleaner to give these their own files.
52 struct omxFreeVar {                     // Free Variables
53         double lbound, ubound;  // Bounds
54         int numLocations;
55         double** location;              // And where they go.
56         int* matrices;                  // Matrix numbers for dirtying.
57 };
58
59 struct omxConstraint {          // Free Variable Constraints
60         int size;
61         int opCode;
62         double* lbound;
63         double* ubound;
64         omxMatrix* result;
65 };
66
67 struct omxOptimizerState {                      // For hessian or confidence interval computation
68         int currentParameter;                   // Which parameter is being examined?
69         double offset;                                  // Current offset of optimization
70         short int alpha;                                // Parameter multiplier
71         // Objective should be:  (3.84 - (-2LL))^2 + alpha * parameter
72         // Alpha should generally be +1 to minimize parameter -1 to maximize
73 };
74
75 #define MAX_STRING_LEN 250
76
77 struct omxState {                                                                                                       // The Current State of Optimization
78
79 /* Model and Optimizer Pointers */
80
81 //      omxOptimizer* optimizer;                                                                                // Current Optimizer
82         int numMats, numAlgs, numData;                                                                  // Number of matrices, algebras, and data elements
83         omxMatrix** matrixList;                                                                                 // Model Matrices
84         omxMatrix** algebraList;                                                                                // Model Algebras
85         omxData** dataList;                                                                                             // Data Objects
86         omxMatrix* objectiveMatrix;                                                                             // Objective Algebra
87
88         /* May want to farm these out to the omxObjective object. */
89         int numConstraints;
90         omxConstraint* conList;                                                                                 // List of constraints
91
92         int numFreeParams;
93         omxFreeVar* freeVarList;                                                                                // List of Free Variables and where they go.
94         
95         /* Saved Optimum State */ // TODO: Rename saved optimum state
96         double* optimalValues;                                                                                  // Values of the free parameters at the optimum value
97         double optimum;                                                                                                 // Objective value at last saved optimum
98         int optimumStatus;                                                                                              // Optimizer status of last saved optimum (0=converged, 1=green, -1=error, >1=red)
99         char optimumMsg[250];                                                                                   // Status message of last saved optimum
100         omxOptimizerState* optimizerState;                                                              // Current optimum parameters for limit computation
101
102 /* Current Optimization State (optimizer-specific) */
103 //      void* optimizerInfo;                                                                                    // Optimizer specific storage
104
105 /* Data members for use by Objective Function and Algebra Calculations */
106         long int computeCount;                                                                                  // How many times have things been evaluated so far?
107         long int currentRow;                                                                                    // If we're calculating row-by-row, what row are we on?
108         
109         int statusCode;                                                                                                 // Status code, if appropriate
110         char statusMsg[250];                                                                                    // Status/Error message to report
111         double saturatedModel;                                                                                  // Saturated model likelihood, where applicable
112
113 };
114
115 /* Initialize and Destroy */
116         void omxInitState(omxState* state);                                                                     // Null Constructor
117         void omxFillState(omxState* state, /*omxOptimizer *oo,*/ omxMatrix** matrixList, omxMatrix** algebraList, omxData** dataList, omxMatrix* objective); 
118         void omxFreeState(omxState *oo);                                                                        // Destructor
119         void omxSaveState(omxState *os, double* freeVals, double minimum);      // Saves the current optimization values //TODO: Rename omxSaveState.
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