Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011-2015 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.generator;
 
 import static com.github.rinde.rinsim.util.StochasticSuppliers.constant;
 import static com.google.common.base.Preconditions.checkArgument;
 
 
 
Utility class for creating TimeWindows.TimeWindowGenerators.

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

  

Returns:
A new TimeWindows.Builder instance for creating TimeWindows.TimeWindowGenerators.
 
   public static Builder builder() {
     return new Builder();
   }

  
Generator of com.github.rinde.rinsim.util.TimeWindows for pickup and delivery problems.

Author(s):
Rinde van Lon
 
   public interface TimeWindowGenerator {
    
Should create two com.github.rinde.rinsim.util.TimeWindows, a pickup time window and a delivery time window. These time windows should be theoretically feasible, meaning that they should be serviceable such that there is enough time for a vehicle to return to the depot.

Parameters:
seed Random seed.
parcelBuilder The com.github.rinde.rinsim.core.pdptw.ParcelDTO.Builder that is being used for creating a com.github.rinde.rinsim.core.pdptw.ParcelDTO. The time windows should be added to this builder via the com.github.rinde.rinsim.core.pdptw.ParcelDTO.Builder.pickupTimeWindow(com.github.rinde.rinsim.util.TimeWindow) and com.github.rinde.rinsim.core.pdptw.ParcelDTO.Builder.deliveryTimeWindow(com.github.rinde.rinsim.util.TimeWindow) methods.
travelTimes An object that provides information about the travel times in the scenario.
endTime The end time of the scenario.
 
     void generate(long seedParcelDTO.Builder parcelBuilder,
         TravelTimes travelTimeslong endTime);
   }

  
A builder for creating com.github.rinde.rinsim.util.TimeWindow instances using urgency. Urgency is defined as follows:
  • pickup_urgency = pickupTW.R - orderAnnounceTime
  • delivery_urgency = deliveryTW.R - earliest possible leave time from pickup site

Author(s):
Rinde van Lon
 
   public static class Builder {
     private static final StochasticSupplier<LongDEFAULT_URGENCY = constant(30 * 60 * 1000L);
     private static final StochasticSupplier<LongDEFAULT_PICKUP_LENGTH = constant(10 * 60 * 1000L);
     private static final StochasticSupplier<LongDEFAULT_DELIVERY_OPENING = constant(0L);
     private static final StochasticSupplier<DoubleDEFAULT_DELIVERY_LENGTH_FACTOR = constant(2d);
 
 
     Builder() {
       = Optional.absent();
    }

    
Set the urgency of a pickup operation.

Parameters:
urgency The urgency of the pickup.
Returns:
This, as per the builder pattern.
    public Builder pickupUrgency(StochasticSupplier<Longurgency) {
       = urgency;
      return this;
    }

    
Sets the pickup time window length.

Parameters:
length The length to set.
Returns:
This, as per the builder pattern.
       = length;
      return this;
    }

    
Sets the opening of the delivery time window. The value is interpreted as the time relative to the earliest feasible delivery opening time: pickup opening + pickup duration + travel time from pickup to delivery .

Parameters:
opening May only return values which are &ge; 0.
Returns:
This, as per the builder pattern.
    public Builder deliveryOpening(StochasticSupplier<Longopening) {
       = opening;
      return this;
    }

    
Sets the length of delivery TW as a ratio to length of pickup TW.

Parameters:
factor May only return values which are > 0.
Returns:
This, as per the builder pattern.
       = factor;
      return this;
    }

    
The minimum length of the delivery time window.

Parameters:
del The minimum length.
Returns:
This, as per the builder pattern.
       = Optional.of(del);
      return this;
    }

    

Returns:
A new TimeWindows.TimeWindowGenerator instance.
    public TimeWindowGenerator build() {
      return new DefaultTimeWindowGenerator(this);
    }
  }
  static class DefaultTimeWindowGenerator implements TimeWindowGenerator {
    private final RandomGenerator rng;
    private final StochasticSupplier<LongpickupUrgency;
    private final StochasticSupplier<LongpickupTWLength;
    private final StochasticSupplier<LongdeliveryOpening;
       = new MersenneTwister();
       = b.pickupUrgency;
       = b.pickupTWLength;
       = b.deliveryOpening;
       = b.deliveryLengthFactor;
       = b.minDeliveryLength;
    }
    @Override
    public void generate(long seedParcelDTO.Builder parcelBuilder,
        TravelTimes travelTimeslong endTime) {
      .setSeed(seed);
      final long orderAnnounceTime = parcelBuilder.getOrderAnnounceTime();
      final Point pickup = parcelBuilder.getPickupLocation();
      final Point delivery = parcelBuilder.getDeliveryLocation();
      final long pickupToDeliveryTT = travelTimes.getShortestTravelTime(pickup,
          delivery);
      final long deliveryToDepotTT = travelTimes
          .getTravelTimeToNearestDepot(delivery);
      // PICKUP
      final long earliestPickupOpening = orderAnnounceTime;
      final long earliestPickupClosing = earliestPickupOpening;
      final long latestPickupClosing = endTime - deliveryToDepotTT
          - pickupToDeliveryTT - parcelBuilder.getPickupDuration()
          - parcelBuilder.getDeliveryDuration();
      final TimeWindow pickupTW = urgencyTimeWindow(earliestPickupOpening,
          earliestPickupClosinglatestPickupClosing,
          );
      // DELIVERY
      final long earliestDeliveryOpening = pickupTW.begin + pickupToDeliveryTT
          + parcelBuilder.getPickupDuration();
      final long latestDeliveryOpening = endTime - deliveryToDepotTT;
      final long delOpen = .get(.nextLong());
      checkArgument(delOpen >= 0);
      long delOpening = Math.min(earliestDeliveryOpening + delOpen,
          latestDeliveryOpening);
      delOpening = Math.max(delOpeningearliestDeliveryOpening);
      final long earliestDeliveryClosing = pickupTW.end + pickupToDeliveryTT
          + parcelBuilder.getPickupDuration();
      final long latestDeliveryClosing = endTime - deliveryToDepotTT
          - parcelBuilder.getDeliveryDuration();
      final double delFactor = .get(.nextLong());
      checkArgument(delFactor > 0d);
      long deliveryClosing = DoubleMath.roundToLong(pickupTW.length()
          * delFactor.);
      if (.isPresent()) {
        deliveryClosing = Math.max(
            delOpening + .get().get(.nextLong()),
            deliveryClosing);
      }
      final long boundedDelClose = boundValue(deliveryClosing,
          earliestDeliveryClosinglatestDeliveryClosing);
      final TimeWindow deliveryTW = new TimeWindow(delOpening,
          boundedDelClose);
      parcelBuilder.pickupTimeWindow(pickupTW);
      parcelBuilder.deliveryTimeWindow(deliveryTW);
    }
    static long boundValue(long valuelong lowerBoundlong upperBound) {
      return Math.max(lowerBound, Math.min(valueupperBound));
    }
    TimeWindow urgencyTimeWindow(long earliestOpeninglong earliestClosing,
        long latestClosingStochasticSupplier<Longurgency,
        StochasticSupplier<Longlength) {
      final long closing = boundValue(
          earliestClosing + urgency.get(.nextLong()), earliestClosing,
          latestClosing);
      final long opening = boundValue(closing - length.get(.nextLong()),
          earliestOpeningclosing);
      return new TimeWindow(openingclosing);
    }
  }
New to GrepCode? Check out our FAQ X