-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a method to cap the size of the cache in DijkstraShortestPaths and DijkstraDistance #80
Open
lhepto
wants to merge
1
commit into
jrtom:master
Choose a base branch
from
lhepto:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...orithms/src/test/java/edu/uci/ics/jung/algorithms/shortestpath/TestCachingLargeGraph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package edu.uci.ics.jung.algorithms.shortestpath; | ||
|
||
import edu.uci.ics.jung.graph.Graph; | ||
import edu.uci.ics.jung.graph.UndirectedGraph; | ||
import edu.uci.ics.jung.graph.UndirectedSparseGraph; | ||
import edu.uci.ics.jung.graph.util.EdgeType; | ||
import junit.framework.TestCase; | ||
import junit.framework.TestResult; | ||
import org.junit.Ignore; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Created by Lewis Heptonstall on 06/05/2017. | ||
* | ||
* Tests demonstrate the value of capping the max_cache_size when doing many, recurrent shortest paths, calculations | ||
* All tests fail when max_cache_size is not set with either OutOfMemory or GC Limit Exceeded. | ||
* All test should pass when max_cache_size set to 100 | ||
* Please set int vertex_count = 4000. | ||
* VM options used: -Xms128m -Xmx750m | ||
*/ | ||
public class TestCachingLargeGraph extends TestCase { | ||
|
||
Graph<String,Integer> ug; | ||
List<String> vertices; | ||
|
||
public void testAllPathsUnweighted(){ | ||
|
||
setUp(); | ||
|
||
DijkstraShortestPath<String,Integer> sp = new DijkstraShortestPath<String, Integer>(ug,true, 100); | ||
|
||
Map<String,Map<String,Number>> allSp = new HashMap<String, Map<String, Number>>(); | ||
|
||
// Find shortest path from every vertex to every vertex | ||
for (int v1 = 0; v1 < vertices.size(); v1++) { | ||
|
||
for (int v2 = 0; v2 < vertices.size(); v2 ++) { | ||
List<Integer> thisPath = sp.getPath(vertices.get(v1),vertices.get(v2)); | ||
} | ||
|
||
if (v1 % 100 == 0){ // Every 100 iterations, print progress | ||
System.out.println("Progress: " + new Double(v1)/vertices.size()); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
public void testDistanceMapUnweighted(){ | ||
|
||
setUp(); | ||
|
||
DijkstraShortestPath<String,Integer> sp = new DijkstraShortestPath<String, Integer>(ug, true, 100); | ||
|
||
Map<String,Map<String,Number>> allSp = new HashMap<String, Map<String, Number>>(); | ||
|
||
// Find shortest path distance from every vertex to every vertex | ||
for (int v1 = 0; v1 < vertices.size(); v1++) { | ||
Map<String, Number> test = sp.getDistanceMap(vertices.get(v1)); | ||
|
||
allSp.put(vertices.get(v1),test); | ||
|
||
if (v1 % 100 == 0){ // Every 100 iterations, print progress | ||
System.out.println("Progress: " + new Double(v1)/vertices.size()); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
@Override | ||
protected void setUp() { | ||
|
||
/* | ||
Builds a very large, sparsely connected, unweighted, undirected graph | ||
*/ | ||
|
||
ug = new UndirectedSparseGraph<String,Integer>(); | ||
vertices = new ArrayList<String>(); | ||
|
||
int vertex_count = 40; | ||
int max_connections_per_vertex = 30; | ||
Random random = new Random(); | ||
random.setSeed(1234); | ||
|
||
// Add vertex_count number of vertices to the list, with a random ID | ||
for (int v = 0; v < vertex_count; v++) { | ||
vertices.add(((Long) random.nextLong()).toString()); | ||
ug.addVertex(vertices.get(v)); | ||
} | ||
|
||
int e = 0; // edge iterator | ||
|
||
for (int v1 = 0; v1 < vertices.size(); v1++) { | ||
for (int v2 = v1 + 1; v2 < ((v1 + max_connections_per_vertex < vertices.size()) ? (v1 + max_connections_per_vertex) : vertices.size()); v2++){ | ||
|
||
ug.addEdge(e,vertices.get(v1),vertices.get(v2), EdgeType.UNDIRECTED); | ||
e++; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I missed a discussion of this, but is the cost (of a possible virtual lookup when declaring as the most general type) really a factor with modern JVMs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is a factor on modern JVMs, I've personally not heard of it. :/
IIRC, it's more idiomatic to keep the base type here as
Map
, i.e. whatever interface is most generic, or as high up the type hierarchy as possible, that has all the methods one needs.Just my 2 cents.