// Add an order to the list // Keep in mind: The Travel Time is in seconds! public RouteSim AddOrder(Order destination, int location = -1) { RouteSim result = new RouteSim (); Route obj = this; if (this.Orders.Count == 0) { if (this.Clearings.Count == 0) { this.Clearings.AddLast(new GarbageStop ()); } result.getRoute = () => { RouteStop stop = new RouteStop(); stop.Order = destination; stop.Node = obj.Orders.AddLast(stop); stop.NextClearing = obj.Clearings.Last; stop.NextClearing.Value.Amount = stop.Order.CompressedVolume; stop.NextClearing.Value.Last = stop.Node; obj.EnRouteTime = Program.afstandenMatrix [287, destination.MatrixID].TravelTime + destination.LoadTime + Program.afstandenMatrix [destination.MatrixID, 287].TravelTime + 1800; return obj; }; result.time = Program.afstandenMatrix [287, destination.MatrixID].TravelTime + destination.LoadTime + Program.afstandenMatrix [destination.MatrixID, 287].TravelTime + 1800; if (result.time > dayTimeSec) { throw new OverflowException ("Too much!"); } return result; } if (location == -1) { Random rnd = new Random (); location = rnd.Next(this.Orders.Count); } RouteStop next = this.Orders.ElementAt(location); int prevID; if (next.Node.Previous != null) { prevID = next.Node.Previous.Value.Order.MatrixID; } else { prevID = 287; // The garbage disposal location } int curID = destination.MatrixID; int nextID = next.Order.MatrixID; float EnRouteTime = this.EnRouteTime; EnRouteTime -= Program.afstandenMatrix [prevID, nextID].TravelTime; EnRouteTime += Program.afstandenMatrix [prevID, curID].TravelTime; EnRouteTime += Program.afstandenMatrix [curID, nextID].TravelTime; EnRouteTime += destination.LoadTime; var nextClearing = next.NextClearing; RouteStop stop2 = new RouteStop(); stop2.Order = destination; stop2.Node = obj.Orders.AddBefore (next.Node, stop2); stop2.NextClearing = nextClearing; var fixSim = FixClearings (nextClearing, destination.CompressedVolume, false); obj.Orders.Remove (stop2.Node); EnRouteTime += fixSim.delta; result.getRoute = () => { //fixSim.getRoute(); // Get the new time and distance required obj.EnRouteTime = EnRouteTime; stop2.Node = obj.Orders.AddBefore (next.Node, stop2); obj = fixSim.getRoute(); return obj; }; result.time = EnRouteTime; if (result.time > dayTimeSec) { throw new OverflowException ("Too much!"); } return result; }
// O(n) time to remove a specified node public RouteSim RemoveOrder(RouteStop order) { RouteSim result = new RouteSim (); if (this.Orders.Count == 1 && this.Orders.First.Value == order) { result.time = 0; result.getRoute = () => { return new Route(this.GarbageTruck, this.DayId); }; return result; } float EnRouteTime = this.EnRouteTime; EnRouteTime -= order.Order.LoadTime; int prevID = 287; int nextID = 287; int curID = order.Order.MatrixID; if (order.Node.Previous != null) { prevID = order.Node.Previous.Value.Order.MatrixID; } if (order.NextClearing.Value.Last.Value != order) { nextID = order.Node.Next.Value.Order.MatrixID; } EnRouteTime -= Program.afstandenMatrix [curID, nextID].TravelTime; EnRouteTime -= Program.afstandenMatrix [prevID, curID].TravelTime; EnRouteTime += Program.afstandenMatrix [prevID, nextID].TravelTime; var next = order.Node.Next; bool changedClearing = false; if (order.NextClearing.Value.Last.Value == order) { order.NextClearing.Value.Last = order.Node.Previous; changedClearing = true; } this.Orders.Remove (order.Node); var fixSim = FixClearings (order.NextClearing, order.Order.CompressedVolume, true); if (next != null) { this.Orders.AddBefore (next, order.Node); } else { this.Orders.AddLast (order.Node); } if (changedClearing) { order.NextClearing.Value.Last = order.Node; } EnRouteTime += fixSim.delta; result = new RouteSim (); Route obj = this; result.getRoute = () => { if (order.NextClearing.Value.Last == order.Node) { order.NextClearing.Value.Last = order.Node.Previous; } obj.Orders.Remove(order.Node); obj.EnRouteTime = EnRouteTime; obj = fixSim.getRoute(); return obj; }; result.time = EnRouteTime; return result; }