summaryrefslogtreecommitdiffstats
path: root/runtime/solvers
diff options
context:
space:
mode:
authorvrvarma <vikas.varma@att.com>2020-03-03 22:22:28 -0500
committervrvarma <vikas.varma@att.com>2020-03-04 20:55:57 -0500
commitde5fdaafad9fccba0b9a7f308d72f26816dd1a0f (patch)
treee8e683e21002c20f3d743375d742bf0819d82409 /runtime/solvers
parent7d4f37c45d50c56dfe438c04dbecea3ca9f7c9d2 (diff)
Adding the generic solver code
Add docker file for optim engine Run pods as a non-root user Fix docker tag script Change-Id: If25fe66b839a70e83e35292031a2da012e81fe47 Signed-off-by: vrvarma <vikas.varma@att.com> Issue-ID: OPTFRA-712
Diffstat (limited to 'runtime/solvers')
-rw-r--r--runtime/solvers/__init__.py17
-rw-r--r--runtime/solvers/mzn/__init__.py17
-rw-r--r--runtime/solvers/mzn/mzn_solver.py102
-rw-r--r--runtime/solvers/py/__init__.py17
-rw-r--r--runtime/solvers/py/py_solver.py92
5 files changed, 245 insertions, 0 deletions
diff --git a/runtime/solvers/__init__.py b/runtime/solvers/__init__.py
new file mode 100644
index 0000000..2aa67d8
--- /dev/null
+++ b/runtime/solvers/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+#
diff --git a/runtime/solvers/mzn/__init__.py b/runtime/solvers/mzn/__init__.py
new file mode 100644
index 0000000..2aa67d8
--- /dev/null
+++ b/runtime/solvers/mzn/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+#
diff --git a/runtime/solvers/mzn/mzn_solver.py b/runtime/solvers/mzn/mzn_solver.py
new file mode 100644
index 0000000..cf002e7
--- /dev/null
+++ b/runtime/solvers/mzn/mzn_solver.py
@@ -0,0 +1,102 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+#
+
+import json
+from datetime import datetime
+
+from pymzn import Status, minizinc, cbc, gecode, chuffed, or_tools
+
+from osdf.utils.file_utils import delete_file_folder
+
+error_status_map = {
+ Status.INCOMPLETE: "incomplete",
+ Status.COMPLETE: "complete",
+ Status.UNSATISFIABLE: "unsatisfiable",
+ Status.UNKNOWN: "unknown",
+ Status.UNBOUNDED: "unbounded",
+ Status.UNSATorUNBOUNDED: "unsat_or_unbounded",
+ Status.ERROR: "error"
+}
+
+solver_dict = {
+ 'cbc': cbc,
+ 'geocode': gecode,
+ 'chuffed': chuffed,
+ 'cp': chuffed,
+ 'or_tools': or_tools
+}
+
+
+def map_status(status):
+ return error_status_map.get(status, "failed")
+
+
+def solve(request_json, mzn_content):
+ req_info = request_json['requestInfo']
+ opt_info = request_json['optimInfo']
+ try:
+ mzn_solution = mzn_solver(mzn_content, opt_info)
+
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': 'done',
+ 'statusMessage': map_status(mzn_solution.status),
+ 'solutions': mzn_solution[0] if mzn_solution else {}
+ }
+ return 200, json.dumps(response)
+ except Exception as e:
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': 'failed',
+ 'statusMessage': 'Failed due to {}'.format(e)
+ }
+ return 400, json.dumps(response)
+
+
+def mzn_solver(mzn_content, opt_info):
+ args = opt_info['solverArgs']
+ solver = get_mzn_solver(args.pop('solver'))
+ mzn_opts = dict()
+
+ try:
+ file_name = persist_opt_data(opt_info)
+ mzn_opts.update(args)
+ return minizinc(mzn_content, file_name, **mzn_opts, solver=solver)
+
+ finally:
+ delete_file_folder(file_name)
+
+
+def persist_opt_data(opt_info):
+
+ if opt_info['optData'].get('json'):
+ data_content = json.dumps(opt_info['optData']['json'])
+ file_name = '/tmp/optim_engine_{}.json'.format(datetime.timestamp(datetime.now()))
+ elif opt_info['optData'].get('text'):
+ data_content = opt_info['optData']['text']
+ file_name = '/tmp/optim_engine_{}.dzn'.format(datetime.timestamp(datetime.now()))
+
+ with open(file_name, "wt") as data:
+ data.write(data_content)
+ return file_name
+
+
+def get_mzn_solver(solver):
+ return solver_dict.get(solver)
diff --git a/runtime/solvers/py/__init__.py b/runtime/solvers/py/__init__.py
new file mode 100644
index 0000000..a8aa582
--- /dev/null
+++ b/runtime/solvers/py/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+# \ No newline at end of file
diff --git a/runtime/solvers/py/py_solver.py b/runtime/solvers/py/py_solver.py
new file mode 100644
index 0000000..6b200ab
--- /dev/null
+++ b/runtime/solvers/py/py_solver.py
@@ -0,0 +1,92 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# -------------------------------------------------------------------------
+#
+
+import json
+import subprocess
+import traceback
+from datetime import datetime
+
+from osdf.logging.osdf_logging import error_log, debug_log
+from osdf.utils.file_utils import delete_file_folder
+
+
+def py_solver(py_content, opt_info):
+ py_file = '/tmp/custom_heuristics_{}.py'.format(datetime.timestamp(datetime.now()))
+ with open(py_file, "wt") as f:
+ f.write(py_content)
+ if opt_info['optData'].get('json'):
+ data_content = json.dumps(opt_info['optData']['json'])
+ input_file = '/tmp/optim_engine_{}.json'.format(datetime.timestamp(datetime.now()))
+ elif opt_info['optData'].get('text'):
+ data_content = opt_info['optData']['text']
+ input_file = '/tmp/optim_engine_{}.txt'.format(datetime.timestamp(datetime.now()))
+ with open(input_file, "wt") as f:
+ f.write(data_content)
+
+ output_file = '/tmp/opteng_output_{}.json'.format(datetime.timestamp(datetime.now()))
+
+ command = ['python', py_file, input_file, output_file]
+
+ try:
+ p = subprocess.run(command, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
+
+ debug_log.debug('Process return code {}'.format(p.returncode))
+ if p.returncode > 0:
+ error_log.error('Process return code {} {}'.format(p.returncode, p.stdout))
+ return 'error', {}
+ with open(output_file) as file:
+ data = file.read()
+ return 'success', json.loads(data)
+
+ except Exception as e:
+ error_log.error("Error running optimizer {}".format(traceback.format_exc()))
+ return 'error', {}
+ finally:
+ cleanup((input_file, output_file, py_file))
+
+
+def cleanup(file_tup):
+ for f in file_tup:
+ try:
+ delete_file_folder(f)
+ except Exception as e:
+ error_log.error("Failed deleting the file {} - {}".format(f, traceback.format_exc()))
+
+
+def solve(request_json, py_content):
+ req_info = request_json['requestInfo']
+ opt_info = request_json['optimInfo']
+ try:
+ status, solution = py_solver(py_content, opt_info)
+
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': status,
+ 'statusMessage': "completed",
+ 'solutions': solution if solution else {}
+ }
+ return 200, json.dumps(response)
+ except Exception as e:
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': 'failed',
+ 'statusMessage': 'Failed due to {}'.format(e)
+ }
+ return 400, json.dumps(response)