Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011-2014 Rinde van Lon, iMinds DistriNet, KU Leuven
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *         http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.github.rinde.rinsim.scenario.measure;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Sets.newLinkedHashSet;
 
 import java.util.List;
 import java.util.Set;
 
 

Author(s):
Rinde van Lon
 
 public final class Metrics {
 
   private Metrics() {}

  
Computes the absolute load at every time instance of the specified scenario. Load is a measure of expected vehicle utilization.

Parameters:
s The com.github.rinde.rinsim.scenario.Scenario to measure.
Returns:
A list of load values. The value at index i indicates the load at time i. All values are always ≥ 0. All time instances not included in the list are assumed to have load 0.
 
     return measureLoad(s, 1);
   }
 
   static ImmutableList<DoublemeasureLoad(Scenario sint numVehicles) {
     final TravelTimes tt = ScenarioGenerator.createTravelTimes(s);
     final ImmutableList.Builder<LoadPartloadParts = ImmutableList.builder();
     for (final TimedEvent te : s.asList()) {
       if (te instanceof AddParcelEvent) {
         loadParts.addAll(measureLoad((AddParcelEventtett));
       }
     }
     return sum(0, loadParts.build(), numVehicles);
   }
 
     final int numVehicles = getEventTypeCounts(s).count(
         .);
     return measureLoad(snumVehicles);
   }
 
       TravelTimes tt) {
     checkArgument(
         event.parcelDTO.pickupTimeWindow.begin <= event.parcelDTO.deliveryTimeWindow.begin,
         "Delivery TW begin may not be before pickup TW begin.");
     checkArgument(
         event.parcelDTO.pickupTimeWindow.end <= event.parcelDTO.deliveryTimeWindow.end,
         "Delivery TW end may not be before pickup TW end.");
 
     // pickup lower bound,
     final long pickupLb = event.parcelDTO.pickupTimeWindow.begin;
     // pickup upper bound
     final long pickupUb = event.parcelDTO.pickupTimeWindow.end
         + event.parcelDTO.pickupDuration;
     final double pickupLoad = event.parcelDTO.pickupDuration
         / (double) (pickupUb - pickupLb);
    final LoadPart pickupPart = new LoadPart(pickupLbpickupUbpickupLoad);
    final long expectedTravelTime = tt.getShortestTravelTime(
        event.parcelDTO.pickupLocation,
        event.parcelDTO.deliveryLocation);
    // first possible departure time from pickup location
    final long travelLb = pickupLb + event.parcelDTO.pickupDuration;
    // latest possible arrival time at delivery location
    final long travelUb = Math.max(event.parcelDTO.deliveryTimeWindow.end,
        travelLb + expectedTravelTime);
    final double travelLoad = expectedTravelTime
        / (double) (travelUb - travelLb);
    final LoadPart travelPart = new LoadPart(travelLbtravelUbtravelLoad);
    // delivery lower bound: the first possible time the delivery can start,
    // normally uses the start of the delivery TW, in case this is not
    // feasible we correct for the duration and travel time.
    final long deliveryLb = Math.max(event.parcelDTO.deliveryTimeWindow.begin,
        pickupLb + event.parcelDTO.pickupDuration + expectedTravelTime);
    // delivery upper bound: the latest possible time the delivery can end
    final long deliveryUb = Math.max(event.parcelDTO.deliveryTimeWindow.end,
        deliveryLb) + event.parcelDTO.deliveryDuration;
    final double deliveryLoad = event.parcelDTO.deliveryDuration
        / (double) (deliveryUb - deliveryLb);
    final LoadPart deliveryPart = new LoadPart(deliveryLbdeliveryUb,
        deliveryLoad);
    return ImmutableList.of(pickupParttravelPartdeliveryPart);
  }
  static ImmutableList<Doublesum(long stList<LoadPartpartsint num) {
    checkArgument(num >= 1);
    final ImmutableList.Builder<Doublebuilder = ImmutableList.builder();
    long i = st;
    final Set<LoadPartpartSet = newLinkedHashSet(parts);
    while (!partSet.isEmpty()) {
      double currentLoadVal = 0d;
      final List<LoadParttoRemove = newArrayList();
      for (final LoadPart lp : partSet) {
        if (lp.isIn(i)) {
          currentLoadVal += lp.get(i);
        }
        if (!lp.isBeforeEnd(i)) {
          toRemove.add(lp);
        }
      }
      partSet.removeAll(toRemove);
      if (!partSet.isEmpty()) {
        if (num > 1) {
          currentLoadVal /= num;
        }
        builder.add(currentLoadVal);
        i++;
      }
    }
    return builder.build();
  }

  
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.

