[virtualcommons-svn] SF.net SVN: virtualcommons:[426] foraging/trunk/src/main/java/edu/asu/ common
Status: Beta
Brought to you by:
alllee
From: <al...@us...> - 2010-01-07 05:17:14
|
Revision: 426 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=426&view=rev Author: alllee Date: 2010-01-07 05:17:04 +0000 (Thu, 07 Jan 2010) Log Message: ----------- added aggregate spatial distribution Modified Paths: -------------- foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java Modified: foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java =================================================================== --- foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java 2010-01-05 20:36:23 UTC (rev 425) +++ foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java 2010-01-07 05:17:04 UTC (rev 426) @@ -57,6 +57,7 @@ // new AllDataProcessor(), new AggregateTimeIntervalProcessor(), // new SummaryProcessor(), + new AggregateTokenSpatialDistributionProcessor(), // new CollectedTokenSpatialDistributionProcessor(), // new MovementStatisticsProcessor(), new AggregateCollectedTokenNeighborProcessor() @@ -217,7 +218,7 @@ } } } - + private static class CollectedTokenSpatialDistributionProcessor extends SaveFileProcessor.Base { @Override public void process(SavedRoundData savedRoundData, PrintWriter writer) { @@ -233,36 +234,37 @@ TokenCollectedEvent tokenCollectedEvent = (TokenCollectedEvent) event; Point point = tokenCollectedEvent.getLocation(); Identifier id = tokenCollectedEvent.getId(); - ClientSpatialDistribution clientDistribution = clientSpatialDistributionMap.get(id); - clientDistribution.columnCounts[point.x]++; - clientDistribution.rowCounts[point.y]++; - clientDistribution.tokens++; + ClientSpatialDistribution spatialDistribution = clientSpatialDistributionMap.get(id); + spatialDistribution.columnCounts[point.x]++; + spatialDistribution.rowCounts[point.y]++; + spatialDistribution.tokens++; } } // calculate for group - ArrayList<GroupDataModel> groups = new ArrayList<GroupDataModel>(serverDataModel.getGroups()); + writeData(writer, serverDataModel, clientSpatialDistributionMap); + } + + private void writeData( + PrintWriter writer, + ServerDataModel serverDataModel, + Map<Identifier, ClientSpatialDistribution> clientSpatialDistributionMap) { + ArrayList<GroupDataModel> groups = new ArrayList<GroupDataModel>(serverDataModel.getGroups()); for (GroupDataModel group: groups) { - writer.println("Group # " + groups.indexOf(group) + "\n----------------------"); - writer.println("Identifier, # tokens, row stdev, column stdev"); + String groupLabel = "Group #" + groups.indexOf(group); + writer.println("Identifier, Group, # tokens, row stdev, column stdev"); double groupWeightedSpatialMetric = 0.0d; int totalTokens = 0; for (Identifier id: group.getClientIdentifiers()) { ClientSpatialDistribution spatialDistribution = clientSpatialDistributionMap.get(id); spatialDistribution.calculateStandardDeviation(); groupWeightedSpatialMetric += spatialDistribution.weightedSpatialMetric; - writer.println(String.format("%s, %s, %s, %s", id, spatialDistribution.tokens, spatialDistribution.rowStandardDeviation, spatialDistribution.columnStandardDeviation)); + writer.println(String.format("%s, %s, %s, %s, %s", id, groupLabel, spatialDistribution.tokens, spatialDistribution.rowStandardDeviation, spatialDistribution.columnStandardDeviation)); totalTokens += spatialDistribution.tokens; } groupWeightedSpatialMetric /= totalTokens; - writer.println(groupWeightedSpatialMetric); + writer.println(groupLabel + " weighted spatial metric: " + groupWeightedSpatialMetric); } - // for (Map.Entry<Identifier, ClientSpatialDistribution> entry: clientSpatialDistributionMap.entrySet()) { - // Identifier id = entry.getKey(); - // ClientSpatialDistribution spatialDistribution = entry.getValue(); - // spatialDistribution.calculateStandardDeviation(); - // writer.println(String.format("%s, %s", id, spatialDistribution.weightedSpatialMetric)); - // } - } + } @Override public String getOutputFileExtension() { @@ -270,6 +272,67 @@ } } + private static class AggregateTokenSpatialDistributionProcessor extends SaveFileProcessor.Base { + @Override + public void process(SavedRoundData savedRoundData, PrintWriter writer) { + ServerDataModel serverDataModel = (ServerDataModel) savedRoundData.getDataModel(); + SortedSet<PersistableEvent> actions = savedRoundData.getActions(); + Map<Identifier, ClientSpatialDistribution> clientSpatialDistributionMap = new HashMap<Identifier, ClientSpatialDistribution>(); + Dimension boardSize = new Dimension(serverDataModel.getBoardWidth(), serverDataModel.getBoardHeight()); + for (ClientData clientData : serverDataModel.getClientDataMap().values()) { + clientSpatialDistributionMap.put(clientData.getId(), new ClientSpatialDistribution(boardSize)); + } + for (PersistableEvent event : actions) { + int elapsedTime = savedRoundData.getElapsedTimeInSeconds(event); + if (isIntervalElapsed(elapsedTime)) { + writeData(writer, serverDataModel, clientSpatialDistributionMap); + } + if (event instanceof TokenCollectedEvent) { + TokenCollectedEvent tokenCollectedEvent = (TokenCollectedEvent) event; + Point point = tokenCollectedEvent.getLocation(); + Identifier id = tokenCollectedEvent.getId(); + ClientSpatialDistribution spatialDistribution = clientSpatialDistributionMap.get(id); + spatialDistribution.columnCounts[point.x]++; + spatialDistribution.rowCounts[point.y]++; + spatialDistribution.tokens++; + } + } + // write last interval out + writeData(writer, serverDataModel, clientSpatialDistributionMap); + } + + private void writeData( + PrintWriter writer, + ServerDataModel serverDataModel, + Map<Identifier, ClientSpatialDistribution> clientSpatialDistributionMap) { + ArrayList<GroupDataModel> groups = new ArrayList<GroupDataModel>(serverDataModel.getGroups()); + for (GroupDataModel group: groups) { + String groupLabel = "Group # " + groups.indexOf(group); + writer.println("Time, Identifier, Group, # tokens, row stdev, column stdev"); + double groupWeightedSpatialMetric = 0.0d; + int totalTokens = 0; + for (Identifier id: group.getClientIdentifiers()) { + ClientSpatialDistribution spatialDistribution = clientSpatialDistributionMap.get(id); + spatialDistribution.calculateStandardDeviation(); + groupWeightedSpatialMetric += spatialDistribution.weightedSpatialMetric; + writer.println(String.format("%d, %s, %s, %s, %s, %s", getIntervalEnd(), id, groupLabel, spatialDistribution.tokens, spatialDistribution.rowStandardDeviation, spatialDistribution.columnStandardDeviation)); + totalTokens += spatialDistribution.tokens; + } + groupWeightedSpatialMetric /= totalTokens; + writer.println(groupLabel + " weighted spatial metric: " + groupWeightedSpatialMetric); + } + // clear data after processing all groups. + for (ClientSpatialDistribution spatialDistribution : clientSpatialDistributionMap.values()) { + spatialDistribution.zeroRowColumnCounts(); + } + } + + @Override + public String getOutputFileExtension() { + return "-aggregated-spatial-distribution.txt"; + } + } + private static class ClientSpatialDistribution { private int[] rowCounts; private double standardizedRowDistribution; @@ -283,12 +346,18 @@ private ClientSpatialDistribution(Dimension boardSize) { rowCounts = new int[boardSize.height]; columnCounts = new int[boardSize.width]; + zeroRowColumnCounts(); + } + + public void zeroRowColumnCounts() { Arrays.fill(rowCounts, 0); Arrays.fill(columnCounts, 0); } + public String toString() { return String.format("tokens: %d, row: %s, col: %s", tokens, standardizedRowDistribution, standardizedColumnDistribution); } + private void calculateStandardDeviation() { rowStandardDeviation = stdDev(rowCounts); columnStandardDeviation = stdDev(columnCounts); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |