/** Builds the set of paths between all the couple of vertexes */
  private void buildPaths() {

    logger.debug("IN");

    allGraphPaths = new HashSet<GraphPath<IModelEntity, Relationship>>();
    paths = new HashSet<EntitiesPath>();

    // build all the path between entities
    Iterator<IModelEntity> vertexesIter = entities.iterator();

    // First loop for the source of the path
    while (vertexesIter.hasNext()) {
      IModelEntity startVertex = vertexesIter.next();
      logger.debug("Building the list of path starting from " + startVertex.getName());
      // inner loop for the target of the path
      Iterator<IModelEntity> vertexesInnerIter = entities.iterator();
      while (vertexesInnerIter.hasNext()) {
        IModelEntity endVertex = vertexesInnerIter.next();
        if (!startVertex.equals(endVertex)) {
          EntitiesPath entitiesPath = new EntitiesPath(startVertex, endVertex);
          if (!this.paths.contains(entitiesPath)) {
            logger.debug(
                "Building the list of path between the vertexes ["
                    + startVertex.getName()
                    + ","
                    + endVertex.getName()
                    + "]");
            KShortestPaths<IModelEntity, Relationship> kshortestPath =
                new KShortestPaths<IModelEntity, Relationship>(graph, startVertex, maxPathLength);
            List<GraphPath<IModelEntity, Relationship>> graphPaths =
                kshortestPath.getPaths(endVertex);

            // if there is at least one path between the 2 vertex
            if (graphPaths != null) {
              entitiesPath.setPaths(graphPaths);

              // updating the class variables
              this.paths.add(entitiesPath);
              for (int i = 0; i < graphPaths.size(); i++) {
                GraphPath<IModelEntity, Relationship> path = graphPaths.get(i);
                if (!containsPath(this.allGraphPaths, path)) {
                  this.allGraphPaths.add(path);
                }
              }
            }
          }
        }
      }
    }
    logger.debug("OUT");
  }
Esempio n. 2
0
  private static String GetDiameter(DefaultDirectedWeightedGraph<Vertex, DefaultWeightedEdge> g) {
    String retDiaReport = "";

    //		HashMap<Vertex,KShortestPaths<Vertex,DefaultWeightedEdge>> kShortestPathMap =
    //			new HashMap<Vertex,KShortestPaths<Vertex,DefaultWeightedEdge>>();

    boolean isConnected = true;
    int diameter = -1;

    int totalProcessed = 0;

    Iterator<Vertex> vIter = g.vertexSet().iterator();
    while (vIter.hasNext()) {
      Vertex v = vIter.next();
      if (!v.isSeedNode()) {
        continue;
      }

      ++totalProcessed;
      if (totalProcessed % 1000 == 0) {
        logger.info("Processed: " + totalProcessed + " curr_dia: " + diameter);
      }

      KShortestPaths<Vertex, DefaultWeightedEdge> ksp = new KShortestPaths(g, v, 1);
      // kShortestPathMap.put(v, new KShortestPaths(g, v, _kPrime));

      Iterator<Vertex> vIter2 = g.vertexSet().iterator();
      while (vIter2.hasNext()) {
        Vertex nv = vIter2.next();

        // skip self comparison
        if (v.equals(nv)) {
          continue;
        }

        List<GraphPath<Vertex, DefaultWeightedEdge>> paths = ksp.getPaths(nv);

        if (paths == null) {
          isConnected = false;
        } else if (paths.get(0).getEdgeList().size() > diameter) {
          diameter = paths.get(0).getEdgeList().size();
        }
      }
    }

    retDiaReport += "Connected(from_seed_nodes): " + (isConnected ? "true" : "false") + "\n";
    retDiaReport += "Diameter(from_seed_nodes): " + diameter + "\n";

    return (retDiaReport);
  }
