public double getDistance(List<Vehicle> vehicles) { double distance = 0.0; for (Vehicle vehicle : vehicles) { Client prevClient = vehicle.getClients()[0]; for (int i = 1; i < vehicle.getClients().length - 1; i++) { distance += Client.getDistance(prevClient, vehicle.getClients()[i]); prevClient = vehicle.getClients()[i]; } } return distance; }
private PriorityQueue<Path> getSavedPaths() { double[][] minDistances = getMinDistances(); PriorityQueue<Path> paths = new PriorityQueue<Path>(); for (int i = 1; i < clients.length; i++) { for (int j = i + 1; j < clients.length; j++) { if (i == j) { continue; } Path path = new Path(clients[i], clients[j]); double saved = minDistances[0][i] + minDistances[0][j] - Client.getDistance(clients[i], clients[j]); path.setDistance(saved); paths.add(path); } } return paths; }
public boolean validateForAClient(Vehicle vehicle, Client cli) { double load = vehicle.getLoad(); double distance = 0; double requirementTotal = 0.0; // 这个配送路线上所有的需求之和 Client[] newClients = new Client[vehicle.getClients().length + 2]; for (int i = 0; i < vehicle.getClients().length; i++) { newClients[i] = vehicle.getClients()[i]; } newClients[newClients.length - 2] = cli; newClients[newClients.length - 1] = clients[0]; Client prevClient = clients[0]; for (Client client : newClients) { load += client.getSupply() - client.getRequirement(); distance += Client.getDistance(prevClient, client); prevClient = client; requirementTotal += client.getRequirement(); } // 这条路径上的用户需求之和不能大于卡车的运输能力 if (requirementTotal > vehicle.getMaxLoad()) { // System.out.println("requirement > maxLoad"); return false; } if (load > vehicle.getMaxLoad() || load < 0) { // System.out.println("overload"); return false; } if (distance > vehicle.getMaxDistance()) { // System.out.println("over distance"); return false; } return true; }
public boolean validate(Vehicle vehicle, List<Path> paths, Path path) { double load = vehicle.getLoad(); double distance = 0; double requirementTotal = 0.0; // 这个配送路线上所有的需求之和 List newPath = new LinkedList(); for (Path p : paths) { newPath.add(p); } newPath.add(path); Client[] clientsOnPath = getPath(newPath); Client prevClient = clientsOnPath[0]; for (Client client : clientsOnPath) { load += client.getSupply() - client.getRequirement(); distance += Client.getDistance(prevClient, client); prevClient = client; requirementTotal += client.getRequirement(); } // 这条路径上的用户需求之和不能大于卡车的运输能力 if (requirementTotal > vehicle.getMaxLoad()) { // System.out.println("requirement > maxLoad"); return false; } if (load > vehicle.getMaxLoad() || load < 0) { // System.out.println("overload"); return false; } if (distance > vehicle.getMaxDistance()) { // System.out.println("over distance"); return false; } return true; }