transport_neos.py
Go to the documentation of this file.
1import os
2 import sys
3 import time
4 import xmlrpc.client
5 from gams import *
6 
7 import ssl
8 import certifi
9 
10 # NEOS XML Template (to be filled)
11 xml = r'''<document>
12 <category>:category:</category>
13 <solver>:solver:</solver>
14 <inputType>GAMS</inputType>
15 <email>:email:</email>
16 <priority>short</priority>
17 <model><![CDATA[:model:]]></model>
18 <wantgdx><![CDATA[yes]]></wantgdx>
19 <wantlog><![CDATA[yes]]></wantlog>
20 <wantlst><![CDATA[yes]]></wantlst>
21 </document>'''
22 
23 if __name__ == "__main__":
24  if len(sys.argv) > 1:
25  ws = GamsWorkspace(system_directory = sys.argv[1])
26  else:
27  ws = GamsWorkspace()
28 
29  model = 'trnsport' # str | Name of the main .gms file
30  ws.gamslib(model)
31 
32  ssl_context = ssl.create_default_context()
33  # Explicitly point to 'certifi' *.pem file in case the OpenSSL default CA certificate path points to expired certificates
34  if sys.platform == "win32":
35  ssl_context.load_verify_locations(certifi.where())
36  neos = xmlrpc.client.ServerProxy('https://neos-server.org:3333', context=ssl_context)
37  alive = neos.ping()
38  if alive != "NeosServer is alive\n":
39  raise Exception('*** Could not make connection to NEOS Server')
40  # can neither choose SoPlex nor CBC as LP solver on NEOS, so pretend its a MIP and use CBC
41  xml = xml.replace(':category:', 'MILP')
42  xml = xml.replace(':solver:', 'CBC')
43  if 'NEOS_EMAIL' in os.environ:
44  xml = xml.replace(':email:', os.environ['NEOS_EMAIL'])
45  else:
46  raise Exception("Environment variable 'NEOS_EMAIL' not found")
47  with open(os.path.join(ws.working_directory, model+'.gms'), 'r') as f:
48  xml = xml.replace(':model:', f.read())
49 
50  jobNumber, password = neos.submitJob(xml)
51  print("Job number:", jobNumber)
52  print("Job password:", password)
53 
54  if jobNumber == 0:
55  raise Exception('*** NEOS Server error:' + password)
56 
57  offset = 0
58  echo = True
59  status = ''
60  while status != 'Done':
61  time.sleep(1)
62  result, offset = neos.getIntermediateResults(jobNumber, password, offset)
63  result = result.data.decode()
64  if echo:
65  if result.find('Composing results.') != -1: #this removes the lst output
66  result = result.split('Composing results.', 1)[0]
67  echo = False
68  print(result, end='')
69  status = neos.getJobStatus(jobNumber, password)
70 
71  result = neos.getFinalResults(jobNumber, password)
72  with open(str(jobNumber)+'-'+os.path.splitext(os.path.basename(model+'.gms'))[0]+'.lst', 'w') as rf:
73  rf.write(result.data.decode())
74  result = neos.getOutputFile(jobNumber, password, 'solver-output.zip')
75  with open(str(jobNumber)+'-'+'solver-output.zip', 'wb') as rf:
76  rf.write(result.data)