using System; using System.Collections.Generic; //using System.Linq; using System.Web; using System.Text; namespace AtomicCityBusScheduler { public class Route { public List cLocation; public string name; public List cTripPattern; public List cTrip; public Route() { cLocation = new List(); cTripPattern = new List(); } public List setcTrip() { cTrip = new List(); foreach (TripPattern tp in cTripPattern) { cTrip.AddRange(tp.setcTrip()); } // storing yesterday's trips. int nTrip = cTrip.Count; int day = 0; while (cTrip.Count < (nTrip + 4)) { day = day + 1; for (int ii = 0; ii < nTrip; ii++) { Trip yTrip = cTrip[ii].Copy(); for (int i = 0; i < yTrip.c1ArrivalTime.Count; i++) { yTrip.c1ArrivalTime[i] = yTrip.c1ArrivalTime[i] - (day * 24 * 60); } cTrip.Add(yTrip); } } return cTrip; } public List cLocationIndex(AtomicCityBusScheduler.Location aLocation) { List cLocationIndex = new List(); List cLocation = this.cLocation; for (int i = 0; i < cLocation.Count; i++) { if (aLocation == cLocation[i]) { cLocationIndex.Add(i); } } if (cLocationIndex.Contains(0) & cLocationIndex.Contains(cLocation.Count - 1)) { cLocationIndex.Remove(0); } return cLocationIndex; } public List cTripNearestTime(AtomicCityBusScheduler.Location aLocation, int time) { List cTripNearestTime = new List(); List cLocationIndex = this.cLocationIndex(aLocation); for (int i = 0; i < cTrip.Count; i++) { Trip aTrip = cTrip[i]; if (cLocationIndex.Exists(delegate(int index) { return aTrip.c1ArrivalTime[index] < time; })) { cTripNearestTime.Add(aTrip); } } int j = cLocationIndex[cLocationIndex.Count - 1]; cTripNearestTime.Sort(delegate(Trip t1, Trip t2) { return t1.c1ArrivalTime[j].CompareTo(t2.c1ArrivalTime[j]); }); return cTripNearestTime; } public List startandEndTime(AtomicCityBusScheduler.Location startDest, AtomicCityBusScheduler.Location endDest, int aArrivalTime) { List cTripNearestTime = this.cTripNearestTime(endDest, aArrivalTime); int index = cTripNearestTime.Count - 1; Trip bestTrip = cTripNearestTime[index].Copy(); JoinedTrip bestJoinedTrip = new JoinedTrip(); bool found = false; while (!found) { index--; Trip previousTrip = cTripNearestTime[index]; if (!(previousTrip.overlaps(bestTrip))) { found = true; bestJoinedTrip = previousTrip.Append(bestTrip); } } index = cTripNearestTime.Count - 2; Trip secondBestTrip = cTripNearestTime[index].Copy(); JoinedTrip secondBestJoinedTrip = new JoinedTrip(); found = false; while (!found) { index--; Trip previousTrip = cTripNearestTime[index]; if (!(previousTrip.overlaps(secondBestTrip))) { found = true; secondBestJoinedTrip = previousTrip.Append(secondBestTrip); } } List bestStartandEndTime = bestJoinedTrip.startandEndTime(startDest, endDest, aArrivalTime); List secondBestStartEndTime = secondBestJoinedTrip.startandEndTime(startDest, endDest, aArrivalTime); bestStartandEndTime.AddRange(secondBestStartEndTime); return bestStartandEndTime; } public AtomicCityBusScheduler.Location joinStartandEnd(Route startRoute, AtomicCityBusScheduler.Location startDest, AtomicCityBusScheduler.Location endDest) { List csDest = new List(); List ceDest = new List(); csDest.AddRange(startRoute.cLocation); ceDest.AddRange(this.cLocation); List commonDest = new List(); foreach (AtomicCityBusScheduler.Location str in csDest) { if (ceDest.Contains(str)) { commonDest.Add(str); } } int endIndex = -1; List cEndDest = this.cLocation; for (int i = cEndDest.Count - 1; endIndex < 0; i--) { if (cEndDest[i] == endDest) { endIndex = i; } } int startIndex = -1; for (int i = endIndex - 1; (startIndex < 0) & (i >= 0); i--) { if (cEndDest[i] == endDest) { endIndex = i; } if (commonDest.Contains(cEndDest[i])) { startIndex = i; } } return cEndDest[startIndex]; } public TripPattern addTripPattern(TripPattern aTripPattern) { cTripPattern.Add(aTripPattern); aTripPattern.aRoute = this; return aTripPattern; } public bool ContainsEnd(AtomicCityBusScheduler.Location endDest) { for (int i = 1; i < cLocation.Count; i++) { if (endDest == cLocation[i]) { return true; } } return false; } public bool ContainsStart(AtomicCityBusScheduler.Location startDest) { for (int i = 0; i < cLocation.Count - 1; i++) { if (startDest == cLocation[i]) { return true; } } return false; } public String getLocationIndex(AtomicCityBusScheduler.Location aLocation, AtomicCityBusScheduler.Location bLocation) { string path = ""; bool foundEnd = false, foundStart=false; int index = 0; List cLocation = this.cLocation; while (!foundEnd) { for (index = 0; index < cLocation.Count; index++) { if (aLocation == cLocation[index]) { path = "from: " + aLocation.latitude + " , " + aLocation.longitude; foundStart = true; } else if (bLocation == cLocation[index] && foundStart== true) { path = path + " to: " + bLocation.latitude + " , " + bLocation.longitude; foundEnd = true; return path; } else if (foundStart == true) { path = path + " to: " + cLocation[index].latitude + " , " + cLocation[index].longitude; } } index = 1; } return ""; } } }