override internal void AgentHasArrived(AgentState agentState) { TurnBasedAgent a = Activator.CreateInstance(agentState.AgentType) as TurnBasedAgent; a.LoadState(agentState); a.Name = agentState.Name; a.MustRunSetup = true; Add(a); }
/// <summary> /// Stops the execution of the agent and removes it from the environment. Use the Remove method instead of Agent.Stop /// when the decision to stop an agent does not belong to the agent itself, but to some other agent or to an external factor. /// </summary> /// <param name="agent">The agent to be removed</param> public void Remove(TurnBasedAgent agent) { if (Agents.Contains(agent)) { Agents.Remove(agent); AgentsDict.Remove(agent.Name); } else { throw new Exception("Agent " + agent.Name + " does not exist (TurnBasedAgent.Remove)"); } }
/// <summary> /// Stops the execution of the agent identified by name and removes it from the environment. Use the Remove method instead of Agent.Stop /// when the decision to stop an agent does not belong to the agent itself, but to some other agent or to an external factor. /// </summary> /// <param name="agentName">The name of the agent to be removed</param> override public void Remove(string agentName) { if (AgentsDict.ContainsKey(agentName)) { TurnBasedAgent ag = AgentsDict[agentName]; Agents.Remove(ag); AgentsDict.Remove(agentName); } else { throw new Exception("Agent " + agentName + " does not exist (TurnBasedAgent.Remove)"); } }
/// <summary> /// Adds an agent to the environment. Its name should be unique. /// </summary> /// <param name="agent">The concurrent agent that will be added</param> /// <param name="name">The name of the agent</param> public void Add(TurnBasedAgent agent, string name) { if (AgentsDict.ContainsKey(name)) { throw new Exception("Trying to add an agent with an existing name: " + name + " (TurnBasedEnvironment.Add(agent, name))"); } else { agent.Name = name; agent.Environment = this; Agents.Add(agent); AgentsDict[name] = agent; } }
/// <summary> /// Adds an agent to the environment. The agent should already have a name and its name should be unique. /// </summary> /// <param name="agent">The concurrent agent that will be added</param> public void Add(TurnBasedAgent agent) { if (agent.Name == null || agent.Name == "") { throw new Exception("Trying to add an agent without a name (TurnBasedEnvironment.Add(agent))"); } if (AgentsDict.ContainsKey(agent.Name)) { throw new Exception("Trying to add an agent with an existing name: " + agent.Name + " (TurnBasedEnvironment.Add(Agent))"); } agent.Environment = this; AgentsDict[agent.Name] = agent; Agents.Add(agent); }
private void RunTurn(int turn) { int[] agentOrder = null; if (_randomOrder) { agentOrder = RandomPermutation(this.NoAgents); } else { agentOrder = SortedPermutation(this.NoAgents); } // address situations when the agent list changes during the turn TurnBasedAgent[] agentsCopy = new TurnBasedAgent[this.NoAgents]; Agents.CopyTo(agentsCopy); for (int i = 0; i < agentsCopy.Length; i++) { int aoi = agentOrder[i]; if (agentsCopy[aoi] != null && Agents.Contains(agentsCopy[aoi])) // agent not stopped { if (agentsCopy[aoi].MustRunSetup) // first turn runs Setup { agentsCopy[aoi].Setup(); agentsCopy[aoi].MustRunSetup = false; } else { agentsCopy[aoi].InternalAct(); } } } Thread.Sleep(_delayAfterTurn); TurnFinished(turn); }