Beispiel #1
1
  public PathResult dijkstra() {
    PathResult p = new PathResult();
    Map dMap = map.newInstance();
    HashSet<Node> evaluated = new HashSet<Node>();
    HashSet<Node> unevaluated = new HashSet<Node>();
    HashMap<String, Double> dist = new HashMap<String, Double>();
    HashMap<String, String> previous = new HashMap<String, String>();
    for (int i = 0; i < dMap.getSize(); i++) {
      Node toAdd = dMap.getNode(i);
      unevaluated.add(toAdd);
      dist.put(toAdd.getName(), Double.MAX_VALUE);
      previous.put(toAdd.getName(), null);
    }
    dist.put("START", 0D);

    Node curNode = dMap.getStartNode(), temp = null;
    while (!unevaluated.isEmpty()) {
      Iterator<Node> it = unevaluated.iterator();
      curNode = it.next();
      while (it.hasNext()) {
        temp = it.next();
        if (dist.get(temp.getName()) < dist.get(curNode.getName())) curNode = temp;
      }
      evaluated.add(curNode);
      unevaluated.remove(curNode);
      if (dist.get(curNode.getName()) == Double.MAX_VALUE || dMap.getEndNode().equals(curNode))
        break;

      for (int i = 0; i < curNode.getNumDepend(); i++) {
        Node depend = curNode.getDepend(i);
        if (evaluated.contains(depend)) continue;
        double tempWeight =
            dist.get(curNode.getName())
                + dMap.getPath(curNode.getName(), depend.getName()).getWeight();
        if (tempWeight < dist.get(depend.getName())) {
          dist.put(depend.getName(), tempWeight);
          previous.put(depend.getName(), curNode.getName());
        }
      }
    }
    p.setWeight(dist.get(curNode.getName()));

    String name = curNode.getName();
    while (previous.get(curNode.getName()) != null) {
      curNode = dMap.getFromName(previous.get(curNode.getName()));
      name = curNode.getName() + "," + name;
    }
    p.setPath(name);
    return p;
  }
Beispiel #2
0
  public ArrayList<Node> findBestPackageSet(Map map, int maxWeight) {
    long binaryCounter = 0b0;
    long bitMask = getBitMask(map.size());
    ArrayList<Node> best = new ArrayList<>();
    while ((binaryCounter & bitMask) != bitMask) {

      if (System.currentTimeMillis() - startTime > timeout) {
        stop = true;
        return null;
      }

      ArrayList<Node> current = new ArrayList<>();
      binaryCounter++;
      long temp = binaryCounter;
      for (int i = 0; i < map.size(); i++) {
        if ((temp & 0b1) == 0b1) {
          current.add(map.getNode(i));
        }
        temp = temp >>> 1;
      }
      if (map.getValue(current) > map.getValue(best) && map.getWeight(current) < maxWeight) {
        best = (ArrayList<Node>) current.clone();
      }
    }
    return best;
  }