示例#1
0
    def breed(self, generation):
        splicer = GeneSplicer()

        children = []
        for count in range(0, int(self.expected_children)):
            if count == 0:
                mum = copy.deepcopy(self.member_organisms[count])
            else:
                mum = copy.deepcopy(self.member_organisms[random.randint(0, len(self.member_organisms) - 1)])

            if random.random() < MATE_PROBABILITY:
                dad = copy.deepcopy(self.member_organisms[random.randint(0, len(self.member_organisms) - 1)])
                junior = splicer.mate(mum, dad)
                junior["reproduction"] = "Mate"
            else:
                junior = mum
                splicer.mutate(junior)
                junior["reproduction"] = "Mutate"

            junior["parent"] = mum["id"]
            junior["id"] = ControlPanel.next_organism_number()
            junior["generation"] = generation
            junior["species"] = self.id
            children.append(junior)

        mutants = []
        for child in children:
            if splicer.compatibility_scan(self.representative, child) < COMPATIBILITY_THRESHOLD:
                self.member_organisms.append(child)
            else:
                mutants.append(child)

        return mutants
示例#2
0
 def new_neuron(self):
     next_innovation_number = ControlPanel.next_innovation_number()
     return {
         "innovation_number": next_innovation_number,
         "label": "H%s" % next_innovation_number,
         "type": HIDDEN
     }
示例#3
0
 def __init__(self, representative):
     self.id = ControlPanel.next_species_id()
     self.representative = representative
     self.member_organisms = [representative]
     self.last_improvement = 0
     self.max_fitness = 0
     self.age = 0
     self.expected_children = 0
示例#4
0
 def new_synapse(self, axon, dendrite, weight):
     next_innovation_number = ControlPanel.next_innovation_number()
     return {
         "innovation_number": next_innovation_number,
         "axon": axon,
         "dendrite": dendrite,
         "weight": weight
     }
示例#5
0
 def __init__(self, representative):
     self.id = ControlPanel.next_species_id()
     self.representative = representative
     self.member_organisms = [representative]
     self.last_improvement = 0
     self.max_fitness = 0
     self.age = 0
     self.expected_children = 0
示例#6
0
 def new_synapse(self, axon, dendrite, weight):
     next_innovation_number = ControlPanel.next_innovation_number()
     return {
         "innovation_number": next_innovation_number,
         "axon": axon,
         "dendrite": dendrite,
         "weight": weight
     }
示例#7
0
    def mate(self, mum, dad):
        mum_neurons = mum["neurons"]
        dad_neurons = dad["neurons"]
        mum_synapses = mum["synapses"]
        dad_synapses = dad["synapses"]
        better_fitness = mum if mum["fitness"] > dad["fitness"] else dad
        junior_synapses = []
        junior_neurons = {mum_neuron["label"]: mum_neuron for mum_neuron in mum_neurons if
                          mum_neuron["label"][0] != "H"}
        g1 = 0
        g2 = 0
        while g1 < len(mum_synapses) and g2 < len(dad_synapses):
            if g1 >= len(dad_synapses):
                if better_fitness is mum:
                    junior_synapses.append(mum_synapses[g1])
                    self.mum_neurons(g1, junior_neurons, mum_neurons, mum_synapses[g1])
                g1 += 1
            elif g2 >= len(dad_synapses):
                if better_fitness is not mum:
                    junior_synapses.append(dad_synapses[g2])
                    self.add_dad_neurons(g2, dad_neurons, junior_neurons, dad_synapses[g2])
                g2 += 1
            else:
                innov1 = mum_synapses[g1]["innovation_number"]
                innov2 = dad_synapses[g2]["innovation_number"]
                if innov1 == innov2:
                    if rand_pos_neg() == 1:
                        junior_synapses.append(mum_synapses[g1])
                        self.mum_neurons(g1, junior_neurons, mum_neurons, mum_synapses[g1])

                    else:
                        junior_synapses.append(dad_synapses[g2])
                        self.add_dad_neurons(g2, dad_neurons, junior_neurons, dad_synapses[g2])

                    g1 += 1
                    g2 += 1
                elif innov1 < innov2:
                    if better_fitness is mum:
                        junior_synapses.append(mum_synapses[g1])
                        self.mum_neurons(g1, junior_neurons, mum_neurons, mum_synapses[g1])
                    g1 += 1
                else:
                    if better_fitness is not mum:
                        junior_synapses.append(dad_synapses[g1])
                        self.add_dad_neurons(g2, dad_neurons, junior_neurons, dad_synapses[g2])
                    g2 += 1

        junior = {"id": ControlPanel.next_organism_number(), "fitness": mum["fitness"], "neurons": [], "synapses": []}

        connections = set()
        for i, synapse in enumerate(junior_synapses):
            connection = synapse["axon"] + synapse["dendrite"]
            if connection not in connections:
                connections.add(connection)
                junior["synapses"].append(synapse)
        junior["neurons"].extend(junior_neurons.values())

        return junior
