// converts NN data to serializable object public SerializableWeights(WeightsInfo weights) { weights1 = new List <List <double> >(); weights2 = new List <List <double> >(); for (int i = 0; i < weights.weights1.GetLength(0); i++) { var weightsList = new List <double>(); for (int j = 0; j < weights.weights1.GetLength(1); j++) { weightsList.Add(weights.weights1[i, j]); } weights1.Add(weightsList); } for (int i = 0; i < weights.weights2.GetLength(0); i++) { var weightsList = new List <double>(); for (int j = 0; j < weights.weights2.GetLength(1); j++) { weightsList.Add(weights.weights2[i, j]); } weights2.Add(weightsList); } }
private static void EventClosed(object sender, EventArgs e) { //Environment.Exit(0); mainWindow.Close(); gameObjects = new List <GameObject>(); rand = new Random(); playerAlive = true; playerPoints = 0; playerBird = null; isPlayerPlaying = false; numberOfAliveBirds = 0; deadBirds = new List <Bird>(); highScorePoints = 0; isPretrained = false; bestNetworkWeightsAlltime = null; }
// converts back into NN data public WeightsInfo ToWeightsInfo() { WeightsInfo weights = new WeightsInfo(); weights.weights1 = new double[NeuralNetwork.inputSize, NeuralNetwork.hiddenSize]; weights.weights2 = new double[NeuralNetwork.hiddenSize, NeuralNetwork.outputSize]; int i = 0; int j = 0; foreach (var w1 in weights1) { j = 0; foreach (double weight in w1) { weights.weights1[i, j] = weight; j++; } i++; } i = 0; foreach (var w2 in weights2) { j = 0; foreach (double weight in w2) { weights.weights2[i, j] = weight; j++; } i++; } return(weights); }
public void SetGenes(WeightsInfo weights) { Network.Weights = new WeightsInfo(weights.weights1, weights.weights2); }
private static void NewGeneration() { gameObjects.Clear(); // update scores, highscore and keep track of the best performing bird from all previous generations double maxScore = 0; double scoreSum = 0; foreach (var bird in deadBirds) { if (bird.Points > maxScore) { maxScore = bird.Points; bestNetworkWeightsAlltime = new WeightsInfo(bird.brain.Network.Weights.weights1, bird.brain.Network.Weights.weights2); } scoreSum += bird.Points; } // add birds with their pick probability depending on their score List <KeyValuePair <Bird, double> > elements = new List <KeyValuePair <Bird, double> >(); foreach (Bird bird in deadBirds) { elements.Add(new KeyValuePair <Bird, double>(bird, bird.Points / scoreSum)); } // pick new birds, higher scoring birds have greater chances of being picked Bird pickedBird = null; for (int i = 0; i < WorldRules.BirdsPerGeneration - 1; i++) { Bird bird = new Bird(); double diceRoll = rand.NextDouble(); double cumulative = 0.0; for (int j = 0; j < elements.Count; j++) { cumulative += elements[j].Value; if (diceRoll < cumulative) { pickedBird = elements[j].Key; break; } } pickedBird.brain.Network.Mutate(); bird.brain.SetGenes(pickedBird.brain.GetGenes()); bird.brain = pickedBird.brain; // in case all birds do very poorly, reset every bird to a new network if (maxScore < 200) { bird = new Bird(); } gameObjects.Add(bird); playerBird = bird; } // elitism, the best bird from all generations is always picked Bird bestBirdAlltime = new Bird(); bestBirdAlltime.brain.SetGenes(bestNetworkWeightsAlltime); gameObjects.Add(bestBirdAlltime); numberOfAliveBirds = WorldRules.BirdsPerGeneration; // we reset the whole run, this includes new pipes var pipe = new Pipe(); pipe.Position += new Vector2f(0, 0); gameObjects.Add(pipe); pipe = new Pipe(); pipe.Position += new Vector2f(333, 0); gameObjects.Add(pipe); pipe = new Pipe(); pipe.Position += new Vector2f(666, 0); gameObjects.Add(pipe); playerAlive = true; deadBirds.Clear(); }