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.google.common.base.Preconditions.checkArgument;
 import static com.google.common.collect.Lists.newArrayList;
 
 import java.util.List;
 
 
 
A generator of com.github.rinde.rinsim.scenario.Scenarios.

Author(s):
Rinde van Lon
 
 // TODO rename to Scenarios? or Generators?
 public final class ScenarioGenerator {
 
   // global properties
   final Builder builder;
   final ImmutableList<Supplier<? extends Model<?>>> modelSuppliers;
 
   private final ParcelGenerator parcelGenerator;
   private final VehicleGenerator vehicleGenerator;
   private final DepotGenerator depotGenerator;
 
      = b;
      = b.parcelGenerator;
      = b.vehicleGenerator;
      = b.depotGenerator;
 
     boolean containsRoadModelSupplier = false;
     final ImmutableList.Builder<Supplier<? extends Model<?>>> modelsBuilder = ImmutableList
         .builder();
     for (final ModelSupplierScenGen<?> sup : .) {
       final Supplier<? extends Model<?>> modelSupplier = sup.get(this);
       if (modelSupplier.get() instanceof RoadModel) {
         containsRoadModelSupplier = true;
       }
       modelsBuilder.add(modelSupplier);
     }
      = modelsBuilder.build();
     checkArgument(containsRoadModelSupplier,
         "A supplier of a RoadModel is mandatory. Found suppliers: %s.",
         );
   }

  

Returns:
The speed unit used in generated scenarios.
 
   public Unit<VelocitygetSpeedUnit() {
     return .getSpeedUnit();
   }

  

Returns:
The distance unit used in generated scenarios.
 
  public Unit<LengthgetDistanceUnit() {
    return .getDistanceUnit();
  }

  

Returns:
The time unit used in generated scenarios.
  public Unit<DurationgetTimeUnit() {
    return .getTimeUnit();
  }

  

Returns:
The time window of generated scenarios.
  public TimeWindow getTimeWindow() {
    return .getTimeWindow();
  }

  

Returns:
The tick size of generated scenarios.
  public long getTickSize() {
    return .getTickSize();
  }

  

Returns:
The minimum position found in generated scenarios.
  public Point getMin() {
    return .getMin();
  }

  

Returns:
The maximum position found in generated scenarios.
  public Point getMax() {
    return .getMax();
  }

  

Returns:
The com.github.rinde.rinsim.scenario.Scenario.ProblemClass of the generated scenarios.
    return .;
  }

  
Generates a new com.github.rinde.rinsim.scenario.Scenario instance.

Parameters:
rng The random number generator used for drawing random numbers.
id The id of this specific scenario.
Returns:
A new instance.
  // TODO change rng to seed?
  public Scenario generate(RandomGenerator rngString id) {
    final ImmutableList.Builder<TimedEventb = ImmutableList.builder();
    // depots
    final Iterable<? extends AddDepotEventdepots = .generate(
        rng.nextLong(), .getCenter());
    b.addAll(depots);
    // vehicles
        rng.nextLong(), .getCenter(),
        .getTimeWindow().);
    b.addAll(vehicles);
        depotsvehicles);
    // parcels
        .getTimeWindow().));
    // time out
    // create
    return Scenario.builder(.)
        .addModels()
        .addEvents(b.build())
        .instanceId(id)
        .build();
  }

  
Create a ScenarioGenerator.Builder for constructing ScenarioGenerators.

Parameters:
problemClass The com.github.rinde.rinsim.scenario.Scenario.ProblemClass of the scenarios that will be generated by the generator under construction.
Returns:
The builder.
  public static Builder builder(ProblemClass problemClass) {
    return new Builder(problemClass);
  }

  
Create a ScenarioGenerator.Builder for constructing ScenarioGenerators.

Returns:
The builder.
  public static Builder builder() {
  }

  
Creates a ScenarioGenerator.TravelTimes instance based on the specified com.github.rinde.rinsim.scenario.Scenario.

