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;
  }
  public boolean isDone(List<Vehicle> vehicles) {
    Set<Client> clientSet = new TreeSet<Client>();
    for (Vehicle vehicle : vehicles) {
      if (null == vehicle.getClients()) {
        return false;
      }

      for (Client client : vehicle.getClients()) {
        clientSet.add(client);
      }
    }
    if (clientSet.size() == clients.length) {
      return true;
    }
    return false;
  }
  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 List<Client> getRemainingClient(List<Vehicle> vehicles) {
   Set<Client> allUsed = new TreeSet<Client>();
   List<Client> remainingClient = new LinkedList<Client>();
   for (Vehicle vehicle : vehicles) {
     for (Client client : vehicle.getClients()) {
       allUsed.add(client);
     }
   }
   for (int i = 0; i < clients.length; i++) {
     if (!allUsed.contains(clients[i])) {
       remainingClient.add(clients[i]);
     }
   }
   return remainingClient;
 }
  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;
  }