/[ascend]/trunk/ascend4/bin/ascend2gms.pl
ViewVC logotype

Contents of /trunk/ascend4/bin/ascend2gms.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Fri Oct 29 20:54:12 2004 UTC (17 years, 2 months ago) by aw0a
File MIME type: text/x-perl
File size: 11569 byte(s)
Setting up web subdirectory in repository
1 #!/usr/local/bin/perl
2
3 # Created 12/96 by Chad Farschman, Mark Thomas
4 # the input to the program comes from the command line
5 # the first argument after the executable is the name of the
6 # file you want to parse. This is a file generated from
7 # ASCEND4 using the dbg_write_slv0_sys filename command.
8 # Here filename is the name of the ascend output.
9 #
10 # The name of the gams output is obtained by munging the
11 # name of the input.
12 #
13 # $Revision: 1.4 $
14 # $Author: ballan $
15 # $Date: 1997/07/05 14:11:59 $
16 #
17
18 $filename = shift(@ARGV);
19
20 # this may not be very robust (n != 1 '.' in a name may mess it up)
21 # split the list filename into the base and its extension
22 ($base,$extension) = split(/\./,$filename);
23
24 # concatenate the base with the extension .con
25 $outfile = $base."\.con";
26
27 # concatenate the base with the extension .lk
28 $lookup = $base."\.lk";
29
30 open(OUTPUT, ">$outfile");
31 open(INPUT, "$filename");
32
33 # the following are GAMS reserved syntax words in equations
34 $reserved{"abs"} = 1;
35 $reserved{"exp"} = 1;
36 $reserved{"log"} = 1;
37 $reserved{"sqrt"} = 1;
38 $reserved{"sqr"} = 1;
39 $reserved{"if"} = 1;
40 $reserved{"na"} = 1;
41 $reserved{"ne"} = 1;
42 $reserved{"no"} = 1;
43
44 # the following words are also reserved in gams input, though
45 # we don't know if we need to treat them specially yet.
46 #
47 # These are reserved statement starters:
48 # ABORT ASSIGN EQUATION[i] INTEGER OPTION[i] SET[i] SOS2
49 # ACRONYM[i] BINARY FREE LOOP PARAMETER[i] SOLVE TABLE
50 # ALIAS DISPLAY MODEL[i] POSITIVE SOS1 VARIABLE[i]
51 # NEGATIVE SCALAR[i]
52 #
53 # Comma is also reserved in gams in some contexts.
54 #
55 # Other words gams reserves:
56 # ACRONYMS ALL AND CARD DISPLAY EPS EQ GE GT INF LE LT MAXIMIZING
57 # MINIMIZING MODELS NOT OR ORD PARAMETERS PROD SETS SMAX SMIN
58 # USING VARIABLES XOR YES
59 # =L= .. =G= =E= =N= -- ++ **
60
61
62 # the following are direct conversion
63 $name{"ln"} = "log";
64 $name{"="} = ")=e=(";
65
66 # initialize the following variables (counters)
67 $solve = 0;
68 $variables = 0;
69 $parameters = 0;
70 $objective = 0;
71 $bounds = 0;
72 $relation = 0;
73 $linenum = 0;
74 $parameternum = 0;
75 $equationnum = 0;
76 $negcount = 0;
77 $poscount = 0;
78 $count = 0;
79 $fix = 0;
80 $paracount = 0;
81 $numvariables = 0;
82 $relnomcount = 0;
83 $previousterm = "";
84
85 # the following is header information for the gams input file
86 print OUTPUT "\$TITLE \n";
87 print OUTPUT "\$inlinecom /* */\n";
88 print OUTPUT "\n";
89 print OUTPUT "/* Turn off the listing of the input file */\n";
90 print OUTPUT "\$offlisting\n";
91 print OUTPUT "\n";
92 print OUTPUT "/* Turn off the listing and cross-reference of the symbols used */\n";
93 print OUTPUT "\$offsymxref offsymlist\n";
94 print OUTPUT "\n";
95 print OUTPUT "option\n";
96 print OUTPUT " limrow = 0, /* equations listed per block */\n";
97 print OUTPUT " limcol = 0, /* variables listed per block */\n";
98 print OUTPUT " solprint = on, /* solver's solution output printed */\n";
99 print OUTPUT " sysout = off; /* solver's system output printed */\n";
100 print OUTPUT "\n";
101
102 # begin counting variables at the letter a
103 $n = "a";
104
105 while (<INPUT>) {
106 $linenum = $linenum + 1;
107
108 # set some switches so you know where you are in the file you are parsing
109 if (/^Solver: (.*)/){
110 $solve = 1;
111 }
112 if (/^Variables: (.*)/){
113 $solve = 0;
114 $variables = 1;
115 }
116 if (/^Parameters: (.*)/){
117 $variables = 0;
118 $parameters = 1;
119 }
120 if (/^Objective: (.*)/){
121 $parameters = 0;
122 $objective = 1;
123 }
124 if (/^Boundaries: (.*)/){
125 $objective = 0;
126 $bounds = 1;
127 }
128 if (/^Relations: (.*)/){
129 $bounds = 0;
130 $relation = 1;
131 }
132
133 # split up the table of information given and output that information in the standard gams format
134 # this information will go at the end of a gams file
135 # should only do this for information between Variables and Objective headers
136
137 if(($variables == 1) && ($linenum >= 5)){
138 $numvariables = $variables + 1;
139 if(length($_) > 1){
140 ($variable, $value, $nominal, $lower, $upper, $fixed) = split(' ', $_);
141
142 # the following line deletes the character " and replaces it with nothing
143 $variable =~ s/\"//g;
144
145 $name{$variable} = $n++;
146 if($reserved{$n}){
147 $n++;
148 }
149
150 if($upper >= 1e+20){
151 $upper = 1e+20;
152 }
153 if($lower <= -1e+20){
154 $lower = -1e+20;
155 }
156
157 if($fixed == 0){
158
159 # if the lower bound of the variable is 0, then the variable is positive
160 if($lower == 0){
161 $poscount = $poscount + 1;
162 @posvararray = (@posvararray, $name{$variable});
163 @posvalarray = (@posvalarray, $value);
164 @posloarray = (@posloarray, $lower);
165 @posuparray = (@posuparray, $upper);
166
167 # if the upper bound of the variable is 0, then the variable is negative
168 }elsif($upper == 0){
169 $negcount = $negcount + 1;
170 @negvararray = (@negvararray, $name{$variable});
171 @negvalarray = (@negvalarray, $value);
172 @negloarray = (@negloarray, $lower);
173 @neguparray = (@neguparray, $upper);
174
175 # if the above two don't apply, the variable is neither
176 }else{
177 $count = $count + 1;
178 @vararray = (@vararray, $name{$variable});
179 @valarray = (@valarray, $value);
180 @loarray = (@loarray, $lower);
181 @uparray = (@uparray, $upper);
182 }
183 }else{
184
185 # the variable is fixed
186 $fix = $fix + 1;
187 @fixarray = (@fixarray, $name{$variable});
188 @fixvalarray = (@fixvalarray, $value);
189 }
190 }
191 }
192 # if the keyword ITERATIONS is found, we are at the end of the file
193 # (essentially). Turn the variable RELATION off. Thus, the rest of
194 # the file will be ignored.
195 if(/^Iterations: (.*)/){
196 $relation = 0;
197 }
198
199 # if we are in the parameters subsection
200 if($parameters == 1){
201 if($parameternum <= 1){
202 $parameternum = $parameternum + 1;
203 }else{
204 if(length($_) > 1){
205 ($parameter, $value) = split(' ', $_);
206
207 # the following line deletes the character " and replaces it with nothing
208 $parameter =~ s/\"//g;
209
210 $name{$parameter} = $n++;
211 if($reserved{$n}){
212 $n++;
213 }
214 $paracount = $paracount + 1;
215 @paraarray = (@paraarray, $name{$parameter});
216 @paravalarray = (@paravalarray, $value);
217 }
218 }
219 }
220
221 # if we are in the relations subsection
222 if($relation == 1){
223 if($equationnum == 0){
224 $equationnum = $equationnum + 1;
225 }else{
226
227 # if the NOMINAL keyword is found
228 if(/^.*Nominal: (.*)/){
229 chop($_);
230 ($extra1,$extra2,$relnom) = split(' ',$_);
231 $relnomcount = $relnomcount + 1;
232 @relnomarray = (@relnomarray, $relnom);
233 }else{
234
235
236 # if the CONDITIONS keyword is found, don't print anything.
237 if(/^.*Conditions: (.*)/){
238
239 # if the CONDITIONS keyword isn't found, we are at an equation.
240 }else{
241
242
243 # following line removes the return character from the line
244 chop($_);
245
246 # the following line removes the " from the equation
247 $_ =~ s/\"//g;
248
249 # the following line replaces variables such as -d.flash.x
250 # with -1 * d.flash.x. The variables is not recognizable
251 # otherwise.
252 $_ =~ s/-([a-zA-Z])/-1 \* $1/g;
253
254 # the following code replaces abs with sqrt(sqr)
255 while(/\A(.*\b(abs)\s*)\((.*)\Z/s){
256 $_ = $1 . 'sqrt(sqr(';
257 $operator = $2;
258 $balance_me = $3;
259 $balance = 1;
260
261 foreach $c ( split( /([\(\)])/, $balance_me ) ) {
262 if ( $balance == 0 ) {
263 $_ .= $c;
264 }
265 elsif ( $c eq '(' ) {
266 $balance++;
267 $_ .= $c;
268 }
269 elsif (( $c eq ')' ) && ( --$balance == 0 )) {
270 $_ .= '))';
271 }
272 else {
273 $_ .= $c;
274 }
275 }
276 $_ =~ s/abssqrt\(sqr\(/sqrt\(sqr\(/;
277 }
278
279 # end the code to replace abs with sqrt(sqr)
280
281 # this breaks up an equation into its respective parts and
282 # evaluates each part (replacing and deleting as necessary).
283
284 @term = split(/([\s\(\)])/,$_);
285 $termcount = 0;
286 foreach $term (@term){
287 $termcount = $termcount + 1;
288
289 # the following line replaces ^ with ** for power functions
290 $term =~ s/\^/\**/g;
291
292 # the following line replaces terms such as -1 with (-1).
293 # this prevents problems with double operators such as
294 # flash.x + -flash.y = 1
295 $term =~ s/\s-\w/\s\(-\w\)/g;
296
297 if($name{$term} ne ""){
298 $term = $name{$term};
299 }
300 }
301
302 # before the terms are joined back together, determine the string
303 # length and join so that the equation printed to each line is no
304 # more than 100 characters.
305 $totlen = 0;
306 for($i=1; $i <= $termcount; $i++){
307
308 # if the length of the string is zero, we are at the beginning of equation
309 if($totlen == 0){
310 $totlen = length($term[$i - 1]);
311 if($term[$i - 1] < 0){
312 $_ = "(".$term[$i - 1].")";
313 }else{
314 $_ = $term[$i - 1];
315 }
316 }else{
317
318 # if the length of the current string plus the new string is greater than 100
319 # we will not add the two string directly. Instead we will add a carriage
320 # return between the strings and reset the length counter to the length of
321 # the present string
322 $totlen = $totlen + length($term[$i-1]);
323 if($totlen < 100){
324 if($term[$i - 1] < 0){
325 $_ = $_."(".$term[$i - 1].")";
326 }else{
327 $_ = $_.$term[$i-1];
328 }
329 }else{
330 if($term[$i - 1] < 0){
331 $_ = $_."\n\t"."(".$term[$i - 1].")";
332 }else{
333 $_ = $_."\n\t".$term[$i-1];
334 }
335 $totlen = length($term[$i-1]);
336 }
337 }
338 }
339 @eqnarray = (@eqnarray, $_);
340 # increment the equation counter
341 $equationnum = $equationnum + 1;
342
343 }
344 }
345 }
346 }
347 }
348
349 # print the output in gams format
350 if($paracount > 0){
351 print OUTPUT "\nparameters\n";
352 for($i=1; $i <= $paracount; $i++){
353 if($i == $paracount){
354 print OUTPUT $paraarray[$i - 1],";\n";
355 }else{
356 print OUTPUT $paraarray[$i - 1],"\n";
357 }
358 }
359 }
360 if($poscount > 0){
361 print OUTPUT "\npositive variables\n";
362 for($i=1; $i <= $poscount; $i++){
363 if($i == $poscount){
364 print OUTPUT $posvararray[$i - 1],";\n";
365 }else{
366 print OUTPUT $posvararray[$i - 1],"\n";
367 }
368 }
369 }
370 if($negcount > 0){
371 print OUTPUT "\nnegative variables\n";
372 for($i=1; $i <= $negcount; $i++){
373 if($i == $negcount){
374 print OUTPUT $negvararray[$i - 1],";\n";
375 }else{
376 print OUTPUT $negvararray[$i - 1],"\n";
377 }
378 }
379 }
380 if($count > 0){
381 print OUTPUT "\nvariables\n";
382 for($i=1; $i <= $count; $i++){
383 print OUTPUT $vararray[$i - 1],"\n";
384 }
385 }
386 if($fix > 0){
387 for($i=1; $i <= $fix; $i++){
388 print OUTPUT $fixarray[$i - 1],"\n";
389 }
390 }
391 print OUTPUT "z1;\n";
392
393 print OUTPUT "\nequations\n";
394 for($i = 1; $i<$equationnum; $i++){
395 print OUTPUT "eqn",$i," \n";
396 }
397
398 print OUTPUT "obj1 ;\n\n";
399
400 for($i = 0; $i<$equationnum-1; $i++){
401 print OUTPUT "eqn",$i+1,".. ","1/",$relnomarray[$i],"*(",$eqnarray[$i],")/",$relnomarray[$i],";\n\n";
402 }
403
404
405 print OUTPUT "obj1.. z1 =e= ;\n";
406 print OUTPUT "\n";
407 print OUTPUT "Model unitop /all/;\n";
408 print OUTPUT "\n";
409
410 print OUTPUT "unitop.SCALEOPT = 1;\n";
411 print OUTPUT "unitop.OPTFILE = 1;\n";
412 print OUTPUT "\n";
413
414 for($i = 0; $i< $poscount; $i++){
415 # print OUTPUT $posvararray[$i], ".lo=",$posloarray[$i],";\n";
416 print OUTPUT $posvararray[$i], ".up=",$posuparray[$i],";\n";
417 print OUTPUT $posvararray[$i], ".l=",$posvalarray[$i],";\n";
418 }
419
420 for($i = 0; $i< $negcount; $i++){
421 print OUTPUT $negvararray[$i], ".lo=",$negloarray[$i],";\n";
422 # print OUTPUT $negvararray[$i], ".up=",$neguparray[$i],";\n";
423 print OUTPUT $negvararray[$i], ".l=",$negvalarray[$i],";\n";
424 }
425
426 for($i = 0; $i< $count; $i++){
427 print OUTPUT $vararray[$i], ".lo=",$loarray[$i],";\n";
428 print OUTPUT $vararray[$i], ".up=",$uparray[$i],";\n";
429 print OUTPUT $vararray[$i], ".l=",$valarray[$i],";\n";
430 }
431
432 for($i = 0; $i< $fix; $i++){
433 print OUTPUT $fixarray[$i], ".fx=",$fixvalarray[$i],";\n";
434 }
435
436 for($i = 0; $i< $paracount; $i++){
437 print OUTPUT $paraarray[$i], "=",$paravalarray[$i],";\n";
438 }
439
440 print OUTPUT "\nSolve unitop using nlp minimizing z1;\n";
441
442 # need to produce a look-up table for the variables
443 open(TABLE, ">$lookup");
444 foreach $name(%name){
445 print TABLE $name,"\n";
446 }

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