gamsapi 52.5.0__cp312-cp312-win_amd64.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.
- gams/__init__.py +27 -0
- gams/_version.py +1 -0
- gams/connect/__init__.py +28 -0
- gams/connect/agents/__init__.py +24 -0
- gams/connect/agents/_excel/__init__.py +32 -0
- gams/connect/agents/_excel/excelagent.py +312 -0
- gams/connect/agents/_excel/workbook.py +155 -0
- gams/connect/agents/_sqlconnectors/__init__.py +42 -0
- gams/connect/agents/_sqlconnectors/_accesshandler.py +211 -0
- gams/connect/agents/_sqlconnectors/_databasehandler.py +250 -0
- gams/connect/agents/_sqlconnectors/_mysqlhandler.py +168 -0
- gams/connect/agents/_sqlconnectors/_postgreshandler.py +131 -0
- gams/connect/agents/_sqlconnectors/_pyodbchandler.py +112 -0
- gams/connect/agents/_sqlconnectors/_sqlalchemyhandler.py +74 -0
- gams/connect/agents/_sqlconnectors/_sqlitehandler.py +262 -0
- gams/connect/agents/_sqlconnectors/_sqlserverhandler.py +179 -0
- gams/connect/agents/concatenate.py +440 -0
- gams/connect/agents/connectagent.py +743 -0
- gams/connect/agents/csvreader.py +675 -0
- gams/connect/agents/csvwriter.py +151 -0
- gams/connect/agents/domainwriter.py +143 -0
- gams/connect/agents/excelreader.py +756 -0
- gams/connect/agents/excelwriter.py +467 -0
- gams/connect/agents/filter.py +223 -0
- gams/connect/agents/gamsreader.py +112 -0
- gams/connect/agents/gamswriter.py +239 -0
- gams/connect/agents/gdxreader.py +109 -0
- gams/connect/agents/gdxwriter.py +146 -0
- gams/connect/agents/labelmanipulator.py +303 -0
- gams/connect/agents/projection.py +539 -0
- gams/connect/agents/pythoncode.py +71 -0
- gams/connect/agents/rawcsvreader.py +248 -0
- gams/connect/agents/rawexcelreader.py +312 -0
- gams/connect/agents/schema/CSVReader.yaml +92 -0
- gams/connect/agents/schema/CSVWriter.yaml +44 -0
- gams/connect/agents/schema/Concatenate.yaml +52 -0
- gams/connect/agents/schema/DomainWriter.yaml +25 -0
- gams/connect/agents/schema/ExcelReader.yaml +121 -0
- gams/connect/agents/schema/ExcelWriter.yaml +78 -0
- gams/connect/agents/schema/Filter.yaml +74 -0
- gams/connect/agents/schema/GAMSReader.yaml +20 -0
- gams/connect/agents/schema/GAMSWriter.yaml +47 -0
- gams/connect/agents/schema/GDXReader.yaml +23 -0
- gams/connect/agents/schema/GDXWriter.yaml +32 -0
- gams/connect/agents/schema/LabelManipulator.yaml +99 -0
- gams/connect/agents/schema/Projection.yaml +24 -0
- gams/connect/agents/schema/PythonCode.yaml +6 -0
- gams/connect/agents/schema/RawCSVReader.yaml +34 -0
- gams/connect/agents/schema/RawExcelReader.yaml +42 -0
- gams/connect/agents/schema/SQLReader.yaml +75 -0
- gams/connect/agents/schema/SQLWriter.yaml +103 -0
- gams/connect/agents/sqlreader.py +301 -0
- gams/connect/agents/sqlwriter.py +276 -0
- gams/connect/connectdatabase.py +275 -0
- gams/connect/connectvalidator.py +93 -0
- gams/connect/errors.py +34 -0
- gams/control/__init__.py +136 -0
- gams/control/database.py +2231 -0
- gams/control/execution.py +1900 -0
- gams/control/options.py +2792 -0
- gams/control/workspace.py +1198 -0
- gams/core/__init__.py +24 -0
- gams/core/cfg/__init__.py +26 -0
- gams/core/cfg/_cfgmcc.cp312-win_amd64.pyd +0 -0
- gams/core/cfg/cfgmcc.py +519 -0
- gams/core/dct/__init__.py +26 -0
- gams/core/dct/_dctmcc.cp312-win_amd64.pyd +0 -0
- gams/core/dct/dctmcc.py +574 -0
- gams/core/embedded/__init__.py +26 -0
- gams/core/embedded/gamsemb.py +1024 -0
- gams/core/emp/__init__.py +24 -0
- gams/core/emp/emplexer.py +89 -0
- gams/core/emp/empyacc.py +281 -0
- gams/core/gdx/__init__.py +26 -0
- gams/core/gdx/_gdxcc.cp312-win_amd64.pyd +0 -0
- gams/core/gdx/gdxcc.py +866 -0
- gams/core/gev/__init__.py +26 -0
- gams/core/gev/_gevmcc.cp312-win_amd64.pyd +0 -0
- gams/core/gev/gevmcc.py +855 -0
- gams/core/gmd/__init__.py +26 -0
- gams/core/gmd/_gmdcc.cp312-win_amd64.pyd +0 -0
- gams/core/gmd/gmdcc.py +917 -0
- gams/core/gmo/__init__.py +26 -0
- gams/core/gmo/_gmomcc.cp312-win_amd64.pyd +0 -0
- gams/core/gmo/gmomcc.py +2046 -0
- gams/core/idx/__init__.py +26 -0
- gams/core/idx/_idxcc.cp312-win_amd64.pyd +0 -0
- gams/core/idx/idxcc.py +510 -0
- gams/core/numpy/__init__.py +29 -0
- gams/core/numpy/_gams2numpy.cp312-win_amd64.pyd +0 -0
- gams/core/numpy/gams2numpy.py +1048 -0
- gams/core/opt/__init__.py +26 -0
- gams/core/opt/_optcc.cp312-win_amd64.pyd +0 -0
- gams/core/opt/optcc.py +840 -0
- gams/engine/__init__.py +204 -0
- gams/engine/api/__init__.py +13 -0
- gams/engine/api/auth_api.py +7653 -0
- gams/engine/api/cleanup_api.py +751 -0
- gams/engine/api/default_api.py +887 -0
- gams/engine/api/hypercube_api.py +2629 -0
- gams/engine/api/jobs_api.py +5229 -0
- gams/engine/api/licenses_api.py +2220 -0
- gams/engine/api/namespaces_api.py +7783 -0
- gams/engine/api/usage_api.py +5627 -0
- gams/engine/api/users_api.py +5931 -0
- gams/engine/api_client.py +804 -0
- gams/engine/api_response.py +21 -0
- gams/engine/configuration.py +601 -0
- gams/engine/exceptions.py +216 -0
- gams/engine/models/__init__.py +86 -0
- gams/engine/models/bad_input.py +89 -0
- gams/engine/models/cleanable_job_result.py +104 -0
- gams/engine/models/cleanable_job_result_page.py +113 -0
- gams/engine/models/engine_license.py +107 -0
- gams/engine/models/files_not_found.py +93 -0
- gams/engine/models/forwarded_token_response.py +112 -0
- gams/engine/models/generic_key_value_pair.py +89 -0
- gams/engine/models/hypercube.py +160 -0
- gams/engine/models/hypercube_page.py +111 -0
- gams/engine/models/hypercube_summary.py +91 -0
- gams/engine/models/hypercube_token.py +97 -0
- gams/engine/models/identity_provider.py +107 -0
- gams/engine/models/identity_provider_ldap.py +121 -0
- gams/engine/models/identity_provider_oauth2.py +146 -0
- gams/engine/models/identity_provider_oauth2_scope.py +89 -0
- gams/engine/models/identity_provider_oauth2_with_secret.py +152 -0
- gams/engine/models/identity_provider_oidc.py +133 -0
- gams/engine/models/identity_provider_oidc_with_secret.py +143 -0
- gams/engine/models/inex.py +91 -0
- gams/engine/models/invitation.py +136 -0
- gams/engine/models/invitation_quota.py +106 -0
- gams/engine/models/invitation_token.py +87 -0
- gams/engine/models/job.py +165 -0
- gams/engine/models/job_no_text_entry.py +138 -0
- gams/engine/models/job_no_text_entry_page.py +111 -0
- gams/engine/models/license.py +91 -0
- gams/engine/models/log_piece.py +96 -0
- gams/engine/models/message.py +87 -0
- gams/engine/models/message_and_token.py +99 -0
- gams/engine/models/message_with_webhook_id.py +89 -0
- gams/engine/models/model_auth_token.py +87 -0
- gams/engine/models/model_configuration.py +125 -0
- gams/engine/models/model_default_instance.py +99 -0
- gams/engine/models/model_default_user_instance.py +98 -0
- gams/engine/models/model_hypercube_job.py +106 -0
- gams/engine/models/model_hypercube_usage.py +130 -0
- gams/engine/models/model_instance_info.py +116 -0
- gams/engine/models/model_instance_info_full.py +123 -0
- gams/engine/models/model_instance_pool_info.py +112 -0
- gams/engine/models/model_job_labels.py +179 -0
- gams/engine/models/model_job_usage.py +133 -0
- gams/engine/models/model_pool_usage.py +124 -0
- gams/engine/models/model_usage.py +115 -0
- gams/engine/models/model_user.py +96 -0
- gams/engine/models/model_userinstance_info.py +119 -0
- gams/engine/models/model_userinstancepool_info.py +95 -0
- gams/engine/models/model_version.py +91 -0
- gams/engine/models/models.py +120 -0
- gams/engine/models/namespace.py +104 -0
- gams/engine/models/namespace_quota.py +96 -0
- gams/engine/models/namespace_with_permission.py +96 -0
- gams/engine/models/not_found.py +91 -0
- gams/engine/models/password_policy.py +97 -0
- gams/engine/models/perm_and_username.py +89 -0
- gams/engine/models/quota.py +117 -0
- gams/engine/models/quota_exceeded.py +97 -0
- gams/engine/models/status_code_meaning.py +89 -0
- gams/engine/models/stream_entry.py +89 -0
- gams/engine/models/system_wide_license.py +92 -0
- gams/engine/models/text_entries.py +87 -0
- gams/engine/models/text_entry.py +101 -0
- gams/engine/models/time_span.py +95 -0
- gams/engine/models/time_span_pool_worker.py +99 -0
- gams/engine/models/token_forward_error.py +87 -0
- gams/engine/models/user.py +127 -0
- gams/engine/models/user_group_member.py +96 -0
- gams/engine/models/user_groups.py +108 -0
- gams/engine/models/vapid_info.py +87 -0
- gams/engine/models/webhook.py +138 -0
- gams/engine/models/webhook_parameterized_event.py +99 -0
- gams/engine/py.typed +0 -0
- gams/engine/rest.py +258 -0
- gams/magic/__init__.py +32 -0
- gams/magic/gams_magic.py +142 -0
- gams/magic/interactive.py +402 -0
- gams/tools/__init__.py +30 -0
- gams/tools/errors.py +34 -0
- gams/tools/toolcollection/__init__.py +24 -0
- gams/tools/toolcollection/alg/__init__.py +24 -0
- gams/tools/toolcollection/alg/rank.py +51 -0
- gams/tools/toolcollection/data/__init__.py +24 -0
- gams/tools/toolcollection/data/csvread.py +444 -0
- gams/tools/toolcollection/data/csvwrite.py +311 -0
- gams/tools/toolcollection/data/exceldump.py +47 -0
- gams/tools/toolcollection/data/sqlitewrite.py +276 -0
- gams/tools/toolcollection/gdxservice/__init__.py +24 -0
- gams/tools/toolcollection/gdxservice/gdxencoding.py +104 -0
- gams/tools/toolcollection/gdxservice/gdxrename.py +94 -0
- gams/tools/toolcollection/linalg/__init__.py +24 -0
- gams/tools/toolcollection/linalg/cholesky.py +57 -0
- gams/tools/toolcollection/linalg/eigenvalue.py +56 -0
- gams/tools/toolcollection/linalg/eigenvector.py +58 -0
- gams/tools/toolcollection/linalg/invert.py +55 -0
- gams/tools/toolcollection/linalg/ols.py +138 -0
- gams/tools/toolcollection/tooltemplate.py +321 -0
- gams/tools/toolcollection/win32/__init__.py +24 -0
- gams/tools/toolcollection/win32/excelmerge.py +93 -0
- gams/tools/toolcollection/win32/exceltalk.py +76 -0
- gams/tools/toolcollection/win32/msappavail.py +49 -0
- gams/tools/toolcollection/win32/shellexecute.py +54 -0
- gams/tools/tools.py +116 -0
- gams/transfer/__init__.py +35 -0
- gams/transfer/_abcs/__init__.py +37 -0
- gams/transfer/_abcs/container_abcs.py +433 -0
- gams/transfer/_internals/__init__.py +63 -0
- gams/transfer/_internals/algorithms.py +436 -0
- gams/transfer/_internals/casepreservingdict.py +124 -0
- gams/transfer/_internals/constants.py +270 -0
- gams/transfer/_internals/domainviolation.py +103 -0
- gams/transfer/_internals/specialvalues.py +172 -0
- gams/transfer/containers/__init__.py +26 -0
- gams/transfer/containers/_container.py +1794 -0
- gams/transfer/containers/_io/__init__.py +28 -0
- gams/transfer/containers/_io/containers.py +164 -0
- gams/transfer/containers/_io/gdx.py +1029 -0
- gams/transfer/containers/_io/gmd.py +872 -0
- gams/transfer/containers/_mixins/__init__.py +26 -0
- gams/transfer/containers/_mixins/ccc.py +1274 -0
- gams/transfer/syms/__init__.py +33 -0
- gams/transfer/syms/_methods/__init__.py +24 -0
- gams/transfer/syms/_methods/tables.py +120 -0
- gams/transfer/syms/_methods/toDict.py +115 -0
- gams/transfer/syms/_methods/toList.py +83 -0
- gams/transfer/syms/_methods/toValue.py +60 -0
- gams/transfer/syms/_mixins/__init__.py +32 -0
- gams/transfer/syms/_mixins/equals.py +626 -0
- gams/transfer/syms/_mixins/generateRecords.py +499 -0
- gams/transfer/syms/_mixins/pivot.py +313 -0
- gams/transfer/syms/_mixins/pve.py +627 -0
- gams/transfer/syms/_mixins/sa.py +27 -0
- gams/transfer/syms/_mixins/sapve.py +27 -0
- gams/transfer/syms/_mixins/saua.py +27 -0
- gams/transfer/syms/_mixins/sauapve.py +199 -0
- gams/transfer/syms/_mixins/spve.py +1528 -0
- gams/transfer/syms/_mixins/ve.py +936 -0
- gams/transfer/syms/container_syms/__init__.py +31 -0
- gams/transfer/syms/container_syms/_alias.py +984 -0
- gams/transfer/syms/container_syms/_equation.py +333 -0
- gams/transfer/syms/container_syms/_parameter.py +973 -0
- gams/transfer/syms/container_syms/_set.py +604 -0
- gams/transfer/syms/container_syms/_universe_alias.py +461 -0
- gams/transfer/syms/container_syms/_variable.py +321 -0
- gamsapi-52.5.0.dist-info/METADATA +150 -0
- gamsapi-52.5.0.dist-info/RECORD +257 -0
- gamsapi-52.5.0.dist-info/WHEEL +5 -0
- gamsapi-52.5.0.dist-info/licenses/LICENSE +22 -0
- gamsapi-52.5.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
#
|
|
2
|
+
# GAMS - General Algebraic Modeling System Python API
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2017-2026 GAMS Development Corp. <support@gams.com>
|
|
5
|
+
# Copyright (c) 2017-2026 GAMS Software GmbH <support@gams.com>
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
# furnished to do so, subject to the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
# copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
# SOFTWARE.
|
|
24
|
+
#
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
import copy
|
|
28
|
+
import itertools
|
|
29
|
+
import weakref
|
|
30
|
+
import pandas as pd
|
|
31
|
+
from pandas.api.types import CategoricalDtype, is_bool_dtype
|
|
32
|
+
from gams.core import gdx
|
|
33
|
+
from gams.transfer._abcs import ABCSet, ABCContainer
|
|
34
|
+
from gams.transfer._internals import generate_unique_labels
|
|
35
|
+
from gams.transfer.syms._mixins import (
|
|
36
|
+
SAMixin,
|
|
37
|
+
SAPVEMixin,
|
|
38
|
+
SAUAMixin,
|
|
39
|
+
SAUAPVEMixin,
|
|
40
|
+
SPVEMixin,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
from gams.transfer.syms._mixins.pivot import PivotSetMixin
|
|
44
|
+
from gams.transfer.syms._mixins.generateRecords import GenerateRecordsSetMixin
|
|
45
|
+
from gams.transfer.syms._mixins.equals import EqualsSetMixin
|
|
46
|
+
from typing import Any, List, Optional, Union, TYPE_CHECKING
|
|
47
|
+
|
|
48
|
+
if TYPE_CHECKING:
|
|
49
|
+
from gams.transfer import Container
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Set(
|
|
53
|
+
SAMixin,
|
|
54
|
+
SAPVEMixin,
|
|
55
|
+
SAUAMixin,
|
|
56
|
+
SAUAPVEMixin,
|
|
57
|
+
SPVEMixin,
|
|
58
|
+
PivotSetMixin,
|
|
59
|
+
GenerateRecordsSetMixin,
|
|
60
|
+
EqualsSetMixin,
|
|
61
|
+
ABCSet,
|
|
62
|
+
):
|
|
63
|
+
"""
|
|
64
|
+
Represents a Set symbol in GAMS.
|
|
65
|
+
https://www.gams.com/latest/docs/UG_SetDefinition.html
|
|
66
|
+
|
|
67
|
+
Parameters
|
|
68
|
+
----------
|
|
69
|
+
container : Container
|
|
70
|
+
name : str
|
|
71
|
+
domain : list, optional
|
|
72
|
+
is_singleton : bool, optional
|
|
73
|
+
records : int | float | DataFrame, optional
|
|
74
|
+
domain_forwarding : bool, optional
|
|
75
|
+
description : str, optional
|
|
76
|
+
|
|
77
|
+
Examples
|
|
78
|
+
--------
|
|
79
|
+
>>> import gams.transfer as gt
|
|
80
|
+
>>> m = gt.Container()
|
|
81
|
+
>>> i = gt.Set(m, "i", records=['i1','i2'])
|
|
82
|
+
|
|
83
|
+
Attributes
|
|
84
|
+
----------
|
|
85
|
+
container : Container object
|
|
86
|
+
Container where the symbol exists
|
|
87
|
+
description : str
|
|
88
|
+
description of symbol
|
|
89
|
+
dimension : int
|
|
90
|
+
The dimension of symbol
|
|
91
|
+
domain : List[Set | Alias | str]
|
|
92
|
+
List of domains given either as string (* for universe set) or as reference to the Set/Alias object
|
|
93
|
+
domain_forwarding : bool
|
|
94
|
+
Flag that identifies if domain forwarding is enabled for the symbol
|
|
95
|
+
domain_labels : List[str]
|
|
96
|
+
The column headings for the records DataFrame
|
|
97
|
+
domain_names : List[str]
|
|
98
|
+
String version of domain names
|
|
99
|
+
domain_type : str
|
|
100
|
+
The state of domain links
|
|
101
|
+
is_singleton : bool
|
|
102
|
+
Flag that identifies if the the alias is singleton
|
|
103
|
+
modified : bool
|
|
104
|
+
Flag that identifies if the symbol has been modified
|
|
105
|
+
name : str
|
|
106
|
+
Name of the symbol
|
|
107
|
+
number_records : int
|
|
108
|
+
The number of symbol records
|
|
109
|
+
records : DataFrame
|
|
110
|
+
The main symbol records
|
|
111
|
+
summary : dict
|
|
112
|
+
A dict of only the metadata
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
@classmethod
|
|
116
|
+
def _from_gams(
|
|
117
|
+
cls,
|
|
118
|
+
container,
|
|
119
|
+
name,
|
|
120
|
+
domain,
|
|
121
|
+
is_singleton=False,
|
|
122
|
+
records=None,
|
|
123
|
+
description="",
|
|
124
|
+
):
|
|
125
|
+
# create new symbol object
|
|
126
|
+
obj = Set.__new__(cls)
|
|
127
|
+
|
|
128
|
+
# set private properties directly
|
|
129
|
+
obj._requires_state_check = False
|
|
130
|
+
obj._container = weakref.proxy(container)
|
|
131
|
+
obj._name = name
|
|
132
|
+
obj._domain = domain
|
|
133
|
+
obj._domain_forwarding = False
|
|
134
|
+
obj._description = description
|
|
135
|
+
|
|
136
|
+
obj._records = records
|
|
137
|
+
obj._modified = True
|
|
138
|
+
obj._is_singleton = is_singleton
|
|
139
|
+
|
|
140
|
+
# typing
|
|
141
|
+
if obj.is_singleton:
|
|
142
|
+
obj._gams_type = gdx.GMS_DT_SET
|
|
143
|
+
obj._gams_subtype = 1
|
|
144
|
+
else:
|
|
145
|
+
obj._gams_type = gdx.GMS_DT_SET
|
|
146
|
+
obj._gams_subtype = 0
|
|
147
|
+
|
|
148
|
+
# add to container
|
|
149
|
+
obj._container.data.update({name: obj})
|
|
150
|
+
obj._container._requires_state_check = True
|
|
151
|
+
|
|
152
|
+
return obj
|
|
153
|
+
|
|
154
|
+
def __new__(cls, *args, **kwargs):
|
|
155
|
+
# fastpath
|
|
156
|
+
if len(args) == len(kwargs) == 0:
|
|
157
|
+
return object.__new__(cls)
|
|
158
|
+
|
|
159
|
+
try:
|
|
160
|
+
container = args[0]
|
|
161
|
+
except IndexError:
|
|
162
|
+
container = kwargs.get("container", None)
|
|
163
|
+
|
|
164
|
+
try:
|
|
165
|
+
name = args[1]
|
|
166
|
+
except IndexError:
|
|
167
|
+
name = kwargs.get("name", None)
|
|
168
|
+
|
|
169
|
+
try:
|
|
170
|
+
symobj = container[name]
|
|
171
|
+
except (KeyError, IndexError, TypeError):
|
|
172
|
+
symobj = None
|
|
173
|
+
|
|
174
|
+
if symobj is None:
|
|
175
|
+
return object.__new__(cls)
|
|
176
|
+
else:
|
|
177
|
+
if isinstance(symobj, cls):
|
|
178
|
+
return symobj
|
|
179
|
+
else:
|
|
180
|
+
raise TypeError(
|
|
181
|
+
f"Cannot overwrite symbol '{symobj.name}' in container because it is not a {cls.__name__} object"
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def __init__(
|
|
185
|
+
self,
|
|
186
|
+
container: "Container",
|
|
187
|
+
name: str,
|
|
188
|
+
domain: Optional[List[Union[Set, str]]] = None,
|
|
189
|
+
is_singleton: bool = False,
|
|
190
|
+
records: Optional[Any] = None,
|
|
191
|
+
domain_forwarding: bool = False,
|
|
192
|
+
description: str = "",
|
|
193
|
+
uels_on_axes: bool = False,
|
|
194
|
+
):
|
|
195
|
+
# domain handling
|
|
196
|
+
if domain is None:
|
|
197
|
+
domain = ["*"]
|
|
198
|
+
|
|
199
|
+
if isinstance(domain, (Set, str)):
|
|
200
|
+
domain = [domain]
|
|
201
|
+
|
|
202
|
+
# does symbol exist
|
|
203
|
+
has_symbol = False
|
|
204
|
+
if isinstance(getattr(self, "container", None), ABCContainer):
|
|
205
|
+
has_symbol = True
|
|
206
|
+
|
|
207
|
+
if has_symbol:
|
|
208
|
+
try:
|
|
209
|
+
if not isinstance(self, Set):
|
|
210
|
+
raise TypeError(
|
|
211
|
+
f"Cannot overwrite symbol {self.name} in container"
|
|
212
|
+
" because it is not a Set object)"
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
if any(
|
|
216
|
+
d1 != d2 for d1, d2 in itertools.zip_longest(self.domain, domain)
|
|
217
|
+
):
|
|
218
|
+
raise ValueError(
|
|
219
|
+
"Cannot overwrite symbol in container unless symbol"
|
|
220
|
+
" domains are equal"
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
if self.is_singleton != is_singleton:
|
|
224
|
+
raise ValueError(
|
|
225
|
+
"Cannot overwrite symbol in container unless"
|
|
226
|
+
" 'is_singleton' is left unchanged"
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
if self.domain_forwarding != domain_forwarding:
|
|
230
|
+
raise ValueError(
|
|
231
|
+
"Cannot overwrite symbol in container unless"
|
|
232
|
+
" 'domain_forwarding' is left unchanged"
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
except ValueError as err:
|
|
236
|
+
raise ValueError(err)
|
|
237
|
+
|
|
238
|
+
except TypeError as err:
|
|
239
|
+
raise TypeError(err)
|
|
240
|
+
|
|
241
|
+
# reset some properties
|
|
242
|
+
self._requires_state_check = True
|
|
243
|
+
self.container._requires_state_check = True
|
|
244
|
+
if description != "":
|
|
245
|
+
self.description = description
|
|
246
|
+
self.records = None
|
|
247
|
+
self.modified = True
|
|
248
|
+
|
|
249
|
+
# only set records if records are provided
|
|
250
|
+
if records is not None:
|
|
251
|
+
self.setRecords(records, uels_on_axes=uels_on_axes)
|
|
252
|
+
|
|
253
|
+
else:
|
|
254
|
+
# populate new symbol properties
|
|
255
|
+
self._requires_state_check = True
|
|
256
|
+
self.container = container
|
|
257
|
+
self.container._requires_state_check = True
|
|
258
|
+
self.name = name
|
|
259
|
+
self.domain = domain
|
|
260
|
+
self.domain_forwarding = domain_forwarding
|
|
261
|
+
self.description = description
|
|
262
|
+
self.records = None
|
|
263
|
+
self.modified = True
|
|
264
|
+
self.is_singleton = is_singleton
|
|
265
|
+
|
|
266
|
+
# typing
|
|
267
|
+
if self.is_singleton:
|
|
268
|
+
self._gams_type = gdx.GMS_DT_SET
|
|
269
|
+
self._gams_subtype = 1
|
|
270
|
+
else:
|
|
271
|
+
self._gams_type = gdx.GMS_DT_SET
|
|
272
|
+
self._gams_subtype = 0
|
|
273
|
+
|
|
274
|
+
# only set records if records are provided
|
|
275
|
+
if records is not None:
|
|
276
|
+
self.setRecords(records, uels_on_axes=uels_on_axes)
|
|
277
|
+
|
|
278
|
+
# add to container
|
|
279
|
+
container.data.update({name: self})
|
|
280
|
+
|
|
281
|
+
def __repr__(self):
|
|
282
|
+
return f"<Set `{self.name}` ({hex(id(self))})>"
|
|
283
|
+
|
|
284
|
+
def __delitem__(self):
|
|
285
|
+
# TODO: add in some functionality that might relax the symbols down to a different domain
|
|
286
|
+
# This function would mimic the <Container>.removeSymbols() method -- is more pythonic
|
|
287
|
+
del self.container.data[self.name]
|
|
288
|
+
|
|
289
|
+
@property
|
|
290
|
+
def _attributes(self):
|
|
291
|
+
return ["element_text"]
|
|
292
|
+
|
|
293
|
+
@property
|
|
294
|
+
def summary(self) -> dict:
|
|
295
|
+
"""
|
|
296
|
+
Returns a dict of only the metadata
|
|
297
|
+
|
|
298
|
+
Returns
|
|
299
|
+
-------
|
|
300
|
+
dict
|
|
301
|
+
Outputs a dict of only the metadata
|
|
302
|
+
"""
|
|
303
|
+
return {
|
|
304
|
+
"name": self.name,
|
|
305
|
+
"description": self.description,
|
|
306
|
+
"is_singleton": self.is_singleton,
|
|
307
|
+
"domain": self.domain_names,
|
|
308
|
+
"domain_type": self.domain_type,
|
|
309
|
+
"dimension": self.dimension,
|
|
310
|
+
"number_records": self.number_records,
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
def toList(
|
|
314
|
+
self, include_element_text=False
|
|
315
|
+
) -> Union[List[Union[str, tuple]], None]:
|
|
316
|
+
"""
|
|
317
|
+
Convenience method to return symbol records as a python list
|
|
318
|
+
|
|
319
|
+
Parameters
|
|
320
|
+
----------
|
|
321
|
+
include_element_text : bool, optional
|
|
322
|
+
If True, include the element text as tuples (record, element text).
|
|
323
|
+
If False, return a list of records only.
|
|
324
|
+
|
|
325
|
+
Returns
|
|
326
|
+
-------
|
|
327
|
+
list | None
|
|
328
|
+
A list containing the records of the symbol, None if no record was assigned
|
|
329
|
+
|
|
330
|
+
Examples
|
|
331
|
+
--------
|
|
332
|
+
>>> m = gt.Container()
|
|
333
|
+
>>> i = gt.Set(m, "i", records=["new-york", "chicago", "topeka"])
|
|
334
|
+
>>> print(i.toList())
|
|
335
|
+
['new-york', 'chicago', 'topeka']
|
|
336
|
+
|
|
337
|
+
"""
|
|
338
|
+
from gams.transfer.syms._methods.toList import toListSet
|
|
339
|
+
|
|
340
|
+
if not self.isValid():
|
|
341
|
+
raise Exception(
|
|
342
|
+
f"Cannot extract list because `{self.name}` is not a valid"
|
|
343
|
+
f" symbol object. Use `{self.name}.isValid(verbose=True)` to"
|
|
344
|
+
" debug."
|
|
345
|
+
)
|
|
346
|
+
return toListSet(self, include_element_text=include_element_text)
|
|
347
|
+
|
|
348
|
+
@property
|
|
349
|
+
def is_singleton(self) -> bool:
|
|
350
|
+
"""
|
|
351
|
+
Whether a symbol is a singleton set
|
|
352
|
+
|
|
353
|
+
Returns
|
|
354
|
+
-------
|
|
355
|
+
bool
|
|
356
|
+
True if the symbol is a singleton set; False otherwise
|
|
357
|
+
"""
|
|
358
|
+
return self._is_singleton
|
|
359
|
+
|
|
360
|
+
@is_singleton.setter
|
|
361
|
+
def is_singleton(self, is_singleton):
|
|
362
|
+
if not isinstance(is_singleton, bool):
|
|
363
|
+
raise TypeError("Argument 'is_singleton' must be type bool")
|
|
364
|
+
|
|
365
|
+
# check to see if _is_singleton is being changed
|
|
366
|
+
if getattr(self, "_is_singleton", None) != is_singleton:
|
|
367
|
+
self._requires_state_check = True
|
|
368
|
+
|
|
369
|
+
if isinstance(self.container, ABCContainer):
|
|
370
|
+
self.container._requires_state_check = True
|
|
371
|
+
|
|
372
|
+
self._is_singleton = is_singleton
|
|
373
|
+
|
|
374
|
+
def setRecords(self, records: Any, uels_on_axes: bool = False) -> None:
|
|
375
|
+
"""
|
|
376
|
+
main convenience method to set standard pandas.DataFrame formatted records.
|
|
377
|
+
If uels_on_axes=True setRecords will assume that all domain information is contained in the axes of the pandas object – data will be flattened (if necessary).
|
|
378
|
+
|
|
379
|
+
Parameters
|
|
380
|
+
----------
|
|
381
|
+
records : Any
|
|
382
|
+
uels_on_axes : bool, optional
|
|
383
|
+
"""
|
|
384
|
+
if not isinstance(uels_on_axes, bool):
|
|
385
|
+
raise TypeError("Argument 'uels_on_axes' must be type bool")
|
|
386
|
+
|
|
387
|
+
if isinstance(records, pd.DataFrame):
|
|
388
|
+
self._from_dataframe(records, uels_on_axes=uels_on_axes)
|
|
389
|
+
|
|
390
|
+
elif isinstance(records, pd.Series):
|
|
391
|
+
if uels_on_axes:
|
|
392
|
+
self._from_series(records)
|
|
393
|
+
else:
|
|
394
|
+
if self.dimension != 1:
|
|
395
|
+
raise Exception(
|
|
396
|
+
"Dimensionality of data (1) is inconsistent "
|
|
397
|
+
f"with domain specification ({self.dimension})"
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
records = pd.DataFrame(records)
|
|
401
|
+
r, c = records.shape
|
|
402
|
+
|
|
403
|
+
if c == self.dimension:
|
|
404
|
+
records = records.assign(element_text="")
|
|
405
|
+
|
|
406
|
+
records.columns = (
|
|
407
|
+
generate_unique_labels(self.domain_names) + self._attributes
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
self._from_dataframe(records)
|
|
411
|
+
|
|
412
|
+
else:
|
|
413
|
+
self._from_else(records)
|
|
414
|
+
|
|
415
|
+
def _from_series(self, records: pd.Series) -> None:
|
|
416
|
+
from gams.transfer.syms._methods.tables import (
|
|
417
|
+
_assert_axes_no_nans,
|
|
418
|
+
_get_implied_dimension_from_axes,
|
|
419
|
+
_flatten_and_convert,
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
records = copy.deepcopy(records)
|
|
423
|
+
|
|
424
|
+
# check if index has NaNs
|
|
425
|
+
try:
|
|
426
|
+
_assert_axes_no_nans(records)
|
|
427
|
+
except Exception as err:
|
|
428
|
+
raise err
|
|
429
|
+
|
|
430
|
+
# else:
|
|
431
|
+
dim = _get_implied_dimension_from_axes(records)
|
|
432
|
+
if dim != self.dimension:
|
|
433
|
+
raise Exception(
|
|
434
|
+
f"Dimensionality of data ({dim}) is inconsistent "
|
|
435
|
+
f"with domain specification ({self.dimension})"
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
# flatten and convert to categorical
|
|
439
|
+
records = _flatten_and_convert(records)
|
|
440
|
+
|
|
441
|
+
# reset columns
|
|
442
|
+
records.columns = generate_unique_labels(self.domain_names) + self._attributes
|
|
443
|
+
|
|
444
|
+
# fill any missing element_text with empty str and convert everything to str
|
|
445
|
+
records.isetitem(-1, records.iloc[:, -1].astype(object))
|
|
446
|
+
records.iloc[records.iloc[:, -1].isna(), -1] = ""
|
|
447
|
+
records.isetitem(-1, records.iloc[:, -1].astype(str))
|
|
448
|
+
|
|
449
|
+
# set records
|
|
450
|
+
self.records = records
|
|
451
|
+
|
|
452
|
+
def _from_dataframe(
|
|
453
|
+
self, records: pd.DataFrame, uels_on_axes: bool = False
|
|
454
|
+
) -> None:
|
|
455
|
+
if uels_on_axes:
|
|
456
|
+
if is_bool_dtype(records.to_numpy().dtype):
|
|
457
|
+
self._from_table_dataframe(records)
|
|
458
|
+
|
|
459
|
+
else:
|
|
460
|
+
if any(
|
|
461
|
+
not is_bool_dtype(dtype)
|
|
462
|
+
for dtype in records.convert_dtypes().dtypes
|
|
463
|
+
):
|
|
464
|
+
raise TypeError(
|
|
465
|
+
"Encountered records that could not successfully be"
|
|
466
|
+
" converted to type bool. All columns must be type"
|
|
467
|
+
" bool when `uels_on_axes=True`"
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
else:
|
|
471
|
+
self._from_table_dataframe(records.convert_dtypes())
|
|
472
|
+
else:
|
|
473
|
+
self._from_else(records)
|
|
474
|
+
|
|
475
|
+
def _from_table_dataframe(self, records: pd.DataFrame) -> None:
|
|
476
|
+
from gams.transfer.syms._methods.tables import (
|
|
477
|
+
_assert_axes_no_nans,
|
|
478
|
+
_get_implied_dimension_from_axes,
|
|
479
|
+
_flatten_and_convert,
|
|
480
|
+
)
|
|
481
|
+
|
|
482
|
+
records = pd.DataFrame(copy.deepcopy(records))
|
|
483
|
+
|
|
484
|
+
# check if index has NaNs
|
|
485
|
+
try:
|
|
486
|
+
_assert_axes_no_nans(records)
|
|
487
|
+
except Exception as err:
|
|
488
|
+
raise err
|
|
489
|
+
|
|
490
|
+
# check dimensionality of data
|
|
491
|
+
dim = _get_implied_dimension_from_axes(records)
|
|
492
|
+
if dim != self.dimension:
|
|
493
|
+
raise Exception(
|
|
494
|
+
f"Dimensionality of table ({dim}) is inconsistent "
|
|
495
|
+
f"with set domain specification ({self.dimension})"
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
# flatten and convert to categorical
|
|
499
|
+
records = _flatten_and_convert(records)
|
|
500
|
+
|
|
501
|
+
# drop records and reset_index
|
|
502
|
+
records.drop(index=records[records.iloc[:, -1] == False].index, inplace=True)
|
|
503
|
+
records.drop(columns=records.columns[-1], inplace=True)
|
|
504
|
+
records.reset_index(drop=True, inplace=True)
|
|
505
|
+
|
|
506
|
+
# add in element_text column
|
|
507
|
+
records["element_text"] = ""
|
|
508
|
+
|
|
509
|
+
# set columns names
|
|
510
|
+
records.columns = generate_unique_labels(self.domain_names) + self._attributes
|
|
511
|
+
|
|
512
|
+
# set records
|
|
513
|
+
self.records = records
|
|
514
|
+
|
|
515
|
+
def _from_else(self, records: Any) -> None:
|
|
516
|
+
from_dataframe = False
|
|
517
|
+
|
|
518
|
+
if isinstance(records, str):
|
|
519
|
+
records = [records]
|
|
520
|
+
|
|
521
|
+
# check dimensionality of data
|
|
522
|
+
try:
|
|
523
|
+
# create a deep copy of all passed DataFrames
|
|
524
|
+
if isinstance(records, pd.DataFrame):
|
|
525
|
+
records = pd.DataFrame(copy.deepcopy(records))
|
|
526
|
+
from_dataframe = True
|
|
527
|
+
else:
|
|
528
|
+
records = pd.DataFrame(records)
|
|
529
|
+
except Exception as err:
|
|
530
|
+
raise Exception(
|
|
531
|
+
"Data structure passed as argument 'records' could not be"
|
|
532
|
+
" successfully converted into a pandas DataFrame (reason:"
|
|
533
|
+
f" {err})."
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
# check dimensionality
|
|
537
|
+
r, c = records.shape
|
|
538
|
+
if c == self.dimension:
|
|
539
|
+
records = records.assign(element_text="")
|
|
540
|
+
|
|
541
|
+
r, c = records.shape
|
|
542
|
+
if c - 1 != self.dimension:
|
|
543
|
+
raise Exception(
|
|
544
|
+
f"Dimensionality of records ({c - 1}) is inconsistent "
|
|
545
|
+
f"with set domain specification ({self.dimension})"
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
# keep user defined categories if provided
|
|
549
|
+
for i in range(self.dimension):
|
|
550
|
+
# create categorical
|
|
551
|
+
if not isinstance(records.iloc[:, i].dtype, CategoricalDtype):
|
|
552
|
+
records.isetitem(
|
|
553
|
+
i,
|
|
554
|
+
records.iloc[:, i].astype(
|
|
555
|
+
CategoricalDtype(
|
|
556
|
+
categories=records.iloc[:, i].unique(),
|
|
557
|
+
ordered=True,
|
|
558
|
+
)
|
|
559
|
+
),
|
|
560
|
+
)
|
|
561
|
+
|
|
562
|
+
# capture user categories
|
|
563
|
+
old_cats = records.iloc[:, i].cat.categories.tolist()
|
|
564
|
+
is_ordered = records.iloc[:, i].cat.ordered
|
|
565
|
+
|
|
566
|
+
# convert any non-str categories to str, strip trailing white-space and de-dup
|
|
567
|
+
new_cats = list(dict.fromkeys(list(map(str.rstrip, map(str, old_cats)))))
|
|
568
|
+
|
|
569
|
+
# if categories are not unique after strip then need to remake the categorical
|
|
570
|
+
if len(old_cats) != len(new_cats):
|
|
571
|
+
# convert data to str and strip trailing white-space from data
|
|
572
|
+
records.isetitem(
|
|
573
|
+
i,
|
|
574
|
+
records.iloc[:, i]
|
|
575
|
+
.astype(str)
|
|
576
|
+
.map(str.rstrip)
|
|
577
|
+
.astype(CategoricalDtype(categories=new_cats, ordered=is_ordered)),
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
else:
|
|
581
|
+
# only need to rename the categories
|
|
582
|
+
records.isetitem(
|
|
583
|
+
i,
|
|
584
|
+
records.iloc[:, i].cat.rename_categories(new_cats),
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
# set column names
|
|
588
|
+
if from_dataframe:
|
|
589
|
+
records.columns = (
|
|
590
|
+
generate_unique_labels(records.columns[: self.dimension].tolist())
|
|
591
|
+
+ self._attributes
|
|
592
|
+
)
|
|
593
|
+
else:
|
|
594
|
+
records.columns = (
|
|
595
|
+
generate_unique_labels(self.domain_names) + self._attributes
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
# fill any missing element_text with empty str and convert everything to str
|
|
599
|
+
records.isetitem(-1, records.iloc[:, -1].astype(object))
|
|
600
|
+
records.iloc[records.iloc[:, -1].isna(), -1] = ""
|
|
601
|
+
records.isetitem(-1, records.iloc[:, -1].astype(str))
|
|
602
|
+
|
|
603
|
+
# set records
|
|
604
|
+
self.records = records
|