private void StartNewGeneration() { curGeneration++; finishedBotCount = 0; generationText.text = "Generation " + curGeneration.ToString(); GeneScore baseGene = null; List <GeneScore> crossoverCandidates = new List <GeneScore>(); if (geneScores.Count > 0) { List <GeneScore> orderedGenes = geneScores.OrderByDescending(x => x.score).ToList(); UpdateLeaderBoard(orderedGenes); baseGene = new GeneScore(orderedGenes.First().CreateForceGeneCopy()); List <GeneScore> mostFitting = GetMostFitting(orderedGenes.Skip(1).ToList()); foreach (GeneScore gs in mostFitting) { crossoverCandidates.Add(new GeneScore(gs.CreateForceGeneCopy())); } geneScores = new List <GeneScore>(orderedGenes); CleanGeneList(); } for (int i = 0; i < generationTotal; i++) { CreateNewBot(i, new Vector3(startPos.x + (i * 4), startPos.y, startPos.z), baseGene, crossoverCandidates); } }
private List <ForceGene> GetCrossoverAndMutatedGene(GeneScore baseGene, List <GeneScore> crossoverCandidates) { GeneScore newGene = new GeneScore(baseGene.CreateForceGeneCopy()); GeneScore crossoverGene = new GeneScore(newGene.CrossOver(crossoverCandidates)); List <ForceGene> mutatedGene = new GeneScore(crossoverGene.Mutate()).CreateForceGeneCopy(); return(mutatedGene); }
public void RegisterGene(GeneScore geneScore) { finishedBotCount++; geneScores.Add(geneScore); if (finishedBotCount == generationTotal) { StartNewGeneration(); } }
private void HandleGenerationComplete(object sender, GenerationCompleteEventArgs args) { // Update best if necessary if (_bestResult == null || args.Scores[0] > _bestResult) { _bestResult = args.Scores[0]; } var iterationsPerSecond = _iterationNumber / (DateTime.Now - _startDateTime).TotalSeconds; Strategy.Print(Strategy.Instrument.FullName + ": completed generation " + (args.GenerationNumber + 1) + "/" + __lastParameters.MaximumGenerations + ", iterations per second: " + iterationsPerSecond.ToString("N1") + ", max fitness this gen: " + args.Scores[0].Fitness.ToString("N2")); _parameterWriter.Flush(); }
private void CreateNewBot(int curBotId, Vector3 botPos, GeneScore baseGene, List <GeneScore> crossoverCandidates) { GameObject curBot = Instantiate(ragdoll, botPos, Quaternion.identity) as GameObject; RagdollController ragdollController = curBot.GetComponent <RagdollController>(); ragdollController.Initialize(this, "G" + curGeneration.ToString() + "B" + curBotId.ToString()); if (baseGene == null) { ragdollController.StartMoving(GetNewGene(ragdollController.GetNumOfJoints())); } else { if (curBotId == 0) { ragdollController.StartMoving(baseGene.CreateForceGeneCopy()); } else { ragdollController.StartMoving(GetCrossoverAndMutatedGene(baseGene, crossoverCandidates)); } } }
public IterationCompleteEventArgs(GeneScore score, int iterationNumber, int iterationsRemaining) { _score = score; _iterationNumber = iterationNumber; _iterationsRemaining = iterationsRemaining; }
private bool OnIterationComplete(GeneScore score, int iterationCount, int iterationsRemaining) { if (IterationComplete != null) { var args = new IterationCompleteEventArgs(score, iterationCount, iterationsRemaining); IterationComplete(this, args); return args.Stop; } return false; }