public void EnqueueDeliveryRequest(Improbable.Entity.Component.ResponseHandle <DeliveryHandler.Commands.RequestDelivery, DeliveryRequest, DeliveryResponse> handle) { MetricsWriter.Send(new ControllerMetrics.Update().SetIncomingDeliveryRequests(++incomingRequests)); float estimatedTime = Vector3.Distance(gameObject.transform.position, handle.Request.destination.ToUnityVector()) / SimulationSettings.MaxDroneSpeed; QueueEntry queueEntry = new QueueEntry(Time.time, handle.Request, 0, estimatedTime); if (requestQueue.Count >= SimulationSettings.MaxDeliveryRequestQueueSize) { QueueEntry longestJob = requestQueue.Max; float duration, value, maxValue; if (estimatedTime > longestJob.expectedDuration) { handle.Respond(new DeliveryResponse(false)); value = TimeValueFunctions.ExpectedProfit(estimatedTime, estimatedTime, queueEntry.request.packageInfo, queueEntry.request.timeValueFunction); potential += value; rejecValue += value; ++rejections; return; } requestQueue.Remove(longestJob); duration = Time.time - longestJob.timestamp + longestJob.expectedDuration; value = TimeValueFunctions.ExpectedProfit(duration, longestJob.expectedDuration, longestJob.request.packageInfo, longestJob.request.timeValueFunction); maxValue = TimeValueFunctions.ExpectedProfit(longestJob.expectedDuration, longestJob.expectedDuration, longestJob.request.packageInfo, longestJob.request.timeValueFunction); potential += value; rejecValue += maxValue; ++rejections; } requestQueue.Add(queueEntry); handle.Respond(new DeliveryResponse(true)); }
void RegisterCompletedDelivery(DeliveryInfo deliveryInfo) { float deliveryTime = Time.time - deliveryInfo.timestamp; if (deliveryTime < 0) { Debug.LogError("DELIVERY TIME < 0 - BIG ERROR"); return; } ++completedDeliveries; avgDeliveryTime += deliveryTime; revenue += TimeValueFunctions.DeliveryValue(deliveryTime, deliveryInfo.packageInfo, deliveryInfo.timeValueFunction); }
private void PruneQueue() { int toRemove = requestQueue.Count - ((int)SimulationSettings.MaxDeliveryRequestQueueSize); for (int i = 0; i < toRemove; i++) { QueueEntry maxEntry = requestQueue.Max; float duration = Time.time - maxEntry.timestamp + maxEntry.expectedDuration; float value = TimeValueFunctions.ExpectedProfit(duration, maxEntry.expectedDuration, maxEntry.request.packageInfo, maxEntry.request.timeValueFunction); float maxValue = TimeValueFunctions.ExpectedProfit(maxEntry.expectedDuration, maxEntry.expectedDuration, maxEntry.request.packageInfo, maxEntry.request.timeValueFunction); requestQueue.Remove(maxEntry); potential += value; rejecValue += maxValue; ++rejections; } }
private void SortQueue() { QueueEntry[] entries = new QueueEntry[requestQueue.Count]; int i = 0; foreach (QueueEntry entry in requestQueue) { entries[i++] = entry.DeepCopy(); } for (int j = 0; j < entries.Length; j++) { float lostValue = 0; float maxDuration = float.MinValue; float entryDuration = entries[j].expectedDuration; float timePassed; for (int k = 0; k < entries.Length; k++) { if (j != k) { //timePassed = wait time so far + estimated time til the delivery timePassed = Time.time - entries[k].timestamp + entries[k].expectedDuration; lostValue += TimeValueFunctions.ExpectedProfit(timePassed, entries[k].expectedDuration, entries[k].request.packageInfo, entries[k].request.timeValueFunction) - TimeValueFunctions.ExpectedProfit(timePassed + entryDuration, entries[k].expectedDuration, entries[j].request.packageInfo, entries[j].request.timeValueFunction); maxDuration = Mathf.Max(maxDuration, entries[k].expectedDuration); } } //timePassed = wait time so far + estimated time til the delivery timePassed = Time.time - entries[j].timestamp + entries[j].expectedDuration; float wonValue = TimeValueFunctions.ExpectedProfit(timePassed, entries[j].expectedDuration, entries[j].request.packageInfo, entries[j].request.timeValueFunction) - TimeValueFunctions.ExpectedProfit(timePassed + maxDuration, entries[j].expectedDuration, entries[j].request.packageInfo, entries[j].request.timeValueFunction); entries[j].priority = lostValue - wonValue; } requestQueue.Clear(); foreach (QueueEntry entry in entries) { requestQueue.Add(entry); } sorted = true; }
// TODO: update properly with TVF A and TVF B private TimeValueFunction GenerateTVF(bool random) { //if bool true, return random tvf if (random) { return(GenerateRandomTVF()); } //if bool false, return either TVF B or TVF B by 50% chance of each if (UnityEngine.Random.Range(0f, 1f) < 0.5f) { //return TVF A return(TimeValueFunctions.GenerateTypeA(GenerateDeliveryType())); } //return TVF B return(TimeValueFunctions.GenerateTypeB(GenerateDeliveryType())); }
public void EnqueueDeliveryRequest(Improbable.Entity.Component.ResponseHandle <DeliveryHandler.Commands.RequestDelivery, DeliveryRequest, DeliveryResponse> handle) { MetricsWriter.Send(new ControllerMetrics.Update().SetIncomingDeliveryRequests(++incomingRequests)); float expectedDuration = Vector3.Distance(gameObject.transform.position, handle.Request.destination.ToUnityVector()) / SimulationSettings.MaxDroneSpeed; if (deliveryRequestQueue.Count >= SimulationSettings.MaxDeliveryRequestQueueSize) { handle.Respond(new DeliveryResponse(false)); float value = TimeValueFunctions.DeliveryValue(expectedDuration, handle.Request.packageInfo, handle.Request.timeValueFunction); potential += value; rejecValue += value; ++rejections; } else { deliveryRequestQueue.Enqueue(new QueueEntry(Time.time, handle.Request, 0, expectedDuration)); handle.Respond(new DeliveryResponse(true)); } }