Example #1
0
        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);
        }
Example #2
0
        private void timer_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            if (_useAgentVisual)
            {
                if (_use3D)
                {
                    meshGeometry.Clear();
                    ClearViewport();
                }

                for (int i = 0; i < scenario.agentsList.Count; i++)
                {
                    if (_use3D)
                    {
                        if (scenario.agentsList[i].GetPosition() != new Point(-1, -1))
                        {
                            Point3D position = new Point3D(scenario.agentsList[i].GetPosition().X - scenario.map.GetMap().GetLength(0) / 2, 0, scenario.agentsList[i].GetPosition().Y - scenario.map.GetMap().GetLength(1) / 2);

                            int key = scenario.agentsList[i].Group;
                            if (!meshGeometry.ContainsKey(key))
                            {
                                meshGeometry.Add(key, new MeshGeometry3D());
                            }

                            if (scenario.agentsList[i] is HumanAgent)
                            {
                                meshGeometry[key] = HumanAgentVisual3D.AddAgentGeometry(position, new Size3D(1.0D - new Random(scenario.agentsList[i].ID).NextDouble() / 3, 3.0D - new Random(scenario.agentsList[i].ID).NextDouble(), 1.0D - new Random(scenario.agentsList[i].ID).NextDouble() / 3), meshGeometry[key]);
                            }
                            else if (scenario.agentsList[i] is BusAgent)
                            {
                                //TODO Раньше модели создавались в коде, теперь будут подгружаться из внешнего файла
                                //meshGeometry[key] = BusAgentVisual3D.AddAgentGeometry(position, (scenario.agentsList[i] as BusAgent).Size, meshGeometry[key]);
                                mainViewport.Children.Add(BusAgentVisual3D.GetModel(position, (scenario.agentsList[i] as BusAgent).Size, (scenario.agentsList[i] as BusAgent).Angle));
                            }
                            else if (scenario.agentsList[i] is TrainAgent)
                            {
                                TrainAgent ag = (scenario.agentsList[i] as TrainAgent);
                                for (int g = 0; g < ag.Positions.Length; g++)
                                {
                                    if (ag.NeedDraw[g] == true)
                                    {
                                        position = new Point3D(ag.Positions[g].X - scenario.map.GetMap().GetLength(0) / 2, 0, ag.Positions[g].Y - scenario.map.GetMap().GetLength(1) / 2);
                                        //    meshGeometry[key] = TrainAgentVisual3D.AddAgentGeometry(position, (scenario.agentsList[i] as TrainAgent).Size, meshGeometry[key]);
                                        mainViewport.Children.Add(TrainAgentVisual3D.GetModel(position, ag.Size, ag.Angles[g]));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (agentVisuals.ContainsKey(scenario.agentsList[i].ID))
                        {
                            if (agentVisuals[scenario.agentsList[i].ID].Location != scenario.agentsList[i].GetPosition())
                            {
                                //double angle;
                                //TODO Здесь раньше расчитывался угол поворота, теперь это все перехало в реализацию агента
                                if (UseAnimation)
                                {
                                    //agentVisuals[scenario.agentsList[i].ID].AngleAn = angle;
                                    agentVisuals[scenario.agentsList[i].ID].LocationAn = scenario.agentsList[i].GetPosition();
                                }
                                else
                                {
                                    //agentVisuals[scenario.agentsList[i].ID].Angle = angle;
                                    agentVisuals[scenario.agentsList[i].ID].Location = scenario.agentsList[i].GetPosition();
                                }
                            }
                        }
                        else
                        {
                            if (scenario.agentsList[i].GetPosition() != new Point(-1, -1))
                            {
                                if (scenario.agentsList[i] is HumanAgent)
                                {
                                    HumanAgentVisual ag = new HumanAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                                else if (scenario.agentsList[i] is BusAgent)
                                {
                                    BusAgentVisual ag = new BusAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                                else if (scenario.agentsList[i] is TrainAgent)
                                {
                                    TrainAgentVisual ag = new TrainAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                            }
                        }
                    }
                }
                if (Use3D)
                {
                    //Делаем группу для отрисовки агентов
                    Model3DGroup group = new Model3DGroup();
                    //Добавляем группы агентов в группу отрисофки
                    foreach (var pair in meshGeometry)
                    {
                        GeometryModel3D geom = new GeometryModel3D(pair.Value, new DiffuseMaterial(AgentVisualBase.GetGroupColor(pair.Key)));
                        group.Children.Add(geom);
                    }
                    //Создаем 3D модель
                    ModelVisual3D model = new ModelVisual3D();
                    model.Content = group;
                    //Чистим ViewPort TODO теперь он чистится раньше
                    //ClearViewport();
                    //Заполняем ViewPort моделью
                    mainViewport.Children.Add(model);
                    LightDirectionChanged();
                }
                else
                {
                    for (int i = 0; i < pnlMap.Children.Count; i++)
                    {
                        if (pnlMap.Children[i] is AgentVisualBase)
                        {
                            AgentVisualBase vag = pnlMap.Children[i] as AgentVisualBase;
                            if (!scenario.agentsList.Contains(vag.agentBase))
                            {
                                pnlMap.Children.Remove(vag);
                                agentVisuals.Remove(vag.agentBase.ID);
                                i--;
                            }
                        }
                    }
                }
            }
            if (!UseAnimation)
            {
                tbBenchmark.Text = ((200 - scenario.Benchmark - (DateTime.Now - now).Ticks / 10000) * 10).ToString();
            }
            else
            {
                tbBenchmark.Text = ((4000 - scenario.Benchmark - (DateTime.Now - now).Ticks / 10000) / 2).ToString();
            }
            tbSimulationTime.Text = new DateTime().Add(scenario.currentTime).ToString("HH:mm:ss");
            tbAgentsCount.Text    = scenario.agentsList.Count.ToString();
        }
Example #3
0
        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));
            }
        }