Skip to content

Instantly share code, notes, and snippets.

@rish-16
Last active July 21, 2018 12:16
Show Gist options
  • Save rish-16/193afd54c5ab315c9782a60e0e9378bd to your computer and use it in GitHub Desktop.
Save rish-16/193afd54c5ab315c9782a60e0e9378bd to your computer and use it in GitHub Desktop.
# Crossing over the Primary Agents
def crossover_pagent(agents):
offspring = []
for _ in range(int((self.population - len(agents)) / 2)):
parent1 = random.choice(agents)
parent2 = random.choice(agents)
child1 = PAgent(self.in_str_len)
child2 = PAgent(self.in_str_len)
split = random.randint(0, self.in_str_len)
child1.string = parent1.string[0:split] + parent2.string[split:self.in_str_len]
child2.string = parent2.string[0:split] + parent1.string[split:self.in_str_len]
offspring.append(child1)
offspring.append(child2)
agents.extend(offspring)
return agents
# Crossing over the Secondary Agents
def crossover_sagent(agents):
offspring = []
for _ in range(int((agent_population - len(agents)) / 2)):
parent1 = random.choice(agents)
parent2 = random.choice(agents)
child1 = SAgent(in_str)
child2 = SAgent(in_str)
child1.population = int((0.2 * parent1.population) + (0.4 * parent2.population))
child1.population = int((0.2 * parent2.population) + (0.4 * parent1.population))
child1.generations = int((0.2 * parent1.population) + (0.4 * parent2.population))
child2.generations = int((0.2 * parent2.population) + (0.4 * parent1.population))
offspring.append(child1)
offspring.append(child2)
agents.extend(offspring)
return agents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment