Ejemplo n.º 1
0
 private static List<Schedule> generateSchedules(
     List<ArrayList<CourseSelection>> selections, int count) {
   List<Schedule> schedules = new ArrayList<Schedule>();
   if (count == 0) {
     for (CourseSelection cs : selections.get(count)) {
       // This is one course's course selections.
       schedules.add(new Schedule(cs));
     }
   } else {
     schedules = generateSchedules(selections, count - 1);
     List<Schedule> nextSchedules = new ArrayList<Schedule>();
     for (Schedule s : schedules) {
       for (CourseSelection cs : selections.get(count)) {
         if (!s.collidesWith(cs)) {
           List<CourseSelection> temp = s.getCourseSelections();
           Schedule result = new Schedule(cs);
           for (int i = 0; i < temp.size(); i++) {
             result.addCourseSelection(temp.get(i));
           }
           nextSchedules.add(result);
         }
       }
     }
     return nextSchedules;
   }
   return schedules;
 }
Ejemplo n.º 2
0
  // remove overrunnable task-ending chunk, before deadline,
  public void testRemoveChunkTaskEndOver() {
    final List finished = new ArrayList();
    StepTask t1 =
        task(
            100,
            200,
            100,
            new TaskCallback() {
              public void taskEvent(SchedulableTask task, Schedule.EventType event) {
                if (log.isDebug2()) {
                  log.debug2("testRemoveChunkTaskEndOver callback");
                }
                if (event == Schedule.EventType.FINISH) {
                  finished.add(task);
                }
              }
            });
    t1.setOverrunAllowed(true);
    Schedule s = sched(ListUtil.list(t1));
    fact.setResult(s);
    assertTrue(tr.addToSchedule(t1));

    Schedule.Chunk chunk = (Schedule.Chunk) s.getEvents().get(0);
    assertTrue(tr.getCurrentSchedule().getEvents().contains(chunk));
    chunk.setTaskEnd();
    tr.removeChunk(chunk);
    assertFalse(tr.getCurrentSchedule().getEvents().contains(chunk));
    assertEmpty(finished);
    assertIsomorphic(ListUtil.list(t1), tr.getAcceptedTasks());
    assertIsomorphic(SetUtil.set(t1), tr.getOverrunTasks());
  }
Ejemplo n.º 3
0
  // remove task-ending chunk, past task deadline, s.b. Timeout error.
  public void testRemoveChunkTaskEndTimeout() {
    final List finished = new ArrayList();
    StepTask t1 =
        task(
            100,
            200,
            100,
            new TaskCallback() {
              public void taskEvent(SchedulableTask task, Schedule.EventType event) {
                if (log.isDebug2()) {
                  log.debug2("testRemoveChunkTaskEndTimeout callback");
                }
                if (event == Schedule.EventType.FINISH) {
                  finished.add(task);
                }
              }
            });
    Schedule s = sched(ListUtil.list(t1));
    fact.setResult(s);
    assertTrue(tr.addToSchedule(t1));

    Schedule.Chunk chunk = (Schedule.Chunk) s.getEvents().get(0);
    assertTrue(tr.getCurrentSchedule().getEvents().contains(chunk));
    chunk.setTaskEnd();
    TimeBase.setSimulated(201);
    tr.removeChunk(chunk);
    assertFalse(tr.getCurrentSchedule().getEvents().contains(chunk));
    assertSame(t1, finished.get(0));
    assertNotNull(t1.e);
    assertTrue(t1.e.toString(), t1.e instanceof SchedService.Timeout);
    assertEmpty(tr.getAcceptedTasks());
  }
Ejemplo n.º 4
0
  public static void main(String[] args) {
    Schedule s = createSchedule();
    System.out.println(s.totalCredits());
    System.out.println(s);

    testGetCourse(s);
    testRemove(s);
  }
Ejemplo n.º 5
0
  // Tests remove
  public static void testRemove(Schedule s) {
    Time t1 = new Time(12, 30, true);
    Time t2 = new Time(11, 30, false);

    s.remove(Weekday.MONDAY, t1);
    s.remove(Weekday.TUESDAY, t2);
    // s.remove(Weekday.FRIDAY, t2);
    s.remove(Weekday.MONDAY, t2);
    System.out.println(s.toString());
  }
Ejemplo n.º 6
0
  // Tests getCourse
  public static void testGetCourse(Schedule s) {
    Time t1 = new Time(12, 30, true);
    Time t2 = new Time(11, 30, false);

    System.out.println(s.getCourse(Weekday.MONDAY, t1));
    System.out.println(s.getCourse(Weekday.WEDNESDAY, t1));
    System.out.println(s.getCourse(Weekday.FRIDAY, t1));
    System.out.println(s.getCourse(Weekday.MONDAY, t2));
    System.out.println(s.getCourse(Weekday.TUESDAY, t2));
  }
