public LinkGenome(NeuronGenome from, NeuronGenome to, float weight, bool enabled) { this.from = from; this.to = to; this.weight = weight; this.enabled = enabled; }
/// Finds a link and expands it void AddNewHiddenNeuron() { if (linkCount == 0) { Debug.LogError("Cannot create new hidden neuron because link list count is zero"); return; } int randLinkID = Random.Range(0, linkCount); // Create new neuron //NeuronGenome newNeuronGenome = new NeuronGenome("HidNew", NeuronType.Hid, BrainModuleID.Undefined, hiddenNeurons.Count); var newHiddenNeuron = new NeuronGenome(hiddenTemplate, inOutNeurons.Count + hiddenNeurons.Count); hiddenNeurons.Add(newHiddenNeuron); // create 2 new links LinkGenome linkGenome1 = new LinkGenome(links[randLinkID].from, newHiddenNeuron, 1f, true); LinkGenome linkGenome2 = new LinkGenome(newHiddenNeuron, links[randLinkID].to, links[randLinkID].weight, true); // delete old link links[randLinkID].enabled = false; // *** not needed? **** links.RemoveAt(randLinkID); // add new links links.Add(linkGenome1); links.Add(linkGenome2); //Debug.Log("New Neuron! " + newNeuronGenome.nid.neuronID.ToString() + // " - from: [" + linkGenome1.fromModuleID.ToString() + ", " + // linkGenome1.fromNeuronID.ToString() + "], to: [" + linkGenome2.toModuleID.ToString() + // ", " + linkGenome2.toNeuronID.ToString() + "]"); }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { if (useGravityDir) { NeuronGenome neuronX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); NeuronGenome neuronY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 1); NeuronGenome neuronZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 2); neuronList.Add(neuronX); neuronList.Add(neuronY); neuronList.Add(neuronZ); } if (useVel) { NeuronGenome neuronVelX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 3); NeuronGenome neuronVelY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 4); NeuronGenome neuronVelZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 5); neuronList.Add(neuronVelX); neuronList.Add(neuronVelY); neuronList.Add(neuronVelZ); } if (useAltitude) { NeuronGenome neuronAltitude = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 6); neuronList.Add(neuronAltitude); } }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { NeuronGenome neuron1 = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); NeuronGenome neuron2 = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 1); neuronList.Add(neuron1); neuronList.Add(neuron2); }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { NeuronGenome health = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); NeuronGenome takingDamage = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 1); neuronList.Add(health); neuronList.Add(takingDamage); }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { NeuronGenome neuronX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); NeuronGenome neuronY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 1); NeuronGenome neuronZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 2); neuronList.Add(neuronX); neuronList.Add(neuronY); neuronList.Add(neuronZ); }
/// Copy existing NeuronGenome public NeuronGenome(NeuronGenome original) { if (original == null) { Debug.LogError("Attempting to copy null NeuronGenome"); } data = original.data; index = original.index; }
public void InitializeAxons() { for (int i = 0; i < initialHiddenNeuronCount; i++) { var neuron = new NeuronGenome(hiddenTemplate, inOutNeurons.Count + i); hiddenNeurons.Add(neuron); } LinkLayers(inputNeurons, outputNeurons, initialConnectionChance, initialWeightMultiplier); LinkLayers(inputNeurons, hiddenNeurons, initialConnectionChance, initialWeightMultiplier); LinkLayers(hiddenNeurons, outputNeurons, initialConnectionChance, initialWeightMultiplier); }
Neuron GetMatchingNeuron(List <Neuron> list, NeuronGenome genome) { foreach (var item in list) { if (item.IsMe(genome)) { return(item); } } return(null); }
bool LinkExists(NeuronGenome from, NeuronGenome to) { foreach (var link in links) { if (link.from == from && link.to == to) { return(true); } } return(false); }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { NeuronGenome throttle = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 0); NeuronGenome steerAngle = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 1); NeuronGenome brake = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 2); //NeuronGenome speed = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 3); neuronList.Add(throttle); neuronList.Add(steerAngle); neuronList.Add(brake); //neuronList.Add(speed); }
/// 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 void InitializeBrainGenome(List <NeuronGenome> neuronList) { if (useX) { NeuronGenome neuronX = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 0); neuronList.Add(neuronX); } if (useY) { NeuronGenome neuronY = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 1); neuronList.Add(neuronY); } if (useZ) { NeuronGenome neuronZ = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 2); neuronList.Add(neuronZ); } }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { NeuronGenome neuronLeft = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); NeuronGenome neuronLeftCenter = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 1); NeuronGenome neuronCenter = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 2); NeuronGenome neuronRightCenter = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 3); NeuronGenome neuronRight = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 4); NeuronGenome neuronBack = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 5); NeuronGenome neuronCenterShort = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 6); neuronList.Add(neuronLeft); neuronList.Add(neuronLeftCenter); neuronList.Add(neuronCenter); neuronList.Add(neuronRightCenter); neuronList.Add(neuronRight); neuronList.Add(neuronBack); neuronList.Add(neuronCenterShort); }
void SortIONeuron(NeuronGenome neuron) { switch (neuron.data.io) { case NeuronType.In: if (!inputNeurons.Contains(neuron)) { inputNeurons.Add(neuron); } break; case NeuronType.Out: if (!outputNeurons.Contains(neuron)) { outputNeurons.Add(neuron); } break; } }
List <NeuronGenome> MutateHiddenNeurons(List <NeuronGenome> original) { var result = new List <NeuronGenome>(); foreach (var element in original) { var newHiddenNeuron = new NeuronGenome(element); result.Add(newHiddenNeuron); // Create new neuron as a copy of parent neuron //NeuronGenome newHiddenNeuronGenome = new NeuronGenome(parentGenome.hiddenNeurons[i]); // 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 //hiddenNeurons.Add(newHiddenNeuronGenome); } return(result); }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { if (useX) { NeuronGenome neuronX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); neuronList.Add(neuronX); if (useVel) { NeuronGenome neuronVelX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 1); neuronList.Add(neuronVelX); } } if (useY) { NeuronGenome neuronY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 2); neuronList.Add(neuronY); if (useVel) { NeuronGenome neuronVelY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 3); neuronList.Add(neuronVelY); } } if (useZ) { NeuronGenome neuronZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 4); neuronList.Add(neuronZ); if (useVel) { NeuronGenome neuronVelZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 5); neuronList.Add(neuronVelZ); } } if (useDist) { NeuronGenome neuronDist = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 6); neuronList.Add(neuronDist); } if (useInvDist) { NeuronGenome neuronInvDist = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 7); neuronList.Add(neuronInvDist); } }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { NeuronGenome bias = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); //NeuronGenome ownPosX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 1); //NeuronGenome ownPosY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 2); NeuronGenome ownVelX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 3); NeuronGenome ownVelY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 4); NeuronGenome enemyPosX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 5); NeuronGenome enemyPosY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 6); NeuronGenome enemyVelX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 7); NeuronGenome enemyVelY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 8); NeuronGenome enemyDirX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 9); NeuronGenome enemyDirY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 10); NeuronGenome distLeft = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 11); NeuronGenome distRight = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 12); NeuronGenome distUp = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 13); NeuronGenome distDown = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 14); NeuronGenome throttleX = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 15); NeuronGenome throttleY = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 16); neuronList.Add(bias); //neuronList.Add(ownPosX); //neuronList.Add(ownPosY); neuronList.Add(ownVelX); neuronList.Add(ownVelY); neuronList.Add(enemyPosX); neuronList.Add(enemyPosY); neuronList.Add(enemyVelX); neuronList.Add(enemyVelY); neuronList.Add(enemyDirX); neuronList.Add(enemyDirY); neuronList.Add(distLeft); neuronList.Add(distRight); neuronList.Add(distUp); neuronList.Add(distDown); neuronList.Add(throttleX); neuronList.Add(throttleY); }
/// For neurons created post-initialization, such as from mutations public void LinkNeuronToLayer(NeuronGenome neuron) { switch (neuron.io) { case NeuronType.In: //Debug.Log($"Requesting connections from {neuron.data.name} to {outputNeurons.Count} output neurons"); foreach (var outputNeuron in outputNeurons) { RequestConnection(neuron, outputNeuron, initialConnectionChance, initialWeightMultiplier); } break; case NeuronType.Out: //Debug.Log($"Requesting connections from input neurons {inputNeurons.Count} to {neuron.data.name}"); foreach (var inputNeuron in inputNeurons) { RequestConnection(inputNeuron, neuron, initialConnectionChance, initialWeightMultiplier); } break; } }
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() + "]"); } }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { if (useX) { if (useMotors) { NeuronGenome throttleX = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 0); neuronList.Add(throttleX); } if (angleSensors) { NeuronGenome angleX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 3); neuronList.Add(angleX); } if (velocitySensors) { NeuronGenome velX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 6); neuronList.Add(velX); } } if (useY) { if (useMotors) { NeuronGenome throttleY = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 1); neuronList.Add(throttleY); } if (angleSensors) { NeuronGenome angleY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 4); neuronList.Add(angleY); } if (velocitySensors) { NeuronGenome velY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 7); neuronList.Add(velY); } } if (useZ) { if (useMotors) { NeuronGenome throttleZ = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 2); neuronList.Add(throttleZ); } if (angleSensors) { NeuronGenome angleZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 5); neuronList.Add(angleZ); } if (velocitySensors) { NeuronGenome velZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 8); neuronList.Add(velZ); } } if (positionSensors) { NeuronGenome posX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 9); neuronList.Add(posX); NeuronGenome posY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 10); neuronList.Add(posY); NeuronGenome posZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 11); neuronList.Add(posZ); } if (quaternionSensors) { NeuronGenome quatX = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 12); neuronList.Add(quatX); NeuronGenome quatY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 13); neuronList.Add(quatY); NeuronGenome quatZ = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 14); neuronList.Add(quatZ); NeuronGenome quatW = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 15); neuronList.Add(quatW); } if (usePistonY) { NeuronGenome pistonThrottleY = new NeuronGenome(NeuronGenome.NeuronType.Out, inno, 16); neuronList.Add(pistonThrottleY); NeuronGenome pistonPosY = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 17); neuronList.Add(pistonPosY); } }
public void InitializeAxons(float initialWeightMultiplier) { int numInputs = 0; for (int i = 0; i < bodyNeuronList.Count; i++) { if (bodyNeuronList[i].neuronType == NeuronGenome.NeuronType.In) { numInputs++; } } // Create Hidden nodes TEMP!!!! for (int i = 0; i < numInputs; i++) { NeuronGenome neuron = new NeuronGenome(NeuronGenome.NeuronType.Hid, -1, i); hiddenNeuronList.Add(neuron); } // Create initial connections -- : List <NeuronGenome> inputNeuronList = new List <NeuronGenome>(); //List<NeuronGenome> hiddenNeuronList = new List<NeuronGenome>(); List <NeuronGenome> outputNeuronList = new List <NeuronGenome>(); for (int i = 0; i < bodyNeuronList.Count; i++) { if (bodyNeuronList[i].neuronType == NeuronGenome.NeuronType.In) { inputNeuronList.Add(bodyNeuronList[i]); } //if (brainGenome.neuronList[i].neuronType == NeuronGenome.NeuronType.Hid) { // hiddenNeuronList.Add(brainGenome.neuronList[i]); //} if (bodyNeuronList[i].neuronType == NeuronGenome.NeuronType.Out) { outputNeuronList.Add(bodyNeuronList[i]); } } // Initialize fully connected with all weights Random for (int i = 0; i < outputNeuronList.Count; i++) { for (int j = 0; j < inputNeuronList.Count; j++) { float randomWeight = Gaussian.GetRandomGaussian() * initialWeightMultiplier; LinkGenome linkGenome = new LinkGenome(inputNeuronList[j].nid.moduleID, inputNeuronList[j].nid.neuronID, outputNeuronList[i].nid.moduleID, outputNeuronList[i].nid.neuronID, randomWeight, true); linkList.Add(linkGenome); } } for (int i = 0; i < outputNeuronList.Count; i++) { for (int j = 0; j < hiddenNeuronList.Count; j++) { float randomWeight = Gaussian.GetRandomGaussian() * initialWeightMultiplier; LinkGenome linkGenome = new LinkGenome(hiddenNeuronList[j].nid.moduleID, hiddenNeuronList[j].nid.neuronID, outputNeuronList[i].nid.moduleID, outputNeuronList[i].nid.neuronID, randomWeight, true); linkList.Add(linkGenome); } } for (int i = 0; i < hiddenNeuronList.Count; i++) { for (int j = 0; j < inputNeuronList.Count; j++) { float randomWeight = Gaussian.GetRandomGaussian() * initialWeightMultiplier; LinkGenome linkGenome = new LinkGenome(inputNeuronList[j].nid.moduleID, inputNeuronList[j].nid.neuronID, hiddenNeuronList[i].nid.moduleID, hiddenNeuronList[i].nid.neuronID, randomWeight, true); linkList.Add(linkGenome); } } //PrintBrainGenome(); //Debug.Log("numAxons: " + linkList.Count.ToString()); }
public NeuronGenome(NeuronGenome template) { neuronType = template.neuronType; nid = template.nid; }
public void InitializeBrainGenome(List <NeuronGenome> neuronList) { NeuronGenome neuron = new NeuronGenome(NeuronGenome.NeuronType.In, inno, 0); neuronList.Add(neuron); }