Exemplo n.º 1
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;
  }
Exemplo n.º 2
0
 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));
 }
  @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;
  }