Ejemplo n.º 7
0
  public void testRemoveChunk() {
    StepTask t1 = task(100, 200, 100);
    Schedule s = sched(ListUtil.list(t1));
    fact.setResult(s);
    assertTrue(tr.addToSchedule(t1));
    assertIsomorphic(ListUtil.list(t1), tr.getAcceptedTasks());

    Schedule.Chunk chunk = (Schedule.Chunk) s.getEvents().get(0);
    assertTrue(tr.getCurrentSchedule().getEvents().contains(chunk));
    tr.removeChunk(chunk);
    assertFalse(tr.getCurrentSchedule().getEvents().contains(chunk));
  }
Ejemplo n.º 8
0
    @Override
    protected void appendTasksAfterDropoff(Schedule<TaxiTask> schedule) {
      if (rankmode) {
        TaxiDropoffTask dropoffStayTask = (TaxiDropoffTask) Schedules.getLastTask(schedule);

        Link link = dropoffStayTask.getLink();
        Link startLink = schedule.getVehicle().getStartLink();

        if (link != startLink) {
          double t5 = dropoffStayTask.getEndTime();
          VrpPathWithTravelData path = calcPath(link, startLink, t5);
          schedule.addTask(new TaxiDriveTask(path));
        }
      }

      appendStayTask(schedule);
    }
Ejemplo n.º 9
0
  public void testFindChunkTaskToRun() {
    assertFalse(tr.findTaskToRun());
    StepTask t1 = task(100, 200, 100);
    StepTask t2 = task(100, 300, 50);

    Schedule s = sched(ListUtil.list(t1, t2));
    fact.setResults(s, s);
    assertTrue(tr.addToSchedule(t1));
    assertTrue(tr.addToSchedule(t2));
    assertFalse(tr.findTaskToRun());
    assertEquals(Deadline.at(100), tr.runningDeadline);

    TimeBase.setSimulated(101);
    assertTrue(tr.findTaskToRun());
    assertEquals(t1, tr.runningTask);
    assertEquals(t1.getLatestFinish(), tr.runningDeadline);
    assertEquals(s.getEvents().get(0), tr.runningChunk);
  }
Ejemplo n.º 10
0
 public void testFindRunnableChunk() {
   assertFalse(tr.findTaskToRun());
   StepTask t1 = task(100, 200, 100);
   StepTask t2 = task(10, 300, 50);
   Schedule.Chunk c1 = new Schedule.Chunk(t1, Deadline.at(100), Deadline.at(200), 100);
   Schedule.Chunk c2 = new Schedule.Chunk(t2, Deadline.at(200), Deadline.at(300), 100);
   Schedule s = new Schedule(ListUtil.list(c1, c2));
   fact.setResults(s, s);
   assertTrue(tr.addToSchedule(t1));
   assertTrue(tr.addToSchedule(t2));
   assertFalse(tr.findTaskToRun());
   assertEquals(Deadline.at(100), tr.runningDeadline);
   TimeBase.setSimulated(11);
   assertTrue(tr.findTaskToRun());
   assertEquals(t2, tr.runningTask);
   assertEquals(c2, tr.runningChunk);
   assertEquals(Deadline.at(100), tr.runningDeadline);
   assertEquals(s.getEvents().get(1), tr.runningChunk);
 }
Ejemplo n.º 11
0
  // Creates a new schedule
  public static Schedule createSchedule() {
    Time t1 = new Time(12, 30, true);
    Time t2 = new Time(11, 30, false);
    Time t3 = new Time(9, 30, false);
    Set<Weekday> d1 = new HashSet<Weekday>();
    d1.add(Weekday.MONDAY);
    d1.add(Weekday.WEDNESDAY);
    d1.add(Weekday.FRIDAY);

    Course c1 = new Course("Math 480", 3, d1, t1, 50);
    Course c2 = new Course("Math 324", 3, d1, t2, 50);
    Course c3 = new Course("Math 462", 3, d1, t3, 50);

    Schedule s = new Schedule();
    s.add(c1);
    s.add(c2);
    s.add(c3);

    return s;
  }
Ejemplo n.º 12
0
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    ScheduledEvent that = (ScheduledEvent) o;

    if (schedule != null ? !schedule.equals(that.schedule) : that.schedule != null) return false;
    if (source != null ? !source.equals(that.source) : that.source != null) return false;
    if (timeStamp != null ? !timeStamp.equals(that.timeStamp) : that.timeStamp != null)
      return false;

    return true;
  }