Esempio n. 3
0
  private EscapePath determineEscapePath(Collection<Integer> goalAreaIds) {
    // get the knowledge object and the current area id, we'll need it further down
    CompleteKnowledgeInverted knowledge =
        (CompleteKnowledgeInverted) agent.getEnvironmentKnowledge();
    int currentAreaId = agent.getCurrentAreaId();
    //        System.out.println("deterimining an exit path");
    //        int firstLink = currentAreaId;
    GraphPath<Integer, RoomEdge> finalPath = null;
    double minWeight = Double.MAX_VALUE;
    //        GraphPath<Integer, RoomEdge> tempPath;
    //        int tempRoom;
    int goalArea = -1;

    for (Integer goalAreaId : goalAreaIds) {
      // is current area a room?
      //            System.out.println("here" + goalAreaId + "from" + currentAreaId);
      if (knowledge.resolveAreaById(currentAreaId) == null) {
        // currently on a getLink
        //
        //                System.out.println("getLink");
        KShortestPaths<Integer, RoomEdge> ksp =
            new KShortestPaths<Integer, RoomEdge>(knowledge.getGraph(), currentAreaId, 1);
        //                KShortestPaths<Integer, RoomEdge> ksp1 = new
        // KShortestPaths<>(knowledge.getGraph(), roomId1, 1);
        TreeMap<Double, GraphPath<Integer, RoomEdge>> paths =
            new TreeMap<Double, GraphPath<Integer, RoomEdge>>();
        for (CLink connectingLink : knowledge.getConnectingLinks(goalAreaId)) {
          for (GraphPath<Integer, RoomEdge> path : ksp.getPaths(connectingLink.getId())) {
            paths.put(path.getWeight(), path);
          }
        }
        //                List<GraphPath<Integer, RoomEdge>> paths1 = ksp1.getPaths(goalAreaId);

        // there should be at least one path to the exit from each room
        assert (paths != null && !paths.isEmpty());
        GraphPath<Integer, RoomEdge> route = paths.firstEntry().getValue();

        if (minWeight >= route.getWeight()) {
          finalPath = route;
          minWeight = route.getWeight();
          goalArea = goalAreaId;
        }

      } // if not, then it must be a area
      else {
        //                System.out.println("area");
        for (CLink homeLink : knowledge.getConnectingLinks(currentAreaId)) {
          // System.out.println(knowledge.getGraph().edgeSet());
          KShortestPaths<Integer, RoomEdge> ksp =
              new KShortestPaths<Integer, RoomEdge>(knowledge.getGraph(), homeLink.getId(), 1);
          //                    System.out.println("home getLink " + homeLink.getId());
          TreeMap<Double, GraphPath<Integer, RoomEdge>> paths =
              new TreeMap<Double, GraphPath<Integer, RoomEdge>>();
          for (CLink goalLink : knowledge.getConnectingLinks(goalAreaId)) {
            //                        System.out.println("goal getLink " + goalLink.getId());
            if (goalLink.getId() == homeLink.getId()) {
              return new EscapePathImpl(goalLink, goalAreaId);
            }
            if (ksp.getPaths(goalLink.getId()) == null) {
              //                            System.out.println("no path to exit");
              continue;
            }
            for (GraphPath<Integer, RoomEdge> path : ksp.getPaths(goalLink.getId())) {

              paths.put(path.getWeight(), path);
            }
          }
          //                List<GraphPath<Integer, RoomEdge>> paths1 = ksp1.getPaths(goalAreaId);

          // there should be at least one path to the exit from each room
          assert (paths != null && !paths.isEmpty());
          GraphPath<Integer, RoomEdge> route = paths.firstEntry().getValue();

          if (minWeight >= route.getWeight()) {
            finalPath = route;
            minWeight = route.getWeight();
            //                        firstLink = homeConnectingLink;
            goalArea = goalAreaId;
          }
        }

        // there is at least one path to the exit

      }
    }
    //        if (finalPath == null) {
    //            System.out.println(((IbevacAgent) this.agent).getId() + "," +
    // goalAreaIds.iterator().next());
    //        }
    //        assert (finalPath != null);
    //        //TODO : change this stupid hack;
    //        agent.setCurrentGoal(new IbevacLogicalWaypoint(null,
    // knowledge.resolveAreaById(goalArea), null, space, agent).getWP1().getPoint());
    return new EscapePathImpl(finalPath, goalArea);
  }