Exemple #1
1
  public void doRFileEvaluate(final IScope scope, final IContainer param) {
    int size = param.length(scope);
    // if ( size == 0 ) { throw GamaRuntimeException.error("Missing Parameter Exception", scope); }

    final String RFile = getPath();
    try {
      // Call R
      RCaller caller = new RCaller();

      String RPath = GamaPreferences.LIB_R.value(scope).getPath();
      caller.setRscriptExecutable(RPath);

      double[] vectorParam = new double[param.length(scope)];

      int k = 0;
      for (Object o : param.iterable(scope)) {
        vectorParam[k++] = Cast.asFloat(scope, o);
      }

      RCode c = new RCode();
      // Adding the parameters
      c.addDoubleArray("vectorParam", vectorParam);

      // Adding the codes in file
      List<String> R_statements = new ArrayList<String>();

      // tmthai.begin----------------------------------------------------------------------------
      String fullPath = FileUtils.constructAbsoluteFilePath(scope, RFile, true);
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute_param.RScript:" + RPath);
        GuiUtils.debug("Stats.R_compute_param.Param:" + vectorParam.toString());
        GuiUtils.debug("Stats.R_compute_param.RFile:" + RFile);
        GuiUtils.debug("Stats.R_compute_param.fullPath:" + fullPath);
      }

      // FileReader fr = new FileReader(RFile);
      FileReader fr = new FileReader(fullPath);
      // tmthai.end----------------------------------------------------------------------------

      BufferedReader br = new BufferedReader(fr);
      String statement;

      while ((statement = br.readLine()) != null) {
        c.addRCode(statement);
        R_statements.add(statement);
        // java.lang.System.out.println(statement);
      }
      br.close();
      fr.close();
      caller.setRCode(c);

      GamaMap<String, IList> result = GamaMapFactory.create(Types.STRING, Types.LIST);

      String var = computeVariable(R_statements.get(R_statements.size() - 1).toString());
      caller.runAndReturnResult(var);

      // DEBUG:
      // java.lang.System.out.println("Name: '" + R_statements.length(scope) + "'");
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute_param.R_statements.length: '" + R_statements.size() + "'");
      }

      for (String name : caller.getParser().getNames()) {
        String[] results = null;
        results = caller.getParser().getAsStringArray(name);
        // java.lang.System.out.println("Name: '" + name + "'");
        if (DEBUG) {
          GuiUtils.debug(
              "Stats.R_compute_param.caller.Name: '"
                  + name
                  + "' length: "
                  + results.length
                  + " - Value: "
                  + results.toString());
        }

        result.put(name, GamaListFactory.create(scope, Types.NO_TYPE, results));
      }

      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute_param.return:" + result.serialize(false));
      }

      setBuffer(result);

    } catch (Exception ex) {

      throw GamaRuntimeException.error("RCallerExecutionException " + ex.getMessage(), scope);
    }
  }
Exemple #2
0
 @operator(
     value = {"percent_absolute_deviation"},
     content_type = IType.FLOAT,
     category = {IOperatorCategory.MAP_COMPARAISON},
     concept = {IConcept.STATISTIC})
 @doc(
     value =
         "percent absolute deviation indicator for 2 series of values: percent_absolute_deviation(list_vals_observe,list_vals_sim)",
     examples = {
       @example(
           value = "percent_absolute_deviation([200,300,150,150,200],[250,250,100,200,200])",
           isExecutable = false)
     })
 public static double percentAbsoluteDeviation(
     final IScope scope, final IList<Double> vals1, final IList<Double> vals2) {
   if (vals1 == null || vals2 == null) {
     return 1;
   }
   int nb = vals1.size();
   if (nb != vals2.size()) {
     return 0;
   }
   double sum = 0;
   double coeff = 0;
   for (int i = 0; i < nb; i++) {
     double val1 = Cast.asFloat(scope, vals1.get(i));
     double val2 = Cast.asFloat(scope, vals2.get(i));
     coeff += val1;
     sum += FastMath.abs(val1 - val2) * 100.0;
   }
   if (coeff == 0) {
     return 0;
   }
   return sum / coeff;
 }
