find accomplices, the "trouble starter" should be the last of the list
Closed this issue · 0 comments
ambitious-octopus commented
PyPROTON-OC/protonoc/simulator/entities.py
Lines 414 to 455 in c547d45
def find_accomplices(self, n_of_accomplices: int) -> List[Person]: | |
""" | |
This method is used to find accomplices during commit_crimes procedure | |
:param n_of_accomplices: int, number of accomplices | |
:return: List[Person] | |
""" | |
if n_of_accomplices == 0: | |
return [self] | |
else: | |
d = 1 # start with a network distance of 1 | |
accomplices = set() | |
facilitator_needed = n_of_accomplices >= self.model.threshold_use_facilitators and not self.facilitator | |
if facilitator_needed: | |
n_of_accomplices -= 1 # save a slot for the facilitator | |
while len(accomplices) < n_of_accomplices and d <= self.model.max_accomplice_radius: | |
# first create the group | |
candidates = sorted(self.agents_in_radius(d), key=lambda x: self.candidates_weight(x)) | |
while len(accomplices) < n_of_accomplices and len(candidates) > 0: | |
candidate = candidates[0] | |
candidates.remove(candidate) | |
accomplices.add(candidate) | |
# todo: Should be if candidate.facilitator and facilitator_needed? tracked issue #234 | |
if candidate.facilitator: | |
n_of_accomplices += 1 | |
facilitator_needed = False | |
d += 1 | |
if facilitator_needed: | |
# Search a facilitator into my networks | |
available_facilitators = [facilitator for facilitator in set(chain.from_iterable( | |
[agent.agents_in_radius(self.model.max_accomplice_radius) for agent in accomplices])) if | |
facilitator.facilitator] | |
if available_facilitators: | |
accomplices.add(self.model.random.choice(available_facilitators)) | |
if len(accomplices) < n_of_accomplices: | |
self.model.crime_size_fails += 1 | |
accomplices.add(self) | |
if n_of_accomplices >= self.model.threshold_use_facilitators: | |
if [agent for agent in accomplices if agent.facilitator]: | |
self.model.facilitator_crimes += 1 | |
else: | |
self.model.facilitator_fails += 1 | |
return list(accomplices) |
find accomplices assumes that the caller is the last one on the list. This may not be the case since we treat the set of accomplices as a python set.
Line 449 should be:
accomplices = list(accomplices)
accomplices.append(self)
or something better...