Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package rinde.sim.pdptw.gendreau06;
  
  import static com.google.common.base.Preconditions.checkArgument;
  import static com.google.common.base.Preconditions.checkNotNull;
  import static rinde.sim.core.model.pdp.PDPScenarioEvent.ADD_DEPOT;
  import static rinde.sim.core.model.pdp.PDPScenarioEvent.ADD_PARCEL;
 import static rinde.sim.core.model.pdp.PDPScenarioEvent.ADD_VEHICLE;
 import static rinde.sim.core.model.pdp.PDPScenarioEvent.TIME_OUT;
 
 import java.io.File;
 import java.util.List;
 import java.util.Set;
 
 import  javax.annotation.Nullable;
 
 
Parser for files from the Gendreau et al. (2006) data set. The parser allows to customize some of the properties of the scenarios.

Format specification: (columns)

  • 1: request arrival time
  • 2: pick-up service time
  • 3 and 4: x and y position for the pick-up
  • 5 and 6: service window time of the pick-up
  • 7: delivery service time
  • 8 and 9:x and y position for the delivery
  • 10 and 11: service window time of the delivery
All times are expressed in seconds.

References

  • [1]. Gendreau, M., Guertin, F., Potvin, J.-Y., and Séguin, R. Neighborhood search heuristics for a dynamic vehicle dispatching problem with pick-ups and deliveries. Transportation Research Part C: Emerging Technologies 14, 3 (2006), 157–174.

Author(s):
Rinde van Lon <rinde.vanlon@cs.kuleuven.be>
 
 public final class Gendreau06Parser {
 
   private static final String REGEX = ".*req_rapide_(1|2|3|4|5)_(450|240)_(24|33)";
   private static final double TIME_MULTIPLIER = 1000d;
   private static final int TIME_MULTIPLIER_INTEGER = 1000;
   private static final int PARCEL_MAGNITUDE = 0;
 
   private int numVehicles;
   private boolean allowDiversion;
   private boolean online;
   private long tickSize;
   private final ImmutableMap.Builder<StringInputStreamstreams;
   @Nullable
 
   private Gendreau06Parser() {
      = false;
      = true;
      = -1;
      = 1000L;
      = ImmutableMap.builder();
   }

  

Returns:
A Gendreau06Parser.
 
   public static Gendreau06Parser parser() {
     return new Gendreau06Parser();
   }

  
Convenience method for parsing a single file.

Parameters:
file The file to parse.
Returns:
The scenario as described by the file.
  public static Gendreau06Scenario parse(File file) {
    return parser().addFile(file).parse().get(0);
  }

  
Add the specified file to the parser.

Parameters:
file The file to add.
Returns:
This, as per the builder pattern.
  public Gendreau06Parser addFile(File file) {
    try {
      .put(file.getName(), new FileInputStream(file));
    } catch (final FileNotFoundException e) {
      throw new IllegalArgumentException(e);
    }
    return this;
  }

  
Add the specified file to the parser.

Parameters:
file The file to add.
Returns:
This, as per the builder pattern.
  public Gendreau06Parser addFile(String file) {
    return addFile(new File(file));
  }

  
Add the specified stream to the parser. A file name needs to be specified for identification of this particular scenario.

Parameters:
stream The stream to use for the parsing of the scenario.
fileName The file name that identifies the scenario's class and instance.
Returns:
This, as per the builder pattern.
  public Gendreau06Parser addFile(InputStream streamString fileName) {
    checkValidFileName(fileName);
    .put(fileNamestream);
    return this;
  }

  
Adds all Gendreau scenario files in the specified directory.

Parameters:
dir The directory to search.
Returns:
This, as per the builder pattern.
    return addDirectory(new File(dir));
  }

  
Adds all Gendreau scenario files in the specified directory.

Parameters:
dir The directory to search.
Returns:
This, as per the builder pattern.
  public Gendreau06Parser addDirectory(File dir) {
    final File[] files = dir.listFiles(
        new FileFilter() {
          @Override
          public boolean accept(@Nullable File file) {
            checkNotNull(file);
            return isValidFileName(file.getName());
          }
        });
    for (final File f : files) {
      addFile(f);
    }
    return this;
  }

  
When this method is called all scenarios generated by this parser will allow vehicle diversion. For more information about the vehicle diversion concept please consult the class documentation of rinde.sim.pdptw.common.PDPRoadModel.

Returns:
This, as per the builder pattern.
     = true;
    return this;
  }

  
When this method is called, all scenarios generated by this parser will be offline scenarios. This means that all parcel events will arrive at time -1, which means that everything is known beforehand. By default the parser uses the original event arrival times as defined by the scenario file.

Returns:
This, as per the builder pattern.
  public Gendreau06Parser offline() {
     = false;
    return this;
  }

  
This method allows to override the number of vehicles in the scenarios. For the default values see GendreauProblemClass.

Parameters:
num The number of vehicles that should be used in the parsed scenarios. Must be positive.
Returns:
This, as per the builder pattern.
  public Gendreau06Parser setNumVehicles(int num) {
    checkArgument(num > 0, "The number of vehicles must be positive.");
     = num;
    return this;
  }

  
Change the default tick size of 1000 ms into something else.

Parameters:
tick Must be positive.
Returns:
This, as per the builder pattern.
  public Gendreau06Parser setTickSize(long tick) {
    checkArgument(tick > 0L, "Tick size must be positive.");
     = tick;
    return this;
  }

  
Filters out any files that are added to this parser which are not of one of the specified problem classes.

Parameters:
classes The problem classes which should be parsed.
Returns:
This, as per the builder pattern.
  public Gendreau06Parser filter(GendreauProblemClass... classes) {
     = ImmutableList.<ProblemClasscopyOf(classes);
    return this;
  }

  
