Thread: [virtualcommons-svn] SF.net SVN: virtualcommons:[47] foraging/trunk/src/main/java/edu/asu/ commons
Status: Beta
Brought to you by:
alllee
From: <al...@us...> - 2008-08-22 22:22:45
|
Revision: 47 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=47&view=rev Author: alllee Date: 2008-08-22 22:22:55 +0000 (Fri, 22 Aug 2008) Log Message: ----------- updated save file converter 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 2008-08-21 16:56:18 UTC (rev 46) +++ foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java 2008-08-22 22:22:55 UTC (rev 47) @@ -43,9 +43,9 @@ if (allSaveFilesDirectory.exists() && allSaveFilesDirectory.isDirectory()) { List<SaveFileProcessor> processors = new ArrayList<SaveFileProcessor>(); processors.addAll(Arrays.asList( - new ForagingSaveFileProcessor(), - new SummaryStatisticsProcessor(), - new ForagingSummarySaveFileProcessor())); + new AllDataProcessor(), + new AggregateTimeIntervalProcessor(), + new SummaryProcessor())); Persister.processSaveFiles(allSaveFilesDirectory, processors); return true; } @@ -60,7 +60,7 @@ convert(args[0]); } - private static class ForagingSummarySaveFileProcessor extends SaveFileProcessor.Base { + private static class SummaryProcessor extends SaveFileProcessor.Base { @Override public void process(SavedRoundData savedRoundData, PrintWriter writer) { ServerDataModel serverDataModel = (ServerDataModel) savedRoundData.getDataModel(); @@ -76,13 +76,19 @@ } */ List<GroupDataModel> groups = new ArrayList<GroupDataModel>(serverDataModel.getGroups()); - writer.println("Group #, Members, ..., Resources Left, Final Resource Positions"); for (GroupDataModel group: groups) { + int totalConsumedGroupTokens = 0; + ArrayList<String> clientTokens = new ArrayList<String>(); + for (ClientData data : group.getClientDataMap().values()) { + clientTokens.add(String.format("%s, %s", data.getId(), data.getTotalTokens())); + totalConsumedGroupTokens += data.getTotalTokens(); + } writer.println( String.format("%s, %s, %s, %s", "Group #" + groups.indexOf(group), - Utils.join(',', new TreeSet<Identifier>(group.getClientIdentifiers())), + Utils.join(',', clientTokens), group.getResourceDistributionSize(), + totalConsumedGroupTokens, Utils.join(',', group.getResourceDistribution().keySet()) )); } @@ -94,11 +100,11 @@ } } - private static class ForagingSaveFileProcessor extends SaveFileProcessor.Base { + private static class AllDataProcessor extends SaveFileProcessor.Base { @Override public String getOutputFileExtension() { - return "-foraging_all-data.txt"; + return "-all-data.txt"; } @Override @@ -113,8 +119,7 @@ } private void processData2d(SavedRoundData savedRoundData, PrintWriter writer) { - RoundConfiguration roundConfiguration = (RoundConfiguration) savedRoundData.getRoundParameters(); - System.err.println("is chat enabled? " + roundConfiguration.isChatEnabled() + roundConfiguration.getRoundNumber()); +// RoundConfiguration roundConfiguration = (RoundConfiguration) savedRoundData.getRoundParameters(); SortedSet<PersistableEvent> actions = savedRoundData.getActions(); ServerDataModel model = (ServerDataModel) savedRoundData.getDataModel(); Map<Identifier, ClientStats> clientStats = createClientStats(model); @@ -238,7 +243,7 @@ } - public static class SummaryStatisticsProcessor extends SaveFileProcessor.Base { + public static class AggregateTimeIntervalProcessor extends SaveFileProcessor.Base { private static int secondsPerInterval = 5; public void process(SavedRoundData savedRoundData, PrintWriter writer) { // populate the ordered identifiers, try directly from the participant tokens map that @@ -251,7 +256,22 @@ RoundConfiguration roundConfiguration = (RoundConfiguration) savedRoundData.getRoundParameters(); TreeSet<Identifier> orderedIdentifiers = new TreeSet<Identifier>(serverDataModel.getClientDataMap().keySet()); List<GroupDataModel> groups = new ArrayList<GroupDataModel>(serverDataModel.getGroups()); + +// XXX: not really any better than the below, and actually more inefficient to boot. Damn. +// List<String> foo = Utils.map(orderedIdentifiers, new Utils.MapOp<Identifier, String>() { +// public String apply(Identifier id) { +// return id + " movement"; +// } +// }); + + List<String> movementHeader = new ArrayList<String>(); + List<String> collectedTokensHeader = new ArrayList<String>(); + for (Identifier id: orderedIdentifiers) { + movementHeader.add(id + " moves"); + collectedTokensHeader.add(id + " tokens collected"); + } + // headers for average probability of a token for each group // List<String> tokenProbabilityGroupNumberHeader = new ArrayList<String>(); // headers for tokens left in each group @@ -275,9 +295,9 @@ // write out the header String header = Utils.join(',', "Period", // moves taken - Utils.join(',', orderedIdentifiers), + Utils.join(',', movementHeader), // tokens - Utils.join(',', orderedIdentifiers), + Utils.join(',', collectedTokensHeader), // group token probabilities // Utils.join(',', tokenProbabilityGroupNumberHeader), // group total tokens left @@ -430,5 +450,4 @@ return clientStats; } - -} +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <al...@us...> - 2009-05-13 00:57:32
|
Revision: 139 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=139&view=rev Author: alllee Date: 2009-05-13 00:57:19 +0000 (Wed, 13 May 2009) Log Message: ----------- adding chat request parsing and spatial distribution statistics 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 2009-05-04 20:29:59 UTC (rev 138) +++ foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java 2009-05-13 00:57:19 UTC (rev 139) @@ -1,5 +1,6 @@ package edu.asu.commons.foraging.util; +import java.awt.Dimension; import java.awt.Point; import java.io.File; import java.io.PrintWriter; @@ -37,6 +38,14 @@ import edu.asu.commons.net.Identifier; import edu.asu.commons.util.Utils; +/** + * $Id$ + * + * Save file processors for binary savefiles for the foraging experiment. + * + * @author <a href='mailto:All...@as...'>Allen Lee</a> + * @version $Rev$ + */ public class ForagingSaveFileConverter { @@ -48,7 +57,9 @@ new AllDataProcessor(), new AggregateTimeIntervalProcessor(), new SummaryProcessor(), - new MovementStatisticsProcessor())); + new CollectedTokenSpatialDistributionProcessor(), + new MovementStatisticsProcessor() + )); Persister.processSaveFiles(allSaveFilesDirectory, processors); return true; } @@ -82,6 +93,7 @@ MovementEvent movementEvent = (MovementEvent) event; Identifier id = movementEvent.getId(); GroupDataModel groupDataModel = serverDataModel.getGroup(id); + // only count movements when the resource count is > 0 if (resourceCountMap.get(groupDataModel) > 0) { clientStatisticsMap.get(id).move(movementEvent.getDirection()); } @@ -105,7 +117,7 @@ resourceCountMap.put(groupDataModel, resources - 1); } } - System.err.println("resource count map should be all zeroed out: " + resourceCountMap); +// System.err.println("resource count map: " + resourceCountMap); // tally their very last movement counts // (since ClientMovementStatistics only adds to the movement distribution when they change direction) for (ClientMovementStatistics summary: clientStatisticsMap.values()) { @@ -140,6 +152,9 @@ } + /** + * Helper class to keep track of client movements. + */ private static class ClientMovementStatistics { private final Identifier id; private Direction lastDirection; @@ -192,7 +207,89 @@ throw new RuntimeException("Identifier id: " + id + " -- allMoves: " + allMoves + " not equal to sum of all moves: " + sum); } } + } + + private static class CollectedTokenSpatialDistributionProcessor 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) { + if (event instanceof TokenCollectedEvent) { + 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++; + } + } + for (Map.Entry<Identifier, ClientSpatialDistribution> entry: clientSpatialDistributionMap.entrySet()) { + Identifier id = entry.getKey(); + ClientSpatialDistribution spatialDistribution = entry.getValue(); + spatialDistribution.calculateStandardDeviation(); + writer.println(String.format("%s, %s, %s", id, spatialDistribution.standardizedColumnDistribution, spatialDistribution.standardizedRowDistribution)); + System.err.println("spatial distribution for id: " + id + spatialDistribution); + } + } + + @Override + public String getOutputFileExtension() { + return "-spatial-distribution.txt"; + } + } + + private static class ClientSpatialDistribution { + private int[] rowCounts; + private double standardizedRowDistribution; + private int[] columnCounts; + private double standardizedColumnDistribution; + private int tokens = 0; + private double rowStandardDeviation; + private double columnStandardDeviation; + private ClientSpatialDistribution(Dimension boardSize) { + rowCounts = new int[boardSize.height]; + columnCounts = new int[boardSize.width]; + 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); + double averageTokens = (double) tokens / (double) rowCounts.length; + standardizedRowDistribution = (rowStandardDeviation / averageTokens); + standardizedColumnDistribution = (columnStandardDeviation / averageTokens); + } + + private double stdDev(int[] counts) { + // calculate mean + int totalTokensInRow = sum(counts); + double size = (double) counts.length; + double mean = (double) totalTokensInRow / size; + double sumOfSquares = 0; + for (int count : counts) { + double difference = count - mean; + sumOfSquares += (difference * difference); + } + return Math.sqrt(sumOfSquares / size); + } + private int sum(int[] count) { + int total = 0; + for (int tokens: count) { + total += tokens; + } + return total; + } } private static class SummaryProcessor extends SaveFileProcessor.Base { @@ -227,6 +324,45 @@ Utils.join(',', group.getResourceDistribution().keySet()) )); } + // FIXME: should update later once we fix rolling chat logs. + Map<GroupDataModel, SortedSet<ChatRequest>> chatRequestMap = new HashMap<GroupDataModel, SortedSet<ChatRequest>>(); + SortedSet<ChatRequest> allChatRequests = savedRoundData.getChatRequests(); + if (! allChatRequests.isEmpty()) { + ChatRequest first = allChatRequests.first(); + for (ChatRequest request: savedRoundData.getChatRequests()) { + // FIXME: hack to deal with rolling chat logs, eventually should only have per-round chat requests + // in savedRoundData. +// if (request.getCreationTime() < lastCreationTime) { +// continue; +// } + + GroupDataModel group = serverDataModel.getGroup(request.getSource()); + if (chatRequestMap.containsKey(group)) { + chatRequestMap.get(group).add(request); + } + else { + TreeSet<ChatRequest> chatRequests = new TreeSet<ChatRequest>(); + chatRequests.add(request); + chatRequestMap.put(group, chatRequests); + } + } + // FIXME: hack to deal with rolling chat logs + // set last creation time to the last chat request that occurred so in the next chat round + // we'll know if we repeated a chat request. +// ChatRequest last = allChatRequests.last(); +// lastCreationTime = last.getCreationTime(); +// System.err.println("last creation time: " + lastCreationTime + " - " + last); + for (GroupDataModel group: groups) { + SortedSet<ChatRequest> chatRequests = chatRequestMap.get(group); + if (chatRequests != null) { + writer.println("Group #" + groups.indexOf(group)); + for (ChatRequest request: chatRequests) { + writer.println(String.format("%s: %s (%s)", request.getSource(), request.toString(), (request.getCreationTime() - first.getCreationTime())/1000L)); + } + } + } + } + } @Override public String getOutputFileExtension() { @@ -257,14 +393,14 @@ // RoundConfiguration roundConfiguration = (RoundConfiguration) savedRoundData.getRoundParameters(); SortedSet<PersistableEvent> actions = savedRoundData.getActions(); ServerDataModel model = (ServerDataModel) savedRoundData.getDataModel(); - Map<Identifier, ClientStats> clientStats = createClientStats(model); + Map<Identifier, ClientMovementTokenCount> clientMovementTokenCounts = createClientMovementTokenCounts(model); List<GroupDataModel> groups = new ArrayList<GroupDataModel>(model.getGroups()); Map<Identifier, ClientData> clientDataMap = model.getClientDataMap(); for (PersistableEvent event: actions) { if (event instanceof MovementEvent) { MovementEvent movementEvent = (MovementEvent) event; ClientData clientData = clientDataMap.get(event.getId()); - ClientStats client = clientStats.get(event.getId()); + ClientMovementTokenCount client = clientMovementTokenCounts.get(event.getId()); client.moves++; GroupDataModel group = clientData.getGroupDataModel(); String line = String.format("%s, %s, %d, %d, %s, %s", @@ -280,7 +416,7 @@ else if (event instanceof TokenCollectedEvent) { TokenCollectedEvent tokenCollectedEvent = (TokenCollectedEvent) event; ClientData clientData = clientDataMap.get(event.getId()); - ClientStats client = clientStats.get(event.getId()); + ClientMovementTokenCount client = clientMovementTokenCounts.get(event.getId()); Point location = tokenCollectedEvent.getLocation(); client.tokens++; GroupDataModel group = clientData.getGroupDataModel(); @@ -314,7 +450,7 @@ private void processData3d(SavedRoundData savedRoundData, PrintWriter writer) { RoundConfiguration roundConfiguration = (RoundConfiguration) savedRoundData.getRoundParameters(); ServerDataModel dataModel = (ServerDataModel) savedRoundData.getDataModel(); - Map<Identifier, ClientStats> clientStatsMap = createClientStats(dataModel); + Map<Identifier, ClientMovementTokenCount> clientStatsMap = createClientMovementTokenCounts(dataModel); SortedSet<PersistableEvent> actions = savedRoundData.getActions(); for (PersistableEvent event: actions) { if (event instanceof ChatRequest) { @@ -340,7 +476,7 @@ } else if (event instanceof HarvestFruitRequest) { HarvestFruitRequest request = (HarvestFruitRequest) event; - ClientStats clientStats = clientStatsMap.get(event.getId()); + ClientMovementTokenCount clientStats = clientStatsMap.get(event.getId()); clientStats.tokens += roundConfiguration.getTokensPerFruits(); Resource resource = request.getResource(); String line = String.format("%s, %s, %d, %d, %d, %d, %d, %d, %s", @@ -358,7 +494,7 @@ else if (event instanceof HarvestResourceRequest) { HarvestResourceRequest request = (HarvestResourceRequest) event; Resource resource = request.getResource(); - ClientStats clientStats = clientStatsMap.get(event.getId()); + ClientMovementTokenCount clientStats = clientStatsMap.get(event.getId()); clientStats.tokens += roundConfiguration.calculateTokens(resource.getAge()); String line = String.format("%s, %s, %d, %d, %d, %d, %d, %d, %s", savedRoundData.toSecondString(event), @@ -384,7 +520,7 @@ // populate the ordered identifiers, try directly from the participant tokens map that // is persisted in later versions of the experiment. ServerDataModel serverDataModel = (ServerDataModel) savedRoundData.getDataModel(); - Map<Identifier, ClientStats> clientStatistics = createClientStats(serverDataModel); + Map<Identifier, ClientMovementTokenCount> clientStatistics = createClientMovementTokenCounts(serverDataModel); for (ClientData clientData: serverDataModel.getClientDataMap().values()) { clientData.initializePosition(); } @@ -461,7 +597,7 @@ List<Integer> tokensLeft = getTokensLeft(groups); List<Double> distances = getClientDistances(groups); for (Identifier id : orderedIdentifiers) { - ClientStats stats = clientStatistics.get(id); + ClientMovementTokenCount stats = clientStatistics.get(id); movesTaken.add(stats.moves); harvestedTokens.add(stats.tokens); stats.reset(); @@ -478,7 +614,7 @@ currentInterval++; } // next, process the current persistable event - ClientStats stats = clientStatistics.get(event.getId()); + ClientMovementTokenCount stats = clientStatistics.get(event.getId()); if (event instanceof MovementEvent) { MovementEvent movementEvent = (MovementEvent) event; serverDataModel.moveClient(movementEvent.getId(), movementEvent.getDirection()); @@ -567,7 +703,7 @@ } - private static class ClientStats { + private static class ClientMovementTokenCount { private int moves = 0; private int tokens = 0; public void reset() { @@ -576,12 +712,12 @@ } } - public static Map<Identifier, ClientStats> createClientStats(ServerDataModel model) { - Map<Identifier, ClientStats> clientStats = new HashMap<Identifier, ClientStats>(); + public static Map<Identifier, ClientMovementTokenCount> createClientMovementTokenCounts(ServerDataModel model) { + Map<Identifier, ClientMovementTokenCount> clientStats = new HashMap<Identifier, ClientMovementTokenCount>(); for (Identifier id: model.getClientDataMap().keySet()) { - clientStats.put(id, new ClientStats()); + clientStats.put(id, new ClientMovementTokenCount()); } return clientStats; } -} \ No newline at end of file +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <al...@us...> - 2009-05-19 17:42:41
|
Revision: 142 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=142&view=rev Author: alllee Date: 2009-05-19 17:42:25 +0000 (Tue, 19 May 2009) Log Message: ----------- adding new weighted spatial metric calculation to save file converter 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 2009-05-19 01:11:57 UTC (rev 141) +++ foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java 2009-05-19 17:42:25 UTC (rev 142) @@ -230,13 +230,29 @@ clientDistribution.tokens++; } } - for (Map.Entry<Identifier, ClientSpatialDistribution> entry: clientSpatialDistributionMap.entrySet()) { - Identifier id = entry.getKey(); - ClientSpatialDistribution spatialDistribution = entry.getValue(); - spatialDistribution.calculateStandardDeviation(); - writer.println(String.format("%s, %s, %s", id, spatialDistribution.standardizedColumnDistribution, spatialDistribution.standardizedRowDistribution)); - System.err.println("spatial distribution for id: " + id + spatialDistribution); + // calculate for group + 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"); + 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)); + totalTokens += spatialDistribution.tokens; + } + groupWeightedSpatialMetric /= totalTokens; + writer.println(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 @@ -250,10 +266,11 @@ private double standardizedRowDistribution; private int[] columnCounts; private double standardizedColumnDistribution; - private int tokens = 0; private double rowStandardDeviation; private double columnStandardDeviation; + private double weightedSpatialMetric; + private ClientSpatialDistribution(Dimension boardSize) { rowCounts = new int[boardSize.height]; columnCounts = new int[boardSize.width]; @@ -269,6 +286,7 @@ double averageTokens = (double) tokens / (double) rowCounts.length; standardizedRowDistribution = (rowStandardDeviation / averageTokens); standardizedColumnDistribution = (columnStandardDeviation / averageTokens); + weightedSpatialMetric = tokens * (rowStandardDeviation + columnStandardDeviation); } private double stdDev(int[] counts) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <al...@us...> - 2009-08-04 06:05:29
|
Revision: 213 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=213&view=rev Author: alllee Date: 2009-08-04 06:05:21 +0000 (Tue, 04 Aug 2009) Log Message: ----------- quicktime movie creation appears to work. still need to tweak frame rate, should create a frame for each movement/token collection 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 2009-08-03 19:34:52 UTC (rev 212) +++ foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java 2009-08-04 06:05:21 UTC (rev 213) @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -19,8 +20,8 @@ import java.util.SortedSet; import java.util.TreeSet; -import javax.imageio.ImageIO; import javax.swing.JFrame; +import javax.swing.SwingUtilities; import edu.asu.commons.event.ChatRequest; import edu.asu.commons.event.PersistableEvent; @@ -95,69 +96,107 @@ public MovieCreatorProcessor(VideoFormat videoFormat) { this.videoFormat = videoFormat; } - + @Override public void process(SavedRoundData savedRoundData, OutputStream stream) { - try { - // hmm, there needs to be one output stream per group because we write 1 video per group. -// QuickTimeOutputStream quickTimeOutputStream = new QuickTimeOutputStream(stream, videoFormat); - ServerDataModel serverDataModel = (ServerDataModel) savedRoundData.getDataModel(); - RoundConfiguration roundConfiguration = (RoundConfiguration) savedRoundData.getRoundParameters(); - List<GroupView> groupViewList = new ArrayList<GroupView>(); - serverDataModel.reinitialize(); - List<JFrame> jframes = new ArrayList<JFrame>(); - Dimension dimension = new Dimension(800, 600); -// int i = 0; - for (GroupDataModel groupDataModel: serverDataModel.getGroups()) { - GroupView groupView = new GroupView(dimension, groupDataModel); - groupView.setup(roundConfiguration); - groupViewList.add(groupView); - JFrame jframe = new JFrame("Group: " + groupViewList.size()); - jframe.setPreferredSize(dimension); - jframe.add(groupView); - jframe.pack(); - jframe.setVisible(true); - jframes.add(jframe); - } - int secondHasPassedCheck = -1; - // grab out all add client events to initialize the state of the game properly. - for (PersistableEvent event: savedRoundData.getActions()) { - if (event instanceof AddClientEvent) { - serverDataModel.apply(event); - } - } - for (PersistableEvent event: savedRoundData.getActions()) { - int elapsedTimeInSeconds = savedRoundData.getElapsedTimeInSeconds(event); - System.err.println("elapsed time in seconds: " + elapsedTimeInSeconds); - serverDataModel.apply(event); - if (elapsedTimeInSeconds > secondHasPassedCheck) { - secondHasPassedCheck = elapsedTimeInSeconds; - // take a snapshot of each group... - for (GroupView groupView: groupViewList) { - Graphics2D graphics = (Graphics2D) groupView.getGraphics(); - BufferedImage bufferedImage = graphics.getDeviceConfiguration().createCompatibleImage(800, 600); -// write a frame to the quicktime movie... -// quickTimeOutputStream.writeFrame(bufferedImage, 40); - // write buffered image out to named sequentially. - ImageIO.write(bufferedImage, videoFormat.name(), new File(elapsedTimeInSeconds + "-group-" + groupViewList.indexOf(groupView) + "." + videoFormat.name())); - } - } - } - for (JFrame jframe : jframes) { - jframe.setVisible(false); - jframe.dispose(); - } - + // hmm, there needs to be one output stream per group because we write 1 video per group. + // QuickTimeOutputStream quickTimeOutputStream = new QuickTimeOutputStream(stream, videoFormat); + ServerDataModel serverDataModel = (ServerDataModel) savedRoundData.getDataModel(); + RoundConfiguration roundConfiguration = (RoundConfiguration) savedRoundData.getRoundParameters(); + final List<GroupView> groupViewList = new ArrayList<GroupView>(); + final Map<GroupView, QuickTimeOutputStream> groupViewMap = new HashMap<GroupView, QuickTimeOutputStream>(); + serverDataModel.reinitialize(); + List<JFrame> jframes = new ArrayList<JFrame>(); + Dimension dimension = new Dimension(800, 800); + File savedRoundDataFile = new File(savedRoundData.getSaveFilePath()); + String saveFilePath = savedRoundDataFile.getName(); + for (GroupDataModel groupDataModel: serverDataModel.getGroups()) { + GroupView groupView = new GroupView(dimension, groupDataModel); + groupView.setup(roundConfiguration); + groupViewList.add(groupView); + try { + File groupMovieFile = new File(savedRoundDataFile.getCanonicalPath() + "-group-" + groupViewList.size() + "-" + saveFilePath + ".mov"); + groupViewMap.put(groupView, new QuickTimeOutputStream(groupMovieFile, videoFormat)); + } + catch (IOException exception) { + exception.printStackTrace(); + } + JFrame jframe = new JFrame("Group: " + groupViewList.size()); + jframe.add(groupView); + jframe.pack(); + jframe.setVisible(true); + jframes.add(jframe); } - catch (IOException exception) { - exception.printStackTrace(); - throw new RuntimeException(exception); + int secondHasPassedCheck = -1; + // grab out all add client events to initialize the state of the game properly. + for (PersistableEvent event: savedRoundData.getActions()) { + if (event instanceof AddClientEvent) { + System.err.println("Adding client: " + event); + serverDataModel.apply(event); + } } - - + for (PersistableEvent event: savedRoundData.getActions()) { + final int elapsedTimeInSeconds = savedRoundData.getElapsedTimeInSeconds(event); + System.err.println("elapsed time in seconds: " + elapsedTimeInSeconds + ":" + event); + serverDataModel.apply(event); + if (elapsedTimeInSeconds > secondHasPassedCheck) { + secondHasPassedCheck = elapsedTimeInSeconds; + // take a snapshot of each group... + for (final GroupView groupView: groupViewList) { + try { +// groupView.repaint(); + final Dimension groupViewSize = groupView.getSize(); + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + Graphics2D graphics = (Graphics2D) groupView.getGraphics(); + + BufferedImage bufferedImage = graphics.getDeviceConfiguration().createCompatibleImage(groupViewSize.width, groupViewSize.height); + Graphics2D bufferedImageGraphics = bufferedImage.createGraphics(); + groupView.paint(bufferedImageGraphics); + try { + // currently each frame is 1 second.. maybe we can divvy this up so it's .5 seconds instead. + groupViewMap.get(groupView).writeFrame(bufferedImage, 600); + } + catch (IOException exception) { + exception.printStackTrace(); + throw new RuntimeException(exception); + } + + // write buffered image out to sequentially named file..? +// try { +// ImageIO.write(bufferedImage, videoFormat.name(), new File(elapsedTimeInSeconds + "-group-" + groupViewList.indexOf(groupView) + "." + videoFormat.name())); +// } +// catch (IOException e) { +// e.printStackTrace(); +// throw new RuntimeException(e); +// } + } + }); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + for (QuickTimeOutputStream out : groupViewMap.values()) { + try { + out.close(); + } + catch (IOException exception) { + exception.printStackTrace(); + } + } + for (JFrame jframe : jframes) { + jframe.setVisible(false); + jframe.dispose(); + } - } + @Override public void process(SavedRoundData savedRoundData, PrintWriter writer) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <al...@us...> - 2010-07-13 06:06:44
|
Revision: 523 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=523&view=rev Author: alllee Date: 2010-07-13 06:06:37 +0000 (Tue, 13 Jul 2010) Log Message: ----------- adding assigned number to client summary data 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-06-30 19:17:48 UTC (rev 522) +++ foraging/trunk/src/main/java/edu/asu/commons/foraging/util/ForagingSaveFileConverter.java 2010-07-13 06:06:37 UTC (rev 523) @@ -7,6 +7,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -216,6 +218,7 @@ public void validate() { int sum = 0; for (int i = 0; i < movementDistribution.length; i++) { + // i+1 to offset zero-based array index sum += ((i+1) * movementDistribution[i]); } if (allMoves != sum) { @@ -407,8 +410,15 @@ for (GroupDataModel group: groups) { int totalConsumedGroupTokens = 0; ArrayList<String> clientTokens = new ArrayList<String>(); - for (ClientData data : group.getClientDataMap().values()) { - clientTokens.add(String.format("%s, %s", data.getId(), data.getTotalTokens())); + ArrayList<ClientData> clientDataList = new ArrayList<ClientData>(group.getClientDataMap().values()); + Collections.sort(clientDataList, new Comparator<ClientData>() { + @Override + public int compare(ClientData a, ClientData b) { + return Integer.valueOf(a.getAssignedNumber()).compareTo(b.getAssignedNumber()); + } + }); + for (ClientData data : clientDataList) { + clientTokens.add(String.format("%s, %s, %s", data.getId(), data.getAssignedNumber(), data.getTotalTokens())); totalConsumedGroupTokens += data.getTotalTokens(); } writer.println( @@ -420,18 +430,11 @@ Utils.join(',', group.getResourceDistribution().keySet()) )); } - // FIXME: should update later once we fix rolling chat logs. Map<GroupDataModel, SortedSet<ChatRequest>> chatRequestMap = new HashMap<GroupDataModel, SortedSet<ChatRequest>>(); SortedSet<ChatRequest> allChatRequests = savedRoundData.getChatRequests(); if (! allChatRequests.isEmpty()) { ChatRequest first = allChatRequests.first(); for (ChatRequest request: savedRoundData.getChatRequests()) { - // FIXME: hack to deal with rolling chat logs, eventually should only have per-round chat requests - // in savedRoundData. - // if (request.getCreationTime() < lastCreationTime) { - // continue; - // } - GroupDataModel group = serverDataModel.getGroup(request.getSource()); if (chatRequestMap.containsKey(group)) { chatRequestMap.get(group).add(request); @@ -442,12 +445,6 @@ chatRequestMap.put(group, chatRequests); } } - // FIXME: hack to deal with rolling chat logs - // set last creation time to the last chat request that occurred so in the next chat round - // we'll know if we repeated a chat request. - // ChatRequest last = allChatRequests.last(); - // lastCreationTime = last.getCreationTime(); - // System.err.println("last creation time: " + lastCreationTime + " - " + last); for (GroupDataModel group: groups) { SortedSet<ChatRequest> chatRequests = chatRequestMap.get(group); if (chatRequests != null) { @@ -462,7 +459,6 @@ } @Override public String getOutputFileExtension() { - // TODO Auto-generated method stub return "-summary.txt"; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |