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,272 +1,272 @@
1
- from __future__ import annotations
2
-
3
- import logging
4
- from typing import TYPE_CHECKING, Any
5
-
6
- import gamspy._algebra.expression as expression
7
- import gamspy._algebra.operable as operable
8
- import gamspy._algebra.operation as operation
9
- import gamspy._symbols as syms
10
- import gamspy._symbols.implicits as implicits
11
- import gamspy._validation as validation
12
- import gamspy.utils as utils
13
- from gamspy._symbols.implicits.implicit_symbol import ImplicitSymbol
14
- from gamspy._symbols.implicits.implicit_variable import ImplicitVariable
15
- from gamspy.exceptions import ValidationError
16
- from gamspy.math.matrix import permute
17
-
18
- if TYPE_CHECKING:
19
- from collections.abc import Sequence
20
- from types import EllipsisType
21
-
22
- import pandas as pd
23
-
24
- from gamspy import (
25
- Alias,
26
- Equation,
27
- Parameter,
28
- Set,
29
- Variable,
30
- )
31
- from gamspy._algebra.expression import Expression
32
- from gamspy._algebra.operation import Operation
33
-
34
- logger = logging.getLogger("GAMSPy")
35
- logger.setLevel(logging.WARNING)
36
- stream_handler = logging.StreamHandler()
37
- stream_handler.setLevel(logging.WARNING)
38
- formatter = logging.Formatter("[%(name)s - %(levelname)s] %(message)s")
39
- stream_handler.setFormatter(formatter)
40
- logger.addHandler(stream_handler)
41
-
42
- ATTR_MAPPING = {
43
- "l": "level",
44
- "m": "marginal",
45
- "lo": "lower",
46
- "up": "upper",
47
- "scale": "scale",
48
- "range": "range",
49
- "slack": "slack",
50
- "slacklo": "slacklo",
51
- "slackup": "slackup",
52
- "infeas": "infeas",
53
- }
54
-
55
- SET_ATTR_MAPPING = {
56
- "pos": "position",
57
- "ord": "order",
58
- "off": "off",
59
- "rev": "reverse",
60
- "uel": "uel_position",
61
- "len": "length",
62
- "tlen": "text_length",
63
- "val": "value",
64
- "tval": "text_value",
65
- "first": "is_first",
66
- "last": "is_last",
67
- }
68
-
69
-
70
- class ImplicitParameter(ImplicitSymbol, operable.Operable):
71
- def __init__(
72
- self,
73
- parent: Set | Alias | Parameter | Variable | Equation,
74
- name: str,
75
- domain: list[Set | str] = [],
76
- records: Any | None = None,
77
- permutation: list[int] | None = None,
78
- scalar_domains: list[tuple[int, Set]] | None = None,
79
- ) -> None:
80
- """Implicit Parameter
81
-
82
- Parameters
83
- ----------
84
- parent : Parameter | Variable | Equation
85
- name : str
86
- domain : list[Set | str], optional
87
- records : Any, optional
88
- """
89
- super().__init__(parent, name, domain, permutation, scalar_domains)
90
- self._records = records
91
- self._assignment = None
92
-
93
- def __getitem__(
94
- self,
95
- indices: EllipsisType | slice | Sequence | str | implicits.ImplicitSet,
96
- ) -> ImplicitParameter:
97
- domain = validation.validate_domain(self, indices)
98
-
99
- return ImplicitParameter(
100
- parent=self.parent,
101
- name=self.name,
102
- domain=domain,
103
- permutation=self.permutation,
104
- scalar_domains=self._scalar_domains,
105
- )
106
-
107
- def __setitem__(
108
- self,
109
- indices: EllipsisType | slice | Sequence | str | implicits.ImplicitSet,
110
- rhs: Expression | Operation | ImplicitParameter | int | float,
111
- ) -> None:
112
- if (
113
- isinstance(self.parent, (syms.Variable, syms.Equation))
114
- and len(self.parent.domain) > 0
115
- and all(len(elem) == 0 for elem in self.parent.domain)
116
- ):
117
- logger.warning(
118
- f"Domain was not initialized. Default values for {self.gamsRepr()} will be used."
119
- )
120
- # self[domain] = rhs
121
- domain = validation.validate_domain(self, indices)
122
-
123
- if isinstance(rhs, float):
124
- rhs = utils._map_special_values(rhs) # type: ignore
125
-
126
- statement = expression.Expression(
127
- ImplicitParameter(
128
- parent=self.parent,
129
- name=self.name,
130
- domain=domain,
131
- permutation=self.permutation,
132
- scalar_domains=self._scalar_domains,
133
- ),
134
- "=",
135
- rhs,
136
- )
137
-
138
- statement._validate_definition(utils._unpack(domain))
139
-
140
- self.container._add_statement(statement)
141
- self.parent._assignment = statement
142
-
143
- self.container._synch_with_gams(gams_to_gamspy=True)
144
- self.parent._winner = "gams"
145
-
146
- def __eq__(self, other):
147
- op = "eq"
148
- if isinstance(
149
- other,
150
- (ImplicitVariable, expression.Expression, operation.Operation),
151
- ):
152
- op = "=e="
153
- return expression.Expression(self, op, other)
154
-
155
- def __ne__(self, other):
156
- return expression.Expression(self, "ne", other)
157
-
158
- def __repr__(self) -> str:
159
- return f"ImplicitParameter(parent={self.parent}, name='{self.name}', domain={self.domain}, permutation={self.permutation}), parent_scalar_domains={self.parent_scalar_domains})"
160
-
161
- @property
162
- def records(self) -> pd.DataFrame | None:
163
- if self.parent.records is None:
164
- return None
165
-
166
- if isinstance(self.parent, (syms.Set, syms.Alias)):
167
- temp_name = "p" + utils._get_unique_name()
168
- temp_param = syms.Parameter._constructor_bypass(
169
- self.container, temp_name, [self.parent, "*"]
170
- )
171
- column_name = SET_ATTR_MAPPING[self.name.split(".")[1]]
172
- temp_param[self.parent, column_name] = self
173
- del self.container.data[temp_name]
174
- return temp_param.records
175
- elif isinstance(self.parent, syms.Parameter):
176
- temp_name = "p" + utils._get_unique_name()
177
- temp_param = syms.Parameter._constructor_bypass(
178
- self.container, temp_name, self.parent.domain
179
- )
180
- domain = list(self.domain)
181
- for i, d in self._scalar_domains:
182
- domain.insert(i, d)
183
-
184
- if domain == []:
185
- domain = [...]
186
-
187
- temp_param[domain] = self[...]
188
- del self.container.data[temp_name]
189
-
190
- recs = temp_param.records
191
- return recs
192
- elif isinstance(self.parent, (syms.Variable, syms.Equation)):
193
- extension = self.name.split(".")[-1]
194
- temp_name = "ip" + utils._get_unique_name()
195
- temp_param = syms.Parameter._constructor_bypass(
196
- self.container, temp_name, self.parent.domain
197
- )
198
- domain = list(self.domain)
199
- for i, d in self._scalar_domains:
200
- domain.insert(i, d)
201
-
202
- if domain == []:
203
- domain = [...]
204
-
205
- temp_param[domain] = self
206
- del self.container.data[temp_name]
207
-
208
- recs = temp_param.records
209
- if recs is None:
210
- return recs
211
-
212
- extension = ATTR_MAPPING[extension]
213
- columns = recs.columns.to_list()
214
- columns[columns.index("value")] = extension
215
- recs.columns = columns
216
-
217
- return recs
218
-
219
- return None
220
-
221
- @property
222
- def T(self) -> ImplicitParameter:
223
- """See gamspy.ImplicitParameter.t"""
224
- return self.t()
225
-
226
- def t(self) -> ImplicitParameter:
227
- """Returns an ImplicitParameter derived from this
228
- implicit parameter by swapping its last two indices.
229
- This operation does not generate a new parameter in GAMS.
230
-
231
- Examples
232
- --------
233
- >>> import gamspy as gp
234
- >>> m = gp.Container()
235
- >>> i = gp.Set(m, "i", records=['i1','i2'])
236
- >>> j = gp.Set(m, "j", records=['j1','j2'])
237
- >>> v = gp.Parameter(m, "v", domain=[i, j])
238
- >>> v_t = v.t() # v_t is an ImplicitParameter
239
- >>> v_t_t = v_t.t() # you can get transpose of ImplicitParameter as well
240
- >>> v_t_t.domain
241
- [Set(name='i', domain=['*']), Set(name='j', domain=['*'])]
242
-
243
- """
244
- dims = list(range(len(self.domain)))
245
- if len(dims) < 2:
246
- raise ValidationError(
247
- "Parameter must contain at least 2 dimensions to transpose"
248
- )
249
-
250
- x = dims[-1]
251
- dims[-1] = dims[-2]
252
- dims[-2] = x
253
- return permute(self, dims) # type: ignore
254
-
255
- def gamsRepr(self) -> str:
256
- """Representation of the parameter in GAMS syntax.
257
-
258
- Returns:
259
- str: String representation of the parameter in GAMS syntax.
260
- """
261
- representation = self.name
262
- domain = list(self.domain)
263
- if domain and self.permutation is not None:
264
- domain = utils._permute_domain(domain, self.permutation)
265
-
266
- for i, d in self._scalar_domains:
267
- domain.insert(i, d)
268
-
269
- if domain:
270
- representation += utils._get_domain_str(domain)
271
-
272
- return representation
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ from typing import TYPE_CHECKING, Any
5
+
6
+ import gamspy._algebra.expression as expression
7
+ import gamspy._algebra.operable as operable
8
+ import gamspy._algebra.operation as operation
9
+ import gamspy._symbols as syms
10
+ import gamspy._symbols.implicits as implicits
11
+ import gamspy._validation as validation
12
+ import gamspy.utils as utils
13
+ from gamspy._symbols.implicits.implicit_symbol import ImplicitSymbol
14
+ from gamspy._symbols.implicits.implicit_variable import ImplicitVariable
15
+ from gamspy.exceptions import ValidationError
16
+ from gamspy.math.matrix import permute
17
+
18
+ if TYPE_CHECKING:
19
+ from collections.abc import Sequence
20
+ from types import EllipsisType
21
+
22
+ import pandas as pd
23
+
24
+ from gamspy import (
25
+ Alias,
26
+ Equation,
27
+ Parameter,
28
+ Set,
29
+ Variable,
30
+ )
31
+ from gamspy._algebra.expression import Expression
32
+ from gamspy._algebra.operation import Operation
33
+
34
+ logger = logging.getLogger("GAMSPy")
35
+ logger.setLevel(logging.WARNING)
36
+ stream_handler = logging.StreamHandler()
37
+ stream_handler.setLevel(logging.WARNING)
38
+ formatter = logging.Formatter("[%(name)s - %(levelname)s] %(message)s")
39
+ stream_handler.setFormatter(formatter)
40
+ logger.addHandler(stream_handler)
41
+
42
+ ATTR_MAPPING = {
43
+ "l": "level",
44
+ "m": "marginal",
45
+ "lo": "lower",
46
+ "up": "upper",
47
+ "scale": "scale",
48
+ "range": "range",
49
+ "slack": "slack",
50
+ "slacklo": "slacklo",
51
+ "slackup": "slackup",
52
+ "infeas": "infeas",
53
+ }
54
+
55
+ SET_ATTR_MAPPING = {
56
+ "pos": "position",
57
+ "ord": "order",
58
+ "off": "off",
59
+ "rev": "reverse",
60
+ "uel": "uel_position",
61
+ "len": "length",
62
+ "tlen": "text_length",
63
+ "val": "value",
64
+ "tval": "text_value",
65
+ "first": "is_first",
66
+ "last": "is_last",
67
+ }
68
+
69
+
70
+ class ImplicitParameter(ImplicitSymbol, operable.Operable):
71
+ def __init__(
72
+ self,
73
+ parent: Set | Alias | Parameter | Variable | Equation,
74
+ name: str,
75
+ domain: list[Set | str] = [],
76
+ records: Any | None = None,
77
+ permutation: list[int] | None = None,
78
+ scalar_domains: list[tuple[int, Set]] | None = None,
79
+ ) -> None:
80
+ """Implicit Parameter
81
+
82
+ Parameters
83
+ ----------
84
+ parent : Parameter | Variable | Equation
85
+ name : str
86
+ domain : list[Set | str], optional
87
+ records : Any, optional
88
+ """
89
+ super().__init__(parent, name, domain, permutation, scalar_domains)
90
+ self._records = records
91
+ self._assignment = None
92
+
93
+ def __getitem__(
94
+ self,
95
+ indices: EllipsisType | slice | Sequence | str | implicits.ImplicitSet,
96
+ ) -> ImplicitParameter:
97
+ domain = validation.validate_domain(self, indices)
98
+
99
+ return ImplicitParameter(
100
+ parent=self.parent,
101
+ name=self.name,
102
+ domain=domain,
103
+ permutation=self.permutation,
104
+ scalar_domains=self._scalar_domains,
105
+ )
106
+
107
+ def __setitem__(
108
+ self,
109
+ indices: EllipsisType | slice | Sequence | str | implicits.ImplicitSet,
110
+ rhs: Expression | Operation | ImplicitParameter | int | float,
111
+ ) -> None:
112
+ if (
113
+ isinstance(self.parent, (syms.Variable, syms.Equation))
114
+ and len(self.parent.domain) > 0
115
+ and all(len(elem) == 0 for elem in self.parent.domain)
116
+ ):
117
+ logger.warning(
118
+ f"Domain was not initialized. Default values for {self.gamsRepr()} will be used."
119
+ )
120
+ # self[domain] = rhs
121
+ domain = validation.validate_domain(self, indices)
122
+
123
+ if isinstance(rhs, float):
124
+ rhs = utils._map_special_values(rhs) # type: ignore
125
+
126
+ statement = expression.Expression(
127
+ ImplicitParameter(
128
+ parent=self.parent,
129
+ name=self.name,
130
+ domain=domain,
131
+ permutation=self.permutation,
132
+ scalar_domains=self._scalar_domains,
133
+ ),
134
+ "=",
135
+ rhs,
136
+ )
137
+
138
+ statement._validate_definition(utils._unpack(domain))
139
+
140
+ self.container._add_statement(statement)
141
+ self.parent._assignment = statement
142
+
143
+ self.container._synch_with_gams(gams_to_gamspy=True)
144
+ self.parent._winner = "gams"
145
+
146
+ def __eq__(self, other):
147
+ op = "eq"
148
+ if isinstance(
149
+ other,
150
+ (ImplicitVariable, expression.Expression, operation.Operation),
151
+ ):
152
+ op = "=e="
153
+ return expression.Expression(self, op, other)
154
+
155
+ def __ne__(self, other):
156
+ return expression.Expression(self, "ne", other)
157
+
158
+ def __repr__(self) -> str:
159
+ return f"ImplicitParameter(parent={self.parent}, name='{self.name}', domain={self.domain}, permutation={self.permutation}), parent_scalar_domains={self.parent_scalar_domains})"
160
+
161
+ @property
162
+ def records(self) -> pd.DataFrame | None:
163
+ if self.parent.records is None:
164
+ return None
165
+
166
+ if isinstance(self.parent, (syms.Set, syms.Alias)):
167
+ temp_name = "p" + utils._get_unique_name()
168
+ temp_param = syms.Parameter._constructor_bypass(
169
+ self.container, temp_name, [self.parent, "*"]
170
+ )
171
+ column_name = SET_ATTR_MAPPING[self.name.split(".")[1]]
172
+ temp_param[self.parent, column_name] = self
173
+ del self.container.data[temp_name]
174
+ return temp_param.records
175
+ elif isinstance(self.parent, syms.Parameter):
176
+ temp_name = "p" + utils._get_unique_name()
177
+ temp_param = syms.Parameter._constructor_bypass(
178
+ self.container, temp_name, self.parent.domain
179
+ )
180
+ domain = list(self.domain)
181
+ for i, d in self._scalar_domains:
182
+ domain.insert(i, d)
183
+
184
+ if domain == []:
185
+ domain = [...]
186
+
187
+ temp_param[domain] = self[...]
188
+ del self.container.data[temp_name]
189
+
190
+ recs = temp_param.records
191
+ return recs
192
+ elif isinstance(self.parent, (syms.Variable, syms.Equation)):
193
+ extension = self.name.split(".")[-1]
194
+ temp_name = "ip" + utils._get_unique_name()
195
+ temp_param = syms.Parameter._constructor_bypass(
196
+ self.container, temp_name, self.parent.domain
197
+ )
198
+ domain = list(self.domain)
199
+ for i, d in self._scalar_domains:
200
+ domain.insert(i, d)
201
+
202
+ if domain == []:
203
+ domain = [...]
204
+
205
+ temp_param[domain] = self
206
+ del self.container.data[temp_name]
207
+
208
+ recs = temp_param.records
209
+ if recs is None:
210
+ return recs
211
+
212
+ extension = ATTR_MAPPING[extension]
213
+ columns = recs.columns.to_list()
214
+ columns[columns.index("value")] = extension
215
+ recs.columns = columns
216
+
217
+ return recs
218
+
219
+ return None
220
+
221
+ @property
222
+ def T(self) -> ImplicitParameter:
223
+ """See gamspy.ImplicitParameter.t"""
224
+ return self.t()
225
+
226
+ def t(self) -> ImplicitParameter:
227
+ """Returns an ImplicitParameter derived from this
228
+ implicit parameter by swapping its last two indices.
229
+ This operation does not generate a new parameter in GAMS.
230
+
231
+ Examples
232
+ --------
233
+ >>> import gamspy as gp
234
+ >>> m = gp.Container()
235
+ >>> i = gp.Set(m, "i", records=['i1','i2'])
236
+ >>> j = gp.Set(m, "j", records=['j1','j2'])
237
+ >>> v = gp.Parameter(m, "v", domain=[i, j])
238
+ >>> v_t = v.t() # v_t is an ImplicitParameter
239
+ >>> v_t_t = v_t.t() # you can get transpose of ImplicitParameter as well
240
+ >>> v_t_t.domain
241
+ [Set(name='i', domain=['*']), Set(name='j', domain=['*'])]
242
+
243
+ """
244
+ dims = list(range(len(self.domain)))
245
+ if len(dims) < 2:
246
+ raise ValidationError(
247
+ "Parameter must contain at least 2 dimensions to transpose"
248
+ )
249
+
250
+ x = dims[-1]
251
+ dims[-1] = dims[-2]
252
+ dims[-2] = x
253
+ return permute(self, dims) # type: ignore
254
+
255
+ def gamsRepr(self) -> str:
256
+ """Representation of the parameter in GAMS syntax.
257
+
258
+ Returns:
259
+ str: String representation of the parameter in GAMS syntax.
260
+ """
261
+ representation = self.name
262
+ domain = list(self.domain)
263
+ if domain and self.permutation is not None:
264
+ domain = utils._permute_domain(domain, self.permutation)
265
+
266
+ for i, d in self._scalar_domains:
267
+ domain.insert(i, d)
268
+
269
+ if domain:
270
+ representation += utils._get_domain_str(domain)
271
+
272
+ return representation