-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplanner-api.py
More file actions
54 lines (46 loc) · 1.88 KB
/
planner-api.py
File metadata and controls
54 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from flask import Flask, request, jsonify
import subprocess
import tempfile
import os
app = Flask(__name__)
@app.route('/plan', methods=['POST'])
def plan():
domain = request.files['domain']
problem = request.files['problem']
with tempfile.TemporaryDirectory() as tmpdir:
domain_path = os.path.join(tmpdir, 'domain.pddl')
problem_path = os.path.join(tmpdir, 'problem.pddl')
domain.save(domain_path)
problem.save(problem_path)
cmd = [
'./ff', # your Metric-FF binary
'-o', domain_path,
'-f', problem_path,
'-s', '0' # standard FF search: EHC+H then BFS without cost optimization
]
try:
# Set cwd to where your ff binary is located if needed
result = subprocess.run(cmd, cwd='/home/pi/Metric-FF-v2.1',
capture_output=True, text=True, timeout=30)
output = result.stdout
print("Planner Output:\n\n", output, "\n\n")
if 'found legal plan' in output.lower() or 'found plan' in output.lower():
# Extract plan lines, usually the lines after "found legal plan"
plan = []
recording = False
for line in output.splitlines():
line = line.strip()
if 'found legal plan' in line.lower() or 'found plan' in line.lower():
recording = True
continue
if recording:
if line == '':
break
plan.append(line)
return jsonify({'plan': plan})
else:
return jsonify({'plan': None, 'error': 'No plan found'})
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
app.run(debug=True)