public AgentGenome Mutate(AgentGenome parentGenome, bool bodySettings, bool brainSettings) { //AgentGenome parentGenome = leaderboardGenomesList[selectedIndex].candidateGenome; //***EAC Creature NAME mutation here??? string parentName = parentGenome.name; int randIndex = Random.Range(0, parentName.Length - 1); string frontHalf = parentName.Substring(0, randIndex); string middleChar = parentName.Substring(randIndex, 1); string backHalf = parentName.Substring(randIndex + 1); if (RandomStatics.CoinToss(.05f)) { middleChar = RandomStatics.GetRandomLetter(); } frontHalf += middleChar; string newName = RandomStatics.CoinToss(.025f) ? backHalf + frontHalf : frontHalf + backHalf; //-------------------------------------------------------------------------------------------------------------------- tempMutationSettings = bodySettings ? mutationSettings : cachedNoneMutationSettings; BodyGenome newBodyGenome = new BodyGenome(parentGenome.bodyGenome, tempMutationSettings); tempMutationSettings = brainSettings ? mutationSettings : cachedNoneMutationSettings; BrainGenome newBrainGenome = new BrainGenome(parentGenome.brainGenome, newBodyGenome, tempMutationSettings); return new AgentGenome(newBodyGenome, newBrainGenome, parentGenome.generationCount + 1, newName); }
List <LinkGenome> MutateLinks(List <LinkGenome> original, MutationSettingsInstance settings) { var result = new List <LinkGenome>(); foreach (var element in original) { LinkGenome newLinkGenome = new LinkGenome(element.from, element.to, element.weight, true); // Remove fully??? ***** if (RandomStatics.CoinToss(settings.brainRemoveLinkChance)) { newLinkGenome.weight = 0f; } if (RandomStatics.CoinToss(settings.brainWeightMutationChance)) { float randomWeight = Gaussian.GetRandomGaussian(); newLinkGenome.weight += Mathf.Lerp(0f, randomWeight, settings.brainWeightMutationStepSize); } newLinkGenome.weight *= settings.brainWeightDecayAmount; result.Add(newLinkGenome); } return(result); }
public static bool GetMutatedBool(bool curValue, float mutationChance) { bool mutatedValue = curValue; if (RandomStatics.CoinToss(mutationChance)) { mutatedValue = RandomStatics.CoinToss(); } return(mutatedValue); }
void FixedUpdate() { if (!active) { refactoryCounter++; } else if (RandomStatics.CoinToss(resetChance)) { active = false; refactoryCounter = 0; } }
/// Apply a random chance of connecting two neurons with a random weight void RequestConnection(NeuronGenome from, NeuronGenome to, float connectionChance, float weightMultiplier) { var connectNeurons = RandomStatics.CoinToss(connectionChance); if (!connectNeurons) { return; } var randomWeight = Gaussian.GetRandomGaussian() * weightMultiplier; var linkGenome = new LinkGenome(from, to, randomWeight, true); links.Add(linkGenome); }
public UnlockedTech GetInitialUnlocks() { List <TechElement> values = new List <TechElement>(); foreach (var value in potentialValues) { if (RandomStatics.CoinToss(value.probability)) { values.Add(value.value); } } return(new UnlockedTech(values)); }
public static int GetMutatedIntAdditive(int curValue, float mutationChance, int maxMutationSize, int minValue, int maxValue) { int mutatedValue = curValue; if (RandomStatics.CoinToss(mutationChance)) { int randomPerturbation = Random.Range(-maxMutationSize, maxMutationSize + 1); mutatedValue += randomPerturbation; mutatedValue = Mathf.Max(mutatedValue, minValue); mutatedValue = Mathf.Min(mutatedValue, maxValue); } return(mutatedValue); }
public static float GetMutatedFloatAdditive(float curValue, float mutationChance, float mutationStepSize, float minValue, float maxValue) { float mutatedValue = curValue; if (RandomStatics.CoinToss(mutationChance)) { float randomPerturbation = Gaussian.GetRandomGaussian(); mutatedValue += Mathf.Lerp(0f, randomPerturbation, mutationStepSize); mutatedValue = Mathf.Max(mutatedValue, minValue); mutatedValue = Mathf.Min(mutatedValue, maxValue); } return(mutatedValue); }
public void SetToMutatedCopyOfParentGenome(BrainGenome parentGenome, BodyGenome bodyGenome, MutationSettingsInstance settings) { /* * //this.bodyNeuronList = parentGenome.bodyNeuronList; // UNSUSTAINABLE!!! might work now since all neuronLists are identical ****** * * // Copy from parent brain or rebuild neurons from scratch based on the new mutated bodyGenome???? --- ****** * for(int i = 0; i < parentGenome.bodyNeuronList.Count; i++) { * NeuronGenome newBodyNeuronGenome = new NeuronGenome(parentGenome.bodyNeuronList[i]); * this.bodyNeuronList.Add(newBodyNeuronGenome); * } * // Alternate: SetBodyNeuronsFromTemplate(BodyGenome templateBody); */ // Rebuild BodyNeuronGenomeList from scratch based on bodyGenome InitializeBodyNeuronList(); InitializeIONeurons(bodyGenome); hiddenNeurons = MutateHiddenNeurons(parentGenome.hiddenNeurons); links = MutateLinks(parentGenome.links, settings); if (RandomStatics.CoinToss(settings.brainCreateNewLinkChance)) { AddNewLink(settings); } if (RandomStatics.CoinToss(settings.brainCreateNewHiddenNodeChance)) { AddNewHiddenNeuron(); } var newNeurons = GetNewlyUnlockedNeurons(bodyGenome); //Debug.Log($"Initializing axons with {newNeurons.Count} new neurons from " + // $"{bodyGenome.newlyUnlockedNeuronInfo.Count} new tech."); foreach (var neuron in newNeurons) { SortIONeuron(neuron); LinkNeuronToLayer(neuron); } //RemoveVestigialLinks(); }
public UnlockedTech GetMutatedCopy() { var copy = new UnlockedTech(this); var removed = new List <TechElement>(); var added = new List <TechElement>(); // Random chance of removing a tech if it is not the prerequisite of some other tech the agent has. foreach (var value in values) { if (!value.IsPrerequisite(values) && RandomStatics.CoinToss(value.mutationLockChance)) { removed.Add(value); } } foreach (var remove in removed) { values.Remove(remove); } // Random chance of adding a tech if its prerequisite is met and the agent doesn't already have it. foreach (var value in values) { foreach (var tech in value.nextTech) { if (RandomStatics.CoinToss(tech.mutationUnlockChance) && !values.Contains(tech)) { added.Add(tech); } } } foreach (var add in added) { copy.values.Add(add); } return(copy); }
public SpeciesGenomePool SelectNewGenomeSourceSpecies(bool weighted, float weightedAmount) { if (weighted) { // Filter Out species which are flagged for extinction?!?!?! // figure out which species has most evals int totalNumActiveEvals = 0; //int evalLeaderActiveIndex = 0; int recordNumEvals = 0; foreach (var idList in currentlyActiveSpeciesIDList) { int numEvals = completeSpeciesPoolsList[idList].numAgentsEvaluated + 1; totalNumActiveEvals += numEvals; if (numEvals > recordNumEvals) { recordNumEvals = numEvals; //evalLeaderActiveIndex = i; } } float[] unsortedEvalScoresArray = new float[currentlyActiveSpeciesIDList.Count]; float[] rankedEvalScoresArray = new float[currentlyActiveSpeciesIDList.Count]; int[] rankedEvalIndices = new int[currentlyActiveSpeciesIDList.Count]; //float weightedAmount = 0.5f; float avgFractionVal = 1f / currentlyActiveSpeciesIDList.Count; for (int i = 0; i < currentlyActiveSpeciesIDList.Count; i++) { float rawEvalFraction = 1f - (((float)completeSpeciesPoolsList[currentlyActiveSpeciesIDList[i]].numAgentsEvaluated + 1f) / (float)totalNumActiveEvals); unsortedEvalScoresArray[i] = Mathf.Lerp(avgFractionVal, rawEvalFraction, weightedAmount); rankedEvalScoresArray[i] = unsortedEvalScoresArray[i]; rankedEvalIndices[i] = i; } // SORT ARRAY BY #EVALS: for (int i = 0; i < rankedEvalIndices.Length - 1; i++) { for (int j = 0; j < rankedEvalIndices.Length - 1; j++) { float swapFitA = rankedEvalScoresArray[j]; float swapFitB = rankedEvalScoresArray[j + 1]; int swapIdA = rankedEvalIndices[j]; int swapIdB = rankedEvalIndices[j + 1]; // bigger is better now after inversion if (swapFitA < swapFitB) { rankedEvalScoresArray[j] = swapFitB; rankedEvalScoresArray[j + 1] = swapFitA; rankedEvalIndices[j] = swapIdB; rankedEvalIndices[j + 1] = swapIdA; } } } int selectedIndex = 0; // generate random lottery value between 0f and totalFitness: float lotteryValue = Random.Range(0f, 1f); float currentValue = 0f; for (int i = 0; i < rankedEvalIndices.Length; i++) { if (lotteryValue >= currentValue && lotteryValue < (currentValue + rankedEvalScoresArray[i])) { // Jackpot! selectedIndex = rankedEvalIndices[i]; //Debug.Log("Selected: " + selectedIndex.ToString() + "! (" + i.ToString() + ") fit= " + currentValue.ToString() + "--" + (currentValue + (1f - rankedFitnessList[i])).ToString() + " / " + totalFitness.ToString() + ", lotto# " + lotteryValue.ToString() + ", fit= " + (1f - rankedFitnessList[i]).ToString()); } currentValue += rankedEvalIndices[i]; // add this agent's fitness to current value for next check } return(completeSpeciesPoolsList[currentlyActiveSpeciesIDList[selectedIndex]]); } // NAIVE RANDOM AT FIRST: // filter flagged extinct species: List <int> eligibleSpeciesIDList = new List <int>(); foreach (var id in currentlyActiveSpeciesIDList) { if (!completeSpeciesPoolsList[id].isFlaggedForExtinction) { eligibleSpeciesIDList.Add(id); } } int randomTableIndex = Random.Range(0, eligibleSpeciesIDList.Count); // temp minor penalty to oldest species: if (randomTableIndex == 0 && RandomStatics.CoinToss(oldestSpeciesRerollChance)) { randomTableIndex = Random.Range(0, eligibleSpeciesIDList.Count); } int speciesIndex = eligibleSpeciesIDList[randomTableIndex]; return(completeSpeciesPoolsList[speciesIndex]); }