/// <summary> /// Adds the monitored state to the processing pipeline. /// </summary> /// <param name="currentState">The monitored state.</param> public void MonitorStep(MonitoredState currentState, DateTime now) { var cachedMs = new CachedHashMonitoredState(currentState, now, projection); //if (this is ObstacleMonitor om && om.Obstacle.Identifier == "a3_stopped_in_traffic_monitored") //{ // Console.WriteLine("MonitorStep: " + cachedMs.StateHash + " " + string.Join(",", projection)); //} buffer.Post(cachedMs); }
public void Update(MonitoredState ms, DateTime datetime) { logger.Info("Update. " + _roots.Count); // Create the new monitors and update the existing ones accordingly foreach (var m in obstacleMonitors.Values) { try { m.MonitorStep(ms, datetime); if (m.MonitoredSatisfactionRate != null) { // Update the satisfaction rate for the obstacle _model_running.satisfactionRateRepository.AddObstacleSatisfactionRate( m.Obstacle.Identifier, new DoubleSatisfactionRate(m.MonitoredSatisfactionRate.Mean)); } } catch (Exception e) { logger.Error($"Fail to update monitor for '{m.Obstacle.FriendlyName}' ({e.Message})"); } } // Compute the satisfaction rate for the root goals foreach (var root in _roots) { RootSatisfactionRates[root.Identifier] = (DoubleSatisfactionRate)_propagator.GetESR(root); logger.Info($"Satisfaction rate for {root.Identifier} = " + RootSatisfactionRates[root.Identifier]); } if (!string.IsNullOrEmpty(outputFilename)) { var now = DateTime.Now; string str = string.Format("{0:yyyy-MM-dd HH:mm:ss.fffffff},{1},{2}", now, string.Join(",", RootSatisfactionRates.OrderBy(x => x.Key).Select(x => x.Value.SatisfactionRate)), string.Join(",", obstacleMonitors.Select(x => (x.Value.MonitoredSatisfactionRate?.Mean ?? 0) + "," + (x.Value.MonitoredSatisfactionRate?.StdDev ?? 0))) ); using (StreamWriter sw = File.AppendText(outputFilename)) { sw.WriteLine(str); } } }
static MonitoredState GetMonitoredState() { int nIncident = 10; var inc = incidentRepository.GetAllIncidents().ToArray(); var amb = ambulanceRepository.GetAllAmbulances().ToArray(); var allocations = allocationRepository.GetAllocations().ToArray(); var a = new Ambulance[15]; for (int i = 0; i < 15; i++) { a[i] = amb.Single((arg) => arg.AmbulanceId == "A" + (i + 1)); } var getAmbulance = new Func <string, Ambulance>((arg) => { int id = int.Parse(arg.Substring(1)); return(a[id - 1]); }); var ms = new MonitoredState(); var incidents = new Incident[nIncident]; for (int i = 0; i < nIncident; i++) { var incs = inc.Where(x => !x.Resolved & x.IncidentId % nIncident == i); incidents[i] = (incs.Count() > 0) ? incs.MaxBy(x => x.IncidentId) : null; } var getIncident = new Func <int, Incident>((arg) => { return(incidents[arg % nIncident]); }); //for (int i = 0; i < nIncident; i++) { // logger.Info("incident " + i + " = " + incidents[i]?.IncidentId); //} // TODO Fix FirstOrDefault to SingleOrDefault var alloc = new Allocation[nIncident]; for (int i = 0; i < nIncident; i++) { alloc[i] = (incidents[i] != null) ? allocations.FirstOrDefault(x => x.IncidentId == incidents[i].IncidentId & !x.Refused && !x.Cancelled) : null; } var getIncAlloc = new Func <Incident, Allocation>((incident) => { return((incident != null) ? allocations.FirstOrDefault(x => x.IncidentId == incident.IncidentId & !x.Refused && !x.Cancelled) : null); }); // TODO Fix FirstOrDefault to SingleOrDefault var incList = new HashSet <int>(incidents.Where(x => x != null).Select(x => x.IncidentId)); var ambAlloc = new Allocation[15]; for (int i = 0; i < 15; i++) { ambAlloc[i] = allocations.FirstOrDefault(x => x.AmbulanceId == a[i].AmbulanceId & !x.Refused && !x.Cancelled & incList.Contains(x.IncidentId)); } var getAmbAlloc = new Func <Ambulance, Allocation>((Ambulance aa) => { return(allocations.FirstOrDefault(x => x.AmbulanceId == aa.AmbulanceId & !x.Refused && !x.Cancelled & incList.Contains(x.IncidentId))); }); for (int i = 0; i < nIncident; i++) { ms.Set("IncReported" + (i + 1), incidents[i] != null); } ms.Set("IncAllocated", incidents.Any(ii => ii != null && allocations.Any(x => x.IncidentId == ii.IncidentId & !x.Refused & !x.Cancelled))); for (int i = 0; i < nIncident; i++) { ms.Set("IncAllocated" + (i + 1), incidents[i] != null && allocations.Any(x => x.IncidentId == incidents[i].IncidentId & !x.Refused & !x.Cancelled)); } ms.Set("PendingIncident", inc.Any(x => !x.Resolved && !allocations.Any(y => y.IncidentId == x.IncidentId))); for (int i = 0; i < nIncident; i++) { ms.Set("IncMobilized" + (i + 1), incidents[i] != null && allocations.Any(x => x.IncidentId == incidents[i].IncidentId & !x.Refused & !x.Cancelled & x.MobilizationConfirmedTimestamp != null)); } ms.Set("AmbAvailable", amb.Any(x => x.Status == Core.Domain.AmbulanceStatus.AvailableAtStation | x.Status == Core.Domain.AmbulanceStatus.AvailableRadio)); var now = DateTime.Now; ms.Set("AmbPositionKnown", amb.Any(x => (now - x.LastPositionUpdate) < TimeSpan.FromSeconds(1 * 60 / 10))); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "PositionKnown", (now - a[i].LastPositionUpdate) < TimeSpan.FromSeconds(1 * 60 / 10)); } for (int i = 0; i < nIncident; i++) { ms.Set("IncPending" + (i + 1), incidents[i] != null && allocations.Any(x => x.IncidentId == incidents[i].IncidentId & !x.Refused & !x.Cancelled & x.MobilizationConfirmedTimestamp == null)); } for (int i = 0; i < nIncident; i++) { ms.Set("MobilizationOrderRefused" + (i + 1), incidents[i] != null && allocations.Where(x => x.IncidentId == incidents[i].IncidentId & !x.Cancelled && x.MobilizationReceivedTimestamp != null).All(x => x.Refused)); } ms.Set("AmbMobilizedAndLeaving", amb.Any(ai => allocations.Any(x => x.AmbulanceId == ai.AmbulanceId && !x.Refused && !x.Cancelled) & ai.Status == AmbulanceStatus.Leaving)); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "MobilizedAndLeaving", allocations.Any(x => x.AmbulanceId == a[i].AmbulanceId && !x.Refused && !x.Cancelled) & a[i].Status == AmbulanceStatus.Leaving); } ms.Set("AmbMobilized", amb.Any(ai => incidents.Any(ii => ii != null && allocations.Any(x => x.AmbulanceId == ai.AmbulanceId & !x.Refused & !x.Cancelled & x.IncidentId == ii.IncidentId & x.MobilizationConfirmedTimestamp != null)) )); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "Mobilized", incidents.Any(ii => ii != null && allocations.Any(x => x.AmbulanceId == a[i].AmbulanceId & !x.Refused & !x.Cancelled & x.IncidentId == ii.IncidentId & x.MobilizationConfirmedTimestamp != null)) ); } ms.Set("AmbAllocated", amb.Any(ai => incidents.Any( ii => ii != null && allocations.Any( x => x.AmbulanceId == ai.AmbulanceId & !x.Refused & !x.Cancelled & x.IncidentId == ii.IncidentId)))); for (int i = 0; i < 15; i++) { int id = i + 1; bool allocated = incidents.Any(ii => ii != null && !ii.Resolved && allocations.Any(x => x.AmbulanceId == a[i].AmbulanceId & !x.Refused & !x.Cancelled & x.IncidentId == ii.IncidentId)); ms.Set("AmbA" + id + "Allocated", allocated); //logger.Info("Amb A" + id + "Allocated: " + allocated + " and on road: " + (a[i].Status == AmbulanceStatus.AvailableRadio)); } ms.Set("AmbLeaving", amb.Any(ai => ai.Status == AmbulanceStatus.Leaving)); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "Leaving", a[i].Status == AmbulanceStatus.Leaving); } for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "AvailableAtRadio", a[i].Status == AmbulanceStatus.AvailableRadio); } for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "StuckInTraffic", a[i].InTrafficJam); } ms.Set("AmbMobilizedAndToHospital", amb.Any(ai => { var allocAi = getAmbAlloc(ai); return(allocAi != null && allocAi.MobilizationConfirmedTimestamp != null && ai.Status == AmbulanceStatus.ToHospital); })); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "MobilizedAndToHospital", ambAlloc[i] != null && ambAlloc[i].MobilizationConfirmedTimestamp != null && a[i].Status == AmbulanceStatus.ToHospital); } //ms.Set("AmbA9MobilizedAndToHospital", allocA9 != null && allocA9.MobilizationConfirmedTimestamp != null && a9.Status == AmbulanceStatus.ToHospital); //ms.Set("AmbA2MobilizedToHospital", allocA2 != null && allocA2.MobilizationConfirmedTimestamp != null & a2.Status == AmbulanceStatus.ToHospital); //ms.Set("AmbA15MobilizedAndToHospital", allocA15 != null && allocA15.MobilizationConfirmedTimestamp != null & a15.Status == AmbulanceStatus.ToHospital); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbA" + id + "NotStuckInTrafficAndNotAmbA" + id + "AtHospital", !a[i].InTrafficJam & a[i].Status != AmbulanceStatus.AtHospital); } //ms.Set("AmbA9NotStuckInTrafficAndNotAmbA9AtHospital", !a9.InTrafficJam & a9.Status != AmbulanceStatus.AtHospital); //ms.Set("AmbA2NotStuckInTrafficAndNotAmbA2AtHospital", !a2.InTrafficJam & a2.Status != AmbulanceStatus.AtHospital); //ms.Set("AmbA15NotStuckInTrafficAndNotAmbA15AtHospital", !a15.InTrafficJam & a15.Status != AmbulanceStatus.AtHospital); ms.Set("AmbulanceOnScene", incidents.Any(x => { var alloci = getIncAlloc(x); return(alloci != null && getAmbulance(alloci.AmbulanceId).Status == AmbulanceStatus.OnScene); })); for (int i = 0; i < nIncident; i++) { var condition = alloc[i] != null && getAmbulance(alloc[i].AmbulanceId).Status == AmbulanceStatus.OnScene; ms.Set("AmbulanceOnScene" + (i + 1), condition); //if (condition) { // logger.Info("ambulance on scene: " + alloc[i].AmbulanceId + " - " + getAmbulance(alloc[i].AmbulanceId).AmbulanceId + " - " + incidents[i].Unreachable); //} } //ms.Set("AmbulanceOnScene1", alloc1 != null && getAmbulance(alloc1.AmbulanceId).Status == AmbulanceStatus.OnScene); //ms.Set("AmbulanceOnScene2", alloc2 != null &&getAmbulance(alloc2.AmbulanceId).Status == AmbulanceStatus.OnScene); //ms.Set("AmbulanceOnScene3", alloc3 != null &&getAmbulance(alloc3.AmbulanceId).Status == AmbulanceStatus.OnScene); for (int i = 0; i < nIncident; i++) { ms.Set("DestinationUnreachable" + (i + 1), incidents[i] != null && incidents[i].Unreachable); } //ms.Set("DestinationUnreachable1", inc1 != null && !inc1.Reachable); //ms.Set("DestinationUnreachable2", inc2 != null && !inc2.Reachable); //ms.Set("DestinationUnreachable3", inc3 != null && !inc3.Reachable); var radius = 50; var onSceneLocation = false; for (int i = 0; i < 15; i++) { int id = i + 1; var allocinc = (ambAlloc[i] != null) ? getIncident(ambAlloc[i].IncidentId) : null; if (ambAlloc[i] != null && allocinc != null) { var dist = Coordinate.DistanceEstimateInMeter( new Coordinate(allocinc.Latitude, allocinc.Longitude), new Coordinate(a[i].Latitude, a[i].Longitude) ); //if (dist <= radius) { // logger.Info("Distance from incident: " + dist + " status: " + a[i].Status); //} ms.Set("AmbulanceA" + id + "OnSceneLocation", dist <= radius); onSceneLocation |= dist <= radius; } else { ms.Set("AmbulanceA" + id + "OnSceneLocation", false); } } ms.Set("AmbulanceOnSceneLocation", onSceneLocation); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbulanceA" + id + "OnScene", a[i].Status == AmbulanceStatus.OnScene); } for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbulanceA" + id + "ToHospital", a[i].Status == AmbulanceStatus.ToHospital); } radius = 10; onSceneLocation = false; for (int i = 0; i < 15; i++) { int id = i + 1; if (ambAlloc[i] != null) { var inci = getIncident(ambAlloc[i].IncidentId); if (inci != null && !inci.Resolved) { var h = hospitalRepository.GetHospital(ambAlloc[i].HospitalId); var dist = Coordinate.DistanceEstimateInMeter(new Coordinate(h.Latitude, h.Longitude), new Coordinate(a[i].Latitude, a[i].Longitude)); ms.Set("AmbulanceA" + id + "AtHospitalLocation", dist <= radius); onSceneLocation |= dist <= radius; } else { ms.Set("AmbulanceA" + id + "AtHospitalLocation", false); } } else { ms.Set("AmbulanceA" + id + "AtHospitalLocation", false); } } ms.Set("AmbulanceAtHospitalLocation", onSceneLocation); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbulanceA" + id + "AtHospital", a[i].Status == AmbulanceStatus.AtHospital); } // TODO onSceneLocation = false; for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbulanceA" + id + "AtStationLocation", a[i].Status == AmbulanceStatus.AvailableAtStation); onSceneLocation |= a[i].Status == AmbulanceStatus.AvailableAtStation; } ms.Set("AmbulanceAtStationLocation", onSceneLocation); //ms.Set("AmbulanceA9AtStationLocation", a9.Status == AmbulanceStatus.AvailableAtStation); //ms.Set("AmbulanceA2AtStationLocation", a2.Status == AmbulanceStatus.AvailableAtStation); //ms.Set("AmbulanceA15AtStationLocation", a15.Status == AmbulanceStatus.AvailableAtStation); for (int i = 0; i < 15; i++) { int id = i + 1; ms.Set("AmbulanceA" + id + "AtStation", a[i].Status == AmbulanceStatus.AvailableAtStation); } //ms.Set("AmbulanceA9AtStation", a9.Status == AmbulanceStatus.AvailableAtStation); //ms.Set("AmbulanceA2AtStation", a2.Status == AmbulanceStatus.AvailableAtStation); //ms.Set("AmbulanceA15AtStation", a15.Status == AmbulanceStatus.AvailableAtStation); var currentAllocator = configurationRepository.GetActiveAllocator(); ms.Set("DefaultAllocator", currentAllocator == "DefaultAmbulanceAllocator"); ms.Set("AmbulanceAtStationAllocator", currentAllocator == "AmbulanceAtStationAllocator"); return(ms); }
public CachedHashMonitoredState(MonitoredState ms, DateTime now, HashSet <string> projection) : base(ms) { Timestamp = now; StateHash = GetProjectedHashCode(projection); //Console.WriteLine("Projecting on " + string.Join(",", projection) + " > " + StateHash); }