forked from DREAM-DK/MAKRO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamY.py
1390 lines (1188 loc) · 48.6 KB
/
gamY.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
A preprocessor for GAMS files (.gms files), implementing a number of additional features.
Basic syntax:
Nothing is case sensitive, just like in GAMS
All commands (preprocessor macros) start with a "$"
'#'' is used for comments instead of '*'
Items can be separated by commas, line breaks, or a mix.
Commands are processed in the order they appear and can be nested.
Flow control statements (IF, LOOP, FOR, and FUNCTION) are processed before other commands.
Nested flow control commands must be identified with a number, e.g.
$IF1 <condition>:
$IF2 <condition>:
$ENDIF2
$ENDIF1
Available commands:
$GROUP <variables or groups>;
A group is a data structure containing variables.
New variables should always be defined using the $GROUP command rather than a GAMS VARIABLES statement.
The group command groups together variables so that they can be manipulated together more easily, using other gamY commands such as $FIX, $UNFIX, $LOOP, $DISPLAY, or $GROUP.
Variable elements can be selectively included in a group using dollar conditions. Note that conditions ALWAYS need to be enclosed in round brackets.
Groups can be added together (union operation) or removed (complement operation).
Example:
$GROUP G_newGroup
var1[t] "label for variable 1"
var2[a,t]$(a.val >= 18) "label for variable 2"
G_oldGroup
var3[a,t], -var3$(a.val < 18) # Equivalent to var2
;
$BLOCK <block name> <equations> $ENDBLOCK
A block is a data structure containing equations.
New equations should always be defined using the $BLOCK command rather than a GAMS EQUATIONS statement.
The block command bundles together equations so that they can be manipulated together more easily, using other gamY commands such as $LOOP or $MODEL.
Example:
$BLOCK B_myBlock
E_eq1[t].. v1[t] =E= v2;
$EndBlock
$MODEL <model name> <equations and/or blocks/models>;
$MODEL can use a mix of Models, Blocks, and Equations.
Example:
$MODEL M_newModel
M_oldModel
B_myBlock
E_eq2
;
$LOOP <group name>: <content replacing {NAME}, {SETS}, {CONDITIONS}> $ENDLOOP
Example:
$LOOP G_myGroup:
parameter saved_{name}{sets};
saved_{name}{sets} = {name}.L{sets};
$EndLoop
$LOOP <block name>: <content replacing {NAME}, {SETS}, {CONDITIONS}, {LHS}, or {RHS}> $ENDLOOP
Example:
$LOOP B_myBlock:
{name}_ss{sets}$(tx0[t] and {conditions}) {LHS} =E= {RHS};
$EndLoop
$FIX <variables or groups>;
Exogenous variables.
Equivalent to writing
<variable>.fx[<variable>] = <variable>.l[<sets>];
for each variable.
$UNFIX[(<lower bound>, <upper bound>)] <variables or groups>;
Endogenize variables.
Equivalent to writing
<variable>.lo[<variable>] = <lower bound>;
<variable>.up[<variable>] = <upper bound>;
for each variable.
If no bounds are given, lower and upper bounds are set to -inf and inf respectively.
$DISPLAY <variables or groups>;
$IMPORT <filename>
Include a separate file in this file. Note that the regular GAMS $INCLUDE still exists (and is faster). Use @IMPORT if imported file should be processed with gamY.
$IF <condition expression>: <content> $ENDIF
If statement where the condition is evaluated in python.
$FOR <python expression>: <content> $ENDFOR
$For {parameter}, {value} in [("a", 1), ("b",2)]:
{parameter} = {value};
$EndFor
$FUNCTION <function name>([<argument name>, <...>]) $ENDFUNCTION
Define a new gamY function.
@<function name>([<argument value>, <...>])
Call a previously defined function.
$REPLACE $ENDREPLACE
Find and replace.
$REGEX $ENDREGEX
Find and replace with regular expressions (wild card search).
Save/Read options
As in regular GAMS, runs can be saved and read using the options s=<filename> and r=<filename>
Group definitions etc. are saved in and read from a pkl file along side the GAMS g00 file.
$FIX ALL;
$UNFIX G_myGroup$(subset1[t])
$FIX var1;
$If %my_var% == conditional:
# then do stuff here
$EndIf
Implementation notes
The preprocessor uses 'brute force' regular expressions to find and replace the new macro commands.
Commands are processed in the order they appear and can be nested (using recursive descent parsing).
A tokenizer is not used and is unlikely to simplify the code as syntax varies for each command,
and only the commands, rather than all the GAMS code, need to be parsed.
The program should be rewritten using a tokenizer, lexer, and parser to allow for unlimited nesting of flow control statements,
and to make the syntax more robust.
"""
import sys
import os
import shutil
import subprocess
import pickle
import re
from math import ceil, floor
from heapq import heappush
import itertools # Useful in FOR loops in MAKRO
# gamY data objects
from classes import Variable, Equation, Function, MockMatch, Group, Block, CaseInsensitiveDict
# The regex patterns used to match commands
from patterns import PATTERNS
class Precompiler:
"""Object with methods to parse each gamY command"""
def __init__(self, file_path, patterns=PATTERNS, add_adjust=None, mult_adjust=None):
self.groups = CaseInsensitiveDict({"all": {}})
self.groups_conditions = CaseInsensitiveDict({"all": {}})
self.pgroups = CaseInsensitiveDict({"all": {}})
self.pgroups_conditions = CaseInsensitiveDict({"all": {}})
self.blocks = CaseInsensitiveDict()
self.globals = dict(os.environ)
self.user_functions = CaseInsensitiveDict()
self.locals = {}
self.has_read_file = False
self.adjustment_terms = []
self.add_adjust = add_adjust
self.mult_adjust = mult_adjust
if add_adjust:
self.adjustment_terms.append(add_adjust)
if mult_adjust:
self.adjustment_terms.append(mult_adjust)
for term in self.adjustment_terms:
self.groups[term] = Group()
self.groups_conditions[term] = {}
self.model_counter = 0 # As models cannot be redefined, we increment the names of temporary models defined
self.patterns = patterns
self.file_path = os.path.abspath(file_path)
self.file_dir, self.file_name = os.path.split(self.file_path)
# Check that LST folder exists
list_file_dir = os.path.join(self.file_dir, "LST")
if not os.path.exists(list_file_dir):
os.makedirs(list_file_dir)
list_file_name = self.file_name.replace(".gms", ".lst")
self.list_file_path = os.path.join(list_file_dir, list_file_name)
# In cases where a command should be parsed by gamY, but also left for GAMS to parse, we replace special characters temporarily
self.DOLLAR_SUB = "¤dollar¤"
self.SEMICOLON_SUB = "¤semicolon¤"
self.PERCENT_SUB = "¤percent¤"
self.AT_SUB = "¤at¤"
self.star_comment_pattern = re.compile(r"^\*.*", re.MULTILINE)
self.hashtag_comment_pattern = re.compile(r"\#.*", re.MULTILINE)
self.block_comment_pattern = re.compile(r"^\$ontext.*?\$offtext", re.IGNORECASE | re.MULTILINE | re.DOTALL)
# Maps between regex patterns and the correpsonding method for each gamY command
self.recursive_commands = [
("set", self.set_env_variable),
("user_function", self.user_function),
("eval", self.eval),
("import", self.import_file),
("block", self.block_define),
("solve", self.solve),
("model", self.model_define),
("group", self.group_define),
("pgroup", self.pgroup_define),
("replace", self.sub),
("regex", self.regex),
("display", self.display),
("display_all", self.display_all),
("fix", self.fix_unfix),
]
self.top_down_commands = [ # Rememeber also to add these to the top down pattern in patterns.py
("define_function", self.define_function),
("env_variable", self.insert_env_variable),
("if", self.if_statements),
("for_loop", self.for_loop),
("loop", self.loop),
]
self.commands = self.recursive_commands + self.top_down_commands
self.prev_match = None
@property
def equations(self):
return {k: v for block in self.blocks.values() for k, v in block.items()}
def warning(self, msg):
print("WARNING: " + msg)
return "**** " + msg
def log(self, msg):
# print(msg)
return msg
def error(self, msg):
error_text = "ERROR!\ngamY could not process the file due to the following error:\n" + msg
with open(self.list_file_path, 'w+') as file:
file.write(error_text)
sys.exit(error_text)
def __call__(self):
# Read GAMS file
with open(self.file_path, 'r') as f:
text = "\n" + f.read()
if not self.has_read_file:
text = "$ONEOLCOM\n$EOLCOM #\n\n" + text # Add option for # comments if this is the first file being run
self.processed_text = ""
text = self.parse(text, top_level=True)
text = self.processed_text + text
text = self.dedent_dollar(text)
return self.restore_temporary_substitutions(text)
def parse(self, text, top_level=False):
while True:
text = self.clean_comments(text)
match = self.patterns["Any"].search(text)
if match and (repr(match) == repr(self.prev_match) and text == self.prev_text):
match = self.patterns["Any"].search(text, match.start() + 1) # Check one character in, so that we don't match the same pattern forever
self.prev_match = match
self.prev_text = text
if top_level and match:
self.processed_text += text[:match.start()]
text = text[match.start():]
if match:
# Check if commands needs top parsed right away, instead of recursively
if self.patterns["TopDown"].fullmatch(match.group(0)):
# print(f"TopDown approach used for match: {match.group(0)[:10]} [..] {match.group(0)[-10:]}")
text = text.replace(match.group(0), self.process_command(match.group(0)), 1)
else:
text = text.replace(match.group(0), self.parse(match.group(0)), 1) # Recursively parse
else:
return self.process_command(text) # Process commands when inner scope of recursion is reached
def round_parentheses(self, text):
return text.replace("[", "(").replace("]", ")")
def process_command(self, text):
for command, func in self.commands:
match = self.patterns[command].fullmatch(text) # match method is used (rather than search) to avoid matching inside commands when parsing top down
if match:
# print("***MATCH***", command)
text = text.replace(match.group(0), func(match, text), 1)
return text
def read(self, file_name):
"""
Read blocks, groups, and variables from saved file if the r=<file_name> option is used.
"""
self.has_read_file = True
try:
with open(os.path.join(self.file_dir, file_name) + ".pkl", 'rb') as f:
self.blocks, self.groups, self.groups_conditions, self.pgroups, self.pgroups_conditions, loaded_globals, self.user_functions = pickle.load(f)
print("Precompiler file read: " + file_name + ".pkl")
self.globals.update(loaded_globals)
except FileNotFoundError:
self.warning(f"gamY read file not found ({file_name}.pkl), macro groups have been reset")
def save(self, file_name):
"""
Save dicts of blocks, groups, and variables in pickle file if s=<file_name> option is used
"""
with open(os.path.join(self.file_dir, file_name) + ".pkl", 'wb') as f:
pickle.dump(
(self.blocks, self.groups, self.groups_conditions, self.pgroups, self.pgroups_conditions, self.globals, self.user_functions),
f, pickle.HIGHEST_PROTOCOL)
def comment_out(self, text):
return_string = text.replace("\n", "\n#")
return self.clean_comments(return_string)
def clean_comments(self, text):
"""
Return string with special characters removed from comments.
"""
for pattern in (self.star_comment_pattern, self.hashtag_comment_pattern, self.block_comment_pattern):
for match_text in pattern.findall(text):
replacement_text = match_text
replacement_text = replacement_text.replace("$", self.DOLLAR_SUB)
replacement_text = replacement_text.replace(";", self.SEMICOLON_SUB)
replacement_text = replacement_text.replace("%", self.PERCENT_SUB)
replacement_text = replacement_text.replace("@", self.AT_SUB)
text = text.replace(match_text, replacement_text)
return text
def restore_temporary_substitutions(self, text):
"""
Return string with special characters reinserted.
"""
text = text.replace(self.DOLLAR_SUB, "$")
text = text.replace(self.SEMICOLON_SUB, ";")
text = text.replace(self.PERCENT_SUB, "%")
text = text.replace(self.AT_SUB, "@")
return text
@staticmethod
def remove_comments(text):
"""
Return string with comments removed.
"""
star_comment_pattern = re.compile(r"^\#.*", re.MULTILINE)
hashtag_comment_pattern = re.compile(r"#.*", re.MULTILINE)
block_comment_pattern = re.compile(r"^\$ontext.*?\$offtext", re.IGNORECASE | re.MULTILINE | re.DOTALL)
for pattern in (star_comment_pattern, hashtag_comment_pattern, block_comment_pattern):
text = pattern.sub("", text)
return text
def insert_env_variable(self, match, text):
"""
Replace environmental variables with their value, e.g. %variable_name%
"""
key = match.group(1)
in_scope = self.in_scope()
if key in in_scope:
return in_scope[key]
else:
self.warning(f"\%{key}\% is not defined and was not replaced")
return self.PERCENT_SUB + key + self.PERCENT_SUB
def user_function(self, match, text):
"""
Insert user defined function call, e.g. @my_funct(), defined using $FUNCTION
"""
func_name, args = match.group(1), match.group(2)
if func_name not in self.user_functions:
self.error(f"@{func_name} has not been defined: {match.group(0)}")
func = self.user_functions[func_name]
args = re.findall(r"(?:[^,\[]+|(?:\[[^\]]*\]))+", args)
replacement_text = func.expression
self.log(f"User function <{func_name}> called with {args}")
for arg_name, arg in zip(func.args, args):
replacement_text = replacement_text.replace(arg_name, arg.strip())
return replacement_text
def in_scope(self):
in_scope = {}
in_scope.update(self.globals)
in_scope.update(self.locals) # locals overwrite globals
in_scope = {k: v for k, v in in_scope.items()}
return in_scope
def set_env_variable(self, match, text, eval_command=False):
"""
Read $set, $setglobal, and $setlocal commands
The commands are left intact to also be processed by GAMS
"""
key = match.group(2)
val = match.group(3)
if eval_command:
val = str(eval(val))
if match.group(1).lower() == "global":
self.globals[key] = val
else:
self.locals[key] = val
# replacement_text = match.group(0).replace("$", self.DOLLAR_SUB).lstrip()
# return replacement_text
return ""
def eval(self, match, text):
return self.set_env_variable(match, text, eval_command=True)
def if_statements(self, match, text):
"""
Parse $If .. $EndIf command.
To nest if statements use an id, e.g. $IF1 $ENDIF1
"""
id_ = match.group(1)
condition = self.parse(match.group(2))
expression = match.group(3)
if not id_:
id_ = " "
if f"$IF{id_}" in expression:
self.error(
f"""
Nested IF statements must be identified with id numbers (e.g. $IF1 .. $ENDIF1).
Condition: '{condition}'
Expression: '{expression}'
"""
)
condition = re.sub(r"(?<![=><!])=(?![=><!])", "==", condition)
condition = re.sub(r"<>", "!=", condition)
condition = re.sub(r"(?<![a-zA-Z])EQ(?![a-zA-Z])", "==", condition, flags=re.IGNORECASE)
condition = re.sub(r"(?<![a-zA-Z])NE(?![a-zA-Z])", "!=", condition, flags=re.IGNORECASE)
condition = re.sub(r"(?<![a-zA-Z])LT(?![a-zA-Z])", "<", condition, flags=re.IGNORECASE)
condition = re.sub(r"(?<![a-zA-Z])GT(?![a-zA-Z])", ">", condition, flags=re.IGNORECASE)
condition = re.sub(r"(?<![a-zA-Z])LE(?![a-zA-Z])", "<=", condition, flags=re.IGNORECASE)
condition = re.sub(r"(?<![a-zA-Z])GE(?![a-zA-Z])", ">=", condition, flags=re.IGNORECASE)
condition_trunc = condition.split("\n")[0]
replacement_text = (
"\n# " + "-" * 100 +
"\n# IF " + condition_trunc + ":"
"\n# " + "-" * 100 + "\n"
)
try:
if eval(condition.lower()):
replacement_text += expression
else:
replacement_text += "# If condition evaluated to false"
except Exception as e:
self.error(f"""Failed to evaluate IF-condition: {condition}
{e}""")
replacement_text += (
"\n# " + "-"*100 +
"\n# ENDIF" +
"\n# " + "-" * 100 + "\n"
)
return replacement_text
def import_file(self, match, text):
"""
Return text with $Import commands replaced by code in Import file
"""
file_name = os.path.join(self.file_dir, match.group(1))
replacement_text = (
"\n# " + "-"*100 +
"\n# Import file: " + file_name +
"\n# " + "-" * 100 + "\n"
)
if os.path.isfile(file_name):
try:
with open(file_name, "r") as f:
replacement_text += self.parse("\n"+f.read())
except FileNotFoundError:
replacement_text += self.warning(f"File was found, but could not be read: '{file_name}'")
else:
replacement_text += self.warning(f"File not found: '{file_name}'")
return replacement_text
def sub(self, match, text):
"""
Parse $REPLACE command
Example:
$REPLACE('t', 'tt'):
t_in_name[t]
$ENDREPLACE
->
tt_in_name(tt)
"""
old, new, count, expression = match.groups()
if count:
return expression.replace(old[1:-1], new[1:-1], int(count[1:]))
else:
return expression.replace(old[1:-1], new[1:-1])
def regex(self, match, text):
"""
Parse $REGEX command
First argument should be a string or python code without commas.
Second argument must be a string enclosed in '' or ""
Third arguments is an optional integer
Example:
$REGEX('\bt\b', 'tt'):
t_in_name[t]
$ENDREGEX
->
t_in_name(tt)
"""
id_, old, new, count, expression = match.groups()
if not id_:
id_ = " "
if f"$REGED{id_}" in expression:
self.error(
f"""
Nested REGED statements must be identified with id numbers (e.g. $REGED1 .. $ENDREGED1).
Condition: '{condition}'
Expression: '{expression}'
"""
)
# If argument is not a string, evaluate it
if not (old[0] == "'" or old[0] == '"'):
old = eval(old)
else:
old = old[1:-1]
pattern = re.compile(old, re.IGNORECASE | re.MULTILINE)
if count:
return pattern.sub(new[1:-1], expression, int(count[1:]))
else:
return pattern.sub(new[1:-1], expression)
def block_define(self, match, text):
"""
Block command syntax example:
$Block B_block_name
E_eq1[t].. v1[t] =E= v2;
$EndBlock
==>
# ***block_name****
Equation E_eq1[t];
Variable j_eq1[t];
j_eq1.L[t] = 0;
Variable jr_eq1[t];
jr_eq1.L[t] = 0;
E_eq1[t].. v1[t] =E= (1+jr_eq1[t]) * (j_eq1[t] + v2);
"""
equation_pattern = re.compile(r"""
(?:^|\,) # Check only beginning of line or after a comma.
\s* # Ignore whitespace
([^#*\s\(\[]+) # Name of equation
([(\[][^$]+?[)\]])? # Sets
\s*
(\$.+?)? # Set restrictions
\s*
\.\.
(.+?) # LHS
=E=
(.+?)\; # RHS
""", re.VERBOSE | re.MULTILINE | re.DOTALL | re.IGNORECASE)
block_name = match.group(1)
content = self.remove_comments(match.group(2))
replacement_text = (
"\n# " + "-"*ceil(50-len(block_name)/2) + block_name + "-"*floor(50-len(block_name)/2) +
"\n# Initialize " + block_name + " equation block" +
"\n# " + "-"*100 + "\n"
)
self.blocks[block_name] = Block()
for e_match in equation_pattern.finditer(content):
eq = Equation(*[v if v is not None else "" for v in e_match.groups()])
self.blocks[block_name][eq.name] = eq
replacement_text += f"EQUATION {eq.name}{eq.sets};"
RHS = eq.RHS
if self.add_adjust:
j_name = self.add_adjust + eq._name
j_group_name = f"{self.add_adjust}_{block_name}"
docstring = f"Additive adjustment term for equation {eq.name}"
replacement_text += f" VARIABLE {j_name}{eq.sets} \"{docstring}\"; {j_name}.FX{eq.sets} = 0;"
if j_group_name not in self.groups:
self.groups[j_group_name] = Group()
self.groups_conditions[j_group_name] = {}
j_var = Variable(j_name, eq.sets, docstring)
self.groups[self.add_adjust][j_name] = j_var
self.groups["all"][j_name] = j_var
self.groups[j_group_name][j_name] = j_var
if RHS.strip()[0] == "-":
RHS = f"{self.add_adjust}{eq._name}{eq.sets} {RHS}"
else:
RHS = f"{self.add_adjust}{eq._name}{eq.sets} + {RHS}"
if self.mult_adjust:
j_name = self.mult_adjust + eq._name
j_group_name = f"{self.mult_adjust}_{block_name}"
docstring = f"Multiplicative adjustment term for equation {eq.name}"
replacement_text += f"VARIABLE {j_name}{eq.sets} \"{docstring}\"; {j_name}.FX{eq.sets} = 0;"
if j_group_name not in self.groups:
self.groups[j_group_name] = Group()
self.groups_conditions[j_group_name] = {}
j_var = Variable(j_name, eq.sets, docstring)
self.groups[self.mult_adjust][j_name] = j_var
self.groups["all"][j_name] = j_var
self.groups[j_group_name][j_name] = j_var
RHS = f"(1+{self.mult_adjust}{eq._name}{eq.sets}) * ({RHS})"
replacement_text += "\n"+f"{eq.name}{eq.sets}{eq.conditions}.. {eq.LHS} =E= {RHS};"+"\n"
replacement_text += f"$MODEL {block_name} {block_name};"
return replacement_text
def model_define(self, match, text):
"""
Parse $MODEL command.
Define models from blocks, models, and equations.
Syntax example:
$MODEL M_myNewModel
M_oldModel
B_myBlock
E_eq
;
"""
item_pattern = re.compile(r"""
(?:^|\,) # Check only beginning of line or after a comma.
\s* # Ignore whitespace
(\-)? # Optional MINUS character if block or equation is to be removed instead of added ($1)
([^\,\;\s]+) # Name of equation ($2)
""", re.VERBOSE | re.MULTILINE)
equations = CaseInsensitiveDict({eq.name: eq for block in self.blocks.values() for eq in block.values()})
model_name = match.group(1)
content = self.remove_comments(match.group(2))
replacement_text = (
"\n# " + "-" * 100 +
"\n# Define " + model_name + " model" +
"\n# " + "-" * 100 +
"\nModel " + model_name + " /\n"
)
new_model = Block()
for item_match in item_pattern.finditer(content):
remove = item_match.group(1)
name = item_match.group(2)
if name in self.blocks:
for eq in self.blocks[name].values():
if remove:
new_model.pop(eq.name)
else:
new_model[eq.name] = eq
elif name in equations:
if remove:
if name in new_model:
new_model.pop(name)
else:
self.warning(
f"Equation {name} could not be removed from {model_name}, as it is not part of that block or model.")
else:
new_model[name] = equations[name]
else:
raise KeyError(name, " is not a valid equation, block of equations, or model")
for eq in new_model.values():
replacement_text += eq.name + ", "
replacement_text = replacement_text[:-2] + "\n/;\n"
# Define an equation block, so that the model can be used in the same ways a regular blocks
self.blocks[model_name] = new_model
# Define a group of all the adjustment variables in model, with the name "Adjust_[model_name]"
for j in self.adjustment_terms:
j_group_name = f"{j}_{model_name}"
if j_group_name not in self.groups:
self.groups[j_group_name] = {}
self.groups_conditions[j_group_name] = {}
for eq in new_model.values():
j_name = j+eq._name
self.groups[j_group_name][j_name] = self.groups[j][j_name]
return replacement_text
def group_define(self, match, text, init_val="0", parameter_group=False):
"""
Parse $GROUP command
Syntax example:
$GROUP G_newGroup
var1[a,t] "label for variable 1"
var2[t] "label for variable 2"
var3, var4
G_oldGroup
;
"""
group_variable_pattern = re.compile(r"""
(?:^|\,) # Check only beginning of line or after a comma.
\s* # Ignore whitespace
(\-)? # Optional MINUS character, if group or variable should be removed rather than added ($1)
\s*
([^$#*\s(\[,]+) # Name of variable ($2)
([(\[].*?[)\]])? # Optional sets ($3)
(\$[(\[].+?[)\]])? # Optional conditions ($4)
\s*
('.*?'|".*?")? # Optional label ($5)
(\s+-?\d+(?:\.\d*)?)? # Optional initial value of variable ($6)
\s*
(?=[\n\,\;]) # Variable separator (comma or new line)
""", re.VERBOSE | re.MULTILINE)
if parameter_group:
GROUPS = self.pgroups
CONDITIONS = self.pgroups_conditions
DECLARE = "PARAMETER "
L = ""
else:
GROUPS = self.groups
CONDITIONS = self.groups_conditions
DECLARE = "VARIABLE "
L = ".L"
group_name = match.group(1)
content = self.remove_comments(match.group(2))
replacement_text = ("\n# " + "-"*ceil(50-len(group_name)/2) + group_name + "-"*floor(50-len(group_name)/2) +
"\n# Initialize " + group_name + " group" +
"\n# " + "-" * 100 +
"\n$offlisting\n")
new_group = Group()
new_group_conditions = CaseInsensitiveDict()
# Loop over variables and groups to be added or removed from group
for item in group_variable_pattern.finditer(content):
remove, name, sets, item_conditions, label, level = item.group(1, 2, 3, 4, 5, 6)
if name in GROUPS:
variables = GROUPS[name].values()
old_group_conditions = CONDITIONS[name]
elif name in GROUPS["all"]:
variables = (GROUPS["all"][name],)
old_group_conditions = {}
elif remove:
self.error(
f"{name} is not a variable or group, and could not be removed from {group_name} (this might be due to a typo).")
else: # Initialize a new variable in a dummy group to loop over.
variables = (Variable(name, sets, label),)
old_group_conditions = {}
if not parameter_group and not label:
self.warning(
f"{name} was defined as a new variable without an explanatory text in group {group_name} (this might be due to a typo).")
for var in variables:
if sets and "$" in sets:
self.error(
f"""Conditionals in the GROUP statement must be surrounded by parentheses, e.g. $(d1[d]).
Error in {group_name}: {name}{sets}{item_conditions}""")
if remove:
if var.name not in new_group:
replacement_text += f"# {var.name} is not in group and could therefor not be removed""\n"
else:
remove_conditions = self.combine_conditions(item_conditions, old_group_conditions.get(var.name, None))
if remove_conditions:
new_group_conditions[var.name] = self.combine_conditions(new_group_conditions[var.name], "not " + remove_conditions)
else:
new_group.pop(var.name)
new_group_conditions.pop(var.name)
else:
new_conditions = self.combine_conditions(
item_conditions,
old_group_conditions.get(var.name, None),
)
if var.name in new_group:
new_conditions = self.combine_conditions(
new_conditions,
new_group_conditions[var.name],
intersect=False # We use OR (intersect) to merge groups
)
else:
new_group[var.name] = var, level
new_group_conditions[var.name] = new_conditions
for var, level in new_group.values():
# Declare the variables if new
if var.name in GROUPS["all"]:
new_group[var.name] = GROUPS["all"][var.name]
else:
replacement_text += DECLARE + var.name + var.sets + " \"" + var.label[1:-1] + "\";\n"
new_group[var.name] = var
if not level:
level = init_val
# Set levels if a value is given
if level:
if new_group_conditions[var.name]:
replacement_text += var.name + L + var.sets + "$" + new_group_conditions[var.name] + " = " + level + ";\n"
else:
replacement_text += var.name + L + var.sets + " = " + level + ";\n"
replacement_text += "$onlisting"
GROUPS[group_name] = new_group
GROUPS["all"] = Group(new_group, **GROUPS["all"])
CONDITIONS[group_name] = new_group_conditions
CONDITIONS["all"] = {k: None for k in GROUPS["all"]}
return replacement_text
def pgroup_define(self, match, text):
return self.group_define(match, text, parameter_group=True)
def define_function(self, match, text):
"""
Define gamY macros (multiline global variable)
Example:
$FUNCTION InflationCorrection:
$LOOP G_prices
{name}.l{sets} = {name}.l{sets} * inf_factor[t];
$ENDLOOP
$ENDFUNCTION
# Remove inflation and growth correction
%InflationCorrection%
"""
id_, name, args, expression = match.groups()
if not id_:
id_ = " "
if f"$FUNCTION{id_}" in expression:
self.error(f"Nested FUNCTION definitions must be identified with id numbers (e.g. $FUNCTION1 .. $ENDFUNCTION1): {match.groups()[:-1]}")
self.user_functions[name] = Function(name, args, expression)
replacement_text = (
"\n# " + "-"*100 +
"\n# Define function: " + name +
"\n# " + "-"*100 + "\n"
)
return replacement_text
def for_loop(self, match, text):
"""
Parse $FOR command.
Loops over the expression parsing the for loop with python syntax.
Examples:
$FOR &i in range(1,5):
a("t&i") = &i**2;
$ENDFOR
$FOR &i in ["Grane", "joao", "Martin"]:
$import &i.gms
$ENDFOR
For loops can be nested by adding an id number after the command
E.g.
$FOR {p1} in ["Lob"]:
$FOR1 {p2} in ["Bob"]:
$FOR2 {value} in ["Loblaw"]:
set {p1}{p2} /{value}/;
$ENDFOR2
$ENDFOR1
$ENDFOR
"""
id_ = match.group(1)
if not id_:
id_ = " "
iterators = self.parse(match.group(2)).replace(" ", "").split(",")
iterable = self.parse(match.group(3))
expression = match.group(4)
replacement_text = ""
if f"$FOR{id_}" in expression:
self.error(f"Nested FOR loops must be identified with id numbers (e.g. $FOR1 .. $ENDFOR1): {match.groups()[:-1]}")
try:
for i in eval(iterable):
replacement_text += expression
if len(iterators) == 1:
replacement_text = replacement_text.replace(iterators[0], str(i))
else:
for index, iterator in enumerate(iterators):
replacement_text = replacement_text.replace(iterator, str(i[index]))
except SyntaxError:
self.error(f"Failed to evalute: {iterable}""\n It is not a proper iterable.")
return replacement_text
def loop(self, match, text):
"""
Parse $Loop command.
Loops over the expression once for each variable in the group, or each equation in a block
replacing
"{NAME}" with the name of the variable or equation
"{SETS}" with the sets of the variable or equation
"{CONDITIONS}" with any conditionals (subsetting) of the equation. E.g. "$ax0(a)"
"{LHS}" with the left hand side of an equation.
"{RHS}" with the right hand side of an equation.
The {$} operator can be used to modify sets
Examples:
# Remove a and add t to all variables
$LOOP G_endo
{name}.l{sets}{$}[+t,-a] = {name}.l{sets};
$ENDLOOP
# Replace 'a' with 'a0'
$LOOP G_endo
{name}.l{sets}{$}[<a0>a] = {name}.l{sets};
$ENDLOOP
# Replace 'a' with 'a0(a)'
$LOOP G_endo
{name}.l{sets}{$}[a0[a]] = {name}.l{sets};
$ENDLOOP
"""
id_ = match.group(1)
if not id_:
id_ = " "
iterable_name = self.parse(match.group(2)) # Name of group or block to be iterated over
expression = match.group(3) # Inside of loop
# expression = self.round_parentheses(expression)
if f"$LOOP{id_}" in expression:
self.error(f"Nested loops must be identified with id numbers (e.g. $LOOP1 .. $ENDLOOP1): {match.groups()[:-1]}")
replacement_text = ("\n# " + "-"*100 +
"\n# Loop over " + iterable_name +
"\n# " + "-" * 100 + "\n")
if iterable_name in self.groups:
replacement_text += self.loop_over_variables(
expression,
self.groups[iterable_name].values(),
group_conditions=self.groups_conditions[iterable_name]
)
elif iterable_name in self.pgroups:
replacement_text += self.loop_over_variables(
expression,
self.pgroups[iterable_name].values(),
group_conditions=self.pgroups_conditions[iterable_name]
)
elif iterable_name in self.blocks:
replacement_text += self.loop_over_equations(expression, self.blocks[iterable_name].values())
elif iterable_name in self.groups["all"]:
# If looping over a single variable, we put it in a list to be iterable
replacement_text += self.loop_over_variables(expression, [self.groups["all"][iterable_name]], {iterable_name: None})
elif iterable_name in self.equations:
# If looping over a single equation, we put it in a list to be iterable
replacement_text += self.loop_over_equations(expression, [self.equations[iterable_name]])
else:
self.error('"{}" is not a block, group, or variable and cannot be looped over.'.format(iterable_name))
return replacement_text
def loop_over_variables(self, expression, variables, group_conditions):
"""
Loop over the variables in a group (used by $LOOP command)
"""
iter_patterns = {
"name": re.compile(r"{NAME}", re.IGNORECASE),
"sets": re.compile(r"{SETS?}?(\{\$\}\[[^$=\n;{}]+\])?", re.IGNORECASE),
"conditions": re.compile(r"{(SUBSET|CONDITION)S?}", re.IGNORECASE), # "SUBSETS" will be depreciated
"text": re.compile(r"{text}", re.IGNORECASE),
}
replacement_text = ""
for variable in variables:
sub = expression
# Replace sets
while iter_patterns["sets"].search(sub):
filter = iter_patterns["sets"].search(sub).group(1)
if filter:
sets = variable.sets[1:-1].replace(" ", "").split(",") # Remove parentheses and whitespace and splits sets into list
add_sets, subtract_sets, replace_sets = [], [], []
for f in filter[4:-1].replace(" ", "").split(","):
if f[0] == "+":
add_sets.append(f[1:])
elif f[0] == "-":
subtract_sets.append(f[1:])
else:
replace_sets.append(f)
filtered_sets = sets + add_sets
for s in sets:
for f in add_sets:
if f"[{f}]" in s: # If the variable's set is a subset of the add set, or if the add set is already present, don't add it
pass
elif "[{}]".format(s) in f:
filtered_sets.remove(s)
for f in subtract_sets:
if s == f or "[{}]".format(f) in s: # If the variable's sets is a subset of the filter set, the set should be removed
filtered_sets.remove(s)