protected boolean beginExecution(final IScope scope) throws GamaRuntimeException { final IAgent agent = scope.getAgentScope(); if (scope.interrupted()) { return false; } final Boolean enter = (Boolean) agent.getAttribute(ENTER); Map<String, Object> memory = (Map) agent.getAttribute(STATE_MEMORY); if (enter || memory == null) { memory = new THashMap(); agent.setAttribute(STATE_MEMORY, memory); } else { for (final Map.Entry<String, Object> entry : memory.entrySet()) { scope.addVarWithValue(entry.getKey(), entry.getValue()); } } if (enter) { if (enterActions != null) { enterActions.executeOn(scope); } if (agent.dead()) { return false; } agent.setAttribute(ENTER, false); } return true; }
private static Instances convertToInstances( final IScope scope, final IList<String> attributes, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents) throws GamaRuntimeException { FastVector attribs = new FastVector(); for (String att : attributes) { attribs.addElement(new Attribute(att)); } Instances dataset = new Instances(scope.getAgentScope().getName(), attribs, agents.length(scope)); for (IAgent ag : agents.iterable(scope)) { int nb = attributes.size(); double vals[] = new double[nb]; for (int i = 0; i < nb; i++) { String attrib = attributes.get(i); Double var = Cast.asFloat(scope, ag.getDirectVarValue(scope, attrib)); vals[i] = var; } Instance instance = new Instance(1, vals); dataset.add(instance); } return dataset; }
private static double[] computeXaXs( final IScope scope, final IAgentFilter filter, final Map<Object, Integer> categoriesId, final Map<IAgent, Integer> agsId, final int valObsId, final int valSimId, final int valInitId, final GamaMatrix<Double> fuzzytransitions, final Double distance, final IAgent agent, final IList<Object> valsInit, final IList<Object> valsObs, final IList<Object> valsSim, final IContainer.Addressable<Integer, IAgent> agents, final int nbCat) { double xa = 0.0; double xs = 0.0; double[] XaXs = new double[2]; double sizeNorm = FastMath.sqrt(agent.getEnvelope().getArea()); List<IAgent> neighbors = distance == 0 || filter == null ? new ArrayList<IAgent>() : new ArrayList<IAgent>( scope.getTopology().getNeighborsOf(scope, agent, distance, filter)); Map<IAgent, Double> distancesCoeff = new TOrderedHashMap<IAgent, Double>(); distancesCoeff.put(agent, 1.0); for (IAgent ag : neighbors) { double euclidDist = agent.getLocation().euclidianDistanceTo(ag.getLocation()); distancesCoeff.put(ag, 1 / (1.0 + euclidDist / sizeNorm)); } for (IAgent ag : distancesCoeff.keySet()) { int id = agsId.get(ag); Object valI = valsInit.get(id); Object valO = valsObs.get(id); Object valS = valsSim.get(id); int valOId = categoriesId.get(valO); int valSId = categoriesId.get(valS); int valIId = categoriesId.get(valI); double dist = distancesCoeff.get(ag); double valxatmp = fuzzyTransition(scope, fuzzytransitions, nbCat, valInitId, valSimId, valIId, valOId) * dist; double valxstmp = fuzzyTransition(scope, fuzzytransitions, nbCat, valInitId, valObsId, valIId, valSId) * dist; if (valxatmp > xa) { xa = valxatmp; } if (valxstmp > xs) { xs = valxstmp; } } XaXs[0] = xa; XaXs[1] = xs; return XaXs; }
protected String evaluateTransitions(final IScope scope) throws GamaRuntimeException { final IAgent agent = scope.getAgentScope(); for (int i = 0; i < transitionsSize; i++) { final FsmTransitionStatement transition = transitions.get(i); if (transition.evaluatesTrueOn(scope)) { final String futureState = transition.getName(); haltOn(scope); transition.executeOn(scope); scope.setAgentVarValue(agent, STATE, futureState); return futureState; } } if (!agent.dead()) { scope.saveAllVarValuesIn((Map) agent.getAttribute(STATE_MEMORY)); } return name; }
@Override public IPopulation getPopulation(final IScope scope) { final IAgent a = scope.getAgentScope(); IPopulation result = null; if (a != null) { // AD 19/09/13 Patch to allow experiments to gain access to the simulation populations result = a.getPopulationFor(this); } return result; }
@operator( value = {IKeyword.AT, "@"}, type = ITypeProvider.FIRST_CONTENT_TYPE) @doc( deprecated = "The use of at on a species is deprecated, please use it one a population instead (list(species_name) instead of species_name)") public static IAgent at(final IScope scope, final ISpecies s, final GamaPoint val) throws GamaRuntimeException { return scope.getAgentScope().getPopulationFor(s).getAgent(scope, val); }
private static int buildRings( final IScope scope, final IAgentFilter filter, final Double distance, final List<Double> rings, final Map<Double, Integer> ringsPn, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents) { IList<ILocation> locs = GamaListFactory.create(Types.POINT); for (IAgent ag : agents.iterable(scope)) { locs.add(ag.getLocation()); } ILocation centralLoc = (ILocation) Stats.getMean(scope, locs); IAgent centralAg = scope.getTopology().getAgentClosestTo(scope, centralLoc, filter); List<IAgent> neighbors = distance == 0 || filter == null ? new ArrayList<IAgent>() : new ArrayList<IAgent>( scope.getTopology().getNeighborsOf(scope, centralAg, distance, filter)); for (IAgent ag : neighbors) { double dist = centralLoc.euclidianDistanceTo(ag.getLocation()); if (dist == 0) { continue; } if (!rings.contains(dist)) { rings.add(dist); ringsPn.put(dist, 1); } else { ringsPn.put(dist, 1 + ringsPn.get(dist)); } } Collections.sort(rings); for (int i = 1; i < rings.size(); i++) { double dist = rings.get(i); double dist1 = rings.get(i - 1); ringsPn.put(dist, ringsPn.get(dist) + ringsPn.get(dist1)); } return rings.size(); }
@Override public IList<ISpecies> getSubSpecies(final IScope scope) { IList<ISpecies> subspecies = GamaListFactory.create(Types.SPECIES); GamlModelSpecies model = (GamlModelSpecies) scope.getModel().getSpecies(); for (ISpecies s : model.getAllSpecies().values()) { if (s.getParentSpecies() == this) { subspecies.add(s); } } return subspecies; }
@Override public IList listValue(final IScope scope, final IType contentsType, final boolean copy) throws GamaRuntimeException { // return getPopulation(scope).listValue(scope, contentsType); // hqnghi 16/04/14 IPopulation pop = getPopulation(scope); if (pop == null) { pop = scope.getSimulationScope().getPopulationFor(contentsType.getName()); } return pop.listValue(scope, contentsType, false); // end-hqnghi }
@operator( value = {"clustering_em"}, content_type = IType.LIST, category = {IOperatorCategory.STATISTICAL}, concept = {IConcept.STATISTIC}) @doc( value = "A list of agent groups clustered by EM Algorithm based on the given attributes. Some paremeters can be defined: " + "max_iterations: the maximum number of iterations to perform;" + "num_clusters: the number of clusters; minStdDev: minimum allowable standard deviation", examples = { @example( value = "clustering_em([ag1, ag2, ag3, ag4, ag5],[\"size\",\"age\", \"weight\"],[\"max_iterations\"::10, \"num_clusters\"::3])", equals = "for example, can return [[ag1, ag3], [ag2], [ag4, ag5]]", isExecutable = false) }, see = { "clustering_xmeans", "clustering_simple_kmeans", "clustering_farthestFirst", "clustering_DBScan", "clustering_cobweb" }) public static IList<IList<IAgent>> primClusteringEM( final IScope scope, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents, final IList<String> attributes, final GamaMap<String, Object> parameters) { EM em = new EM(); em.setSeed(Cast.asInt(scope, scope.getRandom().getSeed())); if (parameters != null) { try { if (parameters.containsKey("max_iterations")) { em.setMaxIterations(Cast.asInt(scope, parameters.get("max_iterations"))); } if (parameters.containsKey("num_clusters")) { em.setNumClusters(Cast.asInt(scope, parameters.get("num_clusters"))); } if (parameters.containsKey("minStdDev")) { em.setMinStdDev(Cast.asFloat(scope, parameters.get("minStdDev"))); } } catch (Exception e) { } } IList<IList<IAgent>> groupes = clusteringUsingWeka(scope, em, attributes, agents); return groupes; }
@Override public GamaMap mapValue( final IScope scope, final IType keyType, final IType contentsType, final boolean copy) throws GamaRuntimeException { final IList<IAgent> agents = listValue(scope, contentsType, false); // Default behavior : Returns a map containing the names of agents as keys and the agents // themselves as values final GamaMap result = GamaMapFactory.create(Types.STRING, scope.getModelContext().getTypeNamed(getName())); for (final IAgent agent : agents.iterable(scope)) { result.put(agent.getName(), agent); } return result; }
@operator( value = {"clustering_cobweb"}, content_type = IType.LIST, category = {IOperatorCategory.STATISTICAL}, concept = {IConcept.STATISTIC}) @doc( value = "A list of agent groups clusteredby CobWeb Algorithm based on the given attributes. Some paremeters can be defined: " + "acuity: minimum standard deviation for numeric attributes; " + "cutoff: category utility threshold by which to prune nodes seed", examples = { @example( value = "clustering_cobweb([ag1, ag2, ag3, ag4, ag5],[\"size\",\"age\", \"weight\"],[\"acuity\"::3.0, \"cutoff\"::0.5)", equals = "for example, can return [[ag1, ag3], [ag2], [ag4, ag5]]", isExecutable = false) }, see = { "clustering_xmeans", "clustering_em", "clustering_farthestFirst", "clustering_simple_kmeans", "clustering_cobweb" }) public static IList<IList<IAgent>> primClusteringCobweb( final IScope scope, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents, final IList<String> attributes, final GamaMap<String, Object> parameters) { Cobweb cobweb = new Cobweb(); cobweb.setSeed(Cast.asInt(scope, scope.getRandom().getSeed())); if (parameters != null) { try { if (parameters.containsKey("acuity")) { cobweb.setAcuity(Cast.asFloat(scope, parameters.get("acuity"))); } if (parameters.containsKey("cutoff")) { cobweb.setCutoff(Cast.asFloat(scope, parameters.get("cutoff"))); } } catch (Exception e) { } } IList<IList<IAgent>> groupes = clusteringUsingWeka(scope, cobweb, attributes, agents); return groupes; }
public int getDate(IScope scope) { return scope.getClock().getCycle(); }
private static double computeSimilarity( final IScope scope, final IAgentFilter filter, final Double distance, final IList<Object> vals1, final IList<Object> vals2, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents, final int nbCat, final int nb, final double[][] crispVector1, final double[][] crispVector2, final boolean[] sim, final double[][] fuzzyVector1, final double[][] fuzzyVector2, final IList<Double> similarities, final IList<Object> weights) { Map<IAgent, Integer> agsId = new TOrderedHashMap<IAgent, Integer>(); for (int i = 0; i < agents.length(scope); i++) { agsId.put(agents.get(scope, i), i); } for (int i = 0; i < nb; i++) { if (sim[i]) { similarities.add(1.0); } else { IAgent agent = agents.get(scope, i); // double sizeNorm = agent.getPerimeter() / 4.0; double sizeNorm = FastMath.sqrt(agent.getEnvelope().getArea()); List<IAgent> neighbors = distance == 0 || filter == null ? new ArrayList<IAgent>() : new ArrayList<IAgent>( scope.getTopology().getNeighborsOf(scope, agent, distance, filter)); Map<IAgent, Double> distancesCoeff = new TOrderedHashMap<IAgent, Double>(); distancesCoeff.put(agent, 1.0); for (IAgent ag : neighbors) { double euclidDist = agent.getLocation().euclidianDistanceTo(ag.getLocation()); distancesCoeff.put(ag, 1 / (1.0 + euclidDist / sizeNorm)); } for (int j = 0; j < nbCat; j++) { double max1 = 0.0; double max2 = 0.0; for (IAgent ag : neighbors) { int id = agsId.get(ag); double val1 = crispVector1[id][j] * distancesCoeff.get(ag); double val2 = crispVector2[id][j] * distancesCoeff.get(ag); if (val1 > max1) { max1 = val1; } if (val2 > max2) { max2 = val2; } } fuzzyVector1[i][j] = max1; fuzzyVector2[i][j] = max2; } double s1Max = -1 * Double.MAX_VALUE; double s2Max = -1 * Double.MAX_VALUE; for (int j = 0; j < nbCat; j++) { double s1 = FastMath.min(fuzzyVector1[i][j], crispVector2[i][j]); double s2 = FastMath.min(fuzzyVector2[i][j], crispVector1[i][j]); if (s1 > s1Max) { s1Max = s1; } if (s2 > s2Max) { s2Max = s2; } } similarities.add(FastMath.min(s1Max, s2Max)); } } double meanSimilarity = 0; double total = 0; for (int i = 0; i < nb; i++) { double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i)); double val = weight * similarities.get(i); total += weight; meanSimilarity += val; } meanSimilarity /= total; return meanSimilarity; }
@operator( value = {"clustering_xmeans"}, content_type = IType.LIST, category = {IOperatorCategory.STATISTICAL}, concept = {IConcept.STATISTIC}) @doc( value = "A list of agent groups clustered by X-Means Algorithm based on the given attributes. Some paremeters can be defined: bin_value: value given for true value of boolean attributes; cut_off_factor: the cut-off factor to use;" + "distance_f: The distance function to use. 4 possible distance functions: euclidean (by default) ; 'chebyshev', 'manhattan' or 'levenshtein'; " + "max_iterations: the maximum number of iterations to perform; max_kmeans: the maximum number of iterations to perform in KMeans; max_kmeans_for_children: the maximum number of iterations KMeans that is performed on the child centers;" + "max_num_clusters: the maximum number of clusters; min_num_clusters: the minimal number of clusters", examples = { @example( value = "clustering_xmeans([ag1, ag2, ag3, ag4, ag5],[\"size\",\"age\", \"weight\", \"is_male\"],[\"bin_value\"::1.0, \"distance_f\"::\"manhattan\", \"max_num_clusters\"::10, \"min_num_clusters\"::2])", equals = "for example, can return [[ag1, ag3], [ag2], [ag4, ag5]]", isExecutable = false) }, see = { "clustering_simple_kmeans", "clustering_em", "clustering_farthestFirst", "clustering_DBScan", "clustering_cobweb" }) public static IList<IList<IAgent>> primClusteringXMeans( final IScope scope, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents, final IList<String> attributes, final GamaMap<String, Object> parameters) throws GamaRuntimeException { XMeans xmeans = new XMeans(); xmeans.setSeed(Cast.asInt(scope, scope.getRandom().getSeed())); if (parameters != null) { if (parameters.containsKey("bin_value")) { xmeans.setBinValue(Cast.asFloat(scope, parameters.get("bin_value"))); } if (parameters.containsKey("cut_off_factor")) { xmeans.setCutOffFactor(Cast.asFloat(scope, parameters.get("cut_off_factor"))); } if (parameters.containsKey("distance_f")) { String distanceFct = Cast.asString(scope, parameters.get("distance_f")); if (distanceFct.equals("chebyshev")) { xmeans.setDistanceF(new ChebyshevDistance()); } else if (distanceFct.equals("manhattan")) { xmeans.setDistanceF(new ManhattanDistance()); } else if (distanceFct.equals("levenshtein")) { xmeans.setDistanceF(new EditDistance()); } } if (parameters.containsKey("max_iterations")) { try { xmeans.setMaxIterations(Cast.asInt(scope, parameters.get("max_iterations"))); } catch (Exception e) { } } if (parameters.containsKey("max_kmeans")) { xmeans.setMaxKMeans(Cast.asInt(scope, parameters.get("max_kmeans"))); } if (parameters.containsKey("max_kmeans_for_children")) { xmeans.setMaxKMeansForChildren( Cast.asInt(scope, parameters.get("max_kmeans_for_children"))); } if (parameters.containsKey("max_num_clusters")) { xmeans.setMaxNumClusters(Cast.asInt(scope, parameters.get("max_num_clusters"))); } if (parameters.containsKey("min_num_clusters")) { xmeans.setMinNumClusters(Cast.asInt(scope, parameters.get("min_num_clusters"))); } } IList<IList<IAgent>> groupes = clusteringUsingWeka(scope, xmeans, attributes, agents); return groupes; }
private static void computeXaXsTransitions( final IScope scope, final IAgentFilter filter, final GamaMatrix<Double> fuzzytransitions, final Double distance, final IContainer<Integer, IAgent> agents, final int nbCat, final Map<List<Integer>, Map<Double, Double>> XaPerTransition, final Map<List<Integer>, Map<Double, Double>> XsPerTransition, final Set<Double> Xvals) { IList<ILocation> locs = GamaListFactory.create(Types.POINT); for (IAgent ag : agents.iterable(scope)) { locs.add(ag.getLocation()); } ILocation centralLoc = (ILocation) Stats.getMean(scope, locs); if (filter != null) { IAgent centralAg = scope.getTopology().getAgentClosestTo(scope, centralLoc, filter); List<IAgent> neighbors = distance == 0 ? new ArrayList<IAgent>() : new ArrayList<IAgent>( scope.getTopology().getNeighborsOf(scope, centralAg, distance, filter)); double sizeNorm = FastMath.sqrt(centralAg.getEnvelope().getArea()); Map<IAgent, Double> distancesCoeff = new TOrderedHashMap<IAgent, Double>(); distancesCoeff.put(centralAg, 1.0); for (IAgent ag : neighbors) { double euclidDist = centralAg.getLocation().euclidianDistanceTo(ag.getLocation()); double dist = 1 / (1.0 + euclidDist / sizeNorm); distancesCoeff.put(ag, dist); } for (int i = 0; i < nbCat; i++) { for (int j = 0; j < nbCat; j++) { for (int k = 0; k < nbCat; k++) { List<Integer> ca = new ArrayList(); ca.add(i); ca.add(j); ca.add(k); double xa = 0; double xs = 0; for (IAgent ag : distancesCoeff.keySet()) { double dist = distancesCoeff.get(ag); double xatmp = fuzzyTransition(scope, fuzzytransitions, nbCat, i, k, i, j) * dist; double xstmp = fuzzyTransition(scope, fuzzytransitions, nbCat, i, j, i, k) * dist; if (xatmp > xa) { xa = xatmp; } if (xstmp > xs) { xs = xstmp; } } if (xa > 0) { Map<Double, Double> mapxa = XaPerTransition.get(ca); if (mapxa == null) { mapxa = new TOrderedHashMap<Double, Double>(); mapxa.put(xa, 1.0); XaPerTransition.put(ca, mapxa); } else { if (mapxa.containsKey(xa)) { mapxa.put(xa, mapxa.get(xa) + 1.0); } else { mapxa.put(xa, 1.0); } } Xvals.add(xa); } if (xs > 0) { Map<Double, Double> mapxs = XsPerTransition.get(ca); if (mapxs == null) { mapxs = new TOrderedHashMap<Double, Double>(); mapxs.put(xs, 1.0); XsPerTransition.put(ca, mapxs); } else { if (mapxs.containsKey(xa)) { mapxs.put(xs, mapxs.get(xs) + 1.0); } else { mapxs.put(xs, 1.0); } } Xvals.add(xs); } } } } } }
@Override public Object value(final IScope scope) { // return scope.getSimulationScope(); IAgent sc = scope.getAgentScope(); return sc.getScope().getRoot().getScope().getSimulationScope(); }
@operator( value = {"clustering_simple_kmeans"}, content_type = IType.LIST, category = {IOperatorCategory.STATISTICAL}, concept = {IConcept.STATISTIC}) @doc( value = "A list of agent groups clustered by K-Means Algorithm based on the given attributes. Some paremeters can be defined: " + "distance_f: The distance function to use. 4 possible distance functions: euclidean (by default) ; 'chebyshev', 'manhattan' or 'levenshtein'; " + "dont_replace_missing_values: if false, replace missing values globally with mean/mode; max_iterations: the maximum number of iterations to perform;" + "num_clusters: the number of clusters", examples = { @example( value = "clustering_simple_kmeans([ag1, ag2, ag3, ag4, ag5],[\"size\",\"age\", \"weight\"],[\"distance_f\"::\"manhattan\", \"num_clusters\"::3])", equals = "for example, can return [[ag1, ag3], [ag2], [ag4, ag5]]", isExecutable = false) }, see = { "clustering_xmeans", "clustering_em", "clustering_farthestFirst", "clustering_DBScan", "clustering_cobweb" }) public static IList<IList<IAgent>> primClusteringSimpleKMeans( final IScope scope, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents, final IList<String> attributes, final GamaMap<String, Object> parameters) { SimpleKMeans kmeans = new SimpleKMeans(); kmeans.setSeed(Cast.asInt(scope, scope.getRandom().getSeed())); if (parameters != null) { try { if (parameters.containsKey("distance_f")) { String distanceFct = Cast.asString(scope, parameters.get("distance_f")); if (distanceFct.equals("chebyshev")) { kmeans.setDistanceFunction(new ChebyshevDistance()); } else if (distanceFct.equals("manhattan")) { kmeans.setDistanceFunction(new ManhattanDistance()); } else if (distanceFct.equals("levenshtein")) { kmeans.setDistanceFunction(new EditDistance()); } } if (parameters.containsKey("dont_replace_missing_values")) { kmeans.setDontReplaceMissingValues( Cast.asBool(scope, parameters.get("dont_replace_missing_values"))); } if (parameters.containsKey("max_iterations")) { kmeans.setMaxIterations(Cast.asInt(scope, parameters.get("max_iterations"))); } if (parameters.containsKey("num_clusters")) { kmeans.setNumClusters(Cast.asInt(scope, parameters.get("num_clusters"))); } } catch (Exception e) { } } IList<IList<IAgent>> groupes = clusteringUsingWeka(scope, kmeans, attributes, agents); return groupes; }