-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDomain.ml
50 lines (39 loc) · 1.54 KB
/
Domain.ml
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
open DomainTypes
(* A problem is defined by a list of available actions and a goal
state *)
type problem = action list * State.t
type node = State.t
type edge = action * string Binding.t
let solved state (_, goal) =
State.subset goal state
(* This heuristic is in general inadmissible, but we
will construct our problems in a way that it isn't *)
let heuristic state (_, goal) = State.cardinal (State.diff goal state)
let compare_nodes = State.compare
let substitute binding vars =
List.map (fun v -> Binding.find v binding) vars
let instantiate_pattern binding state (Pattern(name, vars)) =
State.add (Bound(name, substitute binding vars)) state
let instantiate_action action binding =
let instantiate = instantiate_pattern binding in
let adds = List.fold_left instantiate State.empty action.adds
and dels = List.fold_left instantiate State.empty action.dels in
(adds, dels)
let apply_action state action binding =
let adds, dels = instantiate_action action binding in
State.union adds (State.diff state dels)
let create_edge state action binding =
let cost = 1
and state' = apply_action state action binding
and edge = (action, binding) in
(cost, state', edge)
let applicable state action =
let bindings =
Unifier.find_assignments state action.arity action.preconditions in
EnvSet.fold
(fun binding l -> (create_edge state action binding) :: l)
bindings []
let applicable_actions state actions =
List.flatten (List.map (applicable state) actions)
let edges state (actions, _) =
applicable_actions state actions