Package jsprit.core.problem.solution.route.activity

Examples of jsprit.core.problem.solution.route.activity.TourActivity$JobActivity


    }
  }

  private void setRouteEndToLastActivity(VehicleRoute route) {
    if(!route.getActivities().isEmpty()){
      TourActivity lastAct = route.getActivities().get(route.getActivities().size()-1);
      route.getEnd().setLocationId(lastAct.getLocationId());
    }
  }
View Full Code Here


   * @return
   */
  public static double calculateTransportTime(Collection<VehicleRoute> routes, TransportTime transportTimes) {
    double tpTime = 0.;
    for(VehicleRoute r : routes){
      TourActivity lastact = r.getStart();
      double lastActDepTime = r.getDepartureTime();
      for(TourActivity act : r.getActivities()){
        tpTime += transportTimes.getTransportTime(lastact.getLocationId(), act.getLocationId(), lastActDepTime, DriverImpl.noDriver(), r.getVehicle());
        lastact=act;
        lastActDepTime=act.getEndTime();
      }
      tpTime+=transportTimes.getTransportTime(lastact.getLocationId(), r.getEnd().getLocationId(), lastActDepTime, DriverImpl.noDriver(), r.getVehicle());
    }
    return tpTime;
  }
View Full Code Here

  @Test
  public void whenAddingTwoSoftConstrainta_managerShouldSumCostsCorrectly(){
    SoftActivityConstraint c1 = mock(SoftActivityConstraint.class);
    JobInsertionContext iContext = mock(JobInsertionContext.class);
    TourActivity act_i = mock(TourActivity.class);
    TourActivity act_k = mock(TourActivity.class);
    TourActivity act_j = mock(TourActivity.class);
    when(c1.getCosts(iContext,act_i,act_k,act_j,0.0)).thenReturn(1.0);
    SoftActivityConstraint c2 = mock(SoftActivityConstraint.class);
    when(c2.getCosts(iContext,act_i,act_k,act_j,0.0)).thenReturn(2.0);
   
    SoftActivityConstraintManager man = new SoftActivityConstraintManager();
View Full Code Here

  @Override
  public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
    JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
        Service service = (Service)jobToInsert;
        int insertionIndex = InsertionData.NO_INDEX;
        TourActivity deliveryAct2Insert = activityFactory.createActivities(service).get(0);
        insertionContext.getAssociatedActivities().add(deliveryAct2Insert);

        /*
        check hard constraints at route level
         */
        if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
      return InsertionData.createEmptyInsertionData();
    }

        /*
        check soft constraints at route level
         */
        double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);

    double bestCost = bestKnownCosts;
        additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);

        /*
        generate new start and end for new vehicle
         */
    Start start = Start.newInstance(newVehicle.getStartLocationId(), newVehicle.getEarliestDeparture(), Double.MAX_VALUE);
    start.setEndTime(newVehicleDepartureTime);
    End end = End.newInstance(newVehicle.getEndLocationId(), 0.0, newVehicle.getLatestArrival());
   
    TourActivity prevAct = start;
    double prevActStartTime = newVehicleDepartureTime;
    int actIndex = 0;
    boolean loopBroken = false;
    for(TourActivity nextAct : currentRoute.getTourActivities().getActivities()){
      ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
      if(status.equals(ConstraintsStatus.FULFILLED)){
        //from job2insert induced costs at activity level
        double additionalICostsAtActLevel = softActivityConstraint.getCosts(insertionContext, prevAct, deliveryAct2Insert, nextAct, prevActStartTime);
        double additionalTransportationCosts = additionalTransportCostsCalculator.getCosts(insertionContext, prevAct, nextAct, deliveryAct2Insert, prevActStartTime);
        if(additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts < bestCost){
          bestCost = additionalICostsAtRouteLevel + additionalICostsAtActLevel + additionalTransportationCosts;
          insertionIndex = actIndex;
        }
      }
      else if(status.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)){
        loopBroken = true;
        break;
      }
      double nextActArrTime = prevActStartTime + transportCosts.getTransportTime(prevAct.getLocationId(), nextAct.getLocationId(), prevActStartTime, newDriver, newVehicle);
      prevActStartTime = CalculationUtils.getActivityEndTime(nextActArrTime, nextAct);
      prevAct = nextAct;
      actIndex++;
    }
    if(!loopBroken){
View Full Code Here

   */
  @Override
  public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
        JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
        Shipment shipment = (Shipment)jobToInsert;
        TourActivity pickupShipment = activityFactory.createActivities(shipment).get(0);
        TourActivity deliverShipment = activityFactory.createActivities(shipment).get(1);
        insertionContext.getAssociatedActivities().add(pickupShipment);
        insertionContext.getAssociatedActivities().add(deliverShipment);

        /*
        check hard route constraints
         */
        if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
      return InsertionData.createEmptyInsertionData();
    }
        /*
        check soft route constraints
         */
        double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);

        double bestCost = bestKnownCosts;
        additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);

        int pickupInsertionIndex = InsertionData.NO_INDEX;
    int deliveryInsertionIndex = InsertionData.NO_INDEX;
   
    Start start = Start.newInstance(newVehicle.getStartLocationId(), newVehicle.getEarliestDeparture(), newVehicle.getLatestArrival());
    start.setEndTime(newVehicleDepartureTime);
   
    End end = End.newInstance(newVehicle.getEndLocationId(), 0.0, newVehicle.getLatestArrival());

        ActivityContext pickupContext = new ActivityContext();

    TourActivity prevAct = start;
    double prevActEndTime = newVehicleDepartureTime;
    boolean pickupShipmentLoopBroken = false;
    //pickupShipmentLoop
    List<TourActivity> activities = currentRoute.getTourActivities().getActivities();
    for(int i=0;i<activities.size();i++){
      ConstraintsStatus pickupShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, pickupShipment, activities.get(i), prevActEndTime);
      if(pickupShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED)){
        double nextActArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), activities.get(i).getLocationId(), prevActEndTime, newDriver, newVehicle);
        prevActEndTime = CalculationUtils.getActivityEndTime(nextActArrTime, activities.get(i));
        prevAct = activities.get(i);
        continue;
      }
      else if(pickupShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)){
        pickupShipmentLoopBroken = true;
        break;
      }
      double additionalPickupICosts = softActivityConstraint.getCosts(insertionContext, prevAct, pickupShipment, activities.get(i), prevActEndTime);
      double pickupAIC = calculate(insertionContext,prevAct,pickupShipment,activities.get(i),prevActEndTime);
      TourActivity prevAct_deliveryLoop = pickupShipment;
      double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), pickupShipment.getLocationId(), prevActEndTime, newDriver, newVehicle);
      double shipmentPickupEndTime = CalculationUtils.getActivityEndTime(shipmentPickupArrTime, pickupShipment);

            pickupContext.setArrivalTime(shipmentPickupArrTime);
            pickupContext.setEndTime(shipmentPickupEndTime);
            pickupContext.setInsertionIndex(i);
            insertionContext.setRelatedActivityContext(pickupContext);

            double prevActEndTime_deliveryLoop = shipmentPickupEndTime;
      boolean deliverShipmentLoopBroken = false;
      //deliverShipmentLoop
      for(int j=i;j<activities.size();j++){
        ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, activities.get(j), prevActEndTime_deliveryLoop);
        if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
          double additionalDeliveryICosts = softActivityConstraint.getCosts(insertionContext, prevAct_deliveryLoop, deliverShipment, activities.get(j), prevActEndTime_deliveryLoop);
          double deliveryAIC = calculate(insertionContext,prevAct_deliveryLoop,deliverShipment,activities.get(j),prevActEndTime_deliveryLoop);
          double totalActivityInsertionCosts = pickupAIC + deliveryAIC
              + additionalICostsAtRouteLevel + additionalPickupICosts + additionalDeliveryICosts;
          if(totalActivityInsertionCosts < bestCost){
            bestCost = totalActivityInsertionCosts;
            pickupInsertionIndex = i;
            deliveryInsertionIndex = j;
          }
        }
        else if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)){
          deliverShipmentLoopBroken = true;
          break;
       
        //update prevAct and endTime
        double nextActArrTime = prevActEndTime_deliveryLoop + transportCosts.getTransportTime(prevAct_deliveryLoop.getLocationId(), activities.get(j).getLocationId(), prevActEndTime_deliveryLoop, newDriver, newVehicle);
        prevActEndTime_deliveryLoop = CalculationUtils.getActivityEndTime(nextActArrTime, activities.get(j));
        prevAct_deliveryLoop = activities.get(j);
      }
      if(!deliverShipmentLoopBroken){ //check insertion between lastAct and endOfTour
        ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
        if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
          double additionalDeliveryICosts = softActivityConstraint.getCosts(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
          double deliveryAIC = calculate(insertionContext,prevAct_deliveryLoop,deliverShipment,end,prevActEndTime_deliveryLoop);
          double totalActivityInsertionCosts = pickupAIC + deliveryAIC
              + additionalICostsAtRouteLevel + additionalPickupICosts + additionalDeliveryICosts;
          if(totalActivityInsertionCosts < bestCost){
            bestCost = totalActivityInsertionCosts;
            pickupInsertionIndex = i;
            deliveryInsertionIndex = activities.size();
          }
        }
      }
      //update prevAct and endTime
      double nextActArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), activities.get(i).getLocationId(), prevActEndTime, newDriver, newVehicle);
      prevActEndTime = CalculationUtils.getActivityEndTime(nextActArrTime, activities.get(i));
      prevAct = activities.get(i);
    }
    if(!pickupShipmentLoopBroken){ //check insertion of pickupShipment and deliverShipment at just before tour ended
      ConstraintsStatus pickupShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, pickupShipment, end, prevActEndTime);
      if(pickupShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
        double additionalPickupICosts = softActivityConstraint.getCosts(insertionContext, prevAct, pickupShipment, end, prevActEndTime);
        double pickupAIC = calculate(insertionContext,prevAct,pickupShipment,end,prevActEndTime);
        TourActivity prevAct_deliveryLoop = pickupShipment;
        double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), pickupShipment.getLocationId(), prevActEndTime, newDriver, newVehicle);
        double shipmentPickupEndTime = CalculationUtils.getActivityEndTime(shipmentPickupArrTime, pickupShipment);
        double prevActEndTime_deliveryLoop = shipmentPickupEndTime;

                pickupContext.setArrivalTime(shipmentPickupArrTime);
