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

Contents of /trunk/base/generic/solver/slv_interface.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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