Package org.nlogo.agent

Examples of org.nlogo.agent.AgentSet$Iterator


          I18N.errorsJ().getN("org.nlogo.$common.thatAgentIsDead", agent.classDisplayName()));
      }
      args[0].checkAgentClass(agent, context);
      return new Context(context, agent).evaluateReporter(agent, args[0]);
    } else if (agentOrSet instanceof AgentSet) {
      AgentSet sourceSet = (AgentSet) agentOrSet;
      LogoListBuilder result = new LogoListBuilder();
      Context freshContext = new Context(context, sourceSet);
      args[0].checkAgentSetClass(sourceSet, context);
      for (AgentSet.Iterator iter = sourceSet.shufflerator(context.job.random);
           iter.hasNext();) {
        result.add(freshContext.evaluateReporter(iter.next(), args[0]));
      }
      return result.toLogoList();
    } else {
View Full Code Here


    }
    Turtle turtle = world.getTurtle(id);
    if (turtle == null) {
      return Nobody$.MODULE$;
    }
    AgentSet breed = world.getBreed(breedName);
    if (!breed.contains(turtle)) {
      throw new EngineException
          (context, this,
              turtle + " is not a " + world.getBreedSingular(breed));
    }
    return turtle;
View Full Code Here

    return super.toString() + ":" + breedName;
  }

  @Override
  public Object report(final Context context) throws LogoException {
    AgentSet breed = breedName == null ? world.links() : world.getLinkBreed(breedName);
    mustNotBeUndirected(breed, context);
    return world.linkManager.findLinksFrom
        ((Turtle) context.agent, breed);
  }
View Full Code Here

  }

  @Override
  public void perform(final Context context)
      throws LogoException {
    AgentSet nodeset = argEvalAgentSet(context, 0, Turtle.class);
    AgentSet linkset = argEvalAgentSet(context, 1, Link.class);
    Turtle root = argEvalTurtle(context, 2);
    try {
      org.nlogo.agent.Layouts.radial(world, nodeset, linkset, root);
    } catch (org.nlogo.api.AgentException e) {
      throw new EngineException(context, this, e.getMessage());
View Full Code Here

public final strictfp class _atpoints
    extends Reporter {
  @Override
  public Object report(final Context context) throws LogoException {
    // part 1: get arguments, context.checked validity
    AgentSet sourceSet = argEvalAgentSet(context, 0);
    List<Agent> result = new ArrayList<Agent>();
    LogoList points = argEvalList(context, 1);
    for (Iterator<Object> it = points.iterator(); it.hasNext();) {
      if (!validateListEntry(it.next())) {
        throw new EngineException(context, this, I18N.errorsJ().getN(
            "org.nlogo.prim.etc._atpoints.invalidListOfPoints", Dump.logoObject(points)));
      }
    }

    // part 2: figure out which patches are at the given points
    Set<Patch> patches =
        getPatchesAtPoints(context, context.agent, points);

    // part 3: construct a new agentset and return it
    if (sourceSet.type() == Patch.class) {
      if (sourceSet != world.patches()) {   //sourceSet is not the entire set of patches
        for (Iterator<Patch> iter = patches.iterator(); iter.hasNext();) {
          Patch patch = iter.next();
          if (sourceSet.contains(patch)) {
            result.add(patch);
          }
        }
      } else //sourceSet is the entire set of patches
        result.addAll(patches);
      }
    } else if (sourceSet.type() == Turtle.class) {
      if (sourceSet != world.turtles()) {  //sourceSet is not the entire set of turtles
        if (world.isBreed(sourceSet)) {  //source set is a breed
          for (Iterator<Patch> iter = patches.iterator(); iter.hasNext();) {
            Patch otherPatch = iter.next();
            for (Turtle tempTurtle : otherPatch.turtlesHere()) {
              if (sourceSet == tempTurtle.getBreed()) {
                result.add(tempTurtle);
              }
            }
          }
        } else //sourceSet not the entire set of turtles and is not a breed
          for (Iterator<Patch> iter = patches.iterator(); iter.hasNext();) {
            Patch otherPatch = iter.next();
            for (Turtle tempTurtle : otherPatch.turtlesHere()) {
              if (sourceSet.contains(tempTurtle)) {
                result.add(tempTurtle);
              }
            }
          }
        }
      } else {   //sourceSet is the entire set of turtles
        for (Patch p : patches) {
          for (Turtle t : p.turtlesHere()) {
            result.add(t);
          }
        }
      }

    }
    return new ArrayAgentSet
        (sourceSet.type(), result.toArray(new Agent[result.size()]),
            world);
  }
View Full Code Here

    int n = argEvalIntValue(context, 0);
    if (n < 0) {
      throw new EngineException(context, this,
          I18N.errorsJ().getN("org.nlogo.prim.etc.$common.firstInputCantBeNegative", displayName()));
    }
    AgentSet sourceSet = argEvalAgentSet(context, 1);
    int count = sourceSet.count();
    if (n > count) {
      throw new EngineException(context, this,
          I18N.errorsJ().getN("org.nlogo.prim.etc.$common.notThatManyAgentsExist", n, count));
    }
    args[2].checkAgentSetClass(sourceSet, context);
    TreeMap<Object, LinkedList<Agent>> resultAgents =
        new TreeMap<Object, LinkedList<Agent>>();

    org.nlogo.nvm.Context freshContext =
        new org.nlogo.nvm.Context(context, sourceSet);
    for (AgentSet.Iterator iter = sourceSet.shufflerator(context.job.random);
         iter.hasNext();) {
      org.nlogo.agent.Agent tester = iter.next();
      Object result = freshContext.evaluateReporter(tester, args[2]);
      if (!(result instanceof Double)) {
        continue;
      }
      LinkedList<Agent> resultList = resultAgents.get(result);
      if (resultList == null) {
        resultList = new LinkedList<Agent>();
        resultAgents.put(result, resultList);
      }
      resultList.add(tester);
    }

    AgentSet resultSet = new org.nlogo.agent.ArrayAgentSet
        (sourceSet.type(), n, false, world);

    for (Iterator<LinkedList<Agent>> iter = resultAgents.values().iterator();
         n > 0 && iter.hasNext();) {
      LinkedList<Agent> list = iter.next();
      for (Iterator<Agent> iter2 = list.iterator(); n > 0 && iter2.hasNext();) {
        resultSet.add(iter2.next());
        n--;
      }
    }

    return resultSet;
View Full Code Here

      if (n == list.size()) {
        return list;
      }
      return randomSubset(list, n, context.job.random);
    } else if (obj instanceof AgentSet) {
      AgentSet agents = (AgentSet) obj;
      // only call count() once, since it's expensive
      // on some turtlesets - ST 11/5/03
      int count = agents.count();
      if (n > count) {
        throw new EngineException(context, this,
            I18N.errorsJ().getN("org.nlogo.prim.etc.$common.notThatManyAgentsExist", n, count));
      }
      return agents.randomSubset(n, count, context.job.random);
    } else {
      throw new ArgumentTypeException
          (context, this, 1, Syntax.ListType() | Syntax.AgentsetType(), obj);
    }
  }
