public static AgentBase GetRandomAgentTemplateFromGroup(List <AgentTemplate> atmass, int group_id, Scenario scenario) { int j = 0; Random rand = new Random(); double value = rand.Next(1, 101); while (value > 0 && j < atmass.Count) { value -= atmass[j].Persent; j++; } j--; int AgentSpeedms = Convert.ToInt32(MapOld.CellSize * 3600 * 1000 / (rand.Next(Convert.ToInt32(atmass[j].MinSpeed * 1000), Convert.ToInt32(atmass[j].MaxSpeed * 1000)))); AgentBase agent = null; if (atmass[j].Type == typeof(HumanAgent).Name) { agent = new HumanAgent(AgentIDEnumerator.GetNextID(), scenario, group_id, AgentSpeedms); } else if (atmass[j].Type == typeof(BusAgent).Name) { agent = new BusAgent(AgentIDEnumerator.GetNextID(), scenario, group_id, AgentSpeedms) { RoadGraph = scenario.RoadGraph, Size = new System.Windows.Media.Media3D.Size3D(atmass[j].Length / MapOld.CellSize, atmass[j].Width / MapOld.CellSize, atmass[j].Height / MapOld.CellSize), MaxCapasity = atmass[j].Capasity, InputFactor = atmass[j].InputFactor, OutputFactor = atmass[j].OutputFactor }; } else if (atmass[j].Type == typeof(TrainAgent).Name) { agent = new TrainAgent(AgentIDEnumerator.GetNextID(), scenario, group_id, AgentSpeedms, atmass[j].NumberOfCarriges) { RoadGraph = scenario.RoadGraph, Size = new System.Windows.Media.Media3D.Size3D(atmass[j].Length / MapOld.CellSize, atmass[j].Width / MapOld.CellSize, atmass[j].Height / MapOld.CellSize), MaxCapasity = atmass[j].Capasity, InputFactor = atmass[j].InputFactor, OutputFactor = atmass[j].OutputFactor }; } if (agent != null) { agent.WayPointsList = new List <WayPoint>(); foreach (var wp in atmass[j].WayPointsList) { agent.WayPointsList.Add((WayPoint)wp.Clone()); } } return(agent); }
void AcceptAgent_DoWork(object sender, DoWorkEventArgs e) { AgentsGroup group = e.Argument as AgentsGroup; TcpListener listener = GroupListeners[group.ID]; while (true) { bool pending = false; try { pending = listener.Pending(); } catch (InvalidOperationException) { listener = new TcpListener(System.Net.IPAddress.Parse(group.Address), group.Port); listener.Start(); pending = listener.Pending(); } if (pending) { Socket serverSocket = listener.AcceptSocket(); if (serverSocket.Connected) { NetworkStream networkStream = new NetworkStream(serverSocket); StreamReader streamReader = new StreamReader(networkStream); StreamWriter streamWriter = new StreamWriter(networkStream); Random rand = new Random(); while (true) { try { streamReader.ReadLine(); } catch (IOException) { break; } int j = 0; double value = rand.NextDouble(); while (value > 0 && j < group.AgentTemplateList.Count) { value -= group.AgentTemplateList[j].Persent; j++; } j--; int AgentSpeedms = Convert.ToInt32(MapOld.CellSize * 3600 * 1000 / (rand.Next(Convert.ToInt32(group.AgentTemplateList[j].MinSpeed * 1000), Convert.ToInt32(group.AgentTemplateList[j].MaxSpeed * 1000)))); AgentBase agent = null; if (group.AgentTemplateList[j].Type == typeof(HumanAgent).Name) { agent = new HumanAgent(AgentIDEnumerator.GetNextID(), this, group.ID, AgentSpeedms); } else if (group.AgentTemplateList[j].Type == typeof(BusAgent).Name) { agent = new BusAgent(AgentIDEnumerator.GetNextID(), this, group.ID, AgentSpeedms) { RoadGraph = this.RoadGraph, Size = new System.Windows.Media.Media3D.Size3D(group.AgentTemplateList[j].Length / MapOld.CellSize, group.AgentTemplateList[j].Width / MapOld.CellSize, group.AgentTemplateList[j].Height / MapOld.CellSize), MaxCapasity = group.AgentTemplateList[j].Capasity, InputFactor = group.AgentTemplateList[j].InputFactor, OutputFactor = group.AgentTemplateList[j].OutputFactor }; } else if (group.AgentTemplateList[j].Type == typeof(TrainAgent).Name) { agent = new TrainAgent(AgentIDEnumerator.GetNextID(), this, group.ID, AgentSpeedms, group.AgentTemplateList[j].NumberOfCarriges) { RoadGraph = this.RoadGraph, Size = new System.Windows.Media.Media3D.Size3D(group.AgentTemplateList[j].Length / MapOld.CellSize, group.AgentTemplateList[j].Width / MapOld.CellSize, group.AgentTemplateList[j].Height / MapOld.CellSize), MaxCapasity = group.AgentTemplateList[j].Capasity, InputFactor = group.AgentTemplateList[j].InputFactor, OutputFactor = group.AgentTemplateList[j].OutputFactor }; } if (agent != null) { agent.WayPointsList = new List <WayPoint>(); foreach (var point in group.AgentTemplateList[j].WayPointsList) { agent.WayPointsList.Add(point); } agentsList.Add(agent); } } } } Thread.Sleep(TimeSpan.FromMilliseconds(1000)); } }