View Full Code Here

    if(path.isEmpty()){
      return 0.0;
    }
    double cost = 0.0;
    Iterator<TourActivity> actIter = path.iterator();
    TourActivity prevAct = actIter.next();
    double startCost = 0.0;
    cost += startCost;
    double departureTimePrevAct = depTime;
    while(actIter.hasNext()){
      TourActivity act = actIter.next();
      if(act instanceof End){
        if(!vehicle.isReturnToDepot()){
          return cost;
        }
      }
      double transportCost = routingCosts.getTransportCost(prevAct.getLocationId(), act.getLocationId(), departureTimePrevAct, driver, vehicle);
      double transportTime = routingCosts.getTransportTime(prevAct.getLocationId(), act.getLocationId(), departureTimePrevAct, driver, vehicle);
      cost += transportCost;
      double actStartTime = departureTimePrevAct + transportTime;
            departureTimePrevAct = Math.max(actStartTime, act.getTheoreticalEarliestOperationStartTime()) + act.getOperationTime();
      cost += activityCosts.getActivityCost(act, actStartTime, driver, vehicle);
      prevAct = act;
    }
    return cost;
  }
View Full Code Here

      double accessTransportCostOld = routingCosts.getTransportCost(currentRoute.getStart().getLocationId(), currentRoute.getActivities().get(0).getLocationId(), currentRoute.getDepartureTime(), currentRoute.getDriver(), currentRoute.getVehicle());
     
      delta_access = accessTransportCostNew - accessTransportCostOld;
     
      if(newVehicle.isReturnToDepot()){
        TourActivity lastActivityBeforeEndOfRoute = currentRoute.getActivities().get(currentRoute.getActivities().size()-1);
        double lastActivityEndTimeWithOldVehicleAndDepartureTime = lastActivityBeforeEndOfRoute.getEndTime();
        double lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime = Math.max(0.0, lastActivityEndTimeWithOldVehicleAndDepartureTime + (newVehicleDepartureTime - currentRoute.getDepartureTime()));
        double egressTransportCostNew = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), newVehicle.getEndLocationId() , lastActivityEndTimeEstimationWithNewVehicleAndNewDepartureTime, newDriver, newVehicle);
        double egressTransportCostOld = routingCosts.getTransportCost(lastActivityBeforeEndOfRoute.getLocationId(), currentRoute.getEnd().getLocationId(), lastActivityEndTimeWithOldVehicleAndDepartureTime, currentRoute.getDriver(), currentRoute.getVehicle());
       
        delta_egress = egressTransportCostNew - egressTransportCostOld;
      }
    }
    return delta_access + delta_egress;
