public NEATNetwork AddToPop(NEATGenome newGenome)
        {
            genomes.Add(newGenome);
            NEATNetwork net = new NEATNetwork(newGenome);

            networks.Add(net);
            SpeciateOne(newGenome);
            currentPopulation.Add(net, newGenome);
            return(net);
        }
        public virtual void Evaluate(bool verbose = false)
        {
            genomes.Sort();
            float bestFitness = float.MinValue;

            for (int i = 0; i < networks.Count; i++)
            {
                if (networks[i].genome.Fitness > bestFitness)
                {
                    bestFitness = networks[i].genome.Fitness;
                    lastGenBest = networks[i];
                }
            }
            species.Clear();
            species.Add(new Species(genomes[0]));
            for (int i = 1; i < genomes.Count; i++)
            {
                bool newSpecies = true;
                foreach (Species s in species)
                {
                    if (s.BelongsToSpecies(genomes[i]))
                    {
                        newSpecies = false;
                        s.AddMemeber(genomes[i]);
                        break;
                    }
                }
                if (newSpecies)
                {
                    species.Add(new Species(genomes[i]));
                }
            }
            foreach (Species s in species)
            {
                s.AdjustFitnesses();
                s.SortMembersByFitness();
            }
            species.Sort();

            List <NEATGenome> newPop = new List <NEATGenome>();

            for (int i = 0; i < popSize; i++)
            {
                Species    rSpecies  = GetRandomSpecies();
                NEATGenome newGenome = NEATGenome.Reproduce(rSpecies.GetRandomMember(), rSpecies.GetRandomMember());
                newPop.Add(newGenome);
            }
            generations++;
            NextGeneration(newPop);
        }
        public void SpeciateOne(NEATGenome genome)
        {
            bool newSpecies = true;

            foreach (Species s in species)
            {
                if (s.BelongsToSpecies(genome))
                {
                    newSpecies = false;
                    s.AddMemeber(genome);
                    break;
                }
            }
            if (newSpecies)
            {
                species.Add(new Species(genome));
            }
        }
 protected void Init(int inputSize, int outputSize, int startConnections)
 {
     for (int i = 0; i < popSize; i++)
     {
         NEATGenome newGenome = new NEATGenome(inputSize, outputSize);
         for (int j = 0; j < startConnections; j++)
         {
             newGenome.CreateNewConnection();
         }
         Connection c  = new Connection(0, 2, 1, true, 0);
         Connection c2 = new Connection(1, 2, 1, true, 1);
         newGenome.AddConnection(c);
         newGenome.AddConnection(c2);
         genomes.Add(newGenome);
         NEATNetwork net = new NEATNetwork(newGenome);
         networks.Add(net);
         currentPopulation.Add(net, newGenome);
     }
 }
        public NEATNetworkDistance(NEATGenome g) : base(_genome: g)
        {
            HashSet <int> cons = new HashSet <int>();

            for (int i = 0; i < genome.inputDim; i++)
            {
                neurons[i].Distance = 0;
                //for(int j = 0; j < neurons[i].)
            }


            foreach (Neuron n in neurons)
            {
                if (n.type == NodeType.input)
                {
                    n.Distance = 0;
                    continue;
                }
            }
        }
        public virtual void Evaluate(Vector[] x, Vector[] y)
        {
            for (int k = 0; k < x.Length; k++)
            {
                for (int i = 0; i < networks.Count; i++)
                {
                    Vector output = networks[i].RunNetwork(x[k]);
                    float  error  = Error(output.GetValue(0), y[k].GetValue(0));

                    /*if (error < bestError)
                     * {
                     *      bestError = error;
                     *      best = networks[i];
                     *
                     * }*/
                    NEATGenome genome;
                    currentPopulation.TryGetValue(networks[i], out genome);

                    if (genome != null)
                    {
                        genome.Error   += error;
                        genome.Fitness += (1f / (error + 0.001f)) * 100f;
                    }
                    else
                    {
                        throw new System.Exception("invalid network to genome");
                    }
                }
            }
            NEATNetwork best        = networks[0];
            float       BestFitness = float.MinValue;

            for (int i = 0; i < networks.Count; i++)
            {
                NEATGenome genome;
                currentPopulation.TryGetValue(networks[i], out genome);
                if (genome.Fitness > BestFitness)
                {
                    BestFitness = genome.Fitness;
                    best        = networks[i];
                }
            }


            lastGenBest   = best;
            lastGenGenome = null;
            currentPopulation.TryGetValue(best, out lastGenGenome);
            if (lastGenGenome == null)
            {
                throw new System.Exception("Can't find genome for network?");
            }
            species.Add(new Species(genomes[0]));
            for (int i = 1; i < genomes.Count; i++)
            {
                bool newSpecies = true;
                foreach (Species s in species)
                {
                    if (s.BelongsToSpecies(genomes[i]))
                    {
                        newSpecies = false;
                        s.AddMemeber(genomes[i]);
                        break;
                    }
                }
                if (newSpecies)
                {
                    species.Add(new Species(genomes[i]));
                }
            }
            foreach (Species s in species)
            {
                s.AdjustFitnesses();
                s.SortMembersByFitness();
            }
            species.Sort();
            genomes.Sort();
            List <NEATGenome> newPop = new List <NEATGenome>();

            for (int i = 0; i < 5; i++)
            {
                newPop.Add(genomes[i]);
            }
            for (int i = 0; i < popSize - 5; i++)
            {
                Species    rSpecies  = GetRandomSpecies();
                NEATGenome newGenome = NEATGenome.Reproduce(rSpecies.GetRandomMember(), rSpecies.GetRandomMember());
                newPop.Add(newGenome);
            }
            Debug.Log("Generation: " + generations + " | Error: " + lastGenGenome.Error / x.Length * 100f + "%" + " | Species: " + species.Count);
            generations++;
            NextGeneration(newPop);
        }
 public NEATNetwork(NEATGenome _genome)
 {
     genome = _genome;
     ConstructNetwork();
 }
 public bool Contains(NEATGenome genome)
 {
     return(members.Contains(genome));
 }
 public int RemoveMember(NEATGenome genome)
 {
     members.Remove(genome);
     return(members.Count);
 }
 public bool BelongsToSpecies(NEATGenome toCompare, float compTreshold = 0.3f)
 {
     return(NEATGenome.GetCompababliltyRating(mascot, toCompare) < compTreshold);
 }
 public void AddMemeber(NEATGenome genome)
 {
     members.Add(genome);
 }
 public Species(NEATGenome genome)
 {
     mascot = genome;
     members.Add(genome);
 }