/// <summary> /// Destroy all agents in the area (that belongs to this script) and creates them again. /// The list of newly created objects is returned. /// </summary> /// <returns></returns> public List <AgentLogic> RegenerateObjects() { for (int i = transform.childCount - 1; i >= 0; --i) { DestroyImmediate(transform.GetChild(i).gameObject); } _activeAgents = new List <AgentLogic>(); for (uint i = 0; i < count; i++) { AgentLogic created = Instantiate <AgentLogic>(agentsToBeCreated[Random.Range(0, agentsToBeCreated.Length)], GenerateUtils.GetRandomPositionInWorldBounds(_bounds), GenerateUtils.GetRandomRotation(randomRotationMinimal, randomRotationMaximal)); created.transform.parent = transform; _activeAgents.Add(created); } return(_activeAgents); }
/// <summary> /// Compares the points of two agents. When used on Sort function will make the highest performance to be on top. /// </summary> /// <param name="obj"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public int CompareTo(object obj) { if (obj == null) { return(1); } AgentLogic otherAgent = obj as AgentLogic; if (otherAgent != null) { return(otherAgent.GetPerformance().CompareTo(GetPerformance())); } else { throw new ArgumentException("Object is not an AgentLogic"); } }
/// <summary> /// Creates a new generation by using GenerateBoxes and GenerateBoats/Pirates. /// Previous generations will be removed and the best parents will be selected and used to create the new generation. /// The best parents (top 1) of the generation will be stored as a Prefab in the [savePrefabsAt] folder. Their name /// will use the [generationCount] as an identifier. /// </summary> public void MakeNewGeneration() { GenerateObjects(); foreach (GenerateAgentsInArea agentGenerator in agentsGenerators) { List <AgentLogic> agentLogics = agentGenerator.GetActiveAgents(); agentLogics.RemoveAll(logic => logic == null); agentLogics.ForEach(logic => logic.CalculatePerformance(environment)); if (agentLogics.Count > 0) { agentLogics.Sort(); uint generated = agentGenerator.GetCount(); uint actualParentsSize = (uint)Mathf.Min(agentLogics.Count, Mathf.Min(generated, parentSize)); AgentLogic[] parents = new AgentLogic[actualParentsSize]; for (int i = 0; i < actualParentsSize; i++) { parents[i] = agentLogics[i]; } AgentLogic lastWinner = parents[0]; lastWinner.name += generationName + "[" + generationCount + "]"; PrefabUtility.SaveAsPrefabAsset(lastWinner.gameObject, savePrefabsAt + lastWinner.name + ".prefab"); List <AgentLogic> newAgents = agentGenerator.RegenerateObjects(); newAgents.ForEach(agent => { AgentLogic parent = parents[Random.Range(0, (int)actualParentsSize)]; agent.Birth(parent.GetData()); agent.Mutate(mutationFactor, mutationChance); agent.WakeUp(); }); } else { agentGenerator.RegenerateObjects().ForEach(agent => { agent.WakeUp(); }); } } }
public abstract float EvaluatePerfomance(AgentLogic agent, Environment environment);