View Full Code Here

   
    /**
     * some inis
     */
    TourActivity serviceAct2Insert = activityFactory.createActivities(service).get(0);
    int best_insertion_index = InsertionData.NO_INDEX;
   
    initialiseStartAndEnd(newVehicle, newVehicleDepartureTime);
   
    TourActivity prevAct = start;
    int actIndex = 0;
    double sumOf_prevCosts_newVehicle = 0.0;
    double prevActDepTime_newVehicle = start.getEndTime();

        boolean loopBroken = false;
    /**
     * inserting serviceAct2Insert in route r={0,1,...,i-1,i,j,j+1,...,n(r),n(r)+1}
     * i=prevAct
     * j=nextAct
     * k=serviceAct2Insert
     */
    for(TourActivity nextAct : tour.getActivities()){
            ConstraintsStatus hardActivityConstraintsStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, serviceAct2Insert, nextAct, prevActDepTime_newVehicle);
            if(hardActivityConstraintsStatus.equals(ConstraintsStatus.FULFILLED)){
                /**
                 * builds a path on this route forwardPath={i,k,j,j+1,j+2,...,j+nuOfActsForwardLooking}
                 */
                double actInsertionCosts = activityInsertionCostsCalculator.getCosts(insertionContext, prevAct, nextAct, serviceAct2Insert, prevActDepTime_newVehicle);

                /**
                 * insertion_cost_approximation = c({0,1,...,i},newVehicle) + c({i,k,j,j+1,j+2,...,j+nuOfActsForwardLooking},newVehicle) - c({0,1,...,i,j,j+1,...,j+nuOfActsForwardLooking},oldVehicle)
                 */
                double insertion_cost_approximation = sumOf_prevCosts_newVehicle - sumOf_prevCosts_oldVehicle(currentRoute,prevAct) + actInsertionCosts;

                /**
                 * memorize it in insertion-queue
                 */
                if(insertion_cost_approximation < best_known_insertion_costs){
                    bestInsertionsQueue.add(new InsertionData(insertion_cost_approximation, InsertionData.NO_INDEX, actIndex, newVehicle, newDriver));
                }
            }
            else if(hardActivityConstraintsStatus.equals(ConstraintsStatus.NOT_FULFILLED_BREAK)){
                loopBroken = true;
                break;
            }


            /**
             * calculate transport and activity costs with new vehicle (without inserting k)
             */
      double transportCost_prevAct_nextAct_newVehicle = transportCosts.getTransportCost(prevAct.getLocationId(), nextAct.getLocationId(), prevActDepTime_newVehicle, newDriver, newVehicle);
      double transportTime_prevAct_nextAct_newVehicle = transportCosts.getTransportTime(prevAct.getLocationId(), nextAct.getLocationId(), prevActDepTime_newVehicle, newDriver, newVehicle);
      double arrTime_nextAct_newVehicle = prevActDepTime_newVehicle + transportTime_prevAct_nextAct_newVehicle;
      double activityCost_nextAct = activityCosts.getActivityCost(nextAct, arrTime_nextAct_newVehicle, newDriver, newVehicle);

      /**
       * memorize transport and activity costs with new vehicle without inserting k
View Full Code Here

   
    {
      Iterator<TourActivity> iter = route.getTourActivities().iterator();
      int count = 0;
      while(iter.hasNext()){
        @SuppressWarnings("unused")
        TourActivity act = iter.next();
        count++;
      }
      assertEquals(1,count);
    }
    {
      route.getTourActivities().addActivity(ServiceActivity.newInstance(Service.Builder.newInstance("3").addSizeDimension(0, 30).setLocationId("1").build()));
      Iterator<TourActivity> iter = route.getTourActivities().iterator();
      int count = 0;
      while(iter.hasNext()){
        @SuppressWarnings("unused")
        TourActivity act = iter.next();
        count++;
      }
      assertEquals(2,count);
    }
View Full Code Here

  public void whenBuildingANonEmptyTour_tourReverseIterIteratesOverActivitiesCorrectly(){
    VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, driver).build();
    Iterator<TourActivity> iter = route.getTourActivities().reverseActivityIterator();
    int count = 0;
    while(iter.hasNext()){
      @SuppressWarnings("unused")
      TourActivity act = iter.next();
      count++;
    }
    assertEquals(0,count);
  }
View Full Code Here

TOP

Related Classes of jsprit.core.problem.solution.route.activity.TourActivity$JobActivity

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.