|
| 1 | +#!/usr/bin/python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +sys.path.insert(0, '/home/peter/code/projects/MultiNEAT') # duh |
| 5 | + |
| 6 | +import time |
| 7 | +import random as rnd |
| 8 | +import subprocess as comm |
| 9 | +import cv2 |
| 10 | +import numpy as np |
| 11 | +import pickle as pickle |
| 12 | +import MultiNEAT as NEAT |
| 13 | +from MultiNEAT import GetGenomeList, ZipFitness |
| 14 | +from MultiNEAT import EvaluateGenomeList_Serial |
| 15 | + |
| 16 | +from concurrent.futures import ProcessPoolExecutor, as_completed |
| 17 | + |
| 18 | +params = NEAT.Parameters() |
| 19 | +params.PopulationSize = 200 |
| 20 | + |
| 21 | +params.DynamicCompatibility = True |
| 22 | +params.CompatTreshold = 2.0 |
| 23 | +params.YoungAgeTreshold = 15 |
| 24 | +params.SpeciesMaxStagnation = 100 |
| 25 | +params.OldAgeTreshold = 35 |
| 26 | +params.MinSpecies = 5 |
| 27 | +params.MaxSpecies = 10 |
| 28 | +params.RouletteWheelSelection = False |
| 29 | + |
| 30 | +params.MutateRemLinkProb = 0.02 |
| 31 | +params.RecurrentProb = 0 |
| 32 | +params.OverallMutationRate = 0.15 |
| 33 | +params.MutateAddLinkProb = 0.08 |
| 34 | +params.MutateAddNeuronProb = 0.01 |
| 35 | +params.MutateWeightsProb = 0.90 |
| 36 | +params.MaxWeight = 8.0 |
| 37 | +params.WeightMutationMaxPower = 0.2 |
| 38 | +params.WeightReplacementMaxPower = 1.0 |
| 39 | + |
| 40 | +params.MutateActivationAProb = 0.0 |
| 41 | +params.ActivationAMutationMaxPower = 0.5 |
| 42 | +params.MinActivationA = 0.05 |
| 43 | +params.MaxActivationA = 6.0 |
| 44 | + |
| 45 | +params.MutateNeuronActivationTypeProb = 0.03 |
| 46 | + |
| 47 | +params.ActivationFunction_SignedSigmoid_Prob = 0.0 |
| 48 | +params.ActivationFunction_UnsignedSigmoid_Prob = 0.0 |
| 49 | +params.ActivationFunction_Tanh_Prob = 1.0 |
| 50 | +params.ActivationFunction_TanhCubic_Prob = 0.0 |
| 51 | +params.ActivationFunction_SignedStep_Prob = 1.0 |
| 52 | +params.ActivationFunction_UnsignedStep_Prob = 0.0 |
| 53 | +params.ActivationFunction_SignedGauss_Prob = 1.0 |
| 54 | +params.ActivationFunction_UnsignedGauss_Prob = 0.0 |
| 55 | +params.ActivationFunction_Abs_Prob = 0.0 |
| 56 | +params.ActivationFunction_SignedSine_Prob = 1.0 |
| 57 | +params.ActivationFunction_UnsignedSine_Prob = 0.0 |
| 58 | +params.ActivationFunction_Linear_Prob = 1.0 |
| 59 | + |
| 60 | +params.MutateNeuronTraitsProb = 0 |
| 61 | +params.MutateLinkTraitsProb = 0 |
| 62 | + |
| 63 | +params.DivisionThreshold = 0.5 |
| 64 | +params.VarianceThreshold = 0.03 |
| 65 | +params.BandThreshold = 0.3 |
| 66 | +params.InitialDepth = 2 |
| 67 | +params.MaxDepth = 3 |
| 68 | +params.IterationLevel = 1 |
| 69 | +params.Leo = False |
| 70 | +params.GeometrySeed = False |
| 71 | +params.LeoSeed = False |
| 72 | +params.LeoThreshold = 0.3 |
| 73 | +params.CPPN_Bias = -1.0 |
| 74 | +params.Qtree_X = 0.0 |
| 75 | +params.Qtree_Y = 0.0 |
| 76 | +params.Width = 1. |
| 77 | +params.Height = 1. |
| 78 | +params.Elitism = 0.1 |
| 79 | + |
| 80 | +rng = NEAT.RNG() |
| 81 | +rng.TimeSeed() |
| 82 | + |
| 83 | +substrate = NEAT.Substrate([(-1., -1., 0.0), (1., -1., 0.0), (0., -1., 0.0)], |
| 84 | + [], |
| 85 | + [(0., 1., 0.0)]) |
| 86 | + |
| 87 | +substrate.m_allow_input_hidden_links = False |
| 88 | +substrate.m_allow_input_output_links = False |
| 89 | +substrate.m_allow_hidden_hidden_links = False |
| 90 | +substrate.m_allow_hidden_output_links = False |
| 91 | +substrate.m_allow_output_hidden_links = False |
| 92 | +substrate.m_allow_output_output_links = False |
| 93 | +substrate.m_allow_looped_hidden_links = False |
| 94 | +substrate.m_allow_looped_output_links = False |
| 95 | + |
| 96 | +substrate.m_allow_input_hidden_links = True |
| 97 | +substrate.m_allow_input_output_links = False |
| 98 | +substrate.m_allow_hidden_output_links = True |
| 99 | +substrate.m_allow_hidden_hidden_links = False |
| 100 | + |
| 101 | +substrate.m_hidden_nodes_activation = NEAT.ActivationFunction.SIGNED_SIGMOID |
| 102 | +substrate.m_output_nodes_activation = NEAT.ActivationFunction.UNSIGNED_SIGMOID |
| 103 | + |
| 104 | +substrate.m_with_distance = False |
| 105 | + |
| 106 | +substrate.m_max_weight_and_bias = 8.0 |
| 107 | + |
| 108 | + |
| 109 | +def evaluate_xor(genome): |
| 110 | + net = NEAT.NeuralNetwork() |
| 111 | + |
| 112 | + try: |
| 113 | + |
| 114 | + genome.BuildESHyperNEATPhenotype(net, substrate, params) |
| 115 | + error = 0 |
| 116 | + depth = 3 |
| 117 | + correct = 0.0 |
| 118 | + |
| 119 | + net.Flush() |
| 120 | + |
| 121 | + net.Input([1, 0, 1]) |
| 122 | + [net.Activate() for _ in range(depth)] |
| 123 | + o = net.Output() |
| 124 | + error += abs(o[0] - 1) |
| 125 | + if o[0] > 0.75: |
| 126 | + correct += 1. |
| 127 | + |
| 128 | + net.Flush() |
| 129 | + net.Input([0, 1, 1]) |
| 130 | + [net.Activate() for _ in range(depth)] |
| 131 | + o = net.Output() |
| 132 | + error += abs(o[0] - 1) |
| 133 | + if o[0] > 0.75: |
| 134 | + correct += 1. |
| 135 | + |
| 136 | + net.Flush() |
| 137 | + net.Input([1, 1, 1]) |
| 138 | + [net.Activate() for _ in range(depth)] |
| 139 | + o = net.Output() |
| 140 | + error += abs(o[0] - 0) |
| 141 | + if o[0] < 0.25: |
| 142 | + correct += 1. |
| 143 | + |
| 144 | + net.Flush() |
| 145 | + net.Input([0, 0, 1]) |
| 146 | + [net.Activate() for _ in range(depth)] |
| 147 | + o = net.Output() |
| 148 | + error += abs(o[0] - 0) |
| 149 | + if o[0] < 0.25: |
| 150 | + correct += 1. |
| 151 | + |
| 152 | + return (4 - error) ** 2 |
| 153 | + |
| 154 | + except Exception as ex: |
| 155 | + print('Exception:', ex) |
| 156 | + return 0.0 |
| 157 | + |
| 158 | + |
| 159 | +def getbest(run): |
| 160 | + g = NEAT.Genome(0, |
| 161 | + substrate.GetMinCPPNInputs(), |
| 162 | + 0, |
| 163 | + substrate.GetMinCPPNOutputs(), |
| 164 | + False, |
| 165 | + NEAT.ActivationFunction.TANH, |
| 166 | + NEAT.ActivationFunction.TANH, |
| 167 | + 0, |
| 168 | + params, 0) |
| 169 | + |
| 170 | + pop = NEAT.Population(g, params, True, 1.0, run) |
| 171 | + for generation in range(1000): |
| 172 | + # Evaluate genomes |
| 173 | + genome_list = NEAT.GetGenomeList(pop) |
| 174 | + |
| 175 | + fitnesses = EvaluateGenomeList_Serial(genome_list, evaluate_xor, display=False) |
| 176 | + [genome.SetFitness(fitness) for genome, fitness in zip(genome_list, fitnesses)] |
| 177 | + |
| 178 | + print('Gen: %d Best: %3.5f' % (generation, max(fitnesses))) |
| 179 | + |
| 180 | + # Print best fitness |
| 181 | + # print("---------------------------") |
| 182 | + # print("Generation: ", generation) |
| 183 | + # print("max ", max([x.GetLeader().GetFitness() for x in pop.Species])) |
| 184 | + |
| 185 | + |
| 186 | + # Visualize best network's Genome |
| 187 | + |
| 188 | + net = NEAT.NeuralNetwork() |
| 189 | + pop.Species[0].GetLeader().BuildPhenotype(net) |
| 190 | + img = np.zeros((500, 500, 3), dtype=np.uint8) |
| 191 | + img += 10 |
| 192 | + NEAT.DrawPhenotype(img, (0, 0, 500, 500), net) |
| 193 | + cv2.imshow("CPPN", img) |
| 194 | + # Visualize best network's Pheotype |
| 195 | + net = NEAT.NeuralNetwork() |
| 196 | + pop.Species[0].GetLeader().BuildESHyperNEATPhenotype(net, substrate, params) |
| 197 | + img = np.zeros((500, 500, 3), dtype=np.uint8) |
| 198 | + img += 10 |
| 199 | + |
| 200 | + NEAT.DrawPhenotype(img, (0, 0, 500, 500), net, substrate=True) |
| 201 | + cv2.imshow("NN", img) |
| 202 | + cv2.waitKey(1) |
| 203 | + |
| 204 | + if max(fitnesses) > 15.0: |
| 205 | + break |
| 206 | + |
| 207 | + # Epoch |
| 208 | + generations = generation |
| 209 | + pop.Epoch() |
| 210 | + |
| 211 | + return generations |
| 212 | + |
| 213 | + |
| 214 | +gens = [] |
| 215 | +for run in range(100): |
| 216 | + gen = getbest(run) |
| 217 | + gens += [gen] |
| 218 | + print('Run:', run, 'Generations to solve XOR:', gen) |
| 219 | +avg_gens = sum(gens) / len(gens) |
| 220 | + |
| 221 | +print('All:', gens) |
| 222 | +print('Average:', avg_gens) |
0 commit comments