gamspy 1.18.3__py3-none-any.whl → 1.19.0__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.
Files changed (89) hide show
  1. gamspy/__init__.py +86 -98
  2. gamspy/__main__.py +6 -6
  3. gamspy/_algebra/__init__.py +13 -13
  4. gamspy/_algebra/condition.py +290 -194
  5. gamspy/_algebra/domain.py +103 -93
  6. gamspy/_algebra/expression.py +820 -799
  7. gamspy/_algebra/number.py +79 -70
  8. gamspy/_algebra/operable.py +185 -185
  9. gamspy/_algebra/operation.py +948 -845
  10. gamspy/_backend/backend.py +313 -311
  11. gamspy/_backend/engine.py +960 -960
  12. gamspy/_backend/local.py +124 -124
  13. gamspy/_backend/neos.py +567 -567
  14. gamspy/_cli/__init__.py +1 -1
  15. gamspy/_cli/cli.py +64 -64
  16. gamspy/_cli/gdx.py +377 -377
  17. gamspy/_cli/install.py +375 -372
  18. gamspy/_cli/list.py +94 -94
  19. gamspy/_cli/mps2gms.py +128 -128
  20. gamspy/_cli/probe.py +52 -52
  21. gamspy/_cli/retrieve.py +79 -79
  22. gamspy/_cli/run.py +158 -158
  23. gamspy/_cli/show.py +246 -255
  24. gamspy/_cli/uninstall.py +165 -165
  25. gamspy/_cli/util.py +94 -94
  26. gamspy/_communication.py +215 -215
  27. gamspy/_config.py +132 -132
  28. gamspy/_container.py +1694 -1452
  29. gamspy/_convert.py +720 -720
  30. gamspy/_database.py +271 -271
  31. gamspy/_extrinsic.py +181 -181
  32. gamspy/_miro.py +356 -352
  33. gamspy/_model.py +1803 -1615
  34. gamspy/_model_instance.py +701 -701
  35. gamspy/_options.py +780 -700
  36. gamspy/_serialization.py +156 -144
  37. gamspy/_symbols/__init__.py +17 -17
  38. gamspy/_symbols/alias.py +305 -299
  39. gamspy/_symbols/equation.py +1407 -1298
  40. gamspy/_symbols/implicits/__init__.py +11 -11
  41. gamspy/_symbols/implicits/implicit_equation.py +186 -186
  42. gamspy/_symbols/implicits/implicit_parameter.py +272 -272
  43. gamspy/_symbols/implicits/implicit_set.py +124 -124
  44. gamspy/_symbols/implicits/implicit_symbol.py +315 -315
  45. gamspy/_symbols/implicits/implicit_variable.py +255 -255
  46. gamspy/_symbols/parameter.py +648 -609
  47. gamspy/_symbols/set.py +985 -923
  48. gamspy/_symbols/symbol.py +395 -386
  49. gamspy/_symbols/universe_alias.py +182 -182
  50. gamspy/_symbols/variable.py +1101 -1017
  51. gamspy/_types.py +7 -7
  52. gamspy/_validation.py +735 -735
  53. gamspy/_workspace.py +72 -72
  54. gamspy/exceptions.py +128 -128
  55. gamspy/formulations/__init__.py +46 -46
  56. gamspy/formulations/ml/__init__.py +11 -11
  57. gamspy/formulations/ml/decision_tree_struct.py +80 -80
  58. gamspy/formulations/ml/gradient_boosting.py +203 -203
  59. gamspy/formulations/ml/random_forest.py +187 -187
  60. gamspy/formulations/ml/regression_tree.py +533 -533
  61. gamspy/formulations/nn/__init__.py +19 -19
  62. gamspy/formulations/nn/avgpool2d.py +232 -232
  63. gamspy/formulations/nn/conv1d.py +533 -533
  64. gamspy/formulations/nn/conv2d.py +529 -529
  65. gamspy/formulations/nn/linear.py +341 -341
  66. gamspy/formulations/nn/maxpool2d.py +88 -88
  67. gamspy/formulations/nn/minpool2d.py +88 -88
  68. gamspy/formulations/nn/mpool2d.py +245 -245
  69. gamspy/formulations/nn/torch_sequential.py +278 -278
  70. gamspy/formulations/piecewise.py +682 -682
  71. gamspy/formulations/result.py +119 -119
  72. gamspy/formulations/shape.py +188 -188
  73. gamspy/formulations/utils.py +173 -173
  74. gamspy/math/__init__.py +215 -215
  75. gamspy/math/activation.py +783 -767
  76. gamspy/math/log_power.py +435 -435
  77. gamspy/math/matrix.py +534 -534
  78. gamspy/math/misc.py +1709 -1625
  79. gamspy/math/probability.py +170 -170
  80. gamspy/math/trigonometric.py +232 -232
  81. gamspy/utils.py +810 -791
  82. gamspy/version.py +5 -5
  83. {gamspy-1.18.3.dist-info → gamspy-1.19.0.dist-info}/METADATA +90 -121
  84. gamspy-1.19.0.dist-info/RECORD +90 -0
  85. {gamspy-1.18.3.dist-info → gamspy-1.19.0.dist-info}/WHEEL +1 -1
  86. {gamspy-1.18.3.dist-info → gamspy-1.19.0.dist-info}/licenses/LICENSE +22 -22
  87. gamspy-1.18.3.dist-info/RECORD +0 -90
  88. {gamspy-1.18.3.dist-info → gamspy-1.19.0.dist-info}/entry_points.txt +0 -0
  89. {gamspy-1.18.3.dist-info → gamspy-1.19.0.dist-info}/top_level.txt +0 -0
