Ejemplo n.º 1
0
  public double compute(Assignment a) {

    double sum = 0;

    for (Entry<Vehicle, List<Action>> e : a.vehicleRoutes.entrySet()) {
      Vehicle v = e.getKey();
      List<Action> route = e.getValue();
      if (!route.isEmpty()) {
        sum += distance(v, route.get(0)) * v.costPerKm();
        Action lastAction = route.get(0);
        for (Action act : route) {
          sum += distance(lastAction, act) * v.costPerKm();
          lastAction = act;
        }
      }
    }

    return sum;
  }
Ejemplo n.º 2
0
  public State(
      City currentCity,
      ArrayList<Task> availableTasks,
      ArrayList<Task> pickedUpTasks,
      ArrayList<Action> actionList,
      Vehicle vehicle,
      double totalCost,
      int weightCarried) {

    this.currentCity = currentCity;
    this.availableTasks = availableTasks;
    this.carriedTasks = pickedUpTasks;
    this.actionList = actionList;

    this.vehicle = vehicle;

    this.totalCost = totalCost;
    this.weightCarried = weightCarried;

    double futureCost = 0;
    for (Task task : availableTasks) {
      double taskCost =
          (currentCity.distanceTo(task.pickupCity) + task.pickupCity.distanceTo(task.deliveryCity))
              * vehicle.costPerKm();
      if (taskCost > futureCost) {
        futureCost = taskCost;
      }
    }
    for (Task task : carriedTasks) {
      double taskCost = currentCity.distanceTo(task.deliveryCity) * vehicle.costPerKm();
      if (taskCost > futureCost) {
        futureCost = taskCost;
      }
    }

    heuristicValue = totalCost + futureCost;
  }
Ejemplo n.º 3
0
  private void init() {
    costs = new double[topology.size()][topology.size()];

    costPerKm = Integer.MAX_VALUE;
    for (Vehicle vehicle : agent.vehicles()) {
      costPerKm = Math.min(costPerKm, vehicle.costPerKm());
    }

    for (City from : topology.cities()) {
      for (City to : topology.cities()) {
        if (from.equals(to)) {
          continue;
        }
        // cost per unit
        costs[from.id][to.id] = -1;
      }
    }
  }
Ejemplo n.º 4
0
  public ArrayList<State> next() {

    ArrayList<State> nextStates = new ArrayList<State>();

    /*
     * The only interesting next stops are the ones where we either
     * pick up or deliver tasks.
     */

    ArrayList<City> interestingCities = new ArrayList<City>();

    for (Task carriedtask : carriedTasks) {
      if (!interestingCities.contains(carriedtask.deliveryCity)) {
        interestingCities.add(carriedtask.deliveryCity);
      }
    }

    for (Task availabletask : availableTasks) {
      if (!interestingCities.contains(availabletask.pickupCity)) {
        interestingCities.add(availabletask.pickupCity);
      }
    }

    for (City nextCity : interestingCities) {

      City newCurrentCity = nextCity;

      ArrayList<Task> newAvailableTasks = new ArrayList<Task>(availableTasks);
      ArrayList<Task> newCarriedTasks = new ArrayList<Task>(carriedTasks);
      ArrayList<Action> newActionList = new ArrayList<Action>(actionList);

      for (City city : currentCity.pathTo(nextCity)) {
        newActionList.add(new Move(city));
      }

      double newTotalCost =
          totalCost + currentCity.distanceTo(newCurrentCity) * vehicle.costPerKm();
      int newWeightCarried = weightCarried;

      ArrayList<Task> deliveredTasks = new ArrayList<Task>();
      for (Task task : newCarriedTasks) {
        if (task.deliveryCity.equals(nextCity)) {
          newActionList.add(new Delivery(task));
          newWeightCarried = newWeightCarried - task.weight;
          deliveredTasks.add(task);
        }
      }

      newCarriedTasks.removeAll(deliveredTasks);

      ArrayList<Task> pickedUpTasks = new ArrayList<Task>();
      for (Task task : newAvailableTasks) {
        if (task.pickupCity.equals(nextCity)
            && vehicle.capacity() >= newWeightCarried + task.weight) {
          newActionList.add(new Pickup(task));
          newWeightCarried = newWeightCarried + task.weight;
          pickedUpTasks.add(task);
        }
      }

      newAvailableTasks.removeAll(pickedUpTasks);
      newCarriedTasks.addAll(pickedUpTasks);

      State state =
          new State(
              newCurrentCity,
              newAvailableTasks,
              newCarriedTasks,
              newActionList,
              this.vehicle,
              newTotalCost,
              newWeightCarried);

      nextStates.add(state);
    }

    return nextStates;
  }