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,151 @@
|
|
|
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
|
+
import os
|
|
27
|
+
from gams import transfer as gt
|
|
28
|
+
from gams.connect.agents.connectagent import ConnectAgent
|
|
29
|
+
import pandas as pd
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class CSVWriter(ConnectAgent):
|
|
33
|
+
|
|
34
|
+
def __init__(self, cdb, inst, agent_index):
|
|
35
|
+
super().__init__(cdb, inst, agent_index)
|
|
36
|
+
self._parse_options(self._inst)
|
|
37
|
+
|
|
38
|
+
def _parse_options(self, inst):
|
|
39
|
+
inst["file"] = os.path.abspath(inst["file"])
|
|
40
|
+
self._file = inst["file"]
|
|
41
|
+
self._name = inst["name"]
|
|
42
|
+
self._header = inst["header"]
|
|
43
|
+
self._set_header = inst["setHeader"]
|
|
44
|
+
self._unstack = inst["unstack"]
|
|
45
|
+
self._skip_text = inst["skipText"]
|
|
46
|
+
self._field_sep = inst["fieldSeparator"]
|
|
47
|
+
self._decimal_sep = inst["decimalSeparator"]
|
|
48
|
+
self._quoting = inst["quoting"]
|
|
49
|
+
self._trace = inst["trace"]
|
|
50
|
+
self._to_csv_arguments = self._dict_get(inst, "toCSVArguments", {})
|
|
51
|
+
self._value_subs = self._dict_get(inst, "valueSubstitutions", {})
|
|
52
|
+
|
|
53
|
+
if isinstance(self._unstack, list):
|
|
54
|
+
self._unstack = [i - 1 for i in self._unstack]
|
|
55
|
+
|
|
56
|
+
def execute(self):
|
|
57
|
+
if self._trace > 0:
|
|
58
|
+
self._log_instructions(self._inst, self._inst_raw)
|
|
59
|
+
self._describe_container(self._cdb.container, "Connect Container:")
|
|
60
|
+
|
|
61
|
+
self._symbols_exist_cdb(self._name, should_exist=True)
|
|
62
|
+
gt_sym = self._cdb.container[self._name]
|
|
63
|
+
|
|
64
|
+
if self._trace > 2:
|
|
65
|
+
self._cdb.print_log(
|
|
66
|
+
f"Connect Container symbol={self._name}:\n {gt_sym.records}\n"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
if not isinstance(gt_sym, gt.Set) and not isinstance(gt_sym, gt.Parameter):
|
|
70
|
+
self._connect_error(
|
|
71
|
+
f"Symbol type >{type(gt_sym)}< of symbol >{self._name}< is not supported. Supported symbol types are set and parameter."
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
dim = gt_sym.dimension
|
|
75
|
+
df = self._sym_records_no_none(gt_sym).copy(deep=True)
|
|
76
|
+
|
|
77
|
+
df = self._apply_value_substitutions(
|
|
78
|
+
df,
|
|
79
|
+
self._value_subs,
|
|
80
|
+
"par" if isinstance(gt_sym, gt.Parameter) else "set",
|
|
81
|
+
sv_eps="EPS",
|
|
82
|
+
sv_na="NA",
|
|
83
|
+
sv_undef="UNDEF",
|
|
84
|
+
sv_posinf="INF",
|
|
85
|
+
sv_neginf="-INF",
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
if dim != 0:
|
|
89
|
+
df = df.set_index(list(df.columns[:dim]))
|
|
90
|
+
index = True
|
|
91
|
+
if self._trace > 2:
|
|
92
|
+
self._cdb.print_log(f"DataFrame after .set_index():\n{df}")
|
|
93
|
+
else:
|
|
94
|
+
index = False
|
|
95
|
+
|
|
96
|
+
if self._unstack and dim > 1:
|
|
97
|
+
if isinstance(self._unstack, list):
|
|
98
|
+
df = df.unstack(
|
|
99
|
+
level=self._unstack
|
|
100
|
+
) # unstacks and sorts remaining index
|
|
101
|
+
else:
|
|
102
|
+
df = df.unstack() # unstacks and sorts remaining index
|
|
103
|
+
df.columns = df.columns.droplevel()
|
|
104
|
+
df.sort_index(axis="columns", inplace=True) # sorts column indices
|
|
105
|
+
if self._trace > 2:
|
|
106
|
+
self._cdb.print_log(f"DataFrame after unstack:\n{df}")
|
|
107
|
+
if isinstance(gt_sym, gt.Set):
|
|
108
|
+
if self._skip_text:
|
|
109
|
+
# pandas-version-check
|
|
110
|
+
if hasattr(pd.DataFrame, "map"): # pandas >= 2.1.0
|
|
111
|
+
df = df.map(lambda x: "Y" if isinstance(x, str) else x)
|
|
112
|
+
else: # pandas < 2.1.0
|
|
113
|
+
df = df.applymap(lambda x: "Y" if isinstance(x, str) else x)
|
|
114
|
+
else:
|
|
115
|
+
df = df.replace("", "Y")
|
|
116
|
+
else:
|
|
117
|
+
if index:
|
|
118
|
+
# sort index
|
|
119
|
+
df.sort_index(inplace=True)
|
|
120
|
+
if self._trace > 2:
|
|
121
|
+
self._cdb.print_log(f"DataFrame after sort:\n{df}")
|
|
122
|
+
if isinstance(gt_sym, gt.Set) and self._skip_text:
|
|
123
|
+
df = df.drop(columns="element_text")
|
|
124
|
+
|
|
125
|
+
if self._set_header is not None:
|
|
126
|
+
if self._trace > 1:
|
|
127
|
+
self._cdb.print_log(
|
|
128
|
+
"Write header first and switch to append mode to append the DataFrame."
|
|
129
|
+
)
|
|
130
|
+
with open(self._file, "w") as file:
|
|
131
|
+
file.write(f"{self._set_header}\n")
|
|
132
|
+
to_csv_args = {"mode": "a", "header": False}
|
|
133
|
+
else:
|
|
134
|
+
to_csv_args = {"header": self._header}
|
|
135
|
+
to_csv_args.update(
|
|
136
|
+
{
|
|
137
|
+
"index": index,
|
|
138
|
+
"sep": f"{self._field_sep}",
|
|
139
|
+
"decimal": f"{self._decimal_sep}",
|
|
140
|
+
"quoting": self._quoting,
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
to_csv_args.update(self._to_csv_arguments)
|
|
144
|
+
|
|
145
|
+
if self._trace > 2:
|
|
146
|
+
self._cdb.print_log(
|
|
147
|
+
f"Final DataFrame that will be processed by pandas.to_csv:\n{df}"
|
|
148
|
+
)
|
|
149
|
+
self._cdb.print_log(f"pandas.to_csv arguments:\n{to_csv_args}")
|
|
150
|
+
|
|
151
|
+
df.to_csv(self._file, **to_csv_args)
|
|
@@ -0,0 +1,143 @@
|
|
|
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
|
+
import re
|
|
27
|
+
from gams import transfer as gt
|
|
28
|
+
from gams.connect.agents.connectagent import ConnectAgent
|
|
29
|
+
import pandas as pd
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class DomainWriter(ConnectAgent):
|
|
33
|
+
|
|
34
|
+
def __init__(self, cdb, inst, agent_index):
|
|
35
|
+
super().__init__(cdb, inst, agent_index)
|
|
36
|
+
self._parse_options(self._inst)
|
|
37
|
+
|
|
38
|
+
def _parse_options(self, inst):
|
|
39
|
+
self._symbols = inst["symbols"]
|
|
40
|
+
self._drop_domain_violations = inst["dropDomainViolations"]
|
|
41
|
+
self._trace = inst["trace"]
|
|
42
|
+
self._write_all = self._symbols == "all"
|
|
43
|
+
|
|
44
|
+
def execute(self):
|
|
45
|
+
if self._trace > 0:
|
|
46
|
+
self._log_instructions(self._inst, self._inst_raw)
|
|
47
|
+
self._describe_container(self._cdb.container, "Connect Container (before):")
|
|
48
|
+
|
|
49
|
+
if self._trace > 2:
|
|
50
|
+
for name, sym in self._cdb.container.data.items():
|
|
51
|
+
self._cdb.print_log(
|
|
52
|
+
f"Connect Container symbol={name}:\n {sym.records}\n"
|
|
53
|
+
)
|
|
54
|
+
if self._write_all:
|
|
55
|
+
# For symbols with None records, empty df is assigned
|
|
56
|
+
for _, sym in self._cdb.container:
|
|
57
|
+
self._transform_sym_none_to_empty(sym)
|
|
58
|
+
|
|
59
|
+
# Apply dropDomainViolations to all symbols
|
|
60
|
+
if self._drop_domain_violations is not False:
|
|
61
|
+
self._cdb.container.dropDomainViolations()
|
|
62
|
+
|
|
63
|
+
else:
|
|
64
|
+
for sym in self._symbols:
|
|
65
|
+
sym_raw = sym.copy()
|
|
66
|
+
self._update_sym_inst(sym, self._inst)
|
|
67
|
+
|
|
68
|
+
if self._trace > 0:
|
|
69
|
+
self._log_instructions(
|
|
70
|
+
sym, sym_raw, description=f"Apply on symbol >{sym['name']}<:"
|
|
71
|
+
)
|
|
72
|
+
regex = (
|
|
73
|
+
r'(?P<name>[a-zA-Z0-9_]+)(\((?P<domains>[a-zA-Z0-9_,"\s\']*)\))?'
|
|
74
|
+
)
|
|
75
|
+
ms = re.fullmatch(regex, sym["name"])
|
|
76
|
+
|
|
77
|
+
if ms is None:
|
|
78
|
+
self._connect_error(
|
|
79
|
+
f'Invalid symbol name: >{sym["name"]}< Hint: Only alphanumeric characters and underscores are allowed.'
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
sname = ms.group("name")
|
|
83
|
+
|
|
84
|
+
self._symbols_exist_cdb(sname, should_exist=True)
|
|
85
|
+
ssym = self._cdb.container[sname]
|
|
86
|
+
|
|
87
|
+
if (
|
|
88
|
+
ms.group("domains") is not None
|
|
89
|
+
): # domains have been specified, e.g. "a(d1, d2)", "a(d1, )", or "a()"
|
|
90
|
+
sdom = [dom.strip() for dom in ms.group("domains").split(",")]
|
|
91
|
+
else: # no domains have been specified, e.g. "a"
|
|
92
|
+
sdom = []
|
|
93
|
+
|
|
94
|
+
# For symbols with None records, empty df is assigned
|
|
95
|
+
self._transform_sym_none_to_empty(ssym)
|
|
96
|
+
|
|
97
|
+
ddv = sym["dropDomainViolations"]
|
|
98
|
+
if ddv in ["before", True]:
|
|
99
|
+
ssym.dropDomainViolations()
|
|
100
|
+
if len(sdom) != len(ssym.domain):
|
|
101
|
+
self._connect_error(
|
|
102
|
+
f"Number of specified dimensions of symbol >{sym['name']}< does not correspond to the symbol's number of dimensions in the database ({len(sdom)}<>{ssym.dimension})."
|
|
103
|
+
)
|
|
104
|
+
elif len(sdom) > 0:
|
|
105
|
+
new_domain = []
|
|
106
|
+
for d in sdom:
|
|
107
|
+
if d.startswith(('"', "'")):
|
|
108
|
+
if (
|
|
109
|
+
" " in d
|
|
110
|
+
): # Is there a space in the relaxed domain? e.g. "a(' d 1')"
|
|
111
|
+
self._connect_error(
|
|
112
|
+
f"Domain sets cannot contain spaces >{d}<"
|
|
113
|
+
)
|
|
114
|
+
new_domain.append(d[1:-1])
|
|
115
|
+
|
|
116
|
+
elif d == "": # Empty domain
|
|
117
|
+
new_domain.append("")
|
|
118
|
+
|
|
119
|
+
else:
|
|
120
|
+
if d not in self._cdb.container:
|
|
121
|
+
self._connect_error(
|
|
122
|
+
f"Domain set '{d}' does not exist in the Connect database."
|
|
123
|
+
)
|
|
124
|
+
dsym = self._cdb.container[d]
|
|
125
|
+
assert (
|
|
126
|
+
type(dsym) in [gt.Set, gt.Alias] and dsym.dimension == 1
|
|
127
|
+
)
|
|
128
|
+
new_domain.append(dsym)
|
|
129
|
+
if self._trace > 1:
|
|
130
|
+
self._cdb.print_log(f"New domain for {sname}: {new_domain}\n")
|
|
131
|
+
ssym.domain = new_domain
|
|
132
|
+
ssym.domain_labels = ssym.domain_names
|
|
133
|
+
if ddv in ["after", True]:
|
|
134
|
+
ssym.dropDomainViolations()
|
|
135
|
+
|
|
136
|
+
if self._trace > 2:
|
|
137
|
+
for name, sym in self._cdb.container.data.items():
|
|
138
|
+
self._cdb.print_log(
|
|
139
|
+
f"Connect Container symbol={name}:\n {sym.records}\n"
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
if self._trace > 0:
|
|
143
|
+
self._describe_container(self._cdb.container, "Connect Container (after):")
|