def crossover(self, other): """Returns a tuple of the results of the crossover (copies)""" # allow crossover to change the whole thing. # start_self, end_self = randsegment(self.swaps) # start_other, end_other = randsegment(other.swaps) length_self = int(min(abs(np.random.normal(0, len(self.swaps) / 10.0)), len(self.swaps))) start_self = randrange(len(self.swaps) - length_self) end_self = start_self + length_self length_other = int(min(abs(np.random.normal(0, len(other.swaps) / 10.0)), len(other.swaps))) start_other = randrange(len(other.swaps) - length_other) end_other = start_other + length_other try: self_a, self_b, self_c = self.swaps[:start_self], self.swaps[start_self:end_self], self.swaps[end_self:] other_a, other_b, other_c = ( other.swaps[:start_other], other.swaps[start_other:end_other], other.swaps[end_other:], ) except: print start_self, end_self print start_other, end_other raise # new_self = Candidate(list(unique(self_a + other_b + self_c))) # new_other = Candidate(list(unique(other_a + self_b + other_c))) new_self = Candidate(self_a + other_b + self_c) new_other = Candidate(other_a + self_b + other_c) return (new_self, new_other)
def __init__(self, swaps=None): self.swaps = swaps or [Candidate.swap() for i in range(randrange(40) + 10)]
def __init__(self, initial_values=None, length=16): if initial_values != None: self.test_values = initial_values else: self.test_values = [randrange(90) + 10 for x in range(length)]
def mutate(self, severity=1): for i in range(severity): index = randindex(self.test_values) self.test_values[index] = randrange(90) + 10
def swap(cls, length=16): return (randrange(length), randrange(length))