Loading...
Searching...
No Matches
transport_engine.py
Go to the documentation of this file.
1import os
2import sys
3import time
4import zipfile
5from gams import GamsWorkspace
6import gams.engine
7from gams.engine.api import jobs_api
8
9
10if __name__ == "__main__":
11 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
12 ws = GamsWorkspace(system_directory=sys_dir)
13
14 model = "trnsport"
15 ws.gamslib(model)
16
17 model_data_path = os.path.join(ws.working_directory, model + ".zip")
18
19 with zipfile.ZipFile(model_data_path, "w", zipfile.ZIP_DEFLATED) as model_data:
20 model_data.write(
21 os.path.join(ws.working_directory, model + ".gms"), arcname=model + ".gms"
22 )
23
24 stdout_filename = "log_stdout.txt" # str | name of the file that captures stdout (default to 'log_stdout.txt')
25 arguments = [
26 "gdx=default"
27 ] # list[str] | arguments that will be passed to GAMS call (optional)
28
29 data = None # file | file containing data in zip (optional)
30 pf_file_name = None # str | name of the pf file in the zip, if there is (optional)
31 text_entries = [] # list[str] | (optional)
32 stream_entries = [] # list[str] | (optional)
33 inex_file = None # file | optional file to filter what will be inside the result zip file (optional)
34
35 configuration = gams.engine.Configuration(
36 host=os.environ["ENGINE_URL"],
37 username=os.environ["ENGINE_USER"],
38 password=os.environ["ENGINE_PASSWORD"],
39 discard_unknown_keys=True,
40 )
41 configuration.temp_folder_path = ws.working_directory
42 namespace = os.environ[
43 "ENGINE_NAMESPACE"
44 ] # str | namespace containing(or will contain) the model
45
46 # enter a context with an instance of the API client
47 with gams.engine.ApiClient(configuration) as api_client:
48 # create an instance of the API class
49 job_api_instance = jobs_api.JobsApi(api_client)
50 try:
51 print(f"Posting {model}")
52 with open(model_data_path, "rb") as model_data:
53 create_job_response = job_api_instance.create_job(
54 model,
55 namespace,
56 stdout_filename=stdout_filename,
57 model_data=model_data,
58 arguments=arguments,
59 )
60
61 token = create_job_response.token
62 print(f"Job token: {token}")
63
64 except gams.engine.ApiException as e:
65 print(f"Exception when calling JobsApi.create_job(): {e}\n")
66 sys.exit(1)
67
68 time_spent = 0
69 while True:
70 try:
71 resp = job_api_instance.pop_job_logs(token)
72 print(resp.message, end="")
73 if resp.queue_finished:
74 break
75 time.sleep(0.5)
76 except gams.engine.ApiException as e:
77 if e.status == 403:
78 print("Job still in queue. Wait 0.5 seconds.")
79 time.sleep(0.5)
80 time_spent += 0.5
81 if time_spent > 120:
82 print(
83 "The Engine instance seems to be busy. Please try again later."
84 )
85 sys.exit(1)
86 else:
87 raise e
88
89 if job_api_instance.get_job(token).process_status != 0:
90 print("Job did not terminate successfully.")
91
92 try:
93 print(f"Fetching results of model: {model}")
94 with zipfile.ZipFile(job_api_instance.get_job_zip(token)) as zf:
95 gdx_file = zf.extract(model + ".gdx", path=ws.working_directory)
96 except gams.engine.ApiException as e:
97 print(f"Exception when calling JobsApi.get_job_zip(): {e}\n")
98 sys.exit(1)
99
100 try:
101 # remove results from server
102 job_api_instance.delete_job_zip(token)
103 except gams.engine.ApiException as e:
104 print(f"Exception when calling JobsApi.delete_job_zip(): {e}\n")
105 sys.exit(1)
106
107 result_db = ws.add_database_from_gdx(
108 os.path.join(ws.working_directory, gdx_file)
109 )
110
111 for rec in result_db["x"]:
112 print(
113 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
114 )