Parses the files which are added to this parser. In case filter(GendreauProblemClass...) has been called, only files in one of these problem classes will be parsed.

Returns:
A list of scenarios in order of adding them to the parser.
    final ImmutableList.Builder<Gendreau06Scenarioscenarios = ImmutableList
        .builder();
    for (final Entry<StringInputStreamentry : .build().entrySet()) {
      boolean include = false;
      if ( == null) {
        include = true;
      } else {
        for (final ProblemClass pc : ) {
          if (entry.getKey().endsWith(pc.getId())) {
            include = true;
            break;
          }
        }
      }
      if (include) {
        scenarios.add(parse(entry.getValue(), entry.getKey(), ,
            ));
      }
    }
    return scenarios.build();
  }
  // public static Gendreau06Scenario parse(String file) {
  // return parse(file, -1);
  // }
  //
  // public static Gendreau06Scenario parse(String file, int numVehicles) {
  // return parse(file, numVehicles, false);
  // }
  //
  // public static Gendreau06Scenario parse(String file, int numVehicles,
  // boolean allowDiversion) {
  // FileReader reader;
  // try {
  // reader = new FileReader(file);
  // } catch (final FileNotFoundException e) {
  // throw new IllegalArgumentException("File not found: " + e.getMessage());
  // }
  // return parse(new BufferedReader(reader), new File(file).getName(),
  // numVehicles, 1000L, allowDiversion);
  // }
  //
  // public static Gendreau06Scenario parse(BufferedReader reader,
  // String fileName, int numVehicles) {
  // return parse(reader, fileName, numVehicles, 1000L);
  // }
  //
  // public static Gendreau06Scenario parse(BufferedReader reader,
  // String fileName, int numVehicles, final long tickSize) {
  // return parse(reader, fileName, numVehicles, tickSize, false);
  // }
  static boolean isValidFileName(String name) {
    return Pattern.compile().matcher(name).matches();
  }
  static void checkValidFileName(String name) {
        "The filename must conform to the following regex: %s input was: %s",
        name);
  }
  private static Gendreau06Scenario parse(InputStream inputStream,
      String fileNameint numVehiclesfinal long tickSize,
      final boolean allowDiversionboolean online) {
        );
    final BufferedReader reader = new BufferedReader(new InputStreamReader(
        inputStream));
    final Matcher m = Pattern.compile().matcher(fileName);
        "The filename must conform to the following regex: %s input was: %s",
        fileName);
    final int instanceNumber = Integer.parseInt(m.group(1));
    final long minutes = Long.parseLong(m.group(2));
    final long totalTime = minutes * 60000L;
    final long requestsPerHour = Long.parseLong(m.group(3));
    final GendreauProblemClass problemClass = GendreauProblemClass.with(
        minutesrequestsPerHour);
    final int vehicles = numVehicles == -1 ? problemClass.vehicles
        : numVehicles;
    final Point depotPosition = new Point(2.0, 2.5);
    final double truckSpeed = 30;
    sb.addEvent(new AddDepotEvent(-1, depotPosition));
    for (int i = 0; i < vehiclesi++) {
      sb.addEvent(new AddVehicleEvent(-1, new VehicleDTO(depotPosition,
          truckSpeed, 0, new TimeWindow(0, totalTime))));
    }
    String line;
    try {
      while ((line = reader.readLine()) != null) {
        final String[] parts = line.split(" ");
        final long requestArrivalTime = DoubleMath.roundToLong(
            Double.parseDouble(parts[0]) * ,
            .);
        // FIXME currently filtering out first and last lines of file. Is
        // this ok?
        if (requestArrivalTime >= 0) {
          final long pickupServiceTime = Long.parseLong(parts[1])
              * ;
          final double pickupX = Double.parseDouble(parts[2]);
          final double pickupY = Double.parseDouble(parts[3]);
          final long pickupTimeWindowBegin = DoubleMath.roundToLong(
              Double.parseDouble(parts[4]) * ,
              .);
          final long pickupTimeWindowEnd = DoubleMath.roundToLong(
              Double.parseDouble(parts[5]) * ,
              .);
          final long deliveryServiceTime = Long.parseLong(parts[6])
              * ;
          final double deliveryX = Double.parseDouble(parts[7]);
          final double deliveryY = Double.parseDouble(parts[8]);
          final long deliveryTimeWindowBegin = DoubleMath.roundToLong(
              Double.parseDouble(parts[9]) * ,
              .);
          final long deliveryTimeWindowEnd = DoubleMath.roundToLong(
              Double.parseDouble(parts[10]) * ,
              .);
          // when an offline scenario is desired, all times are set to -1
          final long arrTime = online ? requestArrivalTime : -1;
          final ParcelDTO dto = new ParcelDTO(new Point(pickupXpickupY),
              new Point(deliveryXdeliveryY), new TimeWindow(
                  pickupTimeWindowBeginpickupTimeWindowEnd), new TimeWindow(
                  deliveryTimeWindowBegindeliveryTimeWindowEnd),
              arrTimepickupServiceTime,
              deliveryServiceTime);
          sb.addEvent(new AddParcelEvent(dto));
        }
      }
      sb.addEvent(new TimedEvent(totalTime));
      reader.close();
    } catch (final IOException e) {
      throw new IllegalArgumentException(e);
    }
    return sb.build(new ScenarioCreator<Gendreau06Scenario>() {
      @Override
      public Gendreau06Scenario create(List<TimedEventeventList,
          Set<Enum<?>> eventTypes) {
        return new Gendreau06Scenario(eventListeventTypestickSize,
            problemClassinstanceNumberallowDiversion);
      }
    });
  }
New to GrepCode? Check out our FAQ X