Exemple #3
0
  @operator(
      value = {"clustering_DBScan"},
      content_type = IType.LIST,
      category = {IOperatorCategory.STATISTICAL},
      concept = {IConcept.STATISTIC})
  @doc(
      value =
          "A list of agent groups clustered by DBScan Algorithm based on the given attributes. Some paremeters can be defined: "
              + "distance_f: The distance function to use for instances comparison (euclidean or manhattan); "
              + "min_points: minimun number of DataObjects required in an epsilon-range-query"
              + "epsilon: epsilon -- radius of the epsilon-range-queries",
      examples = {
        @example(
            value =
                "clustering_DBScan([ag1, ag2, ag3, ag4, ag5],[\"size\",\"age\", \"weight\"],[\"distance_f\"::\"manhattan\"])",
            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>> primClusteringDBScan(
      final IScope scope,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents,
      final IList<String> attributes,
      final GamaMap<String, Object> parameters) {
    DBSCAN dbScan = new DBSCAN();

    if (parameters != null) {
      try {
        if (parameters.containsKey("distance_f")) {
          String distanceFct = Cast.asString(scope, parameters.get("distance_f"));
          if (distanceFct.equals("manhattan")) {
            dbScan.setDatabase_distanceType(ManhattanDataObject.class.getName());
          } else {
            dbScan.setDatabase_distanceType(EuclideanDistance.class.getName());
          }
        }

        if (parameters.containsKey("min_points")) {
          dbScan.setMinPoints(Cast.asInt(scope, parameters.get("min_points")));
        }
        if (parameters.containsKey("epsilon")) {
          dbScan.setEpsilon(Cast.asInt(scope, parameters.get("epsilon")));
        }
      } catch (Exception e) {
      }
    }

    IList<IList<IAgent>> groupes = clusteringUsingWeka(scope, dbScan, attributes, agents);

    return groupes;
  }
Exemple #4
0
  @Override
  public void firstLaunchOn(final IDisplaySurface surface) {
    super.firstLaunchOn(surface);
    final IExpression eventType = definition.getFacet(IKeyword.NAME);
    final IExpression actionName = definition.getFacet(IKeyword.ACTION);
    IScope scope = surface.getDisplayScope();

    String currentMouseEvent = Cast.asString(scope, eventType.value(scope));
    String currentAction = Cast.asString(scope, actionName.value(scope));

    listener = new EventListener(surface, currentMouseEvent, currentAction);
    surface.addMouseListener(listener);
  }
Exemple #5
0
  @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;
  }
Exemple #6
0
  @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;
  }
Exemple #7
0
  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;
  }
Exemple #8
0
  private static double computePo(
      final IScope scope,
      final IAgentFilter filter,
      final Map<Object, Integer> categoriesId,
      final GamaMatrix<Double> fuzzytransitions,
      final Double distance,
      final IList<Object> valsInit,
      final IList<Object> valsObs,
      final IList<Object> valsSim,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents,
      final int nbCat,
      final int nb,
      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++) {
      Object valObs = valsObs.get(i);
      Object valSim = valsSim.get(i);
      Object valInit = valsInit.get(i);
      int valObsId = categoriesId.get(valObs);
      int valSimId = categoriesId.get(valSim);
      int valInitId = categoriesId.get(valInit);
      IAgent agent = agents.get(scope, i);
      double[] XaXs =
          computeXaXs(
              scope,
              filter,
              categoriesId,
              agsId,
              valObsId,
              valSimId,
              valInitId,
              fuzzytransitions,
              distance,
              agent,
              valsInit,
              valsObs,
              valsSim,
              agents,
              nbCat);
      similarities.add(FastMath.min(XaXs[0], XaXs[1]));
    }
    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;
  }
Exemple #9
0
 private static void computeXYCrispVector(
     final IScope scope,
     final Map<Object, Integer> categoriesId,
     final List<Object> categories,
     final IList<Object> vals1,
     final IList<Object> vals2,
     final GamaMatrix<Double> fuzzycategories,
     final int nbCat,
     final int nb,
     final double[][] crispVector1,
     final double[][] crispVector2,
     final double[] X,
     final double[] Y,
     final boolean[] sim,
     final IList<Object> weights) {
   for (int j = 0; j < nbCat; j++) {
     X[j] = 0;
     Y[j] = 0;
   }
   double total = 0;
   for (int i = 0; i < nb; i++) {
     double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
     total += weight;
     Object val1 = vals1.get(i);
     Object val2 = vals2.get(i);
     int indexVal1 = categoriesId.get(val1);
     int indexVal2 = categoriesId.get(val2);
     X[indexVal1] += weight;
     Y[indexVal2] += weight;
     for (int j = 0; j < nbCat; j++) {
       crispVector1[i][j] = Cast.asFloat(scope, fuzzycategories.get(scope, indexVal1, j));
       crispVector2[i][j] = Cast.asFloat(scope, fuzzycategories.get(scope, indexVal2, j));
     }
     if (val1.equals(val2)) {
       sim[i] = true;
     } else {
       sim[i] = false;
     }
   }
   for (int j = 0; j < nbCat; j++) {
     X[j] /= total;
     Y[j] /= total;
   }
 }
 public FsmStateStatement(final IDescription desc) {
   super(desc);
   setName(getLiteral(IKeyword.NAME)); // A VOIR
   isInitial = Cast.asBool(null, getLiteral(FsmStateStatement.INITIAL));
   isFinal = Cast.asBool(null, getLiteral(FsmStateStatement.FINAL));
 }
Exemple #11
0
  @operator(
      value = {"kappa"},
      content_type = IType.FLOAT,
      category = {IOperatorCategory.MAP_COMPARAISON},
      concept = {})
  @doc(
      value =
          "kappa indicator for 2 map comparisons: kappa(list_vals1,list_vals2,categories, weights). Reference: Cohen, J. A coefficient of agreement for nominal scales. Educ. Psychol. Meas. 1960, 20. ",
      examples = {
        @example(
            value =
                "kappa([cat1,cat1,cat2,cat3,cat2],[cat2,cat1,cat2,cat1,cat2],[cat1,cat2,cat3], [1.0, 2.0, 3.0, 1.0, 5.0])",
            isExecutable = false)
      })
  public static double kappa(
      final IScope scope,
      final IList<Object> vals1,
      final IList<Object> vals2,
      final List<Object> categories,
      final IList<Object> weights) {
    if (vals1 == null || vals2 == null) {
      return 1;
    }
    int nb = vals1.size();
    if (nb != vals2.size()) {
      return 0;
    }
    int nbCat = categories.size();
    double[] X = new double[nbCat];
    double[] Y = new double[nbCat];
    double[][] contigency = new double[nbCat][nbCat];
    for (int j = 0; j < nbCat; j++) {
      X[j] = 0;
      Y[j] = 0;
      for (int k = 0; k < nbCat; k++) {
        contigency[j][k] = 0;
      }
    }

    Map<Object, Integer> categoriesId = new TOrderedHashMap<Object, Integer>();
    for (int i = 0; i < nbCat; i++) {
      categoriesId.put(categories.get(i), i);
    }
    double total = 0;
    for (int i = 0; i < nb; i++) {
      double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
      total += weight;
      Object val1 = vals1.get(i);
      Object val2 = vals2.get(i);
      int indexVal1 = categoriesId.get(val1);
      int indexVal2 = categoriesId.get(val2);
      X[indexVal1] += weight;
      Y[indexVal2] += weight;
      contigency[indexVal1][indexVal2] += weight;
    }
    for (int j = 0; j < nbCat; j++) {
      X[j] /= total;
      Y[j] /= total;
      for (int k = 0; k < nbCat; k++) {
        contigency[j][k] /= total;
      }
    }
    double po = 0;
    double pe = 0;
    for (int i = 0; i < nbCat; i++) {
      po += contigency[i][i];
      pe += X[i] * Y[i];
    }
    if (pe == 1) {
      return 1;
    }
    return (po - pe) / (1 - pe);
  }
