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