Beispiel #1
0
 public Neuron(int inputNr)
 {
     for (int i = 0; i <= inputNr; i++)
     {
         weights.Add(GeneticManager.getRandomWeight());
     }
 }
Beispiel #2
0
        public void calculateOutput()
        {
            double sum = 0;
            int    num = 0;

            foreach (double input in inputs)
            {
                sum += input * weights[num];
                num++;
            }
            sum   += weights[num];
            output = GeneticManager.signoid(sum);
        }
        public void startNewGeneration(List <List <double> > weights)
        {
            Console.WriteLine("Starting new gen process {0}", DateTime.Now);
            genCount = 0;
            Random rand = new Random();

            genNum++;
            //Console.WriteLine("Starting generation wth number {0}", genNum);
            parent.Text = "Generation - " + genNum;
            for (int i = 0; i < mapSize; i++)
            {
                for (int j = 0; j < mapSize; j++)
                {
                    map[i, j] = 0;
                }
            }
            if (weights != null)
            {
                snakes.Clear();
                for (int i = 1; i <= weights.Count; i++)
                {
                    int y, x;
                    y = rand.Next(5, mapSize - 5);
                    x = rand.Next(5, mapSize - 5);
                    Snake snake = new Snake(this, spawnSnakeAt(y, x, 3), 2);
                    snake.getBrain().setWeights(weights[i - 1]);
                    snakes.Add(snake);
                }
                generateFood();
                return;
            }
            if (snakes.Count != 0)
            {
                List <List <double> > oldBrains = new List <List <double> >();
                snakes.Sort(delegate(Snake a, Snake b)
                {
                    if (a.getBody().Count == b.getBody().Count)
                    {
                        if (a.getTravel() == b.getTravel())
                        {
                            if (a.timeAlive == b.timeAlive)
                            {
                                return(0);
                            }
                            else if (a.timeAlive < b.timeAlive)
                            {
                                return(1);
                            }
                            else
                            {
                                return(-1);
                            }
                        }
                        else if (a.getTravel() < b.getTravel())
                        {
                            return(1);
                        }
                        else
                        {
                            return(-1);
                        }
                    }
                    else if (a.getBody().Count < b.getBody().Count)
                    {
                        return(1);
                    }
                    else
                    {
                        return(-1);
                    }
                });
                foreach (Snake snake in snakes)
                {
                    oldBrains.Add(snake.getBrain().getWeights());
                }
                snakes.Clear();
                Console.WriteLine("Starting mating process {0}", DateTime.Now);
                List <List <double> > newBrains = GeneticManager.getNewGeneration(oldBrains);
                Console.WriteLine("Finished mating process {0}", DateTime.Now);
                foreach (List <double> brain in newBrains)
                {
                    int i, j;
                    i = rand.Next(5, mapSize - 5);
                    j = rand.Next(5, mapSize - 5);
                    //Console.WriteLine("Spawning snake at {0} - {1}", i, j);
                    Snake snake = new Snake(this, spawnSnakeAt(i, j, 3), 2);
                    snake.getBrain().setWeights(brain);
                    snakes.Add(snake);
                }
            }
            else
            {
                snakes.Clear();
                for (int i = 1; i <= GeneticManager.generationSize; i++)
                {
                    int y, x;
                    y = rand.Next(5, mapSize - 5);
                    x = rand.Next(5, mapSize - 5);
                    //Console.WriteLine("Spawning snake at {0} - {1}", y, x);
                    snakes.Add(new Snake(this, spawnSnakeAt(y, x, 3), 2));
                }
            }
            generateFood();
            Console.WriteLine("Finished new gen process {0}", DateTime.Now);
        }