Example #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);
    }
  }
Example #2
0
  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);
            }
          }
        }
      }
    }
  }