@@ -29,7 +29,15 @@ def __goal_count_heuristic(self, state):
2929
3030class DeleteRelaxationHeuristic :
3131 def __init__ (self , automated_planner , heuristic_key ):
32+ class DRHCache :
33+ def __init__ (self , domain = None , axioms = None , preconds = None , additions = None ):
34+ self .domain = domain
35+ self .axioms = axioms
36+ self .preconds = preconds
37+ self .additions = additions
38+
3239 self .automated_planner = automated_planner
40+ self .cache = DRHCache ()
3341 self .heuristic_keys = {
3442 "delete_relaxation/h_add" : self .__h_add ,
3543 "delete_relaxation/h_max" : self .__h_max
@@ -39,22 +47,56 @@ def __init__(self, automated_planner, heuristic_key):
3947 heuristic_key = "delete_relaxation/h_add"
4048
4149 self .current_h = heuristic_key
50+ self .has_been_precomputed = False
51+ self .__pre_compute ()
52+ # return self.heuristic_keys[self.current_h](state)
4253
43- def compute (self , state ):
44- return self .heuristic_keys [self .current_h ](state )
54+ def compute (self , state , goals ):
55+ if not self .has_been_precomputed :
56+ self .__pre_compute ()
57+ domain = self .cache .domain
58+ goals = self .automated_planner .goals
59+ types = state .types
60+ facts = state .facts
61+
62+ fact_costs = dict ([(f , 0 ) for f in facts ])
63+ while not (len (fact_costs ) == len (facts ) and self .__facts_eq (fact_costs , facts )):
64+ facts_set = self .automated_planner .pddl .Set (self .automated_planner .pddl .keys (fact_costs ))
65+ state = self .automated_planner .pddl .create_state (types , facts_set )
66+ if self .automated_planner .satisfies (goals , state ):
67+ costs = [fact_costs [g ] for g in goals ]
68+ costs .insert (0 , 0 )
69+ return self .heuristic_keys [self .current_h ](costs )
70+
71+ for ax in self .cache .axioms :
72+ pass
4573
4674 def __pre_compute (self ):
47- logging .fatal ("Delete relaxation h_add is not yet implemented" )
48- exit ()
75+ if self .has_been_precomputed :
76+ return
77+ domain = self .automated_planner .domain
78+ domain , axioms = self .automated_planner .pddl .compute_hsp_axioms (domain )
79+ preconditions = dict ()
80+ additions = dict ()
81+ for name , definition in domain .actions .items ():
82+ precond = self .automated_planner .pddl .filter_negative_preconds (definition )
83+ preconditions [name ] = precond
84+ additions [name ] = self .automated_planner .pddl .effect_diff (definition .effect ).add
85+ self .cache .additions = additions
86+ self .cache .preconds = preconditions
87+ self .cache .domain = domain
88+ self .cache .axioms = axioms
89+
90+
91+ def __h_add (self , costs ):
92+ return sum (costs )
4993
50- def __cache (self ):
51- logging .fatal ("Delete relaxation h_add is not yet implemented" )
52- exit ()
94+ def __h_max (self , costs ):
95+ return max (costs )
5396
54- def __h_add (self , state ):
55- logging .fatal ("Delete relaxation h_add is not yet implemented" )
56- exit ()
97+ def __facts_eq (self , facts_dict , facts_set ):
98+ for f in facts_set :
99+ if not (f in facts_dict .keys ()):
100+ return False
101+ return True
57102
58- def __h_max (self , state ):
59- logging .fatal ("Delete relaxation h_max is not yet implemented" )
60- exit ()
0 commit comments