def _choose_comp(self, comps):
weights = list((comp, self.pheromones[comp]**self.alpha * heuristic**self.beta) for comp, heuristic in comps)
# Pseudo-random proportional update (ACS)
if random.random() < self.q0:
comp, weight = max(weights, key=itemgetter(1))
return comp
else:
weights = list(itertools.accumulate(weights, lambda acc, t: (t[0], acc[1] + t[1])))
_, total_weight = weights[-1]
threshold = random.random() * total_weight
return next(comp for comp, w in weights if w >= threshold)
评论列表
文章目录