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 RemoveFromPop(NEATNetwork toRemove)
 {
     foreach (Species s in species)
     {
         if (s.Contains(toRemove.genome))
         {
             int sSize = s.RemoveMember(toRemove.genome);
             if (sSize <= 0)
             {
                 species.RemoveAt(species.IndexOf(s));
                 break;
             }
         }
     }
     networks.Remove(toRemove);
     genomes.Remove(toRemove.genome);
     currentPopulation.Remove(toRemove);
 }
 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 void NextGeneration(List <NEATGenome> newGenomes)
 {
     if (newGenomes.Count != popSize)
     {
         throw new System.Exception("Invalid population initializer!");
     }
     currentPopulation.Clear();
     species.Clear();
     genomes.Clear();
     networks.Clear();
     //NEATGenome[] a = new NEATGenome[newGenomes.Count];
     //newGenomes.CopyTo(a);
     //genomes = a.ToList();
     //newGenomes.Clear();
     for (int i = 0; i < newGenomes.Count; i++)
     {
         genomes.Add(newGenomes[i]);
         NEATNetwork net = new NEATNetwork(newGenomes[i]);
         networks.Add(net);
         currentPopulation.Add(net, newGenomes[i]);
     }
     newGenomes.Clear();
 }
        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);
        }