/[ascend]/trunk/ascend/solver/slv_interface.c
ViewVC logotype

Contents of /trunk/ascend/solver/slv_interface.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2375 - (show annotations) (download) (as text)
Tue Feb 1 05:53:26 2011 UTC (11 years, 5 months ago) by jpye
File MIME type: text/x-csrc
File size: 25161 byte(s)
Add updated comments to slv_interface.[ch].
Add markup to solver/solver.h so that it appears correctly in the new doxygen documentation.
1 /* ASCEND modelling environment
2 Copyright (c) 1990 Thomas Guthrie Epperly
3 Copyright (C) 1993 Joseph Zaher
4 Copyright (c) 1994 Joseph Zaher, Benjamin Andrew Allan
5 Copyright (c) 2011 Carnegie Mellon University
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21 *//** @file
22 This file implements some kind of command-line based interface for ASCEND's
23 solver. It hasn't been carefully tested for a long time, although it still
24 compiles successfully. It may have some value for debugging purposes,
25 although currently we prefer to use our CUnit test suite together with
26 Python scripting to drive ASCEND. -- JP, Feb 2011.
27
28 OUTDATED: This file defines the function Solve(struct Instance *inst), which is
29 swiped from solver/chris.h. This module will instead use Karl's
30 solver. (was newascend/compiler/chris.h on wetterhorn)
31 much of slv_interface.h is not yet implemented for SLV.
32 *//*
33 * SLV: Ascend Numeric Solver
34 * by Karl Michael Westerberg
35 * Created: 2/6/90
36 * Last in CVS: $Revision: 1.23 $ $Date: 1998/01/11 17:19:09 $ $Author: ballan $
37 */
38
39 #include "slv_interface.h"
40 #include "solver.h"
41
42 #include <ascend/utilities/config.h>
43 #ifdef ASC_SIGNAL_TRAPS
44 # include <ascend/utilities/ascSignal.h>
45 #endif
46
47 #include <ascend/general/ascMalloc.h>
48 #include <ascend/utilities/readln.h> /* you wot? */
49
50 /* DISABLED -- JP
51 #include <ascend/compiler/plot.h>
52 */
53
54 #include <ascend/system/slv_client.h>
55 #include <ascend/system/calc.h>
56 #include <ascend/system/relman.h>
57 #include <ascend/system/system.h>
58 #include "checkdim.h"
59
60 #define prompt printf
61
62 #define yorn(b) ((b) ? "YES" : "NO")
63 #define torf(b) ((b) ? "TRUE" : "FALSE")
64
65 #define SAFE_FIX_ME 0
66
67 #define C_NOP 0
68 #define C_RETURN 1
69 #define C_HELP 2
70 #define C_INPUT_PARAMETERS 3
71 #define C_OUTPUT_PARAMETERS 4
72 #define C_OUTPUT_STATUS 5
73 #define C_COUNT 6
74 #define C_COUNT_IN_BLOCK 7
75 #define C_WRITE_VAR 8
76 #define C_WRITE_VARS 9
77 #define C_WRITE_VARS_IN_BLOCK 10
78 #define C_TOGGLE_SUPPRESS_REL 11
79 #define C_WRITE_REL 12
80 #define C_WRITE_RELS 13
81 #define C_WRITE_RELS_IN_BLOCK 14
82 #define C_WRITE_OBJ 15
83 #define C_CHECK_DIMENSIONS 16
84 #define C_ELIGIBLE_SOLVERS 17
85 #define C_SELECT_SOLVER 18
86 #define C_SELECTED_SOLVER 19
87 #define C_PRESOLVE 20
88 #define C_RESOLVE 21
89 #define C_ITERATE 22
90 #define C_SOLVE 23
91 #define C_DUMP_SYSTEM 25
92 /* #define C_PLOT 26 */
93
94 static struct command_list_t {
95 char *name;
96 char *descr;
97 int num;
98 } commands[] = {
99 { "", "", C_NOP } ,
100 { "return", "Returns to ascend", C_RETURN } ,
101 { "quit", "Returns to ascend", C_RETURN } ,
102 { "bye", "Returns to ascend", C_RETURN } ,
103 { "?", "Prints out this list", C_HELP } ,
104 { "help", "Prints out this list", C_HELP } ,
105 { "input parameters", "Input solver parameters", C_INPUT_PARAMETERS } ,
106 { "output parameters", "Output solver parameters", C_OUTPUT_PARAMETERS } ,
107 { "output status", "Output solver status", C_OUTPUT_STATUS } ,
108 { "count", "Counts variables/relations", C_COUNT } ,
109 { "count in block", "Counts variables/relations in block",
110 C_COUNT_IN_BLOCK } ,
111 { "write var" , "Writes one variable" , C_WRITE_VAR } ,
112 { "write vars" , "Writes complete variable list" , C_WRITE_VARS } ,
113 { "write vars in block" , "Writes variables in current block" ,
114 C_WRITE_VARS_IN_BLOCK } ,
115 { "toggle suppress rel" , "Toggles the suppress-relation-flag" ,
116 C_TOGGLE_SUPPRESS_REL } ,
117 { "write rel" , "Writes one relation" , C_WRITE_REL } ,
118 { "write rels" , "Writes complete relation list" , C_WRITE_RELS } ,
119 { "write rels in block" , "Writes relations in current block" ,
120 C_WRITE_RELS_IN_BLOCK } ,
121 { "write obj" , "Writes objective function" , C_WRITE_OBJ } ,
122 { "check dimensions" , "Checks global dimensional consistency" ,
123 C_CHECK_DIMENSIONS } ,
124 { "eligible solvers" , "Indicates which solvers can solve the problem" ,
125 C_ELIGIBLE_SOLVERS } ,
126 { "select solver" , "Allows user to select a solver" , C_SELECT_SOLVER } ,
127 { "selected solver" , "Indicates which solver has been selected" ,
128 C_SELECTED_SOLVER } ,
129 { "presolve" , "Pre-solves system" , C_PRESOLVE } ,
130 { "resolve" , "Re-solves system" , C_RESOLVE } ,
131 { "iterate" , "Perform one iteration" , C_ITERATE } ,
132 { "solve" , "Attempts to solve entire system" , C_SOLVE } ,
133 { "dump system" , "Dump solve system to a file" , C_DUMP_SYSTEM }/* ,
134 { "plot" , "Plots the solve instance" , C_PLOT } */
135 };
136 #define NCOMMANDS array_length(commands)
137
138 static void print_commands(void)
139 /**
140 *** Prints out all commands
141 **/
142 {
143 unsigned int ndx;
144 for( ndx=0 ; ndx < NCOMMANDS ; ++ndx )
145 PRINTF("%-30s%s\n",commands[ndx].name,commands[ndx].descr);
146 }
147
148 static int command_number(char *str)
149 /**
150 *** Returns number corresponding to command string.
151 *** If non-existent, -1 is returned.
152 **/
153 {
154 unsigned int i;
155 for( i=0 ; i<NCOMMANDS ; ++i )
156 if( strcmp(commands[i].name , str) == 0 )
157 break; /* found it */
158 return( i < NCOMMANDS ? commands[i].num : -1 );
159 }
160
161 static boolean input_boolean(boolean def)
162 /**
163 *** Inputs a boolean (true or false)
164 **/
165 {
166 char buf[10];
167 readln(buf,sizeof(buf));
168 switch( buf[0] ) {
169 case 'y':
170 case 'Y':
171 case 't':
172 case 'T':
173 return(TRUE);
174 case 'n':
175 case 'N':
176 case 'f':
177 case 'F':
178 return(FALSE);
179 default:
180 return(def);
181 }
182 }
183
184 static int input_command(void)
185 /**
186 *** Inputs command, returns command number.
187 **/
188 {
189 char s[100];
190
191 PRINTF("Solver> ");
192 readln(s,sizeof(s));
193 return( command_number(s) );
194 }
195
196 static void write_var(FILE *out, slv_system_t sys, struct var_variable *var)
197 /**
198 *** Writes a variable out.
199 **/
200 {
201 char *name;
202 name = var_make_name(sys,var);
203 if( *name == '?' ) FPRINTF(out,"%d",var_sindex(var));
204 else FPRINTF(out,"%s",name);
205 ascfree(name);
206 }
207
208 static void write_rel(FILE *out, slv_system_t sys, struct rel_relation *rel, boolean suppress)
209 /**
210 *** Writes a relation out.
211 **/
212 {
213 char *name, *str;
214 name = rel_make_name(sys,rel);
215 if( *name == '?' ) FPRINTF(out,"%d",rel_sindex(rel));
216 else FPRINTF(out,"%s",name);
217 ascfree(name);
218 if( suppress ) return;
219 PUTC('\n',out);
220 str = relman_make_string_infix(sys,rel);
221 FPRINTF(out,"\t%s\n",str);
222 ascfree(str);
223 }
224
225 static void write_varlist(FILE *out, slv_system_t sys, var_filter_t *vfilter, boolean justwritestats)
226 /**
227 *** Writes the variable list for those variables
228 *** passing through filter.
229 **/
230 {
231 linsolqr_system_t lsys;
232 mtx_matrix_t mtx;
233 mtx_region_t reg;
234 int32 col;
235 struct var_variable **vp;
236 int32 num[2][2]; /* num[?fixed][?incident] */
237 int32 tnum[2]; /* tnum[?incident] */
238
239 lsys = slv_get_linsolqr_sys(sys);
240 mtx = linsolqr_get_matrix(lsys);
241 FPRINTF(out,"#/block. value (nominal) [LB,UB] ");
242 FPRINTF(out,"?fixed ?incident ?solved - name\n");
243 num[0][0] = num[0][1] = num[1][0] = num[1][1] = 0;
244 for( vp=slv_get_master_var_list(sys) ; *vp != NULL ; ++vp )
245 if( var_apply_filter(*vp,vfilter) ) {
246 ++num[(int)(var_fixed(*vp)&&var_active(*vp))]
247 [(int)(var_incident(*vp)&&var_active(*vp))];
248 if( justwritestats ) continue;
249 col = mtx_org_to_col(mtx,var_sindex(*vp));
250 FPRINTF(out,"%3d/%3d. %14e (%14e) [%14e,%14e] %2s %4s - ",
251 var_sindex(*vp) , mtx_block_containing_col(mtx,col,&reg) ,
252 var_value(*vp),var_nominal(*vp),
253 var_lower_bound(*vp) , var_upper_bound(*vp) ,
254 (var_fixed(*vp)&&var_active(*vp))?"FX":"FR",
255 (var_incident(*vp)&&var_active(*vp))?"INC":"!INC");
256 write_var(out,sys,*vp);
257 PUTC('\n',out);
258 }
259
260 tnum[0]=num[0][0]+num[1][0];
261 tnum[1]=num[0][1]+num[1][1];
262 FPRINTF(out,"# vars free fixed total\n");
263 FPRINTF(out,"incident %4d %4d %4d\n",
264 num[0][1],num[1][1],tnum[1]);
265 FPRINTF(out,"otherwise %4d %4d %4d\n",
266 num[0][0],num[1][0],tnum[0]);
267 FPRINTF(out,"total %4d %4d %4d\n",
268 num[0][0]+num[0][1],num[1][0]+num[1][1],tnum[0]+tnum[1]);
269 }
270
271 static void write_rellist(FILE *out, slv_system_t sys, rel_filter_t *rfilter,
272 boolean justwritestats, boolean suppress)
273 /**
274 *** Writes the relation list for those relations
275 *** passing through filter.
276 **/
277 {
278 linsolqr_system_t lsys;
279 mtx_matrix_t mtx;
280 mtx_region_t reg;
281 int32 row;
282 struct rel_relation **rp;
283 boolean old_calc_ok;
284 int32 num[2]; /* [?included] */
285
286 old_calc_ok = calc_ok;
287 lsys = slv_get_linsolqr_sys(sys);
288 mtx = linsolqr_get_matrix(lsys);
289
290 FPRINTF(out,"#/block. residual ?included - name\\n relation\n");
291 num[0] = num[1] = 0L;
292 #ifdef ASC_SIGNAL_TRAPS
293 Asc_SignalHandlerPush(SIGFPE,SIG_IGN);
294 #endif
295 for( rp=slv_get_master_rel_list(sys) ; *rp != NULL ; ++rp ) {
296 if( rel_apply_filter(*rp,rfilter) ) {
297 real64 res;
298
299 ++num[(int)(rel_included(*rp) && rel_active(*rp))];
300 if( justwritestats ) continue;
301
302 calc_ok = TRUE;
303 res = relman_eval(*rp,&calc_ok,SAFE_FIX_ME);
304 row = mtx_org_to_row(mtx,rel_sindex(*rp));
305 FPRINTF(out,"%3d/%3d. %c%14e %2s - ",
306 rel_sindex(*rp) , mtx_block_containing_row(mtx,row,&reg) ,
307 calc_ok?' ':'!',res,
308 (rel_included(*rp) && rel_active(*rp))?"IN":"IG");
309 write_rel(out,sys,*rp,suppress);
310 }
311 }
312 #ifdef ASC_SIGNAL_TRAPS
313 Asc_SignalHandlerPop(SIGFPE,SIG_IGN);
314 #endif
315 FPRINTF(out,"There are %d relations, %d included and %d ignored.\n",
316 num[0]+num[1],num[1],num[0]);
317 calc_ok = old_calc_ok;
318 }
319
320 static void write_obj(FILE *out, slv_system_t sys)
321 /**
322 *** Writes out the objective function. THE SEMANTICS MAY BE WRONG HERE.
323 **/
324 {
325 struct rel_relation *obj = slv_get_obj_relation(sys);
326 real64 val;
327
328 if( obj == NULL )
329 FPRINTF(out,"Objective: ----NONE---- ( = 0.0)\n");
330 else {
331 char *str = relman_make_string_infix(sys,obj);
332 #ifdef ASC_SIGNAL_TRAPS
333 Asc_SignalHandlerPush(SIGFPE,SIG_IGN);
334 #endif
335 val = relman_eval(obj,&calc_ok,SAFE_FIX_ME);
336 #ifdef ASC_SIGNAL_TRAPS
337 Asc_SignalHandlerPop(SIGFPE,SIG_IGN);
338 #endif
339 FPRINTF(out,"Objective: %s ( = %g)\n",str,val);
340 ascfree(str);
341 }
342 }
343
344 static void input_parameters(slv_system_t sys)
345 /* Inputs parameters for the given system. */
346 {
347 slv_parameters_t p;
348
349 slv_get_parameters(sys,&p);
350
351 PRINTF("Print more important messages? [%s] ",
352 yorn(p.output.more_important!=NULL));
353 p.output.more_important = input_boolean(p.output.more_important!=NULL) ?
354 stdout : NULL;
355
356 PRINTF("Print less important messages? [%s] ",
357 yorn(p.output.less_important!=NULL));
358 p.output.less_important = input_boolean(p.output.less_important!=NULL) ?
359 stdout : NULL;
360
361 PRINTF("Cpu time limit (in seconds) [%g]: ",p.time_limit);
362 p.time_limit = readdouble(p.time_limit);
363
364 PRINTF("Iteration limit [%d]: ",p.iteration_limit);
365 p.iteration_limit = readlong((long)p.iteration_limit);
366
367 PRINTF("Tolerance, singular [%g]: ",p.tolerance.singular);
368 p.tolerance.singular = readdouble(p.tolerance.singular);
369
370 PRINTF("Tolerance, feasible [%g]: ",p.tolerance.feasible);
371 p.tolerance.feasible = readdouble(p.tolerance.feasible);
372
373 PRINTF("Partition problem? [%s] ",yorn(p.partition));
374 p.partition = input_boolean(p.partition);
375
376 PRINTF("Ignore bounds? [%s] ",yorn(p.ignore_bounds));
377 p.ignore_bounds = input_boolean(p.ignore_bounds);
378
379 slv_set_parameters(sys,&p);
380 }
381
382 static void output_parameters(slv_system_t sys)
383 /**
384 *** Outputs parameters for the given system.
385 **/
386 {
387 slv_parameters_t p;
388 slv_get_parameters(sys,&p);
389
390 PRINTF("Print more important messages? = %s\n",
391 yorn(p.output.more_important!=NULL));
392 PRINTF("Print less important messages? = %s\n",
393 yorn(p.output.less_important!=NULL));
394 PRINTF("Cpu time limit = %g seconds\n",p.time_limit);
395 PRINTF("Iteration limit = %d\n",p.iteration_limit);
396 PRINTF("Tolerance, singular = %g\n",p.tolerance.singular);
397 PRINTF("Tolerance, feasible = %g\n",p.tolerance.feasible);
398 PRINTF("Partition problem? = %s\n",yorn(p.partition));
399 PRINTF("Ignore bounds? = %s\n",yorn(p.ignore_bounds));
400 }
401
402 static void output_status(slv_system_t sys)
403 /**
404 *** Outputs status for the given system.
405 **/
406 {
407 slv_status_t s;
408 slv_get_status(sys,&s);
409
410 PRINTF("\nSolver status\n-------------\n");
411
412 if( s.converged )
413 PRINTF("System converged.\n");
414 else
415 PRINTF("System is %sready to solve.\n",s.ready_to_solve ? "" : "not ");
416
417 PRINTF("Abnormalities -->");
418 if( s.over_defined ) PRINTF(" over-defined");
419 if( s.under_defined ) PRINTF(" under-defined");
420 if( s.struct_singular ) PRINTF(" structurally singular");
421 if( s.diverged ) PRINTF(" diverged");
422 if( s.inconsistent ) PRINTF(" inconsistent");
423 if( !s.calc_ok ) PRINTF(" calculation error");
424 if( s.iteration_limit_exceeded ) PRINTF(" iteration limit exceeded");
425 if( s.time_limit_exceeded ) PRINTF(" time limit exceeded");
426 if( s.ok ) PRINTF(" NONE");
427 PUTCHAR('\n');
428
429 PRINTF("# of blocks = %d\n",s.block.number_of);
430 PRINTF("current block = %d\n",s.block.current_block);
431 PRINTF("its size = %d\n",s.block.current_size);
432 PRINTF("# vars/rels solved already = %d\n",
433 s.block.previous_total_size);
434 PRINTF("Iteration = %d (this block = %d)\n",
435 s.iteration,s.block.iteration);
436 PRINTF("CPU time elapsed = %g sec (this block = %g sec)\n",
437 s.cpu_elapsed,s.block.cpu_elapsed);
438 PRINTF("Residual norm in current block = %g\n",s.block.residual);
439 }
440
441
442
443 struct rock {
444 struct var_variable **vlist;
445 struct rel_relation **rlist;
446 int32 nvars,nrels;
447 };
448
449 static void output_system(FILE *fp, slv_system_t sys)
450 {
451 struct rock vr;
452 int32 n;
453 struct var_variable **vp;
454 struct rel_relation **rp, *obj;
455 slv_parameters_t p;
456
457 vr.nvars = vr.nrels = 0;
458 vr.vlist = slv_get_master_var_list(sys);
459 vr.rlist = slv_get_master_rel_list(sys);
460
461 for( vp=vr.vlist ; vp != NULL && *vp != NULL ; ++vp )
462 ++vr.nvars;
463 FPRINTF(fp,"nvars %d\n",vr.nvars);
464
465 for( rp=vr.rlist ; rp != NULL && *rp != NULL ; ++rp )
466 ++vr.nrels;
467 FPRINTF(fp,"nrels %d\n\n",vr.nrels);
468
469 for( n=0,vp=slv_get_master_var_list(sys);
470 vp != NULL && *vp != NULL;
471 ++vp,++n ) {
472 FPRINTF(fp,";v%d\n",n);
473 FPRINTF(fp," v%d = %g\n",n,var_value(*vp));
474 FPRINTF(fp," v%d nominal %g\n",n,var_nominal(*vp));
475 FPRINTF(fp," v%d LB %g\n",n,var_lower_bound(*vp));
476 FPRINTF(fp," v%d UB %g\n",n,var_upper_bound(*vp));
477 FPRINTF(fp," v%d %s\n",n,var_fixed(*vp)?"fixed":"free");
478 FPRINTF(fp," v%d %s\n",n,var_active(*vp)?"active":"inactive");
479 }
480
481 for( n=0,rp=slv_get_master_rel_list(sys);
482 rp != NULL && *rp != NULL;
483 ++rp,++n ) {
484 char *str = relman_make_string_infix(sys,*rp);
485 FPRINTF(fp,"r%d: %s\n",n,str);
486 FPRINTF(fp," r%d %s\n",n,(rel_included(*rp) && rel_active(*rp)) ?
487 "included" : "ignored");
488 ascfree(str);
489 }
490
491 if( (obj=slv_get_obj_relation(sys)) != NULL ) {
492 char *str = relman_make_string_infix(sys,obj);
493 FPRINTF(fp,"\nobj %s\n",str);
494 ascfree(str);
495 }
496
497 slv_get_parameters(sys,&p);
498 FPRINTF(fp,"\ntime limit %g ;seconds\n",p.time_limit);
499 FPRINTF(fp,"iteration limit %d\n",p.iteration_limit);
500 FPRINTF(fp,"singular tolerance %g\n",p.tolerance.singular);
501 FPRINTF(fp,"feasible tolerance %g\n",p.tolerance.feasible);
502 FPRINTF(fp,"partition %s\n",p.partition?"yes":"no");
503 FPRINTF(fp,"ignore bounds %s\n",p.ignore_bounds?"yes":"no");
504 FPRINTF(fp,"output more important %s\n",(p.output.more_important!=NULL)?
505 "yes" :"no");
506 FPRINTF(fp,"output less important %s\n",(p.output.less_important!=NULL)?
507 "yes" :"no");
508 FPRINTF(fp,"\nend\n");
509 }
510
511
512
513 static slv_system_t sys = NULL;
514 /* The system in use */
515 static struct Instance *inst = NULL;
516 /* Instance in use */
517
518 static boolean do_command(int command)
519 /**
520 *** Executes given command: returns FALSE if terminate.
521 **/
522 {
523 static boolean suppress_rel_flag = TRUE;
524
525 switch(command) {
526 default:
527 PRINTF("No such command.\n");
528 break;
529
530 case C_NOP:
531 break;
532
533 case C_RETURN:
534 PRINTF("Keep system around for next time? [yes]: ");
535 if( !input_boolean(TRUE) ) {
536 system_destroy(sys);
537 sys = NULL;
538 }
539 return(FALSE);
540
541 case C_HELP:
542 print_commands();
543 break;
544
545 case C_INPUT_PARAMETERS:
546 input_parameters(sys);
547 break;
548
549 case C_OUTPUT_PARAMETERS:
550 output_parameters(sys);
551 break;
552
553 case C_OUTPUT_STATUS:
554 output_status(sys);
555 break;
556
557 case C_COUNT: {
558 var_filter_t vfilter;
559 rel_filter_t rfilter;
560
561 vfilter.matchbits = 0; /* allow all */
562 rfilter.matchbits = 0; /* allow all */
563 FPRINTF(stdout,"There are %d variables and %d relations.\n",
564 slv_count_solvers_vars(sys,&vfilter),
565 slv_count_solvers_rels(sys,&rfilter));
566 break;
567 }
568
569 case C_COUNT_IN_BLOCK: {
570 slv_status_t s;
571 int32 bnum;
572 linsolqr_system_t lsys;
573 mtx_matrix_t mtx;
574 mtx_region_t reg;
575
576 slv_get_status(sys,&s);
577 PRINTF("Block number [%d]: ",s.block.current_block);
578 bnum = (int32)readlong((long)s.block.current_block);
579 lsys = slv_get_linsolqr_sys(sys);
580 mtx = linsolqr_get_matrix(lsys);
581 mtx_block(mtx,bnum,&reg);
582 FPRINTF(stdout,"There are %d variables in block %d.\n",
583 (reg.col.high-reg.col.low+1),bnum);
584 FPRINTF(stdout,"There are %d relations in block %d.\n",
585 (reg.row.high-reg.row.low+1),bnum);
586 break;
587 }
588
589 case C_WRITE_VAR: {
590 int32 n;
591 struct var_variable *var;
592
593 PRINTF("Which variable [0]: ");
594 n = (int32)readlong(0L);
595 var = slv_get_master_var_list(sys)[n];
596 write_var(stdout,sys,var);
597 break;
598 }
599
600 case C_WRITE_VARS: {
601 var_filter_t vfilter;
602 vfilter.matchbits = 0;
603 write_varlist(stdout,sys,&vfilter,FALSE);
604 break;
605 }
606
607 case C_WRITE_VARS_IN_BLOCK: {
608 struct var_variable **vp = slv_get_solvers_var_list(sys);
609 slv_status_t s;
610 int32 bnum;
611 linsolqr_system_t lsys;
612 mtx_matrix_t mtx;
613 mtx_region_t reg;
614
615 slv_get_status(sys,&s);
616 PRINTF("Block number [%d]: ",s.block.current_block);
617 bnum = (int32)readlong((long)s.block.current_block);
618 lsys = slv_get_linsolqr_sys(sys);
619 mtx = linsolqr_get_matrix(lsys);
620 mtx_block(mtx,bnum,&reg);
621 for( ; reg.col.low <= reg.col.high; reg.col.low++ ) {
622 struct var_variable *var = vp[mtx_col_to_org(mtx,reg.col.low)];
623 write_var(stdout,sys,var);
624 }
625 break;
626 }
627
628 case C_TOGGLE_SUPPRESS_REL:
629 PRINTF("Relations will %sbe printed now.\n",
630 (suppress_rel_flag = !suppress_rel_flag)?"not " : "");
631 break;
632
633 case C_WRITE_REL: {
634 struct rel_relation *rel;
635 int32 n;
636
637 PRINTF("Which relation [0]: ");
638 n = (int32)readlong(0L);
639 rel = slv_get_master_rel_list(sys)[n];
640 write_rel(stdout,sys,rel,suppress_rel_flag);
641 break;
642 }
643
644 case C_WRITE_RELS: {
645 rel_filter_t rfilter;
646 rfilter.matchbits = 0;
647 write_rellist(stdout,sys,&rfilter,FALSE,suppress_rel_flag);
648 break;
649 }
650
651 case C_WRITE_RELS_IN_BLOCK: {
652 struct rel_relation **rp = slv_get_solvers_rel_list(sys);
653 slv_status_t s;
654 int32 bnum;
655 linsolqr_system_t lsys;
656 mtx_matrix_t mtx;
657 mtx_region_t reg;
658
659 slv_get_status(sys,&s);
660 PRINTF("Block number [%d]: ",s.block.current_block);
661 bnum = (int32)readlong((long)s.block.current_block);
662 lsys = slv_get_linsolqr_sys(sys);
663 mtx = linsolqr_get_matrix(lsys);
664 mtx_block(mtx,bnum,&reg);
665 for( ; reg.row.low <= reg.row.high; reg.row.low++ ) {
666 struct rel_relation *rel = rp[mtx_row_to_org(mtx,reg.row.low)];
667 write_rel(stdout,sys,rel,suppress_rel_flag);
668 }
669 break;
670 }
671
672 case C_WRITE_OBJ:
673 write_obj(stdout,sys);
674 break;
675
676 case C_CHECK_DIMENSIONS: {
677 /* broken 12/05 - checkdim not implemented */
678 FPRINTF(ASCERR, "Error - checkdim not implemented.\n");
679 /*
680 chkdim_system_t chk;
681 slv_parameters_t p;
682 / * expr_t obj; * /
683
684 PRINTF("Creating system . . .\n");
685 chkdim_create_system(&chk,slv_get_master_rel_list(sys));
686 / * if( (obj=slv_get_obj_function(sys)) != NULL )
687 chkdim_append_expr(&chk,obj); * /
688
689 PRINTF(". . . done. Check system . . .\n");
690 slv_get_parameters(sys,&p);
691 chkdim_check_dimensions(&chk,&p);
692
693 PRINTF(". . . done. Assign dimensions to variables? [no] ");
694 if( input_boolean(FALSE) )
695 chkdim_assign_dimensions(&chk,&p);
696 chkdim_destroy_system(&chk);
697 */
698 break;
699 }
700
701 case C_ELIGIBLE_SOLVERS: {
702 int cur,i;
703 const struct gl_list_t *L;
704 const SlvFunctionsT *S;
705 /* broken 6/96 baa */
706 /* maybe fixed -- JP Jun 2007 */
707 cur = slv_get_selected_solver(sys);
708 L = solver_get_engines();
709 PRINTF("Solver Name ?Eligible\n");
710 PRINTF("-----------------------------\n");
711 for(i=1; i<=gl_length(L); ++i){
712 S = gl_fetch(L,i);
713 PRINTF("%c%3d %-11s %s\n",
714 (S->number==cur?'*':' '),S->number,S->name,
715 yorn(slv_eligible_solver(sys)) );
716 }
717 break;
718 }
719
720 case C_SELECT_SOLVER: {
721 const struct gl_list_t *L;
722 const SlvFunctionsT *S;
723 L = solver_get_engines();
724 PRINTF("Solver Name\n");
725 PRINTF("----------------------\n");
726 long unsigned i;
727 for(i=1; i<=gl_length(L); ++i){
728 S = (const SlvFunctionsT *)gl_fetch(L,i);
729 PRINTF("%4d %s\n",S->number,S->name);
730 }
731 PRINTF("Which solver? [%d]: ",(int)(i=slv_get_selected_solver(sys)));
732 i = (int)readlong((long)i);
733
734 slv_select_solver(sys,i);
735 break;
736 }
737
738 case C_SELECTED_SOLVER: {
739 int n;
740 n = slv_get_selected_solver(sys);
741 PRINTF("Selected solver is %d (%s).\n",n,slv_solver_name(n));
742 break;
743 }
744
745 case C_PRESOLVE:
746 slv_presolve(sys);
747 output_status(sys);
748 break;
749
750 case C_RESOLVE:
751 slv_resolve(sys);
752 output_status(sys);
753 break;
754
755 case C_ITERATE:
756 slv_iterate(sys);
757 output_status(sys);
758 break;
759
760 case C_SOLVE:
761 slv_solve(sys);
762 output_status(sys);
763 break;
764
765 case C_DUMP_SYSTEM: {
766 FILE *out;
767 char fname[200];
768 struct var_variable **vp;
769 struct rel_relation **rp;
770
771 PRINTF("Target filename [stdout]: ");
772 readln(fname,sizeof(fname));
773 out = (*fname=='\0') ? stdout : fopen(fname,"w");
774 if( out == NULL ) {
775 FPRINTF(stderr,"Unable to open %s for writing.\n",fname);
776 break;
777 }
778
779 for( vp=slv_get_master_var_list(sys) ; *vp != NULL ; ++vp ) {
780 FPRINTF(out,"; v%d <--> ",var_sindex(*vp));
781 write_var(out,sys,*vp);
782 PUTC('\n',out);
783 }
784 for( rp=slv_get_master_rel_list(sys) ; *rp != NULL ; ++rp ) {
785 FPRINTF(out,"; r%d <--> ",rel_sindex(*rp));
786 write_rel(out,sys,*rp,suppress_rel_flag);
787 PUTC('\n',out);
788 }
789 output_system(out,sys);
790
791 if( out != stdout )
792 fclose(out);
793 break;
794 }
795
796 /* case C_PLOT: {
797 char args[50];
798 static char plotfilename[] = "~/ascend.plot";
799
800 if( !plot_allowed(inst) ) {
801 PRINTF("Not allowed to plot instance: wrong type.\n");
802 break;
803 }
804
805 PRINTF("Command line arguments: ");
806 readln(args,sizeof(args));
807 plot_prepare_file(inst,plotfilename);
808 PRINTF("Plot left in %s\n",plotfilename);
809 break;
810 }*/
811 }
812 return(TRUE);
813 }
814
815 #define USER_SAYS_KEEP \
816 (PRINTF("Keep previous system? [yes]: ") , input_boolean(TRUE))
817 /**
818 *** Asks user if system should be kept, and returns response.
819 **/
820
821 void Solve(struct Instance *i)
822 {
823 if( sys != NULL )
824 if( inst != i || !USER_SAYS_KEEP ) {
825 system_destroy(sys);
826 sys = NULL;
827 }
828
829 if( sys == NULL ) {
830 sys = system_build(i);
831 PRINTF("Presolving . . .\n");
832 do_command(C_PRESOLVE);
833 }
834
835 inst = i;
836 while( do_command(input_command()) )
837 ;
838 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22