/// <summary>
        /// Finds a BunchOfNode in bofList being equal to nodeList. If no such BoF exists, a new one will be created and added.
        /// </summary>
        /// <param name="nodeList">List of LineNodes</param>
        /// <param name="bofList">List of BunchOfNodes</param>
        /// <returns>A BunchOfNodes containing the same LineNodes as nodeList</returns>
        private BunchOfNodes GetOrCreateEqualBoF(List <LineNode> nodeList, List <BunchOfNodes> bofList)
        {
            // search for an fitting BunchOfNode
            foreach (BunchOfNodes bof in bofList)
            {
                bool equal = true;
                foreach (LineNode ln in nodeList)
                {
                    if (!bof.nodes.Contains(ln))
                    {
                        equal = false;
                        break;
                    }
                }

                if (equal)
                {
                    return(bof);
                }
            }

            // No equal BoF was found => create a new one
            BunchOfNodes newBof = new BunchOfNodes(nodeList, "Bunch " + (bofList.Count + 1).ToString());

            bofList.Add(newBof);
            return(newBof);
        }
        /// <summary>
        /// Imports traffic volume from older file versions still containing "Aufträge"
        /// </summary>
        /// <param name="fahrauftraege">List of all "Aufträge" to import</param>
        public void ImportOldTrafficVolumeData(List <Auftrag> fahrauftraege)
        {
            foreach (Auftrag a in fahrauftraege)
            {
                BunchOfNodes startBof       = GetOrCreateEqualBoF(a.startNodes, startPoints);
                BunchOfNodes destinationBof = GetOrCreateEqualBoF(a.endNodes, destinationPoints);

                TrafficVolume tv = GetOrCreateTrafficVolume(startBof, destinationBof);
                switch (a.vehicleType)
                {
                case CityTrafficSimulator.Vehicle.IVehicle.VehicleTypes.CAR:
                    tv.SetTrafficVolume((int)(tv.trafficVolumeCars + a.trafficDensity * 0.92), (int)(tv.trafficVolumeTrucks + a.trafficDensity * 0.08), tv.trafficVolumeBusses, tv.trafficVolumeTrams);
                    break;

                case CityTrafficSimulator.Vehicle.IVehicle.VehicleTypes.BUS:
                    tv.SetTrafficVolume(tv.trafficVolumeCars, tv.trafficVolumeTrucks, tv.trafficVolumeBusses + a.trafficDensity, tv.trafficVolumeTrams);
                    break;

                case CityTrafficSimulator.Vehicle.IVehicle.VehicleTypes.TRAM:
                    tv.SetTrafficVolume(tv.trafficVolumeCars, tv.trafficVolumeTrucks, tv.trafficVolumeBusses, tv.trafficVolumeTrams + a.trafficDensity);
                    break;
                }
            }

            OnStartPointsChanged(new StartPointsChangedEventArgs());
            OnDestinationPointsChanged(new DestinationPointsChangedEventArgs());
        }
        public double getEstatistica(object startItem, object destinationItem) // Replicado método que monta as estatísticas da aplicação, com a diferença de que este retorna o tempo médio de viagem
        {
            ignoreUpdateEvent = true;

            BunchOfNodes start       = startItem as BunchOfNodes;
            BunchOfNodes destination = destinationItem as BunchOfNodes;

            editStartNodeTitle.Text       = start.title;
            m_mainForm.fromLineNodes      = start.nodes;
            editDestinationNodeTitle.Text = destination.title;
            m_mainForm.toLineNodes        = destination.nodes;

            if (start != null && destination != null)
            {
                m_currentVolume = m_steuerung.GetOrCreateTrafficVolume(start, destination);

                spinCarsVolume.Value    = m_currentVolume.trafficVolumeCars;
                spinTruckVolume.Value   = m_currentVolume.trafficVolumeTrucks;
                spinBusVolume.Value     = m_currentVolume.trafficVolumeBusses;
                spinTramVolume.Value    = m_currentVolume.trafficVolumeTrams;
                spinCarsVolume.Enabled  = true;
                spinTruckVolume.Enabled = true;
                spinBusVolume.Enabled   = true;
                spinTramVolume.Enabled  = true;
                ignoreUpdateEvent       = false;

                double milage = (m_currentVolume.statistics.sumMilage / m_currentVolume.statistics.numVehiclesReachedDestination) / 10;
                double tt     = m_currentVolume.statistics.sumTravelTime / m_currentVolume.statistics.numVehiclesReachedDestination;
                if (m_currentVolume.statistics.numVehicles == 0)
                {
                    milage = 0;
                    tt     = 1;
                }
                lblNumVehicles.Text = "Total Vehicles: " + m_currentVolume.statistics.numVehicles + " (" + m_currentVolume.statistics.numVehiclesReachedDestination + " reached Destination)";
                lblMilage.Text      = "Average Milage: " + milage + "m";
                lblTravelTime.Text  = "Average Travel Time: " + tt + "s";
                lblVelocity.Text    = "Average Milage: " + (milage / tt) + "m/s";
                lblNumStops.Text    = "Average Number of Stops: " + ((float)m_currentVolume.statistics.numStops / m_currentVolume.statistics.numVehicles);

                return(tt);
            }
            else
            {
                m_currentVolume         = null;
                spinCarsVolume.Enabled  = false;
                spinTruckVolume.Enabled = false;
                spinBusVolume.Enabled   = false;
                spinTramVolume.Enabled  = false;
                lblNumVehicles.Text     = "Total Vehicles: 0";
                lblMilage.Text          = "Average Milage: 0m";
                lblTravelTime.Text      = "Average Travel Time: 0s";
                lblVelocity.Text        = "Average Milage: 0m/s";
                lblNumStops.Text        = "Average Number of Stops: 0";
            }

            ignoreUpdateEvent = false;

            return(0);
        }
        /// <summary>
        /// entfernt die BunchOfNodes sp aus targetPoints
        /// </summary>
        /// <param name="sp">zu löschende BunchOfNodes</param>
        public void RemoveDestinationPoint(BunchOfNodes sp)
        {
            // remove all corresponding traffic volumes
            trafficVolumes.RemoveAll(delegate(TrafficVolume tv) { return(tv.destinationNodes == sp); });

            _destinationPoints.Remove(sp);
            OnDestinationPointsChanged(new DestinationPointsChangedEventArgs());
        }
        /// <summary>
        /// entfernt die BunchOfNodes sp aus startPoints
        /// </summary>
        /// <param name="sp">zu löschende BunchOfNodes</param>
        public void RemoveStartPoint(BunchOfNodes sp)
        {
            // remove all corresponding traffic volumes
            trafficVolumes.RemoveAll(delegate(TrafficVolume tv) { return(tv.startNodes == sp); });

            _startPoints.Remove(sp);
            OnStartPointsChanged(new StartPointsChangedEventArgs());
        }
        private void btnRemoveDestinationNode_Click(object sender, EventArgs e)
        {
            BunchOfNodes bon = lbDestinationNodes.SelectedItem as BunchOfNodes;

            if (bon != null)
            {
                m_steuerung.RemoveDestinationPoint(bon);
            }
        }
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="start">start nodes</param>
 /// <param name="destination">Destination nodes</param>
 public TrafficVolume(BunchOfNodes start, BunchOfNodes destination)
 {
     this.startNodes           = start;
     this.destinationNodes     = destination;
     this._trafficVolumeCars   = 0;
     this._trafficVolumeTrucks = 0;
     this._trafficVolumeBusses = 0;
     this._trafficVolumeTrams  = 0;
 }
        private void lbDestinationNodes_SelectedIndexChanged(object sender, EventArgs e)
        {
            BunchOfNodes bon = lbDestinationNodes.SelectedItem as BunchOfNodes;

            if (bon != null)
            {
                editDestinationNodeTitle.Text = bon.title;
                m_mainForm.toLineNodes        = bon.nodes;
            }
            GetTrafficVolume();
        }
        /// <summary>
        /// Gets the TrafficVolume record to the corresponding start and destination nodes and updates the SpinEdit values.
        /// </summary>
        private void GetTrafficVolume()
        {
            ignoreUpdateEvent = true;
            BunchOfNodes start       = lbStartNodes.SelectedItem as BunchOfNodes;
            BunchOfNodes destination = lbDestinationNodes.SelectedItem as BunchOfNodes;

            if (start != null && destination != null)
            {
                m_currentVolume = m_steuerung.GetOrCreateTrafficVolume(start, destination);

                spinCarsVolume.Value    = m_currentVolume.trafficVolumeCars;
                spinTruckVolume.Value   = m_currentVolume.trafficVolumeTrucks;
                spinBusVolume.Value     = m_currentVolume.trafficVolumeBusses;
                spinTramVolume.Value    = m_currentVolume.trafficVolumeTrams;
                spinCarsVolume.Enabled  = true;
                spinTruckVolume.Enabled = true;
                spinBusVolume.Enabled   = true;
                spinTramVolume.Enabled  = true;
                ignoreUpdateEvent       = false;

                double milage = (m_currentVolume.statistics.sumMilage / m_currentVolume.statistics.numVehiclesReachedDestination) / 10;
                double tt     = m_currentVolume.statistics.sumTravelTime / m_currentVolume.statistics.numVehiclesReachedDestination;
                if (m_currentVolume.statistics.numVehicles == 0)
                {
                    milage = 0;
                    tt     = 1;
                }
                lblNumVehicles.Text = "Total Vehicles: " + m_currentVolume.statistics.numVehicles + " (" + m_currentVolume.statistics.numVehiclesReachedDestination + " reached Destination)";
                lblMilage.Text      = "Average Milage: " + milage + "m";
                lblTravelTime.Text  = "Average Travel Time: " + tt + "s";
                lblVelocity.Text    = "Average Milage: " + (milage / tt) + "m/s";
                lblNumStops.Text    = "Average Number of Stops: " + ((float)m_currentVolume.statistics.numStops / m_currentVolume.statistics.numVehicles);
            }
            else
            {
                m_currentVolume         = null;
                spinCarsVolume.Enabled  = false;
                spinTruckVolume.Enabled = false;
                spinBusVolume.Enabled   = false;
                spinTramVolume.Enabled  = false;
                lblNumVehicles.Text     = "Total Vehicles: 0";
                lblMilage.Text          = "Average Milage: 0m";
                lblTravelTime.Text      = "Average Travel Time: 0s";
                lblVelocity.Text        = "Average Milage: 0m/s";
                lblNumStops.Text        = "Average Number of Stops: 0";
            }
            ignoreUpdateEvent = false;
        }
 /// <summary>
 /// Recovers the references after XML deserialization.
 /// </summary>
 /// <param name="saveVersion">Version of the read file</param>
 /// <param name="startList">List of all start BunchOfNodes</param>
 /// <param name="destinationList">List of all destination BunchOfNodes</param>
 public void RecoverFromLoad(int saveVersion, List <BunchOfNodes> startList, List <BunchOfNodes> destinationList)
 {
     foreach (BunchOfNodes bof in startList)
     {
         if (bof.hashcode == startHash)
         {
             startNodes = bof;
         }
     }
     foreach (BunchOfNodes bof in destinationList)
     {
         if (bof.hashcode == destinationHash)
         {
             destinationNodes = bof;
         }
     }
 }
        /// <summary>
        /// Returns the Traffic Volume for the route from start to destination.
        /// If no such TrafficVolume exists, a new one will be created.
        /// start and destination MUST be != null!
        /// </summary>
        /// <param name="start">Start nodes</param>
        /// <param name="destination">Destination nodes</param>
        /// <returns></returns>
        public TrafficVolume GetOrCreateTrafficVolume(BunchOfNodes start, BunchOfNodes destination)
        {
            Debug.Assert(start != null && destination != null);

            // There certainly are data structures offering better algorithms to search for these specific entities.
            // But m_trafficVolumes usually contains < 100 items, so performance can be seen as unimportant here.
            foreach (TrafficVolume tv in _trafficVolumes)
            {
                if (tv.startNodes == start && tv.destinationNodes == destination)
                {
                    return(tv);
                }
            }

            TrafficVolume newTV = new TrafficVolume(start, destination);

            newTV.VehicleSpawned += new TrafficVolume.VehicleSpawnedEventHandler(newTV_VehicleSpawned);
            _trafficVolumes.Add(newTV);
            return(newTV);
        }
