/// <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> /// 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> /// 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); }
/// <summary> /// Creates new VehicleSpawnedEventArgs /// </summary> /// <param name="v">The vehicle to spawn</param> /// <param name="tv">Corresponding TrafficVolume</param> public VehicleSpawnedEventArgs(IVehicle v, TrafficVolume tv) { this.vehicleToSpawn = v; this.tv = tv; }
private bool SpawnVehicle(TrafficVolume.VehicleSpawnedEventArgs e) { LineNode start = e.tv.startNodes.nodes[GlobalRandom.Instance.Next(e.tv.startNodes.nodes.Count)]; if (start.nextConnections.Count > 0) { int foo = GlobalRandom.Instance.Next(start.nextConnections.Count); NodeConnection nc = start.nextConnections[foo]; e.vehicleToSpawn.state = new IVehicle.State(nc, 0); if (e.vehicleToSpawn.GetType() == typeof(Car)) e.vehicleToSpawn.physics = new IVehicle.Physics(carTargetVelocity + ((GlobalRandom.Instance.NextDouble() - 0.5) * 4), Math.Min(nc.targetVelocity, carTargetVelocity), 0); else if (e.vehicleToSpawn.GetType() == typeof(Truck)) e.vehicleToSpawn.physics = new IVehicle.Physics(truckTargetVelocity + ((GlobalRandom.Instance.NextDouble() - 0.5) * 4), Math.Min(nc.targetVelocity, truckTargetVelocity), 0); else if (e.vehicleToSpawn.GetType() == typeof(Tram)) e.vehicleToSpawn.physics = new IVehicle.Physics(tramTargetVelocity + ((GlobalRandom.Instance.NextDouble() - 0.5) * 4), Math.Min(nc.targetVelocity, tramTargetVelocity), 0); else if (e.vehicleToSpawn.GetType() == typeof(Bus)) e.vehicleToSpawn.physics = new IVehicle.Physics(busTargetVelocity + ((GlobalRandom.Instance.NextDouble() - 0.5) * 4), Math.Min(nc.targetVelocity, busTargetVelocity), 0); if (start.nextConnections[foo].AddVehicle(e.vehicleToSpawn)) { e.vehicleToSpawn.targetNodes = e.tv.destinationNodes.nodes; e.vehicleToSpawn.VehicleDied += new IVehicle.VehicleDiedEventHandler(e.tv.SpawnedVehicleDied); return true; } } return false; }
private void newTV_VehicleSpawned(object sender, TrafficVolume.VehicleSpawnedEventArgs e) { _vehiclesToSpawn.Add(e); }
/// <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; }
/// <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()); }