@@ -1,194 +1,290 @@
1
- from __future__ import annotations
2
-
3
- from typing import TYPE_CHECKING
4
-
5
- import gamspy._algebra.domain as domain
6
- import gamspy._algebra.expression as expression
7
- import gamspy._algebra.operable as operable
8
- import gamspy._symbols as syms
9
- import gamspy._symbols.implicits as implicits
10
- import gamspy.utils as utils
11
- from gamspy._symbols.implicits.implicit_symbol import ImplicitSymbol
12
- from gamspy._symbols.symbol import Symbol
13
-
14
- if TYPE_CHECKING:
15
- import pandas as pd
16
-
17
- from gamspy import Alias, Parameter, Set, Variable
18
- from gamspy._algebra.domain import Domain
19
- from gamspy._algebra.expression import Expression
20
- from gamspy._algebra.number import Number
21
- from gamspy._algebra.operation import Card, Operation, Ord
22
- from gamspy._symbols.implicits import (
23
- ImplicitParameter,
24
- ImplicitSet,
25
- )
26
- from gamspy.math import MathOp
27
-
28
-
29
- class Condition(operable.Operable):
30
- """
31
- Condition class allows conditioning on GAMSPy constructs such as Symbols and Expressions.
32
-
33
- Parameters
34
- ----------
35
- symbol: ImplicitSymbol | Expression
36
- Reference to the symbol to be conditioned.
37
- """
38
-
39
- def __init__(
40
- self,
41
- conditioning_on: ImplicitSymbol
42
- | Set
43
- | Alias
44
- | Parameter
45
- | Variable
46
- | Expression
47
- | Operation
48
- | Domain
49
- | Number
50
- | Card
51
- | Ord
52
- | MathOp,
53
- condition: Operation
54
- | Expression
55
- | ImplicitParameter
56
- | ImplicitSet
57
- | int
58
- | None = None,
59
- ):
60
- self.conditioning_on = conditioning_on
61
- self.condition = condition
62
- self._where = None
63
- self.container = None
64
- if hasattr(conditioning_on, "container"):
65
- self.container = conditioning_on.container
66
-
67
- self.domain = None
68
- if hasattr(conditioning_on, "domain"):
69
- self.domain = conditioning_on.domain
70
-
71
- @property
72
- def where(self):
73
- return Condition(self)
74
-
75
- def __getitem__(
76
- self,
77
- condition: Operation | Expression | ImplicitParameter | ImplicitSet,
78
- ) -> Condition:
79
- if isinstance(condition, expression.Expression):
80
- condition._fix_equalities()
81
-
82
- return Condition(self.conditioning_on, condition)
83
-
84
- def __setitem__(self, condition, rhs):
85
- # conditioning_on.where[condition] = rhs
86
- eq_types = (syms.Equation, implicits.ImplicitEquation)
87
- if isinstance(rhs, bool):
88
- rhs = "yes" if rhs is True else "no"
89
-
90
- op_type = ".." if isinstance(self.conditioning_on, eq_types) else "="
91
-
92
- if isinstance(condition, expression.Expression):
93
- condition._fix_equalities()
94
-
95
- lhs = Condition(self.conditioning_on, condition)
96
- statement = expression.Expression(lhs, op_type, rhs)
97
-
98
- if isinstance(self.conditioning_on, ImplicitSymbol):
99
- statement._validate_definition(utils._unpack(self.conditioning_on.domain))
100
-
101
- self.conditioning_on.container._add_statement(statement)
102
-
103
- if isinstance(self.conditioning_on, ImplicitSymbol):
104
- self.conditioning_on.parent._assignment = statement
105
- self.conditioning_on.parent._winner = "gams"
106
- elif isinstance(self.conditioning_on, Symbol):
107
- self.conditioning_on._assignment = statement
108
- self.conditioning_on._winner = "gams"
109
-
110
- if isinstance(self.conditioning_on, implicits.ImplicitEquation):
111
- self.conditioning_on.parent._definition = statement
112
-
113
- self.conditioning_on.container._synch_with_gams(gams_to_gamspy=True)
114
-
115
- def __repr__(self) -> str:
116
- return f"Condition(conditioning_on={self.conditioning_on}, condition={self.condition})"
117
-
118
- @property
119
- def dimension(self) -> int:
120
- if self.domain is None:
121
- return 0
122
-
123
- return len(self.domain)
124
-
125
- @property
126
- def records(self) -> pd.DataFrame | None:
127
- assert self.container is not None
128
- assert self.domain is not None
129
- if isinstance(
130
- self.conditioning_on,
131
- (syms.Set, syms.Alias, implicits.ImplicitSet),
132
- ):
133
- temp_name = "c" + utils._get_unique_name()
134
- temp_sym = syms.Set._constructor_bypass(
135
- self.container,
136
- temp_name,
137
- self.domain, # type: ignore
138
- )
139
- temp_sym[...] = self
140
- del self.container.data[temp_name]
141
- elif isinstance(self.conditioning_on, domain.Domain):
142
- temp_name = "c" + utils._get_unique_name()
143
- temp_sym = syms.Set._constructor_bypass(
144
- self.container,
145
- temp_name,
146
- self.domain, # type: ignore
147
- )
148
- temp_sym[...].where[self.condition] = True
149
- del self.container.data[temp_name]
150
- else:
151
- temp_name = "c" + utils._get_unique_name()
152
- temp_sym = syms.Parameter._constructor_bypass(
153
- self.container,
154
- temp_name,
155
- self.domain, # type: ignore
156
- )
157
- temp_sym[...] = self
158
- del self.container.data[temp_name]
159
-
160
- return temp_sym.records
161
-
162
- def gamsRepr(self) -> str:
163
- condition_str = (
164
- self.condition.gamsRepr() # type: ignore
165
- if hasattr(self.condition, "gamsRepr")
166
- else str(self.condition)
167
- )
168
- conditioning_on_str = self.conditioning_on.gamsRepr()
169
-
170
- if isinstance(self.condition, bool):
171
- condition_str = str(int(self.condition))
172
-
173
- if isinstance(self.conditioning_on, expression.Expression):
174
- conditioning_on_str = f"({conditioning_on_str})"
175
-
176
- return f"{conditioning_on_str} $ ({condition_str})" # type: ignore
177
-
178
- def getDeclaration(self) -> str:
179
- return self.gamsRepr()
180
-
181
- def latexRepr(self) -> str:
182
- """
183
- Representation of this condition in Latex.
184
-
185
- Returns
186
- -------
187
- str
188
- """
189
- condition_str = (
190
- self.condition.latexRepr() # type: ignore
191
- if hasattr(self.condition, "latexRepr")
192
- else str(self.condition)
193
- )
194
- return f"{self.conditioning_on.latexRepr()} ~ | ~ {condition_str}" # type: ignore
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ import gamspy._algebra.domain as domain
6
+ import gamspy._algebra.expression as expression
7
+ import gamspy._algebra.operable as operable
8
+ import gamspy._symbols as syms
9
+ import gamspy._symbols.implicits as implicits
10
+ import gamspy.utils as utils
11
+ from gamspy._symbols.implicits.implicit_symbol import ImplicitSymbol
12
+ from gamspy._symbols.symbol import Symbol
13
+
14
+ if TYPE_CHECKING:
15
+ import pandas as pd
16
+
17
+ from gamspy import Alias, Parameter, Set, Variable
18
+ from gamspy._algebra.domain import Domain
19
+ from gamspy._algebra.expression import Expression
20
+ from gamspy._algebra.number import Number
21
+ from gamspy._algebra.operation import Card, Operation, Ord
22
+ from gamspy._symbols.implicits import (
23
+ ImplicitParameter,
24
+ ImplicitSet,
25
+ )
26
+ from gamspy.math import MathOp
27
+
28
+
29
+ class Condition(operable.Operable):
30
+ """
31
+ Condition class allows conditioning on GAMSPy constructs such as Symbols and Expressions.
32
+
33
+ Parameters
34
+ ----------
35
+ symbol: ImplicitSymbol | Expression
36
+ Reference to the symbol to be conditioned.
37
+
38
+ Examples
39
+ --------
40
+ >>> import gamspy as gp
41
+ >>> m = gp.Container()
42
+ >>> i = gp.Set(m, "i", records=["i1", "i2", "i3"])
43
+ >>> a = gp.Parameter(m, "a", domain=i, records=[("i1", 10), ("i2", 20), ("i3", 30)])
44
+ >>> # Conditional assignment: Set values to 1 where they exceed 15
45
+ >>> a[i].where[a[i] > 15] = 1
46
+ >>> a.records.values.tolist()
47
+ [['i1', 10.0], ['i2', 1.0], ['i3', 1.0]]
48
+
49
+ """
50
+
51
+ def __init__(
52
+ self,
53
+ conditioning_on: ImplicitSymbol
54
+ | Set
55
+ | Alias
56
+ | Parameter
57
+ | Variable
58
+ | Expression
59
+ | Operation
60
+ | Domain
61
+ | Number
62
+ | Card
63
+ | Ord
64
+ | MathOp,
65
+ condition: Operation
66
+ | Expression
67
+ | ImplicitParameter
68
+ | ImplicitSet
69
+ | int
70
+ | None = None,
71
+ ):
72
+ self.conditioning_on = conditioning_on
73
+ self.condition = condition
74
+ self._where = None
75
+ self.container = None
76
+ if hasattr(conditioning_on, "container"):
77
+ self.container = conditioning_on.container
78
+
79
+ self.domain = None
80
+ if hasattr(conditioning_on, "domain"):
81
+ self.domain = conditioning_on.domain
82
+
83
+ @property
84
+ def where(self):
85
+ return Condition(self)
86
+
87
+ def __getitem__(
88
+ self,
89
+ condition: Operation | Expression | ImplicitParameter | ImplicitSet,
90
+ ) -> Condition:
91
+ if isinstance(condition, expression.Expression):
92
+ condition._fix_equalities()
93
+
94
+ return Condition(self.conditioning_on, condition)
95
+
96
+ def __setitem__(self, condition, rhs):
97
+ # conditioning_on.where[condition] = rhs
98
+ eq_types = (syms.Equation, implicits.ImplicitEquation)
99
+ if isinstance(rhs, bool):
100
+ rhs = "yes" if rhs is True else "no"
101
+
102
+ op_type = ".." if isinstance(self.conditioning_on, eq_types) else "="
103
+
104
+ if isinstance(condition, expression.Expression):
105
+ condition._fix_equalities()
106
+
107
+ lhs = Condition(self.conditioning_on, condition)
108
+ statement = expression.Expression(lhs, op_type, rhs)
109
+
110
+ if isinstance(self.conditioning_on, ImplicitSymbol):
111
+ statement._validate_definition(utils._unpack(self.conditioning_on.domain))
112
+
113
+ self.conditioning_on.container._add_statement(statement)
114
+
115
+ if isinstance(self.conditioning_on, ImplicitSymbol):
116
+ self.conditioning_on.parent._assignment = statement
117
+ self.conditioning_on.parent._winner = "gams"
118
+ elif isinstance(self.conditioning_on, Symbol):
119
+ self.conditioning_on._assignment = statement
120
+ self.conditioning_on._winner = "gams"
121
+
122
+ if isinstance(self.conditioning_on, implicits.ImplicitEquation):
123
+ self.conditioning_on.parent._definition = statement
124
+
125
+ self.conditioning_on.container._synch_with_gams(gams_to_gamspy=True)
126
+
127
+ def __repr__(self) -> str:
128
+ return f"Condition(conditioning_on={self.conditioning_on}, condition={self.condition})"
129
+
130
+ @property
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
+ """
151
+ if self.domain is None:
152
+ return 0
153
+
154
+ return len(self.domain)
155
+
156
+ @property
157
+ def records(self) -> pd.DataFrame | None:
158
+ """
159
+ The records of the condition.
160
+
161
+ Returns
162
+ -------
163
+ pd.DataFrame | None
164
+
165
+ Examples
166
+ --------
167
+ >>> import gamspy as gp
168
+ >>> m = gp.Container()
169
+ >>> i = gp.Set(m, "i", records=["i1", "i2", "i3"])
170
+ >>> a = gp.Parameter(m, "a", domain=i, records=[("i1", 10), ("i2", 20), ("i3", 5)])
171
+ >>> condition = a[i].where[a[i] > 9]
172
+ >>> condition.records.values.tolist()
173
+ [['i1', 10.0], ['i2', 20.0]]
174
+
175
+ """
176
+ assert self.container is not None
177
+ assert self.domain is not None
178
+ if isinstance(
179
+ self.conditioning_on,
180
+ (syms.Set, syms.Alias, implicits.ImplicitSet),
181
+ ):
182
+ temp_name = "c" + utils._get_unique_name()
183
+ temp_sym = syms.Set._constructor_bypass(
184
+ self.container,
185
+ temp_name,
186
+ self.domain, # type: ignore
187
+ )
188
+ temp_sym[...] = self
189
+ del self.container.data[temp_name]
190
+ elif isinstance(self.conditioning_on, domain.Domain):
191
+ temp_name = "c" + utils._get_unique_name()
192
+ temp_sym = syms.Set._constructor_bypass(
193
+ self.container,
194
+ temp_name,
195
+ self.domain, # type: ignore
196
+ )
197
+ temp_sym[...].where[self.condition] = True
198
+ del self.container.data[temp_name]
199
+ else:
200
+ temp_name = "c" + utils._get_unique_name()
201
+ temp_sym = syms.Parameter._constructor_bypass(
202
+ self.container,
203
+ temp_name,
204
+ self.domain, # type: ignore
205
+ )
206
+ temp_sym[...] = self
207
+ del self.container.data[temp_name]
208
+
209
+ return temp_sym.records
210
+
211
+ def gamsRepr(self) -> str:
212
+ """
213
+ Representation of this condition in GAMS.
214
+
215
+ Returns
216
+ -------
217
+ str
218
+
219
+ Examples
220
+ --------
221
+ >>> import gamspy as gp
222
+ >>> m = gp.Container()
223
+ >>> i = gp.Set(m, "i", records=["i1", "i2"])
224
+ >>> a = gp.Parameter(m, "a", domain=i)
225
+ >>> condition = a[i].where[a[i] > 5]
226
+ >>> condition.gamsRepr()
227
+ 'a(i) $ (a(i) > 5)'
228
+
229
+ """
230
+ condition_str = (
231
+ self.condition.gamsRepr() # type: ignore
232
+ if hasattr(self.condition, "gamsRepr")
233
+ else str(self.condition)
234
+ )
235
+ conditioning_on_str = self.conditioning_on.gamsRepr()
236
+
237
+ if isinstance(self.condition, bool):
238
+ condition_str = str(int(self.condition))
239
+
240
+ if isinstance(self.conditioning_on, expression.Expression):
241
+ conditioning_on_str = f"({conditioning_on_str})"
242
+
243
+ return f"{conditioning_on_str} $ ({condition_str})" # type: ignore
244
+
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
+ """
264
+ return self.gamsRepr()
265
+
266
+ def latexRepr(self) -> str:
267
+ """
268
+ Representation of this condition in Latex.
269
+
270
+ Returns
271
+ -------
272
+ str
273
+
274
+ Examples
275
+ --------
276
+ >>> import gamspy as gp
277
+ >>> m = gp.Container()
278
+ >>> i = gp.Set(m, "i", records=["i1", "i2"])
279
+ >>> a = gp.Parameter(m, "a", domain=i)
280
+ >>> condition = a[i].where[a[i] > 2]
281
+ >>> condition.latexRepr()
282
+ 'a_{i} ~ | ~ a_{i} > 2'
283
+
284
+ """
285
+ condition_str = (
286
+ self.condition.latexRepr() # type: ignore
287
+ if hasattr(self.condition, "latexRepr")
288
+ else str(self.condition)
289
+ )
290
+ return f"{self.conditioning_on.latexRepr()} ~ | ~ {condition_str}" # type: ignore