示例#8
0
    def breed(self, generation):
        splicer = GeneSplicer()

        children = []
        for count in range(0, int(self.expected_children)):
            if count == 0:
                mum = copy.deepcopy(self.member_organisms[count])
            else:
                mum = copy.deepcopy(self.member_organisms[random.randint(
                    0,
                    len(self.member_organisms) - 1)])

            if random.random() < MATE_PROBABILITY:
                dad = copy.deepcopy(self.member_organisms[random.randint(
                    0,
                    len(self.member_organisms) - 1)])
                junior = splicer.mate(mum, dad)
                junior["reproduction"] = "Mate"
            else:
                junior = mum
                splicer.mutate(junior)
                junior["reproduction"] = "Mutate"

            junior["parent"] = mum["id"]
            junior["id"] = ControlPanel.next_organism_number()
            junior["generation"] = generation
            junior["species"] = self.id
            children.append(junior)

        mutants = []
        for child in children:
            if splicer.compatibility_scan(self.representative,
                                          child) < COMPATIBILITY_THRESHOLD:
                self.member_organisms.append(child)
            else:
                mutants.append(child)

        return mutants
示例#9
0
    def mate(self, mum, dad):
        mum_neurons = mum["neurons"]
        dad_neurons = dad["neurons"]
        mum_synapses = mum["synapses"]
        dad_synapses = dad["synapses"]
        better_fitness = mum if mum["fitness"] > dad["fitness"] else dad
        junior_synapses = []
        junior_neurons = {
            mum_neuron["label"]: mum_neuron
            for mum_neuron in mum_neurons if mum_neuron["label"][0] != "H"
        }
        g1 = 0
        g2 = 0
        while g1 < len(mum_synapses) and g2 < len(dad_synapses):
            if g1 >= len(dad_synapses):
                if better_fitness is mum:
                    junior_synapses.append(mum_synapses[g1])
                    self.mum_neurons(g1, junior_neurons, mum_neurons,
                                     mum_synapses[g1])
                g1 += 1
            elif g2 >= len(dad_synapses):
                if better_fitness is not mum:
                    junior_synapses.append(dad_synapses[g2])
                    self.add_dad_neurons(g2, dad_neurons, junior_neurons,
                                         dad_synapses[g2])
                g2 += 1
            else:
                innov1 = mum_synapses[g1]["innovation_number"]
                innov2 = dad_synapses[g2]["innovation_number"]
                if innov1 == innov2:
                    if rand_pos_neg() == 1:
                        junior_synapses.append(mum_synapses[g1])
                        self.mum_neurons(g1, junior_neurons, mum_neurons,
                                         mum_synapses[g1])

                    else:
                        junior_synapses.append(dad_synapses[g2])
                        self.add_dad_neurons(g2, dad_neurons, junior_neurons,
                                             dad_synapses[g2])

                    g1 += 1
                    g2 += 1
                elif innov1 < innov2:
                    if better_fitness is mum:
                        junior_synapses.append(mum_synapses[g1])
                        self.mum_neurons(g1, junior_neurons, mum_neurons,
                                         mum_synapses[g1])
                    g1 += 1
                else:
                    if better_fitness is not mum:
                        junior_synapses.append(dad_synapses[g1])
                        self.add_dad_neurons(g2, dad_neurons, junior_neurons,
                                             dad_synapses[g2])
                    g2 += 1

        junior = {
            "id": ControlPanel.next_organism_number(),
            "fitness": mum["fitness"],
            "neurons": [],
            "synapses": []
        }

        connections = set()
        for i, synapse in enumerate(junior_synapses):
            connection = synapse["axon"] + synapse["dendrite"]
            if connection not in connections:
                connections.add(connection)
                junior["synapses"].append(synapse)
        junior["neurons"].extend(junior_neurons.values())

        return junior
示例#10
0
 def new_neuron(self):
     next_innovation_number = ControlPanel.next_innovation_number()
     return {"innovation_number": next_innovation_number,
             "label": "H%s" % next_innovation_number,
             "type": HIDDEN
             }