//private void infectOtherAgent(int sourceAgentId) //{ // if (this.agents.Count < 2) // { // Trace.TraceInformation("Warning: too litle agents"); // return; // } // // Random: // // int idx = random.Next(0, this.agents.Count - 1); // ContainersCore currentContainer = this.agentLocations[sourceAgentId]; // if (currentContainer == null) // { // Trace.TraceInformation("Warning: no current container for {0}", sourceAgentId); // return; // } // if (currentContainer is Home) // { // // Trace.TraceInformation("Current container for {0} is Home", sourceAgentId); // return; // } // if (currentContainer.AgentCount < 2) // { // Trace.TraceInformation("Warning: noone in container for {0}", sourceAgentId); // return; // } // int destAgentId = sourceAgentId; // while (destAgentId == sourceAgentId) // { // destAgentId = currentContainer.GetRandomAgent(); // } // Guid clientId = this.agents[destAgentId]; // var msg0 = new Message(MessageTransportSystem.Instance.Id, MessageType.Infect); // msg0.data = destAgentId.ToString(); // //Trace.TraceInformation("Infecting: {0} -> {1}", sourceAgentId, destAgentId); // MessageTransportSystem.Instance.SendMessage(msg0, clientId); //} private void gotoContainer(AddAgentMessage amsg, Enums.ContainerType containerType) { //if (agentId == 2) // Trace.TraceInformation("Go: {0} to {1} ; Time: {2}", agentId, containerId, GlobalTime.realTime); List <ContainersCore> containersWithType = Containers.Instance.Values.Where((c) => c.ContainerType == containerType).ToList(); if (containersWithType.Count == 0) { Trace.TraceWarning("Warning: Container with type {0} not found", containerType); return; } int idx = GlobalAgentDescriptorTable.random.Next(0, containersWithType.Count - 1); ContainersCore container = containersWithType[idx]; ContainersCore oldContainer = this.agentLocations[amsg.agentId]; amsg.containerId = container.Id; Guid workerId = this.containers2workers[container.Id]; // MessageTransportSystem.Instance.SendMessage(amsg, workerId); this.addAgentMessages[workerId].Add(amsg); agentLocations[amsg.agentId] = container; }
public void MoveContainer(ContainersCore container) { Trace.TraceInformation("Going to move container {0} ({1})", container.Id, container.ContainerType); var agents = GlobalAgentDescriptorTable.PersonsInContainer(container.Id); Trace.TraceInformation("Going to move container agents: {0}", String.Join(", ", agents.Select(a => a.GetId()))); AddContainerMessage msg = new AddContainerMessage( this.transportSystem.Id, container.ContainerType, container.Id, container.Area, container.Dencity ); msg.agentData.AddRange(agents.Select(a => new AddAgentMessage( this.transportSystem.Id, a.GetType().Name, a.GetId(), a.GetHealthState(), 1 ))); this.moveContainerMessage = msg; }
private void onAddContainerMessage(Message message) { AddContainerMessage acm = (AddContainerMessage)message; Trace.TraceInformation("Added containter. Type: {0}; Id: {1}.", acm.containerType, acm.containerId); ContainersCore container = null; switch (acm.containerType) { case Enums.ContainerType.Home: container = new Home(acm.containerId, acm.area, acm.dencity); break; case Enums.ContainerType.Hospital: container = new Hospital(acm.containerId, acm.area, acm.dencity); break; case Enums.ContainerType.Mall: container = new Mall(acm.containerId, acm.area, acm.dencity); break; case Enums.ContainerType.Nursery: container = new Nursery(acm.containerId, acm.area, acm.dencity); break; case Enums.ContainerType.Office: container = new Office(acm.containerId, acm.area, acm.dencity); break; case Enums.ContainerType.School: container = new School(acm.containerId, acm.area, acm.dencity); break; case Enums.ContainerType.University: container = new University(acm.containerId, acm.area, acm.dencity); break; } if (container != null) { if (!Containers.Instance.ContainsKey(container.Id)) { Containers.Instance.Add(container.Id, container); } foreach (AddAgentMessage aam in acm.agentData) { this.onAddAgentMessage(aam, container); } } }
// Запуск всех агентов public static void RunAgents() { int avgNumberOfAgents = totalAgents / totalWorkers; while (true) { NextTimeEvent.WaitOne(); // получаем список всех существующих агентов var agents = GlobalAgentDescriptorTable.GetAgents(); /* foreach (ContainersCore c in Containers.Instance) { * if (c.abstractPersonsInCurrentContainer.Count > 0) * { * Console.WriteLine("{0} ({1})", c.ContainerType, GlobalTime.realTime); * foreach (AbstractPerson p in c.abstractPersonsInCurrentContainer) * { * Console.Write(p.GetId()); * Console.Write(" "); * } * Console.WriteLine(); * } * }*/ if (agents.Count - avgNumberOfAgents > OVERFLOW_DIFF) { Trace.TraceInformation("Too many agents: {0}. Average: {1}", agents.Count, avgNumberOfAgents); ContainersCore containerToMove = GlobalAgentDescriptorTable.FindContainerToMove(); if (containerToMove != null) { MessageTransportSystem.MessageTransfer.Instance.MoveContainer(containerToMove); } } // запускаем каждый агент (агенты меняют свои состояния) for (int i = 0; i < agents.Count; ++i) { if (GlobalTime.Time % 24 == 0) { agents[i].Move(); } agents[i].Run(); } RefreshAgentsStateCount(); // после того как все агенты за этот час изменили свои состояния, мы увеличиваем время GlobalTime.Time += 1; if (GlobalTime.Time % 24 == 0) { //Trace.TraceInformation("New day: {0}", GlobalTime.Day); //Trace.TraceInformation("Susceptible: {0}\nRecovered: {3}\nInfectious: {4}\nFuneral: {1}\nDead: {2}", AgentManagementSystem.susceptibleAgentsCount, AgentManagementSystem.funeralAgentsCount, // AgentManagementSystem.deadAgentsCount, AgentManagementSystem.recoveredAgentsCount, AgentManagementSystem.infectiousAgentsCount); //Trace.TraceInformation("Agents count: {0}", agents.Count); //foreach(var g in GlobalAgentDescriptorTable.GetAgents().Select(a => (AbstractPerson)a).GroupBy(p => p.currentContainerId)) //{ // var fst = g.First(); // Trace.TraceInformation("{0} ({1}) : {2}", fst.currentContainerId, fst.currentContainerType, g.Count()); //} Trace.TraceInformation("{5}: {0} {3} {4} {1} {2}", AgentManagementSystem.susceptibleAgentsCount, AgentManagementSystem.funeralAgentsCount, AgentManagementSystem.deadAgentsCount, AgentManagementSystem.recoveredAgentsCount, AgentManagementSystem.infectiousAgentsCount, GlobalTime.Day); MessageTransportSystem.MessageTransfer.Instance.SetResults(susceptibleAgentsCount, recoveredAgentsCount, infectiousAgentsCount, funeralAgentsCount, deadAgentsCount, exposedAgentsCount, GlobalTime.Time); } //MessageTransportSystem.MessageTransfer.SendTickEnd(); MessageTransportSystem.MessageTransfer.Instance.SendGoto(); // if ((GlobalTime.Time > 1000 && exposedAgentsCount == 0 && infectiousAgentsCount == 0) || GlobalTime.Day >= 80) if (GlobalTime.Day >= DAYS || finishFlag) { break; } } }
private void onAddAgentMessage(Message message, ContainersCore container = null) { AddAgentMessage aaMessage = (AddAgentMessage)message; List <IAgent> ags = new List <IAgent>(); GlobalAgentDescriptorTable.GetNewId = aaMessage.agentId; switch (aaMessage.agentType) { case "Adolescent": ags.AddRange(Adolescent.AdolescentList(aaMessage.state, aaMessage.count, "LocationProbabilities")); break; case "Adult": ags.AddRange(Adult.AdultList(aaMessage.state, aaMessage.count, "LocationProbabilities")); break; case "Child": ags.AddRange(Child.ChildList(aaMessage.state, aaMessage.count, "LocationProbabilities")); break; case "Elder": ags.AddRange(Elder.ElderList(aaMessage.state, aaMessage.count, "LocationProbabilities")); break; case "Youngster": ags.AddRange(Youngster.YoungsterList(aaMessage.state, aaMessage.count, "LocationProbabilities")); break; } foreach (IAgent a in ags) { if (container == null) { int containerId = aaMessage.containerId; if (containerId > 0) { if (Containers.Instance.ContainsKey(containerId)) { var foundContainer = Containers.Instance[containerId]; AbstractPerson ap = (AbstractPerson)a; ap.currentContainerId = containerId; ap.currentContainerType = foundContainer.ContainerType; } else { Trace.TraceWarning("Agent was added to moved to another node container. Moving him again"); CoreAMS.MessageTransportSystem.MessageTransfer.Instance.AddToGoto((AbstractPerson)a, Enums.ContainerType.Home); } } } else { AbstractPerson ap = (AbstractPerson)a; ap.currentContainerId = container.Id; ap.currentContainerType = container.ContainerType; } } GlobalAgentDescriptorTable.AddAgents(ags); //foreach (var ag in ags) { // Trace.TraceInformation("Added agent with id {0} (check: {1})", ag.GetId(), aaMessage.agentId); //} }