1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/****************************************************************
Copyright (C) 1997-2001 Lucent Technologies
All Rights Reserved
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of Lucent or any of its entities
not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/
#ifndef FUNCADD_H_INCLUDED
#define FUNCADD_H_INCLUDED
#include "stdio1.h" /* for ANSI and any printing */
#ifndef VA_LIST
#define VA_LIST va_list
#endif
#ifdef _WIN32
#define Stdio_redefs
#endif
typedef struct cryptblock cryptblock;
#ifdef __cplusplus
#undef KR_headers
extern "C" {
#endif
#ifndef real
typedef double real;
#endif
typedef struct arglist arglist;
typedef struct function function;
typedef struct TVA TVA;
typedef struct AmplExports AmplExports;
typedef struct AuxInfo AuxInfo;
typedef struct TableInfo TableInfo;
typedef struct TMInfo TMInfo;
#ifndef No_arglist_def
#undef Const
#ifdef KR_headers
#define Const /* nothing */
#else
#define Const const
#endif
struct
arglist { /* Information sent to user-defined functions */
int n; /* number of args */
int nr; /* number of real input args */
int *at; /* argument types -- see DISCUSSION below */
real *ra; /* pure real args (IN, OUT, and INOUT) */
Const char **sa; /* symbolic IN args */
real *derivs; /* for partial derivatives (if nonzero) */
real *hes; /* for second partials (if nonzero) */
char *dig; /* if (dig && dig[i]) { partials w.r.t. */
/* ra[i] will not be used } */
Char *funcinfo; /* for use by the function (if desired) */
AmplExports *AE; /* functions made visible (via #defines below) */
function *f; /* for internal use by AMPL */
TVA *tva; /* for internal use by AMPL */
char *Errmsg; /* To indicate an error, set this to a */
/* description of the error. When derivs */
/* is nonzero and the error is that first */
/* derivatives cannot or are not computed, */
/* a single quote character (') should be */
/* the first character in the text assigned */
/* to Errmsg, followed by the actual error */
/* message. Similarly, if hes is nonzero */
/* and the error is that second derivatives */
/* are not or cannot be computed, a double */
/* quote character (") should be the first */
/* character in Errmsg, followed by the */
/* actual error message text. */
TMInfo *TMI; /* used in Tempmem calls */
Char *Private;
/* The following fields are relevant */
/* only when imported functions are called */
/* by AMPL commands (not declarations). */
int nin; /* number of input (IN and INOUT) args */
int nout; /* number of output (OUT and INOUT) args */
int nsin; /* number of symbolic input arguments */
int nsout; /* number of symbolic OUT and INOUT args */
};
typedef real (*rfunc) ANSI((arglist *));
typedef real (ufunc) ANSI((arglist *));
#endif /* No_arglist_def */
enum AMPLFUNC_AT_BITS { /* Intrepretation of at[i] when the type */
/* arg to addfunc has the */
/* FUNCADD_OUTPUT_ARGS bit on.*/
AMPLFUNC_INARG = 1, /* IN or INOUT */
AMPLFUNC_OUTARG = 2, /* OUT or INOUT */
AMPLFUNC_STRING = 4, /* Input value is a string (sa[i]) */
AMPLFUNC_STROUT = 8 /* String output value allowed */
};
enum FUNCADD_TYPE { /* bits in "type" arg to addfunc */
/* The type arg to addfunc should consist of one of the */
/* following values ... */
FUNCADD_REAL_VALUED = 0, /* real (double) valued function */
FUNCADD_STRING_VALUED = 2, /* char* valued function (AMPL only) */
FUNCADD_RANDOM_VALUED = 4, /* real random valued */
FUNCADD_012ARGS = 6, /* Special case: real random valued */
/* with 0 <= nargs <= 2 arguments */
/* passed directly, rather than in */
/* an arglist structure (AMPL only). */
/* possibly or-ed with the following... */
FUNCADD_STRING_ARGS = 1, /* allow string args */
FUNCADD_OUTPUT_ARGS = 16, /* allow output args (AMPL only) */
FUNCADD_TUPLE_VALUED = 32, /* not yet allowed */
/* internal use */
FUNCADD_NO_ARGLIST = 8,
FUNCADD_NO_DUPWARN = 64, /* no complaint if already defined */
FUNCADD_NONRAND_BUILTIN = 128 /* mean, variance, moment, etc. */
};
/* If a constraint involves an imported function and presolve fixes all
* the arguments of the function, AMPL may later need to ask the
* function for its partial derivatives -- even though the solver had
* no reason to call the function. If so, it will pass an arglist *al
* with al->derivs nonzero, and it will expect the function to set
* al->derivs[i] to the partial derivative of the function with respect
* to al->ra[i]. Solvers that need to evaluate an imported function
* work the same way -- they set al->derivs to a nonzero value if they
* require both the function value and its first derivatives. Solvers
* that expect Hessians to be supplied to them also set al->hes to a
* nonzero value if they require second derivatives at the current
* argument. In this case, the function should set
* al->hes[i + j*(j+1)/2] to the partial derivative of the function with
* respect to al->ra[i] and al->ra[j] for all 0 <= i <= j < al->nr.
*/
typedef void AddFunc ANSI((
const char *name,
rfunc f, /* cast f to (rfunc) if it returns char* */
int type, /* see FUNCADD_TYPE above */
int nargs, /* >= 0 ==> exactly that many args
* <= -1 ==> at least -(nargs+1) args
*/
void *funcinfo, /* for use by the function (if desired) */
AmplExports *ae
));
typedef void AddRand ANSI((
const char *name,
rfunc f, /* assumed to be a random function */
rfunc icdf, /* inverse CDF */
int type, /* FUNCADD_STRING_ARGS or 0 */
int nargs, /* >= 0 ==> exactly that many args
* <= -1 ==> at least -(nargs+1) args
*/
void *funcinfo, /* for use by the function (if desired) */
AmplExports *ae
));
typedef void (*RandSeedSetter) ANSI((void*, unsigned long));
typedef void AddRandInit ANSI((AmplExports *ae, RandSeedSetter, void*));
typedef void Exitfunc ANSI((void*));
struct
AuxInfo {
AuxInfo *next;
char *auxname;
void *v;
void (*f) ANSI((AmplExports*, void*, ...));
};
struct
AmplExports {
FILE *StdErr;
AddFunc *Addfunc;
long ASLdate;
int (*FprintF) ANSI((FILE*, const char*, ...));
int (*PrintF) ANSI((const char*, ...));
int (*SprintF) ANSI((char*, const char*, ...));
int (*VfprintF) ANSI((FILE*, const char*, VA_LIST));
int (*VsprintF) ANSI((char*, const char*, VA_LIST));
double (*Strtod) ANSI((const char*, char**));
cryptblock *(*Crypto) ANSI((char *key, size_t scrbytes));
Char *asl;
void (*AtExit) ANSI((AmplExports *ae, Exitfunc*, void*));
void (*AtReset) ANSI((AmplExports *ae, Exitfunc*, void*));
Char *(*Tempmem) ANSI((TMInfo*, size_t));
void (*Add_table_handler) ANSI((
int (*DbRead) (AmplExports *ae, TableInfo *TI),
int (*DbWrite)(AmplExports *ae, TableInfo *TI),
char *handler_info,
int flags,
void *Vinfo
));
Char *Private;
void (*Qsortv) ANSI((void*, size_t, size_t, int(*)(const void*,const void*,void*), void*));
/* More stuff for stdio in DLLs... */
FILE *StdIn;
FILE *StdOut;
void (*Clearerr) ANSI((FILE*));
int (*Fclose) ANSI((FILE*));
FILE* (*Fdopen) ANSI((int, const char*));
int (*Feof) ANSI((FILE*));
int (*Ferror) ANSI((FILE*));
int (*Fflush) ANSI((FILE*));
int (*Fgetc) ANSI((FILE*));
char* (*Fgets) ANSI((char*, int, FILE*));
int (*Fileno) ANSI((FILE*));
FILE* (*Fopen) ANSI((const char*, const char*));
int (*Fputc) ANSI((int, FILE*));
int (*Fputs) ANSI((const char*, FILE*));
size_t (*Fread) ANSI((void*, size_t, size_t, FILE*));
FILE* (*Freopen) ANSI((const char*, const char*, FILE*));
int (*Fscanf) ANSI((FILE*, const char*, ...));
int (*Fseek) ANSI((FILE*, long, int));
long (*Ftell) ANSI((FILE*));
size_t (*Fwrite) ANSI((const void*, size_t, size_t, FILE*));
int (*Pclose) ANSI((FILE*));
void (*Perror) ANSI((const char*));
FILE* (*Popen) ANSI((const char*, const char*));
int (*Puts) ANSI((const char*));
void (*Rewind) ANSI((FILE*));
int (*Scanf) ANSI((const char*, ...));
void (*Setbuf) ANSI((FILE*, char*));
int (*Setvbuf) ANSI((FILE*, char*, int, size_t));
int (*Sscanf) ANSI((const char*, const char*, ...));
char* (*Tempnam) ANSI((const char*, const char*));
FILE* (*Tmpfile) ANSI((void));
char* (*Tmpnam) ANSI((char*));
int (*Ungetc) ANSI((int, FILE*));
AuxInfo *AI;
char* (*Getenv) ANSI((const char*));
void (*Breakfunc) ANSI((int,void*));
Char *Breakarg;
/* Items available with ASLdate >= 20020501 start here. */
int (*SnprintF) ANSI((char*, size_t, const char*, ...));
int (*VsnprintF) ANSI((char*, size_t, const char*, VA_LIST));
AddRand *Addrand; /* for random function/inverse CDF pairs */
AddRandInit *Addrandinit; /* for adding a function to receive a new random seed */
};
extern const char *i_option_ASL, *ix_details_ASL[];
#define funcadd funcadd_ASL
#if defined(_WIN32) && !defined(__MINGW32__)
__declspec(dllexport)
#endif
extern void funcadd ANSI((AmplExports*)); /* dynamically linked */
extern void af_libnamesave_ASL ANSI((AmplExports*, const char *fullname, const char *name, int nlen));
extern void note_libuse_ASL ANSI((void)); /* If funcadd() does not provide any imported */
/* functions, it can call note_libuse_ASL() to */
/* keep the library loaded; note_libuse_ASL() is */
/* called, e.g., by the tableproxy table handler. */
#ifdef __cplusplus
}
#endif
typedef struct
DbCol {
real *dval;
char **sval;
} DbCol;
struct
TableInfo {
int (*AddRows) ANSI((TableInfo *TI, DbCol *cols, long nrows));
char *tname; /* name of this table */
char **strings;
char **colnames;
DbCol *cols;
char *Missing;
char *Errmsg;
void *Vinfo;
TMInfo *TMI;
int nstrings;
int arity;
int ncols;
int flags;
long nrows;
void *Private;
int (*Lookup) ANSI((real*, char**, TableInfo*));
long (*AdjustMaxrows) ANSI((TableInfo*, long new_maxrows));
void *(*ColAlloc) ANSI((TableInfo*, int ncol, int sval));
long maxrows;
};
enum { /* return values from (*DbRead)(...) and (*DbWrite)(...) */
DB_Done = 0, /* Table read or written. */
DB_Refuse = 1, /* Refuse to handle this table. */
DB_Error = 2 /* Error reading or writing table. */
};
enum { /* bits in flags field of TableInfo */
DBTI_flags_IN = 1, /* table has IN or INOUT entities */
DBTI_flags_OUT = 2, /* table has OUT or INOUT entities */
DBTI_flags_INSET = 4 /* table has "in set" phrase: */
/* DbRead could omit rows for */
/* which Lookup(...) == -1; AMPL */
/* will ignore such rows if DbRead */
/* offers them. */
};
#endif /* FUNCADD_H_INCLUDED */
#ifndef No_AE_redefs
/* Assume "{extern|static} AmplExports *ae;" is given elsewhere. */
#undef Stderr
#undef addfunc
#undef fprintf
#undef getenv
#undef printf
#undef sprintf
#undef snprintf
#undef strtod
#undef vfprintf
#undef vsprintf
#undef vsnprintf
#define Stderr (ae->StdErr)
#define addfunc(a,b,c,d,e) (*ae->Addfunc)(a,b,c,d,e,ae)
#define addrand(a,b,c,d,e,f) (*ae->Addrand)(a,b,c,d,e,f,ae)
#define addrandinit(a,b) (*ae->Addrandinit)(ae,a,b)
#define printf (*ae->PrintF)
#define fprintf (*ae->FprintF)
#define snprintf (*ae->SnprintF)
#define sprintf (*ae->SprintF)
#define strtod (*ae->Strtod)
#define vfprintf (*ae->VfprintF)
#define vsprintf (*ae->VsprintF)
#define vsnprintf (*ae->VsnprintF)
#define TempMem(x,y) (*ae->Tempmem)(x,y)
#define at_exit(x,y) (*ae->AtExit)(ae,x,y)
#define at_reset(x,y) (*ae->AtReset)(ae,x,y)
#define add_table_handler(a,b,c,d,e) (*ae->Add_table_handler)(a,b,c,d,e)
#define qsortv(a,b,c,d,e) (*ae->Qsortv)(a,b,c,d,e)
#define getenv(x) (*ae->Getenv)(x)
#ifdef Stdio_redefs
#undef clearerr
#undef fclose
#undef fdopen
#undef feof
#undef ferror
#undef fflush
#undef fgetc
#undef fgets
#undef fileno
#undef fopen
#undef fputc
#undef fputs
#undef fread
#undef freopen
#undef fscanf
#undef fseek
#undef ftell
#undef fwrite
#undef getc
#undef getchar
#undef gets
#undef pclose
#undef perror
#undef popen
#undef putc
#undef putchar
#undef puts
#undef rewind
#undef scanf
#undef setbuf
#undef setvbuf
#undef sscanf
#undef tempnam
#undef tmpfile
#undef tmpnam
#undef ungetc
#undef vprintf
#define clearerr (*ae->Clearerr)
#define fclose (*ae->Fclose)
#define fdopen (*ae->Fdopen)
#define feof (*ae->Feof)
#define ferror (*ae->Ferror)
#define fflush (*ae->Fflush)
#define fgetc (*ae->Fgetc)
#define fgets (*ae->Fgets)
#define fileno (*ae->Fileno)
#define fopen (*ae->Fopen)
#define fputc (*ae->Fputc)
#define fputs (*ae->Fputs)
#define fread (*ae->Fread)
#define freopen (*ae->Freopen)
#define fscanf (*ae->Fscanf)
#define fseek (*ae->Fseek)
#define ftell (*ae->Ftell)
#define fwrite (*ae->Fwrite)
#define getc (*ae->Fgetc)
#define getchar() (*ae->Getc)(ae->StdIn)
#define gets Error - use "fgets" rather than "gets"
#define pclose (*ae->Pclose)
#define perror (*ae->Perror)
#define popen (*ae->Popen)
#define putc (*ae->Fputc)
#define putchar(x) (*ae->Fputc)(ae->StdOut,(x))
#define puts (*ae->Puts)
#define rewind (*ae->Rewind)
#define scanf (*ae->Scanf)
#define setbuf (*ae->Setbuf)
#define setvbuf (*ae->Setvbuf)
#define sscanf (*ae->Sscanf)
#define tempnam (*ae->Tempnam)
#define tmpfile (*ae->Tmpfile)
#define tmpnam (*ae->Tmpnam)
#define ungetc (*ae->Ungetc)
#define vprintf(x,y) (*ae->VfprintF)(ae->StdOut,(x),(y))
#define Stdin (ae->StdIn)
#define Stdout (ae->StdOut)
#ifndef No_std_FILE_redefs /* may elicit compiler warnings */
#undef stdin
#undef stdout
#undef stderr
#define stdin (ae->StdIn)
#define stdout (ae->StdOut)
#define stderr (ae->StdErr)
#endif /* No_std_FILE_redefs */
#endif /* Stdio_redefs */
#endif /* ifndef No_AE_redefs */
/* DISCUSSION: the "at" field of an arglist...
*
* OUT and INOUT arguments are only permitted in AMPL commands,
* such as "let" and "call" commands (and not in declarations, e.g.,
* of constraints and variables).
*
* When addfunc was called with type <= 6 (so there can be no OUT or
* INOUT arguments), for 0 <= i < n,
* at[i] >= 0 ==> arg i is ra[at[i]]
* at[i] < 0 ==> arg i is sa[-(at[i]+1)].
*
* When addfunc was called with type & FUNCADD_OUTPUT_ARGS on (permitting
* OUT and INOUT arguments), arg i is in ra[i] or sa[i] (as explained
* below), derivs and hes are both null, and at[i] is the union of bits
* that describe arg i:
* AMPLFUNC_INARG = 1 ==> input arg;
* AMPLFUNC_OUTARG = 2 ==> output arg;
* AMPLFUNC_STROUT = 4 ==> can be assigned a string value.
*
* INOUT args have both the AMPLFUNC_INARG and the AMPLFUNC_OUTARG bits
* are on, i.e., (at[i] & 3) == 3.
*
* Symbolic OUT and INOUT arguments are a bit complicated. They can only
* correspond to symbolic parameters in AMPL, which may have either a
* string or a numeric value. Thus there is provision for specifying
* output values to be either numbers or strings. For simplicity, when
* the function accepts output arguments, ra[i] and sa[i] together describe
* argument i. In general (whentype & FUNCADD_OUTPUT_ARGS is nonzero in
* the addfunc call), the incoming value of argument i is ra[i]
* (a numeric value) if sa[i] is null and is otherwise sa[i].
* To assign a value to argument i, either assign a numeric value to
* ra[i] and set sa[i] = 0, or assign a non-null value to sa[i]
* (in which case ra[i] will be ignored). A value assigned to argument
* i is ignored unless at[i] & AMPLFUNC_OUTARG is nonzero; if so
* and if (at[i] & AMPLFUNC_STROUT) == 0, string values cause an error
* message.
*/
|