Exemplo n.º 1
1
  public void moveToShape(Collection<ShapeInArea> des, Class<? extends MoveType> type)
      throws SOSActionException {

    if (!(me.me() instanceof Human)) {
      log().warn("can't use move in Center Agent " + me + "\n");
      return;
    }
    try {
      checkTraffic(); // TODO uncomment
      MoveType mt = moves.get(type.hashCode());
      if (mt != null) {
        Path path = mt.getPathToShape(des);
        log().debug("MOVE " + type.getSimpleName() + "\nTO : " + path + "\n");
        move(path);
      } else log().error(new Error("in move can not found type=" + type.getSimpleName()));

    } catch (SOSActionException e) {
      throw e;
    } catch (Exception er) {
      log().error(er);
      log().warn("using bfs for finding path");
      ArrayList<Area> goals = new ArrayList<Area>();

      for (ShapeInArea shapeInArea : des) {
        goals.add(shapeInArea.getArea(me.model()));
      }
      move(bfs.breadthFirstSearch((Area) me.location(), goals));
    }
    log().error("in move can not found type=" + type.getSimpleName());
    me.problemRest("in move can not found type=" + type.getSimpleName());
  }
Exemplo n.º 2
0
  @Test
  public void testBFSFromVertexEight() {

    bfs.calculate(v8);

    assertTrue(graph.getVertex(8).getDistance() == 0);
    assertTrue(graph.getVertex(5).getDistance() == 1);
    assertTrue(graph.getVertex(6).getDistance() == 1);
    assertTrue(graph.getVertex(7).getDistance() == 1);
    assertTrue(graph.getVertex(4).getDistance() == 2);
    assertTrue(graph.getVertex(3).getDistance() == 3);
    assertTrue(graph.getVertex(2).getDistance() == 4);
    assertTrue(graph.getVertex(1).getDistance() == 5);

    sPT = bfs.reconstructTraversalGraph();
    assertTrue(sPT.getRoot().equals(v8));
    assertTrue(sPT.edgeSet().size() == 8);
    assertTrue(sPT.containsEdge(v8, v7));
    assertTrue(sPT.containsEdge(v8, v5));
    assertTrue(sPT.containsEdge(v5, v4));
    assertTrue(sPT.containsEdge(v8, v6));
    assertTrue(sPT.containsEdge(v6, v4));
    assertTrue(sPT.containsEdge(v4, v3));
    assertTrue(sPT.containsEdge(v3, v2));
    assertTrue(sPT.containsEdge(v2, v1));
  }
Exemplo n.º 3
0
 public void moveXY(
     Collection<Pair<? extends Area, Point2D>> destinations, Class<? extends MoveType> type)
     throws SOSActionException {
   if (!(me.me() instanceof Human)) {
     log().warn("can't use move in Center Agent " + me + "\n");
     return;
   }
   try {
     checkTraffic();
     MoveType mt = moves.get(type.hashCode());
     if (mt != null) {
       Path path = mt.getPathToPoints(destinations);
       log().debug("MOVE XY" + type.getSimpleName() + "\nTO : " + path + "\n");
       move(path);
     } else log().error(new Error("in move can not found type=" + type.getSimpleName()));
   } catch (SOSActionException e) {
     throw e;
   } catch (Exception er) {
     log().error(er);
     log().warn("using bfs for finding path");
     move(bfs.breadthFirstSearchXY((Area) me.location(), destinations));
   }
   log().error("in move can not found type=" + type.getSimpleName());
   me.problemRest("in move can not found type=" + type.getSimpleName());
 }
