gamspy 1.18.4__py3-none-any.whl → 1.19.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -129,6 +129,25 @@ class Condition(operable.Operable):
129
129
 
130
130
  @property
131
131
  def dimension(self) -> int:
132
+ """
133
+ The dimension of the records of the condition.
134
+
135
+ Returns
136
+ -------
137
+ int
138
+ Dimensionality
139
+
140
+ Examples
141
+ --------
142
+ >>> import gamspy as gp
143
+ >>> m = gp.Container()
144
+ >>> i = gp.Set(m, "i", records=["i1", "i2", "i3"])
145
+ >>> a = gp.Parameter(m, "a", domain=i, records=[("i1", 10), ("i2", 20), ("i3", 5)])
146
+ >>> condition = a[i].where[a[i] > 9]
147
+ >>> condition.dimension
148
+ 1
149
+
150
+ """
132
151
  if self.domain is None:
133
152
  return 0
134
153
 
@@ -224,6 +243,24 @@ class Condition(operable.Operable):
224
243
  return f"{conditioning_on_str} $ ({condition_str})" # type: ignore
225
244
 
226
245
  def getDeclaration(self) -> str:
246
+ """
247
+ Declaration of this condition in GAMS.
248
+
249
+ Returns
250
+ -------
251
+ str
252
+
253
+ Examples
254
+ --------
255
+ >>> import gamspy as gp
256
+ >>> m = gp.Container()
257
+ >>> i = gp.Set(m, "i", records=["i1", "i2"])
258
+ >>> a = gp.Parameter(m, "a", domain=i)
259
+ >>> condition = a[i].where[a[i] > 5]
260
+ >>> condition.getDeclaration()
261
+ 'a(i) $ (a(i) > 5)'
262
+
263
+ """
227
264
  return self.gamsRepr()
228
265
 
229
266
  def latexRepr(self) -> str:
@@ -2,6 +2,8 @@ from __future__ import annotations
2
2
 
3
3
  from typing import TYPE_CHECKING
4
4
 
5
+ from typing_extensions import override
6
+
5
7
  import gamspy._algebra.condition as condition
6
8
  import gamspy._algebra.domain as domain
7
9
  import gamspy._algebra.expression as expression
@@ -30,19 +32,45 @@ if TYPE_CHECKING:
30
32
 
31
33
 
32
34
  class Operation(operable.Operable):
