private static IList<IList<IAgent>> clusteringUsingWeka( final IScope scope, final Clusterer clusterer, final IList<String> attributes, final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents) throws GamaRuntimeException { Instances dataset = convertToInstances(scope, attributes, agents); try { clusterer.buildClusterer(dataset); IList<IList<IAgent>> groupes = GamaListFactory.create(Types.LIST.of(Types.AGENT)); for (int i = 0; i < clusterer.numberOfClusters(); i++) { groupes.add(GamaListFactory.<IAgent>create(Types.AGENT)); } for (int i = 0; i < dataset.numInstances(); i++) { Instance inst = dataset.instance(i); int clusterIndex = -1; clusterIndex = clusterer.clusterInstance(inst); IList<IAgent> groupe = groupes.get(clusterIndex); groupe.add(agents.get(scope, i)); } return groupes; } catch (Exception e) { return null; } }
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; }