1 |
/* |
2 |
ASCEND Language Interpreter |
3 |
Copyright (C) 2006 Carnegie-Mellon University |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License |
16 |
along with this program; if not, write to the Free Software |
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
18 |
This file is part of the SLV solver. |
19 |
*//** @file |
20 |
|
21 |
@NOTE blackbox equations support the form |
22 |
|
23 |
output[i] = f(input[j] for all j) foreach i |
24 |
|
25 |
which we calculate by calling yhat = f(x), |
26 |
and then residual is (y - yhat). |
27 |
*/ |
28 |
|
29 |
#ifndef ASC_REL_BLACKBOX_H |
30 |
#define ASC_REL_BLACKBOX_H |
31 |
|
32 |
extern int BlackBoxCalcResidual(struct Instance *i, double *res, struct relation *r); |
33 |
|
34 |
/** Compute standard form residual and gradient. |
35 |
See relation_util.h gradient functions |
36 |
for additional notes on the gradient output array and varlist. |
37 |
@param i the relation instance in question. |
38 |
@param residual the residual computed (output). |
39 |
@param gradient the gradient computed (output), an array as long as r->varlist |
40 |
and with elements corresponding to the elements of varlist. |
41 |
@param r the relation data from i. |
42 |
*/ |
43 |
extern int BlackBoxCalcResidGrad(struct Instance *i, double *residual, double *gradient, struct relation *r); |
44 |
|
45 |
/** Compute standard form gradient. |
46 |
See relation_util.h gradient functions |
47 |
for additional notes on the gradient output array and varlist. |
48 |
@param i the relation instance in question. |
49 |
@param gradient the gradient computed (output), an array as long as r->varlist |
50 |
and with elements corresponding to the elements of varlist. |
51 |
@param r the relation data from i. |
52 |
*/ |
53 |
extern int BlackBoxCalcGradient(struct Instance *i, double *gradient, struct relation *r); |
54 |
|
55 |
/** |
56 |
Return the output variable instance from r, assuming r is from a blackbox. |
57 |
*/ |
58 |
extern struct Instance *BlackBoxGetOutputVar(struct relation *r); |
59 |
|
60 |
/** All the relations evaluated in a single call to y=f(x) have this data in common. */ |
61 |
struct BlackBoxCache { /* was extrelcache, sort of. */ |
62 |
struct gl_list_t *formalArgList; /**< only passed on pre_slv */ |
63 |
struct BBoxInterp interp; /**< userdata lives in here only */ |
64 |
int32 inputsLen; /**< number of actual, not formal, inputs */ |
65 |
int32 outputsLen; /**< number of actual, not formal, outputs. */ |
66 |
int32 jacobianLen; |
67 |
int32 hessianLen; |
68 |
double *inputs; /**< aka x; previous input for func eval. */ |
69 |
double *outputs; /**< aka yhat. previous output for func eval. */ |
70 |
double *inputsJac; /**< aka x; previous input for gradient eval. */ |
71 |
double *jacobian; /**< sensitivity dyhat/dx ; row major format; previous gradient output. */ |
72 |
double *hessian; /**< undetermined format */ |
73 |
int residCount; /**< number of calls made for y output. */ |
74 |
int gradCount; /**< number of calls made for gradient. */ |
75 |
int refCount; /* when to destroy */ |
76 |
}; |
77 |
|
78 |
/** All the elements of an array of blackbox relations resulting |
79 |
from a single external statement have the BlackBoxCache in common, but |
80 |
the varlist and lhs are unique to the specific relation. |
81 |
If someone really needs a nodestamp, for some reason, |
82 |
the 'common' pointer is unique to the set. |
83 |
*/ |
84 |
struct BlackBoxData { |
85 |
struct BlackBoxCache *common; |
86 |
unsigned long *inputArgs; /**< an array of indexes into the varlist; |
87 |
see notes elsewhere about why varlist |
88 |
may be shorter than arglist due to alias/ats. |
89 |
size is in common. */ |
90 |
unsigned long lhsindex; /**< location of value yhati in C output array. */ |
91 |
unsigned long lhsvar; /**< location of lhs var(yi) in varlist. */ |
92 |
}; |
93 |
|
94 |
/* make a blackboxdata unique to a single relation. */ |
95 |
extern struct BlackBoxData *CreateBlackBoxData(struct BlackBoxCache *common, |
96 |
unsigned long lhsindex, |
97 |
unsigned long lhsVarNumber); |
98 |
|
99 |
/* called when destroying the relation containing b. */ |
100 |
extern void DestroyBlackBoxData(struct relation * rel, struct BlackBoxData *b); |
101 |
|
102 |
/** |
103 |
Returns an initialized common data for a blackbox relation array |
104 |
with an initial refcount 1. |
105 |
Call DeleteRefBlackBoxCache when done with the object, and |
106 |
make a call to AddRef any time the pointer is stored in a |
107 |
persistent structure. |
108 |
@param inputsLen number of actual, not formal, inputs. |
109 |
@param outputsLen number of actual, not formal, outputs. |
110 |
@param formalArgs list of list of args, which will be copied. |
111 |
*/ |
112 |
extern struct BlackBoxCache *CreateBlackBoxCache( int32 inputsLen, int32 outputsLen, struct gl_list_t *formalArgs); |
113 |
|
114 |
/** make a persistent reference to b. */ |
115 |
extern void AddRefBlackBoxCache(struct BlackBoxCache *b); |
116 |
|
117 |
/** remove a persistent reference to *b. |
118 |
sets *b to null before returning. |
119 |
deallocates **b if refcount has dropped to 0. |
120 |
*/ |
121 |
extern void DeleteRefBlackBoxCache(struct relation *rel, struct BlackBoxCache **b); |
122 |
|
123 |
|
124 |
#endif /* ASC_REL_BLACKBOX_H */ |