Exemple #12
0
  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;
  }
Exemple #13
0
  @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;
  }
Exemple #14
0
  @operator(
      value = {"fuzzy_kappa_sim"},
      content_type = IType.FLOAT,
      category = {IOperatorCategory.MAP_COMPARAISON},
      concept = {IConcept.MAP})
  @doc(
      value =
          "fuzzy kappa simulation indicator for 2 map comparisons: fuzzy_kappa_sim(agents_list,list_vals1,list_vals2, output_similarity_per_agents,fuzzy_transitions_matrix, fuzzy_distance, weights). Reference: Jasper van Vliet, Alex Hagen-Zanker, Jelle Hurkens, Hedwig van Delden, A fuzzy set approach to assess the predictive accuracy of land use simulations, Ecological Modelling, 24 July 2013, Pages 32-42, ISSN 0304-3800, ",
      examples = {
        @example(
            value =
                "fuzzy_kappa_sim([ag1, ag2, ag3, ag4, ag5], [cat1,cat1,cat2,cat3,cat2],[cat2,cat1,cat2,cat1,cat2], similarity_per_agents,[cat1,cat2,cat3],[[1,0,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,0],[0,0,0,1,0,0,0,0,0],[0,0,0,0,1,0,0,0,0],[0,0,0,0,0,1,0,0,0],[0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,0],[0,0,0,0,0,0,0,0,1]], 2,[1.0,3.0,2.0,2.0,4.0])",
            isExecutable = false)
      })
  public static double fuzzyKappaSimulation(
      final IScope scope,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents,
      final IList<Object> valsInit,
      final IList<Object> valsObs,
      final IList<Object> valsSim,
      final IList<Double> similarities,
      final List<Object> categories,
      final GamaMatrix<Double> fuzzytransitions,
      final Double distance,
      final IList<Object> weights) {
    if (agents == null) {
      return 1;
    }
    int nb = agents.length(scope);
    if (nb < 1) {
      return 1;
    }
    similarities.clear();
    int nbCat = categories.size();
    double[] nbObs = new double[nbCat];
    double[] nbSim = new double[nbCat];
    double[] nbInit = new double[nbCat];
    double[][] nbInitObs = new double[nbCat][nbCat];
    double[][] nbInitSim = new double[nbCat][nbCat];
    Map<Object, Integer> categoriesId = new TOrderedHashMap<Object, Integer>();

    Map<List<Integer>, Map<Double, Double>> XaPerTransition =
        new TOrderedHashMap<List<Integer>, Map<Double, Double>>();
    Map<List<Integer>, Map<Double, Double>> XsPerTransition =
        new TOrderedHashMap<List<Integer>, Map<Double, Double>>();
    Set<Double> Xvals = new HashSet<Double>();
    for (int i = 0; i < nbCat; i++) {
      categoriesId.put(categories.get(i), i);
    }

    for (int i = 0; i < nbCat; i++) {
      nbInit[i] = 0;
      nbObs[i] = 0;
      nbSim[i] = 0;
      for (int j = 0; j < nbCat; j++) {
        nbInitObs[i][j] = 0;
        nbInitSim[i][j] = 0;
      }
    }
    IAgentFilter filter = In.list(scope, agents);
    double total = 0;
    for (int i = 0; i < nb; i++) {
      double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
      total += weight;

      int idCatInit = categoriesId.get(valsInit.get(i));
      int idCatObs = categoriesId.get(valsObs.get(i));
      int idCatSim = categoriesId.get(valsSim.get(i));
      nbInit[idCatInit] += weight;
      nbSim[idCatSim] += weight;
      nbObs[idCatObs] += weight;
      nbInitObs[idCatInit][idCatObs] += weight;
      nbInitSim[idCatInit][idCatSim] += weight;
    }
    double po =
        computePo(
            scope,
            filter,
            categoriesId,
            fuzzytransitions,
            distance,
            valsInit,
            valsObs,
            valsSim,
            agents,
            nbCat,
            nb,
            similarities,
            weights);
    double pe = 0;
    computeXaXsTransitions(
        scope,
        filter,
        fuzzytransitions,
        distance,
        agents,
        nbCat,
        XaPerTransition,
        XsPerTransition,
        Xvals);
    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<Integer>();
          ca.add(i);
          ca.add(j);
          ca.add(k);
          Map<Double, Double> pmuXa = XaPerTransition.get(ca);
          Map<Double, Double> pmuXs = XsPerTransition.get(ca);
          double emu = 0;
          for (Double xval : Xvals) {
            double XaVal = pmuXa == null || !pmuXa.containsKey(xval) ? 0 : pmuXa.get(xval);
            double XsVal = pmuXs == null || !pmuXs.containsKey(xval) ? 0 : pmuXs.get(xval);
            double proba = xval * XaVal * XsVal;
            emu += proba;
          }

          double poas = nbInit[i] == 0 ? 0 : nbInitObs[i][j] / nbInit[i] * nbInitSim[i][k] / total;
          pe += emu * poas;
        }
      }
    }
    if (pe == 1) {
      return 1;
    }
    return (po - pe) / (1 - pe);
  }