Example #12
0
 /// <summary>
 /// Recovers the references after XML deserialization.
 /// </summary>
 /// <param name="saveVersion">Version of the read file</param>
 /// <param name="startList">List of all start BunchOfNodes</param>
 /// <param name="destinationList">List of all destination BunchOfNodes</param>
 public void RecoverFromLoad(int saveVersion, List<BunchOfNodes> startList, List<BunchOfNodes> destinationList)
 {
     foreach (BunchOfNodes bof in startList)
         {
         if (bof.hashcode == startHash)
             startNodes = bof;
         }
     foreach (BunchOfNodes bof in destinationList)
         {
         if (bof.hashcode == destinationHash)
             destinationNodes = bof;
         }
 }
Example #13
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="start">start nodes</param>
 /// <param name="destination">Destination nodes</param>
 public TrafficVolume(BunchOfNodes start, BunchOfNodes destination)
 {
     this.startNodes = start;
     this.destinationNodes = destination;
     this._trafficVolumeCars = 0;
     this._trafficVolumeTrucks = 0;
     this._trafficVolumeBusses = 0;
     this._trafficVolumeTrams = 0;
 }
Example #14
0
        /// <summary>
        /// Finds a BunchOfNode in bofList being equal to nodeList. If no such BoF exists, a new one will be created and added.
        /// </summary>
        /// <param name="nodeList">List of LineNodes</param>
        /// <param name="bofList">List of BunchOfNodes</param>
        /// <returns>A BunchOfNodes containing the same LineNodes as nodeList</returns>
        private BunchOfNodes GetOrCreateEqualBoF(List<LineNode> nodeList, List<BunchOfNodes> bofList)
        {
            // search for an fitting BunchOfNode
            foreach (BunchOfNodes bof in bofList)
                {
                bool equal = true;
                foreach (LineNode ln in nodeList)
                    {
                    if (!bof.nodes.Contains(ln))
                        {
                        equal = false;
                        break;
                        }
                    }

                if (equal)
                    {
                    return bof;
                    }
                }

            // No equal BoF was found => create a new one
            BunchOfNodes newBof = new BunchOfNodes(nodeList, "Bunch " + (bofList.Count + 1).ToString());
            bofList.Add(newBof);
            return newBof;
        }
