private void UpdatePool(GAAgent agent, int i) { if (!agent.isAddedToPool) { if (pool.name.Equals("Prey Pool")) { Debug.Log("Removing " + i); } agent.Disable(); agent.isAddedToPool = true; m_AgentPool.Add(agent); m_AgentPool.Sort(); agent.owner.transform.SetParent(pool.transform); if (m_AgentPool.Count > m_MaxPoolSize) { MonoBehaviour.Destroy(m_AgentPool [m_AgentPool.Count - 1].owner); m_AgentPool.RemoveAt(m_AgentPool.Count - 1); } if (pool.name.Equals("Prey Pool")) { Debug.Log("Pool size " + m_AgentPool.Count); } } }
private GAAgent FitnessProportionateSelection() { float randomSlice = Utilities.instance.RandomMinMax(0, m_TotalFitnessScore); GAAgent choosenAgent = null; float fitnessTotal = 0; for (int i = 0; i < m_AgentPool.Count; i++) { fitnessTotal += m_AgentPool[i].timeAlive; if (fitnessTotal > randomSlice) { choosenAgent = m_AgentPool[i]; break; } if (pool.name.Equals("Prey Pool")) { Debug.Log("Chose " + i + " of " + m_AgentPool.Count); } } return(choosenAgent); }
public NeuralNet CrossOver(GAAgent parentOne, GAAgent parentTwo) { NeuralNet neuralNet = new NeuralNet(m_AnnInput, m_AnnOutput, m_AnnHiddenLayers, m_AnnNeuronsPerHiddenLayer); List <float> newWeights = new List <float> (); List <float> parentOneWeights = parentOne.GetNeuralNetwork().GetWeights(); List <float> parentTwoWeights = parentTwo.GetNeuralNetwork().GetWeights(); int crossOverPoint; if (m_RandomCrossoverPoint) { crossOverPoint = (int)Utilities.instance.RandomMinMax(0, parentOneWeights.Count); } else { crossOverPoint = (int)(parentOneWeights.Count * 0.5f); } for (int i = 0; i < crossOverPoint; i++) { newWeights.Add(parentOneWeights [i]); } for (int i = crossOverPoint; i < parentOneWeights.Count; i++) { newWeights.Add(parentTwoWeights [i]); } neuralNet.SetWeights(newWeights); return(neuralNet); }
private void CreateAgentFromCrossover(GAAgent parentOne, GAAgent parentTwo, int index) { NeuralNet neuralNetwork = CrossOver(parentOne, parentTwo); var pos = new Vector2(Random.Range(-10f, 10f), Random.Range(-10f, 10f)); var agent = (GameObject)MonoBehaviour.Instantiate(m_AgentPrefab, pos, Quaternion.identity); m_Agents [index] = agent.GetComponent <GAAgent> (); m_Agents [index].SetNeuralNetwork(neuralNetwork); if (Utilities.instance.RandomMinMax(0f, 1f) < m_MutationChance) { m_Agents [index].Mutate(); } UpdateGenerationNumber(); }
void Awake() { m_Agent = GetComponent <GAAgent>(); }