Parameters:
s The com.github.rinde.rinsim.scenario.Scenario to get the speed from.
Returns:
The vehicle speed if all vehicles have the same speed.
Throws:
java.lang.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.asList()) {
      if (te instanceof AddVehicleEvent) {
        if (vehicleSpeed == -1d) {
          vehicleSpeed = ((AddVehicleEventte)..;
        } else {
          checkArgument(
              vehicleSpeed == ((AddVehicleEventte)..,
              "All vehicles are expected to have the same speed.");
        }
      }
    }
    checkArgument(vehicleSpeed > 0, "There are no vehicles in the scenario.");
    return vehicleSpeed;
  }

  
Computes the number of occurrences of each event type in the specifies com.github.rinde.rinsim.scenario.Scenario.

Parameters:
s The scenario to check.
Returns:
A com.google.common.collect.ImmutableMultiset of event types.
  public static ImmutableMultiset<Enum<?>> getEventTypeCounts(Scenario s) {
    final ImmutableMultiset.Builder<Enum<?>> occurences = ImmutableMultiset
        .builder();
    for (final TimedEvent te : s.asList()) {
      occurences.add(te.getEventType());
    }
    return occurences.build();
  }

  
Checks the time window strictness of the specified com.github.rinde.rinsim.scenario.Scenario. A time window is strict if:
  • dtw.begin &ge; ptw.begin + pd + travelTime
  • ptw.end + pd + travelTime &le; dtw.end
Where:

Parameters:
s The scenario to check.
  public static void checkTimeWindowStrictness(Scenario s) {
    final TravelTimes tt = ScenarioGenerator.createTravelTimes(s);
    for (final TimedEvent te : s.asList()) {
      if (te instanceof AddParcelEvent) {
        checkParcelTWStrictness((AddParcelEventtett);
      }
    }
  }

  
Checks whether the TWs are not unnecessarily big.

Parameters:
event
travelTimes
  static void checkParcelTWStrictness(AddParcelEvent event,
      TravelTimes travelTimes) {
    final long firstDepartureTime = event.parcelDTO.pickupTimeWindow.begin
        + event.parcelDTO.pickupDuration;
    final long latestDepartureTime = event.parcelDTO.pickupTimeWindow.end
        + event.parcelDTO.pickupDuration;
    final double travelTime = travelTimes.getShortestTravelTime(
        event.parcelDTO.pickupLocationevent.parcelDTO.deliveryLocation);
        event.parcelDTO.deliveryTimeWindow.begin >= firstDepartureTime
            + travelTime,
        "The begin of the delivery time window (%s) is too early, should be >= %s.",
        event.parcelDTO.deliveryTimeWindowfirstDepartureTime + travelTime);
        latestDepartureTime + travelTime <= event.parcelDTO.deliveryTimeWindow.end,
        "The end of the pickup time window %s is too late, or end of delivery is too early.",
        event.parcelDTO.pickupTimeWindow.end);
  }

  
Collects all event arrival times of the specified com.github.rinde.rinsim.scenario.Scenario.

Parameters:
s The scenario.
Returns:
com.google.common.collect.ImmutableList containing event arrival times.
  public static ImmutableList<LonggetArrivalTimes(Scenario s) {
    final ImmutableList.Builder<Longbuilder = ImmutableList.builder();
    for (final TimedEvent te : s.asList()) {
      if (te instanceof AddParcelEvent) {
        builder.add(te.time);
      }
    }
    return builder.build();
  }

  
Computes a histogram for the inputs. The result is a multiset with an entry for each bin in ascending order, the count of each entry indicates the size of the bin. A bin is indicated by its leftmost value, for example if the binSize is 2 and the result contains 4 with count 3 this means that there are 3 values in range 2 &le; x < 4.

Parameters:
input The values to compute the histogram of.
binSize The size of the bins.
Returns:
An com.google.common.collect.ImmutableSortedMultiset representing the histogram.
      Iterable<Doubleinputdouble binSize) {
    final ImmutableSortedMultiset.Builder<Doublebuilder = ImmutableSortedMultiset
        .naturalOrder();
    for (final double d : input) {
      checkArgument(!Double.isInfinite(d) && !Double.isNaN(d),
          "Only finite numbers are accepted, found %s."d);
      builder.add(Math.floor(d / binSize) * binSize);
    }
    return builder.build();
  }
  static long pickupUrgency(AddParcelEvent event) {
    return pickupUrgency(event.parcelDTO);
  }
  static long pickupUrgency(ParcelDTO dto) {
    return dto.pickupTimeWindow.end - dto.orderAnnounceTime;
  }
  enum Urgency implements Function<AddParcelEventLong> {
    PICKUP {
      @Override
      public Long apply(AddParcelEvent input) {
        return pickupUrgency(input);
      }
    }
  }
      Iterable<? extends Numbervalues) {
    final SummaryStatistics ss = new SummaryStatistics();
    for (final Number n : values) {
      ss.addValue(n.doubleValue());
    }
    return ss.getSummary();
  }

  
