public List<Vehicle> distForATime(Queue<Client> queue) { List<Vehicle> list = new LinkedList<Vehicle>(); List<Client> badClientList = new LinkedList<Client>(); Object[] clientList = queue.toArray(); Vehicle vehicle = new Vehicle(); vehicle.addAClient(clients[0]); list.add(vehicle); if (getRemainingRequireMents(list) > Vehicle.getMaxMaxLoad()) { vehicle.setLoad(Vehicle.getMaxMaxLoad()); } else { vehicle.setLoad(getRemainingRequireMents(list)); } for (int i = 0; i < clientList.length; i++) { Client client = (Client) clientList[i]; if (validateForAClient(vehicle, client)) { vehicle.addAClient(client); List<Client> addedList = new LinkedList<Client>(); for (Client client1 : badClientList) { if (validateForAClient(vehicle, client1)) { addedList.add(client1); vehicle.addAClient(client1); } } for (Client client1 : addedList) { badClientList.remove(client1); } } else { vehicle = new Vehicle(); list.add(vehicle); vehicle.addAClient(clients[0]); if (getRemainingRequireMents(list) > Vehicle.getMaxMaxLoad()) { vehicle.setLoad(Vehicle.getMaxMaxLoad()); } else { vehicle.setLoad(getRemainingRequireMents(list)); } if (!validateForAClient(vehicle, client)) { badClientList.add(client); } else { i = i - 1; } } } if (0 != badClientList.size()) { for (Client client : badClientList) { // System.out.println("bad client " + client); } return null; } for (Vehicle vehicle1 : list) { vehicle1.addAClient(clients[0]); } /* if (2 == list.size()) { return null; }*/ for (Object vehicle1 : list.toArray()) { if (((Vehicle) vehicle1).getClients().length == 2) { list.remove(vehicle1); } } return list; }
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; }