Exemplo n.º 4
0
  public static void filehandler() {
    try {
      int i = 0;
      File file = new File("map1.txt"); // SPECIFY THE FILE NAME HERE
      FileInputStream fis = new FileInputStream(file);
      BufferedInputStream bis = new BufferedInputStream(fis);
      BufferedReader d = new BufferedReader(new InputStreamReader(bis));
      String line = d.readLine();
      line = line.trim();
      String temp[] = line.split(" ");
      row = Integer.parseInt(temp[0]);
      BFS.setMax_Row(row - 1); // SETTING THE MAX_ROW AS ROW-1
      col = Integer.parseInt(temp[1]);
      BFS.setMax_Col(col - 1); // SETTING THE MAX_COL AS COL-1
      line = d.readLine();
      line = line.trim();
      temp = line.split(" ");
      int startx = Integer.parseInt(temp[0]);
      BFS.setStartx(startx); // SETTING THE START POSITION OF THE SEARCH ON X AXIS
      int starty = Integer.parseInt(temp[1]);
      BFS.setStarty(starty); // SETTING THE START Y POSITION OF THE SEARCH
      int j = i = 0;
      while (i < row) // STORING THE GRAPH FROM THE FILE INTO A VARIABLE
      {
        line = d.readLine();
        char ch[] = line.toCharArray();
        for (int k = 0; k < col; k++) {
          Graph[i][k] = ch[k];
          if (ch[k] != '.') {
            Message_Obtained.add(j);
            j++;
          }
        }
        i++;
      }
      BFS.setGraph(Graph);
      BFS.setGoals(Message_Obtained);
      getpos();
      i = 0;
      while (i < row) // Reading the cost map from the file and saving it
      {
        line = d.readLine();
        line = line.trim();
        temp = line.split(" +");
        for (j = 0; j < col; j++) {
          cost_map[i][j] = Double.parseDouble(temp[j]);
        }
        i++;
      }
      BFS.setMap_cost(cost_map);
    } catch (IOException e) {

    }
  }
Exemplo n.º 5
0
  /** Rigourous Test :-) */
  public void testBFS() {

    DefaultDirectedGraph<Vertex, Double> graph =
        new DefaultDirectedGraph<Vertex, Double>(Double.class);
    Vertex v = null;
    for (int i = 0; i < this.numOfVertices; i++) {
      v = new Vertex("v" + i);
      graph.addVertex(v);
    }

    // use last vertex as start point of search
    Vertex lastVertex = v;

    Set<Vertex> vertices = graph.vertexSet();

    // add edges to graph
    Random rand = new Random();
    Double nextEdge = 0.0;
    for (Vertex start : vertices)
      for (Vertex end : vertices)
        if (!start.equals(end) && !graph.containsEdge(start, end)) {
          nextEdge = rand.nextDouble();
          if (nextEdge > 0.9) graph.addEdge(start, end, nextEdge);
        }

    System.out.println("Vertices: " + vertices.size());
    System.out.println("Edges: " + graph.edgeSet().size());

    BreadthFirstIterator<Vertex, Double> bfsIter =
        new BreadthFirstIterator<Vertex, Double>(graph, lastVertex);

    BFS<Double> bfs = new BFS<Double>(graph);
    Vertex[] bfsResult = bfs.breadthFirstTree(lastVertex);

    for (int i = 0; i < this.numOfVertices && bfsIter.hasNext(); i++) {
      System.out.println(bfsResult[i] + " " + bfsIter.next());
    }
  }
Exemplo n.º 6
0
 private static void getpos() { // Gets the Message position and stores it in a 2D array
   int k = 0;
   for (int i = 0; i < row; i++) {
     for (int j = 0; j < col; j++) {
       int change_row = 0;
       if (Graph[i][j] != '.') {
         Goal_pos[k][change_row] = i;
         Goal_pos[k][change_row + 1] = j;
         k++;
       }
     }
   }
   BFS.setGoal_pos(Goal_pos);
 }
Exemplo n.º 7
0
  private void checkAlakiStock(Path path) throws SOSActionException {
    log().info("checking alaki stock");
    CycleInformations cycleinfo1 = me.informationStacker.getInformations(1);
    CycleInformations cycleinfo2 = me.informationStacker.getInformations(2);
    CycleInformations cycleinfo3 = me.informationStacker.getInformations(3);
    // Move Stock Move Stock Stock Stock
    log().trace("cycleinfo1:" + cycleinfo1);
    log().trace("cycleinfo2:" + cycleinfo2);
    log().trace("cycleinfo3:" + cycleinfo3);
    if (!(cycleinfo1.getAct() instanceof MoveAction)
        || cycleinfo1.getAct() instanceof StockMoveAction) return;
    if (
    /* !(cycleinfo2.getAct() instanceof MoveAction) || */ !(cycleinfo2.getAct()
        instanceof StockMoveAction)) return;
    if (!(cycleinfo3.getAct() instanceof MoveAction)
        || cycleinfo3.getAct() instanceof StockMoveAction) return;

    log()
        .debug(
            "cycleinfo1:"
                + cycleinfo1.getPositionPair()
                + "cycleinfo2:"
                + cycleinfo2.getPositionPair()
                + " cycleinfo3:"
                + cycleinfo3.getPositionPair());
    if (SOSGeometryTools.getDistance(
                cycleinfo1.getPositionPair().second(), cycleinfo2.getPositionPair().second())
            < MoveConstants.TRAFFIC_CHECKING_DISTANCE
        && SOSGeometryTools.getDistance(
                cycleinfo1.getPositionPair().second(), cycleinfo3.getPositionPair().second())
            < MoveConstants.TRAFFIC_CHECKING_DISTANCE) {
      Path randomPath = bfs.getDummyRandomWalkPath();

      me.send(
          new AKMove(
              me.getID(),
              me.time(),
              randomPath.getIds(),
              randomPath.getDestination().second().getIntX(),
              randomPath.getDestination().second().getIntY()));
      sendStockMessage(path);
      me.informationStacker.addInfo(me.model(), new StockMoveAction(randomPath));
      throw new SOSActionException("Move Stock Random Walk(" + randomPath + ")");
    }
  }
