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