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.uniformDouble;
 import static com.google.common.base.Preconditions.checkArgument;
 
 
 
Utility class for creating Locations.LocationGenerators.

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

  
 
   public static Builder builder() {
     return new Builder();
   }

  
A location generator generates locations for orders (aka tasks).

Author(s):
Rinde van Lon
 
   public interface LocationGenerator {
    
Should generate locations for the specified number of orders (aka tasks). There should be enough locations for each order. Typically this is predefined as a ratio, e.g. 1:2 in case origin and destination is required for each order.

Parameters:
seed The random seed.
numLocations The number of locations that is required.
Returns:
A list of locations for the orders of size numLocations.
 
     ImmutableList<Pointgenerate(long seedint numLocations);

    

Returns:
The expected center of all generated locations.
 
     Point getCenter();

    

Returns:
A position representing the lowest possible coordinates.
 
     Point getMin();

    

Returns:
A position representing the highest possible coordinates.
 
     Point getMax();
   }

  
A builder for creating Locations.LocationGenerators.

Author(s):
Rinde van Lon
 
   public static class Builder {
     Optional<DoublexMin;
     Optional<DoubleyMin;
     Optional<DoublexMax;
     Optional<DoubleyMax;
     Optional<DoublexMean;
     Optional<DoubleyMean;
     Optional<DoublexSd;
     Optional<DoubleySd;
 
     Builder() {
        = Optional.absent();
        = Optional.absent();
        = Optional.absent();
       = Optional.absent();
       = Optional.absent();
       = Optional.absent();
       = Optional.absent();
       = Optional.absent();
       = Optional.absent();
    }

    
Sets the borders of the location generator such that it spans the specified area. The min position is (0,0), and max is (sqrt(area),sqrt(area)).

Parameters:
area The area to use.
Returns:
This, as per the builder pattern.
    public Builder squareByArea(double area) {
      return square(Math.sqrt(area));
    }

    
Sets the borders of the location generator such that the min position is (0,0) and the max position is (side,side).

Parameters:
side The side of a square.
Returns:
This, as per the builder pattern.
    public Builder square(double side) {
      return xMin(0d).yMin(0d).xMax(side).yMax(side);
    }

    
Sets the minimum x value that the location generator will generate.

Parameters:
x The minimum x value.
Returns:
This, as per the builder pattern.
    public Builder xMin(double x) {
       = Optional.of(Double.valueOf(x));
      return this;
    }

    
Sets the minimum y value that the location generator will generate.

Parameters:
y The minimum y value.
Returns:
This, as per the builder pattern.
    public Builder yMin(double y) {
       = Optional.of(Double.valueOf(y));
      return this;
    }

    
Sets the minimum x and y values that the location generator will generate.

Parameters:
min The minimum x and y value.
Returns:
This, as per the builder pattern.
    public Builder min(double min) {
      return xMin(min).yMin(min);
    }

    
Sets the minimum position the the location generator will generate.

Parameters:
p The minimum position.
Returns:
This, as per the builder pattern.
    public Builder min(Point p) {
      return xMin(p.x).yMin(p.y);
    }

    
Sets the maximum x value that the location generator will generate.

Parameters:
x The maximum x value.
Returns:
This, as per the builder pattern.
    public Builder xMax(double x) {
       = Optional.of(Double.valueOf(x));
      return this;
    }

    
Sets the maximum y value that the location generator will generate.

Parameters:
y The maximum y value.
Returns:
This, as per the builder pattern.
    public Builder yMax(double y) {
       = Optional.of(Double.valueOf(y));
      return this;
    }

    
Sets the maximum x and y values the location generator will generate.

Parameters:
max The maximum x and y values.
Returns:
This, as per the builder pattern.
    public Builder max(double max) {
      return xMax(max).yMax(max);
    }

    
Sets the maximum position the the location generator will generate.

Parameters:
max The maximum position.
Returns:
This, as per the builder pattern.
    public Builder max(Point max) {
      return xMax(max.x).yMax(max.y);
    }

    
Sets the position that the location generator will use as mean.

Parameters:
mean The mean position.
Returns:
This, as per the builder pattern.
    public Builder mean(Point mean) {
      return xMean(mean.x).yMean(mean.y);
    }

    
Sets the mean position that the location generator will use as mean.

Parameters:
m The x and y value of the mean position.
Returns:
This, as per the builder pattern.
    public Builder mean(double m) {
      return xMean(m).yMean(m);
    }

    
Sets the x mean that the location generator will use.

Parameters:
x The x mean.
Returns:
This, as per the builder pattern.
    public Builder xMean(double x) {
       = Optional.of(Double.valueOf(x));
      return this;
    }

    
Sets the y mean that the location generator will use.

Parameters:
y The y mean.
Returns:
This, as per the builder pattern.
    public Builder yMean(double y) {
       = Optional.of(Double.valueOf(y));
      return this;
    }

    
Sets the standard deviation that the location generator will use.

Parameters:
s The standard deviation.
Returns:
This, as per the builder pattern.
    public Builder std(double s) {
      return xStd(s).yStd(s);
    }

    
Sets the standard deviations for x and y that the location generator will use.

Parameters:
p A point indicating the x and y standard deviations.
Returns:
This, as per the builder pattern.
    public Builder std(Point p) {
      return xStd(p.x).yStd(p.y);
    }

    
Sets the x standard deviation that the location generator will use.

Parameters:
x The x standard deviation.
Returns:
This, as per the builder pattern.
    public Builder xStd(double x) {
       = Optional.of(Double.valueOf(x));
      return this;
    }

    
Sets the y standard deviation that the location generator will use.

Parameters:
y The y standard deviation.
Returns:
This, as per the builder pattern.
    public Builder yStd(double y) {
       = Optional.of(Double.valueOf(y));
      return this;
    }

    
When this option is used and a normal distributed location generator is used it will draw a new random number in case the previous is out of bounds.

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

    
When this option is used and a normal distributed location generator is used it will round all random numbers such that they fit in the bounds.

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

    
Create a uniform distributed Locations.LocationGenerator. The min and max values set to this builder take precedence over the mean and standard deviations. In case the boundaries were set any specified means and standard deviations are ignored when generating the locations. However, the mean as supplied to this builder will be returned in the Locations.LocationGenerator.getCenter() method.

Returns:
A uniform distributed generator.
    public LocationGenerator buildUniform() {
      final Point xMinMax = getUniformMinMax();
      final Point yMinMax = getUniformMinMax();
      final Point min = new Point(xMinMax.xyMinMax.x);
      final Point max = new Point(xMinMax.yyMinMax.y);
      final double xCenter = getUniformCenter(min.xmax.x);
      final double yCenter = getUniformCenter(min.ymax.y);
      return new SupplierLocGen(
          minmaxnew Point(xCenteryCenter),
          uniformDouble(min.xmax.x),
          uniformDouble(min.ymax.y));
    }

    
Create a normal distributed Locations.LocationGenerator.

Returns:
A normal distributed generator.
    public LocationGenerator buildNormal() {
      final StochasticSupplier<DoublexSup = normalVar(,
          );
      final StochasticSupplier<DoubleySup = normalVar(,
          );
      return new SupplierLocGen(
          new Point(.get(), .get()),
          new Point(.get(), .get()),
          new Point(.get(), .get()),
          xSupySup);
    }

    
Creates a fixed Locations.LocationGenerator that will always return the same list of locations. Note that if the number of requested locations is different from the number of locations in this list, the Locations.LocationGenerator will throw an java.lang.IllegalArgumentException. If no previous min, max or center values were supplied these will be computed from the specified locations.

Parameters:
locations The locations that will always be returned by the Locations.LocationGenerator.
Returns:
A new fixed Locations.LocationGenerator.
    public LocationGenerator buildFixed(Iterable<Pointlocations) {
      final ImmutableList<Pointlocs = ImmutableList.copyOf(locations);
      if (!.isPresent()) {
         = Optional.of(Collections.min(locs..).);
      }
      if (!.isPresent()) {
         = Optional.of(Collections.min(locs..).);
      }
      if (!.isPresent()) {
         = Optional.of(Collections.max(locs..).);
      }
      if (!.isPresent()) {
         = Optional.of(Collections.max(locs..).);
      }
      if (!.isPresent()) {
         = Optional.of(DoubleMath.mean(
            Collections2.transform(locs..)));
      }
      if (!.isPresent()) {
         = Optional.of(DoubleMath.mean(
            Collections2.transform(locs..)));
      }
      return new FixedLocGen(new Point(.get(), .get()),
          new Point(.get(), .get()),
          new Point(.get(), .get()),
          locs);
    }
    private static double getUniformCenter(Optional<Doublemeandouble min,
        double max) {
      if (mean.isPresent()) {
        return mean.get();
      }
      return (max - min) / 2d;
    }
    private static Point getUniformMinMax(Optional<Doublemin,
        Optional<DoublemaxOptional<DoublemeanOptional<Doublestd) {
      if (min.isPresent() && max.isPresent()) {
        checkArgument(min.get() < max.get());
        return new Point(min.get(), max.get());
      } else if (mean.isPresent() && std.isPresent()) {
        final double length = Math.sqrt(12) * std.get();
        final double minn = mean.get() - length;
        final double maxx = mean.get() + length;
        return new Point(minnmaxx);
      } else {
        throw new IllegalArgumentException();
      }
    }
    private static StochasticSupplier<DoublenormalVar(Optional<Doublemin,
        Optional<DoublemaxOptional<DoublemeanOptional<Doublestd,
        Optional<Booleanredraw) {
      checkArgument(min.isPresent());
      checkArgument(max.isPresent());
      checkArgument(mean.isPresent());
      checkArgument(std.isPresent());
      checkArgument(redraw.isPresent());
      final StochasticSuppliers.Builder builder = StochasticSuppliers.normal()
          .mean(mean.get())
          .std(std.get())
          .lowerBound(min.get())
          .upperBound(max.get());
      if (redraw.get()) {
        builder.redrawWhenOutOfBounds();
      } else {
        builder.roundWhenOutOfBounds();
      }
      return builder.buildDouble();
    }
  }
  private abstract static class AbstractLocGen implements LocationGenerator {
    final Point min;
    final Point max;
    final Point center;
    AbstractLocGen(Point miPoint maPoint ce) {
       = mi;
       = ma;
       = ce;
    }
    @Override
    public Point getMin() {
      return ;
    }
    @Override
    public Point getMax() {
      return ;
    }
    @Override
    public Point getCenter() {
      return ;
    }
  }
  private static class SupplierLocGen extends AbstractLocGen {
    private final StochasticSupplier<DoublexSupplier;
    private final StochasticSupplier<DoubleySupplier;
    private final RandomGenerator rng;
    SupplierLocGen(Point miPoint maPoint ce,
        StochasticSupplier<DoublexSup,
        StochasticSupplier<DoubleySup) {
      super(mimace);
       = xSup;
       = ySup;
       = new MersenneTwister();
    }
    @Override
    public ImmutableList<Pointgenerate(long seedint numOrders) {
      .setSeed(seed);
      final ImmutableList.Builder<Pointlocs = ImmutableList.builder();
      for (int i = 0; i < numOrdersi++) {
        locs.add(new Point(
            .get(.nextLong()),
            .get(.nextLong())));
      }
      return locs.build();
    }
  }
  private static class FixedLocGen extends AbstractLocGen {
    private final ImmutableList<PointfixedPoints;
    FixedLocGen(Point miPoint maPoint ceIterable<Pointpoints) {
      super(mimace);
       = ImmutableList.copyOf(points);
    }
    @Override
    public ImmutableList<Pointgenerate(long seedint numOrders) {
      checkArgument(
          .size() == numOrders,
          "This fixed LocationGenerator can only output %s locations while %s locations were requested.",
          .size(), numOrders);
      return ;
    }
  }
New to GrepCode? Check out our FAQ X