Esempio n. 1
0
  @ScheduledMethod(start = 1, interval = 100)
  public void step() {

    // generating some numerical output
    sanctionsPerSoldier = 1.0 * sanctionCount / soldiers.size();
    proportionOfNotPermittedExtiorsions = 1.0 * notPermittedExtorsionCount / extorsionCount;

    periodicIncomePerSoldier = cummulatedIncome / soldiers.size();
    cummulatedIncome = 0;
    extorsionCount = 0;
    sanctionCount = 0;
    notPermittedExtorsionCount = 0;
    punishmentCount = punishedNumber;
    punishedNumber = 0;

    // learning --> preliminary!
    if (period > 0) {
      if (periodicIncomePerSoldier < incomeLowThreshold) {
        sanctionProbability *= probMultiplicatorIncomeLow;
      } else if (periodicIncomePerSoldier > incomeHighThreshold) {
        sanctionProbability *= probMultiplicatorIncomeHigh;
      }
      sanctionProbability = sanctionProbability > 1.0 ? 1.0 : sanctionProbability;
    }
    period++;

    // delete network
    Context<Object> context = ContextUtils.getContext(this);
    Network<Object> net = (Network<Object>) context.getProjection("network");
    net.removeEdges();
  }
 public double getClusteringCoefficientOfUser() {
   if (this.hashCode() % NonAggrSamplingQueficient
       == 9) { // only calculate clustering coefficient for 1/20 of the agents
     Context<Object> context = ContextUtils.getContext(this);
     ContextJungNetwork jungNet = (ContextJungNetwork) context.getProjection("user_network");
     Map<Object, Double> clusterCoeffMap =
         repast.simphony.jung.statistics.RepastJungGraphStatistics.clusteringCoefficients(
             jungNet.getGraph());
     return clusterCoeffMap.get(this);
   } else {
     return exclusionNumber;
   }
 }
  /** @param element */
  private void remove(TrafficElement element) {
    context.remove(element);

    if (element instanceof Car) {
      removeCar((Car) element);
    }
  }
 public double getUserNeighbourAverageEdgeCount() {
   int tempInt = 0;
   double sumOfNeigbourDegrees = 0.0;
   Context<Object> context = ContextUtils.getContext(this);
   Network<Object> colNet = (Network<Object>) context.getProjection("user_network");
   for (Object neighbourObject : colNet.getSuccessors(this)) {
     if (neighbourObject instanceof User) {
       sumOfNeigbourDegrees += colNet.getDegree(neighbourObject);
       tempInt++;
     }
   }
   double averageNeighbourDegree = 0.0;
   if (tempInt != 0) {
     averageNeighbourDegree = sumOfNeigbourDegrees / tempInt;
   }
   return averageNeighbourDegree;
 }
Esempio n. 5
0
  public void giveExtorsionMoney(double amount, Soldiers soldier, Shop shop, Color territory) {
    // store money

    // distribute money
    // 50 persent for capos, then rest for solidiers

    System.out.println(
        "capo #"
            + this.getIndex()
            + "cummulatedIncome = "
            + cummulatedIncome
            + "extorsionCount = "
            + extorsionCount);

    extorsionCount++;

    cummulatedIncome += amount * 0.5;
    soldier.setMoney(soldier.getMoney() + amount * 0.5);
    System.out.println("capo #" + index + " has now: $" + cummulatedIncome);

    // draw edges between Capo and by own soldiers extorted shops
    Context<Object> context = ContextUtils.getContext(this);
    Network<Object> net = (Network<Object>) context.getProjection("network");
    net.addEdge(this, shop);

    // sanction if money from shop from other territory
    if (!territory.equals(familyColor)) {
      // decision process --> preliminary! --- TODO: could be implemented
      // with EMIL-S

      notPermittedExtorsionCount++;

      double rnd = RandomHelper.nextDoubleFromTo(0.0, 1.0);
      if (rnd <= sanctionProbability) {
        soldier.sanction(shop, territory);
        sanctionCount++;
      }
    }

    // notify the mandamento
    if (this != mandamento.getCapo()) {
      mandamento.onExtortMoney(index, cummulatedIncome);
    }
  }
 public double getPathlengthOfUser() {
   if (this.hashCode() % NonAggrSamplingQueficient
       == 9) { // only calculate clustering coefficient for 1/20 of the agents
     List<User> userList = new ArrayList<User>();
     Context<Object> context = ContextUtils.getContext(this);
     Network<Object> colNet = (Network<Object>) context.getProjection("user_network");
     ContextJungNetwork jungNet = (ContextJungNetwork) context.getProjection("user_network");
     for (Object tempNode : colNet.getNodes()) {
       if (tempNode instanceof User) {
         userList.add((User) tempNode);
       }
     }
     dijkDistAlg = new DijkstraDistance<User, RepastEdge<Object>>(jungNet.getGraph());
     Number dist = dijkDistAlg.getDistance(this, userList.get(randGen.nextInt(userList.size())));
     if (dist != null) {
       return dist.doubleValue();
     } else {
       return exclusionNumber;
     }
   } else {
     return exclusionNumber;
   }
 }
