コード例 #1
1
ファイル: RFile.java プロジェクト: usuallycwdillon/gama
  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);
    }
  }
コード例 #2
0
 @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;
 }
コード例 #3
0
ファイル: Clustering.java プロジェクト: trungpv88/gama
  @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;
  }
コード例 #4
0
ファイル: Clustering.java プロジェクト: trungpv88/gama
  @operator(
      value = {"clustering_farthestFirst"},
      content_type = IType.LIST,
      category = {IOperatorCategory.STATISTICAL},
      concept = {IConcept.STATISTIC})
  @doc(
      value =
          "A list of agent groups clustered by Farthest First Algorithm based on the given attributes. Some paremeters can be defined: "
              + "num_clusters: the number of clusters",
      examples = {
        @example(
            value =
                "clustering_farthestFirst([ag1, ag2, ag3, ag4, ag5],[\"size\",\"age\", \"weight\"],[\"num_clusters\"::3])",
            equals = "for example, can return [[ag1, ag3], [ag2], [ag4, ag5]]",
            isExecutable = false)
      },
      see = {
        "clustering_xmeans",
        "clustering_simple_kmeans",
        "clustering_em",
        "clustering_DBScan",
        "clustering_cobweb"
      })
  public static IList<IList<IAgent>> primClusteringFarthestFirst(
      final IScope scope,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents,
      final IList<String> attributes,
      final GamaMap<String, Object> parameters) {
    FarthestFirst ff = new FarthestFirst();
    ff.setSeed(Cast.asInt(scope, scope.getRandom().getSeed()));

    if (parameters != null) {
      try {
        if (parameters.containsKey("num_clusters")) {
          ff.setNumClusters(Cast.asInt(scope, parameters.get("num_clusters")));
        }
      } catch (Exception e) {

      }
    }
    IList<IList<IAgent>> groupes = clusteringUsingWeka(scope, ff, attributes, agents);
    return groupes;
  }
コード例 #5
0
ファイル: Clustering.java プロジェクト: trungpv88/gama
  @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;
  }
コード例 #6
0
ファイル: RFile.java プロジェクト: usuallycwdillon/gama
  public void doRFileEvaluate(final IScope scope) {
    final String RFile = getPath();
    try {
      // Call R
      RCaller caller = new RCaller();

      String RPath = GamaPreferences.LIB_R.value(scope).getPath();
      caller.setRscriptExecutable(RPath);
      // caller.setRscriptExecutable("\"" + RPath + "\"");
      // if(java.lang.System.getProperty("os.name").startsWith("Mac"))
      // {
      // caller.setRscriptExecutable(RPath);
      // }

      RCode c = new RCode();
      List<String> R_statements = new ArrayList<String>();

      // tmthai.begin----------------------------------------------------------------------------
      String fullPath = FileUtils.constructAbsoluteFilePath(scope, RFile, true);
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute.RScript:" + RPath);
        GuiUtils.debug("Stats.R_compute.RFile:" + RFile);
        GuiUtils.debug("Stats.R_compute.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);
        if (DEBUG) {
          GuiUtils.debug("Stats.R_compute.statement:" + statement);
        }
      }

      fr.close();
      br.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);
      for (String name : caller.getParser().getNames()) {
        Object[] results = null;
        results = caller.getParser().getAsStringArray(name);
        // for (int i = 0; i < results.length; i++) {
        // java.lang.System.out.println(results[i]);
        // }
        if (DEBUG) {
          GuiUtils.debug(
              "Stats.R_compute_param.caller.Name: '"
                  + name
                  + "' length: "
                  + results.length
                  + " - Value: "
                  + results.toString());
        }
        result.put(name, GamaListFactory.createWithoutCasting(Types.NO_TYPE, results));
      }
      if (DEBUG) {
        GuiUtils.debug("Stats.R_compute.return:" + result.serialize(false));
      }
      // return result;
      setBuffer(result);

    } catch (Exception ex) {

      throw GamaRuntimeException.error("RCallerExecutionException " + ex.getMessage(), scope);
    }
  }
コード例 #7
0
ファイル: Clustering.java プロジェクト: trungpv88/gama
  @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;
  }
コード例 #8
0
ファイル: Clustering.java プロジェクト: trungpv88/gama
  @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;
  }