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