Esempio n. 7
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);
    }
  }
Esempio n. 8
0
  public GridPoint getQueueLocation(String name, Grid grid) {
    GridPoint queueLoc = null;
    QueueSim queueR = null;
    context = ContextUtils.getContext(this);

    for (Object o : context.getObjects(QueueSim.class)) {
      queueR = (QueueSim) o;
      if (queueR.getName() == name) {
        queueLoc = grid.getLocation(o);
        // System.out.println("**** "+ queueR.getId()+ " "
        // + queueLoc);
        break;
      }
    }
    return queueLoc;
  }
  /** Executes step method for each car and adds them to their new position */
  private void moveCars() {
    this.carsToRemove.clear();
    this.positionsToCheck.clear();

    // Cars compute their new position
    for (Car car : travelingCars) car.move();

    // Move cars in the map
    for (Car car : travelingCars) {
      int x = car.getX();
      int y = car.getY();

      // Move car
      if (!this.isPositionOutOfBounds(x, y)) {
        map.moveTo(car, x, y);
        this.addPositionToCheck(car.getPosition().getGridPoint());
      }
      // Car moved out of the map. Add for removal
      else {
        this.addCarToRemove(car);
      }
    }

    // Remove cars out of bounds
    for (Car car : this.carsToRemove) {
      remove(car);
    }

    // Manage collisions
    for (String posId : this.positionsToCheck.keySet()) {
      GridPoint pos = this.positionsToCheck.get(posId);
      int x = pos.getX();
      int y = pos.getY();

      if (this.getNumElements(x, y) > 1) {
        Collision col = new Collision(x, y, map);
        Iterable<TrafficElement> elements = map.getObjectsAt(x, y);
        remove(elements);

        context.add(col);
        map.moveTo(col, x, y);

        this.collisions.add(col);
      }
    }
  }
Esempio n. 10
0
  protected Resource findResourceAvailable(String resourceType) {
    Resource rAvailable = null;
    context = getContext();
    for (Object o : context.getObjects(Resource.class)) {
      Resource resource = (Resource) o;
      if (resource.getResourceType() == resourceType) {
        System.out.println(
            "resource type? "
                + resourceType
                + " is required here, looking if "
                + resource.getId()
                + " is available? "
                + resource.isAvailable());
        if (resource.isAvailable() == true) {
          rAvailable = resource;
          break;
        } else {

        }
      }
    }
    return rAvailable;
  }
  @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();
    }
  }
 public int getUserEdgeCount() {
   Context<Object> context = ContextUtils.getContext(this);
   Network<Object> colNet = (Network<Object>) context.getProjection("user_network");
   return colNet.getDegree(this);
 }
 public int getAffiliationEdgeCountOfUser() {
   Context<Object> context = ContextUtils.getContext(this);
   Network<Object> colNet = (Network<Object>) context.getProjection("collaboration_network");
   return colNet.getDegree(this);
 }
 /** @param col */
 private void removeCollision(Collision col) {
   context.remove(col);
   this.collisions.remove(col);
 }
 /**
  * Adds a car to the simulation
  *
  * @param car
  */
 private void add(Car car) {
   travelingCars.add(car);
   context.add(car);
   map.moveTo(car, car.getX(), car.getY());
 }