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,402 @@
|
|
|
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 gams.control import *
|
|
27
|
+
from gams import transfer as gt
|
|
28
|
+
import pandas as pd
|
|
29
|
+
import os
|
|
30
|
+
import shutil
|
|
31
|
+
import glob
|
|
32
|
+
import uuid
|
|
33
|
+
|
|
34
|
+
class GamsInteractive(object):
|
|
35
|
+
|
|
36
|
+
def __init__(self):
|
|
37
|
+
self._system_directory = None
|
|
38
|
+
self._envs = {}
|
|
39
|
+
self._need_reset = True
|
|
40
|
+
self._fprefix = 'gj_' + str(uuid.uuid4())[:8]
|
|
41
|
+
self._current_env = None
|
|
42
|
+
|
|
43
|
+
def from2dim(self, df, column_names=None):
|
|
44
|
+
if column_names is None:
|
|
45
|
+
return pd.DataFrame(df.stack()).reset_index()
|
|
46
|
+
else:
|
|
47
|
+
df = pd.DataFrame(df.stack()).reset_index()
|
|
48
|
+
return df.rename(columns=dict(zip(df.columns, column_names)))
|
|
49
|
+
|
|
50
|
+
def pivot(self, df, index=None, columns=None, values=None):
|
|
51
|
+
df = df.pivot_table(index=index, columns=columns, values=values, fill_value=0)
|
|
52
|
+
if type(index) == str:
|
|
53
|
+
df.index.names = [None]
|
|
54
|
+
else:
|
|
55
|
+
df.index.names = [None] * len(index)
|
|
56
|
+
if type(columns) == str:
|
|
57
|
+
df.columns.names = [None]
|
|
58
|
+
else:
|
|
59
|
+
df.columns.names = [None] * len(columns)
|
|
60
|
+
return df
|
|
61
|
+
|
|
62
|
+
def pivot2d(self, df):
|
|
63
|
+
return self.pivot(df, index=df.columns[0], columns=df.columns[1], values=df.columns[2])
|
|
64
|
+
|
|
65
|
+
def _get_latest_file(self, pattern):
|
|
66
|
+
return max(glob.glob(pattern), key=os.path.getmtime)
|
|
67
|
+
|
|
68
|
+
def _set_write_all(self, value):
|
|
69
|
+
if self._need_reset:
|
|
70
|
+
self.reset()
|
|
71
|
+
self._current_env._write_all = value
|
|
72
|
+
|
|
73
|
+
def _get_write_all(self):
|
|
74
|
+
if self._need_reset:
|
|
75
|
+
self.reset()
|
|
76
|
+
return self._current_env._write_all
|
|
77
|
+
|
|
78
|
+
write_all = property(_get_write_all, _set_write_all)
|
|
79
|
+
|
|
80
|
+
def _set_read_all(self, value):
|
|
81
|
+
if self._need_reset:
|
|
82
|
+
self.reset()
|
|
83
|
+
self._current_env._read_all = value
|
|
84
|
+
|
|
85
|
+
def _get_read_all(self):
|
|
86
|
+
if self._need_reset:
|
|
87
|
+
self.reset()
|
|
88
|
+
return self._current_env._read_all
|
|
89
|
+
|
|
90
|
+
read_all = property(_get_read_all, _set_read_all)
|
|
91
|
+
|
|
92
|
+
def _get_exchange_container(self):
|
|
93
|
+
if self._need_reset:
|
|
94
|
+
self.reset()
|
|
95
|
+
return self._current_env._m
|
|
96
|
+
|
|
97
|
+
exchange_container = property(_get_exchange_container)
|
|
98
|
+
|
|
99
|
+
def _get_active_name(self):
|
|
100
|
+
if self._need_reset:
|
|
101
|
+
self.reset()
|
|
102
|
+
return self._current_env._name
|
|
103
|
+
|
|
104
|
+
active = property(_get_active_name)
|
|
105
|
+
|
|
106
|
+
def create(self, name, activate=True):
|
|
107
|
+
self._envs[name] = _GamsEnvironment(self._fprefix, name, self._system_directory)
|
|
108
|
+
if activate:
|
|
109
|
+
self.activate(name)
|
|
110
|
+
|
|
111
|
+
def activate(self, name):
|
|
112
|
+
self._current_env = self._envs[name]
|
|
113
|
+
|
|
114
|
+
def _get_job_prefix(self):
|
|
115
|
+
return self._fprefix + '_' + self._current_env._name + '_'
|
|
116
|
+
|
|
117
|
+
def _new_job_name(self):
|
|
118
|
+
self._current_env._jobNumber = self._current_env._jobNumber + 1
|
|
119
|
+
return self._get_job_prefix() + str(self._current_env._jobNumber)
|
|
120
|
+
|
|
121
|
+
def reset(self, system_directory=None):
|
|
122
|
+
if system_directory is None: # keep system directory if system_directory was not provided
|
|
123
|
+
pass
|
|
124
|
+
elif system_directory == '': # reset the system directory to be found automatically if the empty string is provided
|
|
125
|
+
self._system_directory = None
|
|
126
|
+
else: # a system directory was provided
|
|
127
|
+
self._system_directory = system_directory
|
|
128
|
+
|
|
129
|
+
if self._current_env is None:
|
|
130
|
+
self.create('base')
|
|
131
|
+
else:
|
|
132
|
+
self.create(self._current_env._name)
|
|
133
|
+
self._need_reset = False
|
|
134
|
+
|
|
135
|
+
def gams_reset(self, system_directory=None):
|
|
136
|
+
self.reset(system_directory)
|
|
137
|
+
|
|
138
|
+
def gams_log(self):
|
|
139
|
+
if self._need_reset:
|
|
140
|
+
self.reset()
|
|
141
|
+
latest_log_file = self._get_latest_file(self._get_job_prefix() + '*.log')
|
|
142
|
+
with open(latest_log_file) as f:
|
|
143
|
+
print(f.read())
|
|
144
|
+
|
|
145
|
+
def gams_lst(self, execution):
|
|
146
|
+
if self._need_reset:
|
|
147
|
+
self.reset()
|
|
148
|
+
latest_lst_file = self._get_latest_file(self._get_job_prefix() + '*.lst')
|
|
149
|
+
if execution:
|
|
150
|
+
with open(latest_lst_file) as f:
|
|
151
|
+
content = f.readlines()
|
|
152
|
+
in_execution = False
|
|
153
|
+
for l in content:
|
|
154
|
+
if l.startswith("E x e c u t i o n"):
|
|
155
|
+
in_execution = True
|
|
156
|
+
elif l.startswith("EXECUTION TIME"):
|
|
157
|
+
in_execution = False
|
|
158
|
+
if in_execution:
|
|
159
|
+
print(l, end="")
|
|
160
|
+
else:
|
|
161
|
+
with open(latest_lst_file) as f:
|
|
162
|
+
print(f.read())
|
|
163
|
+
|
|
164
|
+
def gams_cleanup(self, closedown=False, all=False, keep=False):
|
|
165
|
+
if closedown:
|
|
166
|
+
for k, v in self._envs.items():
|
|
167
|
+
v.cleanup(keep=False)
|
|
168
|
+
self._current_env = None
|
|
169
|
+
self._envs = {}
|
|
170
|
+
self._need_reset = True
|
|
171
|
+
elif all:
|
|
172
|
+
for k, v in self._envs.items():
|
|
173
|
+
v.cleanup(keep=keep)
|
|
174
|
+
else:
|
|
175
|
+
self._current_env.cleanup(keep=keep)
|
|
176
|
+
if not closedown and not keep:
|
|
177
|
+
if all:
|
|
178
|
+
for k, v in self._envs.items():
|
|
179
|
+
self.create(k)
|
|
180
|
+
else:
|
|
181
|
+
self.create(self._current_env._name)
|
|
182
|
+
|
|
183
|
+
def _parse_trace_file(self, trc_file):
|
|
184
|
+
model_stat = ["", "Optimal Global", "OptimalLocal", "Unbounded", "InfeasibleGlobal", "InfeasibleLocal",
|
|
185
|
+
"InfeasibleIntermed", "Feasible", "Integer", "NonIntegerIntermed", "IntegerInfeasible",
|
|
186
|
+
"LicenseError", "ErrorUnknown", "ErrorNoSolution", "NoSolutionReturned", "SolvedUnique",
|
|
187
|
+
"Solved", "SolvedSingular", "UnboundedNoSolution", "InfeasibleNoSolution"]
|
|
188
|
+
solve_stat = ["", "Normal", "Iteration", "Resource", "Solver", "EvalError", "Capability", "License", "User",
|
|
189
|
+
"SetupErr", "SolverErr", "InternalErr", "Skipped", "SystemErr"]
|
|
190
|
+
|
|
191
|
+
with open(trc_file, 'r') as f:
|
|
192
|
+
lines = f.readlines()
|
|
193
|
+
if len(lines) < 6: # no solve in GAMS code
|
|
194
|
+
return None
|
|
195
|
+
header = (lines[2] + lines[3]).replace('*', '').replace('\n', '').replace(' ', '').split(',')
|
|
196
|
+
used_columns = ["ModelType", "SolverName", "NumberOfEquations", "NumberOfVariables", "ModelStatus",
|
|
197
|
+
"SolverStatus", "ObjectiveValue", "SolverTime"]
|
|
198
|
+
alt_col_names = ["Model Type", "Solver", "#equ", "#var", "Model Status", "Solver Status", "Objective",
|
|
199
|
+
"Solver Time"]
|
|
200
|
+
data = []
|
|
201
|
+
for l in lines[5:]:
|
|
202
|
+
values = l.split(',')
|
|
203
|
+
display_values = []
|
|
204
|
+
for idx in range(len(header)):
|
|
205
|
+
if header[idx] in used_columns:
|
|
206
|
+
if header[idx] == "ModelStatus":
|
|
207
|
+
display_values.append(model_stat[int(values[idx])] + " (" + values[idx] + ")")
|
|
208
|
+
elif header[idx] == "SolverStatus":
|
|
209
|
+
display_values.append(solve_stat[int(values[idx])] + " (" + values[idx] + ")")
|
|
210
|
+
elif header[idx] == "ObjectiveValue":
|
|
211
|
+
if values[idx] != 'NA':
|
|
212
|
+
display_values.append(round(float(values[idx]), 4))
|
|
213
|
+
else:
|
|
214
|
+
display_values.append('NA')
|
|
215
|
+
else:
|
|
216
|
+
display_values.append(values[idx])
|
|
217
|
+
|
|
218
|
+
data.append(display_values)
|
|
219
|
+
|
|
220
|
+
df = pd.DataFrame(data, columns=alt_col_names)
|
|
221
|
+
df = df[["Solver Status", "Model Status", "Objective", "#equ", "#var", "Model Type", "Solver", "Solver Time"]]
|
|
222
|
+
return df
|
|
223
|
+
|
|
224
|
+
def _get_description(self, sym):
|
|
225
|
+
d = sym.description
|
|
226
|
+
if "'" in d:
|
|
227
|
+
return '"' + d + '"'
|
|
228
|
+
else:
|
|
229
|
+
return "'" + d + "'"
|
|
230
|
+
|
|
231
|
+
def _get_dom_str(self, sym):
|
|
232
|
+
if sym.dimension == 0:
|
|
233
|
+
return ""
|
|
234
|
+
else:
|
|
235
|
+
dom = []
|
|
236
|
+
for d in sym.domain:
|
|
237
|
+
if type(d) == str:
|
|
238
|
+
dom.append('*')
|
|
239
|
+
else: # Set or Alias
|
|
240
|
+
dom.append(d.name)
|
|
241
|
+
return '(' + ','.join(dom) + ')'
|
|
242
|
+
|
|
243
|
+
def _get_dom(self, m, sym):
|
|
244
|
+
dom = []
|
|
245
|
+
for d in sym.domain:
|
|
246
|
+
if type(d) == str:
|
|
247
|
+
dom.append(d)
|
|
248
|
+
else: # Set or Alias
|
|
249
|
+
dom.append(m.data[d.name])
|
|
250
|
+
return dom
|
|
251
|
+
|
|
252
|
+
def gams(self, code):
|
|
253
|
+
if self._need_reset:
|
|
254
|
+
self.reset()
|
|
255
|
+
solve_summary = None
|
|
256
|
+
if code:
|
|
257
|
+
job_name = self._new_job_name()
|
|
258
|
+
user_code_start_line = 2
|
|
259
|
+
tmpcode = '$gdxIn ' + job_name + '_gdxin.gdx' + '\n$onMultiR'
|
|
260
|
+
sym_list = []
|
|
261
|
+
for name, sym in self._current_env._m.data.items():
|
|
262
|
+
if not sym.modified and not self.write_all:
|
|
263
|
+
continue
|
|
264
|
+
if type(sym) == gt.Set and sym.is_singleton:
|
|
265
|
+
tmpcode += '\n$if not declared ' + name + ' singleton set ' + name + self._get_dom_str(sym) + ' ' + self._get_description(sym) + ';'
|
|
266
|
+
elif type(sym) == gt.Set:
|
|
267
|
+
tmpcode += '\n$if not declared ' + name + ' set ' + name + self._get_dom_str(sym) + ' ' + self._get_description(sym) + ';'
|
|
268
|
+
elif type(sym) == gt.Parameter and sym.dimension == 0:
|
|
269
|
+
tmpcode += '\n$if not declared ' + name + ' scalar ' + name + ' ' + self._get_description(sym) + ';'
|
|
270
|
+
elif type(sym) == gt.Parameter:
|
|
271
|
+
tmpcode += '\n$if not declared ' + name + ' parameter ' + name + self._get_dom_str(sym) + ' ' + self._get_description(sym) + ';'
|
|
272
|
+
elif type(sym) == gt.Variable:
|
|
273
|
+
tmpcode += '\n$if not declared ' + name + ' ' + sym.type + ' variable ' + name + self._get_dom_str(sym) + ' ' + self._get_description(sym) + ';'
|
|
274
|
+
elif type(sym) == gt.Equation:
|
|
275
|
+
tmpcode += '\n$if not declared ' + name + ' equation ' + name + self._get_dom_str(sym) + ' ' + self._get_description(sym) + ';'
|
|
276
|
+
elif type(sym) == gt.Alias:
|
|
277
|
+
tmpcode += '\n$if not declared ' + name + ' alias (' + name + ',' + sym.alias_with.name + ');'
|
|
278
|
+
else:
|
|
279
|
+
raise Exception('unknown symbol type ' + type(sym))
|
|
280
|
+
user_code_start_line += 1
|
|
281
|
+
if type(sym) != gt.Alias:
|
|
282
|
+
tmpcode += '\n$load ' + name
|
|
283
|
+
user_code_start_line += 1
|
|
284
|
+
sym_list.append(name)
|
|
285
|
+
self._current_env._m.write(job_name + '_gdxin.gdx', sym_list)
|
|
286
|
+
|
|
287
|
+
code = tmpcode + "\n$gdxIn\n$offeolcom\n$eolcom #\n" + code
|
|
288
|
+
user_code_start_line += 3
|
|
289
|
+
self._current_env.job = self._current_env._ws.add_job_from_string(code, checkpoint=self._current_env._cp, job_name=job_name)
|
|
290
|
+
opt = self._current_env._ws.add_options()
|
|
291
|
+
trc_file_name = job_name + ".trc"
|
|
292
|
+
trc_file_path = os.path.join(self._current_env._ws.working_directory, trc_file_name)
|
|
293
|
+
if os.path.isfile(trc_file_path):
|
|
294
|
+
os.remove(trc_file_path)
|
|
295
|
+
opt.trace = trc_file_name
|
|
296
|
+
opt.traceopt = 3
|
|
297
|
+
opt.gdx = job_name + '_gdxout.gdx'
|
|
298
|
+
if not self.read_all:
|
|
299
|
+
opt.reference = job_name + '.ref'
|
|
300
|
+
with open(job_name + ".log", "w") as logFile:
|
|
301
|
+
self._current_env.job.run(opt, checkpoint=self._current_env._cpnew, output=logFile, create_out_db=False)
|
|
302
|
+
if os.path.exists(self._current_env._ws._working_directory + os.sep + self._current_env._cp._checkpoint_file_name):
|
|
303
|
+
try:
|
|
304
|
+
os.remove(self._current_env._ws._working_directory + os.sep + self._current_env._cp._checkpoint_file_name)
|
|
305
|
+
except:
|
|
306
|
+
pass
|
|
307
|
+
shutil.move(self._current_env._cpnew._checkpoint_file_name, self._current_env._cp._checkpoint_file_name)
|
|
308
|
+
|
|
309
|
+
readsyms = {} # cannot use set because sets are unordered and copying symbols into GTP needs good order
|
|
310
|
+
if not self.read_all: # process reference file to get symbols written to by user code
|
|
311
|
+
with open(job_name + '.ref', 'r') as f:
|
|
312
|
+
for l in f.readlines():
|
|
313
|
+
items = l.split()
|
|
314
|
+
if items[0] == '0': # end of reference file code section
|
|
315
|
+
break
|
|
316
|
+
if int(items[5]) < user_code_start_line: # our import of data, not user code
|
|
317
|
+
continue
|
|
318
|
+
if items[4] in ['declared', 'defined', 'impl-asn', 'assigned']:
|
|
319
|
+
readsyms[items[2]] = None
|
|
320
|
+
if len(readsyms) == 0:
|
|
321
|
+
return None
|
|
322
|
+
|
|
323
|
+
m_out = gt.Container(job_name + '_gdxout.gdx', system_directory=self._system_directory)
|
|
324
|
+
if self.read_all:
|
|
325
|
+
readsyms = m_out.data
|
|
326
|
+
|
|
327
|
+
m = self._current_env._m
|
|
328
|
+
for name in readsyms.keys():
|
|
329
|
+
try:
|
|
330
|
+
sym = m_out.data[name]
|
|
331
|
+
except:
|
|
332
|
+
continue # the reference file might have model, file, acronym symbols that do not exist in GDX, so we better skip these
|
|
333
|
+
gtsym = m.data.get(name, None)
|
|
334
|
+
if gtsym:
|
|
335
|
+
if type(sym.records) == pd.DataFrame:
|
|
336
|
+
gtsym.setRecords(sym.records)
|
|
337
|
+
if not gtsym.isValid():
|
|
338
|
+
print(f'*** Validation on symbol {gtsym.name} failed')
|
|
339
|
+
else:
|
|
340
|
+
if type(sym) == gt.Set:
|
|
341
|
+
gtsym = m.addSet(name, self._get_dom(m, sym), sym.is_singleton, None, False, sym.description)
|
|
342
|
+
elif type(sym) == gt.Parameter:
|
|
343
|
+
gtsym = m.addParameter(name, self._get_dom(m, sym), None, False, sym.description)
|
|
344
|
+
elif type(sym) == gt.Variable:
|
|
345
|
+
gtsym = m.addVariable(name, sym.type, self._get_dom(m, sym), None, False, sym.description)
|
|
346
|
+
elif type(sym) == gt.Equation:
|
|
347
|
+
gtsym = m.addEquation(name, sym.type, self._get_dom(m, sym), None, False, sym.description)
|
|
348
|
+
elif type(sym) == gt.Alias:
|
|
349
|
+
gtsym = m.addAlias(name, m.data[sym.alias_with.name])
|
|
350
|
+
else:
|
|
351
|
+
raise Exception('unknown symbol type ' + type(sym))
|
|
352
|
+
if type(sym) != gt.Alias and type(sym.records) == pd.DataFrame:
|
|
353
|
+
gtsym.setRecords(sym.records)
|
|
354
|
+
if not gtsym.isValid():
|
|
355
|
+
print(f'*** Validation on symbol {gtsym.name} failed')
|
|
356
|
+
solve_summary = self._parse_trace_file(trc_file_path)
|
|
357
|
+
self._current_env._m.modified = False
|
|
358
|
+
|
|
359
|
+
return solve_summary
|
|
360
|
+
|
|
361
|
+
class _GamsEnvironment:
|
|
362
|
+
def __init__(self, prefix, name, system_directory=None):
|
|
363
|
+
self._name = name
|
|
364
|
+
self._m = gt.Container(system_directory=system_directory)
|
|
365
|
+
self._jobNumber = 1
|
|
366
|
+
self._job = None
|
|
367
|
+
self._cp = None
|
|
368
|
+
self._cpnew = None
|
|
369
|
+
self._ws = None
|
|
370
|
+
self._prefix = prefix
|
|
371
|
+
self._write_all = False
|
|
372
|
+
self._read_all = False
|
|
373
|
+
|
|
374
|
+
self._ws = GamsWorkspace(".", system_directory)
|
|
375
|
+
job_name = prefix + '_' + name
|
|
376
|
+
self._job = self._ws.add_job_from_string("$onMultiR\n", job_name=job_name + '_1_reset')
|
|
377
|
+
self._cp = self._ws.add_checkpoint(checkpoint_name=job_name + "_cp")
|
|
378
|
+
self._cpnew = self._ws.add_checkpoint(checkpoint_name=job_name + "_cpnew")
|
|
379
|
+
with open(job_name + "_1.log", "w") as logFile:
|
|
380
|
+
self._job.run(checkpoint=self._cp, output=logFile, create_out_db=False)
|
|
381
|
+
|
|
382
|
+
def cleanup(self, keep=False):
|
|
383
|
+
to_be_deleted = []
|
|
384
|
+
for ext in ['g00', 'gdx', 'lst', 'log', 'gms', 'pf', 'trc', 'ref']:
|
|
385
|
+
to_be_deleted += glob.glob(self._prefix + '_' + self._name + '_*.' + ext)
|
|
386
|
+
if not keep:
|
|
387
|
+
del self._job
|
|
388
|
+
del self._cp
|
|
389
|
+
del self._cpnew
|
|
390
|
+
del self._ws
|
|
391
|
+
else:
|
|
392
|
+
keep_files = []
|
|
393
|
+
for ext in ['g00', 'gdx', 'lst', 'log']:
|
|
394
|
+
keep_files.append(self._get_latest_file(self._prefix + '_' + self._name + '_*.' + ext))
|
|
395
|
+
to_be_deleted = set(to_be_deleted) - set(keep_files)
|
|
396
|
+
|
|
397
|
+
for f in to_be_deleted:
|
|
398
|
+
try:
|
|
399
|
+
os.remove(f)
|
|
400
|
+
except Exception as e:
|
|
401
|
+
print("Warning:", e)
|
|
402
|
+
|
gams/tools/__init__.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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 gams.tools.tools import Tools
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"Tools"
|
|
30
|
+
]
|
gams/tools/errors.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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 sys
|
|
27
|
+
|
|
28
|
+
class GamsToolsException(Exception):
|
|
29
|
+
def __init__(self, message, error_code, traceback=False):
|
|
30
|
+
super().__init__(message)
|
|
31
|
+
self.error_code = error_code
|
|
32
|
+
self.traceback = traceback
|
|
33
|
+
if not self.traceback:
|
|
34
|
+
sys.tracebacklimit = 0
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
#
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
#
|
|
@@ -0,0 +1,51 @@
|
|
|
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 gams.tools.toolcollection.tooltemplate import ToolTemplate
|
|
27
|
+
|
|
28
|
+
class Rank (ToolTemplate):
|
|
29
|
+
|
|
30
|
+
def __init__(self, system_directory, tool):
|
|
31
|
+
super().__init__(system_directory, tool)
|
|
32
|
+
self.title = 'rank: This sorts one dimensional symbol sym and stores sorted indices in one dimensional parameter symIdx.'
|
|
33
|
+
self.add_posargdef('sym', 'id.in:any:1', 'Name of parameter or set to be sorted sym(*)')
|
|
34
|
+
self.add_posargdef('symIdx','id.out:par:1', 'Name of parameter containing sort indexes symIdx(*)')
|
|
35
|
+
self.add_namedargdef('gdxIn=fileIn.gdx', 'fnExist', 'Name of GDX file that contains symbol sym', shell_req=True)
|
|
36
|
+
self.add_namedargdef('gdxOut=fileOut.gdx', 'fnWriteable','Name of GDX file that contains symbol symIdx after execution', shell_req=True)
|
|
37
|
+
|
|
38
|
+
def execute(self):
|
|
39
|
+
if self.dohelp():
|
|
40
|
+
return
|
|
41
|
+
import gams.transfer as gt
|
|
42
|
+
self.process_args()
|
|
43
|
+
sym, symIdx = self.posargs
|
|
44
|
+
m = gt.Container(system_directory=self._system_directory)
|
|
45
|
+
self.read_id_inputs(m, sym)
|
|
46
|
+
recs = m[sym].toList()
|
|
47
|
+
if recs is None:
|
|
48
|
+
m.addParameter(symIdx,['*'])
|
|
49
|
+
else:
|
|
50
|
+
m.addParameter(symIdx,['*'], records=[(r[1],e+1) for e,r in enumerate(sorted((r[1],r[0]) for r in recs))])
|
|
51
|
+
self.write_id_outputs(m, symIdx)
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
#
|