|
| 1 | +package org.nlogo.extensions.dbscan; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.Iterator; |
| 6 | + |
| 7 | +import org.christopherfrantz.dbscan.DBSCANClusterer; |
| 8 | +import org.christopherfrantz.dbscan.DBSCANClusteringException; |
| 9 | +import org.nlogo.agent.Agent; |
| 10 | +import org.nlogo.agent.Turtle; |
| 11 | +import org.nlogo.api.AgentSet; |
| 12 | +import org.nlogo.api.Argument; |
| 13 | +import org.nlogo.api.Context; |
| 14 | +import org.nlogo.api.DefaultReporter; |
| 15 | +import org.nlogo.api.ExtensionException; |
| 16 | +import org.nlogo.api.LogoException; |
| 17 | +import org.nlogo.api.LogoListBuilder; |
| 18 | +import org.nlogo.api.PrimitiveManager; |
| 19 | +import org.nlogo.api.Syntax; |
| 20 | +import org.nlogo.extensions.dbscan.metrics.DistanceMetricNetLogoAgents; |
| 21 | +import org.nlogo.extensions.dbscan.metrics.DistanceMetricNetLogoLocation; |
| 22 | + |
| 23 | +/** |
| 24 | + * NetLogo extension for clustering based on DBSCAN by agent variables or coordinates. |
| 25 | + * |
| 26 | + * @author Christopher Frantz <[email protected]> |
| 27 | + * |
| 28 | + */ |
| 29 | +public class DBSCANExtension extends org.nlogo.api.DefaultClassManager { |
| 30 | + |
| 31 | + @Override |
| 32 | + public void load(PrimitiveManager primitiveManager) throws ExtensionException { |
| 33 | + primitiveManager.addPrimitive("cluster-by-variable", new DbscanNetlogoVariableClusterer()); |
| 34 | + primitiveManager.addPrimitive("cluster-by-location", new DbscanNetlogoCoordinateClusterer()); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Clusters based on agent variables |
| 39 | + * @author Christopher Frantz <[email protected]> |
| 40 | + */ |
| 41 | + public static class DbscanNetlogoVariableClusterer extends DefaultReporter { |
| 42 | + |
| 43 | + @Override |
| 44 | + public Syntax getSyntax() { |
| 45 | + //Inputs: values to be clustered, property to be clustered on, minimum number of elements, maximum distance |
| 46 | + int[] input = new int[] {Syntax.AgentsetType(), Syntax.StringType(), Syntax.NumberType(), Syntax.NumberType()}; |
| 47 | + int ret = Syntax.ListType(); |
| 48 | + return Syntax.reporterSyntax(input, ret); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Object report(Argument[] args, Context ctx) |
| 53 | + throws ExtensionException, LogoException { |
| 54 | + |
| 55 | + AgentSet inputValues = null; |
| 56 | + String field = null; |
| 57 | + int minNumberOfElements = -1; |
| 58 | + double maxDistance = -1; |
| 59 | + LogoListBuilder list = new LogoListBuilder(); |
| 60 | + |
| 61 | + try { |
| 62 | + inputValues = args[0].getAgentSet(); |
| 63 | + field = args[1].getString(); |
| 64 | + minNumberOfElements = args[2].getIntValue(); |
| 65 | + maxDistance = args[3].getDoubleValue(); |
| 66 | + } catch (Exception e) { |
| 67 | + throw new ExtensionException(e.getMessage()); |
| 68 | + } |
| 69 | + |
| 70 | + if (minNumberOfElements == -1) { |
| 71 | + throw new ExtensionException("Minimum number of cluster elements has not been defined."); |
| 72 | + } |
| 73 | + |
| 74 | + if (maxDistance == -1) { |
| 75 | + throw new ExtensionException("Maximum distance of cluster variable has not been defined."); |
| 76 | + } |
| 77 | + |
| 78 | + // Convert input agentset to collection |
| 79 | + Collection<Agent> inputCollection = new ArrayList<>(); |
| 80 | + Iterator it = inputValues.agents().iterator(); |
| 81 | + while (it.hasNext()) { |
| 82 | + inputCollection.add((Agent) it.next()); |
| 83 | + } |
| 84 | + |
| 85 | + // Perform clustering |
| 86 | + ArrayList<ArrayList<Agent>> tmpList = null; |
| 87 | + |
| 88 | + try { |
| 89 | + DBSCANClusterer<Agent> clusterer = new DBSCANClusterer<Agent>(inputCollection, minNumberOfElements, maxDistance, new DistanceMetricNetLogoAgents(field.toUpperCase())); |
| 90 | + tmpList = clusterer.performClustering(); |
| 91 | + } catch (DBSCANClusteringException e) { |
| 92 | + throw new ExtensionException(e); |
| 93 | + } |
| 94 | + |
| 95 | + // Convert generated lists of clusters to nested LogoList |
| 96 | + for (ArrayList<Agent> intList: tmpList) { |
| 97 | + LogoListBuilder internalBuilder = new LogoListBuilder(); |
| 98 | + for (Agent agent: intList) { |
| 99 | + internalBuilder.add(agent); |
| 100 | + } |
| 101 | + list.add(internalBuilder.toLogoList()); |
| 102 | + } |
| 103 | + return list.toLogoList(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Clusters based on coordinates |
| 109 | + * @author Christopher Frantz <[email protected]> |
| 110 | + * |
| 111 | + */ |
| 112 | + public static class DbscanNetlogoCoordinateClusterer extends DefaultReporter { |
| 113 | + |
| 114 | + @Override |
| 115 | + public Syntax getSyntax() { |
| 116 | + //Inputs: values to be clustered, minimum number of elements, maximum distance |
| 117 | + int[] input = new int[] {Syntax.AgentsetType(), Syntax.NumberType(), Syntax.NumberType()}; |
| 118 | + int ret = Syntax.ListType(); |
| 119 | + return Syntax.reporterSyntax(input, ret); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Object report(Argument[] args, Context ctx) |
| 124 | + throws ExtensionException, LogoException { |
| 125 | + |
| 126 | + AgentSet inputValues = null; |
| 127 | + int minNumberOfElements = -1; |
| 128 | + double maxDistance = -1; |
| 129 | + LogoListBuilder list = new LogoListBuilder(); |
| 130 | + |
| 131 | + try { |
| 132 | + inputValues = args[0].getAgentSet(); |
| 133 | + minNumberOfElements = args[1].getIntValue(); |
| 134 | + maxDistance = args[2].getDoubleValue(); |
| 135 | + } catch (Exception e) { |
| 136 | + throw new ExtensionException(e.getMessage()); |
| 137 | + } |
| 138 | + |
| 139 | + if (minNumberOfElements == -1) { |
| 140 | + throw new ExtensionException("Minimum number of cluster elements has not been defined."); |
| 141 | + } |
| 142 | + |
| 143 | + if (maxDistance == -1) { |
| 144 | + throw new ExtensionException("Maximum distance of cluster variable has not been defined."); |
| 145 | + } |
| 146 | + |
| 147 | + // Convert input agentset to collection |
| 148 | + Collection<Turtle> inputCollection = new ArrayList<>(); |
| 149 | + Iterator it = inputValues.agents().iterator(); |
| 150 | + while (it.hasNext()) { |
| 151 | + inputCollection.add((Turtle) it.next()); |
| 152 | + } |
| 153 | + |
| 154 | + // Perform clustering |
| 155 | + ArrayList<ArrayList<Turtle>> tmpList = null; |
| 156 | + |
| 157 | + try { |
| 158 | + DBSCANClusterer<Turtle> clusterer = new DBSCANClusterer<Turtle>(inputCollection, minNumberOfElements, maxDistance, new DistanceMetricNetLogoLocation()); |
| 159 | + tmpList = clusterer.performClustering(); |
| 160 | + } catch (DBSCANClusteringException e) { |
| 161 | + throw new ExtensionException(e); |
| 162 | + } |
| 163 | + |
| 164 | + // Convert generated lists of clusters to nested LogoList |
| 165 | + for (ArrayList<Turtle> intList: tmpList) { |
| 166 | + LogoListBuilder internalBuilder = new LogoListBuilder(); |
| 167 | + for (Turtle agent: intList) { |
| 168 | + internalBuilder.add(agent); |
| 169 | + } |
| 170 | + list.add(internalBuilder.toLogoList()); |
| 171 | + } |
| 172 | + return list.toLogoList(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments