private void AgentCrossover(int playerIndex) { List <BrainGenome> newGenBrainGenomeList = new List <BrainGenome>(); // new population! FitnessManager fitnessManager = teamsConfig.playersList[playerIndex].fitnessManager; TrainingSettingsManager trainingSettingsManager = teamsConfig.playersList[playerIndex].trainingSettingsManager; // Keep top-half peformers + mutations: for (int x = 0; x < teamsConfig.playersList[playerIndex].agentGenomeList.Count; x++) { if (x == 0) { BrainGenome parentGenome = teamsConfig.playersList[playerIndex].agentGenomeList[fitnessManager.rankedIndicesList[x]].brainGenome; //parentGenome.index = 0; newGenBrainGenomeList.Add(parentGenome); } else { BrainGenome newBrainGenome = new BrainGenome(); int parentIndex = fitnessManager.GetAgentIndexByLottery(); BrainGenome parentGenome = teamsConfig.playersList[playerIndex].agentGenomeList[parentIndex].brainGenome; newBrainGenome.SetToMutatedCopyOfParentGenome(parentGenome, trainingSettingsManager); newGenBrainGenomeList.Add(newBrainGenome); } } for (int i = 0; i < teamsConfig.playersList[playerIndex].agentGenomeList.Count; i++) { teamsConfig.playersList[playerIndex].agentGenomeList[i].brainGenome = newGenBrainGenomeList[i]; } }
private void EnvironmentCrossover() { List <EnvironmentGenome> newGenGenomeList = new List <EnvironmentGenome>(); // new population! FitnessManager fitnessManager = teamsConfig.environmentPopulation.fitnessManager; TrainingSettingsManager trainingSettingsManager = teamsConfig.environmentPopulation.trainingSettingsManager; float mutationChance = trainingSettingsManager.mutationChance; float mutationStepSize = trainingSettingsManager.mutationStepSize; // Keep top-half peformers + mutations: for (int x = 0; x < teamsConfig.environmentPopulation.environmentGenomeList.Count; x++) { if (false) //x == 0) { // Top performer stays { EnvironmentGenome parentGenome = teamsConfig.environmentPopulation.environmentGenomeList[fitnessManager.rankedIndicesList[x]]; parentGenome.index = 0; newGenGenomeList.Add(parentGenome); } else { int parentIndex = fitnessManager.GetAgentIndexByLottery(); EnvironmentGenome parentGenome = teamsConfig.environmentPopulation.environmentGenomeList[parentIndex]; EnvironmentGenome newGenome = parentGenome.BirthNewGenome(parentGenome, newGenGenomeList.Count, teamsConfig.challengeType, mutationChance, mutationStepSize); newGenGenomeList.Add(newGenome); } } for (int i = 0; i < teamsConfig.environmentPopulation.environmentGenomeList.Count; i++) { teamsConfig.environmentPopulation.environmentGenomeList[i] = newGenGenomeList[i]; } }
public void StartNewTrainingMode() { Destroy(playerGO); // Create population brains agentGenomeList = new List <AgentGenome>(); BodyGenome templateBodyGenome = new BodyGenome(); templateBodyGenome.InitializeGenomeAsDefault(); //BodyGenome bodyGenomeTemplate = new BodyGenome(); //bodyGenomeTemplate.CopyBodyGenomeFromTemplate(bodyTemplate); for (int i = 0; i < trainingPopulationSize; i++) { AgentGenome newGenome = new AgentGenome(i); newGenome.InitializeBodyGenomeFromTemplate(templateBodyGenome); newGenome.InitializeRandomBrainFromCurrentBody(0.033f); agentGenomeList.Add(newGenome); } // CrossoverSettings: trainingSettingsManager = new TrainingSettingsManager(0.06f, 0.35f, 0.1f, 0.01f); GameObject dummyAgentGO = new GameObject("DummyAgent"); dummyAgent = dummyAgentGO.AddComponent <Agent>(); dummyStartGenome = new StartPositionGenome(Vector3.zero, Quaternion.identity); ResetTrainingForNewGen(); //Brain brain = new Brain() //Debug.Log(dummyAgent.testModule.); // Create Visible Display Agent to observe behavior /*displayStartPos = new StartPositionGenome(Vector3.zero, Quaternion.identity); * GameObject agentGO = Instantiate(Resources.Load("PredatorPrey/PredatorPrefab")) as GameObject; ; // GameObject.CreatePrimitive(PrimitiveType.Sphere); * //displayAgent * agentGO.name = "Guinea Pig"; * //agentGO.transform.parent = gameObject.transform; * agentGO.transform.localPosition = displayStartPos.agentStartPosition; * agentGO.transform.localRotation = displayStartPos.agentStartRotation; * //agentGO.GetComponent<Collider>().enabled = false; * displayAgent = agentGO.AddComponent<Agent>(); * displayAgent.isVisible = true; * displayAgent.InitializeAgentFromTemplate(agentGenomeList[0], displayStartPos); * // hook modules: * displayAgent.testModule.enemyTransform = targetGO.transform; * displayAgent.testModule.bias[0] = 1f; * displayAgent.testModule.enemyTestModule = displayAgent.testModule; // self as target, to zero out targetvel */ ResetTrainingAgentAndEnvironment(); }
public void SetStatusFromData(TrainingManager trainerRef) { this.trainerRef = trainerRef; int focusPop = trainerRef.evaluationManager.exhibitionTicketList[trainerRef.evaluationManager.exhibitionTicketCurrentIndex].focusPopIndex; int numPerfReps = 0; int numHistReps = 0; int numBaseReps = 0; bool isEvolving; if (focusPop < 1) { // env numPerfReps = trainerRef.teamsConfig.environmentPopulation.numPerformanceReps; numHistReps = trainerRef.teamsConfig.environmentPopulation.numHistoricalReps; numBaseReps = trainerRef.teamsConfig.environmentPopulation.numBaselineReps; currentTrainingSettingsRef = trainerRef.teamsConfig.environmentPopulation.trainingSettingsManager; isEvolving = trainerRef.teamsConfig.environmentPopulation.isTraining; } else { numPerfReps = trainerRef.teamsConfig.playersList[focusPop - 1].numPerformanceReps; numHistReps = trainerRef.teamsConfig.playersList[focusPop - 1].numHistoricalReps; numBaseReps = trainerRef.teamsConfig.playersList[focusPop - 1].numBaselineReps; currentTrainingSettingsRef = trainerRef.teamsConfig.playersList[focusPop - 1].trainingSettingsManager; isEvolving = trainerRef.teamsConfig.playersList[focusPop - 1].isTraining; } textRepresentativesPerfValue.text = numPerfReps.ToString(); textRepresentativesHistValue.text = numHistReps.ToString(); textRepresentativesBaseValue.text = numBaseReps.ToString(); textMutationChanceValue.text = (100f * currentTrainingSettingsRef.mutationChance).ToString("F3") + "%"; textMutationStepSizeValue.text = (100f * currentTrainingSettingsRef.mutationStepSize).ToString("F1") + "%"; textNewLinkChanceValue.text = (100f * currentTrainingSettingsRef.newLinkChance).ToString("F2") + "%"; textNewNeuronChanceValue.text = (100f * currentTrainingSettingsRef.newHiddenNodeChance).ToString("F2") + "%"; if (isEvolving) { buttonEvolving.GetComponent <Image>().color = Color.white; buttonEvolving.GetComponentInChildren <Text>().color = Color.black; buttonEvolving.GetComponentInChildren <Text>().text = "EVOLVING"; } else { buttonEvolving.GetComponent <Image>().color = Color.black; buttonEvolving.GetComponentInChildren <Text>().color = Color.white; buttonEvolving.GetComponentInChildren <Text>().text = "STATIC"; } }
// Representative system will be expanded later - for now, just defaults to Top # of performers public PlayerPopulation(int index, BodyGenome bodyTemplate, int numGenomes, int numPerfReps) { this.index = index; // Re-Factor: bodyGenomeTemplate = new BodyGenome(); bodyGenomeTemplate.CopyBodyGenomeFromTemplate(bodyTemplate); //graphKing = new TheGraphKing(); popSize = numGenomes; //this.numBaseline = numBaseline; // Create blank AgentGenomes for the standard population agentGenomeList = new List <AgentGenome>(); historicGenomePool = new List <AgentGenome>(); //baselineGenomePool = new List<AgentGenome>(); for (int j = 0; j < numGenomes; j++) { AgentGenome agentGenome = new AgentGenome(j); agentGenome.InitializeBodyGenomeFromTemplate(bodyGenomeTemplate); agentGenome.InitializeRandomBrainFromCurrentBody(0.2f); agentGenomeList.Add(agentGenome); } //RepopulateBaselineGenomes(); //AppendBaselineGenomes(); // Representatives: numPerformanceReps = numPerfReps; //Debug.Log("historicGenomePool count b4: " + historicGenomePool.Count.ToString()); int numStartingHistoricalReps = 20; for (int h = 0; h < numStartingHistoricalReps; h++) { historicGenomePool.Add(agentGenomeList[h]); // init } //Debug.Log("historicGenomePool count after: " + historicGenomePool.Count.ToString()); ResetRepresentativesList(); fitnessManager = new FitnessManager(); SetUpDefaultFitnessComponents(fitnessManager, this.index); //fitnessManager.ResetHistoricalData(); //fitnessManager.ResetCurrentHistoricalDataLists(); fitnessManager.InitializeForNewGeneration(agentGenomeList.Count); trainingSettingsManager = new TrainingSettingsManager(0.075f, 0.04f, 0.015f, 0.003f); }
// Representative system will be expanded later - for now, just defaults to Top # of performers public PlayerPopulation(Challenge.Type challengeType, BodyGenome bodyTemplate, int numGenomes, int numBaseline, int numReps) { bodyGenomeTemplate = new BodyGenome(); bodyGenomeTemplate.CopyBodyGenomeFromTemplate(bodyTemplate); graphKing = new TheGraphKing(); popSize = numGenomes; this.numBaseline = numBaseline; // Create blank AgentGenomes for the standard population agentGenomeList = new List <AgentGenome>(); historicGenomePool = new List <AgentGenome>(); baselineGenomePool = new List <AgentGenome>(); for (int j = 0; j < numGenomes; j++) { AgentGenome agentGenome = new AgentGenome(j); agentGenome.InitializeBodyGenomeFromTemplate(bodyGenomeTemplate); agentGenome.InitializeRandomBrainFromCurrentBody(0.0f); agentGenomeList.Add(agentGenome); } RepopulateBaselineGenomes(); AppendBaselineGenomes(); // Representatives: numPerformanceReps = numReps; ResetRepresentativesList(); historicGenomePool.Add(agentGenomeList[0]); // init fitnessManager = new FitnessManager(); SetUpDefaultFitnessComponents(challengeType, fitnessManager); fitnessManager.ResetHistoricalData(); fitnessManager.ResetCurrentHistoricalDataLists(); fitnessManager.InitializeForNewGeneration(agentGenomeList.Count); trainingSettingsManager = new TrainingSettingsManager(0.01f, 0.8f, 0.2f, 0.005f); }
//public int numBaselineReps = 0; public EnvironmentPopulation(EnvironmentGenome templateGenome, int numGenomes, int numReps) { this.templateGenome = templateGenome; popSize = numGenomes; //this.numBaseline = numBaseline; environmentGenomeList = new List <EnvironmentGenome>(); historicGenomePool = new List <EnvironmentGenome>(); //baselineGenomePool = new List<EnvironmentGenome>(); for (int e = 0; e < numGenomes; e++) { // Create new environmentGenome EnvironmentGenome envGenome = new EnvironmentGenome(e); envGenome.InitializeRandomGenomeFromTemplate(templateGenome); // Add to envGenomesList: environmentGenomeList.Add(envGenome); // Create parallel initial batch of genomes to be used as baseline comparison //EnvironmentGenome baseGenome = new EnvironmentGenome(e); //baseGenome.InitializeRandomGenomeFromTemplate(templateGenome); //baselineGenomePool.Add(baseGenome); } //AppendBaselineGenomes(); // Representatives: numPerformanceReps = numReps; ResetRepresentativesList(); historicGenomePool.Add(environmentGenomeList[0]); // init fitnessManager = new FitnessManager(); SetUpDefaultFitnessComponents(fitnessManager); //fitnessManager.ResetHistoricalData(); fitnessManager.InitializeForNewGeneration(environmentGenomeList.Count); trainingSettingsManager = new TrainingSettingsManager(0.25f, 0.05f, 0f, 0f); }
public void SetToMutatedCopyOfParentGenome(BrainGenome parentGenome, TrainingSettingsManager settings) { this.bodyNeuronList = parentGenome.bodyNeuronList; // UNSUSTAINABLE!!! might work now since all neuronLists are identical // Alternate: SetBodyNeuronsFromTemplate(BodyGenome templateBody); // Existing Hidden Neurons!! hiddenNeuronList = new List <NeuronGenome>(); for (int i = 0; i < parentGenome.hiddenNeuronList.Count; i++) { NeuronGenome newHiddenNeuronGenome = new NeuronGenome(parentGenome.hiddenNeuronList[i]); // create new neuron as a copy of parent neuron // Might be able to simply copy hiddenNeuronList or individual hiddenNeuronGenomes from parent if they are functionally identical... // for now going with the thorough approach of a reference-less copy hiddenNeuronList.Add(newHiddenNeuronGenome); } // Existing Links!! linkList = new List <LinkGenome>(); for (int i = 0; i < parentGenome.linkList.Count; i++) { LinkGenome newLinkGenome = new LinkGenome(parentGenome.linkList[i].fromModuleID, parentGenome.linkList[i].fromNeuronID, parentGenome.linkList[i].toModuleID, parentGenome.linkList[i].toNeuronID, parentGenome.linkList[i].weight, true); float randChance = UnityEngine.Random.Range(0f, 1f); if (randChance < settings.mutationChance) { float randomWeight = Gaussian.GetRandomGaussian(); newLinkGenome.weight = Mathf.Lerp(newLinkGenome.weight, randomWeight, settings.mutationStepSize); } linkList.Add(newLinkGenome); } // Add Brand New Link: // float randLink = UnityEngine.Random.Range(0f, 1f); if (randLink < settings.newLinkChance) { List <NeuronGenome> inputNeuronList = new List <NeuronGenome>(); //List<NeuronGenome> hiddenNeuronList = new List<NeuronGenome>(); List <NeuronGenome> outputNeuronList = new List <NeuronGenome>(); for (int j = 0; j < bodyNeuronList.Count; j++) { if (bodyNeuronList[j].neuronType == NeuronGenome.NeuronType.In) { inputNeuronList.Add(bodyNeuronList[j]); } if (bodyNeuronList[j].neuronType == NeuronGenome.NeuronType.Out) { outputNeuronList.Add(bodyNeuronList[j]); } } for (int j = 0; j < hiddenNeuronList.Count; j++) { inputNeuronList.Add(hiddenNeuronList[j]); outputNeuronList.Add(hiddenNeuronList[j]); } // Try x times to find new connection -- random scattershot approach at first: // other methods: // -- make sure all bodyNeurons are fully-connected when modifying body int maxChecks = 8; for (int k = 0; k < maxChecks; k++) { int randID = UnityEngine.Random.Range(0, inputNeuronList.Count); NID fromNID = inputNeuronList[randID].nid; randID = UnityEngine.Random.Range(0, outputNeuronList.Count); NID toNID = outputNeuronList[randID].nid; // check if it exists: bool linkExists = false; for (int l = 0; l < linkList.Count; l++) { if (linkList[l].fromModuleID == fromNID.moduleID && linkList[l].fromNeuronID == fromNID.neuronID && linkList[l].toModuleID == toNID.moduleID && linkList[l].toNeuronID == toNID.neuronID) { linkExists = true; break; } } if (linkExists) { } else { float randomWeight = Gaussian.GetRandomGaussian() * 0f; LinkGenome linkGenome = new LinkGenome(fromNID.moduleID, fromNID.neuronID, toNID.moduleID, toNID.neuronID, randomWeight, true); //Debug.Log("New Link! from: [" + fromNID.moduleID.ToString() + ", " + fromNID.neuronID.ToString() + "], to: [" + toNID.moduleID.ToString() + ", " + toNID.neuronID.ToString() + "]"); linkList.Add(linkGenome); break; } } } // Add Brand New Hidden Neuron: float randNeuronChance = UnityEngine.Random.Range(0f, 1f); if (randNeuronChance < settings.newHiddenNodeChance) { // find a link and expand it: int randLinkID = UnityEngine.Random.Range(0, linkList.Count); // create new neuron NeuronGenome newNeuronGenome = new NeuronGenome(NeuronGenome.NeuronType.Hid, -1, hiddenNeuronList.Count); hiddenNeuronList.Add(newNeuronGenome); // create 2 new links LinkGenome linkGenome1 = new LinkGenome(linkList[randLinkID].fromModuleID, linkList[randLinkID].fromNeuronID, newNeuronGenome.nid.moduleID, newNeuronGenome.nid.neuronID, 1f, true); LinkGenome linkGenome2 = new LinkGenome(newNeuronGenome.nid.moduleID, newNeuronGenome.nid.neuronID, linkList[randLinkID].toModuleID, linkList[randLinkID].toNeuronID, linkList[randLinkID].weight, true); // delete old link linkList.RemoveAt(randLinkID); // add new links linkList.Add(linkGenome1); linkList.Add(linkGenome2); //Debug.Log("New Neuron! " + newNeuronGenome.nid.neuronID.ToString() + " - from: [" + linkGenome1.fromModuleID.ToString() + ", " + linkGenome1.fromNeuronID.ToString() + "], to: [" + linkGenome2.toModuleID.ToString() + ", " + linkGenome2.toNeuronID.ToString() + "]"); } }