33
- def __init__(
34
- self,
35
- domain: Set | Alias | ImplicitSet | Sequence[Set | Alias] | Domain | Condition,
36
- rhs: (
37
- Expression
38
- | Operation
35
+ """
36
+ Base class for different operations (Sum, Product etc.) over a domain.
37
+
38
+ Parameters
39
+ ----------
40
+ domain : Set | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
41
+ rhs : Operation
42
+ | Expression
39
43
  | MathOp
40
- | ImplicitSet
41
44
  | ImplicitVariable
42
45
  | ImplicitParameter
43
46
  | int
44
47
  | bool
45
- ),
48
+ | Variable
49
+ | Parameter
50
+
51
+ Examples
52
+ --------
53
+ >>> import gamspy as gp
54
+ >>> m = gp.Container()
55
+ >>> i = gp.Set(m, "i", records=['i1','i2', 'i3'])
56
+ >>> v = gp.Variable(m, "v")
57
+ >>> e = gp.Equation(m, "e", type="eq")
58
+ >>> d = gp.Parameter(m, "d", domain=[i], records=[("i1", 1), ("i2", 2), ("i3", 4)])
59
+ >>> e[...] = gp.Sum(i, d[i]) <= v
60
+
61
+ """
62
+
63
+ def __init__(
64
+ self,
65
+ domain: Set | Alias | ImplicitSet | Sequence[Set | Alias] | Domain | Condition,
66
+ rhs: Operation
67
+ | Expression
68
+ | MathOp
69
+ | ImplicitVariable
70
+ | ImplicitParameter
71
+ | ImplicitSet
72
+ | int
73
+ | bool,
46
74
  op_name: str,
47
75
  ):
48
76
  self.op_domain = utils._to_list(domain) # type: ignore
@@ -93,6 +121,18 @@ class Operation(operable.Operable):
93
121
  Returns
94
122
  -------
95
123
  pd.DataFrame | None
124
+
125
+ Examples
126
+ --------
127
+ >>> import numpy as np
128
+ >>> import gamspy as gp
129
+ >>> m = gp.Container()
130
+ >>> i = gp.Set(m, "i", records=["i1", "i2"])
131
+ >>> a = gp.Parameter(m, "a", domain=i, records=np.array([1,2]))
132
+ >>> gp.Sum(i, a[i]).records
133
+ value
134
+ 0 3.0
135
+
96
136
  """
97
137
  assert self.container is not None
98
138
  temp_name = "a" + utils._get_unique_name()
@@ -117,6 +157,17 @@ class Operation(operable.Operable):
117
157
  Returns
118
158
  -------
119
159
  float | None
160
+
161
+ Examples
162
+ --------
163
+ >>> import numpy as np
164
+ >>> import gamspy as gp
165
+ >>> m = gp.Container()
166
+ >>> i = gp.Set(m, "i", records=["i1", "i2"])
167
+ >>> a = gp.Parameter(m, "a", domain=i, records=np.array([1,2]))
168
+ >>> gp.Sum(i, a[i]).toValue()
169
+ np.float64(3.0)
170
+
120
171
  """
121
172
  records = self.records
122
173
  if records is not None:
@@ -131,6 +182,18 @@ class Operation(operable.Operable):
131
182
  Returns
132
183
  -------
133
184
  list | None
185
+
186
+ Examples
187
+ --------
188
+ >>> import numpy as np
189
+ >>> import gamspy as gp
190
+ >>> m = gp.Container()
191
+ >>> i = gp.Set(m, "i", records=["i1", "i2"])
192
+ >>> j = gp.Set(m, "j", records=["j1", "j2"])
193
+ >>> a = gp.Parameter(m, "a", domain=[i, j], records=np.array([(1,2), (3,4)]))
194
+ >>> gp.Sum(i, a[i, j]).toList()
195
+ [['j1', 4.0], ['j2', 6.0]]
196
+
134
197
  """
135
198
  records = self.records
136
199
  if records is not None:
@@ -195,6 +258,24 @@ class Operation(operable.Operable):
195
258
  return output
196
259
 
197
260
  def gamsRepr(self) -> str:
261
+ """
262
+ Representation of this operation in GAMS.
263
+
264
+ Returns
265
+ -------
266
+ str
267
+
268
+ Examples
269
+ --------
270
+ >>> import numpy as np
271
+ >>> import gamspy as gp
272
+ >>> m = gp.Container()
273
+ >>> i = gp.Set(m, "i", records=range(3))
274
+ >>> a = gp.Parameter(m, "a", domain=i, records=np.array([3,5,7]))
275
+ >>> print(gp.Sum(i, a[i]).gamsRepr())
276
+ sum(i,a(i))
277
+
278
+ """
198
279
  # Ex: sum((i,j), c(i,j) * x(i,j))
199
280
  output = f"{self._op_name}("
200
281
 
@@ -231,6 +312,16 @@ class Operation(operable.Operable):
231
312
  Returns
232
313
  -------
233
314
  str
315
+
316
+ Examples
317
+ --------
318
+ >>> import numpy as np
319
+ >>> import gamspy as gp
320
+ >>> m = gp.Container()
321
+ >>> i = gp.Set(m, "i", records=range(3))
322
+ >>> a = gp.Parameter(m, "a", domain=i, records=np.array([3,5,7]))
323
+ >>> print(gp.Sum(i, a[i]).latexRepr()) # doctest: +SKIP
324
+
234
325
  """
235
326
  op_map = {
236
327
  "sum": "sum",
@@ -274,8 +365,8 @@ class Sum(Operation):
274
365
  Parameters
275
366
  ----------
276
367
  domain : Set | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
277
- expression : (
278
- Expression
368
+ expression : Operation
369
+ | Expression
279
370
  | MathOp
280
371
  | ImplicitVariable
281
372
  | ImplicitParameter
@@ -283,8 +374,6 @@ class Sum(Operation):
283
374
  | bool
284
375
  | Variable
285
376
  | Parameter
286
- | Operation
287
- )
288
377
 
289
378
  Examples
290
379
  --------
@@ -315,6 +404,7 @@ class Sum(Operation):
315
404
  def __repr__(self) -> str:
316
405
  return f"Sum(domain={self.domain}, expression={self.rhs})"
317
406
 
407
+ @override
318
408
  def gamsRepr(self):
319
409
  """
320
410
  Representation of the Sum operation in GAMS language.
@@ -344,9 +434,9 @@ class Product(Operation):
344
434
 
345
435
  Parameters
346
436
  ----------
347
- domain : Set | Alias | Sequence[Set | Alias], Domain, Expression
348
- expression : (
349
- Expression
437
+ domain : Set | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
438
+ expression : Operation
439
+ | Expression
350
440
  | MathOp
351
441
  | ImplicitVariable
352
442
  | ImplicitParameter
@@ -354,8 +444,6 @@ class Product(Operation):
354
444
  | bool
355
445
  | Variable
356
446
  | Parameter
357
- | Operation
358
- )
359
447
 
360
448
  Examples
361
449
  --------
@@ -386,6 +474,7 @@ class Product(Operation):
386
474
  def __repr__(self) -> str:
387
475
  return f"Product(domain={self.domain}, expression={self.rhs})"
388
476
 
477
+ @override
389
478
  def gamsRepr(self):
390
479
  """
391
480
  Representation of the Product operation in GAMS language.
@@ -415,9 +504,9 @@ class Smin(Operation):
415
504
 
416
505
  Parameters
417
506
  ----------
418
- domain : Set | Alias | Sequence[Set | Alias], Domain, Expression
419
- expression : (
420
- Expression
507
+ domain : Set | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
508
+ expression : Operation
509
+ | Expression
421
510
  | MathOp
422
511
  | ImplicitVariable
423
512
  | ImplicitParameter
@@ -425,8 +514,6 @@ class Smin(Operation):
425
514
  | bool
426
515
  | Variable
427
516
  | Parameter
428
- | Operation
429
- )
430
517
 
431
518
  Examples
432
519
  --------
@@ -457,6 +544,7 @@ class Smin(Operation):
457
544
  def __repr__(self) -> str:
458
545
  return f"Smin(domain={self.domain}, expression={self.rhs})"
459
546
 
547
+ @override
460
548
  def gamsRepr(self):
461
549
  """
462
550
  Representation of the Smin operation in GAMS language.
@@ -486,9 +574,9 @@ class Smax(Operation):
486
574
 
487
575
  Parameters
488
576
  ----------
489
- domain : Set | Alias | Sequence[Set | Alias], Domain, Expression
490
- expression : (
491
- Expression
577
+ domain : Set | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
578
+ expression : Operation
579
+ | Expression
492
580
  | MathOp
493
581
  | ImplicitVariable
494
582
  | ImplicitParameter
@@ -496,8 +584,6 @@ class Smax(Operation):
496
584
  | bool
497
585
  | Variable
498
586
  | Parameter
499
- | Operation
500
- )
501
587
 
502
588
  Examples
503
589
  --------
@@ -528,6 +614,7 @@ class Smax(Operation):
528
614
  def __repr__(self) -> str:
529
615
  return f"Smax(domain={self.domain}, expression={self.rhs})"
530
616
 
617
+ @override
531
618
  def gamsRepr(self):
532
619
  """
533
620
  Representation of the Smax operation in GAMS language.
@@ -557,9 +644,9 @@ class Sand(Operation):
557
644
 
558
645
  Parameters
559
646
  ----------
560
- domain : Set | Alias | Sequence[Set | Alias], Domain, Expression
561
- expression : (
562
- Expression
647
+ domain : Set | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
648
+ expression : Operation
649
+ | Expression
563
650
  | MathOp
564
651
  | ImplicitVariable
565
652
  | ImplicitParameter
@@ -567,8 +654,6 @@ class Sand(Operation):
567
654
  | bool
568
655
  | Variable
569
656
  | Parameter
570
- | Operation
571
- )
572
657
 
573
658
  Examples
574
659
  --------
@@ -599,6 +684,7 @@ class Sand(Operation):
599
684
  def __repr__(self) -> str:
600
685
  return f"Sand(domain={self.domain}, expression={self.rhs})"
601
686
 
687
+ @override
602
688
  def gamsRepr(self):
603
689
  """
604
690
  Representation of the Sand operation in GAMS language.
@@ -627,9 +713,9 @@ class Sor(Operation):
627
713
 
628
714
  Parameters
629
715
  ----------
630
- domain : Set | Alias | Sequence[Set | Alias], Domain, Expression
631
- expression : (
632
- Expression
716
+ domain : Set | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
717
+ expression : Operation
718
+ | Expression
633
719
  | MathOp
634
720
  | ImplicitVariable
635
721
  | ImplicitParameter
@@ -637,8 +723,6 @@ class Sor(Operation):
637
723
  | bool
638
724
  | Variable
639
725
  | Parameter
640
- | Operation
641
- )
642
726
 
643
727
  Examples
644
728
  --------
@@ -669,6 +753,7 @@ class Sor(Operation):
669
753
  def __repr__(self) -> str:
670
754
  return f"Sor(domain={self.domain}, expression={self.rhs})"
671
755
 
756
+ @override
672
757
  def gamsRepr(self):
673
758
  """
674
759
  Representation of the Sor operation in GAMS language.
@@ -762,6 +847,15 @@ class Ord(operable.Operable):
762
847
  Returns
763
848
  -------
764
849
  str
850
+
851
+ Examples
852
+ --------
853
+ >>> import gamspy as gp
854
+ >>> m = gp.Container()
855
+ >>> i = gp.Set(m, "i")
856
+ >>> print(gp.Ord(i).latexRepr())
857
+ ord(i)
858
+
765
859
  """
766
860
  return f"ord({self._symbol._latex_name})"
767
861
 
@@ -841,5 +935,14 @@ class Card(operable.Operable):
841
935
  Returns
842
936
  -------
843
937
  str
938
+
939
+ Examples
940
+ --------
941
+ >>> import gamspy as gp
942
+ >>> m = gp.Container()
943
+ >>> i = gp.Set(m, "i")
944
+ >>> print(gp.Card(i).latexRepr())
945
+ card(i)
946
+
844
947
  """
845
948
  return f"card({self._symbol._latex_name})"
@@ -222,7 +222,9 @@ class Backend(ABC):
222
222
  symbols = filtered_names
223
223
 
224
224
  if len(symbols) != 0:
225
- self.container._load_records_from_gdx(self.container._gdx_out, symbols)
225
+ self.container._load_records_from_gdx(
226
+ self.container._gdx_out, symbols, create_if_not_declared=True
227
+ )
226
228
  self.make_unmodified(symbols)
227
229
 
228
230
  if relaxed_domain_mapping:
gamspy/_backend/engine.py CHANGED
@@ -798,14 +798,14 @@ class GAMSEngine(backend.Backend):
798
798
  return summary
799
799
 
800
800
  def execute_gams(self, gams_string: str):
801
- extra_options = {
801
+ hidden_options = {
802
802
  "gdx": os.path.basename(self.container._gdx_out),
803
803
  "gdxSymbols": "newOrChanged",
804
804
  "trace": os.path.basename(self.trace_file),
805
805
  "restart": os.path.basename(self.restart_file),
806
806
  "input": os.path.basename(self.gms_file),
807
807
  }
808
- self.options._set_extra_options(extra_options)
808
+ self.options._set_hidden_options(hidden_options)
809
809
  self.options._export(self.pf_file, self.output)
810
810
 
811
811
  with open(self.gms_file, "w", encoding="utf-8") as file:
@@ -891,7 +891,7 @@ class GAMSEngine(backend.Backend):
891
891
  def _prepare_dummy_options(self) -> dict:
892
892
  scrdir = self.container._process_directory
893
893
 
894
- extra_options = {
894
+ hidden_options = {
895
895
  "gdx": self.container._gdx_out,
896
896
  "gdxSymbols": "newOrChanged",
897
897
  "trace": self.trace_file,
@@ -905,18 +905,18 @@ class GAMSEngine(backend.Backend):
905
905
  }
906
906
 
907
907
  if self.container._network_license:
908
- extra_options["netlicense"] = os.path.join(scrdir, "gamslice.dat")
908
+ hidden_options["netlicense"] = os.path.join(scrdir, "gamslice.dat")
909
909
 
910
- return extra_options
910
+ return hidden_options
911
911
 
912
912
  def _create_restart_file(self):
913
913
  with open(self.gms_file, "w", encoding="utf-8") as gams_file:
914
914
  gams_file.write("")
915
915
 
916
916
  options = Options()
917
- extra_options = self._prepare_dummy_options()
918
- options._set_extra_options(extra_options)
919
- options._extra_options["save"] = self.restart_file
917
+ hidden_options = self._prepare_dummy_options()
918
+ options._set_hidden_options(hidden_options)
919
+ options._hidden_options["save"] = self.restart_file
920
920
  options._export(self.pf_file)
921
921
 
922
922
  send_job(self.container._comm_pair_id, self.job_name, self.pf_file)
@@ -930,8 +930,8 @@ class GAMSEngine(backend.Backend):
930
930
  gams_file.write(f'execute_load "{self.container._gdx_out}", {dirty_str};')
931
931
 
932
932
  options = Options()
933
- extra_options = self._prepare_dummy_options()
934
- options._set_extra_options(extra_options)
933
+ hidden_options = self._prepare_dummy_options()
934
+ options._set_hidden_options(hidden_options)
935
935
  options._export(self.pf_file)
936
936
 
937
937
  send_job(self.container._comm_pair_id, self.job_name, self.pf_file)
gamspy/_backend/local.py CHANGED
@@ -38,10 +38,10 @@ class Local(backend.Backend):
38
38
  load_symbols,
39
39
  )
40
40
 
41
- def _prepare_extra_options(self, gams_to_gamspy: bool) -> dict:
41
+ def _prepare_hidden_options(self, gams_to_gamspy: bool) -> dict:
42
42
  scrdir = self.container._process_directory
43
43
 
44
- extra_options = {
44
+ hidden_options = {
45
45
  "trace": self.trace_file,
46
46
  "input": self.gms_file,
47
47
  "output": self.lst_file,
@@ -53,13 +53,13 @@ class Local(backend.Backend):
53
53
  }
54
54
 
55
55
  if gams_to_gamspy:
56
- extra_options["gdx"] = self.container._gdx_out
57
- extra_options["gdxSymbols"] = "newOrChanged"
56
+ hidden_options["gdx"] = self.container._gdx_out
57
+ hidden_options["gdxSymbols"] = "newOrChanged"
58
58
 
59
59
  if self.container._network_license:
60
- extra_options["netlicense"] = os.path.join(scrdir, "gamslice.dat")
60
+ hidden_options["netlicense"] = os.path.join(scrdir, "gamslice.dat")
61
61
 
62
- return extra_options
62
+ return hidden_options
63
63
 
64
64
  def is_async(self):
65
65
  return False
@@ -86,8 +86,8 @@ class Local(backend.Backend):
86
86
  gams_file.write(gams_string)
87
87
 
88
88
  # Write pf file
89
- extra_options = self._prepare_extra_options(gams_to_gamspy)
90
- self.options._set_extra_options(extra_options)
89
+ hidden_options = self._prepare_hidden_options(gams_to_gamspy)
90
+ self.options._set_hidden_options(hidden_options)
91
91
  self.options._export(self.pf_file, self.output)
92
92
 
93
93
  try:
gamspy/_backend/neos.py CHANGED
@@ -441,13 +441,13 @@ class NEOSServer(backend.Backend):
441
441
  if self.container._debugging_level == "keep":
442
442
  self.options.log_file = os.path.basename(self.job_name) + ".log"
443
443
 
444
- extra_options = {
444
+ hidden_options = {
445
445
  "gdx": "output.gdx",
446
446
  "gdxSymbols": "newOrChanged",
447
447
  "trace": os.path.basename(self.trace_file),
448
448
  "forcework": "1",
449
449
  }
450
- self.options._set_extra_options(extra_options)
450
+ self.options._set_hidden_options(hidden_options)
451
451
 
452
452
  self.client._prepare_xml(
453
453
  gams_string,
@@ -505,7 +505,7 @@ class NEOSServer(backend.Backend):
505
505
  def _prepare_dummy_options(self) -> dict:
506
506
  scrdir = self.container._process_directory
507
507
 
508
- extra_options = {
508
+ hidden_options = {
509
509
  "gdx": self.container._gdx_out,
510
510
  "gdxSymbols": "newOrChanged",
511
511
  "trace": self.trace_file,
@@ -518,18 +518,18 @@ class NEOSServer(backend.Backend):
518
518
  }
519
519
 
520
520
  if self.container._network_license:
521
- extra_options["netlicense"] = os.path.join(scrdir, "gamslice.dat")
521
+ hidden_options["netlicense"] = os.path.join(scrdir, "gamslice.dat")
522
522
 
523
- return extra_options
523
+ return hidden_options
524
524
 
525
525
  def _create_restart_file(self):
526
526
  with open(self.gms_file, "w", encoding="utf-8") as gams_file:
527
527
  gams_file.write("")
528
528
 
529
529
  options = Options()
530
- extra_options = self._prepare_dummy_options()
531
- options._set_extra_options(extra_options)
532
- options._extra_options["save"] = self.restart_file
530
+ hidden_options = self._prepare_dummy_options()
531
+ options._set_hidden_options(hidden_options)
532
+ options._hidden_options["save"] = self.restart_file
533
533
  options._export(self.pf_file)
534
534
 
535
535
  send_job(self.container._comm_pair_id, self.job_name, self.pf_file)
@@ -543,8 +543,8 @@ class NEOSServer(backend.Backend):
543
543
  gams_file.write(f'execute_load "{self.container._gdx_out}", {dirty_str};')
544
544
 
545
545
  options = Options()
546
- extra_options = self._prepare_dummy_options()
547
- options._set_extra_options(extra_options)
546
+ hidden_options = self._prepare_dummy_options()
547
+ options._set_hidden_options(hidden_options)
548
548
  options._export(self.pf_file)
549
549
 
550
550
  send_job(self.container._comm_pair_id, self.job_name, self.pf_file)
gamspy/_cli/install.py CHANGED
@@ -144,7 +144,6 @@ def license(
144
144
  command,
145
145
  text=True,
146
146
  capture_output=True,
147
- encoding="utf-8",
148
147
  env=environment_variables,
149
148
  )
150
149
  if process.returncode:
@@ -304,7 +303,6 @@ def solver(
304
303
  _ = subprocess.run(
305
304
  command,
306
305
  check=True,
307
- encoding="utf-8",
308
306
  env=environment_variables,
309
307
  )
310
308
  except subprocess.CalledProcessError as e: