/// <summary> /// Evaluates a list of genomes. Here we decode each genome in using the contained IGenomeDecoder /// and evaluate the resulting TPhenome using the contained IPhenomeEvaluator. /// </summary> public void Evaluate(IList <NeatGenome> genomeList) { Parallel.ForEach(genomeList, delegate(NeatGenome genome) { MarkovChain phenome = _genomeDecoder.Decode(genome); if (null == phenome) { genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AlternativeFitness = 0.0; } if (null == phenome) { // Non-viable genome. genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AlternativeFitness = 0.0; } else { FitnessInfo fitnessInfo = _passwordCrackingEvaluator.Evaluate(phenome); genome.EvaluationInfo.SetFitness(fitnessInfo._fitness); genome.EvaluationInfo.AlternativeFitness = fitnessInfo._alternativeFitness; } }); foreach (string p in _passwordCrackingEvaluator.FoundPasswords) { //PasswordCrackingEvaluator.Passwords.Remove(p); double val = PasswordCrackingEvaluator.Passwords[p]; PasswordCrackingEvaluator.Passwords[p] = val * 0.75; } }
/// <summary> /// Main genome evaluation loop with phenome caching (decode only if no cached phenome is present /// from a previous decode). /// </summary> private void Evaluate_Caching(IList <TGenome> genomeList) { Parallel.ForEach(genomeList, _parallelOptions, delegate(TGenome genome) { TPhenome phenome = (TPhenome)genome.CachedPhenome; if (null == phenome) { // Decode the phenome and store a ref against the genome. phenome = _genomeDecoder.Decode(genome); genome.CachedPhenome = phenome; } }); doneGeneratingGenomesBarrier.SignalAndWait(); Parallel.ForEach(genomeList, _parallelOptions, delegate(TGenome genome) { TPhenome phenome = (TPhenome)genome.CachedPhenome; if (null == phenome) { // Non-viable genome. genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AuxFitnessArr = null; } else { FitnessInfo fitnessInfo = _phenomeEvaluator.Evaluate(phenome); genome.EvaluationInfo.SetFitness(fitnessInfo._fitness); genome.EvaluationInfo.AuxFitnessArr = fitnessInfo._auxFitnessArr; } }); doneEvaluatingGenomesBarrier.SignalAndWait(); }
/// <summary> /// Evaluates a list of genomes. Here we decode each genome in using the contained IGenomeDecoder /// and evaluate the resulting TPhenome using the contained IPhenomeEvaluator. /// </summary> public void EvaluateNeat(IList <NeatGenome> genomeList) { //Console.WriteLine("Number of genomes right before EvaluateNeat: " + genomeList.Count); Parallel.ForEach(genomeList, delegate(NeatGenome genome) { MarkovChain phenome = _genomeDecoder.Decode(genome); if (null == phenome) { genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AlternativeFitness = 0.0; } if (null == phenome) { // Non-viable genome. genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AlternativeFitness = 0.0; } else { FitnessInfo fitnessInfo = _passwordCrackingEvaluator.Evaluate(phenome); genome.EvaluationInfo.SetFitness(fitnessInfo._fitness); genome.EvaluationInfo.AlternativeFitness = fitnessInfo._alternativeFitness; } }); foreach (string p in _passwordCrackingEvaluator.FoundPasswords) { //PasswordCrackingEvaluator.Passwords.Remove(p); double val = PasswordCrackingEvaluator.Passwords[p].Reward; PasswordCrackingEvaluator.Passwords[p].Reward = val * 0.75; } }
/// <summary> /// Evaluate the two black boxes by playing them against each other in a /// game of Tic-Tac-Toe. All permutations of size 2 are going to be /// evaluated, so we only play one game: box1 is X, box2 is O. /// </summary> public void Evaluate(IBlackBox box1, IBlackBox box2, out FitnessInfo fitness1, out FitnessInfo fitness2) { // box1 plays as X. NeatPlayer player1 = new NeatPlayer(box1, SquareTypes.X); // box2 plays as O. NeatPlayer player2 = new NeatPlayer(box2, SquareTypes.O); // Play the two boxes against each other. var winner = TicTacToeGame.PlayGameToEnd(player1, player2); // Score box1 double score1 = getScore(winner, SquareTypes.X); fitness1 = new FitnessInfo(score1, score1); // Score box2 double score2 = getScore(winner, SquareTypes.O); fitness2 = new FitnessInfo(score2, score2); // Update the evaluation counter. _evalCount++; }
public BotTrackingRun(Point3D roomCenter, Point3D roomMin, Point3D roomMax, FitnessInfo score, BotTrackingEntry[] log) { RoomCenter = roomCenter; RoomMin = roomMin; RoomMax = roomMax; Score = score; Log = log; }
/// <summary> /// Evaluate the two black boxes by playing them against each other in a /// fight. All permutations of size 2 are going to be /// evaluated, so we only play one game. /// </summary> public void Evaluate(IBlackBox box1, IBlackBox box2, out FitnessInfo fitness1, out FitnessInfo fitness2) { // build fighter brains AIFighter player1Brain = new AIFighter(box1); AIFighter player2Brain = new AIFighter(box2); //start game by calling a function in the game and save this class so it can be found later if need be number.aiEval = this; Debug.Log("Create Game is called"); Master_Game_Object currGame = number.createGame(); //get player objects Debug.Log("Curr Game " + currGame + "."); Player[] players = currGame.getPlayers(); Player player1 = players[0]; Player player2 = players[1]; //assign brains to players player1.assignBrain(player1Brain); player2.assignBrain(player2Brain); //play game playGame(currGame); //TODO fix this to work correctly //get game results Player winner = null; //currGame.winner; double timeLeft = 300; //currGame.remainingTime; if (player1 == winner) { //player 1 wins. They get 1000 win bonus + points for how fast they won fitness1 = new FitnessInfo(1000 + timeLeft, 100 + timeLeft); //player 2 loses. They only get points for how long they lasted fitness2 = new FitnessInfo(300 - timeLeft, 300 - timeLeft); } else if (player2 == winner) { //player 1 loses. They only get points for how long they lasted fitness1 = new FitnessInfo(300 - timeLeft, 300 - timeLeft); //player 2 wins. They get 1000 win bonus + points for how fast they won fitness2 = new FitnessInfo(1000 + timeLeft, 100 + timeLeft); } else { //Nobody won. Nobody gets points. fitness1 = new FitnessInfo(0, 0); fitness2 = new FitnessInfo(0, 0); } // Update the evaluation counter. _evalCount++; //end the game Object.Destroy(currGame); }
public void Evaluate(IBlackBox box) { if (_neatSupervisor != null) { float fit = _neatSupervisor.GetFitness(box); FitnessInfo fitness = new FitnessInfo(fit, fit); _fitnessByBox.Add(box, fitness); } }
public FitnessInfo GetLastFitness(IBlackBox phenome) { if (dict.ContainsKey(phenome)) { FitnessInfo fit = dict[phenome]; dict.Remove(phenome); return(fit); } return(FitnessInfo.Zero); }
public FitnessInfo evaluate(Dictionary <float, float> trackpoints) { FitnessInfo eval = new FitnessInfo(); eval.time = 0; eval.pointsCleared = 0; float ParTime = 0; List <float> pointsX = new List <float> (trackpoints.Keys); pointsX.Sort(); float pointA = pointsX [0]; float velocity = info.startVelocity; for (int i = 1; i < pointsX.Count; i++) { float pointB = pointsX[i]; float dX = pointB - pointA; float dY = trackpoints[pointB] - trackpoints[pointA]; float l = Mathf.Sqrt(dX * dX + dY * dY); if (dY != 0) { ParTime = (-velocity + Mathf.Sqrt(velocity * velocity + 2 * info.g * dY)) / ((info.g * dY) / l); } else { ParTime = l / velocity; } if (ParTime <= 0) { eval.time = Mathf.Infinity; return(eval); } velocity += (info.g * dY) / l * ParTime; if (velocity < 0) { eval.time = Mathf.Infinity; return(eval); } pointA = pointB; eval.time += ParTime; eval.pointsCleared++; eval.distanceTraveled += dX; } return(eval); }
public FitnessInfo GetLastFitness(IBlackBox phenome) { if (_fitnessByBox.ContainsKey(phenome)) { FitnessInfo fit = _fitnessByBox[phenome]; _fitnessByBox.Remove(phenome); return(fit); } return(FitnessInfo.Zero); }
public IEnumerator Evaluate(IBlackBox box) { if (optimizer != null) { optimizer.Evaluate(box); yield return(new WaitForSeconds(optimizer.TrialDuration)); optimizer.StopEvaluation(box); float fit = optimizer.GetFitness(box); FitnessInfo fitness = new FitnessInfo(fit, fit); dict.Add(box, fitness); } }
//static IGenomeDecoder<NeatGenome, MarkovChain> _genomeDecoder; //static PasswordCrackingEvaluator _passwordCrackingEvaluator; public static void Evaluate(IGenomeDecoder <NeatGenome, MarkovChain> genomeDecoder, PasswordCrackingEvaluator passwordCrackingEvaluator, PasswordEvolutionExperiment experiment) { string[] genomeFiles = Directory.GetFiles(@"..\..\..\experiments\genomes\", "*.xml"); XmlDocument doc = new XmlDocument(); int genomeNumber; foreach (string genomeFile in genomeFiles) { // Read in genome doc.Load(genomeFile); //NeatGenome genome = NeatGenomeXmlIO.LoadGenome(doc, false); //NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(); NeatGenome genome = experiment.LoadPopulation(XmlReader.Create(genomeFile))[0]; MarkovChain phenome = experiment.CreateGenomeDecoder().Decode(genome);//genomeDecoder.Decode(genome); string[] filePath = genomeFile.Split('\\'); string[] fileName = (filePath[filePath.Length - 1]).Split('-'); String fileNumber = (fileName[1]).Split('.')[0]; genomeNumber = Convert.ToInt32(fileNumber); //FileStream fs = File.Open(@"..\..\..\experiments\genomes\genome-results\genome-"+genomeNumber+"-results.txt", FileMode.CreateNew, FileAccess.Write); TextWriter tw = new StreamWriter(@"..\..\..\experiments\genomes\genome-results\genome-" + genomeNumber + "-results.txt"); // Evaluate if (null == phenome) { // Non-viable genome. tw.WriteLine("0.0 0.0"); } else { FitnessInfo fitnessInfo = passwordCrackingEvaluator.Evaluate(phenome); double val = fitnessInfo._fitness; double val2 = fitnessInfo._alternativeFitness; tw.WriteLine(fitnessInfo._fitness + " " + fitnessInfo._alternativeFitness); } tw.Close(); File.Create(@"..\..\..\experiments\genomes\genome-finished\genome-" + genomeNumber + "-finished.txt"); } // Write results?? -> genome_#_results // Write finished flag -> genome_#_finished }
/// <summary> /// Evaluates a list of phenomes and returns a list of their fitness infos. /// </summary> /// <param name="phenomes">The phenomes we want to evaluate.</param> /// <param name="fitness">The list of acumulated fitness info.</param> public void Evaluate(List <IBlackBox> phenomes, out FitnessInfo fitness) { _evalCount += 1; // Create the players from the phenomes. List <NeatPlayer> players = phenomes.Select(blackBox => new NeatPlayer(blackBox)).ToList(); // Calculate the total harmoniousness for all the players. double regularity = MusicEnvironment.FITNESS_FUNCTION.CalculateRegularity(players); fitness = new FitnessInfo(regularity, 0); // Change this if stop condition is required. _stopConditionSatisfied = false; }
public IEnumerator Evaluate(IBlackBox box) { if (se != null) { se.Evaluate(box); yield return(new WaitForSeconds(se.Duration)); se.StopEvaluation(); float fit = se.GetFitness(); if (fit > se.StoppingFitness) { _stopConditionSatisfied = true; } this.fitness = new FitnessInfo(fit, fit); } }
/// <summary> /// Evaluate the provided phenome and return its fitness score. /// This takes a blackbox (pretty much the decoded neuralnetwork (the phenome)) that input can be passed to /// and output processed in the domain. Makes it very easy, we dont have to wory about the genome (genelist) or phenome (network) at all /// </summary> public FitnessInfo Evaluate(IBlackBox box) { // Set a maximum number of evaluations _evalCount++; double fitness = 0.5; FitnessInfo fitnessInfo = new FitnessInfo(fitness, fitness); if (_evalCount > _nextEvalStop) { _nextEvalStop += _evalBlockSize; _stopConditionSatisfied = true; } // Only use player model if it has been initialized. Otherwise, all fitness is 0.5 if (_experiment.playerModel != null) { // Get highlevel features PCGSharpHighLevelFeatures featureCounts = new PCGSharpHighLevelFeatures(); PCGSharpHighLevelFeatures.C_HL_ROOMTYPE lastRoomType = PCGSharpHighLevelFeatures.C_HL_ROOMTYPE.NoPreviousRoom; for (int i = 0; i < _experiment.geomNodeList.Count; i++) { // Get cppn output for each node box.InputSignalArray[0] = _experiment.geomNodeList[i].normDepth; box.InputSignalArray[1] = _experiment.geomNodeList[i].normSiblingIndex; box.ResetState(); box.Activate(); double[] outputs = new double[box.OutputCount]; for (int j = 0; j < box.OutputCount; j++) { outputs[j] = box.OutputSignalArray[j]; } // Convert each nodes cppn output into a contentId int combinedContentID = featureCounts.CPPNOutputToSettingsId(outputs); lastRoomType = featureCounts.UpdateFeatures(combinedContentID, lastRoomType); } // Pass the highlevel features into the player model to get the fitness double[] distributions = _experiment.playerModel.ClassifyNewData(featureCounts.ToDoubleArray()); // Fitness is the membership to the "Like" class (positive class) fitnessInfo = new FitnessInfo(distributions[1], distributions[1]); } return(fitnessInfo); }
public void DoWork() { for (int i = start; i < end; i++) { FitnessInfo f = evaluator.Evaluate(new QuickBlackBox(genomeList[i])); if (SharedParams.SharedParams.USEFITNESSBANK) { genomeList[i].Fitness *= 1 - SharedParams.SharedParams.FITNESSBANKDECAYRATE; // decay the previous fitness genomeList[i].Fitness += f._fitness; // add the new value } else { genomeList[i].Fitness = f._fitness; } genomeList[i].AltFitness = f._auxFitnessArr[0]._value; } }
public static FitnessInfo FinishEvaluation(ref bool isEvaluating, double runningTime, double error, double maxEvalTime, BotTrackingStorage log, TrainingRoom room, BotTrackingEntry[] snapshots) { isEvaluating = false; double fitness = runningTime - error; if (fitness < 0) { fitness = 0; } fitness *= 1000 / maxEvalTime; // scale the score to be 1000 FitnessInfo retVal = new FitnessInfo(fitness, fitness); log.AddEntry(new BotTrackingRun(room.Center, room.AABB.Item1, room.AABB.Item2, retVal, snapshots)); return(retVal); }
public FitnessInfo Evaluate(FastCyclicNetwork phenome) { EvaluationCount++; var fcn = FastCyclicNetworkAdapter.Convert(phenome); ProtocolManager.Open(); var cFitness = ProtocolManager.Client.calculateXorPhenotypeFitness(fcn); //ProtocolManager.Close(); adding this line implies a huge performance hit var auxFitness = new AuxFitnessInfo[cFitness.AuxFitness.Count]; for (int i = 0; i < auxFitness.Length; i++) { auxFitness[i] = new AuxFitnessInfo(cFitness.AuxFitness[i].Name, cFitness.AuxFitness[i].Value); } var fitness = new FitnessInfo(cFitness.Fitness, auxFitness); StopConditionSatisfied = cFitness.StopConditionSatisfied; if (StopConditionSatisfied) { FitnessInfo fi = _xorBlackBoxEvaluator.Evaluate(phenome); bool reallySatisfied = _xorBlackBoxEvaluator.StopConditionSatisfied; Debug.Assert((fi._fitness >= 10) == _xorBlackBoxEvaluator.StopConditionSatisfied); if (!reallySatisfied) { Console.Out.WriteLine("ERROR: really fitness is not calculated (" + fi._fitness + " versus " + cFitness.Fitness + ")"); Console.Out.WriteLine(phenome.ToString()); //throw new Exception("Wheih :("); } else { Console.Out.WriteLine("Networks is really good"); } } // We do not use the NEAT genome decoder. The NEAT genome decoder would // do the same as the following code, but provide us with an IBlackBox object. // Because we pass on the object to our Java code, we need to be aware of its // underlying structure. The additional layer of abstraction gets in the way. return(fitness); }
public IEnumerator Evaluate(IList <TGenome> genomeList) { foreach (TGenome genome in genomeList) { var phenome = genomeDecoder.Decode(genome); if (phenome == null) { genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AuxFitnessArr = null; } else { FitnessInfo fitnessInfo = phenomeEvaluator.Evaluate(phenome); genome.EvaluationInfo.SetFitness(fitnessInfo._fitness); genome.EvaluationInfo.AuxFitnessArr = fitnessInfo._auxFitnessArr; } } yield break; }
/// <summary> /// Evaluate the provided phenome and return its fitness score. /// This takes a blackbox (pretty much the decoded neuralnetwork (the phenome)) that input can be passed to /// and output processed in the domain. Makes it very easy, we dont have to wory about the genome (genelist) or phenome (network) at all /// </summary> public FitnessInfo Evaluate(IBlackBox box) { // Alternative to promote complexity in hidden layer /*** Had to change FastAcyclicNetwork to contain the complexity variable * and had to change FastAcyclicNetworkFactory to assign it during genome decoding to blackbox (a FastAcyclicNetwork in this case) */ /* _evalCount++; * double fitness = 0.0; * if (_evalCount < 5000) * { * FastAcyclicNetwork concrete = (FastAcyclicNetwork)box; * * // Searching for a ideally 2 hidden nodes * fitness = 100 - Math.Abs(2-concrete.hiddenNodeCount); * if (fitness < 0.0) * fitness = 0.0; * } * else * fitness = 1000; */ /* Dummy fitness */ _evalCount++; double fitness = 0.5; if (_evalCount > _nextEvalStop) { _nextEvalStop += _evalBlockSize; _stopConditionSatisfied = true; } //double fitness = 0.0; //For the first set of evaluations, try to establish a number of hidden node. // This initializes the network to a certain complexity //FastAcyclicNetwork concrete = (FastAcyclicNetwork)box; // Searching for a ideally 2 hidden nodes //fitness = 100 - Math.Abs(2 - concrete.hiddenNodeCount); FitnessInfo fitnessInfo = new FitnessInfo(fitness, fitness); // Second arguement is auxilary fitness, probably wont need to use it return(fitnessInfo); }
public IEnumerator Evaluate(IBlackBox box) { if (optimizer != null) { optimizer.Evaluate(box); hasEvaluated = false; while (BraidSimulationManager.ShouldBraidsEvaluate()) { //Debug.Log("Evaluating"); yield return(new WaitForSeconds(0.2f)); } optimizer.StopEvaluation(box); float fit = optimizer.GetFitness(box); FitnessInfo fitness = new FitnessInfo(fit, fit); dict.Add(box, fitness); BraidSimulationManager.evaluationsMade++; hasEvaluated = true; } }
private IEnumerator evaluateList(IList <TGenome> genomeList) { foreach (TGenome genome in genomeList) { TPhenome phenome = _genomeDecoder.Decode(genome); if (null == phenome) { // Non-viable genome. genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AuxFitnessArr = null; } else { yield return(Coroutiner.StartCoroutine(_phenomeEvaluator.Evaluate(phenome))); FitnessInfo fitnessInfo = _phenomeEvaluator.GetLastFitness(phenome); genome.EvaluationInfo.SetFitness(fitnessInfo._fitness); genome.EvaluationInfo.AuxFitnessArr = fitnessInfo._auxFitnessArr; } } }
private static void VerifyNeuralNetResponseInner(bool enableHardwareAcceleration) { var activationFnFactory = new DefaultActivationFunctionFactory <double>(enableHardwareAcceleration); var metaNeatGenome = new MetaNeatGenome <double>(4, 1, true, activationFnFactory.GetActivationFunction("LeakyReLU")); // Load test genome. NeatGenomeLoader <double> loader = NeatGenomeLoaderFactory.CreateLoaderDouble(metaNeatGenome); NeatGenome <double> genome = loader.Load("TestData/binary-three-multiplexer.genome"); // Decode genome to a neural net. var genomeDecoder = NeatGenomeDecoderFactory.CreateGenomeDecoderAcyclic(); IBlackBox <double> blackBox = genomeDecoder.Decode(genome); // Evaluate the neural net. var evaluator = new BinaryThreeMultiplexerEvaluator(); // Confirm the expected fitness (to a limited amount of precision to allow for small variations of floating point // results that can occur as a result of platform/environmental variations). FitnessInfo fitnessInfo = evaluator.Evaluate(blackBox); Assert.Equal(107.50554956432657, fitnessInfo.PrimaryFitness, 6); }
private void updateStats() { if (currentGeneration % 1000 == 0) { System.IO.File.AppendAllText("gen.txt", System.DateTime.Now + ":" + currentGeneration + Environment.NewLine); } if (SharedParams.SharedParams.TRACKFITNESS) { if (currentGeneration % SharedParams.SharedParams.TRACKFITNESSSTRIDE == 0) { // determine who is best at the validation set double maxFit = 0; double maxAlt = 0; int maxAltIndex = 0; for (int j = 0; j < genomeList.Count; j++) { if (genomeList[j].Fitness > maxFit) { maxFit = genomeList[j].Fitness; } if (genomeList[j].AltFitness > maxAlt) { maxAlt = genomeList[j].AltFitness; maxAltIndex = j; } } FitnessInfo fit = evaluator.Test(new QuickBlackBox(genomeList[maxAltIndex])); System.IO.File.AppendAllText("training.txt", maxFit + Environment.NewLine); System.IO.File.AppendAllText("validation.txt", maxAlt + Environment.NewLine); System.IO.File.AppendAllText("test.txt", fit._fitness + Environment.NewLine); } } }
/// <summary> /// Evaluate the two black boxes by playing them against each other in 2 /// games of Tic-Tac-Toe. Each player plays each side once. /// </summary> public void Evaluate(IBlackBox box1, IBlackBox box2, out FitnessInfo fitness1, out FitnessInfo fitness2) { double score1, score2; if (_playerOneSquareType == SquareTypes.X) { // Evaluate with box1 as X. evaluate(box1, box2, out score1, out score2); } else { // Evaluate with box1 as O. evaluate(box2, box1, out score2, out score1); } // Set the final fitness values fitness1 = new FitnessInfo(score1, score1); fitness2 = new FitnessInfo(score2, score2); // Update the evaluation counter. _evalCount++; }
/// <summary> /// Accepts a <see cref="FitnessInfo"/>, which is intended to be from the fittest genome in the population, and returns a boolean /// that indicates if the evolution algorithm can stop, i.e. because the fitness is the best that can be achieved (or good enough). /// </summary> /// <param name="fitnessInfo">The fitness info object to test.</param> /// <returns>Returns true if the fitness is good enough to signal the evolution algorithm to stop.</returns> public bool TestForStopCondition(FitnessInfo fitnessInfo) { return(fitnessInfo.PrimaryFitness >= 100.0); }
public void Reset() { this.fitness = FitnessInfo.Zero; }
public void run() { // evaluate the network once first FitnessInfo fit = evaluator.Evaluate(box); // print stats System.IO.File.AppendAllText("backprop.txt", fit._auxFitnessArr[0]._value + Environment.NewLine); LEEAParams.DOMAIN.generateSampleList(); List <Sample> fullSampleList = LEEAParams.DOMAIN.getSamples(); // for rms prop with connection cache method double[][][] connectionCache = new double[network.weights.Length][][]; // for rms prop with connection queue method double[][][][] connectionQueue = new double[network.weights.Length][][][]; int connectionQueueIndex = 0; double[][][] connectionQueueAverages = new double[network.weights.Length][][]; // for efficient way of maintaining averages if (LEEAParams.RMSPROP) { if (LEEAParams.RMSCONNECTIONQUEUE) { for (int i = 0; i < network.weights.Length; i++) { connectionQueue[i] = new double[network.weights[i].Length][][]; connectionQueueAverages[i] = new double[network.weights[i].Length][]; for (int j = 0; j < network.weights[i].Length; j++) { connectionQueue[i][j] = new double[network.weights[i][j].Length][]; connectionQueueAverages[i][j] = new double[network.weights[i][j].Length]; for (int k = 0; k < network.weights[i][j].Length; k++) { connectionQueue[i][j][k] = new double[LEEAParams.RMSPROPK]; } } } } else { for (int i = 0; i < network.weights.Length; i++) { connectionCache[i] = new double[network.weights[i].Length][]; for (int j = 0; j < network.weights[i].Length; j++) { connectionCache[i][j] = new double[network.weights[i][j].Length]; } } } } for (int epoch = 0; epoch < LEEAParams.BPMAXEPOCHS; epoch++) { // generate a random order to evaluate the samples for each epoch List <int> sampleOrder = new List <int>(fullSampleList.Count); for (int i = 0; i < fullSampleList.Count; i++) { sampleOrder.Add(i); } Random rng = new Random(); sampleOrder = sampleOrder.OrderBy(a => rng.Next()).ToList(); // go through each sample for (int i = 0; i < fullSampleList.Count; i++) { int index = sampleOrder[i]; // activate the network ISignalArray inputArr = box.InputSignalArray; ISignalArray outputArr = box.OutputSignalArray; inputArr.CopyFrom(fullSampleList[index].Inputs, 0); box.ResetState(); box.Activate(); // calculate error double[][] errors = new double[box.nodeActivation.Length][]; for (int j = 0; j < errors.Length; j++) { errors[j] = new double[box.nodeActivation[j].Length]; } // output nodes for (int j = 0; j < outputArr.Length; j++) { // calculate error for this output node double error = (fullSampleList[index].Outputs[j] - outputArr[j]) * outputArr[j] * (1 - outputArr[j]); errors[errors.Length - 1][j] = error; } // hidden nodes for (int j = errors.Length - 2; j > 0; j--) // layer by layer, counting backwards, don't need j=0 since that is the input layer { for (int k = 0; k < errors[j].Length; k++) // each node in the layer { double error = 0; for (int l = 0; l < errors[j + 1].Length; l++) // each node in the upper layer it is connected to { error += box.nodeActivation[j][k] * (1 - box.nodeActivation[j][k]) * errors[j + 1][l] * network.weights[j][l][k]; } errors[j][k] = error; } } // update weights for (int j = 0; j < network.weights.Length; j++) { for (int k = 0; k < network.weights[j].Length; k++) { for (int l = 0; l < network.weights[j][k].Length; l++) { if (LEEAParams.RMSPROP && LEEAParams.PLAINR) { int mult = 1; if (box.nodeActivation[j][l] * errors[j + 1][k] < 0) { mult = -1; } network.weights[j][k][l] += LEEAParams.BPLEARNINGRATE * mult; } else if (LEEAParams.RMSPROP && LEEAParams.RMSCONNECTIONQUEUE) { // get the magnitude of recent gradient for this connection double rms = connectionQueueAverages[j][k][l]; // add the current gradient to the magnitudes queue and update the averages array double prev = connectionQueue[j][k][l][connectionQueueIndex]; double dx = box.nodeActivation[j][l] * errors[j + 1][k]; connectionQueue[j][k][l][connectionQueueIndex] = dx * dx; connectionQueueAverages[j][k][l] = ((connectionQueueAverages[j][k][l] * connectionQueue[j][k][l].Length) + (connectionQueue[j][k][l][connectionQueueIndex] - prev)) / connectionQueue[j][k][l].Length; // update the weight based on the average recent gradient double delta = dx * LEEAParams.BPLEARNINGRATE; if (rms != 0) { rms = Math.Sqrt(rms + .00000001); delta /= rms; } network.weights[j][k][l] += delta; } else if (LEEAParams.RMSPROP) { double dx = box.nodeActivation[j][l] * errors[j + 1][k]; connectionCache[j][k][l] = (1 - LEEAParams.RMSCONNECTIONDECAY) * connectionCache[j][k][l] + LEEAParams.RMSCONNECTIONDECAY * dx * dx; network.weights[j][k][l] += LEEAParams.BPLEARNINGRATE * dx / Math.Sqrt(connectionCache[j][k][l] + .00000001); } else { float delta = (float)(box.nodeActivation[j][l] * errors[j + 1][k] * LEEAParams.BPLEARNINGRATE); network.weights[j][k][l] += delta; } } } } connectionQueueIndex++; connectionQueueIndex = connectionQueueIndex % LEEAParams.RMSPROPK; } // decay the learning rate LEEAParams.BPLEARNINGRATE *= (1 - LEEAParams.BPLEARNINGRATEDECAY); // print stats if (epoch % SharedParams.SharedParams.TRACKFITNESSSTRIDE == 0) { // evaluate the network fit = evaluator.Evaluate(box); System.IO.File.AppendAllText("epoch.txt", System.DateTime.Now + ":" + epoch + Environment.NewLine); System.IO.File.AppendAllText("training.txt", fit._fitness + Environment.NewLine); System.IO.File.AppendAllText("validation.txt", fit._auxFitnessArr[0]._value + Environment.NewLine); fit = evaluator.Test(box); System.IO.File.AppendAllText("test.txt", fit._fitness + Environment.NewLine); } } }
public void evaluate() { CalcTrackPoints(); eval = problem.evaluate(trackPoints); CalcFitness(); }
private IEnumerator evaluateList(IList <TGenome> genomeList) { Dictionary <TGenome, TPhenome> dict = new Dictionary <TGenome, TPhenome>(); Dictionary <TGenome, FitnessInfo[]> fitnessDict = new Dictionary <TGenome, FitnessInfo[]>(); for (int i = 0; i < _optimizer.Trials; i++) { //Utility.Log("Iteration " + (i + 1)); _phenomeEvaluator.Reset(); dict = new Dictionary <TGenome, TPhenome>(); foreach (TGenome genome in genomeList) { TPhenome phenome = _genomeDecoder.Decode(genome); if (null == phenome) { // Non-viable genome. genome.EvaluationInfo.SetFitness(0.0); genome.EvaluationInfo.AuxFitnessArr = null; } else { if (i == 0) { fitnessDict.Add(genome, new FitnessInfo[_optimizer.Trials]); } dict.Add(genome, phenome); //if (!dict.ContainsKey(genome)) //{ // dict.Add(genome, phenome); // fitnessDict.Add(phenome, new FitnessInfo[_optimizer.Trials]); //} Coroutiner.StartCoroutine(_phenomeEvaluator.Evaluate(phenome)); } } yield return(new WaitForSeconds(_optimizer.TrialDuration)); foreach (TGenome genome in dict.Keys) { TPhenome phenome = dict[genome]; if (phenome != null) { FitnessInfo fitnessInfo = _phenomeEvaluator.GetLastFitness(phenome); fitnessDict[genome][i] = fitnessInfo; } } } foreach (TGenome genome in dict.Keys) { TPhenome phenome = dict[genome]; if (phenome != null) { double fitness = 0; for (int i = 0; i < _optimizer.Trials; i++) { fitness += fitnessDict[genome][i]._fitness; } var fit = fitness; fitness /= _optimizer.Trials; // Averaged fitness if (fit > _optimizer.StoppingFitness) { // Utility.Log("Fitness is " + fit + ", stopping now because stopping fitness is " + _optimizer.StoppingFitness); // _phenomeEvaluator.StopConditionSatisfied = true; } if (_optimizer.SelectionHasBeenMade && _optimizer.SelectedGenomeId == genome.Id) { if (_optimizer.TimeSinceSelection < Optimizer.DecayTime) { fitness += (Optimizer.DecayTime - _optimizer.TimeSinceSelection) * Optimizer.SelectionBoost; } } genome.EvaluationInfo.SetFitness(fitness); genome.EvaluationInfo.AuxFitnessArr = fitnessDict[genome][0]._auxFitnessArr; } } }