Exemple #15
0
 @operator(
     value = {"kappa_sim"},
     content_type = IType.FLOAT,
     category = {IOperatorCategory.MAP_COMPARAISON},
     concept = {})
 @doc(
     value =
         "kappa simulation indicator for 2 map comparisons: kappa(list_valsInits,list_valsObs,list_valsSim, categories, weights). Reference: van Vliet, J., Bregt, A.K. & Hagen-Zanker, A. (2011). Revisiting Kappa to account for change in the accuracy assessment of land-use change models, Ecological Modelling 222(8)",
     examples = {
       @example(
           value =
               "kappa([cat1,cat1,cat2,cat2,cat2],[cat2,cat1,cat2,cat1,cat3],[cat2,cat1,cat2,cat3,cat3], [cat1,cat2,cat3],[1.0, 2.0, 3.0, 1.0, 5.0])",
           isExecutable = false)
     })
 public static double kappaSimulation(
     final IScope scope,
     final IList<Object> valsInit,
     final IList<Object> valsObs,
     final IList<Object> valsSim,
     final List<Object> categories,
     final IList<Object> weights) {
   if (valsInit == null || valsObs == null || valsSim == null) {
     return 1;
   }
   int nb = valsInit.size();
   if (nb != valsObs.size() || nb != valsSim.size()) {
     return 0;
   }
   int nbCat = categories.size();
   double[] O = new double[nbCat];
   double[][] contigency = new double[nbCat][nbCat];
   double[][] contigencyOA = new double[nbCat][nbCat];
   double[][] contigencyOS = new double[nbCat][nbCat];
   for (int j = 0; j < nbCat; j++) {
     O[j] = 0;
     for (int k = 0; k < nbCat; k++) {
       contigency[j][k] = 0;
       contigencyOA[j][k] = 0;
       contigencyOS[j][k] = 0;
     }
   }
   Map<Object, Integer> categoriesId = new TOrderedHashMap<Object, Integer>();
   for (int i = 0; i < nbCat; i++) {
     categoriesId.put(categories.get(i), i);
   }
   double total = 0;
   for (int i = 0; i < nb; i++) {
     double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
     total += weight;
     Object val1 = valsObs.get(i);
     Object val2 = valsSim.get(i);
     Object valO = valsInit.get(i);
     int indexVal1 = categoriesId.get(val1);
     int indexVal2 = categoriesId.get(val2);
     int indexValO = categoriesId.get(valO);
     O[indexValO] += weight;
     contigency[indexVal1][indexVal2] += weight;
     contigencyOA[indexValO][indexVal1] += weight;
     contigencyOS[indexValO][indexVal2] += weight;
   }
   for (int j = 0; j < nbCat; j++) {
     for (int k = 0; k < nbCat; k++) {
       contigency[j][k] /= total;
       if (O[j] > 0) {
         contigencyOA[j][k] /= O[j];
         contigencyOS[j][k] /= O[j];
       }
     }
     O[j] /= total;
   }
   double po = 0;
   double pe = 0;
   for (int j = 0; j < nbCat; j++) {
     po += contigency[j][j];
     double sum = 0;
     for (int i = 0; i < nbCat; i++) {
       sum += contigencyOA[j][i] * contigencyOS[j][i];
     }
     pe += O[j] * sum;
   }
   if (pe == 1) {
     return 1;
   }
   return (po - pe) / (1 - pe);
 }
