Package rinde.sim.core.model.pdp

Examples of rinde.sim.core.model.pdp.PDPModel


  public void afterTick(TimeLapse timeLapse) {}

  @Override
  protected void tickImpl(TimeLapse time) {
    final RoadModel rm = roadModel.get();
    final PDPModel pm = pdpModel.get();

    if (!time.hasTimeLeft()) {
      return;
    }
    if (!curr.isPresent()) {
      curr = Optional.fromNullable(RoadModels.findClosestObject(
          rm.getPosition(this), rm, Parcel.class));
    }

    if (curr.isPresent()) {
      final boolean inCargo = pm.containerContains(this, curr.get());
      // sanity check: if it is not in our cargo AND it is also not on the
      // RoadModel, we cannot go to curr anymore.
      if (!inCargo && !rm.containsObject(curr.get())) {
        curr = Optional.absent();
      } else if (inCargo) {
        // if it is in cargo, go to its destination
        rm.moveTo(this, curr.get().getDestination(), time);
        if (rm.getPosition(this).equals(curr.get().getDestination())) {
          // deliver when we arrive
          pm.deliver(this, curr.get(), time);
        }
      } else {
        // it is still available, go there as fast as possible
        rm.moveTo(this, curr.get(), time);
        if (rm.equalPosition(this, curr.get())) {
          // pickup customer
          pm.pickup(this, curr.get(), time);
        }
      }
    }
  }
View Full Code Here


  }

  @Override
  protected void tickImpl(TimeLapse time) {
    final RoadModel rm = roadModel.get();
    final PDPModel pm = pdpModel.get();
    // we always go to the closest available parcel
    final DefaultParcel closest = (DefaultParcel) RoadModels
        .findClosestObject(rm.getPosition(this), rm, new Predicate<RoadUser>() {
          @Override
          public boolean apply(RoadUser input) {
            return input instanceof DefaultParcel
                && pm.getParcelState(((DefaultParcel) input)) == ParcelState.AVAILABLE;
          }
        });

    if (closest != null) {
      rm.moveTo(this, closest, time);
      if (rm.equalPosition(closest, this)
          && pm
              .getTimeWindowPolicy()
              .canPickup(closest.getPickupTimeWindow(), time.getTime(),
                  closest.getPickupDuration())) {
        pm.pickup(this, closest, time);
      }
    }
  }
View Full Code Here

  protected void tickImpl(TimeLapse time) {
    // Check if we can deliver nearby
    final Parcel delivery = getDelivery(time, 5);

    final RoadModel rm = roadModel.get();
    final PDPModel pm = pdpModel.get();

    if (delivery != null) {
      if (delivery.getDestination().equals(getPosition())
          && pm.getVehicleState(this) == VehicleState.IDLE) {
        pm.deliver(this, delivery, time);
      } else {
        rm.moveTo(this, delivery.getDestination(), time);
      }
      return;
    }

    // Otherwise, Check if we can pickup nearby
    final DefaultParcel closest = (DefaultParcel) RoadModels.findClosestObject(
        rm.getPosition(this), rm, new Predicate<RoadUser>() {
          @Override
          public boolean apply(RoadUser input) {
            return input instanceof DefaultParcel
                && pm.getParcelState(((DefaultParcel) input)) == ParcelState.AVAILABLE;
          }
        });

    if (closest != null
        && Point.distance(rm.getPosition(closest), getPosition()) < 10) {
      if (rm.equalPosition(closest, this)
          && pm.getTimeWindowPolicy().canPickup(closest.getPickupTimeWindow(),
              time.getTime(), closest.getPickupDuration())) {
        final double newSize = getPDPModel().getContentsSize(this)
            + closest.getMagnitude();

        if (newSize <= getCapacity()) {
          pm.pickup(this, closest, time);
        }
      } else {
        rm.moveTo(this, rm.getPosition(closest), time);
      }
      return;
View Full Code Here

  @Nullable
  public Parcel getDelivery(TimeLapse time, int distance) {
    Parcel target = null;
    double closest = distance;
    final PDPModel pm = pdpModel.get();
    for (final Parcel p : pm.getContents(this)) {

      final double dist = Point.distance(roadModel.get().getPosition(this),
          p.getDestination());
      if (dist < closest
          && pm.getTimeWindowPolicy().canDeliver(p.getDeliveryTimeWindow(),
              time.getTime(), p.getPickupDuration())) {
        closest = dist;
        target = p;
      }
    }
View Full Code Here

    final Graph<?> g = createGrid(width, height, 1, VERTICAL_LINE_SPACING,
        SPACING);

    final RoadModel roadModel = new BlockingGraphRoadModel(g, SI.METER,
        NonSI.KILOMETERS_PER_HOUR);
    final PDPModel pdpModel = new DefaultPDPModel();
    simulator.register(roadModel);
    simulator.register(pdpModel);

    final List<Point> borderNodes = newArrayList(getBorderNodes(g));
    Collections.shuffle(borderNodes, new Random(123));
View Full Code Here

  }

  @Override
  protected void tickImpl(TimeLapse time) {
    final RoadModel rm = roadModel.get();
    final PDPModel pm = pdpModel.get();
    // we always go to the closest available parcel
    final DefaultParcel closest = (DefaultParcel) RoadModels.findClosestObject(
        rm.getPosition(this), rm, new Predicate<RoadUser>() {
          @Override
          public boolean apply(RoadUser input) {
            return input instanceof DefaultParcel
                && pm.getParcelState(((DefaultParcel) input)) == ParcelState.AVAILABLE;
          }
        });

    if (closest != null) {
      rm.moveTo(this, closest, time);
      if (rm.equalPosition(closest, this)
          && pm.getTimeWindowPolicy().canPickup(closest.getPickupTimeWindow(),
              time.getTime(), closest.getPickupDuration())) {
        pm.pickup(this, closest, time);
      }
    }
  }
View Full Code Here

     * @return The newly created adapter.
     */
    @SuppressWarnings("unchecked")
    public T build() {
      PDPRoadModel rm = roadModel;
      PDPModel pm = pdpModel;
      if (rm == null || pm == null) {
        // in this case we need a model provider
        ModelProvider mp = modelProvider;
        if (mp == null) {
          checkArgument(
View Full Code Here

  }

  @Override
  public MoveProgress moveTo(MovingRoadUser object,
      RoadUser destinationRoadUser, TimeLapse time) {
    final PDPModel pm = pdpModel.get();
    DestinationObject newDestinationObject;
    if (destinationRoadUser instanceof DefaultParcel) {
      final DefaultParcel dp = (DefaultParcel) destinationRoadUser;
      final DestType type = containsObject(dp) ? DestType.PICKUP
          : DestType.DELIVERY;
      final Point pos = getParcelPos(destinationRoadUser);
      if (type == DestType.DELIVERY) {
        checkArgument(
            pm.containerContains((DefaultVehicle) object,
                (DefaultParcel) destinationRoadUser),
            "A vehicle can only move to the delivery location of a parcel if it is carrying it.");
      }
      newDestinationObject = new DestinationObject(type, pos, dp);
    } else {
      newDestinationObject = new DestinationObject(DestType.DEPOT,
          getPosition(destinationRoadUser), destinationRoadUser);
    }

    boolean destChange = true;
    if (destinations.containsKey(object) && !allowDiversion) {
      final DestinationObject prev = destinations.get(object);
      final boolean atDestination = getPosition(object).equals(prev.dest);

      if (!atDestination && prev.type != DestType.DEPOT) {
        // when we haven't reached our destination and the destination
        // isn't the depot we are not allowed to change destination
        checkArgument(
            prev.roadUser == destinationRoadUser
                || isAlreadyServiced(prev.type, prev.roadUser),
            "Diversion from the current destination is not allowed. Prev: %s, new: %s.",
            prev, destinationRoadUser);
        destChange = false;
      } else
      // change destination
      if (prev.type == DestType.PICKUP) {
        // when we are at the prev destination, and it was a pickup, we are
        // allowed to move if it has been picked up
        checkArgument(
            pm.getParcelState((DefaultParcel) prev.roadUser) != ParcelState.AVAILABLE,
            "Can not move away before the parcel has been picked up: %s.",
            prev.roadUser);
      } else if (prev.type == DestType.DELIVERY) {
        // when we are at the prev destination and it was a delivery, we are
        // allowed to move to other objects only, and only if the parcel is
        // delivered
        checkArgument(prev.roadUser != destinationRoadUser,
            "Can not move to the same parcel since we are already there: %s.",
            prev.roadUser);
        checkArgument(
            pm.getParcelState((DefaultParcel) prev.roadUser) == ParcelState.DELIVERED,
            "The parcel needs to be delivered before moving away: %s.",
            prev.roadUser);
      } else {
        // it is a depot
        // the destination is only changed in case we are no longer going
View Full Code Here

  public TimeLinePanel() {}

  @Override
  public void registerModelProvider(ModelProvider mp) {
    final PDPModel pdp = mp.getModel(PDPModel.class);
    checkState(pdp != null, "PDPModel is required.");
    pdp.getEventAPI().addListener(new Listener() {
      @Override
      public void handleEvent(Event e) {
        if (e.getEventType() == PDPModelEventType.NEW_PARCEL) {
          final PDPModelEvent event = (PDPModelEvent) e;
          timeline.addParcel(new ParcelInfo(event.time, event.parcel));
View Full Code Here

      // the route has changed (there is no destination anymore)
      if (route.isEmpty()) {
        return DefaultEvent.NOGO;
      }
      checkCurrentParcelOwnership();
      final PDPModel pm = pdpModel.get();
      final TimeLapse time = currentTime.get();
      final DefaultParcel cur = route.element();
      // we are not at the parcel's position, this means the next parcel has
      // changed in the mean time, so we have to reroute.
      if (!roadModel.get().equalPosition(context, cur)) {
        return DefaultEvent.REROUTE;
      }
      // if parcel is not ready yet, wait
      final boolean pickup = !pm.getContents(context).contains(cur);
      final long timeUntilReady = (pickup ? cur.dto.pickupTimeWindow.begin
          : cur.dto.deliveryTimeWindow.begin) - time.getTime();
      if (timeUntilReady > 0) {
        if (time.getTimeLeft() < timeUntilReady) {
          // in this case we can not yet start servicing
View Full Code Here

TOP

Related Classes of rinde.sim.core.model.pdp.PDPModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.