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; }
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; }
/** * 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 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; } } }
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; }
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 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; }
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; }
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; }
private double distance(Vehicle v, Action a) { return (v == null || a == null) ? 0 : v.getCurrentCity().distanceTo(a.actionCity); }