Example #15
0
        /// <summary>
        /// entfernt die BunchOfNodes sp aus startPoints
        /// </summary>
        /// <param name="sp">zu löschende BunchOfNodes</param>
        public void RemoveStartPoint(BunchOfNodes sp)
        {
            // remove all corresponding traffic volumes
            trafficVolumes.RemoveAll(delegate(TrafficVolume tv) { return tv.startNodes == sp; });

            _startPoints.Remove(sp);
            OnStartPointsChanged(new StartPointsChangedEventArgs());
        }
Example #16
0
        /// <summary>
        /// entfernt die BunchOfNodes sp aus targetPoints
        /// </summary>
        /// <param name="sp">zu löschende BunchOfNodes</param>
        public void RemoveDestinationPoint(BunchOfNodes sp)
        {
            // remove all corresponding traffic volumes
            trafficVolumes.RemoveAll(delegate(TrafficVolume tv) { return tv.destinationNodes == sp; });

            _destinationPoints.Remove(sp);
            OnDestinationPointsChanged(new DestinationPointsChangedEventArgs());
        }
Example #17
0
        /// <summary>
        /// Returns the Traffic Volume for the route from start to destination.
        /// If no such TrafficVolume exists, a new one will be created.
        /// start and destination MUST be != null!
        /// </summary>
        /// <param name="start">Start nodes</param>
        /// <param name="destination">Destination nodes</param>
        /// <returns></returns>
        public TrafficVolume GetOrCreateTrafficVolume(BunchOfNodes start, BunchOfNodes destination)
        {
            Debug.Assert(start != null && destination != null);

            // There certainly are data structures offering better algorithms to search for these specific entities.
            // But m_trafficVolumes usually contains < 100 items, so performance can be seen as unimportant here.
            foreach (TrafficVolume tv in _trafficVolumes)
                {
                if (tv.startNodes == start && tv.destinationNodes == destination)
                    return tv;
                }

            TrafficVolume newTV = new TrafficVolume(start, destination);
            newTV.VehicleSpawned += new TrafficVolume.VehicleSpawnedEventHandler(newTV_VehicleSpawned);
            _trafficVolumes.Add(newTV);
            return newTV;
        }