Exemplo n.º 8
0
  @Test
  public void testTraverse() {
    String[][] data = {{"A", "B"}, {"B", "C"}, {"A", "C"}, {"B", "D"}};
    Graph<String, DirectedEdge<String>> g = TestGraphFactory.createDirectedNew(data);
    final DynamicArray<String> log = DynamicArray.create();
    BFS.traverse(
        g,
        VarargsIterable.create("A"),
        new BFSVisitor<String, DirectedEdge<String>>() {
          @Override
          public void onDiscover(String vertex, int depth, VisitorStopper stopper) {
            log.addToLast(vertex + "(" + depth + ")");
          }

          @Override
          public void onWalk(DirectedEdge<String> e) {
            log.addToLast(e.toString());
          }
        });
    assertEquals("(A(0),A->B,B(1),A->C,C(1),B->D,D(2))", log.toString());
  }
Exemplo n.º 9
0
 @SuppressWarnings("unused")
 private void check4CycleStock() throws SOSActionException {
   CycleInformations cycleinfo1 = me.informationStacker.getInformations(1);
   CycleInformations cycleinfo2 = me.informationStacker.getInformations(2);
   CycleInformations cycleinfo3 = me.informationStacker.getInformations(3);
   CycleInformations cycleinfo4 = me.informationStacker.getInformations(4);
   if (cycleinfo2.getAct() instanceof StockMoveAction) return;
   if (cycleinfo1.getAct() instanceof MoveAction
       && cycleinfo2.getAct() instanceof MoveAction
       && cycleinfo3.getAct() instanceof MoveAction
       && cycleinfo4.getAct() instanceof MoveAction) {
     //			Path path1 = ((MoveAction) cycleinfo1.getAct()).getPath();
     //			Path path2 = ((MoveAction) cycleinfo2.getAct()).getPath();
     //			Path path3 = ((MoveAction) cycleinfo3.getAct()).getPath();
     //			Path path4 = ((MoveAction) cycleinfo4.getAct()).getPath();
     log().debug("Four(4) cycle move ....");
     log().debug("1 cycle ago position:" + cycleinfo1.getPositionPair());
     log().debug("2 cycle ago position:" + cycleinfo2.getPositionPair());
     log().debug("3 cycle ago position:" + cycleinfo3.getPositionPair());
     log().debug("4 cycle ago position:" + cycleinfo4.getPositionPair());
     if (cycleinfo1.getPositionPair().first().equals(cycleinfo2.getPositionPair().first())
         && cycleinfo1.getPositionPair().first().equals(cycleinfo3.getPositionPair().first())
         && cycleinfo1.getPositionPair().first().equals(cycleinfo4.getPositionPair().first())) {
       if (SOSGeometryTools.getDistance(
                   cycleinfo1.getPositionPair().second(), cycleinfo2.getPositionPair().second())
               < MoveConstants.TRAFFIC_CHECKING_DISTANCE
           && SOSGeometryTools.getDistance(
                   cycleinfo1.getPositionPair().second(), cycleinfo3.getPositionPair().second())
               < MoveConstants.TRAFFIC_CHECKING_DISTANCE
           && SOSGeometryTools.getDistance(
                   cycleinfo1.getPositionPair().second(), cycleinfo4.getPositionPair().second())
               < MoveConstants.TRAFFIC_CHECKING_DISTANCE) {
         //////////////////// DO TRAFFIC/////////////////////////////
         log().warn("4 cycle stock!--> do random walk");
         move(bfs.getDummyRandomWalkPath(), false);
       }
     }
   }
 }
Exemplo n.º 10
0
 public Cut minCut(Graph g, EdgeFunction cap, int s, int t) {
   maxFlow(g, cap, s, t);
   BFS.bfsInit(gr);
   BFS.bfsVisit(gr, s);
   return new Cut(BFS.visited, flowValue);
 }