示例#1
0
  public Plan generatePlan(Vehicle vehicle) {

    int vehicleId = vehicle.id();
    City current = vehicle.getCurrentCity();

    Plan p = new Plan(current);

    List<ExtendedTask> tasks = solution.get(vehicleId);

    City intermediateCity = current;
    for (ExtendedTask t : tasks) {
      City nextDestination = t.isPickup() ? t.getT().pickupCity : t.getT().deliveryCity;

      for (City c : intermediateCity.pathTo(nextDestination)) {
        p.appendMove(c);
      }

      if (t.isPickup()) p.appendPickup(t.getT());
      else p.appendDelivery(t.getT());

      intermediateCity = nextDestination;
    }

    return p;
  }
  /**
   * A* algorithm
   *
   * @param vehicle the considered vehicle
   * @param available the available tasks of the world
   * @param carried the tasks picked up and currently carried by the vehicle
   * @return a plan (list of actions)
   */
  public static Plan computeAStar(Vehicle vehicle, TaskSet available, TaskSet carried) {
    Plan plan = null;

    ArrayList<Task> availableTasks = new ArrayList<Task>(available);
    ArrayList<Task> carriedTasks = new ArrayList<Task>(carried);

    PriorityQueue<State> Q =
        new PriorityQueue<State>(1, new StateComparator()); // States we will have to visit
    ArrayList<State> C = new ArrayList<State>(); // States we have already visited, prevent cycles

    City initialCity = vehicle.getCurrentCity();
    State initialState =
        new State(
            initialCity, availableTasks, carriedTasks, new ArrayList<Action>(), vehicle, 0, 0);

    Q.add(initialState);

    boolean foundFinalState = false;
    State finalState = null;

    while (!foundFinalState) {

      if (Q.isEmpty()) {
        foundFinalState = true;

      } else {
        State visitingState = Q.poll();

        if (visitingState.isFinal()) {
          finalState = visitingState;
          foundFinalState = true;
        } else if (!C.contains(visitingState)
            || (C.contains(visitingState)
                && C.get(C.indexOf(visitingState)).heuristicValue > visitingState.heuristicValue)) {
          C.add(visitingState);
          Q.addAll(visitingState.next()); // Hopefully at the end of the list
        }
      }
    }

    if (finalState != null) {
      plan = new Plan(vehicle.getCurrentCity(), finalState.actionList);
    }

    return plan;
  }
  private State computeInitialState(Vehicle vehicle, TaskSet tasks) {
    List<City> listTasksCities = new ArrayList<Topology.City>();
    List<Boolean> isDeliver = new ArrayList<Boolean>();
    for (Task task : tasks) {
      listTasksCities.add(task.pickupCity);
      isDeliver.add(false);
    }

    return new State(vehicle.getCurrentCity(), tasks, isDeliver);
  }
  private Plan BFSPlan(Vehicle vehicle, TaskSet tasks) {
    City current = vehicle.getCurrentCity();
    Plan plan = new Plan(current);
    System.out.println("Compute BFS for vehicle :" + vehicle.id());
    System.out.println(tasks.toString());

    // Initialize states
    State initialState = computeInitialState(vehicle, tasks);
    List<State> finalStates = computeFinalStates(vehicle, tasks);

    // Build the tree
    return plan;
  }
  private Plan naivePlan(Vehicle vehicle, TaskSet tasks) {
    City current = vehicle.getCurrentCity();
    Plan plan = new Plan(current);
    System.out.println("Compute naivePlan for vehicle :" + vehicle.id());
    System.out.println(tasks.toString());
    for (Task task : tasks) {
      // move: current city => pickup location
      for (City city : current.pathTo(task.pickupCity)) plan.appendMove(city);

      plan.appendPickup(task);

      // move: pickup location => delivery location
      for (City city : task.path()) plan.appendMove(city);

      plan.appendDelivery(task);

      // set current city
      current = task.deliveryCity;
    }
    return plan;
  }
示例#6
0
 private double distance(Vehicle v, Action a) {
   return (v == null || a == null) ? 0 : v.getCurrentCity().distanceTo(a.actionCity);
 }