Example #18
0
 /// <summary>
 /// fügt eine BunchOfNodes den startPoints hinzu
 /// </summary>
 /// <param name="sp">hinzuzufügende BunchOfNodes</param>
 public void AddStartPoint(BunchOfNodes sp)
 {
     _startPoints.Add(sp);
     OnStartPointsChanged(new StartPointsChangedEventArgs());
 }
Example #19
0
 /// <summary>
 /// fügt eine BunchOfNodes den targetPoints hinzu
 /// </summary>
 /// <param name="sp">hinzuzufügende BunchOfNodes</param>
 public void AddDestinationPoint(BunchOfNodes sp)
 {
     _destinationPoints.Add(sp);
     OnDestinationPointsChanged(new DestinationPointsChangedEventArgs());
 }
 /// <summary>
 /// fügt eine BunchOfNodes den targetPoints hinzu
 /// </summary>
 /// <param name="sp">hinzuzufügende BunchOfNodes</param>
 public void AddDestinationPoint(BunchOfNodes sp)
 {
     _destinationPoints.Add(sp);
     OnDestinationPointsChanged(new DestinationPointsChangedEventArgs());
 }
        /// <summary>
        /// Loads the traffic volume setup from the given XML file
        /// </summary>
        /// <param name="xd">XmlDocument to parse</param>
        /// <param name="nodesList">List of all existing LineNodes</param>
        /// <param name="lf">LoadingForm for status updates</param>
        public void LoadFromFile(XmlDocument xd, List <LineNode> nodesList, LoadingForm.LoadingForm lf)
        {
            lf.SetupLowerProgess("Parsing XML...", 3);

            // clear everything first
            _trafficVolumes.Clear();
            _startPoints.Clear();
            _destinationPoints.Clear();

            // parse save file version (currently not needed, but probably in future)
            int     saveVersion     = 0;
            XmlNode mainNode        = xd.SelectSingleNode("//CityTrafficSimulator");
            XmlNode saveVersionNode = mainNode.Attributes.GetNamedItem("saveVersion");

            if (saveVersionNode != null)
            {
                saveVersion = Int32.Parse(saveVersionNode.Value);
            }
            else
            {
                saveVersion = 0;
            }

            // Load start points:
            lf.StepLowerProgress();
            // get corresponding XML nodes
            XmlNodeList xnlStartNodes = xd.SelectNodes("//CityTrafficSimulator/TrafficVolumes/StartPoints/BunchOfNodes");

            foreach (XmlNode aXmlNode in xnlStartNodes)
            {
                // Deserialize each node
                TextReader    tr  = new StringReader(aXmlNode.OuterXml);
                XmlSerializer xs  = new XmlSerializer(typeof(BunchOfNodes));
                BunchOfNodes  bof = (BunchOfNodes)xs.Deserialize(tr);
                bof.RecoverFromLoad(saveVersion, nodesList);
                _startPoints.Add(bof);
            }

            // Load destination points:
            lf.StepLowerProgress();
            // get corresponding XML nodes
            XmlNodeList xnlDestinationNodes = xd.SelectNodes("//CityTrafficSimulator/TrafficVolumes/DestinationPoints/BunchOfNodes");

            foreach (XmlNode aXmlNode in xnlDestinationNodes)
            {
                // Deserialize each node
                TextReader    tr  = new StringReader(aXmlNode.OuterXml);
                XmlSerializer xs  = new XmlSerializer(typeof(BunchOfNodes));
                BunchOfNodes  bof = (BunchOfNodes)xs.Deserialize(tr);
                bof.RecoverFromLoad(saveVersion, nodesList);
                // ensure that no hashcode is assigned twice
                BunchOfNodes.hashcodeIndex = Math.Max(bof.hashcode + 1, BunchOfNodes.hashcodeIndex);
                _destinationPoints.Add(bof);
            }

            // Load traffic volumes:
            lf.StepLowerProgress();
            // get corresponding XML nodes
            XmlNodeList xnlTrafficVolumes = xd.SelectNodes("//CityTrafficSimulator/TrafficVolumes/TrafficVolume");

            foreach (XmlNode aXmlNode in xnlTrafficVolumes)
            {
                // Deserialize each node
                TextReader    tr = new StringReader(aXmlNode.OuterXml);
                XmlSerializer xs = new XmlSerializer(typeof(TrafficVolume));
                TrafficVolume tv = (TrafficVolume)xs.Deserialize(tr);

                tv.RecoverFromLoad(saveVersion, startPoints, destinationPoints);
                if (tv.startNodes != null && tv.destinationNodes != null)
                {
                    _trafficVolumes.Add(tv);
                    tv.VehicleSpawned += new TrafficVolume.VehicleSpawnedEventHandler(newTV_VehicleSpawned);
                }
                else
                {
                    lf.Log("Error during traffic volume deserialization: Could not dereference start-/end nodes. Traffic volume was dismissed.");
                }
            }

            OnStartPointsChanged(new StartPointsChangedEventArgs());
            OnDestinationPointsChanged(new DestinationPointsChangedEventArgs());
        }
 /// <summary>
 /// fügt eine BunchOfNodes den startPoints hinzu
 /// </summary>
 /// <param name="sp">hinzuzufügende BunchOfNodes</param>
 public void AddStartPoint(BunchOfNodes sp)
 {
     _startPoints.Add(sp);
     OnStartPointsChanged(new StartPointsChangedEventArgs());
 }