Petri网定向展开的hmax启发式函数实现正确性求证
Petri网定向展开hmax启发式函数验证请求
我基于一篇论文实现了用于Petri网定向展开的hmax启发式函数,该函数用于估算从一个标识(参数a)到另一个标识(参数b)的剩余距离。目前无法自行复现验证,需要协助确认实现的正确性与假设合理性。
相较于论文定义,我做了如下修改:
- 因采用基于代价的定向展开,引入了
cost_function - 为避免递归计算时出现循环,用
in_stack跟踪调用路径,检测到循环时返回inf - 新增
if len(possible_candidates) == 0: return 0的逻辑,但不确定该假设是否正确,也不清楚possible_candidates为何会为空
实现代码如下:
def compute_hmax( self, mark: frozenset[PetriNet.Place], target: Set[PetriNet.Place], cost_function, ): if len(mark) == 0: self.local_configuration.hmax = 0 else: in_stack = ( dict() ) # stack to detect visited nodes in the search to detect cycles d = self.hmax(mark, frozenset(target), in_stack, cost_function) self.local_configuration.hmax = d @cached( cache={}, key=lambda self, a, b, in_stack, cost_function: hashkey((a, b)), ) def hmax( self, a: frozenset[PetriNet.Place], b: frozenset[PetriNet.Place], in_stack, cost_function, ): if b.issubset(a): return 0 elif len(b) == 1: q = list(b)[0] if q in in_stack: return float("inf") # cycle detected in_stack[q] = True pre_trans = set(map(lambda arc: arc.source, q.in_arcs)) possible_candidates = list( map( lambda t: ( t, self.hmax( a, frozenset(t.preset), in_stack, cost_function ), ), pre_trans, ) ) in_stack.pop(q) if len(possible_candidates) == 0: return 0 min_d_tup = min(possible_candidates, key=lambda t: t[1] + cost_function[t[0]]) return cost_function[min_d_tup[0]] + min_d_tup[1] else: return max( list( map( lambda t: self.hmax( a, frozenset({t}), in_stack, cost_function ), b, ) ) )
内容的提问来源于stack exchange,提问作者Ariba Siddiqui
相关产品推荐
相关产品推荐

