1 |
/* |
2 |
* SLV Utilities & Structures for ASCEND Solvers |
3 |
* by Ben Allan 1995 |
4 |
* SLV: Ascend Nonlinear Solver |
5 |
* by Karl Michael Westerberg |
6 |
* Created: 2/6/90 |
7 |
* Version: $Revision: 1.23 $ |
8 |
* Version control file: $RCSfile: slv_common.h,v $ |
9 |
* Date last modified: $Date: 1998/04/26 22:48:25 $ |
10 |
* Last modified by: $Author: ballan $ |
11 |
* |
12 |
* This file is part of the SLV solver. |
13 |
* |
14 |
* Copyright (C) 1995 Benjamin Allan 1995 |
15 |
* |
16 |
* The SLV solver is free software; you can redistribute |
17 |
* it and/or modify it under the terms of the GNU General Public License as |
18 |
* published by the Free Software Foundation; either version 2 of the |
19 |
* License, or (at your option) any later version. |
20 |
* |
21 |
* The SLV solver is distributed in hope that it will be |
22 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 |
* General Public License for more details. |
25 |
* |
26 |
* You should have received a copy of the GNU General Public License |
27 |
* along with the program; if not, write to the Free Software Foundation, |
28 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
29 |
* COPYING. COPYING is found in ../compiler. |
30 |
*/ |
31 |
|
32 |
/* |
33 |
* Contents: slv common utilities and definitions |
34 |
* |
35 |
* Authors: Ben Allan |
36 |
* based on the original slv.h by KW and JZ. |
37 |
* |
38 |
* Dates: 01/94 - original version |
39 |
* |
40 |
* Description: |
41 |
* |
42 |
* General C utility routines for slv/Slv class interfaces. Abstracted from |
43 |
* slvX.c January 1995. Ben Allan. |
44 |
* slv.h is the header for folks on the ASCEND end, and this is the one for |
45 |
* folks on the Slv math end. |
46 |
* Don't protoize this file for ASCEND types other than mtx, vec, and boolean |
47 |
* real64, and int32 or we'll have you shot. In particular, not var and rel. |
48 |
* People who aren't supposed to know about var and rel include this. |
49 |
* |
50 |
* In particular, this header may be used without knowing about the ASCEND |
51 |
* compiler or any of its annoying insanities so long as you drag out |
52 |
* ascmalloc(). |
53 |
* This does commit you to being able to stomach the mtx.h file, however, |
54 |
* even if you choose to ignore the contents of mtx. |
55 |
* Several functions, notably the print suite for rel/var names, |
56 |
* assume you are linking against something that does know about |
57 |
* ASCEND instances unless the SLV_INSTANCES flag is set to FALSE. |
58 |
* |
59 |
* The parameters and status struct definitions have been moved here, |
60 |
* being of general interest. |
61 |
*/ |
62 |
|
63 |
/** @file |
64 |
* SLV common utilities & structures for ASCEND solvers. |
65 |
* This includes the following: |
66 |
* - parameters struct definitions & manipulation routines |
67 |
* - status struct definitions & retrieval routines |
68 |
* - vector operations |
69 |
* - solver print routines |
70 |
* - lnkmap support functions |
71 |
* <pre> |
72 |
* Requires: #include <stdio.h> |
73 |
* #include "utilities/ascConfig.h" |
74 |
* #include "solver/slv_types.h" |
75 |
* #include "solver/rel.h" |
76 |
* #include "solver/logrel.h" |
77 |
* #include "solver/mtx.h" |
78 |
* #include "general/list.h" |
79 |
* </pre> |
80 |
* Details on solver parameter definition: |
81 |
* |
82 |
* When used together the parameter-related structures, functions, and |
83 |
* macros allow us to define all of a solver's parameters in one file |
84 |
* and notify the interface of these parameters upon startup (dynamic |
85 |
* interface construction). The parameters can be defined in any order. |
86 |
* The only bookkeeping needed is associated with the macros. You must |
87 |
* have an array of void pointers large enough for all of the macros |
88 |
* you define and you must give each of the macros you define a unique |
89 |
* element of this array. Here is an example using a real parameter |
90 |
* and a character parameter. (The int and bool are similar to the real). |
91 |
* |
92 |
* ---------- START EXAMPLE CODE ---------- |
93 |
* |
94 |
* (* these 4 macros can be defined anywhere more or less so long as it |
95 |
* is before the calls to slv_define_parm. *) |
96 |
* #define REAL_PTR (sys->parm_array[0]) |
97 |
* #define REAL ((*(real64 *)REAL_PTR)) |
98 |
* #define CHAR_PTR (sys->parm_array[1]) |
99 |
* #define CHAR ((*(char **)CHAR_PTR)) |
100 |
* |
101 |
* #define PA_SIZE 2 |
102 |
* struct example { |
103 |
* struct slv_parameters_t p; |
104 |
* void *parm_array[PA_SIZE]; |
105 |
* struct slv_parameter padata[PA_SIZE]; |
106 |
* } e; |
107 |
* ... |
108 |
* e.p.parms = padata; |
109 |
* e.p.dynamic_parms = 0; |
110 |
* |
111 |
* static char *character_names[] = { |
112 |
* "name_one","name_two" |
113 |
* } |
114 |
* (* fill padata with appropriate info *) |
115 |
* slv_define_parm(&(e.p), real_parm, |
116 |
* "r_parm","real parameter" , |
117 |
* "this is an example of a real parameter" , |
118 |
* U_p_real(val,25),U_p_real(lo,0),U_p_real(hi,100),1); |
119 |
* (* now assign the element of e.parm_array from somewhere in padata *) |
120 |
* SLV_RPARM_MACRO(REAL_PTR,parameters); |
121 |
* |
122 |
* (* fill padata with appropriate info *) |
123 |
* slv_define_parm(&(e.p), char_parm, |
124 |
* "c_parm", "character parameter", |
125 |
* "this is an example of a character parameter", |
126 |
* U_p_string(val,character_names[0]), |
127 |
* U_p_strings(lo,character_names), |
128 |
* U_p_int(hi,sizeof(character_names)/sizeof(char *)),1); |
129 |
* (* now assign the element of e.parm_array that matches. *) |
130 |
* SLV_CPARM_MACRO(CHAR_PTR,parameters); |
131 |
* |
132 |
* Resetting the value of a parameter can be done directly |
133 |
* except for string parameters which should be set with, for example, |
134 |
* slv_set_char_parameter(CHAR_PTR,newvalue); |
135 |
* or outside a solver where there is no sys->parm_array: |
136 |
* |
137 |
* slv_set_char_parameter(&(p.parms[i].info.c.value),argv[j]); |
138 |
* |
139 |
* ---------- END OF EXAMPLE CODE ---------- |
140 |
*/ |
141 |
|
142 |
#ifndef slv_common__already_included |
143 |
#define slv_common__already_included |
144 |
|
145 |
#undef SLV_INSTANCES |
146 |
#define SLV_INSTANCES TRUE |
147 |
/**< SLV_INSTANCES should only be FALSE in a libasc.a free environment */ |
148 |
|
149 |
/* |
150 |
* ------------------------------------------------------- |
151 |
* Common data structures for Westerberg derived solvers |
152 |
* ------------------------------------------------------- |
153 |
*/ |
154 |
|
155 |
/** Solver output file informationn. */ |
156 |
struct slv_output_data { |
157 |
FILE *more_important; /**< More significant output to this file stream. NULL ==> no output. */ |
158 |
FILE *less_important; /**< Less significant output to this file stream. NULL ==> no output. */ |
159 |
}; |
160 |
|
161 |
/** |
162 |
* Solver tolerance data structure. |
163 |
* @todo KHACK THIS SHOULD BE REMOVED - solver/slv_common:slv_tolerance_data. |
164 |
*/ |
165 |
struct slv_tolerance_data { |
166 |
real64 drop; /**< Matrix entry drop tolerance during factorization */ |
167 |
real64 pivot; /**< Detect pivot too small, of those available. */ |
168 |
real64 singular; /**< Detect matrix numerically singular. */ |
169 |
real64 feasible; /**< Detect equality relations satisfied. */ |
170 |
real64 rootfind; /**< Detect single equality relation satisfied. */ |
171 |
real64 stationary; /**< Detect lagrange stationary. */ |
172 |
real64 termination; /**< Detect progress diminished. */ |
173 |
}; |
174 |
|
175 |
/** Solver sub-parameter data structure. */ |
176 |
struct slv_sub_parameters { |
177 |
/* arrays of parametric data */ |
178 |
int32 *iap; /**< Array of parametric int32 data. */ |
179 |
real64 *rap; /**< Array of parametric real64 data. */ |
180 |
char* *cap; /**< Array of parametric char* data. */ |
181 |
void* *vap; /**< Array of parametric void* data. */ |
182 |
/* symbolic parameter names */ |
183 |
char* *ianames; /**< Array of symbolic names for iap parameters. */ |
184 |
char* *ranames; /**< Array of symbolic names for rap parameters. */ |
185 |
char* *canames; /**< Array of symbolic names for cap parameters. */ |
186 |
char* *vanames; /**< Array of symbolic names for vap parameters. */ |
187 |
/* longer explanations of the parameter data */ |
188 |
char* *iaexpln; /**< Array of longer descriptions of iap parameters. */ |
189 |
char* *raexpln; /**< Array of longer descriptions of rap parameters. */ |
190 |
char* *caexpln; /**< Array of longer descriptions of cap parameters. */ |
191 |
char* *vaexpln; /**< Array of longer descriptions of vap parameters. */ |
192 |
/* lengths of arrays above */ |
193 |
int32 ilen; /**< Length of iap, ianames, and iaexpln arrays. */ |
194 |
int32 rlen; /**< Length of rap, ranames, and raexpln arrays. */ |
195 |
int32 clen; /**< Length of cap, canames, and caexpln arrays. */ |
196 |
int32 vlen; /**< Length of vap, vanames, and vaexpln arrays. */ |
197 |
}; |
198 |
|
199 |
/** |
200 |
* Data structure for solver statistics. |
201 |
* This is to collect data for the comparison of algorithms. All solvers |
202 |
* should have at least one of these, though the interface will check for |
203 |
* NULL before reading the data. The interpretation of these data is |
204 |
* somewhat up to the coder. |
205 |
*/ |
206 |
struct slv_block_cost { |
207 |
int32 size, /**< How big is the block, in terms of variables? */ |
208 |
iterations, /**< How many iterations to convergence/divergence? */ |
209 |
funcs, /**< How many function evaluations were made? */ |
210 |
jacs, /**< How many jacobian evaluations were made? */ |
211 |
reorder_method; /**< Not documented. Up to individual solver? */ |
212 |
double time, /**< How much cpu total time elapsed while in the block? */ |
213 |
resid, /**< Not documented. The size of the residual? */ |
214 |
functime, /**< Time spent in function evaluations. */ |
215 |
jactime; /**< Time spent in jacobian evaluations, stuffing. */ |
216 |
}; |
217 |
|
218 |
/** Integer solver parameter substructure. */ |
219 |
struct slv_int_parameter { |
220 |
int32 value; /**< Value. */ |
221 |
int32 low; /**< Lower bound. */ |
222 |
int32 high; /**< Upper bound. */ |
223 |
}; |
224 |
|
225 |
/** Boolean solver parameter substructure. */ |
226 |
struct slv_boolean_parameter { |
227 |
int32 value; /**< Value. */ |
228 |
int32 low; /**< Lower bound. */ |
229 |
int32 high; /**< Upper bound. */ |
230 |
}; |
231 |
|
232 |
/** Real solver parameter substructure. */ |
233 |
struct slv_real_parameter { |
234 |
double value; /**< Value. */ |
235 |
double low; /**< Lower bound. */ |
236 |
double high; /**< Upper bound. */ |
237 |
}; |
238 |
|
239 |
/** Char solver parameter substructure. */ |
240 |
struct slv_char_parameter { |
241 |
char *value; /**< Value. */ |
242 |
char **argv; /**< Lower bound. */ |
243 |
int32 high; /**< Upper bound. */ |
244 |
}; |
245 |
|
246 |
/** Basic solver parameter types. */ |
247 |
enum parm_type { |
248 |
int_parm, /**< Integer type. */ |
249 |
bool_parm, /**< Boolean type. */ |
250 |
real_parm, /**< Real type. */ |
251 |
char_parm /**< Char type. */ |
252 |
}; |
253 |
|
254 |
/** Parameter arguments */ |
255 |
union parm_arg |
256 |
{ |
257 |
char **argv; /**< Strings array argument. */ |
258 |
char *argc; /**< Char argument. */ |
259 |
int32 argi; /**< Integer argument. */ |
260 |
int32 argb; /**< Boolean argument. */ |
261 |
real64 argr; /**< Real argument. */ |
262 |
}; |
263 |
|
264 |
/** Solver parameter structure. */ |
265 |
struct slv_parameter { |
266 |
enum parm_type type; /**< Parameter type. */ |
267 |
int32 number; /**< Index in array. */ |
268 |
int32 display; /**< Display page. */ |
269 |
char *name; /**< Scripting short name. */ |
270 |
char *interface_label; /**< User interface label. */ |
271 |
char *description; /**< Modest help string. */ |
272 |
union { |
273 |
struct slv_int_parameter i; /**< Integer parameter. */ |
274 |
struct slv_boolean_parameter b; /**< Boolean parameter. */ |
275 |
struct slv_real_parameter r; /**< Real parameter. */ |
276 |
struct slv_char_parameter c; /**< Char parameter. */ |
277 |
} info; /**< Data. */ |
278 |
}; |
279 |
|
280 |
/* |
281 |
* Macros for parm_arg unions. |
282 |
* Sets appropriate member (parm_u) of the union to the |
283 |
* value specified (val) and returns (parm_u). |
284 |
* (parm_u) should be one of val, lo, or hi. |
285 |
* These macros are used in calls to the |
286 |
* slv_define_parm function defined below. |
287 |
*/ |
288 |
|
289 |
#define U_p_int(parm_u,val) ((((parm_u).argi = (val))), (parm_u)) |
290 |
/**< |
291 |
* Sets the argi of parm_arg parm_u to val and returns the parm_u. |
292 |
* This macro is used for setting integer parm_arg arguments in calls |
293 |
* to slv_define_parm(). parm_u should be one of { val, lo, hi }, |
294 |
* which correspond to local parm_arg variables that should be used |
295 |
* in client functions calling slv_define_parm(). |
296 |
* |
297 |
* @param parm_u The parm_arg to modify, one of {val, lo, hi}. |
298 |
* @param val int, the new value for the parm_arg. |
299 |
* @return Returns parm_u. |
300 |
*/ |
301 |
#define U_p_bool(parm_u,val) ((((parm_u).argb = (val))), (parm_u)) |
302 |
/**< |
303 |
* Sets the argb of parm_arg parm_u to val and returns the parm_u. |
304 |
* This macro is used for setting boolean parm_arg arguments in calls |
305 |
* to slv_define_parm(). parm_u should be one of { val, lo, hi }, |
306 |
* which correspond to local parm_arg variables that should be used |
307 |
* in client functions calling slv_define_parm(). |
308 |
* |
309 |
* @param parm_u The parm_arg to modify, one of {val, lo, hi}. |
310 |
* @param val boolean, the new value for the parm_arg. |
311 |
* @return Returns parm_u. |
312 |
*/ |
313 |
#define U_p_real(parm_u,val) ((((parm_u).argr = (val))), (parm_u)) |
314 |
/**< |
315 |
* Sets the argr of parm_arg parm_u to val and returns the parm_u. |
316 |
* This macro is used for setting real parm_arg arguments in calls |
317 |
* to slv_define_parm(). parm_u should be one of { val, lo, hi }, |
318 |
* which correspond to local parm_arg variables that should be used |
319 |
* in client functions calling slv_define_parm(). |
320 |
* |
321 |
* @param parm_u The parm_arg to modify, one of {val, lo, hi}. |
322 |
* @param val double, the new value for the parm_arg. |
323 |
* @return Returns parm_u. |
324 |
*/ |
325 |
#define U_p_string(parm_u,val) ((((parm_u).argc = (val))), (parm_u)) |
326 |
/**< |
327 |
* Sets the argc of parm_arg parm_u to val and returns the parm_u. |
328 |
* This macro is used for setting string parm_arg arguments in calls |
329 |
* to slv_define_parm(). parm_u should be one of { val, lo, hi }, |
330 |
* which correspond to local parm_arg variables that should be used |
331 |
* in client functions calling slv_define_parm(). |
332 |
* |
333 |
* @param parm_u The parm_arg to modify, one of {val, lo, hi}. |
334 |
* @param val char *, the new value for the parm_arg. |
335 |
* @return Returns parm_u. |
336 |
* For use in calls to slv_define_parm(). |
337 |
*/ |
338 |
#define U_p_strings(parm_u,val) ((((parm_u).argv = (val))), (parm_u)) |
339 |
/**< |
340 |
* Sets the argv of parm_arg parm_u to val and returns the parm_u. |
341 |
* This macro is used for setting string array parm_arg arguments in |
342 |
* calls to slv_define_parm(). parm_u should be one of { val, lo, hi }, |
343 |
* which correspond to local parm_arg variables that should be used |
344 |
* in client functions calling slv_define_parm(). |
345 |
* |
346 |
* @param parm_u The parm_arg to modify, one of {val, lo, hi}. |
347 |
* @param val char **, the new value for the parm_arg. |
348 |
* @return Returns parm_u. |
349 |
* For use in calls to slv_define_parm(). |
350 |
*/ |
351 |
|
352 |
#define SLV_IPARM_MACRO(NAME,slv_parms) \ |
353 |
if (make_macros == 1) { \ |
354 |
(NAME) = &((slv_parms)->parms[(slv_parms)->num_parms-1].info.i.value); \ |
355 |
} |
356 |
/**< |
357 |
* Macro for defining macros of type integer (IPARM). |
358 |
* See SLV_CPARM_MACRO() for more information. |
359 |
*/ |
360 |
#define SLV_BPARM_MACRO(NAME,slv_parms) \ |
361 |
if (make_macros == 1) { \ |
362 |
(NAME) = &((slv_parms)->parms[(slv_parms)->num_parms-1].info.b.value); \ |
363 |
} |
364 |
/**< |
365 |
* Macro for defining macros of type boolean (BPARM). |
366 |
* See SLV_CPARM_MACRO() for more information. |
367 |
*/ |
368 |
#define SLV_RPARM_MACRO(NAME,slv_parms) \ |
369 |
if (make_macros == 1) { \ |
370 |
(NAME) = &((slv_parms)->parms[(slv_parms)->num_parms-1].info.r.value); \ |
371 |
} |
372 |
/**< |
373 |
* Macro for defining macros of type real (RPARM). |
374 |
* See SLV_CPARM_MACRO() for more information. |
375 |
*/ |
376 |
#define SLV_CPARM_MACRO(NAME,slv_parms) \ |
377 |
if (make_macros == 1) { \ |
378 |
(NAME) = &((slv_parms)->parms[(slv_parms)->num_parms-1].info.c.value); \ |
379 |
} |
380 |
/**< |
381 |
* Macro for defining macros of type character (CPARM). |
382 |
* To use, provide a NAME for the macro (in caps by convention) |
383 |
* and a slv_parameters_t pointer (slv_parm). The NAME should be |
384 |
* defined as an element in an array of void pointers in the |
385 |
* module in which the macro is to be used. This macro uses the |
386 |
* current number of registered parameters to link the array of |
387 |
* _VOID_ _POINTERS_ to the correct parameters. If you want to create |
388 |
* a macro for a parameter, you should put the appropriate macro |
389 |
* creating macro IMEDIATELY after the call to slv_define_parm |
390 |
* for that parameter.<br><br> |
391 |
* Local int make_macros; must be defined. |
392 |
*/ |
393 |
|
394 |
/** |
395 |
* Holds the array of parameters and keeps a count of how many it |
396 |
* contains. Also holds various other information which should be |
397 |
* turned into slv_parameters or moved elsewhere |
398 |
* <pre> |
399 |
* Every registered client should have a slv_parameters_t somewhere in it. |
400 |
* |
401 |
* The following is a list of parameters (those parameters that can be |
402 |
* modified during solve without calling slv_presolve() are marked with |
403 |
* "$$$"). It should be noted that some solvers may not be conformable |
404 |
* to some of the parameters. Default values are subject to change via |
405 |
* experimentation. |
406 |
* |
407 |
* output.more_important (default stdout): $$$ |
408 |
* output.less_important (default NULL): $$$ |
409 |
* All output from the solver is written to one of these two files |
410 |
* (except bug messages which are written to stderr). Common values |
411 |
* are NULL (==> no file) and stdout. The more important messages |
412 |
* go to output.more_important and less important messages go to |
413 |
* output.less_important. To shut the solver up, set both files to |
414 |
* NULL. |
415 |
* |
416 |
* tolerance.drop (default 1e-16): |
417 |
* tolerance.pivot (default 0.1): |
418 |
* tolerance.singular (default 1e-12): |
419 |
* tolerance.feasible (default 1e-8): |
420 |
* tolerance.rootfind (default 1e-12): |
421 |
* tolerance.stationary (default 1e-8): |
422 |
* tolerance.termination (default 1e-12): |
423 |
* These define the criterion for selecting pivotable relations, |
424 |
* whether the equations are satisfied, if a local minimum has been |
425 |
* found, or if no further reduction in the augmented lagrange merit |
426 |
* phi can be achieved. |
427 |
* - During jacobian reduction, each equation pivot selected must be |
428 |
* at least a certain fraction given by TOLERANCE.PIVOT of the largest |
429 |
* available. |
430 |
* Also, the largest value in the row must exceed TOLERANCE.SINGULAR |
431 |
* in order to be considered independent. |
432 |
* - The absolute value of each unscaled equation residual is compared |
433 |
* with TOLERANCE.FEASIBLE in order to determine convergence of the |
434 |
* equality constraints during Newton iteration. |
435 |
* - The absolute value of each unscaled equation residual is compared |
436 |
* with TOLERANCE.ROOTFIND in order to determine convergence of the |
437 |
* constraint during rootfinding of single equations. |
438 |
* - Detection of a minimum requires the stationary condition of the |
439 |
* lagrange to be less than TOLERANCE.STATIONARY. |
440 |
* - If the directional derivative of phi along the negative gradient |
441 |
* direction using the suggested iteration step length falls below |
442 |
* TOLERANCE.TERMINATION, iteration is ceased. |
443 |
* - TOLERANCE.DROP is the smallest number magnitude to be allowed |
444 |
* in the Jacobian matrix during factorization. Default is optimistic. |
445 |
* |
446 |
* time_limit (default 30.0): $$$ |
447 |
* This defines the time limit expressed as cpu seconds per block. |
448 |
* If the solver requires more time than this in any given block, |
449 |
* then it will stop. |
450 |
* |
451 |
* iteration_limit (default 100): $$$ |
452 |
* This defines the maximum number of iterations attempted in a given |
453 |
* block. The solver will stop after this many iterations if it fails |
454 |
* to converge. |
455 |
* |
456 |
* factor_option (default 0): |
457 |
* This sets the number of the linear factorization to suggest. |
458 |
* This does not map directly to linsol numbering of any sort. |
459 |
* The map is: 0 <==> RANKI, 1 <==> RANKI_JZ, 2+ <==> ?. |
460 |
* The solver is free to ignore this suggestion. |
461 |
* In fact, the specific solver is free to define the meaning of factor |
462 |
* option depending on what linear packages it can talk to. |
463 |
* |
464 |
* partition (default TRUE): |
465 |
* Specifies whether or not the system will be partitioned into blocks |
466 |
* or not. If not, then the system will be considered as one large |
467 |
* block. |
468 |
* |
469 |
* ignore_bounds (default FALSE): |
470 |
* Specifies whether or not bounds will be considered during solving. |
471 |
* WARNING: if this flag is set, there will be no guarantees that the |
472 |
* solution will lie in bounds. Suggested use might be to set this |
473 |
* flag to TRUE, solve, reset this flag to FALSE, and resolve. |
474 |
* More often than not, in fact, ignore bounds will lead to floating |
475 |
* point exceptions, halting the solution process. |
476 |
* |
477 |
* rho (default 1.0): |
478 |
* Used as a scalar pre-multiplier of the penalty term quantified by one |
479 |
* half the two norm of the equality constraint residuals in an |
480 |
* augmented lagrange merit function. |
481 |
* |
482 |
* sp.ia/ra/ca/vap (defaults NULL, READ ONLY): |
483 |
* Is a set of pointers to arrays (int/double/(char*)/void*). |
484 |
* The values of the pointers themselves should not be modified, |
485 |
* though the values pointed at may be modified. Note that this is |
486 |
* _direct_ modification and will take effect immediately, not on |
487 |
* the next call to slv_set_parameters. When the engine gets around |
488 |
* to looking at the values in these arrays is engine dependent. |
489 |
* NULL is the expected value for some or all of these array |
490 |
* pointers, depending on the engine. The sizes of these arrays are |
491 |
* specific to each solver's interface. As being of interest (at |
492 |
* compile time) to both the slvI.c file and the GUI/CLUI, the |
493 |
* sizes of the arrays to be pointed to are part of the slvI.h file. |
494 |
* The implementor of each slvI.c should take care to use as much of |
495 |
* the slv_parameters_t as possible before passing data through the |
496 |
* arrays provided in the sub_parameters. This will make for a |
497 |
* minimal amount of work when adding an engine to the GUI/CLUI. |
498 |
* To further aid reusability/sanity preservation, slvI.h should |
499 |
* be appended with proper defines for subscripting these arrays. |
500 |
* |
501 |
* sp.i/r/c/vlen (defaults 0, READ ONLY) |
502 |
* lengths of the sub_parameter arrays. |
503 |
* |
504 |
* sp.ia/ra/ca/vanames (defaults NULL, READONLY) |
505 |
* symbolic names for the corresponding entries in ia/ra/ca/vap. |
506 |
* |
507 |
* sp.ia/ra/ca/vaexpln (defaults NULL, READONLY) |
508 |
* longer explanations for the corresponding entries in ia/ra/ca/vap. |
509 |
* |
510 |
* whose (default 0=>slv0, READ ONLY) |
511 |
* This tells where a parameter set came from, since the default |
512 |
* action of slv_get_parameters is to return a copy of slv0's |
513 |
* parameters if the parameters asked for don't exist because |
514 |
* the solver in question wasn't built/linked. |
515 |
* </pre> |
516 |
*/ |
517 |
typedef struct slv_parameters_structure { |
518 |
struct slv_output_data output; /**< File streams for solver output. */ |
519 |
struct slv_tolerance_data tolerance; /**< Defince various tolerances for the solver. */ |
520 |
struct slv_parameter *parms; /**< Holds the parameters defined for a solver. */ |
521 |
int32 num_parms; /**< The number of parameters in parms. */ |
522 |
int32 dynamic_parms; /**< Set to TRUE if parms is dynamically allocated. */ |
523 |
|
524 |
/* we wish the following were on the way out */ |
525 |
struct slv_sub_parameters sp; /**< Solver sub-parameters. */ |
526 |
int whose; /**< Code for where a parameter set came from. */ |
527 |
int32 ignore_bounds; /**< Set to TRUE to disregard boundary conditions. */ |
528 |
int32 partition; /**< Set to TRUE if system will be partitioned into blocks. */ |
529 |
|
530 |
/* the following are on the way out */ |
531 |
double time_limit; /**< Max cpu seconds per block. @todo kill */ |
532 |
double rho; /**< Scaler premultiplier of penalty term. @todo kill */ |
533 |
int32 iteration_limit; /**< Max number of iterations. @todo kill */ |
534 |
int32 factor_option; /**< Suggests a number for linear factorization. @todo kill */ |
535 |
|
536 |
} slv_parameters_t; |
537 |
|
538 |
|
539 |
/* slv_destroy_parms() is defined in slv.c */ |
540 |
extern void slv_destroy_parms(slv_parameters_t *p); |
541 |
/**< |
542 |
* Deallocates any allocated memory held by a parameter structure. |
543 |
* Only the held memory is freed, not p itself. Further, if |
544 |
* (p->dynamic_parms != 0), the strings in p->parms are freed |
545 |
* but not p->parms itself. Does nothing if p is NULL. |
546 |
* |
547 |
* @param p The parameter structure to destroy. |
548 |
*/ |
549 |
|
550 |
/* slv_define_parm() is defined in slv.c */ |
551 |
extern int32 slv_define_parm(slv_parameters_t *p, |
552 |
enum parm_type type, |
553 |
char *interface_name, |
554 |
char *interface_label, |
555 |
char *description, |
556 |
union parm_arg value, |
557 |
union parm_arg lower_bound, |
558 |
union parm_arg upper_bound, |
559 |
int32 page); |
560 |
/**< |
561 |
* Adds (defines) a new parameter in a parameter structure. |
562 |
* Use this function to add & define new parameters for a solver. |
563 |
* |
564 |
* @param p Parameter structure to receive the new parameter. |
565 |
* @param type Parameter type: int_parm, bool_parm, real_parm, or char_parm. |
566 |
* @param interface_name A very short but descriptive name that the interface |
567 |
* can use to identify the parameter. |
568 |
* @param interface_label A short text string to be displayed on the interface. |
569 |
* @param description A slightly more detailed string to be displayed |
570 |
* upon request a la balloon help. |
571 |
* @param value The value for the parameter, set using one of |
572 |
* the U_p_int() style macros defined above. |
573 |
* @param lower_bound The lower bound for the parameter, set using one of |
574 |
* the U_p_int() style macros defined above. |
575 |
* @param upper_bound The upper bound for the parameter, set using one of |
576 |
* the U_p_int() style macros defined above. |
577 |
* @param page The page of the interface options dialog on which |
578 |
* to display this parameter. Ranges from 1..max_page_no. |
579 |
* Set to -1 if this parameter is not to be displayed in |
580 |
* the interface. |
581 |
* @return Returns -1 if p is NULL or called with unsupported type; |
582 |
* otherwise returns the number of registered parameters in p. |
583 |
*/ |
584 |
|
585 |
/* slv_set_char_parameter() is defined in slv.c */ |
586 |
extern void slv_set_char_parameter(char **cptr, char *newvalue); |
587 |
/**< |
588 |
* Sets a char parameter value to a new string. |
589 |
* Resetting the value of a parameter can be done directly except |
590 |
* for string parameters which must be set with this function. The |
591 |
* string newvalue is not kept by the function.<br><br> |
592 |
* |
593 |
* Example: slv_set_char_parameter(&(p.parms[i].info.c.value),argv[j]); |
594 |
* |
595 |
* @param cptr Pointer to the char array to set. |
596 |
* @param newvalue New value for *cptr. |
597 |
*/ |
598 |
|
599 |
/** Solver block status record. */ |
600 |
struct slv__block_status_structure { |
601 |
int32 number_of; /**< Number of blocks in system. */ |
602 |
int32 current_block; /**< Block number of the current block that the |
603 |
solver is working on. It is assumed that all |
604 |
previous blocks have already converged. */ |
605 |
int32 current_reordered_block; /**< Number of the block most recently reordered. */ |
606 |
int32 current_size; /**< Number of variables/relations in the current block. */ |
607 |
int32 previous_total_size; /**< Total size of previous blocks (= number of |
608 |
variables/relations already converged). */ |
609 |
int32 previous_total_size_vars; /**< Not currently implemented. */ |
610 |
int32 iteration; /**< Number of iterations so far in the current block. */ |
611 |
int32 funcs; /**< Number of residuals calculated in the current block. */ |
612 |
int32 jacs; /**< Number of jacobians evaluated in the current block. */ |
613 |
double cpu_elapsed; /**< Number of cpu seconds elapsed in the current block. */ |
614 |
double functime; /**< Number of cpu seconds elapsed getting residuals. */ |
615 |
double jactime; /**< Number of cpu seconds elapsed getting jacobians. */ |
616 |
real64 residual; /**< Current residual (RMS value) for the current block. */ |
617 |
}; |
618 |
|
619 |
/** |
620 |
* Solver status flags. |
621 |
* <pre> |
622 |
* The following is a list of statuses and their meanings. Statuses |
623 |
* cannot be written to, and thus there is no notion of default value. |
624 |
* |
625 |
* ok: |
626 |
* Specifies whether or not everything is "ok". It is a shorthand for |
627 |
* testing all of the other flags. |
628 |
* |
629 |
* over_defined: |
630 |
* under_defined: |
631 |
* struct_singular: |
632 |
* Specifies whether the system is over-defined, under-defined, or |
633 |
* structurally singular. These fields are set by slv_presolve where |
634 |
* the structural analysis is performed. It should be noted that |
635 |
* over_defined and under_defined are mutually exclusive and both |
636 |
* imply struct_singular, although a system can be structurally |
637 |
* singular without being over-defined or under-defined. |
638 |
* |
639 |
* ready_to_solve: |
640 |
* Specifies whether the system is ready to solve. In other words, is |
641 |
* slv_iterate or slv_solve legal? This flag is FALSE before |
642 |
* slv_presolve or after the system has converged or the solver has |
643 |
* given up for whatever reason. |
644 |
* |
645 |
* converged: |
646 |
* This flag is set whenever the entire system has converged. The |
647 |
* convergence will be genuine (all relations satisfied within |
648 |
* tolerance, all bounds satisfied, all calculations defined, etc.). |
649 |
* |
650 |
* diverged: |
651 |
* This flag is set whenever the solver has truly given up (i.e. given |
652 |
* up for any reason not covered below). |
653 |
* |
654 |
* inconsistent: |
655 |
* The solver has concluded unambiguously (e.g. by symbolic |
656 |
* manipulation) that the system is inconsistent. |
657 |
* |
658 |
* calc_ok: |
659 |
* Specifies whether or not there were any calculation errors in |
660 |
* computing the residuals at the current point. |
661 |
* |
662 |
* iteration_limit_exceeded: |
663 |
* Specifies whether or not the iteration count was exceeded or not. |
664 |
* |
665 |
* time_limit_exceeded: |
666 |
* Specifies whether or not the cpu time limit was exceeded. |
667 |
* |
668 |
* panic: |
669 |
* Specifies whether or not the user called a halt interactively; |
670 |
* |
671 |
* iteration: |
672 |
* Total number of iterations so far. Total iteration count is reset to |
673 |
* zero whenever slv_presolve or slv_resolve is called. |
674 |
* |
675 |
* cpu_elapsed: |
676 |
* Total number of cpu seconds elapsed. Total cpu time elapsed is reset |
677 |
* to zero whenever slv_presolve or slv_resolve is called. |
678 |
* |
679 |
* block.number_of: |
680 |
* Number of blocks in system. |
681 |
* |
682 |
* block.current_block: |
683 |
* Block number of the current block that the solver is working on. |
684 |
* It is assumed that all previous blocks have already converged. |
685 |
* |
686 |
* block.current_size: |
687 |
* Number of variables/relations in the current block. |
688 |
* |
689 |
* block.previous_total_size: |
690 |
* Total size of previous blocks (= number of variables/relations |
691 |
* already converged). |
692 |
* |
693 |
* block.iteration: |
694 |
* Number of iterations so far in the current block. |
695 |
* |
696 |
* block.functime: |
697 |
* Number of cpu seconds elapsed getting residuals from whereever. |
698 |
* |
699 |
* block.jactime: |
700 |
* Number of cpu seconds elapsed getting jacobians from whereever. |
701 |
* |
702 |
* block.cpu_elapsed: |
703 |
* Number of cpu seconds elapsed so far in the current block. |
704 |
* |
705 |
* block.residual: |
706 |
* Current residual (RMS value) for the current block. |
707 |
* |
708 |
* cost (READ ONLY) |
709 |
* This is a pointer to first of an array which is costsize long of |
710 |
* slv_block_cost structures. This is to collect data for the |
711 |
* comparison of algorithms. All solvers should have at least |
712 |
* one of these, though the interface will check for null before |
713 |
* reading the data. The block_cost structure contains: |
714 |
* size (how big is the block, in terms of variables) |
715 |
* iterations (how many iterations to convergence/divergence) |
716 |
* funcs (how many function evaluations were made?) |
717 |
* jacs (how many jacobian evaluations were made?) |
718 |
* time (how much cpu total time elapsed while in the block?) |
719 |
* functime (time spent in function evaluations) |
720 |
* jactime (time spent in jacobian evaluations, stuffing) |
721 |
* (for those codes where a function evaluation is |
722 |
* a byproduct of gradient evaluation, the func cost |
723 |
* will be billed here.) |
724 |
* The interpretation of these data are somewhat up to the coder. |
725 |
* |
726 |
* costsize |
727 |
* This is how big the cost array is. It should in general be the |
728 |
* number of blocks in the system plus 1 so that all the unincluded |
729 |
* relations can be billed to the blocks+1th cost if they are |
730 |
* evaluated. |
731 |
* </pre> |
732 |
*/ |
733 |
typedef struct slv_status_structure { |
734 |
uint32 ok : 1; /**< If TRUE, everything is ok. */ |
735 |
uint32 over_defined : 1; /**< Is system over-defined? */ |
736 |
uint32 under_defined : 1; /**< Is system under-defined? */ |
737 |
uint32 struct_singular : 1; /**< Is system structurally singular? */ |
738 |
uint32 ready_to_solve : 1; /**< Is system ready to solve? */ |
739 |
uint32 converged : 1; /**< Has system fully convergeded? */ |
740 |
uint32 diverged : 1; /**< Has system diverged? */ |
741 |
uint32 inconsistent : 1; /**< Was system was found to be inconsistent? */ |
742 |
uint32 calc_ok : 1; /**< Were any errors encounted calculating residuals? */ |
743 |
uint32 iteration_limit_exceeded : 1; /**< Was the iteraction limit exceeded? */ |
744 |
uint32 time_limit_exceeded : 1; /**< Was the time limit exceeded? */ |
745 |
uint32 panic :1; /**< Did the user stop the solver interactively? */ |
746 |
int32 iteration; /**< Total number of iterations so far. */ |
747 |
int32 costsize; /**< Number of elements in the cost array. */ |
748 |
double cpu_elapsed; /**< Total elapsed cpu seconds. */ |
749 |
struct slv_block_cost *cost; /**< Array of slv_block_cost records. */ |
750 |
struct slv__block_status_structure block; /**< Block status information. */ |
751 |
} slv_status_t; |
752 |
|
753 |
/* |
754 |
* -------------------------------- |
755 |
* vector_data class & operations |
756 |
* -------------------------------- |
757 |
* |
758 |
* If we get brave, we will consider replacing the cores of these |
759 |
* routines with blas calls. We aren't overeager to go mixed |
760 |
* language call nuts just yet, however. |
761 |
*/ |
762 |
|
763 |
/** |
764 |
* A dense vector class of some utility and the functions for it. |
765 |
* The vector consists of an array of real64 (vec) and a mtx_range_t |
766 |
* (rng) which refers to subsets of the range of indexes of vec. |
767 |
* When calling the various vector functions, the range indexes in |
768 |
* rng are used to calculate offsets in the vec array. Therefore, |
769 |
* it is important that your rng->(low,high) refer to valid indexes |
770 |
* of vec[]. In particular |
771 |
* - neither rng->low nor rng->high may be negative |
772 |
* - low <= high |
773 |
* - high < length of vec |
774 |
* This means that whatever your maximum high is, you should allocate |
775 |
* (high+1) values in vec. |
776 |
* @todo solver/slv_common:vector_data & operations should be |
777 |
* moved to a module in general or utilities. |
778 |
*/ |
779 |
struct vector_data { |
780 |
real64 norm2; /**< 2-norm of vector squared. */ |
781 |
mtx_range_t *rng; /**< Pointer to range of vector (low..high). */ |
782 |
real64 *vec; /**< Data array (NULL => uninitialized). */ |
783 |
boolean accurate; /**< Is vector currently accurate? User-manipulated. */ |
784 |
}; |
785 |
|
786 |
extern struct vector_data *slv_create_vector(int32 low, int32 high); |
787 |
/**< |
788 |
* Returns a new vector_data initialized to the specified range. |
789 |
* This function creates, initializes, and returns a new vector_data |
790 |
* structure. The vector is initialized using init_vector() and |
791 |
* a pointer to the new struct is returned. If the specified range |
792 |
* is improper (see slv_init_vector()) then a valid vector cannot be |
793 |
* created and NULL is returned.<br><br> |
794 |
* |
795 |
* Destruction of the returned vector_data is the responsibility of |
796 |
* the caller. slv_destroy_vector() may be used for this purpose. |
797 |
* |
798 |
* @param low The lower bound of the vector's range. |
799 |
* @param high The upper bound of the vector's range. |
800 |
* @return A new initialized vector_data, or NULL if one could |
801 |
* not be created. |
802 |
*/ |
803 |
|
804 |
extern int slv_init_vector(struct vector_data *vec, int32 low, int32 high); |
805 |
/**< |
806 |
* Initializes a vector_data structure. |
807 |
* The new range (low..high) is considered proper if both low and |
808 |
* high are zero or positive, and (low <= high). If the new range is |
809 |
* not proper (or if vec itself is NULL), then no modifications are |
810 |
* made to vec.<br><br> |
811 |
* |
812 |
* If the range is proper then vec->rng is allocated if NULL and then |
813 |
* set using low and high. Then vec->vec is allocated (if NULL) or |
814 |
* reallocated to size (high+1). The data in vec->vec is not |
815 |
* initialized or changed. The member vec->accurate is set to FALSE. |
816 |
* |
817 |
* @param vec Pointer to the vector_data to initialize. |
818 |
* @param low The lower bound of the vector's range. |
819 |
* @param high The upper bound of the vector's range. |
820 |
* @return Returns 0 if the vector is initialized successfully, |
821 |
* 1 if an improper range was specified, 2 if vec is NULL, |
822 |
* and 3 if memory cannot be allocated. |
823 |
*/ |
824 |
|
825 |
extern void slv_destroy_vector(struct vector_data *vec); |
826 |
/**< |
827 |
* Destroys a vector and its assocated data. |
828 |
* Deallocates any memory held in vec->rng and vec->vec, |
829 |
* and then deallocates the vector itself. NULL is tolerated |
830 |
* for vec, vec->rng, or vec->vec. |
831 |
* |
832 |
* @param vec Pointer to the vector_data to destroy. |
833 |
*/ |
834 |
|
835 |
extern void slv_zero_vector(struct vector_data *vec); |
836 |
/**< |
837 |
* Zeroes a vector. |
838 |
* The vector entries between vec->rng.low and vec->rng.high will |
839 |
* be set to 0.0. |
840 |
* The following are not allowed and are checked by assertion: |
841 |
* - NULL vec |
842 |
* - NULL vec->rng |
843 |
* - NULL vec->vec |
844 |
* - vec->rng->low < 0 |
845 |
* - vec->rng->low > vec->rng->high |
846 |
* |
847 |
* @param vec The vector to zero. |
848 |
*/ |
849 |
|
850 |
extern void slv_copy_vector(struct vector_data *srcvec, |
851 |
struct vector_data *destvec); |
852 |
/**< |
853 |
* Copies the data from srcvec to destvec. |
854 |
* The data in the range [srcvec->rng.low .. srcvec->rng.high] |
855 |
* is copied to destvec starting at position destvec->rng.low. |
856 |
* destvec must have at least as many elements in vec as srcvec. |
857 |
* The following are not allowed and are checked by assertion: |
858 |
* - NULL srcvec |
859 |
* - NULL srcvec->rng |
860 |
* - NULL srcvec->vec |
861 |
* - srcvec->rng->low < 0 |
862 |
* - srcvec->rng->low > srcvec->rng->high |
863 |
* - NULL destvec |
864 |
* - NULL destvec->rng |
865 |
* - NULL destvec->vec |
866 |
* - destvec->rng->low < 0 |
867 |
* |
868 |
* @param srcvec The vector to copy. |
869 |
* @param destvec The vector to receive the copied data. |
870 |
*/ |
871 |
|
872 |
extern real64 slv_inner_product(struct vector_data *vec1, |
873 |
struct vector_data *vec2); |
874 |
/**< |
875 |
* Calculates the dot product of 2 vectors. |
876 |
* Dot [vec1->rng.low .. vec1->rng.high] with vec2 starting at |
877 |
* position vec2->rng.low. |
878 |
* The following are not allowed and are checked by assertion: |
879 |
* - NULL vec1 |
880 |
* - NULL vec1->rng |
881 |
* - NULL vec1->vec |
882 |
* - vec1->rng->low < 0 |
883 |
* - vec1->rng->low > vec1->rng->high |
884 |
* - NULL vec2 |
885 |
* - NULL vec2->rng |
886 |
* - NULL vec2->vec |
887 |
* - vec2->rng->low < 0 |
888 |
* |
889 |
* @param vec1 The 1st vector for the dot product. |
890 |
* @param vec2 The 2nd vector for the dot product. |
891 |
* @todo solver/slv_common:slv_inner_product() could stand to be optimized. |
892 |
*/ |
893 |
|
894 |
extern real64 slv_square_norm(struct vector_data *vec); |
895 |
/**< |
896 |
* Calculates the dot product of a vector with itself. |
897 |
* Dot [vec->rng.low .. vec->rng.high] with itself and store the |
898 |
* result in vec->norm2. |
899 |
* The following are not allowed and are checked by assertion: |
900 |
* - NULL vec |
901 |
* - NULL vec->rng |
902 |
* - NULL vec->vec |
903 |
* - vec->rng->low < 0 |
904 |
* - vec->rng->low > vec->rng->high |
905 |
* |
906 |
* @param vec The vector for the dot product. |
907 |
* @todo solver/slv_common:slv_square_norm() could stand to be optimized. |
908 |
*/ |
909 |
|
910 |
extern void slv_matrix_product(mtx_matrix_t mtx, |
911 |
struct vector_data *vec, |
912 |
struct vector_data *prod, |
913 |
real64 scale, |
914 |
boolean transpose); |
915 |
/**< |
916 |
* Calculates the product of a vector, matrix, and scale factor. |
917 |
* Stores prod := (scale)*(mtx)*(vec) if transpose = FALSE, |
918 |
* or prod := (scale)*(mtx-transpose)(vec) if transpose = TRUE. |
919 |
* vec and prod must be completely different. |
920 |
* If (!transpose) vec->vec is assumed indexed by current col and |
921 |
* prod->vec is indexed by current row of mtx. |
922 |
* If (transpose) vec->vec is assumed indexed by current row and |
923 |
* prod->vec is indexed by current col of mtx. |
924 |
* The following are not allowed and are checked by assertion: |
925 |
* - NULL mtx |
926 |
* - NULL vec |
927 |
* - NULL vec->rng |
928 |
* - NULL vec->vec |
929 |
* - vec->rng->low < 0 |
930 |
* - vec->rng->low > vec->rng->high |
931 |
* - NULL prod |
932 |
* - NULL prod->rng |
933 |
* - NULL prod->vec |
934 |
* - prod->rng->low < 0 |
935 |
* - prod->rng->low > prod->rng->high |
936 |
* |
937 |
* @param mtx The matrix for the product. |
938 |
* @param vec The vector for the product. |
939 |
* @param prod The vector to receive the matrix product. |
940 |
* @param scale The scale factor by which to multiply the matrix product. |
941 |
* @param transpose Flag for whether to use mtx or its transpose. |
942 |
* |
943 |
* @todo solver/slv_common:slv_mtx_product needs attention - |
944 |
* does it go into mtx? |
945 |
*/ |
946 |
|
947 |
extern void slv_write_vector(FILE *fp, struct vector_data *vec); |
948 |
/**< |
949 |
* Write vector information to a file stream. |
950 |
* Prints general information about the vector followed by the |
951 |
* values in the range of the vector to file fp. |
952 |
* |
953 |
* @param fp The file stream to receive the report. |
954 |
* @param vec The vector on which to report. |
955 |
*/ |
956 |
|
957 |
/* |
958 |
* ---------------------------- |
959 |
* Misc. BLAS-like functions |
960 |
* ---------------------------- |
961 |
*/ |
962 |
|
963 |
extern real64 slv_dot(int32 len, const real64 *a1, const real64 *a2); |
964 |
/**< |
965 |
* Calculates the dot product of 2 arrays of real64. |
966 |
* This is an optimized routine (loop unrolled). It takes |
967 |
* advantage of identical vectors. The 2 arrays must have |
968 |
* at least len elements. |
969 |
* The following are not allowed and are checked by assertion: |
970 |
* - NULL a1 |
971 |
* - NULL a2 |
972 |
* - len < 0 |
973 |
* |
974 |
* The same algorithm is used inside slv_inner_product(), so there |
975 |
* is no need to use this function directly if you are using the |
976 |
* vector_data type. |
977 |
* |
978 |
* @param len The length of the 2 arrays. |
979 |
* @param a1 The 1st array for the dot product. |
980 |
* @param a2 The 2nd array for the dot product. |
981 |
*/ |
982 |
|
983 |
/* |
984 |
* -------------------------------- |
985 |
* General input/output routines |
986 |
* -------------------------------- |
987 |
*/ |
988 |
|
989 |
extern FILE *slv_get_output_file(FILE *fp); |
990 |
/**< |
991 |
* Checks a file pointer, and if NULL returns a pointer to the nul device. |
992 |
* If you are in environment that doesn't have something like |
993 |
* /dev/null (nul on Windows), you'd better be damn sure your |
994 |
* sys->p.output.*_important are not NULL. |
995 |
* |
996 |
* @param fp The file stream to check. |
997 |
* @return fp if it is not NULL, a pointer to the nul device otherwise. |
998 |
*/ |
999 |
|
1000 |
/* |
1001 |
* FILE pointer macros. |
1002 |
* fp = MIF(sys) |
1003 |
* fp = LIF(sys) |
1004 |
* fp = PMIF(sys) |
1005 |
* fp = PLIF(sys) |
1006 |
* or fprintf(MIF(sys),"stuff",data...); |
1007 |
* Use of these is requested on grounds of readability but not required. |
1008 |
* All of these are macros, which means any specific solver interface |
1009 |
* to ASCEND can use them, since all interfaces are supposed to |
1010 |
* support a parameters structure p somewhere in a larger system |
1011 |
* structure (sys) they keep privately. |
1012 |
* Use the PMIF or PLIF flavors if the parameters sys->p is a pointer |
1013 |
* rather than a in-struct member. |
1014 |
*/ |
1015 |
#define MIF(sys) slv_get_output_file( (sys)->p.output.more_important ) |
1016 |
/**< |
1017 |
* Retrieve the "more important" output file for a system. |
1018 |
* sys must exist and contain an element p of type slv_parameters_t. |
1019 |
* |
1020 |
* @param sys The slv_system_t to query. |
1021 |
* @return A FILE * to the "more important" output file for sys. |
1022 |
*/ |
1023 |
#define LIF(sys) slv_get_output_file( (sys)->p.output.less_important ) |
1024 |
/**< |
1025 |
* Retrieve the "less important" output file for a system. |
1026 |
* sys must exist and contain an element p of type slv_parameters_t. |
1027 |
* |
1028 |
* @param sys The slv_system_t to query. |
1029 |
* @return A FILE * to the "less important" output file for sys. |
1030 |
*/ |
1031 |
#define PMIF(sys) slv_get_output_file( (sys)->p->output.more_important ) |
1032 |
/**< |
1033 |
* Retrieve the "more important" output file for a system. |
1034 |
* sys must exist and contain an element p of type slv_parameters_t*. |
1035 |
* |
1036 |
* @param sys The slv_system_t to query. |
1037 |
* @return A FILE * to the "more important" output file for sys. |
1038 |
*/ |
1039 |
#define PLIF(sys) slv_get_output_file( (sys)->p->output.less_important ) |
1040 |
/**< |
1041 |
* Retrieve the "less important" output file for a system. |
1042 |
* sys must exist and contain an element p of type slv_parameters_t*. |
1043 |
* |
1044 |
* @param sys The slv_system_t to query. |
1045 |
* @return A FILE * to the "less important" output file for sys. |
1046 |
*/ |
1047 |
|
1048 |
/*------------------- begin compiler dependent functions -------------------*/ |
1049 |
#if SLV_INSTANCES |
1050 |
|
1051 |
#ifdef NEWSTUFF |
1052 |
extern void slv_print_obj_name(FILE *outfile, obj_objective_t obj); |
1053 |
/**< |
1054 |
* Not implemented. |
1055 |
* Prints the name of obj to outfile. If obj_make_name() can't |
1056 |
* generate a name, the global index is printed instead. |
1057 |
* @todo Implement solver/slv_common:slv_print_obj_name() or remove prototype. |
1058 |
*/ |
1059 |
#endif |
1060 |
extern void slv_print_rel_name(FILE *outfile, |
1061 |
slv_system_t sys, |
1062 |
struct rel_relation *rel); |
1063 |
/**< |
1064 |
* Prints the name of rel to outfile. If rel_make_name() can't |
1065 |
* generate a name, the global index is printed instead. |
1066 |
* |
1067 |
* @param outfile The stream to receive the output. |
1068 |
* @param sys The solver system. |
1069 |
* @param rel The relation whose name should be printed. |
1070 |
* @todo Move solver/slv_common:slv_print_rel_name() to solver/rel. |
1071 |
*/ |
1072 |
|
1073 |
extern void slv_print_var_name(FILE *outfile, |
1074 |
slv_system_t sys, |
1075 |
struct var_variable *var); |
1076 |
/**< |
1077 |
* Prints the name of var to outfile. If var_make_name() can't |
1078 |
* generate a name, the global index is printed instead. |
1079 |
* |
1080 |
* @param outfile The stream to receive the output. |
1081 |
* @param sys The solver system. |
1082 |
* @param var The variable whose name should be printed. |
1083 |
* @todo Move solver/slv_common:slv_print_var_name() to solver/var. |
1084 |
*/ |
1085 |
|
1086 |
extern void slv_print_logrel_name(FILE *outfile, |
1087 |
slv_system_t sys, |
1088 |
struct logrel_relation *lrel); |
1089 |
/**< |
1090 |
* Prints the name of lrel to outfile. If logrel_make_name() can't |
1091 |
* generate a name, the global index is printed instead. |
1092 |
* |
1093 |
* @param outfile The stream to receive the output. |
1094 |
* @param sys The solver system. |
1095 |
* @param lrel The logical relation whose name should be printed. |
1096 |
* @todo Move solver/slv_common:slv_print_logrel_name() to solver/logrel. |
1097 |
*/ |
1098 |
|
1099 |
extern void slv_print_dis_name(FILE *outfile, |
1100 |
slv_system_t sys, |
1101 |
struct dis_discrete *dvar); |
1102 |
/**< |
1103 |
* Prints the name of dvar to outfile. If dis_make_name() can't |
1104 |
* generate a name, the global index is printed instead. |
1105 |
* |
1106 |
* @param outfile The stream to receive the output. |
1107 |
* @param sys The solver system. |
1108 |
* @param dvar The discrete variable whose name should be printed. |
1109 |
* @todo Move solver/slv_common:slv_print_dis_name() to solver/discrete. |
1110 |
*/ |
1111 |
|
1112 |
#ifdef NEWSTUFF |
1113 |
extern void slv_print_obj_index(FILE *outfile, obj_objective_t obj); |
1114 |
/**< |
1115 |
* Not implemented. |
1116 |
* Prints the index of obj to outfile. |
1117 |
* @todo Implement solver/slv_common:slv_print_obj_index() or remove prototype. |
1118 |
*/ |
1119 |
#endif |
1120 |
extern void slv_print_rel_sindex(FILE *outfile, struct rel_relation *rel); |
1121 |
/**< |
1122 |
* Prints the index of rel to outfile. |
1123 |
* |
1124 |
* @param outfile The stream to receive the output. |
1125 |
* @param rel The relation whose index should be printed. |
1126 |
* @todo Move solver/slv_common:slv_print_rel_name() to solver/rel. |
1127 |
*/ |
1128 |
|
1129 |
extern void slv_print_var_sindex(FILE *outfile, struct var_variable *var); |
1130 |
/**< |
1131 |
* Prints the index of var to outfile. |
1132 |
* |
1133 |
* @param outfile The stream to receive the output. |
1134 |
* @param var The variable whose index should be printed. |
1135 |
* @todo Move solver/slv_common:slv_print_var_name() to solver/var. |
1136 |
*/ |
1137 |
|
1138 |
extern void slv_print_logrel_sindex(FILE *outfile, struct logrel_relation *lrel); |
1139 |
/**< |
1140 |
* Prints the index of lrel to outfile. |
1141 |
* |
1142 |
* @param outfile The stream to receive the output. |
1143 |
* @param lrel The logical relation whose index should be printed. |
1144 |
* @todo Move solver/slv_common:slv_print_logrel_name() to solver/logrel. |
1145 |
*/ |
1146 |
|
1147 |
extern void slv_print_dis_sindex(FILE *outfile, struct dis_discrete *dvar); |
1148 |
/**< |
1149 |
* Prints the index of dvar to outfile. |
1150 |
* |
1151 |
* @param outfile The stream to receive the output. |
1152 |
* @param dvar The discrete variable whose index should be printed. |
1153 |
* @todo Move solver/slv_common:slv_print_dis_name() to solver/discrete. |
1154 |
*/ |
1155 |
|
1156 |
extern int slv_direct_solve(slv_system_t server, |
1157 |
struct rel_relation *rel, |
1158 |
struct var_variable *var, |
1159 |
FILE *file, |
1160 |
real64 epsilon, |
1161 |
int ignore_bounds, |
1162 |
int scaled); |
1163 |
/**< |
1164 |
* Attempts to directly solve the given relation (equality constraint) for |
1165 |
* the given variable, leaving the others fixed. Returns an integer |
1166 |
* signifying the status as one of the following three: |
1167 |
* <pre> |
1168 |
* 0 ==> Unable to determine anything. |
1169 |
* Not symbolically invertible. |
1170 |
* 1 ==> Solution(s) found. |
1171 |
* Variable value set to first found if more than one. |
1172 |
* -1 ==> No solution found. |
1173 |
* Function invertible, but no solution exists satisfying |
1174 |
* var bounds (if active) and the epsilon given. |
1175 |
* </pre> |
1176 |
* The variable bounds will be upheld, unless ignore_bounds=FALSE. |
1177 |
* Residual testing will be against epsilon and either scaled or |
1178 |
* unscaled residual according to scaled (no scale -> 0). |
1179 |
* If file != NULL and there are leftover possible solutions, we |
1180 |
* will write about them to file. |
1181 |
* |
1182 |
* @param server The slv_system_t (mostly ignored). |
1183 |
* @param rel The relation to attempt to solve. |
1184 |
* @param var The variable for which to solve. |
1185 |
* @param file File stream to receive other possible solutions. |
1186 |
* @param epsilon Tolerance for testing convergence. |
1187 |
* @param ignore_bounds If TRUE, ignore bounds on variable. |
1188 |
* @param scaled If TRUE, test scaled residuals against epsilon. |
1189 |
* @todo solver/slv_common:slv_direct_solve() should be in solver/relman |
1190 |
* or solver/slv3. |
1191 |
*/ |
1192 |
|
1193 |
extern int slv_direct_log_solve(slv_system_t sys, |
1194 |
struct logrel_relation *lrel, |
1195 |
struct dis_discrete *dvar, |
1196 |
FILE *file, |
1197 |
int perturb, |
1198 |
struct gl_list_t *instances); |
1199 |
/**< |
1200 |
* Attempt to directly solve the given logrelation for the given |
1201 |
* discrete variable, leaving the others fixed. Returns an integer |
1202 |
* signifying the status as one of the following three: |
1203 |
* <pre> |
1204 |
* 0 ==> Unable to determine anything. Bad logrelation or dvar |
1205 |
* 1 ==> Solution found. |
1206 |
* 2 ==> More than one solution found. It does not modify the value |
1207 |
* of dvar. Conflicting. |
1208 |
* -1 ==> No solution found. Inconsistency |
1209 |
* </pre> |
1210 |
* If file != NULL and there are leftover possible solutions, we |
1211 |
* will write about them to file. |
1212 |
* The flag perturb and the gl_list are used to change the truth |
1213 |
* value of some boundaries. This is sometimes useful in |
1214 |
* conditional modeling. |
1215 |
* |
1216 |
* @param sys The slv_system_t (mostly ignored). |
1217 |
* @param lrel The logical relation to attempt to solve. |
1218 |
* @param dvar The discrete variable for which to solve. |
1219 |
* @param file File stream to receive other possible solutions. |
1220 |
* @param perturb If TRUE, perturbs the truth values if necessary to find the solution. |
1221 |
* @param instances List of instances. |
1222 |
* @todo solver/slv_common:slv_direct_log_solve() should be in solver/logrel |
1223 |
* or solver/slv9. |
1224 |
*/ |
1225 |
|
1226 |
#endif |
1227 |
/*-------------------- END compiler dependent functions --------------------*/ |
1228 |
|
1229 |
/* |
1230 |
* -------------------- |
1231 |
* lnkmap functions |
1232 |
* -------------------- |
1233 |
*/ |
1234 |
|
1235 |
extern int32 **slv_create_lnkmap(int32 m, int32 n, int32 hl, int32 *hi, int32 *hj); |
1236 |
/**< |
1237 |
* Builds a row-biased mapping array from the hi,hj lists given. |
1238 |
* The map returned has the following format: |
1239 |
* - map[i] is a vector describing the incidence in row i of the matrix. |
1240 |
* - Let vars=map[i], where vars is int32 *. |
1241 |
* - vars[0]=number of incidences in the relation. |
1242 |
* - For all 0<=k<vars[0] |
1243 |
* - vars[2*k+1] = original column index of some var in the eqn. |
1244 |
* - vars[2*k+2] = the lnk list index of element(i,vars[2*k+1]) |
1245 |
* |
1246 |
* The ordering of column data (i.e. vars[2*k+1]) is implementation-defined |
1247 |
* and should not be counted on. Similarly, the lnk list index (i.e. |
1248 |
* vars[2*k+2]) will be a unique number in the range (0..hl-1), but the |
1249 |
* exact ordering is implementation-defined. The map should only be |
1250 |
* deallocated by destroy_lnkmap(). The memory allocation for a lnkmap |
1251 |
* is done efficiently.<br><br> |
1252 |
* |
1253 |
* These create an odd compressed row mapping, given the hi and hj |
1254 |
* subscript vectors. The primary utility of the lnkmap is that |
1255 |
* it can be traversed rapidly when one wants to conditionally map a row of |
1256 |
* a Harwell style (arbitrarily ordered) link representation |
1257 |
* back into another representation where adding elements to a row |
1258 |
* is easily done.<br><br> |
1259 |
* |
1260 |
* hi and hj should specify a unique incidence pattern. That is, duplicate |
1261 |
* (hi, hj) coordinates are not allowed and only 1 of the occurrences will |
1262 |
* end up in the map. hi should contain row indexes all less than m. |
1263 |
* hj should contain column indexes all less than n. If an invalid row/col |
1264 |
* index is encountered, NULL is returned. |
1265 |
* |
1266 |
* @param m The number of rows expected (> highest index in hi). |
1267 |
* The map returned will be this long. |
1268 |
* @param n The number of columns expected (> highest index in hj). |
1269 |
* @param hl The length of hi and hj. |
1270 |
* @param hi The eqn indices of a C numbered sparse matrix list. |
1271 |
* @param hj The var indices of a C numbered sparse matrix list. |
1272 |
* @return Pointer to the new lnkmap array, or NULL if an error occurred. |
1273 |
*/ |
1274 |
|
1275 |
extern int32 **slv_lnkmap_from_mtx(mtx_matrix_t mtx, mtx_region_t *region); |
1276 |
/**< |
1277 |
* Generates a lnkmap from a region of a matrix. |
1278 |
* The length of the map returned will be the order of mtx. Empty rows |
1279 |
* and columns are allowed in the matrix. Map entries for rows outside |
1280 |
* the specified region will be 0 even if the row contains non-zero |
1281 |
* elements. If mtx is NULL, or if the region is invalid for mtx, then |
1282 |
* NULL is returned.<br><br> |
1283 |
* |
1284 |
* The map returned has the following format: |
1285 |
* - map[i] is a vector describing the incidence in row i of the matrix. |
1286 |
* - Let vars=map[i], where vars is int32 *. |
1287 |
* - vars[0]=number of non-zeros in the row. |
1288 |
* - For all 0<=k<vars[0] |
1289 |
* - vars[2*k+1] = original column index of some a non-zero element in the row. |
1290 |
* - vars[2*k+2] = the value of the element (i,vars[2*k+1]), cast to int32. |
1291 |
* |
1292 |
* @param mtx The matrix to map (non-NULL). |
1293 |
* @param region The region of the matrix to map (non-NULL). |
1294 |
* @return Pointer to the new lnkmap array, or NULL if an error occurred. |
1295 |
* @see slv_create_lnkmap() for a more details about lnkmaps. |
1296 |
*/ |
1297 |
|
1298 |
extern void slv_destroy_lnkmap(int32 **map); |
1299 |
/**< |
1300 |
* Deallocate a map created by slv_create_lnkmap() or slv_destroy_lnkmap(). |
1301 |
* destroy_lnkmap() will tolerate a NULL map as input. |
1302 |
* |
1303 |
* @param map The lnkmap to destroy. |
1304 |
*/ |
1305 |
|
1306 |
extern void slv_write_lnkmap(FILE *fp, int m, int32 **map); |
1307 |
/**< |
1308 |
* Prints a link map to a file. |
1309 |
* write_lnkmap() will tolerate a NULL map as input. |
1310 |
* |
1311 |
* @param fp The file stream to receive the report. |
1312 |
* @param m The number of rows in map to print. |
1313 |
* @param map The lnkmap to print. |
1314 |
*/ |
1315 |
|
1316 |
#endif /* slv_common__already_included */ |
1317 |
|