public Client[] getPath(List<Path> paths) { List<Path> newPath = new LinkedList<Path>(); for (Path path : paths) { newPath.add(path); } List<Client> clientsOnPath = new LinkedList<Client>(); clientsOnPath.add(clients[0]); clientsOnPath.add(newPath.get(0).getStart()); clientsOnPath.add(newPath.get(0).getEnd()); newPath.remove(0); for (Path path : newPath) { Client start = path.getStart(); Client end = path.getEnd(); if (!clientsOnPath.contains(start)) { clientsOnPath.add(start); } if (!clientsOnPath.contains(end)) { clientsOnPath.add(end); } } clientsOnPath.add(clients[0]); Client[] result = new Client[clientsOnPath.size()]; for (int i = 0; i < clientsOnPath.size(); i++) { result[i] = clientsOnPath.get(i); } return result; }
public List<Vehicle> distribute() { List<Vehicle> vehicles = new LinkedList<Vehicle>(); PriorityQueue<Path> ps = getSavedPaths(); List<Path> paths = new LinkedList<Path>(); Path[] tmpPath = new Path[ps.size()]; for (int j = 0; j < tmpPath.length; j++) { tmpPath[j] = ps.remove(); } for (int j = tmpPath.length - 1; j >= 0; j--) { System.out.println(tmpPath[j]); paths.add(tmpPath[j]); } while (!isDone(vehicles)) { List<Path> p = new LinkedList<Path>(); Vehicle vehicle = new Vehicle(); if (getRemainingRequireMents(vehicles) > Vehicle.getMaxMaxLoad()) { vehicle.setLoad(Vehicle.getMaxMaxLoad()); } else { vehicle.setLoad(getRemainingRequireMents(vehicles)); } Client[] c = new Client[1]; c[0] = clients[0]; vehicle.setClients(c); boolean pathAdded = false; for (int i = 0; i < paths.size(); i++) { Path path = paths.get(i); if (validate(vehicle, p, path)) { p.add(path); // System.out.println("add path " + path); vehicle.setClients(getPath(p)); paths.remove(i); // System.out.println("remove " + path); pathAdded = true; } } if (pathAdded) { vehicles.add(vehicle); } /*将已经配送过的点的边,去掉*/ for (Vehicle v1 : vehicles) { for (Client client : v1.getClients()) { // System.out.println("Client:"+client); Object[] pathsArray = paths.toArray(); for (int i = 0; i < pathsArray.length; i++) { Path path = (Path) pathsArray[i]; // System.out.println("Path to validate "+ path); if ((path.getEnd() == client)) { // System.out.println("end remove " + path); paths.remove(path); } if (path.getStart() == client) { // System.out.println("start remove " + path); paths.remove(path); } } } } /*如果一条边上的点requirement 之和大于vehicle的载重,去掉。*/ Object[] pathArray = paths.toArray(); for (int i = 0; i < pathArray.length; i++) { Path path = (Path) pathArray[i]; if (path.getEnd().getRequirement() + path.getStart().getRequirement() > vehicle.getMaxLoad()) { paths.remove(path); } } // 最后还有一些孤立的配送点需要配送 if (paths.size() == 0) { List<Client> remainingClients = getRemainingClient(vehicles); for (Client client : remainingClients) { Client[] c0 = new Client[3]; c0[0] = clients[0]; c0[1] = client; c0[2] = clients[0]; Vehicle vehicle1 = new Vehicle(); if (getRemainingRequireMents(vehicles) > Vehicle.getMaxMaxLoad()) { vehicle1.setLoad(Vehicle.getMaxMaxLoad()); } else { vehicle1.setLoad(getRemainingRequireMents(vehicles)); } vehicle1.setClients(c0); vehicles.add(vehicle1); } return vehicles; } } return vehicles; }