Ejemplo n.º 13
0
    protected void scheduleRankReturn(Vehicle veh, double time, boolean charge, boolean home) {
      @SuppressWarnings("unchecked")
      Schedule<Task> sched = (Schedule<Task>) veh.getSchedule();
      TaxiStayTask last = (TaxiStayTask) Schedules.getLastTask(veh.getSchedule());
      if (last.getStatus() != TaskStatus.STARTED) throw new IllegalStateException();

      last.setEndTime(time);
      Link currentLink = last.getLink();
      Link nearestRank;
      if (charge) {
        nearestRank = optimizer.getNearestFreeCharger(currentLink.getId());
        log.info("veh" + veh.getId() + " to charge");
      } else if (home) {
        nearestRank = veh.getStartLink();
        //        log.info("start" + veh.getId()+ " at " + time + " t1 " + veh.getT1());
        optimizer.homebound++;
      } else nearestRank = optimizer.getNearestFreeRank(currentLink.getId());

      VrpPathWithTravelData path = calcPath(currentLink, nearestRank, time);
      if (path.getArrivalTime() > veh.getT1())
        return; // no rank return if vehicle is going out of service anyway
      sched.addTask(new TaxiDriveTask(path));
      sched.addTask(new TaxiStayTask(path.getArrivalTime(), veh.getT1(), nearestRank));
    }
Ejemplo n.º 14
0
  public static List<Schedule> generateSchedules(
      List<Course> selectedCourses, List<Course> optionalCourses, int requiredOptional) {
    // Welcome to the worst sub of your life.

    // Original schedules: generate your mandatory course schedules. Easy.
    List<Schedule> originalSchedules = new ArrayList<Schedule>();
    if (selectedCourses.size() > 0) {
      originalSchedules = generateSchedules(selectedCourses);
    }

    // A string list to hold gray code.
    List<String[]> nChooseK = new ArrayList<String[]>();

    // Creates the digits that will be used, depending on how many optional courses there are.
    String[] inputs = new String[optionalCourses.size()];
    for (int j = 0; j < optionalCourses.size(); j++) {
      inputs[j] = Integer.toString(j);
    }

    // Run the gray code nChooseK sub.
    nChooseK = nChooseK(inputs, requiredOptional, 0, new String[requiredOptional], nChooseK);

    // Final schedules will be returned to the user.
    List<Schedule> finalSchedules = new ArrayList<Schedule>();

    // For each string array in nChooseK, meaning for each possible gray code...
    for (String[] sArray : nChooseK) {
      // A temporary schedule holder.
      List<Schedule> tempSchedules;

      // A temporary selections holder
      List<ArrayList<CourseSelection>> selections = new ArrayList<ArrayList<CourseSelection>>();

      // Of the nChooseK string, we add k courses to selections.
      for (String s : sArray) {
        selections.add(
            (ArrayList<CourseSelection>)
                optionalCourses.get(Integer.parseInt(s)).getCourseSelections());
      }
      tempSchedules = generateSchedules(selections, requiredOptional - 1);
      // Now we must merge tempSchedules with original schedules into final schedules.
      if (originalSchedules.size() == 0) {
        for (Schedule s : tempSchedules) {
          finalSchedules.add(s);
        }
      } else {
        for (Schedule s1 : originalSchedules) { // For every schedule generated previously,
          for (Schedule s2 : tempSchedules) {
            boolean valid = true;
            for (CourseSelection cs : s2.getCourseSelections()) {
              if (s1.collidesWith(cs)) {
                valid = false;
              }
            }
            if (valid) {
              // We found a valid match. Let's add them to the final list.
              List<CourseSelection> temp1 = s1.getCourseSelections();
              List<CourseSelection> temp2 = s2.getCourseSelections();
              Schedule result = null;
              for (CourseSelection cs : temp1) {
                if (result == null) {
                  result = new Schedule(cs);
                } else {
                  result.addCourseSelection(cs);
                }
              }
              for (CourseSelection cs : temp2) {
                result.addCourseSelection(cs);
              }
              finalSchedules.add(result);
            }
          }
        }
      }
    }
    return finalSchedules;
  }
Ejemplo n.º 15
0
 public int hashCode() {
   int result = source != null ? source.hashCode() : 0;
   result = 31 * result + (timeStamp != null ? timeStamp.hashCode() : 0);
   result = 31 * result + (schedule != null ? schedule.hashCode() : 0);
   return result;
 }
Ejemplo n.º 16
0
 public OrderWhenComponent copy(Order e) {
   OrderWhenComponent dst = e.new OrderWhenComponent();
   dst.code = code == null ? null : code.copy();
   dst.schedule = schedule == null ? null : schedule.copy();
   return dst;
 }