Exemple #16
0
  @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;
  }
  @Override
  protected Object privateExecuteIn(final IScope scope) throws GamaRuntimeException {
    // IGraph data = createData(scope);
    IGraph g = Cast.asGraph(scope, getFacetValue(scope, IKeyword.GRAPH));
    int thread_number =
        Cast.asInt(
            scope,
            getFacetValue(
                scope, GraphForceAtlasLayoutStatement.THREAD_NUMBER, THREAD_NUMBER_DEFAULT));
    boolean dissuade_hubs =
        Cast.asBool(
            scope,
            getFacetValue(
                scope, GraphForceAtlasLayoutStatement.DISSUADE_HUBS, DISSUADE_HUBS_DEFAULT));
    boolean linlog_mode =
        Cast.asBool(
            scope,
            getFacetValue(scope, GraphForceAtlasLayoutStatement.LINLOG_MODE, LINLOG_MODE_DEFAULT));
    boolean prevent_overlap =
        Cast.asBool(
            scope,
            getFacetValue(
                scope, GraphForceAtlasLayoutStatement.PREVENT_OVERLAP, PREVENT_OVERLAP_DEFAULT));
    double edge_weight_influence =
        Cast.asFloat(
            scope,
            getFacetValue(
                scope,
                GraphForceAtlasLayoutStatement.EDGE_WEIGHT_INFLUENCE,
                EDGE_WEIGHT_INFLUENCE_DEFAULT));
    double scaling =
        Cast.asFloat(
            scope, getFacetValue(scope, GraphForceAtlasLayoutStatement.SCALING, SCALING_DEFAULT));
    boolean stronger_gravity =
        Cast.asBool(
            scope,
            getFacetValue(
                scope, GraphForceAtlasLayoutStatement.STRONGER_GRAVITY, STRONGER_GRAVITY_DEFAULT));
    double gravity =
        Cast.asFloat(
            scope, getFacetValue(scope, GraphForceAtlasLayoutStatement.GRAVITY, GRAVITY_DEFAULT));
    double tolerance =
        Cast.asFloat(
            scope,
            getFacetValue(scope, GraphForceAtlasLayoutStatement.TOLERANCE, TOLERANCE_DEFAULT));
    boolean approximate_repulsion =
        Cast.asBool(
            scope,
            getFacetValue(
                scope,
                GraphForceAtlasLayoutStatement.APPROXIMATE_REPULSION,
                APPROXIMATE_REPULSION_DEFAULT));
    double approximation =
        Cast.asFloat(
            scope,
            getFacetValue(
                scope, GraphForceAtlasLayoutStatement.APPROXIMATION, APPROXIMATION_DEFAULT));
    int nb_steps =
        Cast.asInt(
            scope, getFacetValue(scope, GraphForceAtlasLayoutStatement.NB_STEPS, NB_STEPS_DEFAULT));
    Object bp1 = getFacetValue(scope, GraphForceAtlasLayoutStatement.BOUNDED_P1, null);
    Object bp2 = getFacetValue(scope, GraphForceAtlasLayoutStatement.BOUNDED_P2, null);
    // public static IGraph YifanHuLayout_Step_Unlimited(final IGraph g, double optimal_distance,
    // double initial_step, double step_ratio, double convergence_threshold, double
    // barnes_hut_theta) {
    initializing();
    // System.out.println("ForceAtlas2 algorithm (by step), starting.");
    // System.out.println("converting ...");
    IGraph_to_GraphModel(g);
    // System.out.println("initializing ...");
    ForceAtlas2 fa2Layout = new ForceAtlas2(new ForceAtlas2Builder());
    initializing_GraphModel(g);
    fa2Layout.resetPropertiesValues();
    fa2Layout.setGraphModel(graph_model);
    fa2Layout.setOutboundAttractionDistribution(dissuade_hubs);
    fa2Layout.setLinLogMode(linlog_mode);
    fa2Layout.setAdjustSizes(prevent_overlap);
    fa2Layout.setEdgeWeightInfluence(edge_weight_influence);
    fa2Layout.setScalingRatio(scaling);
    fa2Layout.setStrongGravityMode(stronger_gravity);
    fa2Layout.setGravity(gravity);
    fa2Layout.setJitterTolerance(tolerance);
    fa2Layout.setBarnesHutOptimize(approximate_repulsion);
    fa2Layout.setBarnesHutTheta(approximation);
    // System.out.println("working ...");
    fa2Layout.initAlgo();
    // System.out.println("working ...");
    // int nbsteps=1;
    // for (int i = 0; i < nbsteps && fa2Layout.canAlgo(); i++){
    for (int i = 0; i < nb_steps; i++) {
      fa2Layout.goAlgo();
    }
    fa2Layout.endAlgo();
    // fa2Layout.goAlgo();
    // fa2Layout.endAlgo();
    if (bp1 != null && bp2 != null) {
      ILocation p1 = Cast.asPoint(scope, bp1);
      ILocation p2 = Cast.asPoint(scope, bp2);
      Update_locations(
          g,
          FastMath.min(p1.getX(), p2.getX()),
          FastMath.min(p1.getY(), p2.getY()),
          FastMath.max(p1.getX(), p2.getX()),
          FastMath.max(p1.getY(), p2.getY()));
    } else {
      Update_locations(g);
    }

    // System.out.println("ended.");

    return g;
  }
