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;
 import static java.util.Collections.nCopies;
 
 
Utility class for creating Vehicles.VehicleGenerators.

Author(s):
Rinde van Lon
 
 public final class Vehicles {
   static final int DEFAULT_NUM_OF_VEHICLES = 10;
   static final StochasticSupplier<DoubleDEFAULT_SPEED = constant(50d);
   static final StochasticSupplier<IntegerDEFAULT_CAPACITY = constant(1);
   static final StochasticSupplier<LongDEFAULT_TIME = constant(-1L);
 
   private Vehicles() {}

  
Creates a Vehicles.VehicleGenerator that creates a single vehicle as specified by the com.github.rinde.rinsim.core.pdptw.VehicleDTO.

Parameters:
dto The vehicle specification.
Returns:
The newly created generator.
 
   public static VehicleGenerator single(VehicleDTO dto) {
     return homogenous(dto, 1);
   }

  
Creates a Vehicles.VehicleGenerator that creates identical (homogenous) vehicles as specified by the com.github.rinde.rinsim.core.pdptw.VehicleDTO.

Parameters:
dto The vehicle specification.
numberOfVehicles The number of vehicles to generate.
Returns:
The newly created generator.
 
   public static VehicleGenerator homogenous(VehicleDTO dtoint numberOfVehicles) {
     return new HomogenousVehicleGenerator(numberOfVehiclesdto);
   }

  

Returns:
A newly constructed Vehicles.Builder for constructing Vehicles.VehicleGenerators.
 
   public static Builder builder() {
     return new Builder();
   }

  
Generator of com.github.rinde.rinsim.scenario.AddVehicleEvents.

Author(s):
Rinde van Lon
 
   public interface VehicleGenerator {
    
Should generate a list of com.github.rinde.rinsim.scenario.AddVehicleEvent. Each event indicates the addition of a vehicle.

Parameters:
seed The random seed to use for generating the vehicles.
center The center of the environment.
scenarioLength The length of the scenario for which the vehicles are generated.
Returns:
A list of events.
 
     ImmutableList<AddVehicleEventgenerate(long seedPoint center,
         long scenarioLength);
   }

  
A builder for constructing Vehicles.VehicleGenerators.

Author(s):
Rinde van Lon
 
   public static class Builder {
    Builder() {
       = Optional.absent();
       = Optional.absent();
    }

    
Sets the number of vehicles that are to be created by the generator. Default value: 10. All values returned by the com.github.rinde.rinsim.util.StochasticSupplier must be greater than 0.

Parameters:
num The supplier to draw numbers from.
Returns:
This, as per the builder pattern.
       = num;
      return this;
    }

    
Sets the start positions of the vehicles that are generated by the generator. By default the start positions of the vehicles are set to the center of the area.

Parameters:
pos The supplier to draw positions from.
Returns:
This, as per the builder pattern.
       = Optional.of(pos);
      return this;
    }

    
Sets the start positions of all generated vehicles to the center of the area. This is the default behavior.

Returns:
This, as per the builder pattern.
    public Builder centeredStartPositions() {
       = Optional.absent();
      return this;
    }

    
Sets the availability com.github.rinde.rinsim.util.TimeWindows of the vehicles that are generated by the generator. By default the time windows of the vehicles are set equal to the time window of the scenario.

Parameters:
tw The supplier to draw time windows from.
Returns:
This, as per the builder pattern.
       = Optional.of(tw);
      return this;
    }

    
Sets the availability com.github.rinde.rinsim.util.TimeWindows of the vehicles that are generated by the generator to be equal to that of the scenario.

Returns:
This, as per the builder pattern.
    public Builder timeWindowsAsScenario() {
       = Optional.absent();
      return this;
    }

    
Sets the speeds of the vehicles that are generated by the generator. Default value: 50 (using the speed unit of the scenario).

Parameters:
sp The supplier from which to draw speed values.
Returns:
This, as per the builder pattern.
    public Builder speeds(StochasticSupplier<Doublesp) {
       = sp;
      return this;
    }

    
Sets the capacities of the vehicles that are generated by the generator. Default value: 1.

Parameters:
cap The supplier from which to draw capacity values.
Returns:
This, as per the builder pattern.
    public Builder capacities(StochasticSupplier<Integercap) {
       = cap;
      return this;
    }

    
Sets the creation times of the vehicles that are generated by the generator. Default value: -1, this means that the vehicles exist at the beginning of the scenario.

Parameters:
times The supplier from which to draw times.
Returns:
This, as per the builder pattern.
    public Builder creationTimes(StochasticSupplier<Longtimes) {
       = times;
      return this;
    }

    

Returns:
A newly constructed Vehicles.VehicleGenerator as specified by this builder.
    public VehicleGenerator build() {
      return new DefaultVehicleGenerator(this);
    }
  }
  private static class DefaultVehicleGenerator implements VehicleGenerator {
    private final StochasticSupplier<DoublespeedGenerator;
    private final RandomGenerator rng;
       = b.numberOfVehicles;
       = b.startPositionsSupplier;
       = b.speedsSupplier;
       = b.capacitiesSupplier;
       = b.timeWindowsSupplier;
       = b.creationTimesSupplier;
       = new MersenneTwister();
    }
    @Override
    public ImmutableList<AddVehicleEventgenerate(long seedPoint center,
        long scenarioLength) {
      .setSeed(seed);
      final ImmutableList.Builder<AddVehicleEventbuilder = ImmutableList
          .builder();
      final int num = .get(.nextLong());
      checkArgument(num > 0,
          "The numberOfVehicles supplier must generate values > 0, found %s.",
          num);
      for (int i = 0; i < numi++) {
        final Point pos = .isPresent()
            ? .get().get(.nextLong())
            : center;
        final double speed = .get(.nextLong());
        checkArgument(
            speed > 0d,
            "The speeds supplier must generate values > 0.0, found %s.",
            speed);
        final int capacity = .get(.nextLong());
        checkArgument(
            capacity >= 0,
            "The capacities supplier must generate non-negative values, found %s.",
            capacity);
        final TimeWindow tw = .isPresent()
            ? .get().get(.nextLong())
            : new TimeWindow(0L, scenarioLength);
        final long time = .get(.nextLong());
        checkArgument(
            time < scenarioLength,
            "The creationTimes supplier must generate values smaller than the scenarioLength (%s), found %s.",
            scenarioLengthtime);
        final VehicleDTO dto = VehicleDTO.builder()
            .startPosition(pos)
            .speed(speed)
            .capacity(capacity)
            .availabilityTimeWindow(tw)
            .build();
        builder.add(new AddVehicleEvent(timedto));
      }
      return builder.build();
    }
  }
  private static class HomogenousVehicleGenerator implements VehicleGenerator {
    private final VehicleDTO vehicleDto;
    private final int n;
    private final RandomGenerator rng;
    HomogenousVehicleGenerator(int numberOfVehiclesVehicleDTO dto) {
       = dto;
       = numberOfVehicles;
       = new MersenneTwister();
    }
    @Override
    public ImmutableList<AddVehicleEventgenerate(long seedPoint center,
        long scenarioLength) {
      .setSeed(seed);
      return ImmutableList
          .copyOf(nCopies(new AddVehicleEvent(-1, )));
    }
  }
New to GrepCode? Check out our FAQ X