Parameters:
s The scenario.
Returns:
The travel times.
  public static TravelTimes createTravelTimes(Scenario s) {
    final Iterable<AddDepotEventdepots = FluentIterable.from(s.asList())
        .filter(AddDepotEvent.class);
    final Iterable<AddVehicleEventvehicles = FluentIterable.from(s.asList())
        .filter(AddVehicleEvent.class);
    final List<RoadModelroadModels = newArrayList();
    for (final Supplier<? extends Model<?>> sup : s.getModelSuppliers()) {
      final Model<?> m = sup.get();
      if (m instanceof RoadModel) {
        roadModels.add((RoadModelm);
      }
    }
    checkArgument(roadModels.size() == 1);
    return new DefaultTravelTimes(roadModels.get(0), s.getTimeUnit(),
        depotsvehicles);
  }
      Iterable<? extends Supplier<? extends Model<?>>> modelSuppliers,
      Unit<Durationtu,
      Iterable<? extends AddDepotEventdepots,
      Iterable<? extends AddVehicleEventvehicles) {
    final RoadModel rm = getRm(modelSuppliers);
    return new DefaultTravelTimes(rmtudepotsvehicles);
  }
  static RoadModel getRm(
      Iterable<? extends Supplier<? extends Model<?>>> modelSuppliers) {
    for (final Supplier<?> sup : modelSuppliers) {
      final Object v = sup.get();
      if (v instanceof RoadModel) {
        return (RoadModelv;
      }
    }
    throw new IllegalArgumentException("There is no RoadModel supplier in "
        + modelSuppliers + ".");
  }

  
Builder for creating ScenarioGenerator instances.

Author(s):
Rinde van Lon
  public static class Builder extends AbstractBuilder<Builder> {
    static final ParcelGenerator DEFAULT_PARCEL_GENERATOR = Parcels.builder()
        .build();
    static final VehicleGenerator DEFAULT_VEHICLE_GENERATOR = Vehicles
        .builder().build();
    static final DepotGenerator DEFAULT_DEPOT_GENERATOR = Depots
        .singleCenteredDepot();
    final ProblemClass problemClass;
    Builder(ProblemClass pc) {
      super(Optional.<AbstractBuilder<?>> absent());
       = pc;
       = newArrayList();
    }
    // copying constructor
    Builder(Builder b) {
      super(Optional.<AbstractBuilder<?>> of(b));
       = b.problemClass;
       = b.parcelGenerator;
       = b.vehicleGenerator;
       = b.depotGenerator;
       = newArrayList(b.modelSuppliers);
    }
    @Override
    protected Builder self() {
      return this;
    }

    
Set the Vehicles.VehicleGenerator to use for adding vehicles to the scenario.

Parameters:
vg The vehicle generator.
Returns:
This, as per the builder pattern.
    public Builder vehicles(VehicleGenerator vg) {
       = vg;
      return this;
    }

    
Set the Parcels.ParcelGenerator to use for adding parcels to the scenario.

Parameters:
pg The parcel generator.
Returns:
This, as per the builder pattern.
    public Builder parcels(ParcelGenerator pg) {
       = pg;
      return this;
    }

    
Set the Depots.DepotGenerator to use for adding depots to the scenario.

Parameters:
ds The depot generator.
Returns:
This, as per the builder pattern.
    public Builder depots(DepotGenerator ds) {
       = ds;
      return this;
    }

    
Add a supplier of a com.github.rinde.rinsim.core.model.Model. The provided supplier will use default values provided by the ScenarioGenerator instance which is currently being constructed. For most models implementations are available in Models.

Parameters:
modelSup The supplier to add.
Returns:
This, as per the builder pattern.
    public Builder addModel(ModelSupplierScenGen<?> modelSup) {
      .add(modelSup);
      return this;
    }

    
Add a supplier of a com.github.rinde.rinsim.core.model.Model.

Parameters:
modelSup The supplier to add.
Returns:
This, as per the builder pattern.
    public Builder addModel(Supplier<? extends Model<?>> modelSup) {
      .add(Models.adapt(modelSup));
      return this;
    }

    

Returns:
Constructs a new ScenarioGenerator instance based on this builder.
    public ScenarioGenerator build() {
      return new ScenarioGenerator(new Builder(this));
    }
  }

  
Implementations should provide information about travel times in a scenario. The travel times are usually extracted from a com.github.rinde.rinsim.core.model.road.RoadModel.

Author(s):
Rinde van Lon
  public interface TravelTimes {
    
Computes the travel time between from and to using the fastest available vehicle.

Parameters:
from The origin position.
to The destination position.
Returns:
The expected travel time between the two positions.
    long getShortestTravelTime(Point fromPoint to);

    
Computes the travel time between from and the nearest depot using the fastest available vehicle.

Parameters:
from The origin position.
Returns:
The expected travel time between the two positions.
  }
  static class DefaultTravelTimes implements TravelTimes {
    private final RoadModel roadModel;
    private final Measure<DoubleVelocityvehicleSpeed;
    private final Unit<DurationtimeUnit;
    private final ImmutableList<PointdepotLocations;
        Iterable<? extends AddDepotEventdepots,
        Iterable<? extends AddVehicleEventvehicles) {
       = rm;
      double max = 0;
      for (final AddVehicleEvent ave : vehicles) {
        max = Math.max(maxave.vehicleDTO.speed);
      }
       = Measure.valueOf(max.getSpeedUnit());
      final ImmutableList.Builder<PointdepotBuilder = ImmutableList.builder();
      for (final AddDepotEvent ade : depots) {
        depotBuilder.add(ade.position);
      }
       = depotBuilder.build();
       = tu;
    }
    @Override
    public long getShortestTravelTime(Point fromPoint to) {
      final Iterator<Pointpath = .getShortestPathTo(fromto)
          .iterator();
      long travelTime = 0L;
      final Point prev = path.next();
      while (path.hasNext()) {
        final Point cur = path.next();
        final Measure<DoubleLengthdistance = Measure.valueOf(
            Point.distance(prevcur), .getDistanceUnit());
        travelTime += RoadModels.computeTravelTime(distance,
            );
      }
      return travelTime;
    }
    @Override
    public long getTravelTimeToNearestDepot(Point from) {
      return getShortestTravelTime(fromfindNearestDepot(from));
    }
    private Point findNearestDepot(Point from) {
      final Iterator<Pointit = .iterator();
      Point nearestDepot = it.next();
      final double dist = Point.distance(fromnearestDepot);
      while (it.hasNext()) {
        final Point cur = it.next();
        final double d = Point.distance(fromcur);
        if (d < dist) {
          nearestDepot = cur;
        }
      }
      return nearestDepot;
    }
  }
New to GrepCode? Check out our FAQ X