/** * @return A queue containing all events of this scenario. */ public Queue<TimedEvent> asQueue() { return newLinkedList(getEvents()); }
/** * @return A queue containing all events of this scenario. */ public Queue<TimedEvent> asQueue() { return newLinkedList(getEvents()); }
@Override public boolean apply(@Nullable Scenario input) { if (input == null) { return false; } final int numOrders = Collections.frequency( Collections2.transform(input.getEvents(), ToClassFunction.INSTANCE), AddParcelEvent.class); return numOrders >= min && numOrders <= max; }
/** * Collects all event arrival times of the specified {@link Scenario}. * @param s The scenario. * @return {@link ImmutableList} containing event arrival times. */ public static ImmutableList<Long> getArrivalTimes(Scenario s) { final ImmutableList.Builder<Long> builder = ImmutableList.builder(); for (final TimedEvent te : s.getEvents()) { if (te instanceof AddParcelEvent) { builder.add(te.getTime()); } } return builder.build(); }
/** * Computes a {@link StatisticalSummary} object for all urgency values of a * {@link Scenario}. * @param s The scenario to measure. * @return A statistical summary of the urgency values in the specified * scenario. */ public static StatisticalSummary measureUrgency(Scenario s) { final List<Long> urgencyValues = FluentIterable.from(s.getEvents()) .filter(AddParcelEvent.class) .transform(Urgency.PICKUP) .toList(); return toStatisticalSummary(urgencyValues); }
static ImmutableList<Long> getOrderArrivalTimes(Scenario s) { final ImmutableList.Builder<Long> builder = ImmutableList.builder(); for (final TimedEvent se : s.getEvents()) { if (se instanceof AddParcelEvent) { builder .add(((AddParcelEvent) se).getParcelDTO().getOrderAnnounceTime()); } } return builder.build(); }
static ImmutableList<Double> measureLoad(Scenario s, int numVehicles) { final TravelTimes tt = ScenarioGenerator.createTravelTimes(s); final ImmutableList.Builder<LoadPart> loadParts = ImmutableList.builder(); for (final TimedEvent te : s.getEvents()) { if (te instanceof AddParcelEvent) { loadParts.addAll(measureLoad((AddParcelEvent) te, tt)); } } return sum(0, loadParts.build(), numVehicles); }
@Nonnull @Override public Scenario apply(@Nullable Scenario input) { final Scenario in = verifyNotNull(input); return Scenario.builder(in) .clearEvents() .addEvents( FluentIterable.from(in.getEvents()).transform(converter)) .build(); } };
@Nonnull @Override public Scenario apply(@Nullable Scenario input) { final Scenario in = verifyNotNull(input); return Scenario.builder(in) .clearEvents() .addEvents( FluentIterable.from(in.getEvents()).transform(converter)) .build(); } };
/** * Returns an {@link ImmutableList} containing all service points of * {@link Scenario}. The scenario must contain {@link AddParcelEvent}s. * @param s The scenario to extract the points from. * @return A list containing all service points in order of occurrence in the * scenario event list. */ public static ImmutableList<Point> getServicePoints(Scenario s) { final ImmutableList.Builder<Point> builder = ImmutableList.builder(); for (final TimedEvent se : s.getEvents()) { if (se instanceof AddParcelEvent) { builder.add(((AddParcelEvent) se).getParcelDTO().getPickupLocation()); builder.add(((AddParcelEvent) se).getParcelDTO().getDeliveryLocation()); } } return builder.build(); }
/** * Computes the number of occurrences of each event type in the specified * {@link Scenario}. * @param s The scenario to check. * @return A {@link ImmutableMultiset} of event types. */ public static ImmutableMultiset<Class<?>> getEventTypeCounts(Scenario s) { final Multiset<Class<?>> set = LinkedHashMultiset.create(); for (final TimedEvent te : s.getEvents()) { set.add(te.getClass()); } final List<Class<?>> toMove = new ArrayList<>(); for (final Class<?> c : set.elementSet()) { if (!Modifier.isPublic(c.getModifiers()) && TimedEvent.class.isAssignableFrom(c.getSuperclass()) && !set.contains(c.getSuperclass())) { toMove.add(c); } } for (final Class<?> c : toMove) { set.add(c.getSuperclass(), set.count(c)); set.remove(c, set.count(c)); } return ImmutableMultiset.copyOf(set); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof Scenario) { Scenario that = (Scenario) o; return (this.events.equals(that.getEvents())) && (this.modelBuilders.equals(that.getModelBuilders())) && (this.timeWindow.equals(that.getTimeWindow())) && (this.stopCondition.equals(that.getStopCondition())) && (this.problemClass.equals(that.getProblemClass())) && (this.problemInstanceId.equals(that.getProblemInstanceId())); } return false; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof Scenario) { Scenario that = (Scenario) o; return (this.events.equals(that.getEvents())) && (this.modelBuilders.equals(that.getModelBuilders())) && (this.timeWindow.equals(that.getTimeWindow())) && (this.stopCondition.equals(that.getStopCondition())) && (this.problemClass.equals(that.getProblemClass())) && (this.problemInstanceId.equals(that.getProblemInstanceId())); } return false; }
/** * Checks whether the vehicles defined for the specified scenario have the * same speed. If the speed is the same it is returned, otherwise an exception * is thrown. * @param s The {@link Scenario} to get the speed from. * @return The vehicle speed if all vehicles have the same speed. * @throws IllegalArgumentException if either: not all vehicles have the same * speed, or there are no vehicles. */ public static double getVehicleSpeed(Scenario s) { double vehicleSpeed = -1d; for (final TimedEvent te : s.getEvents()) { if (te instanceof AddVehicleEvent) { if (vehicleSpeed == -1d) { vehicleSpeed = ((AddVehicleEvent) te).getVehicleDTO().getSpeed(); } else { checkArgument( vehicleSpeed == ((AddVehicleEvent) te).getVehicleDTO().getSpeed(), "All vehicles are expected to have the same speed."); } } } checkArgument(vehicleSpeed > 0, "There are no vehicles in the scenario."); return vehicleSpeed; }
/** * Test no arg constructor. */ @Test public void testNoArgConstructor() { final Scenario scenario = new EmptyScenario(); assertTrue(scenario.getEvents().isEmpty()); }
@Override public Builder copyProperties(Scenario scenario) { return super.copyProperties(scenario) .addEvents(scenario.getEvents()) .problemClass(scenario.getProblemClass()) .instanceId(scenario.getProblemInstanceId()) .addModels(scenario.getModelBuilders()); }
@Override public Builder copyProperties(Scenario scenario) { return super.copyProperties(scenario) .addEvents(scenario.getEvents()) .problemClass(scenario.getProblemClass()) .instanceId(scenario.getProblemInstanceId()) .addModels(scenario.getModelBuilders()); }
/** * Test copying by builder. */ @Test public void testCreateScenarioByCopying() { final Scenario s = Scenario.builder() .addEvent(AddObjectEvent.create(100, new Point(0, 0))) .addEvent(AddObjectEvent.create(200, new Point(0, 0))) .addEvent(AddObjectEvent.create(300, new Point(0, 0))) .build(); assertEquals(3, s.getEvents().size()); final Scenario s2 = Scenario.builder(s).build(); assertEquals(3, s.getEvents().size()); assertEquals(3, s2.getEvents().size()); }
/** * Tests whether events are sorted by time. */ @Test public void testSorting() { final List<TimedEvent> events = new ArrayList<>(10); final AddObjectEvent A1 = AddObjectEvent.create(0, new Point(1, 0)); final AddObjectEvent A2 = AddObjectEvent.create(0, new Point(2, 0)); final AddObjectEvent B = AddObjectEvent.create(1, new Point(1, 1)); final AddObjectEvent C = AddObjectEvent.create(2, new Point(1, 0)); final AddObjectEvent D1 = AddObjectEvent.create(3, new Point(1, 2)); final AddObjectEvent D2 = AddObjectEvent.create(3, new Point(1, 3)); final AddObjectEvent E = AddObjectEvent.create(4, new Point(2, 0)); final AddObjectEvent F = AddObjectEvent.create(5, new Point(4, 0)); events.addAll(asList(A1, A2, B, C, D1, D2, E, F)); Collections.reverse(events); final Scenario s = Scenario.builder().addEvents(events).build(); final List<TimedEvent> res = newArrayList(s.getEvents()); assertEquals(asList(A2, A1, B, C, D2, D1, E, F), res); assertFalse(res.equals(events)); assertEquals(events.size(), res.size()); }
/** * Test correct ordering of events. */ @Test public void testAddEvents() { final TimedEvent ev0 = EventA.create(0); final TimedEvent ev1 = EventB.create(205); final TimedEvent ev2 = EventB.create(7); final TimedEvent ev3 = EventB.create(203); final Scenario scenario = Scenario .builder(Scenario.DEFAULT_PROBLEM_CLASS) .addEvent(ev0) .addEvent(ev1) .addEvents(asList(ev2, ev3)) .build(); assertEquals(asList(ev0, ev2, ev3, ev1), scenario.getEvents()); }