public static LaneSchedulerState RecursiveSearch(LaneSchedulerState state, List<LaneSchedulerReservation> reservations, int depth, long timelimit, long time) { if (time > timelimit) { //Debug.WriteLine("TIME LIMIT EXCEEDED-1! Time was: " + time); return null; } long time1 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; //Debug.WriteLine("Reached depth: " + depth); if (reservations.Count == 0) { return state; } reservations = (from y in reservations select y).OrderBy(y => state.GetReservationWeight(y)).ToList<LaneSchedulerReservation>(); foreach (LaneSchedulerReservation reservation in reservations) { // Get applicable actions List<LaneSchedulerAction> actions = Expand(state, reservation); actions = (from y in actions select y).OrderBy(y => y.weight).ToList<LaneSchedulerAction>(); // Loop for actions in a depth-first manner, backtracking if no solution is found. for (int num = 0; num < actions.Count; num++) { LaneSchedulerAction action = actions[num]; state.Apply(action); //if (!Scheduler.closedStateList.ContainsKey(state.ReservationRepr(action.reservation))) //{ List<LaneSchedulerReservation> remainingReservations = new List<LaneSchedulerReservation>(reservations); remainingReservations.Remove(action.reservation); int newDepth = depth + 1; long time2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; long runTime = time2 - time1; time = time + runTime; LaneSchedulerState solution = RecursiveSearch(state, remainingReservations, newDepth, timelimit, time); if (solution != null) { //Debug.WriteLine("Placed reservation: " + action.reservation.id); return solution; } else { //string stateRepr = state.ReservationRepr(action.reservation); //if (!Scheduler.closedStateList.ContainsKey(stateRepr)) //{ // Scheduler.closedStateList.Add(stateRepr, 1); //} state.Unapply(action); if (time > timelimit) { //Debug.WriteLine("TIME LIMIT EXCEEDED-2! Time was: " + time); return null; } //Debug.WriteLine(state.toString()); } //}else{ //Debug.WriteLine(" Hit an explored state!"); //Debug.WriteLine(state.toString()); //Debug.WriteLine(" " + state.ReservationRepr(action.reservation)); //state.Unapply(action); //return null; //} } //Debug.WriteLine(" Backtracking"); //Debug.WriteLine(state.toString()); } return null; }