View Full Code Here

        );
  }

  @Override
  public Object report(final Context context) throws LogoException {
    AgentSet sourceSet = argEvalAgentSet(context, 0);
    double winningValue = Double.MAX_VALUE;
    Context freshContext = new Context(context, sourceSet);
    List<Agent> result = new ArrayList<Agent>();
    args[1].checkAgentSetClass(sourceSet, context);
    for (AgentSet.Iterator iter = sourceSet.iterator(); iter.hasNext();) {
      Agent tester = iter.next();
      Object value = freshContext.evaluateReporter(tester, args[1]);
      if (!(value instanceof Double)) {
        continue;
      }
      double dvalue = ((Double) value).doubleValue();
      if (dvalue <= winningValue) {
        if (dvalue < winningValue) {
          winningValue = dvalue;
          result.clear();
        }
        result.add(tester);
      }
    }
    return new org.nlogo.agent.ArrayAgentSet
        (sourceSet.type(),
            result.toArray(new Agent[result.size()]),
            world);
  }
View Full Code Here

  }

  @Override
  public Object report(final Context context)
      throws LogoException {
    AgentSet sourceSet = argEvalAgentSet(context, 0);
    args[1].checkAgentSetClass(sourceSet, context);
    double winningValue = Double.MAX_VALUE;
    List<Agent> winners = new ArrayList<Agent>();
    org.nlogo.nvm.Context freshContext =
        new org.nlogo.nvm.Context(context, sourceSet);
    for (AgentSet.Iterator iter = sourceSet.iterator(); iter.hasNext();) {
      org.nlogo.agent.Agent tester = iter.next();
      Object result = freshContext.evaluateReporter(tester, args[1]);
      if (!(result instanceof Double)) {
        continue;
      }
View Full Code Here

    int n = argEvalIntValue(context, 0);
    if (n < 0) {
      throw new EngineException(context, this,
          I18N.errorsJ().getN("org.nlogo.prim.etc.$common.firstInputCantBeNegative", displayName()));
    }
    AgentSet sourceSet = argEvalAgentSet(context, 1);
    int count = sourceSet.count();
    if (n > count) {
      throw new EngineException(context, this,
          I18N.errorsJ().getN("org.nlogo.prim.etc.$common.notThatManyAgentsExist", n, count));
    }
    args[2].checkAgentSetClass(sourceSet, context);
    TreeMap<Object, LinkedList<Agent>> resultAgents =
        new TreeMap<Object, LinkedList<Agent>>
            (new Comparator<Object>() {
              public int compare(Object o1, Object o2) {
                if (o1.equals(o2)) {
                  return 0;
                }
                if (o1 instanceof Double && o2 instanceof Double) {
                  if (((Double) o1).doubleValue() > ((Double) o2).doubleValue()) {
                    return -1;
                  } else {
                    return 1;
                  }
                }
                throw new ClassCastException();
              }
            });

    org.nlogo.nvm.Context freshContext =
        new org.nlogo.nvm.Context(context, sourceSet);
    for (AgentSet.Iterator iter = sourceSet.shufflerator(context.job.random);
         iter.hasNext();) {
      org.nlogo.agent.Agent tester = iter.next();
      Object result = freshContext.evaluateReporter(tester, args[2]);
      if (!(result instanceof Double)) {
        continue;
      }
      LinkedList<Agent> resultList = resultAgents.get(result);
      if (resultList == null) {
        resultList = new LinkedList<Agent>();
        resultAgents.put(result, resultList);
      }
      resultList.add(tester);
    }

    AgentSet resultSet = new org.nlogo.agent.ArrayAgentSet
        (sourceSet.type(), n, false, world);

    for (Iterator<LinkedList<Agent>> iter = resultAgents.values().iterator();
         n > 0 && iter.hasNext();) {
      LinkedList<Agent> list = iter.next();
      for (Iterator<Agent> iter2 = list.iterator(); n > 0 && iter2.hasNext();) {
        resultSet.add(iter2.next());
        n--;
      }
    }

    return resultSet;
View Full Code Here

TOP

Related Classes of org.nlogo.agent.AgentSet$Iterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.