Esempio n. 1
0
  /**
   * Returns a random element of the specified arbitrary list.
   *
   * @param list the list to select a random element from; <i>cannot be <code>null</code> or
   *     empty</i>
   * @return a random element of the specified list using the default random generator of {@link
   *     RandomHelper}
   */
  public static <T> T randomElementOf(final List<T> list) {
    if (null == list) {
      throw new IllegalArgumentException("Parameter list cannot be null.");
    }

    if (list.isEmpty()) {
      throw new IllegalArgumentException("Cannot return a random element from an empty list.");
    }

    return list.get(RandomHelper.nextIntFromTo(0, list.size() - 1));
  }
Esempio n. 2
0
  public void infect() {
    GridPoint pt = grid.getLocation(this);
    List<Object> humans = new ArrayList<Object>();
    for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
      if (obj instanceof Human) {
        humans.add(obj);
      }
    }

    if (humans.size() > 0) {
      int index = RandomHelper.nextIntFromTo(0, humans.size() - 1);
      Object obj = humans.get(index);
      NdPoint spacePt = space.getLocation(obj);
      Context<Object> context = ContextUtils.getContext(obj);
      context.remove(obj);
      Zombie zombie = new Zombie(space, grid);
      context.add(zombie);
      space.moveTo(zombie, spacePt.getX(), spacePt.getY());
      grid.moveTo(zombie, pt.getX(), pt.getY());

      Network<Object> net = (Network<Object>) context.getProjection("infection network");
      net.addEdge(this, zombie);
    }
  }
  @ScheduledMethod(start = 1, interval = 1)
  public void step() {

    // create colNetwork in hosting context
    Context<Object> context = ContextUtils.getContext(this);
    Network<Object> colNet = (Network<Object>) context.getProjection("collaboration_network");
    Network<Object> userNet = (Network<Object>) context.getProjection("user_network");
    Network<Object> articleNet = (Network<Object>) context.getProjection("article_network");

    if (!isDone) {
      /*
       * Neighbourhood Connection Algorithm
       */
      // get the grid location of this User
      GridPoint pt = grid.getLocation(this);

      // use the GridCellNgh class to create GridCells for
      // the surrounding neighbourhood
      if (pt != null) { // TODO Why NULL?
        GridCellNgh<Article> nghCreator =
            new GridCellNgh<Article>(
                grid, pt, Article.class, neighbourDimensions, neighbourDimensions);
        List<GridCell<Article>> gridCells = nghCreator.getNeighborhood(false);
        SimUtilities.shuffle(gridCells, RandomHelper.getUniform());

        // if an agent exist in the surrounding environment, add an edge with it.
        for (GridCell<Article> cell : gridCells) {
          if (cell.size() > 0) {
            List<Article> cellUsers = new ArrayList<Article>((Collection<Article>) cell.items());
            articleToEdit = cellUsers.get((RandomHelper.nextIntFromTo(0, cellUsers.size() - 1)));
            if (context != null && colNet != null && cellUsers != null && articleToEdit != null) {
              if (!isActiveUser) { // Good Samaritan - one and only one connection
                if (colNet.getDegree(articleToEdit) <= 0 // if neighbour is unconnected
                    && colNet.getDegree(this) <= 0) { // if our agent is unconnected)
                  colNet.addEdge(this, articleToEdit);
                  this.isDone =
                      true; // this good samaritan is no longer counted in operating agents
                }
              } else if (!hasGeneralInterest) { // Project Leader zealot (active user),
                colNet.addEdge(this, articleToEdit); // connects neighbours in every step

                for (Object coopUser : colNet.getAdjacent(articleToEdit)) {
                  if (coopUser != null && !userNet.containsEdge(userNet.getEdge(this, coopUser))) {
                    userNet.addEdge(this, coopUser);
                  }
                }
                for (Object relatedArticle : colNet.getAdjacent(this)) {
                  if (relatedArticle != null
                      && !articleNet.containsEdge(userNet.getEdge(articleToEdit, relatedArticle))) {
                    articleNet.addEdge(articleToEdit, relatedArticle);
                  }
                }
              }

              // For active agent connection algorithm we need to update good article array if found
              if (colNet.getDegree(articleToEdit)
                      > (goodArticleMultiplier * colNet.getDegree() / colNet.size())
                  && colNet.getDegree(articleToEdit) > goodArticleConnectionCount
                  && !articleToEdit.isGood) {
                articleToEdit.isGood = true;
                goodArticles.add(articleToEdit);
              }
            }
            break;
          }
        }
      }

      /*
       * Active Agent Connection Algorithm
       */
      if (isActiveUser
          && hasGeneralInterest
          && goodArticles.size() > 0) { // if in administrator career path
        articleToEdit = goodArticles.get(RandomHelper.nextIntFromTo(0, goodArticles.size() - 1));
        colNet.addEdge(this, articleToEdit); // TODO reduce goodArticles by one?

        for (Object coopUser : colNet.getAdjacent(articleToEdit)) {
          if (coopUser != null && !userNet.containsEdge(userNet.getEdge(this, coopUser))) {
            userNet.addEdge(this, coopUser);
          }
        }
        for (Object relatedArticle : colNet.getAdjacent(this)) {
          if (relatedArticle != null
              && !articleNet.containsEdge(userNet.getEdge(articleToEdit, relatedArticle))) {
            articleNet.addEdge(articleToEdit, relatedArticle);
          }
        }
        goodArticles.remove(0);
      }

      this.endRun();
    }
  }