我正在研究csp的主题,并且陷入了回跳算法的困境。这本爱玛书说,只要对带有冲突集的回溯算法进行一些修改,就可以得到回跳算法。关键是我看不到如何在Java中设置冲突。
我发现了如下所述的python代码:
def backjumping(assignments, conflict_sets, csp):
if is_complete(assignments, csp):
return assignments
var = select_unassigned_variable(assignments, csp)
if var is not None:
# creating conflict set of var
if len(assignments.keys()) > 1:
last_var = next(reversed(assignments))
conflict_sets[var].append(last_var)
conflict_sets[var].append(conflict_sets[last_var])
# update conflict set after failure
else:
last_var = next(reversed(assignments))
conflict_sets[last_var].append(conflict_sets[var])
conflict_sets[last_var].remove(last_var)
var = last_var
for value in order_domain_values(var, csp):
inferences = {}
if is_consistent(var, value, csp):
assignments[var] = value
inferences = inference(var, value, assignments, csp, InferenceType.AC3)
for key, val in inferences.items():
assignments[key] = val
res = backjumping(assignments, conflict_sets, csp)
if res:
return res
assignments.pop(var, None)
for key, _ in inferences.items():
assignments.pop(key, None)
return False