Exemple #18
0
  public void updateXValues(IScope scope, int chartCycle, int targetNb) {
    Object xval, xlab;
    if (this.useXSource || this.useXLabels) {

      if (this.useXSource) {
        xval = xsource.resolveAgainst(scope).value(scope);
      } else {
        xval = xlabels.resolveAgainst(scope).value(scope);
      }
      if (this.useXLabels) {
        xlab = xlabels.resolveAgainst(scope).value(scope);
      } else {
        xlab = xsource.resolveAgainst(scope).value(scope);
      }

      if (xval instanceof GamaList) {
        IList xv2 = Cast.asList(scope, xval);
        IList xl2 = Cast.asList(scope, xlab);

        if (this.useXSource && xv2.size() > 0 && xv2.get(0) instanceof Number) {
          XSeriesValues = new ArrayList<Double>();
          Xcategories = new ArrayList<String>();
          for (int i = 0; i < xv2.size(); i++) {
            XSeriesValues.add(new Double(Cast.asFloat(scope, xv2.get(i))));
            Xcategories.add(Cast.asString(scope, xl2.get(i)));
          }

        } else {
          if (xv2.size() > Xcategories.size()) {
            Xcategories = new ArrayList<String>();
            for (int i = 0; i < xv2.size(); i++) {
              if (i >= XSeriesValues.size()) {
                XSeriesValues.add(new Double(getXCycleOrPlusOneForBatch(scope, chartCycle)));
              }
              Xcategories.add(Cast.asString(scope, xl2.get(i)));
            }
          }
        }
        if (xv2.size() < targetNb) {
          throw GamaRuntimeException.error(
              "The x-serie length ("
                  + xv2.size()
                  + ") should NOT be shorter than any series length ("
                  + targetNb
                  + ") !",
              scope);
        }

      } else {
        if (this.useXSource && xval instanceof Number) {
          double dvalue = Cast.asFloat(scope, xval);
          String lvalue = Cast.asString(scope, xlab);
          XSeriesValues.add(new Double(dvalue));
          Xcategories.add(lvalue);
        }
        if (targetNb == -1 && !this.forceNoXAccumulate) targetNb = XSeriesValues.size() + 1;
        while (XSeriesValues.size() < targetNb) {
          XSeriesValues.add(new Double(getXCycleOrPlusOneForBatch(scope, chartCycle)));
          Xcategories.add(Cast.asString(scope, xlab));
        }
      }
    }

    if (!this.useXSource && !this.useXLabels) {
      if (targetNb == -1 && !this.forceNoXAccumulate && commonXindex >= XSeriesValues.size())
        targetNb = XSeriesValues.size() + 1;
      while (XSeriesValues.size() < targetNb) {
        addCommonXValue(scope, getXCycleOrPlusOneForBatch(scope, chartCycle));
      }
    }
  }
 public static GamaColor staticCast(
     final IScope scope, final Object obj, final Object param, final boolean copy)
     throws GamaRuntimeException {
   // param can contain the alpha value
   if (obj instanceof GamaColor) {
     GamaColor col = (GamaColor) obj;
     if (param instanceof Integer) {
       return new GamaColor(col.getRed(), col.getGreen(), col.getBlue(), (Integer) param);
     } else if (param instanceof Double) {
       return new GamaColor(col.getRed(), col.getGreen(), col.getBlue(), (Double) param);
     } else {
       return (GamaColor) obj;
     }
   }
   if (obj instanceof List) {
     List l = (List) obj;
     int size = l.size();
     if (size == 0) {
       return new GamaColor(Color.black);
     }
     if (size == 1 || size == 2) {
       return staticCast(scope, ((List) obj).get(0), param, copy);
     } else if (size == 3) {
       return new GamaColor(
           Cast.asInt(scope, l.get(0)),
           Cast.asInt(scope, l.get(1)),
           Cast.asInt(scope, l.get(2)),
           255);
     } else if (size >= 4) {
       return new GamaColor(
           Cast.asInt(scope, l.get(0)),
           Cast.asInt(scope, l.get(1)),
           Cast.asInt(scope, l.get(2)),
           Cast.asInt(scope, l.get(3)));
     }
     /* To allow constructions like rgb [255,255,255] */
   }
   if (obj instanceof IContainer) {
     return staticCast(
         scope, ((IContainer) obj).listValue(scope, Types.NO_TYPE, false), param, copy);
   }
   if (obj instanceof String) {
     String s = ((String) obj).toLowerCase();
     GamaColor c = GamaColor.colors.get(s);
     if (c == null) {
       try {
         c = new GamaColor(Color.decode(s));
       } catch (NumberFormatException e) {
         GamaRuntimeException ex =
             GamaRuntimeException.error("'" + s + "' is not a valid color name", scope);
         throw ex;
       }
       GamaColor.colors.put(s, c);
     }
     if (param == null) {
       return c;
     } else if (param instanceof Integer) {
       return new GamaColor(c, (Integer) param);
     } else if (param instanceof Double) {
       return new GamaColor(c, (Double) param);
     }
   }
   if (obj instanceof Boolean) {
     return (Boolean) obj ? new GamaColor(Color.black) : new GamaColor(Color.white);
   }
   int i = Cast.asInt(scope, obj);
   GamaColor gc = GamaColor.getInt((255 & 0xFF) << 24 | i & 0xFFFFFF << 0);
   if (param instanceof Integer) {
     return new GamaColor(gc, (Integer) param);
   } else if (param instanceof Double) {
     return new GamaColor(gc, (Double) param);
   }
   return gc;
 }