Computes a org.apache.commons.math3.stat.descriptive.StatisticalSummary object for all urgency values of a com.github.rinde.rinsim.scenario.Scenario.

Parameters:
s The scenario to measure.
Returns:
A statistical summary of the urgency values in the specified scenario.
  public static StatisticalSummary measureUrgency(Scenario s) {
    final List<LongurgencyValues = FluentIterable.from(s.asList())
        .filter(AddParcelEvent.class)
        .transform(.)
        .toList();
    return toStatisticalSummary(urgencyValues);
  }

  
Measures the degree of dynamism of the specified scenario.

Parameters:
s The scenario to measure.
Returns:
A double in range [0,1].
  public static double measureDynamism(Scenario s) {
    return measureDynamism(ss.getTimeWindow().);
  }

  
Measures the degree of dynamism of the specified scenario using the specified length of day.

Parameters:
s The scenario to measure.
lengthOfDay The length of the day.
Returns:
A double in range [0,1].
  public static double measureDynamism(Scenario slong lengthOfDay) {
        lengthOfDay);
  }
  // Best version as of March 6th, 2014
  
Computes degree of dynamism of the specified arrival times and length of day.

Parameters:
arrivalTimes A list of event arrival times.
lengthOfDay The length of the day.
Returns:
A number in range [0, 1].
  public static double measureDynamism(Iterable<DoublearrivalTimes,
      double lengthOfDay) {
    final List<Doubletimes = newArrayList(arrivalTimes);
    checkArgument(times.size() >= 2,
        "At least two arrival times are required, found %s time(s).",
        times.size());
    for (final double time : times) {
      checkArgument(time >= 0 && time < lengthOfDay,
          "all specified times should be >= 0 and < %s. Found %s.",
          lengthOfDaytime);
    }
    Collections.sort(times);
    final int numEvents = times.size();
    // this is the expected interarrival time
    final double expectedInterArrivalTime = lengthOfDay
        / numEvents;
    // deviation to expectedInterArrivalTime
    double sumDeviation = 0;
    double maxDeviation = (numEvents - 1) * expectedInterArrivalTime;
    double prevDeviation = 0;
    for (int i = 0; i < numEvents - 1; i++) {
      // compute interarrival time
      final double delta = times.get(i + 1) - times.get(i);
      if (delta < expectedInterArrivalTime) {
        final double diff = expectedInterArrivalTime - delta;
        final double scaledPrev = diff / expectedInterArrivalTime
            * prevDeviation;
        final double cur = diff + scaledPrev;
        sumDeviation += cur;
        maxDeviation += scaledPrev;
        prevDeviation = cur;
      } else {
        prevDeviation = 0;
      }
    }
    return 1d - sumDeviation / maxDeviation;
  }

  
Returns an com.google.common.collect.ImmutableList containing all service points of com.github.rinde.rinsim.scenario.Scenario. The scenario must contain com.github.rinde.rinsim.scenario.AddParcelEvents.

Parameters:
s The scenario to extract the points from.
Returns:
A list containing all service points in order of occurrence in the scenario event list.
  public static ImmutableList<PointgetServicePoints(Scenario s) {
    final ImmutableList.Builder<Pointbuilder = ImmutableList.builder();
    for (final TimedEvent se : s.asList()) {
      if (se instanceof AddParcelEvent) {
        builder.add(((AddParcelEventse)..);
        builder.add(((AddParcelEventse)..);
      }
    }
    return builder.build();
  }
    final ImmutableList.Builder<Longbuilder = ImmutableList.builder();
    for (final TimedEvent se : s.asList()) {
      if (se instanceof AddParcelEvent) {
        builder.add(((AddParcelEventse)..);
      }
    }
    return builder.build();
  }
  static ImmutableList<Doubleconvert(List<Longin) {
    final ImmutableList.Builder<Doublebuilder = ImmutableList.builder();
    for (final Long l : in) {
      builder.add(new Double(l));
    }
    return builder.build();
  }
  // to use for parts of the timeline to avoid excessively long list with
  // mostly 0s.
  static class LoadPart {
    private final double load;
    private final TimeWindow tw;
    LoadPart(long stlong enddouble value) {
       = new TimeWindow(stend);
       = value;
    }
    boolean isBeforeEnd(long i) {
      return .isBeforeEnd(i);
    }
    boolean isIn(long i) {
      return .isIn(i);
    }
    long begin() {
      return .;
    }
    long end() {
      return .;
    }
    long length() {
      return .length();
    }
    double get(long i) {
      if (.isIn(i)) {
        return ;
      }
      return 0d;
    }
    @Override
    public String toString() {
      return MoreObjects.toStringHelper("LoadPart").add("begin".)
          .add("end".).add("load").toString();
    }
  }
New to GrepCode? Check out our FAQ X