-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinc.py
96 lines (80 loc) · 2.68 KB
/
inc.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Multi-Shot control loop for shunting.
Taken largely from
https://github.com/potassco/clingo/blob/master/examples/clingo/multishot/inc.py
"""
import sys
import click
from clingo.application import clingo_main, Application, ApplicationOptions
from clingo.solving import SolveResult
from clingo.symbol import Function, Number
from clorm.clingo import Control
from clorm import FactBase
from model import (
Track,
Car,
Initbefore,
Initlast,
Before,
Last,
Predecessor,
Shunt
)
from visualize import Visualize, VisualizeText, VisualizeBasic
class IncApp(Application):
"""ASP Application implementing Multi-Shot control loop."""
def register_options(self, options: ApplicationOptions):
""" Register program options."""
# no options implemented
pass
@staticmethod
def _on_model(model):
vis = VisualizeText()
vis.visualize(model)
def main(self, ctl_: Control, files):
"""The main function implementing incremental solving."""
if not files or len(files) == 0:
print("Usage: python inc.py <theory1.lp> [<theoryN.lp> ...]")
exit(1)
ctl = Control(
unifier=[Track, Car, Initbefore, Before, Last, Predecessor, Shunt],
control_=ctl_
)
for file in files:
ctl.load(file)
# Add the external atom "query" that selectively turns on the
# solution check of each step t.
# ctl.add("check", ["t"], "#external query(t).")
init_facts = [
Car(idx=1),
Car(idx=2),
Car(idx=3),
Car(idx=4),
Car(idx=5),
Track(idx=1),
Track(idx=2),
Track(idx=3),
Initbefore(first_car=5, second_car=4, track=1),
Initbefore(first_car=4, second_car=3, track=1),
Initbefore(first_car=3, second_car=2, track=1),
Initbefore(first_car=2, second_car=1, track=1),
Initlast(car=1, track=1)
]
instance = FactBase(init_facts)
ctl.add_facts(instance)
step = 0
ret = None
while True:
parts = []
parts.append(("check", [Number(step)]))
if step > 0:
ctl.release_external(Function("query", [Number(step - 1)]))
parts.append(("step", [Number(step)]))
else:
parts.append(("base", []))
ctl.ground(parts)
ctl.assign_external(Function("query", [Number(step)]), True)
ret = ctl.solve(on_model=IncApp._on_model)
step += 1
if ret.satisfiable:
break
clingo_main(IncApp(), sys.argv[1:])