Skip to content

Commit

Permalink
externalized random number generator seed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Patrikalakis committed Apr 8, 2016
1 parent 4a89379 commit 5a56f7a
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ private SortedMap<Integer, Double> clusteringBenchmark(GraphDatabaseType type) t
+ ", Cache Size: " + cacheSize);

Stopwatch watch = Stopwatch.createStarted();
LouvainMethod louvainMethodCache = new LouvainMethod(graphDatabase, cacheSize, bench.randomizedClustering());
LouvainMethod louvainMethodCache = new LouvainMethod(graphDatabase, cacheSize,
bench.randomizedClustering() ? bench.getRandom() : null);
louvainMethodCache.computeModularity();
timeMap.put(cacheSize, watch.elapsed(TimeUnit.MILLISECONDS) / 1000.0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@
public class FindShortestPathBenchmark extends PermutingBenchmarkBase implements RequiresGraphData
{

private final List<Integer> generatedNodes;

public FindShortestPathBenchmark(BenchmarkConfiguration config)
{
super(config, BenchmarkType.FIND_SHORTEST_PATH);
generatedNodes = config.getRandomNodeList();
}

@Override
public void benchmarkOne(GraphDatabaseType type, int scenarioNumber)
{
GraphDatabase<?,?,?,?> graphDatabase = Utils.createDatabaseInstance(bench, type, false /*batchLoading*/);
Stopwatch watch = Stopwatch.createStarted();
graphDatabase.shortestPaths(generatedNodes);
graphDatabase.shortestPaths();
graphDatabase.shutdown();
times.get(type).add((double) watch.elapsed(TimeUnit.MILLISECONDS));
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/eu/socialsensor/clustering/LouvainMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class LouvainMethod
{
boolean isRandomized;
private final Random random;
private double resolution = 1.0;
private double graphWeightSum;
private int N;
Expand All @@ -27,10 +27,10 @@ public class LouvainMethod
GraphDatabase<?,?,?,?> graphDatabase;
Cache cache;

public LouvainMethod(GraphDatabase<?,?,?,?> graphDatabase, int cacheSize, boolean isRandomized) throws ExecutionException
public LouvainMethod(GraphDatabase<?,?,?,?> graphDatabase, int cacheSize, Random random) throws ExecutionException
{
this.graphDatabase = graphDatabase;
this.isRandomized = isRandomized;
this.random = random;
initialize();
cache = new Cache(graphDatabase, cacheSize);
}
Expand All @@ -52,7 +52,6 @@ private void initialize()

public void computeModularity() throws ExecutionException
{
Random rand = new Random();
boolean someChange = true;
while (someChange)
{
Expand All @@ -62,9 +61,9 @@ public void computeModularity() throws ExecutionException
{
localChange = false;
int start = 0;
if (this.isRandomized)
if (null != this.random)
{
start = Math.abs(rand.nextInt()) % this.N;
start = Math.abs(random.nextInt()) % this.N;
}
int step = 0;
for (int i = start; step < this.N; i = (i + 1) % this.N)
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/eu/socialsensor/dataset/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,32 @@
public class Dataset implements Iterable<List<String>>
{
private final List<List<String>> data;
private final List<Integer> generatedNodes;

public Dataset(File datasetFile)
public Dataset(File datasetFile, Random random, int randomNodeSetSize)
{
data = Utils.readTabulatedLines(datasetFile, 4 /* numberOfLinesToSkip */);
final Set<Integer> nodes = new HashSet<>();
//read node strings and convert to Integers and add to HashSet
data.stream().forEach(line -> { //TODO evaluate parallelStream
line.stream().forEach(nodeId -> {
nodes.add(Integer.valueOf(nodeId.trim()));
});
});
if(randomNodeSetSize > nodes.size()) {
throw new IllegalArgumentException("cant select more random nodes than there are unique nodes in dataset");
}

//shuffle
final List<Integer> nodeList = new ArrayList<>(nodes);
Collections.shuffle(nodeList);

//choose randomNodeSetSize of them
generatedNodes = new ArrayList<Integer>(randomNodeSetSize);
Iterator<Integer> it = nodeList.iterator();
while(generatedNodes.size() < randomNodeSetSize) {
generatedNodes.add(it.next());
}
}

@Override
Expand All @@ -30,4 +52,7 @@ public Iterator<List<String>> iterator()
public List<List<String>> getList() {
return new ArrayList<List<String>>(data);
}
public List<Integer> getRandomNodes() {
return generatedNodes;
}
}
12 changes: 10 additions & 2 deletions src/main/java/eu/socialsensor/dataset/DatasetFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
*
Expand All @@ -28,11 +29,18 @@ public static DatasetFactory getInstance()
return theInstance;
}

public Dataset getDataset(File datasetFile)
public Dataset getDataset(File datasetFile) {
if (!datasetMap.containsKey(datasetFile))
{
throw new IllegalArgumentException("no mapping for data file " + datasetFile.getAbsolutePath());
}
return datasetMap.get(datasetFile);
}
public Dataset createAndGetDataset(File datasetFile, Random random, int randomNodeSetSize)
{
if (!datasetMap.containsKey(datasetFile))
{
datasetMap.put(datasetFile, new Dataset(datasetFile));
datasetMap.put(datasetFile, new Dataset(datasetFile, random, randomNodeSetSize));
}

return datasetMap.get(datasetFile);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/eu/socialsensor/graphdatabases/GraphDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ public interface GraphDatabase<VertexIteratorType, EdgeIteratorType, VertexType,

/**
* Find the shortest path between vertex 1 and each of the vertexes in the list
*
* @param nodes
*
* any number of random nodes
*/
public void shortestPaths(List<Integer> nodes);
public void shortestPaths();

/**
* Execute findShortestPaths query from the Query interface
*
* @param nodes
*
* @param fromNode
* @param toNode
* any number of random nodes
*/
public void shortestPath(final VertexType fromNode, Integer node);
public void shortestPath(final VertexType fromNode, Integer toNode);

/**
* @return the number of nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public abstract class GraphDatabaseBase<VertexIteratorType, EdgeIteratorType, Ve
public static final String COMMUNITY = "community";
public static final String NODE_LABEL = "node";
protected final File dbStorageDirectory;
protected final MetricRegistry metrics = new MetricRegistry();
protected final GraphDatabaseType type;
private final Timer nextVertexTimes;
private final Timer getNeighborsOfVertexTimes;
private final Timer nextEdgeTimes;
private final Timer getOtherVertexFromEdgeTimes;
private final Timer getAllEdgesTimes;
private final Timer shortestPathTimes;
private final List<Integer> randomNodes;

protected GraphDatabaseBase(GraphDatabaseType type, File dbStorageDirectory)
protected GraphDatabaseBase(GraphDatabaseType type, File dbStorageDirectory, List<Integer> randomNodes)
{
this.type = type;
final String queryTypeContext = type.getShortname() + QUERY_CONTEXT;
Expand All @@ -44,6 +44,7 @@ protected GraphDatabaseBase(GraphDatabaseType type, File dbStorageDirectory)
this.getOtherVertexFromEdgeTimes = GraphDatabaseBenchmark.metrics.timer(queryTypeContext + "getOtherVertexFromEdge");
this.getAllEdgesTimes = GraphDatabaseBenchmark.metrics.timer(queryTypeContext + "getAllEdges");
this.shortestPathTimes = GraphDatabaseBenchmark.metrics.timer(queryTypeContext + "shortestPath");
this.randomNodes = randomNodes;

this.dbStorageDirectory = dbStorageDirectory;
if (!this.dbStorageDirectory.exists())
Expand Down Expand Up @@ -126,12 +127,11 @@ public void findNodesOfAllEdges() {
}

@Override
public void shortestPaths(List<Integer> nodes) {
public void shortestPaths() {
//randomness of selected node comes from the hashing function of hash set
final Iterator<Integer> it = nodes.iterator();
final Iterator<Integer> it = randomNodes.iterator();
Preconditions.checkArgument(it.hasNext());
final VertexType from = getVertex(it.next());
it.remove();//now the set has n-1 nodes
Timer.Context ctxt;
while(it.hasNext()) {
final Integer i = it.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public static enum RelTypes implements RelationshipType

public static Label NODE_LABEL = DynamicLabel.label("Node");

public Neo4jGraphDatabase(File dbStorageDirectoryIn, boolean batchLoading)
public Neo4jGraphDatabase(File dbStorageDirectoryIn, boolean batchLoading, List<Integer> randomNodes)
{
super(GraphDatabaseType.NEO4J, dbStorageDirectoryIn);
super(GraphDatabaseType.NEO4J, dbStorageDirectoryIn, randomNodes);

if(batchLoading) {
neo4jGraph = null;
Expand Down Expand Up @@ -183,9 +183,9 @@ public void shutdownMassiveGraph()
}

@Override
public void shortestPaths(List<Integer> nodes) {
public void shortestPaths() {
try (Transaction tx = ((Neo4jGraphDatabase) this).neo4jGraph.beginTx()) {
super.shortestPaths(nodes);
super.shortestPaths();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class OrientGraphDatabase extends GraphDatabaseBase<Iterator<Vertex>, Ite
@SuppressWarnings("deprecation")
public OrientGraphDatabase(BenchmarkConfiguration config, File dbStorageDirectoryIn)
{
super(GraphDatabaseType.ORIENT_DB, dbStorageDirectoryIn);
super(GraphDatabaseType.ORIENT_DB, dbStorageDirectoryIn, config.getRandomNodeList());
OGlobalConfiguration.STORAGE_COMPRESSION_METHOD.setValue("nothing");
OGlobalConfiguration.STORAGE_KEEP_OPEN.setValue(false);
graph = getGraph(dbStorageDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class TitanGraphDatabase extends GraphDatabaseBase<Iterator<Vertex>, Iter
public TitanGraphDatabase(GraphDatabaseType type, BenchmarkConfiguration config, File dbStorageDirectory,
boolean batchLoading)
{
super(type, dbStorageDirectory);
super(type, dbStorageDirectory, config.getRandomNodeList());
this.config = config;
if (!GraphDatabaseType.TITAN_FLAVORS.contains(type))
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/socialsensor/insert/InsertionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public final void createGraph(File datasetFile, int scenarioNumber)
{
logger.info("Loading data in {} mode in {} database . . . .", single ? "single" : "massive",
type.name());
Dataset dataset = DatasetFactory.getInstance().getDataset(datasetFile);
final Dataset dataset = DatasetFactory.getInstance().getDataset(datasetFile);

Stopwatch thousandWatch = Stopwatch.createStarted(), watch = Stopwatch.createStarted();

Expand Down
67 changes: 18 additions & 49 deletions src/main/java/eu/socialsensor/main/BenchmarkConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class BenchmarkConfiguration
private static final String CACHE_VALUES_COUNT = "cache-values-count";
private static final String PERMUTE_BENCHMARKS = "permute-benchmarks";
private static final String RANDOM_NODES = "shortest-path-random-nodes";
private static final String RANDOM_SEED = "random-seed";

private static final Set<String> metricsReporters = new HashSet<String>();
static {
Expand Down Expand Up @@ -80,7 +81,7 @@ public class BenchmarkConfiguration
private final boolean dynamodbConsistentRead;

// shortest path
private final int randomNodes;
private final int numShortestPathRandomNodes;

// clustering
private final Boolean randomizedClustering;
Expand All @@ -102,7 +103,8 @@ public class BenchmarkConfiguration
private final String dynamodbTablePrefix;
private final boolean customIds;
private final long tuplMinCacheSize;
private final List<Integer> randomNodeList;

private final Random random;

public String getDynamodbCredentialsFqClassName()
{
Expand All @@ -118,9 +120,6 @@ public String getDynamodbEndpoint()
{
return dynamodbEndpoint;
}
public List<Integer> getRandomNodeList() {
return randomNodeList;
}

public BenchmarkConfiguration(Configuration appconfig)
{
Expand Down Expand Up @@ -176,40 +175,11 @@ public BenchmarkConfiguration(Configuration appconfig)
dbStorageDirectory = new File(socialsensor.getString(DATABASE_STORAGE_DIRECTORY));
dataset = validateReadableFile(socialsensor.getString(DATASET), DATASET);


// load the dataset
// Set<String> nodes = new HashSet<String>();
// for (List<String> line : data.subList(4, data.size()))
// {
// for (String nodeId : line)
// {
// nodes.add(nodeId.trim());
// }
// }
//
// List<String> nodeList = new ArrayList<String>(nodes);
// int[] nodeIndexList = new int[nodeList.size()];
// for (int i = 0; i < nodeList.size(); i++)
// {
// nodeIndexList[i] = i;
// }
// MathArrays.shuffle(nodeIndexList);
//
// Set<Integer> generatedNodes = new HashSet<Integer>();
// for (int i = 0; i < numRandomNodes; i++)
// {
// generatedNodes.add(Integer.valueOf(nodeList.get(nodeIndexList[i])));
// }
//Use old logic for now
randomNodes = socialsensor.getInteger(RANDOM_NODES, new Integer(100));
final int max = 1000;
final int min = 2;
final Random rand = new Random(17);
final Set<Integer> generatedNodes = new HashSet<>();
while(generatedNodes.size() < randomNodes + 1) { //generate one more so that we can
generatedNodes.add(rand.nextInt((max - min) +1) + min);
}
randomNodeList = new LinkedList<>(generatedNodes);
DatasetFactory.getInstance().getDataset(dataset);
random = new Random(socialsensor.getInt(RANDOM_SEED, 17 /*default*/));
numShortestPathRandomNodes = socialsensor.getInteger(RANDOM_NODES, new Integer(101));
DatasetFactory.getInstance().createAndGetDataset(dataset, random, numShortestPathRandomNodes);

if (!socialsensor.containsKey(PERMUTE_BENCHMARKS))
{
Expand Down Expand Up @@ -393,26 +363,18 @@ public int getScenarios()
return scenarios;
}

private static final File validateReadableFile(String fileName, String fileType)
{
private static final File validateReadableFile(String fileName, String fileType) {
File file = new File(fileName);
if (!file.exists())
{
if (!file.exists()) {
throw new IllegalArgumentException(String.format("the %s does not exist", fileType));
}

if (!(file.isFile() && file.canRead()))
{
if (!(file.isFile() && file.canRead())) {
throw new IllegalArgumentException(String.format("the %s must be a file that this user can read", fileType));
}
return file;
}

public int getRandomNodes()
{
return randomNodes;
}

public long getCsvReportingInterval()
{
return csvReportingInterval;
Expand Down Expand Up @@ -480,4 +442,11 @@ public boolean isCustomIds() {
public long getTuplMinCacheSize() {
return tuplMinCacheSize;
}

public Random getRandom() {
return random;
}
public List<Integer> getRandomNodeList() {
return DatasetFactory.getInstance().getDataset(this.dataset).getRandomNodes();
}
}
2 changes: 1 addition & 1 deletion src/main/java/eu/socialsensor/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static final GraphDatabase<?,?,?,?> createDatabaseInstance(BenchmarkConfi
}
else if (GraphDatabaseType.NEO4J == type)
{
graphDatabase = new Neo4jGraphDatabase(dbStorageDirectory, batchLoading);
graphDatabase = new Neo4jGraphDatabase(dbStorageDirectory, batchLoading, config.getRandomNodeList());
}
else if (GraphDatabaseType.ORIENT_DB == type)
{
Expand Down
Loading

0 comments on commit 5a56f7a

Please sign in to comment.