From c01cab4eb4f677119cbd8a98e6d17b3a4080f612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Tue, 24 Feb 2015 21:22:04 +0100 Subject: [PATCH 01/62] close #114 : changed visibility of fields and methods from "private" to "protected" in subclasses of Algorithm --- .../hipster/algorithm/ADStarForward.java | 28 +++++++++---------- .../usc/citius/hipster/algorithm/AStar.java | 12 ++++---- .../citius/hipster/algorithm/BellmanFord.java | 12 ++++---- .../hipster/algorithm/BreadthFirstSearch.java | 8 +++--- .../hipster/algorithm/DepthFirstSearch.java | 16 +++++------ .../usc/citius/hipster/algorithm/IDAStar.java | 20 ++++++------- .../hipster/algorithm/MultiobjectiveLS.java | 11 ++++---- 7 files changed, 54 insertions(+), 53 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java index 8603270..826d131 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java @@ -63,9 +63,9 @@ */ public class ADStarForward, N extends es.usc.citius.hipster.model.ADStarNode> extends Algorithm { - private S begin; - private Collection goals; - private ADStarNodeExpander expander; + protected S begin; + protected Collection goals; + protected ADStarNodeExpander expander; /** * Create an instance of the algorithm with a begin, a goal and a component to @@ -103,15 +103,15 @@ public Iterator iterator() { */ public class Iterator implements java.util.Iterator { //queues used by the algorithm - private Map open; - private Map closed; - private Map incons; - private Iterable> transitionsChanged; - private Queue queue; - private final N beginNode; - private final Collection goalNodes; + protected Map open; + protected Map closed; + protected Map incons; + protected Iterable> transitionsChanged; + protected Queue queue; + protected final N beginNode; + protected final Collection goalNodes; - public Iterator() { + private Iterator() { //initialize nodes this.beginNode = expander.makeNode(null, new Transition(null, begin)); //initialize goal node collection @@ -146,7 +146,7 @@ public Iterator() { * * @param node instance of node to add */ - private void insertOpen(N node) { + protected void insertOpen(N node) { this.open.put(node.state(), node); this.queue.offer(node); } @@ -157,7 +157,7 @@ private void insertOpen(N node) { * * @return most promising node */ - private N takePromising() { + protected N takePromising() { while (!queue.isEmpty()) { N head = queue.peek(); if (!open.containsKey(head.state())) { @@ -174,7 +174,7 @@ private N takePromising() { * * @param node instance of node */ - private void updateQueues(N node) { + protected void updateQueues(N node) { S state = node.state(); if (node.getV().compareTo(node.getG()) != 0) { if (!this.closed.containsKey(state)) { diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java index 06cf084..6e1d196 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java @@ -43,8 +43,8 @@ */ public class AStar,N extends HeuristicNode> extends Algorithm { - private final N initialNode; - private final NodeExpander expander; + protected final N initialNode; + protected final NodeExpander expander; /** * Default constructor for ADStarForward. Requires the initial state, the successor function to generate @@ -67,9 +67,9 @@ public Iterator iterator() { * Internal iterator that implements all the logic of the A* search */ public class Iterator implements java.util.Iterator { - private Map open; - private Map closed; - private Queue queue; + protected Map open; + protected Map closed; + protected Queue queue; private Iterator() { open = new HashMap(); @@ -86,7 +86,7 @@ public boolean hasNext() { return !open.values().isEmpty(); } - private N takePromising() { + protected N takePromising() { // Poll until a valid state is found N node = queue.poll(); while (!open.containsKey(node.state())) { diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java index a0067b8..a1266bc 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java @@ -46,8 +46,8 @@ * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ public class BellmanFord,N extends CostNode> extends Algorithm { - private N initialNode; - private NodeExpander nodeExpander; + protected N initialNode; + protected NodeExpander nodeExpander; public BellmanFord(N initialNode, NodeExpander nodeExpander) { this.initialNode = initialNode; @@ -60,8 +60,8 @@ public BellmanFord(N initialNode, NodeExpander nodeExpander) { * when the queue is fully processed. */ public class Iterator implements java.util.Iterator { - private Queue queue; - private Map explored; + protected Queue queue; + protected Map explored; private Iterator(){ this.queue = new HashQueue(); @@ -76,7 +76,7 @@ private Iterator(){ * * @param node node to update the queue status */ - private void enqueue(N node) { + protected void enqueue(N node) { S state = node.state(); if (!this.queue.contains(state)) { this.queue.add(state); @@ -90,7 +90,7 @@ private void enqueue(N node) { * * @return node of the processing queue head */ - private N dequeue() { + protected N dequeue() { S state = this.queue.poll(); return this.explored.get(state); } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java index 9f678c4..296b892 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java @@ -39,8 +39,8 @@ * @param type of the heuristic search node used. */ public class BreadthFirstSearch> extends Algorithm { - private final N initialNode; - private final NodeExpander expander; + protected final N initialNode; + protected final NodeExpander expander; public BreadthFirstSearch(N initialNode, NodeExpander expander) { this.initialNode = initialNode; @@ -51,8 +51,8 @@ public BreadthFirstSearch(N initialNode, NodeExpander expander) { * Implements all the BFS search logic as an iterator */ public class Iterator implements java.util.Iterator { - private Queue queue = new LinkedList(); - private Map visited = new HashMap(); + protected Queue queue = new LinkedList(); + protected Map visited = new HashMap(); /** * Iterator cannot be instantiated from outside. diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java index 6edffb8..513b186 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java @@ -37,8 +37,8 @@ * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ public class DepthFirstSearch> extends Algorithm { - private N initialNode; - private NodeExpander expander; + protected N initialNode; + protected NodeExpander expander; // TODO; DRY common structures with other algorithms (like IDA) @@ -73,10 +73,10 @@ private class StackFrameNode { * DFS iterator used to expand always the deepest non-visited node. */ public class Iterator implements java.util.Iterator { - private Stack stack = new Stack(); - private StackFrameNode next; - private Set closed = new HashSet(); - private boolean graphSupport = true; + protected Stack stack = new Stack(); + protected StackFrameNode next; + protected Set closed = new HashSet(); + protected boolean graphSupport = true; private Iterator(){ this.stack.add(new StackFrameNode(initialNode)); @@ -117,7 +117,7 @@ public void remove() { } - private StackFrameNode nextUnvisited(){ + protected StackFrameNode nextUnvisited(){ StackFrameNode nextNode; do { nextNode = processNextNode(); @@ -134,7 +134,7 @@ private StackFrameNode nextUnvisited(){ } - private StackFrameNode processNextNode(){ + protected StackFrameNode processNextNode(){ if (stack.isEmpty()) return null; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java index 402e520..0f3d0f5 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java @@ -43,8 +43,8 @@ */ public class IDAStar,N extends HeuristicNode> extends Algorithm { - private final N initialNode; - private final NodeExpander expander; + protected final N initialNode; + protected final NodeExpander expander; public IDAStar(N initialNode, NodeExpander expander) { @@ -81,11 +81,11 @@ private class StackFrameNode { * backtracking. */ public class Iterator implements java.util.Iterator { - private Stack stack = new Stack(); - private C fLimit; - private C minfLimit; - private int reinitialization = 0; - private StackFrameNode next; + protected Stack stack = new Stack(); + protected C fLimit; + protected C minfLimit; + protected int reinitialization = 0; + protected StackFrameNode next; private Iterator(){ // Set initial bound @@ -127,7 +127,7 @@ public void remove() { throw new UnsupportedOperationException(); } - private void updateMinFLimit(C currentFLimit){ + protected void updateMinFLimit(C currentFLimit){ if (minfLimit == null){ minfLimit = currentFLimit; } else { @@ -137,7 +137,7 @@ private void updateMinFLimit(C currentFLimit){ } } - private StackFrameNode nextUnvisited(){ + protected StackFrameNode nextUnvisited(){ StackFrameNode nextNode; do { nextNode = processNextNode(); @@ -161,7 +161,7 @@ private StackFrameNode nextUnvisited(){ } - private StackFrameNode processNextNode(){ + protected StackFrameNode processNextNode(){ // Get and process the current node. Cases: // 1 - empty stack, return null // 2 - node exceeds the bound: update minfLimit, pop and skip. diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java index 1c06e5c..934f32d 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java @@ -40,8 +40,9 @@ * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ public class MultiobjectiveLS,N extends HeuristicNode> extends Algorithm { - private N initialNode; - private NodeExpander nodeExpander; + + protected N initialNode; + protected NodeExpander nodeExpander; public MultiobjectiveLS(N initialNode, NodeExpander nodeExpander) { this.initialNode = initialNode; @@ -56,7 +57,7 @@ public MultiobjectiveLS(N initialNode, NodeExpander nodeExpander) { * with {@code iterator.getNonDominated.get(goalState)} */ public class Iterator implements java.util.Iterator { - private Queue queue = new LinkedList(); + protected Queue queue = new LinkedList(); public Multimap nonDominated; public Iterator(){ @@ -108,7 +109,7 @@ public void remove() { throw new UnsupportedOperationException(); } - private Collection dominatedBy(N node, Iterable nonDominated) { + protected Collection dominatedBy(N node, Iterable nonDominated) { Collection dominated = new HashSet(); for (N n : nonDominated) { if (node.getScore().compareTo(n.getScore())<0) { @@ -118,7 +119,7 @@ private Collection dominatedBy(N node, Iterable nonDominated) { return dominated; } - private boolean isDominated(N node, Iterable nonDominated) { + protected boolean isDominated(N node, Iterable nonDominated) { // Compare all non-dominated nodes with node for (N nd : nonDominated) { if (nd.getScore().compareTo(node.getScore())< 0) { From d5ab21de6ab390be87ba3c431b240b271286bc63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Wed, 25 Feb 2015 19:10:18 +0100 Subject: [PATCH 02/62] close #115 : ProblemBuilder[...].useTransitionFunction() now accepts TransitionFunction when a problem without actions is being created --- .../es/usc/citius/hipster/model/problem/ProblemBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/problem/ProblemBuilder.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/problem/ProblemBuilder.java index 62b1abc..c62bc31 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/problem/ProblemBuilder.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/problem/ProblemBuilder.java @@ -99,7 +99,7 @@ private WithoutAction(){} * * @param transitionFunction transition function to be used. */ - public Uninformed useTransitionFunction(StateTransitionFunction transitionFunction){ + public Uninformed useTransitionFunction(TransitionFunction transitionFunction){ return new Uninformed(transitionFunction); } } From ed92c8a2dde72088878d757a7321860bcf4c4c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Thu, 26 Feb 2015 14:10:49 +0100 Subject: [PATCH 03/62] close #120 : changed assert check to use compareTo() in BinaryOperation constructor --- .../hipster/model/function/impl/BinaryOperation.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/BinaryOperation.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/BinaryOperation.java index dcf20b0..4713b23 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/BinaryOperation.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/BinaryOperation.java @@ -30,7 +30,7 @@ * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> * @author Adrián González Sieira <adrian.gonzalez@usc.es> */ -public class BinaryOperation implements BinaryFunction { +public class BinaryOperation> implements BinaryFunction { private E maxElem; private E identityElem; @@ -45,9 +45,9 @@ public class BinaryOperation implements BinaryFunction { */ public BinaryOperation(BinaryFunction operation, E identityElem, E maxElem) { // Check properties - assert operation.apply(identityElem, maxElem).equals(maxElem); - assert operation.apply(maxElem, identityElem).equals(maxElem); - assert operation.apply(identityElem, identityElem).equals(identityElem); + assert operation.apply(identityElem, maxElem).compareTo(maxElem) == 0; + assert operation.apply(maxElem, identityElem).compareTo(maxElem) == 0; + assert operation.apply(identityElem, identityElem).compareTo(identityElem) == 0; //Preconditions.checkArgument(operation.apply(identityElem, maxElem).equals(maxElem), "Property error: I x A != A"); //Preconditions.checkArgument(operation.apply(maxElem, identityElem).equals(maxElem), "Property error: A x I != A"); //Preconditions.checkArgument(operation.apply(identityElem, identityElem).equals(identityElem), "Property error: I x I != I"); From fa1ec1435e4cf7b65c776e2d709b5b312db1e5a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Thu, 26 Feb 2015 16:26:20 +0100 Subject: [PATCH 04/62] close #117 : pom.xml files now use ${hipster.version} --- hipster-all/pom.xml | 2 +- hipster-core/pom.xml | 2 +- hipster-examples/pom.xml | 2 +- hipster-test/pom.xml | 2 +- hipster-third-party-graphs/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hipster-all/pom.xml b/hipster-all/pom.xml index 428993a..c584c9c 100644 --- a/hipster-all/pom.xml +++ b/hipster-all/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc1 + ${hipster.version} 4.0.0 hipster-all diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index 25b9b70..34e9f84 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -5,7 +5,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc1 + ${hipster.version} 4.0.0 hipster-core diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index 118ab83..04d84d0 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc1 + ${hipster.version} 4.0.0 hipster-examples diff --git a/hipster-test/pom.xml b/hipster-test/pom.xml index 880461e..976e5e6 100644 --- a/hipster-test/pom.xml +++ b/hipster-test/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc1 + ${hipster.version} 4.0.0 hipster-test diff --git a/hipster-third-party-graphs/pom.xml b/hipster-third-party-graphs/pom.xml index e745fd1..eb9415e 100644 --- a/hipster-third-party-graphs/pom.xml +++ b/hipster-third-party-graphs/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc1 + ${hipster.version} 4.0.0 diff --git a/pom.xml b/pom.xml index 7a4f4ff..3bf04b4 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom pom - 1.0.0-rc1 + ${hipster.version} hipster-pom ${site.url} From f71b00060ab987ccf5468a196ef8f2f2247a858c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Fri, 27 Feb 2015 12:20:58 +0100 Subject: [PATCH 05/62] bump version to 1.0.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3bf04b4..9166379 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ http://citiususc.github.io/hipster - 1.0.0-rc1 + 1.0.0-SNAPSHOT es.usc.citius.hipster From e7ae18430e5fd9ee512ca48af292f6a8651e1845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Fri, 27 Feb 2015 12:23:12 +0100 Subject: [PATCH 06/62] bump version to 1.1.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3bf04b4..db59641 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ http://citiususc.github.io/hipster - 1.0.0-rc1 + 1.1.0-SNAPSHOT es.usc.citius.hipster From b1f9e0862279139b171fdaf9e405db2b1798fdef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Fri, 27 Feb 2015 14:08:14 +0100 Subject: [PATCH 07/62] removed references to ${hipster.version} in pom.xml files, caused errors in project --- hipster-all/pom.xml | 2 +- hipster-core/pom.xml | 2 +- hipster-examples/pom.xml | 2 +- hipster-test/pom.xml | 2 +- hipster-third-party-graphs/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hipster-all/pom.xml b/hipster-all/pom.xml index c584c9c..dc9eed4 100644 --- a/hipster-all/pom.xml +++ b/hipster-all/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.0.0-SNAPSHOT 4.0.0 hipster-all diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index 34e9f84..9e59862 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -5,7 +5,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.0.0-SNAPSHOT 4.0.0 hipster-core diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index 04d84d0..ad5aac1 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.0.0-SNAPSHOT 4.0.0 hipster-examples diff --git a/hipster-test/pom.xml b/hipster-test/pom.xml index 976e5e6..6f5efb9 100644 --- a/hipster-test/pom.xml +++ b/hipster-test/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.0.0-SNAPSHOT 4.0.0 hipster-test diff --git a/hipster-third-party-graphs/pom.xml b/hipster-third-party-graphs/pom.xml index eb9415e..028ad65 100644 --- a/hipster-third-party-graphs/pom.xml +++ b/hipster-third-party-graphs/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.0.0-SNAPSHOT 4.0.0 diff --git a/pom.xml b/pom.xml index 9166379..1bf654d 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom pom - ${hipster.version} + 1.0.0-SNAPSHOT hipster-pom ${site.url} From bbf87b0de42bc40278a12e7cdda55da9ddaec714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Fri, 27 Feb 2015 16:19:39 +0100 Subject: [PATCH 08/62] removed references to ${hipster.version} in pom.xml files, caused errors in project --- hipster-all/pom.xml | 2 +- hipster-core/pom.xml | 2 +- hipster-examples/pom.xml | 2 +- hipster-test/pom.xml | 2 +- hipster-third-party-graphs/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hipster-all/pom.xml b/hipster-all/pom.xml index c584c9c..8b22b81 100644 --- a/hipster-all/pom.xml +++ b/hipster-all/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.1.0-SNAPSHOT 4.0.0 hipster-all diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index 34e9f84..3a36fff 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -5,7 +5,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.1.0-SNAPSHOT 4.0.0 hipster-core diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index 04d84d0..1715aa3 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.1.0-SNAPSHOT 4.0.0 hipster-examples diff --git a/hipster-test/pom.xml b/hipster-test/pom.xml index 976e5e6..db11f26 100644 --- a/hipster-test/pom.xml +++ b/hipster-test/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.1.0-SNAPSHOT 4.0.0 hipster-test diff --git a/hipster-third-party-graphs/pom.xml b/hipster-third-party-graphs/pom.xml index eb9415e..d7dde60 100644 --- a/hipster-third-party-graphs/pom.xml +++ b/hipster-third-party-graphs/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - ${hipster.version} + 1.1.0-SNAPSHOT 4.0.0 diff --git a/pom.xml b/pom.xml index db59641..d082bb1 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom pom - ${hipster.version} + 1.1.0-SNAPSHOT hipster-pom ${site.url} From 212997bb061d92d7a69bc375b9913bee1a11c32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Tue, 10 Mar 2015 17:10:21 +0100 Subject: [PATCH 09/62] close #122 : node expander executed before changing values of V and G in the parent node --- .../usc/citius/hipster/algorithm/ADStarForward.java | 12 ++++++------ .../model/function/impl/ADStarNodeExpander.java | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java index 8603270..7079f6d 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java @@ -220,6 +220,12 @@ public N next() { //s removed from OPEN open.remove(state); //this.queue.remove(current); + //expand successors + for (N successorNode : expander.expand(current)) { + if(successorNode.isDoUpdate()){ + updateQueues(successorNode); + } + } //if v(s) > g(s) if (current.isConsistent()) { //v(s) = g(s) @@ -231,12 +237,6 @@ public N next() { expander.setMaxV(current); updateQueues(current); } - - for (N successorNode : expander.expand(current)) { - if(successorNode.isDoUpdate()){ - updateQueues(successorNode); - } - } } else { // for all directed edges (u, v) with changed edge costs for(N nodeTransitionsChanged : expander.expandTransitionsChanged(beginNode.state(), current, transitionsChanged)){ diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/ADStarNodeExpander.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/ADStarNodeExpander.java index 7358d34..1338fbf 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/ADStarNodeExpander.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/ADStarNodeExpander.java @@ -90,6 +90,7 @@ public ADStarNodeExpander(TransitionFunction successorFunction, Transition @Override public Iterable expand(N node) { Collection nodes = new ArrayList(); + boolean consistency = node.isConsistent(); //if s' not visited before: v(s')=g(s')=Infinity; bp(s')=null for (Transition transition : successorFunction.transitionsFrom(node.state())) { N successorNode = visited.get(transition.getState()); @@ -98,7 +99,7 @@ public Iterable expand(N node) { visited.put(transition.getState(), successorNode); } //if consistent - if (node.isConsistent()) { + if (consistency) { //if g(s') > g(s) + c(s, s') // bp(s') = s // g(s') = g(s) + c(s, s') From 8140c3165751386012a09aa6bb934fd7b0bfbf6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Tue, 10 Mar 2015 17:11:01 +0100 Subject: [PATCH 10/62] close #123 : Romania test changed to check size of the solutions before the equality of the elements --- .../problem/romanian/RomaniaProblemOptimalSearchTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java index 6d0c6e8..b5f3ba9 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java @@ -89,6 +89,7 @@ public void optimalPathFromAradToBucharest() { //path returned by the search algorithm List> path = Lists.reverse(node.path()); //check elements returned by the search algorithm + assertEquals("Solutions have not the same size", optimalPath.size(), path.size()); for(int i = 0; i < path.size(); i++){ //check if current element of the path is equals to the assertEquals( From 7462ee91a890e0ade03be362c418ce4c3944b02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Wed, 11 Mar 2015 13:46:57 +0100 Subject: [PATCH 11/62] close #72 : AbstractNode.path() nodes in reverted order --- .../main/java/es/usc/citius/hipster/algorithm/Algorithm.java | 1 - .../src/main/java/es/usc/citius/hipster/model/AbstractNode.java | 2 +- .../problem/romanian/RomaniaProblemOptimalSearchTest.java | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java index 5b6ce4e..851154f 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java @@ -202,7 +202,6 @@ public static > List recoverStatePath(N node){ for(N n : node.path()){ states.add(n.state()); } - Collections.reverse(states); return states; } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/AbstractNode.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/AbstractNode.java index d7f1bb2..fa60d3d 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/AbstractNode.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/AbstractNode.java @@ -55,7 +55,7 @@ public List path() { LinkedList path = new LinkedList(); N currentNode = (N) this; while(currentNode != null){ - path.add(currentNode); + path.addFirst(currentNode); currentNode = currentNode.previousNode; } return path; diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java index b5f3ba9..d4ad190 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java @@ -87,7 +87,7 @@ public void optimalPathFromAradToBucharest() { RomanianProblem.City.Bucharest ); //path returned by the search algorithm - List> path = Lists.reverse(node.path()); + List> path = node.path(); //check elements returned by the search algorithm assertEquals("Solutions have not the same size", optimalPath.size(), path.size()); for(int i = 0; i < path.size(); i++){ From 73da91d71685cf4a82ea6139e0f122ad7b38f5c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Thu, 12 Mar 2015 12:02:31 +0100 Subject: [PATCH 12/62] visibility of the search iterators constructors changed to protected --- .../java/es/usc/citius/hipster/algorithm/ADStarForward.java | 2 +- .../src/main/java/es/usc/citius/hipster/algorithm/AStar.java | 2 +- .../main/java/es/usc/citius/hipster/algorithm/BellmanFord.java | 2 +- .../es/usc/citius/hipster/algorithm/BreadthFirstSearch.java | 2 +- .../java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java | 2 +- .../src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java | 2 +- .../java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java index 3f41923..5c90573 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/ADStarForward.java @@ -111,7 +111,7 @@ public class Iterator implements java.util.Iterator { protected final N beginNode; protected final Collection goalNodes; - private Iterator() { + protected Iterator() { //initialize nodes this.beginNode = expander.makeNode(null, new Transition(null, begin)); //initialize goal node collection diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java index 6e1d196..25aae88 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java @@ -71,7 +71,7 @@ public class Iterator implements java.util.Iterator { protected Map closed; protected Queue queue; - private Iterator() { + protected Iterator() { open = new HashMap(); closed = new HashMap(); queue = new PriorityQueue(); diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java index a1266bc..ddd47b1 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java @@ -63,7 +63,7 @@ public class Iterator implements java.util.Iterator { protected Queue queue; protected Map explored; - private Iterator(){ + protected Iterator(){ this.queue = new HashQueue(); this.explored = new HashMap(); this.queue.add(initialNode.state()); diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java index 296b892..47282be 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BreadthFirstSearch.java @@ -58,7 +58,7 @@ public class Iterator implements java.util.Iterator { * Iterator cannot be instantiated from outside. * Use {@link BreadthFirstSearch#iterator()} to create a new BFS iterator. */ - private Iterator(){ + protected Iterator(){ visited.put(initialNode.state(), initialNode); queue.add(initialNode); } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java index 513b186..7215324 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java @@ -78,7 +78,7 @@ public class Iterator implements java.util.Iterator { protected Set closed = new HashSet(); protected boolean graphSupport = true; - private Iterator(){ + protected Iterator(){ this.stack.add(new StackFrameNode(initialNode)); } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java index 0f3d0f5..22f61a7 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/IDAStar.java @@ -87,7 +87,7 @@ public class Iterator implements java.util.Iterator { protected int reinitialization = 0; protected StackFrameNode next; - private Iterator(){ + protected Iterator(){ // Set initial bound fLimit = initialNode.getEstimation(); minfLimit = null; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java index 934f32d..3b5c9cc 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java @@ -60,7 +60,7 @@ public class Iterator implements java.util.Iterator { protected Queue queue = new LinkedList(); public Multimap nonDominated; - public Iterator(){ + protected Iterator(){ queue = new PriorityQueue(); this.nonDominated = HashMultimap.create(); this.queue.add(initialNode); From 861da9f713e65a1c214d3d712a40a55dd321045b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Wed, 1 Apr 2015 18:49:20 +0200 Subject: [PATCH 13/62] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1c69199..0912366 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![Hipster](src/main/doclava/custom/assets/hipster-template/assets/images/header-logo.png?raw=true) -A powerful and friendly heuristic search library implemented in pure java. +A powerful and friendly heuristic search library implemented in Java. ## What's Hipster? @@ -20,9 +20,9 @@ The current version of the library comes with some very well-known and wide used * Dijkstra. * Bellman-Ford. * Informed search: - * A\*. \\A star\\ - * IDA\*: Iterative Deepening A\*. \\IDA star\\ - * AD\*: Anytime Dynamic A\*. \\AD star\\ + * A star (A*). + * IDA star (IDA*), Iterative Deepening A*. + * AD star (AD*): Anytime Dynamic A*. * Local search: * Hill-Climbing. * Enforced-Hill-Climbing. From 3d8ad0915376152583a708e1a0cec280672ca8f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Mon, 27 Apr 2015 18:39:39 +0200 Subject: [PATCH 14/62] close #113 : removed guava from hipster-core dependencies --- hipster-core/pom.xml | 10 -- .../citius/hipster/algorithm/Algorithm.java | 34 +++--- .../citius/hipster/algorithm/BellmanFord.java | 11 +- .../hipster/algorithm/MultiobjectiveLS.java | 31 ++--- .../{util => }/graph/GraphBuilder.java | 9 +- .../hipster/{util => }/graph/GraphEdge.java | 6 +- .../{util => }/graph/GraphSearchProblem.java | 34 +++--- .../graph/HashBasedHipsterDirectedGraph.java | 24 ++-- .../hipster/graph/HashBasedHipsterGraph.java | 98 ++++++++++++++++ .../graph/HipsterDirectedGraph.java | 2 +- .../{util => }/graph/HipsterGraph.java | 2 +- .../hipster/{util => }/graph/UniqueEdge.java | 2 +- .../{util => }/graph/WeightedEdge.java | 2 +- .../LazyActionStateTransitionFunction.java | 22 ++-- .../model/function/impl/LazyNodeExpander.java | 20 ++-- .../impl/StateTransitionFunction.java | 22 ++-- .../es/usc/citius/hipster/util/Function.java | 12 ++ .../es/usc/citius/hipster/util/Predicate.java | 15 +++ .../util/examples/RomanianProblem.java | 6 +- .../hipster/util/examples/maze/Maze2D.java | 13 ++- .../util/examples/maze/MazeSearch.java | 13 +-- .../util/graph/HashBasedHipsterGraph.java | 108 ------------------ ...ADStarRomaniaProblemOptimalSearchTest.java | 2 +- .../AStarRomaniaProblemOptimalSearchTest.java | 2 +- ...jkstraRomaniaProblemOptimalSearchTest.java | 2 +- ...DAStarRomaniaProblemOptimalSearchTest.java | 2 +- .../RomaniaProblemOptimalSearchTest.java | 4 +- .../hipster/util/graph/GraphBuilderTest.java | 19 ++- .../HashBasedHipsterDirectedGraphTest.java | 91 --------------- .../util/graph/HashBasedHipsterGraphTest.java | 108 ------------------ .../util/graph/RomanianProblemGraph.java | 1 + .../hipster/algorithm/BellmanFordTest.java | 4 +- .../algorithm/DepthFirstSearchTest.java | 6 +- .../MultiobjectiveShortestPathTest.java | 6 +- hipster-examples/pom.xml | 5 + .../examples/DirectedGraphSearchExample.java | 8 +- .../examples/EightQueensProblemExample.java | 4 +- .../examples/RomanianProblemExample.java | 2 +- ...BlueprintsHipsterDirectedGraphAdapter.java | 4 +- .../BlueprintsHipsterGraphAdapter.java | 21 ++-- .../jung/JUNGHipsterDirectedGraphAdapter.java | 29 +++-- .../graphs/jung/JUNGHipsterGraphAdapter.java | 29 +++-- .../graphs/JUNGHipsterGraphAdapterTest.java | 9 +- pom.xml | 1 + 44 files changed, 341 insertions(+), 514 deletions(-) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/GraphBuilder.java (92%) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/GraphEdge.java (94%) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/GraphSearchProblem.java (87%) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/HashBasedHipsterDirectedGraph.java (68%) create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/HipsterDirectedGraph.java (96%) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/HipsterGraph.java (96%) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/UniqueEdge.java (98%) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/WeightedEdge.java (96%) create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/util/Function.java create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/util/Predicate.java delete mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraph.java delete mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterDirectedGraphTest.java delete mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraphTest.java diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index 9e59862..06e77f9 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -14,14 +14,4 @@ ${project.basedir}/.. - - - com.google.guava - guava - - - com.google.code.findbugs - jsr305 - - \ No newline at end of file diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java index 851154f..bd3667c 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/Algorithm.java @@ -17,9 +17,8 @@ package es.usc.citius.hipster.algorithm; -import com.google.common.base.Predicate; -import com.google.common.base.Stopwatch; import es.usc.citius.hipster.model.Node; +import es.usc.citius.hipster.util.Predicate; import java.util.*; @@ -40,30 +39,29 @@ public abstract class Algorithm> implements Iterable goalNodes; + private int iterations; + private Collection goalNodes; + private long elapsed; - public SearchResult(N goalNode, int iterations, Stopwatch stopwatch) { + public SearchResult(N goalNode, int iterations, long elapsed) { this.goalNodes = Collections.singletonList(goalNode); this.iterations = iterations; - this.stopwatch = stopwatch; + this.elapsed = elapsed; } - public SearchResult(Collection goalNodes, int iterations, Stopwatch stopwatch) { + public SearchResult(Collection goalNodes, int iterations, long elapsed) { this.goalNodes = goalNodes; this.iterations = iterations; - this.stopwatch = stopwatch; + this.elapsed = elapsed; } /** - * Returns a stopped {@link Stopwatch} with the total search time. - * Use stopwatch.toString() to print the formatted time. - * @return stopwatch with the total search time. + * @return the elapsed time (in milliseconds) between the begin of the search and the + * search result generation. */ - public Stopwatch getStopwatch() { - return stopwatch; + public long getElapsed() { + return elapsed; } /** @@ -99,7 +97,7 @@ public String toString() { final String ls = System.getProperty("line.separator"); StringBuilder builder = new StringBuilder(); builder.append("Total solutions: ").append(goalNodes.size()).append(ls); - builder.append("Total time: ").append(getStopwatch().toString()).append(ls); + builder.append("Total time: ").append(getElapsed()).append(ls); builder.append("Total number of iterations: ").append(getIterations()).append(ls); // Take solutions int solution=1; @@ -149,7 +147,7 @@ public boolean apply(N n) { public SearchResult search(Predicate condition){ int iteration = 0; Iterator it = iterator(); - Stopwatch w = Stopwatch.createStarted(); + long begin = System.currentTimeMillis(); N currentNode = null; while(it.hasNext()){ iteration++; @@ -159,8 +157,8 @@ public SearchResult search(Predicate condition){ } } - w.stop(); - return new SearchResult(currentNode, iteration, w); + long end = System.currentTimeMillis(); + return new SearchResult(currentNode, iteration, end - begin); } /** diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java index ddd47b1..efd0dbe 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java @@ -16,10 +16,9 @@ package es.usc.citius.hipster.algorithm; -import com.google.common.base.Predicate; -import com.google.common.base.Stopwatch; import es.usc.citius.hipster.model.CostNode; import es.usc.citius.hipster.model.function.NodeExpander; +import es.usc.citius.hipster.util.Predicate; import es.usc.citius.lab.hipster.collections.HashQueue; import java.util.Collections; @@ -133,7 +132,7 @@ public void remove() { public SearchResult search(Predicate condition){ int iteration = 0; Iterator it = iterator(); - Stopwatch w = Stopwatch.createStarted(); + long begin = System.currentTimeMillis(); N currentNode = null; N goalNode = null; while(it.hasNext()){ @@ -144,13 +143,13 @@ public SearchResult search(Predicate condition){ } } - w.stop(); + long end = System.currentTimeMillis(); if (goalNode != null) { N goal = it.explored.get(goalNode.state()); - return new SearchResult(goal, iteration, w); + return new SearchResult(goal, iteration, end - begin); } - return new SearchResult(Collections.emptyList(), iteration, w); + return new SearchResult(Collections.emptyList(), iteration, end - begin); } @Override diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java index 3b5c9cc..a7d5247 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/MultiobjectiveLS.java @@ -16,12 +16,9 @@ package es.usc.citius.hipster.algorithm; -import com.google.common.base.Predicate; -import com.google.common.base.Stopwatch; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.function.NodeExpander; +import es.usc.citius.hipster.util.Predicate; import java.util.*; @@ -51,18 +48,20 @@ public MultiobjectiveLS(N initialNode, NodeExpander nodeExpander) { /** * MultiobjectiveLS iterator. It expands one state at a time and updates - * an internal table (nonDominated) which stores all non-dominated paths. + * an internal connected (nonDominated) which stores all non-dominated paths. * In order to find all non-dominated shortest path, the algorithm must be * executed until {@code iterator.hasNext() == false}. Paths can be recovered * with {@code iterator.getNonDominated.get(goalState)} */ public class Iterator implements java.util.Iterator { protected Queue queue = new LinkedList(); - public Multimap nonDominated; + public Map> nonDominated; + //auxiliar variable which stores an empty list to avoid nullable values in code + private final Collection EMPTYLIST = new ArrayList(); protected Iterator(){ queue = new PriorityQueue(); - this.nonDominated = HashMultimap.create(); + this.nonDominated = new HashMap>(); this.queue.add(initialNode); } @@ -87,7 +86,13 @@ public N next() { for (N candidate : nodeExpander.expand(current)) { // Take non-dominated (nd) nodes associated to the current state // (i.e., all non-dominated paths from start to currentState - Collection ndNodes = this.nonDominated.get(candidate.state()); + Collection ndNodes = EMPTYLIST; + if(!nonDominated.containsKey(candidate.state())){ + nonDominated.put(candidate.state(), new ArrayList()); + } + else{ + ndNodes = nonDominated.get(candidate.state()); + } // Check if the node is non-dominated if (!isDominated(candidate, ndNodes)) { // Assign the candidate to the queue @@ -133,7 +138,7 @@ public Queue getQueue() { return queue; } - public Multimap getNonDominated() { + public Map> getNonDominated() { return nonDominated; } } @@ -142,7 +147,7 @@ public Multimap getNonDominated() { public SearchResult search(Predicate condition){ int iteration = 0; Iterator it = new Iterator(); - Stopwatch w = Stopwatch.createStarted(); + long beginTime = System.currentTimeMillis(); N currentNode; N goalNode = null; while(it.hasNext()){ @@ -152,12 +157,12 @@ public SearchResult search(Predicate condition){ goalNode = currentNode; } } - w.stop(); + long elapsed = System.currentTimeMillis() - beginTime; if (goalNode != null) { Collection solutions = it.nonDominated.get(goalNode.state()); - return new SearchResult(solutions, iteration, w); + return new SearchResult(solutions, iteration, elapsed); } - return new SearchResult(Collections.emptyList(), iteration, w); + return new SearchResult(Collections.emptyList(), iteration, elapsed); } @Override diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphBuilder.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java similarity index 92% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphBuilder.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java index 19d68cd..1476da3 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphBuilder.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java @@ -14,9 +14,12 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; +import es.usc.citius.hipster.graph.HashBasedHipsterDirectedGraph; +import es.usc.citius.hipster.graph.HashBasedHipsterGraph; + import java.util.LinkedList; import java.util.List; @@ -77,6 +80,8 @@ public Builder(VT v1, VT v2, ET edge){ public HipsterDirectedGraph buildDirectedGraph(){ HashBasedHipsterDirectedGraph graph = HashBasedHipsterDirectedGraph.create(); for(Connection c : connections){ + graph.add(c.vertex1); + graph.add(c.vertex2); graph.connect(c.vertex1, c.vertex2, c.edge); } return graph; @@ -85,6 +90,8 @@ public HipsterDirectedGraph buildDirectedGraph(){ public HipsterGraph buildUndirectedGraph(){ HashBasedHipsterGraph graph = HashBasedHipsterGraph.create(); for(Connection c : connections){ + graph.add(c.vertex1); + graph.add(c.vertex2); graph.connect(c.vertex1, c.vertex2, c.edge); } return graph; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphEdge.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphEdge.java similarity index 94% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphEdge.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphEdge.java index b2b4974..531d00f 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphEdge.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphEdge.java @@ -14,11 +14,9 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; -import com.google.common.base.Preconditions; - /** * Hipster graph edge implementation to represent edges (or arcs) of a directed or * undirected graph. @@ -37,7 +35,7 @@ public GraphEdge(V vertex1, V vertex2, E edgeValue) { } public GraphEdge(V vertex1, V vertex2, E edgeValue, boolean directed) { - Preconditions.checkArgument(vertex1 != null && vertex2 != null, "Vertices cannot be null"); + if(vertex1 == null || vertex2 == null) throw new IllegalArgumentException("Vertices cannot be null"); this.vertex1 = vertex1; this.vertex2 = vertex2; this.edgeValue = edgeValue; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphSearchProblem.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java similarity index 87% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphSearchProblem.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java index cda14fd..12e1987 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/GraphSearchProblem.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java @@ -14,11 +14,12 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; +import es.usc.citius.hipster.graph.HipsterGraph; import es.usc.citius.hipster.model.Transition; import es.usc.citius.hipster.model.function.CostFunction; import es.usc.citius.hipster.model.function.HeuristicFunction; @@ -30,6 +31,9 @@ import es.usc.citius.hipster.model.problem.ProblemBuilder; import es.usc.citius.hipster.model.problem.SearchComponents; import es.usc.citius.hipster.model.problem.SearchProblem; +import es.usc.citius.hipster.util.Function; + +import java.util.ArrayList; /** * Builder to generate a {@link es.usc.citius.hipster.model.problem.SearchProblem} but using @@ -63,25 +67,23 @@ public CostType in(final HipsterGraph graph) { tf = new TransitionFunction() { @Override public Iterable> transitionsFrom(final V state) { - return Iterables.transform(dg.outgoingEdgesOf(state), new Function, Transition>() { - @Override - public Transition apply(GraphEdge edge) { - return Transition.create(state, edge.getEdgeValue(), edge.getVertex2()); - } - }); + ArrayList> transitions = new ArrayList>(); + for(GraphEdge edge : dg.outgoingEdgesOf(state)){ + transitions.add(Transition.create(state, edge.getEdgeValue(), edge.getVertex2())); + } + return transitions; } }; } else { tf = new TransitionFunction() { @Override public Iterable> transitionsFrom(final V state) { - return Iterables.transform(graph.edgesOf(state), new Function, Transition>() { - @Override - public Transition apply(GraphEdge edge) { - V oppositeVertex = edge.getVertex1().equals(state) ? edge.getVertex2() : edge.getVertex1(); - return Transition.create(state, edge.getEdgeValue(), oppositeVertex); - } - }); + ArrayList> transitions = new ArrayList>(); + for(GraphEdge edge : graph.edgesOf(state)){ + V oppositeVertex = edge.getVertex1().equals(state) ? edge.getVertex2() : edge.getVertex1(); + transitions.add(Transition.create(state, edge.getEdgeValue(), oppositeVertex)); + } + return transitions; } }; } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HashBasedHipsterDirectedGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java similarity index 68% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HashBasedHipsterDirectedGraph.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java index 40887e8..bc907d9 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HashBasedHipsterDirectedGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; -import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.List; /** * Implementation of a HipsterDirectedGraph using a Guava Hash Table. @@ -27,22 +28,29 @@ public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph connect(V v1, V v2, E value){ - Preconditions.checkArgument(v1 != null && v2 != null, "Vertices cannot be null"); + //check input + if(v1 == null || v2 == null) throw new IllegalArgumentException("Vertices cannot be null"); GraphEdge edge = new GraphEdge(v1, v2, value, true); - graphTable.put(v1, v2, edge); - disconnected.remove(v1); - disconnected.remove(v2); + connected.get(v1).add(edge); return edge; } @Override public Iterable> outgoingEdgesOf(V vertex) { - return graphTable.row(vertex).values(); + return connected.get(vertex); } @Override public Iterable> incomingEdgesOf(V vertex) { - return graphTable.column(vertex).values(); + ArrayList> incomingEdges = new ArrayList>(); + for(List> edgesList : connected.values()){ + for(GraphEdge outgoingEdge : edgesList){ + if(outgoingEdge.getVertex2().equals(vertex)){ + incomingEdges.add(outgoingEdge); + } + } + } + return incomingEdges; } public static HashBasedHipsterDirectedGraph create() { diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java new file mode 100644 index 0000000..17d2e5e --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -0,0 +1,98 @@ +/* + * Copyright 2014 CITIUS , University of Santiago de Compostela. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package es.usc.citius.hipster.graph; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * Implementation of a HipsterGraph using a Guava Hash Table. + * + * @author Adrián González Sieira <adrian.gonzalez@usc.es> + */ +public class HashBasedHipsterGraph implements HipsterGraph { + protected HashMap>> connected; + + public HashBasedHipsterGraph(){ + this.connected = new HashMap>>(); + } + + /** + * Add a new node to the graph with no connections. + * + * @param v + */ + public void add(V v){ + //add a new entry to the hash map if it does not exist + if(!connected.containsKey(v)){ + connected.put(v, new ArrayList>()); + } + } + + public void remove(V v){ + // Get vertices connected with this one + for(GraphEdge edge : edgesOf(v)){ + int edgeRemoval = 0; + List> edgesInverted = connected.get(edge.getVertex2()); + for(GraphEdge current : edgesInverted){ + edgeRemoval++; + if(current.getVertex2().equals(v)){ + break; + } + } + edgesInverted.remove(edgeRemoval); + } + //remove vertices from connected + connected.remove(v); // v no longer exists + } + + public GraphEdge connect(V v1, V v2, E value){ + //check input + if(v1 == null || v2 == null) throw new IllegalArgumentException("Vertices cannot be null"); + GraphEdge edge = new GraphEdge(v1, v2, value); + GraphEdge edgeReverse = new GraphEdge(v2, v1, value); + //add edges to the graph (if not present before) + connected.get(v1).add(edge); + connected.get(v2).add(edgeReverse); + return edge; + } + + @Override + public Iterable> edges() { + ArrayList> edges = new ArrayList>(); + //store all edges in an array + for(List> current : connected.values()){ + edges.addAll(current); + } + return edges; + } + + @Override + public Iterable vertices() { + return connected.keySet(); + } + + @Override + public Iterable> edgesOf(V vertex) { + return connected.get(vertex); + } + + public static HashBasedHipsterGraph create() { + return new HashBasedHipsterGraph(); + } +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HipsterDirectedGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterDirectedGraph.java similarity index 96% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HipsterDirectedGraph.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterDirectedGraph.java index 4a08591..baaa5d8 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HipsterDirectedGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterDirectedGraph.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; /** * A simple representation of a directed graph with two methods for diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterGraph.java similarity index 96% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HipsterGraph.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterGraph.java index e8b3dc9..3002b0d 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterGraph.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; /** * Defines a graph in terms of edges and vertices. diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/UniqueEdge.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UniqueEdge.java similarity index 98% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/UniqueEdge.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/UniqueEdge.java index 5855aab..dfd7d05 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/UniqueEdge.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UniqueEdge.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; import java.util.concurrent.atomic.AtomicInteger; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/WeightedEdge.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/WeightedEdge.java similarity index 96% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/WeightedEdge.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/WeightedEdge.java index 8edd42d..74cd7f0 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/WeightedEdge.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/WeightedEdge.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package es.usc.citius.hipster.util.graph; +package es.usc.citius.hipster.graph; /** diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java index 09fd468..012d513 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java @@ -17,17 +17,16 @@ package es.usc.citius.hipster.model.function.impl; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import es.usc.citius.hipster.model.Transition; import es.usc.citius.hipster.model.function.ActionFunction; import es.usc.citius.hipster.model.function.ActionStateTransitionFunction; import es.usc.citius.hipster.model.function.TransitionFunction; +import java.util.ArrayList; + /** - * Implementation of a {@link es.usc.citius.hipster.model.function.TransitionFunction} which takes advantage - * of method in Guava {@link Iterables#transform(Iterable, com.google.common.base.Function)} to generate - * a {@link java.lang.Iterable} of {@link es.usc.citius.hipster.model.Transition} which are instantiated + * Implementation of a {@link es.usc.citius.hipster.model.function.TransitionFunction} generates + * an {@link java.lang.Iterable} of {@link es.usc.citius.hipster.model.Transition} which are instantiated * in a lazy way, as the elements are iterated by the algorithms, and not in advance. This class * is used for problems with explicit actions. * @@ -53,11 +52,12 @@ public LazyActionStateTransitionFunction(ActionFunction af, ActionStateTra @Override public Iterable> transitionsFrom(final S state) { - return Iterables.transform(af.actionsFor(state), new Function>() { - @Override - public Transition apply(A applicableAction) { - return new Transition(state, applicableAction, tf.apply(applicableAction, state)); - } - }); + ArrayList> transitions = new ArrayList>(); + //generate set of actions + for(A applicableAction : af.actionsFor(state)){ + //generate transition for each action + transitions.add(new Transition(state, applicableAction, tf.apply(applicableAction, state))); + } + return transitions; } } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java index 4ba76bb..770b01b 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java @@ -16,18 +16,17 @@ package es.usc.citius.hipster.model.function.impl; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.model.Transition; import es.usc.citius.hipster.model.function.NodeExpander; import es.usc.citius.hipster.model.function.NodeFactory; import es.usc.citius.hipster.model.function.TransitionFunction; +import java.util.ArrayList; + /** - * Implementation of a {@link es.usc.citius.hipster.model.function.NodeExpander} which takes advantage - * of method in Guava {@link Iterables#transform(Iterable, com.google.common.base.Function)} to generate - * a {@link java.lang.Iterable} of {@link es.usc.citius.hipster.model.Node} which are instantiated + * Implementation of a {@link es.usc.citius.hipster.model.function.NodeExpander} which generates + * an {@link java.lang.Iterable} of {@link es.usc.citius.hipster.model.Node} which are instantiated * in a lazy way, as required by the algorithms, and not in advance. * * @param type of the actions @@ -57,12 +56,11 @@ public Iterable expand(final N node) { // The default expansion of a node consists of // computing the successor transitions of the current state and // generating the associated nodes for each successor - return Iterables.transform(tf.transitionsFrom(node.state()), new Function, N>() { - @Override - public N apply(Transition input) { - return factory.makeNode(node, input); - } - }); + ArrayList nodes = new ArrayList(); + for(Transition current : tf.transitionsFrom(node.state())){ + nodes.add(factory.makeNode(node, current)); + } + return nodes; } /** diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java index 811b9e9..bd371a0 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java @@ -17,15 +17,14 @@ package es.usc.citius.hipster.model.function.impl; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import es.usc.citius.hipster.model.Transition; import es.usc.citius.hipster.model.function.TransitionFunction; +import java.util.ArrayList; + /** - * Implementation of a {@link es.usc.citius.hipster.model.function.TransitionFunction} which takes advantage - * of method in Guava {@link Iterables#transform(Iterable, com.google.common.base.Function)} to generate - * a {@link java.lang.Iterable} of {@link es.usc.citius.hipster.model.Transition} which are instantiated + * Implementation of a {@link es.usc.citius.hipster.model.function.TransitionFunction} which generates + * an {@link java.lang.Iterable} of {@link es.usc.citius.hipster.model.Transition} which are instantiated * in a lazy way, as the elements are iterated by the algorithms, and not in advance. This class * is used for problems without explicit actions. * @@ -38,12 +37,13 @@ public abstract class StateTransitionFunction implements TransitionFunction> transitionsFrom(final S state) { - return Iterables.transform(successorsOf(state), new Function>() { - @Override - public Transition apply(S successor) { - return new Transition(state, null, successor); - } - }); + ArrayList> transitions = new ArrayList>(); + //generate successor states + for(S current : successorsOf(state)){ + //generate successor transitions from the states + transitions.add(new Transition(state, null, current)); + } + return transitions; } /** diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/Function.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/Function.java new file mode 100644 index 0000000..752aed3 --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/Function.java @@ -0,0 +1,12 @@ +package es.usc.citius.hipster.util; + +/** + * + * + * @author Adrián González Sieira <adrian.gonzalez@usc.es> + */ +public interface Function { + + public V apply(E input); + +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/Predicate.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/Predicate.java new file mode 100644 index 0000000..175b017 --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/Predicate.java @@ -0,0 +1,15 @@ +package es.usc.citius.hipster.util; + +/** + * Definition of predictcate to be used with search iterators implementing the + * class {@link es.usc.citius.hipster.algorithm.Algorithm}. A predicate can + * be used to define the stop condition of the algorithm + * when using the method {@link es.usc.citius.hipster.algorithm.Algorithm#se} + * + * @author Adrián González Sieira <adrian.gonzalez@usc.es> + */ +public interface Predicate { + + public boolean apply(T input); + +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java index ba900c9..8fcf3c0 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java @@ -1,8 +1,8 @@ package es.usc.citius.hipster.util.examples; +import es.usc.citius.hipster.graph.GraphBuilder; import es.usc.citius.hipster.model.function.HeuristicFunction; -import es.usc.citius.hipster.util.graph.GraphBuilder; -import es.usc.citius.hipster.util.graph.HipsterGraph; +import es.usc.citius.hipster.graph.HipsterGraph; import java.util.HashMap; import java.util.Map; @@ -87,7 +87,7 @@ public enum City{ } /** - * Returns a {@link es.usc.citius.hipster.util.graph.HipsterGraph} that represents the map of Romania. + * Returns a {@link es.usc.citius.hipster.graph.HipsterGraph} that represents the map of Romania. * @return graph with the cities and costs. */ public static HipsterGraph graph(){ diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/Maze2D.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/Maze2D.java index a912f29..4ea198a 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/Maze2D.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/Maze2D.java @@ -16,9 +16,6 @@ package es.usc.citius.hipster.util.examples.maze; -import com.google.common.base.Strings; -import com.google.common.collect.Sets; - import java.awt.*; import java.io.BufferedReader; import java.io.File; @@ -59,7 +56,7 @@ * } * * - * Symbol table used by Maze2D: + * Symbol connected used by Maze2D: *
    *
  • "X": occupied tile
  • *
  • " ": empty tile
  • @@ -77,7 +74,7 @@ public class Maze2D { private Point goalLoc; private int rows; private int columns; - private static Set FREE_TILES = Sets.newHashSet(Arrays.asList(' ', 'S', 'G', '.')); + private static Set FREE_TILES = new HashSet(Arrays.asList(' ', 'S', 'G', '.')); /** * Symbols allowed to create a maze @@ -146,7 +143,11 @@ public Maze2D(String[] maze2D) throws IllegalFormatException { for (int row = 0; row < this.rows; row++) { // if the current row has less than 'columns' characters, fill with empty tiles if (maze2D[row].length() < this.columns) { - maze2D[row] = maze2D[row].concat(Strings.repeat(String.valueOf(Symbol.EMPTY.value()), this.columns - maze2D[row].length())); + String string = ""; + for(int i = 0; i < this.columns - maze2D[row].length(); i++) { + string = string.concat(String.valueOf(Symbol.EMPTY.value())); + } + maze2D[row] = maze2D[row].concat(string); } for (int column = 0; column < this.columns; column++) { char charPoint = maze2D[row].charAt(column); diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/MazeSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/MazeSearch.java index da1bca7..a6d7397 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/MazeSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/maze/MazeSearch.java @@ -17,11 +17,8 @@ package es.usc.citius.hipster.util.examples.maze; -import com.google.common.base.Function; -import com.google.common.collect.Lists; import es.usc.citius.hipster.model.Node; -import javax.annotation.Nullable; import java.awt.*; import java.util.*; import java.util.List; @@ -101,12 +98,10 @@ public static void printSearch(Iterator> it, Maze2D ma if (currentNode.previousNode() != null) { explored.add(currentNode.previousNode().state()); } - List statePath = Lists.transform(currentNode.path(), new Function, Point>() { - @Override - public Point apply(@Nullable Node pointNode) { - return pointNode.state(); - } - }); + List statePath = new ArrayList(); + for(Node current : currentNode.path()){ + statePath.add(current.state()); + } //clearOutput(20); System.out.println(getMazeStringSolution(maze, explored, statePath)); Thread.sleep(50); diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraph.java deleted file mode 100644 index 9e2d6ae..0000000 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraph.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2014 CITIUS , University of Santiago de Compostela. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package es.usc.citius.hipster.util.graph; - -import com.google.common.base.Preconditions; -import com.google.common.collect.HashBasedTable; -import com.google.common.collect.Sets; - -import java.util.HashSet; -import java.util.Set; - -/** - * Implementation of a HipsterGraph using a Guava Hash Table. - * - * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> - */ -public class HashBasedHipsterGraph implements HipsterGraph { - protected HashBasedTable> graphTable = HashBasedTable.create(); - // keep extra info for all those disconnected vertices - protected Set disconnected = new HashSet(); - - public void add(V v){ - if (!graphTable.containsColumn(v) && !graphTable.containsRow(v)){ - disconnected.add(v); - } - } - - private boolean greaterThan(int size, Iterable iterable){ - int elems = 0; - for(T t : iterable){ - elems++; - if (elems > size){ - return true; - } - } - return false; - } - - public void remove(V v){ - // Get vertices connected with this one - for(GraphEdge edge : edgesOf(v)){ - V connectedVertex = edge.getVertex1().equals(v) ? edge.getVertex2() : edge.getVertex1(); - // Is this vertex connected with a different vertex? - if (!greaterThan(1, edgesOf(connectedVertex))){ - disconnected.add(connectedVertex); - } - } - if (graphTable.containsRow(v)){ - graphTable.row(v).clear(); - } - if (graphTable.containsColumn(v)){ - graphTable.column(v).clear(); - } - // Check for disconnected vertices - disconnected.remove(v); // v no longer exists - - } - - public void remove(V v, GraphEdge edge){ - // Try to remove vertex from row/columns - Preconditions.checkArgument(edge.getVertex1().equals(v) || edge.getVertex2().equals(v), "Edge is not connected with the vertex"); - V opposite = edge.getVertex1().equals(v) ? edge.getVertex2() : edge.getVertex1(); - graphTable.row(v).remove(opposite); - } - - public GraphEdge connect(V v1, V v2, E value){ - Preconditions.checkArgument(v1 != null && v2 != null, "Vertices cannot be null"); - GraphEdge edge = new GraphEdge(v1, v2, value); - graphTable.put(v1, v2, edge); - graphTable.put(v2, v1, edge); - disconnected.remove(v1); - disconnected.remove(v2); - return edge; - } - - @Override - public Iterable> edges() { - return graphTable.values(); - } - - @Override - public Iterable vertices() { - return Sets.union(Sets.union(graphTable.rowKeySet(), graphTable.columnKeySet()), disconnected); - } - - @Override - public Iterable> edgesOf(V vertex) { - return Sets.union(Sets.newHashSet(graphTable.row(vertex).values()), Sets.newHashSet(graphTable.column(vertex).values())); - } - - public static HashBasedHipsterGraph create() { - return new HashBasedHipsterGraph(); - } -} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java index e6fd700..3b07d37 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java @@ -1,10 +1,10 @@ package es.usc.citius.hipster.algorithm.problem.romanian; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.problem.SearchComponents; import es.usc.citius.hipster.util.examples.RomanianProblem; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; import java.util.Iterator; diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java index da6afae..4893d2b 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java @@ -1,10 +1,10 @@ package es.usc.citius.hipster.algorithm.problem.romanian; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; import java.util.Iterator; diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java index 3a55dca..c956da6 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java @@ -1,10 +1,10 @@ package es.usc.citius.hipster.algorithm.problem.romanian; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; import org.junit.Test; import java.util.Iterator; diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java index 6a3e251..e5e0c6d 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java @@ -1,10 +1,10 @@ package es.usc.citius.hipster.algorithm.problem.romanian; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; import java.util.Iterator; diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java index d4ad190..6579e33 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java @@ -1,10 +1,8 @@ package es.usc.citius.hipster.algorithm.problem.romanian; -import com.google.common.collect.Lists; import es.usc.citius.hipster.model.HeuristicNode; -import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.util.examples.RomanianProblem; -import es.usc.citius.hipster.util.graph.HipsterGraph; +import es.usc.citius.hipster.graph.HipsterGraph; import org.junit.Before; import org.junit.Test; diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java index 6f4ca60..ce4a72e 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java @@ -17,7 +17,9 @@ package es.usc.citius.hipster.util.graph; -import com.google.common.collect.Sets; +import es.usc.citius.hipster.graph.GraphBuilder; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; import org.junit.BeforeClass; import org.junit.Test; @@ -54,16 +56,22 @@ public void testVertices() { public void testEdges() { Set expectedValues = new HashSet(Arrays.asList(4d, 2d, 5d, 10d, 3d, 11d, 4d)); Set values = new HashSet(); + int count = 0; for(GraphEdge edge : testGraph.edges()){ values.add(edge.getEdgeValue()); + count++; } - assertEquals(7, Sets.newHashSet(testGraph.edges()).size()); + + assertEquals(7, count); assertEquals(expectedValues, values); } @Test public void testIncomingEdges(){ - Set> edges = Sets.newHashSet(testGraph.incomingEdgesOf("D")); + Set> edges = new HashSet>(); + for(GraphEdge current : testGraph.incomingEdgesOf("D")){ + edges.add(current); + } Set values = new HashSet(); for(GraphEdge e : edges){ values.add(e.getEdgeValue()); @@ -74,7 +82,10 @@ public void testIncomingEdges(){ @Test public void testOutgoingEdges(){ - Set> edges = Sets.newHashSet(testGraph.outgoingEdgesOf("B")); + Set> edges = new HashSet>(); + for(GraphEdge current : testGraph.outgoingEdgesOf("B")){ + edges.add(current); + } Set values = new HashSet(); for(GraphEdge e : edges){ values.add(e.getEdgeValue()); diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterDirectedGraphTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterDirectedGraphTest.java deleted file mode 100644 index ca1750f..0000000 --- a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterDirectedGraphTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2014 CITIUS , University of Santiago de Compostela. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package es.usc.citius.hipster.util.graph; - -import com.google.common.collect.Sets; -import org.junit.Before; -import org.junit.Test; - -import java.util.HashSet; -import java.util.Set; - -import static org.junit.Assert.assertEquals; - - -public class HashBasedHipsterDirectedGraphTest extends HashBasedHipsterGraphTest { - private HashBasedHipsterDirectedGraph directedGraph; - - @Before - @Override - public void setUp() { - directedGraph = HashBasedHipsterDirectedGraph.create(); - directedGraph.connect("A", "B", 4d); - directedGraph.connect("A", "C", 2d); - directedGraph.connect("B", "C", 5d); - directedGraph.connect("B", "D", 10d); - directedGraph.connect("C", "E", 3d); - directedGraph.connect("D", "F", 11d); - directedGraph.connect("E", "D", 4d); - graph = directedGraph; - } - - @Test - public void testConnect() throws Exception { - directedGraph.connect("F", "G", 1d); - assertEquals("F", directedGraph.incomingEdgesOf("G").iterator().next().getVertex1()); - } - - @Test - public void testOutgoingEdgesOf() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("B", "C", 5d, true)); - expected.add(new GraphEdge("B", "D", 10d, true)); - assertEquals(expected, Sets.newHashSet(directedGraph.outgoingEdgesOf("B"))); - } - - @Test - public void testIncomingEdgesOf() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("B", "C", 5d, true)); - expected.add(new GraphEdge("A", "C", 2d, true)); - assertEquals(expected, Sets.newHashSet(directedGraph.incomingEdgesOf("C"))); - } - - @Test - @Override - public void testEdges() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("A", "B", 4d, true)); - expected.add(new GraphEdge("A", "C", 2d, true)); - expected.add(new GraphEdge("B", "C", 5d, true)); - expected.add(new GraphEdge("B", "D", 10d, true)); - expected.add(new GraphEdge("C", "E", 3d, true)); - expected.add(new GraphEdge("D", "F", 11d, true)); - expected.add(new GraphEdge("E", "D", 4d, true)); - assertEquals(expected, Sets.newHashSet(graph.edges())); - } - - @Test - @Override - public void testEdgesOf() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("B", "D", 10d, true)); - expected.add(new GraphEdge("A", "B", 4d, true)); - expected.add(new GraphEdge("B", "C", 5d, true)); - assertEquals(expected, graph.edgesOf("B")); - } -} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraphTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraphTest.java deleted file mode 100644 index b5ff6a3..0000000 --- a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/HashBasedHipsterGraphTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2014 CITIUS , University of Santiago de Compostela. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package es.usc.citius.hipster.util.graph; - -import com.google.common.collect.Sets; -import org.junit.Before; -import org.junit.Test; - -import java.util.HashSet; -import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - - -public class HashBasedHipsterGraphTest { - protected HashBasedHipsterGraph graph; - - @Before - public void setUp(){ - graph = HashBasedHipsterGraph.create(); - graph.connect("A", "B", 4d); - graph.connect("A", "C", 2d); - graph.connect("B", "C", 5d); - graph.connect("B", "D", 10d); - graph.connect("C", "E", 3d); - graph.connect("D", "F", 11d); - graph.connect("E", "D", 4d); - } - - @Test - public void testAdd() throws Exception { - graph.add("G"); - assertTrue(Sets.newHashSet(graph.vertices()).contains("G")); - } - - @Test - public void testRemove() throws Exception { - graph.remove("B"); - assertFalse(Sets.newHashSet(graph.vertices()).contains("B")); - } - - @Test - public void testRemoveEdge() throws Exception { - - } - - @Test - public void testConnect() throws Exception { - graph.connect("X","Y",1.0d); - assertTrue(Sets.newHashSet(graph.vertices()).contains("X")); - assertTrue(Sets.newHashSet(graph.vertices()).contains("Y")); - } - - @Test - public void testEdges() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("A","B",4d)); - expected.add(new GraphEdge("A","C",2d)); - expected.add(new GraphEdge("B","C",5d)); - expected.add(new GraphEdge("B","D",10d)); - expected.add(new GraphEdge("C","E",3d)); - expected.add(new GraphEdge("D","F",11d)); - expected.add(new GraphEdge("E","D",4d)); - assertEquals(expected, Sets.newHashSet(graph.edges())); - } - - @Test - public void testVertices() throws Exception { - Set expected = Sets.newHashSet("A","B","C","D","E","F"); - assertEquals(expected, graph.vertices()); - } - - @Test - public void testEdgesOf() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("B","D",10d)); - expected.add(new GraphEdge("A","B",4d)); - expected.add(new GraphEdge("B","C",5d)); - assertEquals(expected, graph.edgesOf("B")); - } - - @Test - public void testDisconnectedVertices(){ - graph.connect("H", "L", 1d); - graph.connect("I", "L", 1d); - graph.connect("J", "L", 1d); - graph.connect("K", "L", 1d); - assertTrue(graph.disconnected.isEmpty()); - graph.remove("L"); - assertEquals(4, graph.disconnected.size()); - } -} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java index 5ed49e3..62f7b52 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java @@ -17,6 +17,7 @@ package es.usc.citius.hipster.util.graph; +import es.usc.citius.hipster.graph.HipsterGraph; import es.usc.citius.hipster.util.examples.RomanianProblem; import org.junit.Before; import org.junit.Test; diff --git a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/BellmanFordTest.java b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/BellmanFordTest.java index 3dc31b6..6dc4dfb 100644 --- a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/BellmanFordTest.java +++ b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/BellmanFordTest.java @@ -16,8 +16,8 @@ package es.usc.citius.lab.hipster.algorithm; -import es.usc.citius.hipster.util.graph.GraphBuilder; -import es.usc.citius.hipster.util.graph.HipsterDirectedGraph; +import es.usc.citius.hipster.graph.GraphBuilder; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; import org.junit.Test; /** diff --git a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java index 9f0afc5..7b484ab 100644 --- a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java @@ -17,10 +17,10 @@ package es.usc.citius.lab.hipster.algorithm; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphBuilder; +import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.impl.UnweightedNode; -import es.usc.citius.hipster.util.graph.GraphBuilder; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; -import es.usc.citius.hipster.util.graph.HipsterDirectedGraph; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; import org.junit.Test; import java.util.Iterator; diff --git a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/MultiobjectiveShortestPathTest.java b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/MultiobjectiveShortestPathTest.java index 9df412a..f8fc6cb 100644 --- a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/MultiobjectiveShortestPathTest.java +++ b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/MultiobjectiveShortestPathTest.java @@ -17,11 +17,11 @@ package es.usc.citius.lab.hipster.algorithm; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphBuilder; +import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.function.BinaryFunction; import es.usc.citius.hipster.model.function.impl.BinaryOperation; -import es.usc.citius.hipster.util.graph.GraphBuilder; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; -import es.usc.citius.hipster.util.graph.HipsterDirectedGraph; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; import org.junit.Test; /** diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index ad5aac1..7208490 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -41,6 +41,11 @@ hipster-third-party-graphs ${project.version} + + com.google.guava + guava + 18.0 + com.intellij forms_rt diff --git a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java index b0cb373..401a1af 100644 --- a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java +++ b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java @@ -1,13 +1,13 @@ package es.usc.citius.hipster.examples; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphBuilder; +import es.usc.citius.hipster.graph.GraphSearchProblem; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; import es.usc.citius.hipster.model.problem.SearchProblem; -import es.usc.citius.hipster.util.graph.GraphBuilder; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; -import es.usc.citius.hipster.util.graph.HipsterDirectedGraph; /** - * Example that creates a {@link es.usc.citius.hipster.util.graph.HipsterDirectedGraph}, + * Example that creates a {@link es.usc.citius.hipster.graph.HipsterDirectedGraph}, * creates a search problem to be used with Hipster search iterators and performs * a search using the Dijkstra algorithm. * diff --git a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/EightQueensProblemExample.java b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/EightQueensProblemExample.java index 91858d2..11653ea 100644 --- a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/EightQueensProblemExample.java +++ b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/EightQueensProblemExample.java @@ -16,7 +16,6 @@ package es.usc.citius.hipster.examples; -import com.google.common.base.Predicate; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.algorithm.localsearch.HillClimbing; import es.usc.citius.hipster.examples.problem.NQueens; @@ -27,6 +26,7 @@ import es.usc.citius.hipster.model.impl.WeightedNode; import es.usc.citius.hipster.model.problem.ProblemBuilder; import es.usc.citius.hipster.model.problem.SearchProblem; +import es.usc.citius.hipster.util.Predicate; import java.util.Arrays; import java.util.HashSet; @@ -112,7 +112,7 @@ public Double estimate(NQueens state) { System.out.println("Running 8-Queens problem with Enforced Hill Climbing and a custom goal test predicate"); //To execute the algorithm we have two options: // Option 1 - Run the algorithm until the predicate is satisfied (until we find a state with score 0, that is, no attacked queens) - System.out.println(Hipster.createHillClimbing(p, true).search(new Predicate>() { + System.out.println(Hipster.createHillClimbing(p, true).search(new Predicate>() { @Override public boolean apply(WeightedNode node) { return node.getScore().equals(0d); diff --git a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/RomanianProblemExample.java b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/RomanianProblemExample.java index 025a299..b935710 100644 --- a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/RomanianProblemExample.java +++ b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/RomanianProblemExample.java @@ -1,9 +1,9 @@ package es.usc.citius.hipster.examples; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; /** * Implementation of the Romania problem as described in diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterDirectedGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterDirectedGraphAdapter.java index e884519..3c4b0ef 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterDirectedGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterDirectedGraphAdapter.java @@ -21,8 +21,8 @@ import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; -import es.usc.citius.hipster.util.graph.GraphEdge; -import es.usc.citius.hipster.util.graph.HipsterDirectedGraph; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; /** * Simple adapter implementation between a Hipster graph and a Blueprints graph. diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java index b792d9d..c8004f5 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java @@ -17,16 +17,14 @@ package es.usc.citius.hipster.thirdparty.graphs.blueprints; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; -import es.usc.citius.hipster.util.graph.GraphEdge; -import es.usc.citius.hipster.util.graph.HipsterGraph; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterGraph; -import javax.annotation.Nullable; +import java.util.ArrayList; /** * Simple graph adapter between a Blueprints graph and a HipsterGraph. @@ -54,11 +52,12 @@ public Iterable> edgesOf(Vertex vertex) { } protected static Iterable> convertEdges(Iterable edges){ - return Iterables.transform(edges, new Function>() { - @Override - public GraphEdge apply(@Nullable Edge edge) { - return new GraphEdge(edge.getVertex(Direction.IN), edge.getVertex(Direction.OUT), edge); - } - }); + //initialize collection + ArrayList> convertedEdges = new ArrayList>(); + //convert edges + for(Edge current : edges){ + convertedEdges.add(new GraphEdge(current.getVertex(Direction.IN), current.getVertex(Direction.OUT), current)); + } + return convertedEdges; } } diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java index 51bf5f0..90c1e72 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java @@ -17,12 +17,11 @@ package es.usc.citius.hipster.thirdparty.graphs.jung; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import edu.uci.ics.jung.graph.Graph; -import es.usc.citius.hipster.util.graph.GraphEdge; -import es.usc.citius.hipster.util.graph.HipsterDirectedGraph; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -45,12 +44,11 @@ public Iterable> outgoingEdgesOf(V vertex) { if (outEdges == null || outEdges.isEmpty()) { return Collections.emptyList(); } - return Iterables.transform(outEdges, new Function>() { - @Override - public GraphEdge apply(E edge) { - return new GraphEdge(graph.getSource(edge), graph.getDest(edge), edge); - } - }); + ArrayList> outEdgesTransformed = new ArrayList>(outEdges.size()); + for(E current : outEdges){ + outEdgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); + } + return outEdgesTransformed; } catch (NullPointerException e){ return Collections.emptyList(); } @@ -64,12 +62,11 @@ public Iterable> incomingEdgesOf(V vertex) { if (inEdges == null || inEdges.isEmpty()) { return Collections.emptyList(); } - return Iterables.transform(inEdges, new Function>() { - @Override - public GraphEdge apply(E edge) { - return new GraphEdge(graph.getSource(edge), graph.getDest(edge), edge); - } - }); + ArrayList> inEdgesTransformed = new ArrayList>(inEdges.size()); + for(E current : inEdges){ + inEdgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); + } + return inEdgesTransformed; }catch(NullPointerException e){ return Collections.emptyList(); } diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java index b402754..29ecccc 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java @@ -16,12 +16,11 @@ package es.usc.citius.hipster.thirdparty.graphs.jung; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; import edu.uci.ics.jung.graph.Graph; -import es.usc.citius.hipster.util.graph.GraphEdge; -import es.usc.citius.hipster.util.graph.HipsterGraph; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterGraph; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -43,12 +42,11 @@ public Iterable> edges() { if (edges == null || edges.isEmpty()){ return Collections.emptyList(); } - return Iterables.transform(edges, new Function>() { - @Override - public GraphEdge apply(E edge) { - return new GraphEdge(graph.getSource(edge), graph.getDest(edge), edge); - } - }); + ArrayList> edgesTransformed = new ArrayList>(edges.size()); + for(E current : edges){ + edgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); + } + return edgesTransformed; } @Override @@ -62,12 +60,11 @@ public Iterable> edgesOf(V vertex) { if (edges == null || edges.isEmpty()){ return Collections.emptyList(); } - return Iterables.transform(edges, new Function>() { - @Override - public GraphEdge apply(E edge) { - return new GraphEdge(graph.getSource(edge), graph.getDest(edge), edge); - } - }); + ArrayList> edgesTransformed = new ArrayList>(edges.size()); + for(E current : edges){ + edgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); + } + return edgesTransformed; } } diff --git a/hipster-third-party-graphs/src/test/java/es/usc/citius/hipster/thirdparty/graphs/JUNGHipsterGraphAdapterTest.java b/hipster-third-party-graphs/src/test/java/es/usc/citius/hipster/thirdparty/graphs/JUNGHipsterGraphAdapterTest.java index 8baf11c..5e08059 100644 --- a/hipster-third-party-graphs/src/test/java/es/usc/citius/hipster/thirdparty/graphs/JUNGHipsterGraphAdapterTest.java +++ b/hipster-third-party-graphs/src/test/java/es/usc/citius/hipster/thirdparty/graphs/JUNGHipsterGraphAdapterTest.java @@ -17,7 +17,6 @@ package es.usc.citius.hipster.thirdparty.graphs; -import com.google.common.base.Function; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; @@ -25,15 +24,15 @@ import com.tinkerpop.blueprints.oupls.jung.GraphJung; import com.tinkerpop.blueprints.util.io.graphml.GraphMLReader; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; import es.usc.citius.hipster.model.impl.WeightedNode; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.thirdparty.graphs.jung.JUNGHipsterDirectedGraphAdapter; -import es.usc.citius.hipster.util.graph.GraphSearchProblem; -import es.usc.citius.hipster.util.graph.HipsterDirectedGraph; +import es.usc.citius.hipster.util.Function; import org.junit.BeforeClass; import org.junit.Test; -import javax.annotation.Nullable; import java.io.InputStream; import java.util.Arrays; import java.util.List; @@ -99,7 +98,7 @@ public void testWeightedShortestPathSearch(){ .in(adaptedGraph) .extractCostFromEdges(new Function() { @Override - public Double apply(@Nullable Edge edge) { + public Double apply(Edge edge) { return edge.getProperty("weight"); } }) diff --git a/pom.xml b/pom.xml index 1bf654d..0b134e0 100644 --- a/pom.xml +++ b/pom.xml @@ -236,6 +236,7 @@ org.apache.maven.plugins maven-javadoc-plugin + 2.10.1 attach-javadocs From 5d02b4f8fcc739ce4bd3443a073ff1b76ddb4858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Tue, 28 Apr 2015 12:59:57 +0200 Subject: [PATCH 15/62] close #125 : new module hipster-extensions containing implementations of HipsterGraph using guava tables --- hipster-extensions/pom.xml | 43 +++++++ .../graph/HashBasedHipsterDirectedGraph.java | 52 +++++++++ .../graph/HashBasedHipsterGraph.java | 109 ++++++++++++++++++ .../HashBasedHipsterDirectedGraphTest.java | 92 +++++++++++++++ .../graph/HashBasedHipsterGraphTest.java | 109 ++++++++++++++++++ pom.xml | 1 + 6 files changed, 406 insertions(+) create mode 100644 hipster-extensions/pom.xml create mode 100644 hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java create mode 100644 hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java create mode 100644 hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java create mode 100644 hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java diff --git a/hipster-extensions/pom.xml b/hipster-extensions/pom.xml new file mode 100644 index 0000000..3686369 --- /dev/null +++ b/hipster-extensions/pom.xml @@ -0,0 +1,43 @@ + + + + hipster-pom + es.usc.citius.hipster + 1.0.0-SNAPSHOT + + + 4.0.0 + hipster-extensions + + + ${project.basedir}/.. + + + + + es.usc.citius.hipster + hipster-core + ${project.version} + + + es.usc.citius.hipster + hipster-third-party-graphs + ${project.version} + + + com.google.guava + guava + 18.0 + + + + com.google.code.findbugs + jsr305 + 3.0.0 + + + + + \ No newline at end of file diff --git a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java new file mode 100644 index 0000000..53ee549 --- /dev/null +++ b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 CITIUS , University of Santiago de Compostela. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package es.usc.citius.hipster.extensions.graph; + +import com.google.common.base.Preconditions; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterDirectedGraph; + +/** + * Implementation of a HipsterDirectedGraph using a Guava Hash Table. + * + * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> + */ +public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph implements HipsterDirectedGraph { + + @Override + public GraphEdge connect(V v1, V v2, E value){ + Preconditions.checkArgument(v1 != null && v2 != null, "Vertices cannot be null"); + GraphEdge edge = new GraphEdge(v1, v2, value, true); + graphTable.put(v1, v2, edge); + disconnected.remove(v1); + disconnected.remove(v2); + return edge; + } + + @Override + public Iterable> outgoingEdgesOf(V vertex) { + return graphTable.row(vertex).values(); + } + + @Override + public Iterable> incomingEdgesOf(V vertex) { + return graphTable.column(vertex).values(); + } + + public static HashBasedHipsterDirectedGraph create() { + return new HashBasedHipsterDirectedGraph(); + } +} diff --git a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java new file mode 100644 index 0000000..9a6d561 --- /dev/null +++ b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java @@ -0,0 +1,109 @@ +/* + * Copyright 2014 CITIUS , University of Santiago de Compostela. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package es.usc.citius.hipster.extensions.graph; + +import com.google.common.base.Preconditions; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Sets; +import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.HipsterGraph; + +import java.util.HashSet; +import java.util.Set; + +/** + * Implementation of a HipsterGraph using a Guava Hash Table. + * + * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> + */ +public class HashBasedHipsterGraph implements HipsterGraph { + protected HashBasedTable> graphTable = HashBasedTable.create(); + // keep extra info for all those disconnected vertices + protected Set disconnected = new HashSet(); + + public void add(V v){ + if (!graphTable.containsColumn(v) && !graphTable.containsRow(v)){ + disconnected.add(v); + } + } + + private boolean greaterThan(int size, Iterable iterable){ + int elems = 0; + for(T t : iterable){ + elems++; + if (elems > size){ + return true; + } + } + return false; + } + + public void remove(V v){ + // Get vertices connected with this one + for(GraphEdge edge : edgesOf(v)){ + V connectedVertex = edge.getVertex1().equals(v) ? edge.getVertex2() : edge.getVertex1(); + // Is this vertex connected with a different vertex? + if (!greaterThan(1, edgesOf(connectedVertex))){ + disconnected.add(connectedVertex); + } + } + if (graphTable.containsRow(v)){ + graphTable.row(v).clear(); + } + if (graphTable.containsColumn(v)){ + graphTable.column(v).clear(); + } + // Check for disconnected vertices + disconnected.remove(v); // v no longer exists + + } + + public void remove(V v, GraphEdge edge){ + // Try to remove vertex from row/columns + Preconditions.checkArgument(edge.getVertex1().equals(v) || edge.getVertex2().equals(v), "Edge is not connected with the vertex"); + V opposite = edge.getVertex1().equals(v) ? edge.getVertex2() : edge.getVertex1(); + graphTable.row(v).remove(opposite); + } + + public GraphEdge connect(V v1, V v2, E value){ + Preconditions.checkArgument(v1 != null && v2 != null, "Vertices cannot be null"); + GraphEdge edge = new GraphEdge(v1, v2, value); + graphTable.put(v1, v2, edge); + graphTable.put(v2, v1, edge); + disconnected.remove(v1); + disconnected.remove(v2); + return edge; + } + + @Override + public Iterable> edges() { + return graphTable.values(); + } + + @Override + public Iterable vertices() { + return Sets.union(Sets.union(graphTable.rowKeySet(), graphTable.columnKeySet()), disconnected); + } + + @Override + public Iterable> edgesOf(V vertex) { + return Sets.union(Sets.newHashSet(graphTable.row(vertex).values()), Sets.newHashSet(graphTable.column(vertex).values())); + } + + public static HashBasedHipsterGraph create() { + return new HashBasedHipsterGraph(); + } +} diff --git a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java new file mode 100644 index 0000000..5779347 --- /dev/null +++ b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2014 CITIUS , University of Santiago de Compostela. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package es.usc.citius.hipster.extensions.graph; + +import com.google.common.collect.Sets; +import es.usc.citius.hipster.graph.GraphEdge; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; + + +public class HashBasedHipsterDirectedGraphTest extends HashBasedHipsterGraphTest { + private HashBasedHipsterDirectedGraph directedGraph; + + @Before + @Override + public void setUp() { + directedGraph = HashBasedHipsterDirectedGraph.create(); + directedGraph.connect("A", "B", 4d); + directedGraph.connect("A", "C", 2d); + directedGraph.connect("B", "C", 5d); + directedGraph.connect("B", "D", 10d); + directedGraph.connect("C", "E", 3d); + directedGraph.connect("D", "F", 11d); + directedGraph.connect("E", "D", 4d); + graph = directedGraph; + } + + @Test + public void testConnect() throws Exception { + directedGraph.connect("F", "G", 1d); + assertEquals("F", directedGraph.incomingEdgesOf("G").iterator().next().getVertex1()); + } + + @Test + public void testOutgoingEdgesOf() throws Exception { + Set> expected = new HashSet>(); + expected.add(new GraphEdge("B", "C", 5d, true)); + expected.add(new GraphEdge("B", "D", 10d, true)); + assertEquals(expected, Sets.newHashSet(directedGraph.outgoingEdgesOf("B"))); + } + + @Test + public void testIncomingEdgesOf() throws Exception { + Set> expected = new HashSet>(); + expected.add(new GraphEdge("B", "C", 5d, true)); + expected.add(new GraphEdge("A", "C", 2d, true)); + assertEquals(expected, Sets.newHashSet(directedGraph.incomingEdgesOf("C"))); + } + + @Test + @Override + public void testEdges() throws Exception { + Set> expected = new HashSet>(); + expected.add(new GraphEdge("A", "B", 4d, true)); + expected.add(new GraphEdge("A", "C", 2d, true)); + expected.add(new GraphEdge("B", "C", 5d, true)); + expected.add(new GraphEdge("B", "D", 10d, true)); + expected.add(new GraphEdge("C", "E", 3d, true)); + expected.add(new GraphEdge("D", "F", 11d, true)); + expected.add(new GraphEdge("E", "D", 4d, true)); + assertEquals(expected, Sets.newHashSet(graph.edges())); + } + + @Test + @Override + public void testEdgesOf() throws Exception { + Set> expected = new HashSet>(); + expected.add(new GraphEdge("B", "D", 10d, true)); + expected.add(new GraphEdge("A", "B", 4d, true)); + expected.add(new GraphEdge("B", "C", 5d, true)); + assertEquals(expected, graph.edgesOf("B")); + } +} diff --git a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java new file mode 100644 index 0000000..524000a --- /dev/null +++ b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java @@ -0,0 +1,109 @@ +/* + * Copyright 2014 CITIUS , University of Santiago de Compostela. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package es.usc.citius.hipster.extensions.graph; + +import com.google.common.collect.Sets; +import es.usc.citius.hipster.graph.GraphEdge; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + +public class HashBasedHipsterGraphTest { + protected HashBasedHipsterGraph graph; + + @Before + public void setUp(){ + graph = HashBasedHipsterGraph.create(); + graph.connect("A", "B", 4d); + graph.connect("A", "C", 2d); + graph.connect("B", "C", 5d); + graph.connect("B", "D", 10d); + graph.connect("C", "E", 3d); + graph.connect("D", "F", 11d); + graph.connect("E", "D", 4d); + } + + @Test + public void testAdd() throws Exception { + graph.add("G"); + assertTrue(Sets.newHashSet(graph.vertices()).contains("G")); + } + + @Test + public void testRemove() throws Exception { + graph.remove("B"); + assertFalse(Sets.newHashSet(graph.vertices()).contains("B")); + } + + @Test + public void testRemoveEdge() throws Exception { + + } + + @Test + public void testConnect() throws Exception { + graph.connect("X","Y",1.0d); + assertTrue(Sets.newHashSet(graph.vertices()).contains("X")); + assertTrue(Sets.newHashSet(graph.vertices()).contains("Y")); + } + + @Test + public void testEdges() throws Exception { + Set> expected = new HashSet>(); + expected.add(new GraphEdge("A","B",4d)); + expected.add(new GraphEdge("A","C",2d)); + expected.add(new GraphEdge("B","C",5d)); + expected.add(new GraphEdge("B","D",10d)); + expected.add(new GraphEdge("C","E",3d)); + expected.add(new GraphEdge("D","F",11d)); + expected.add(new GraphEdge("E","D",4d)); + assertEquals(expected, Sets.newHashSet(graph.edges())); + } + + @Test + public void testVertices() throws Exception { + Set expected = Sets.newHashSet("A","B","C","D","E","F"); + assertEquals(expected, graph.vertices()); + } + + @Test + public void testEdgesOf() throws Exception { + Set> expected = new HashSet>(); + expected.add(new GraphEdge("B","D",10d)); + expected.add(new GraphEdge("A","B",4d)); + expected.add(new GraphEdge("B","C",5d)); + assertEquals(expected, graph.edgesOf("B")); + } + + @Test + public void testDisconnectedVertices(){ + graph.connect("H", "L", 1d); + graph.connect("I", "L", 1d); + graph.connect("J", "L", 1d); + graph.connect("K", "L", 1d); + assertTrue(graph.disconnected.isEmpty()); + graph.remove("L"); + assertEquals(4, graph.disconnected.size()); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0b134e0..9a2fbd4 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ hipster-all hipster-test hipster-third-party-graphs + hipster-extensions From 44391075ca3713eb0785d3902c72e87d1acfc74f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Tue, 28 Apr 2015 13:03:17 +0200 Subject: [PATCH 16/62] close #126 : updated site.url in parent pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 9a2fbd4..3a52d9c 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ ${project.basedir} - http://citiususc.github.io/hipster + http://hipster4j.org 1.0.0-SNAPSHOT From b0e2e6264a47931215ea0bebc8571a7a60aa99af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Thu, 30 Apr 2015 15:20:12 +0200 Subject: [PATCH 17/62] Update deploy-artifacts.sh --- .config/deploy-artifacts.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.config/deploy-artifacts.sh b/.config/deploy-artifacts.sh index 7834d28..2db58d2 100644 --- a/.config/deploy-artifacts.sh +++ b/.config/deploy-artifacts.sh @@ -8,7 +8,7 @@ if [ "$TRAVIS_REPO_SLUG" == "citiususc/hipster" ] && [ "$TRAVIS_JDK_VERSION" == # Deploy to CITIUS #mvn --settings .config/maven-settings.xml -P citius-snapshot-deploy deploy -DskipTests=true # Deploy to Sonatype Nexus OSS - mvn --settings .config/maven-settings.xml deploy -DskipTests=true + mvn --settings .config/maven-settings.xml -P sonatype-nexus-snapshots deploy -DskipTests=true else echo "Skipping deployment for this build..." fi From 197f2d711b4be6b84785a0e6b204681c133613f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Thu, 30 Apr 2015 15:21:50 +0200 Subject: [PATCH 18/62] Update .travis.yml Activated auto-snapshots --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 88453c7..b5e190b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ after_success: - chmod +x .config/deploy-site.sh #- .config/deploy-site.sh - chmod +x .config/deploy-artifacts.sh -#- .config/deploy-artifacts.sh +- .config/deploy-artifacts.sh env: global: - secure: ILdOYjPt+8g5rlexXBYAhECtn5Zm26FRf0/nwCxUU303qtzFQyMcxinIC93aun880OnINjA7fzQeBkG4P+LSVOAXbmGTGhtyPBzMOGcnZouJM/RyXHUft6tAXPimXQ7JjDFjyv7EzSeStk/4WEp0mkxheIryZS3X1pbED1TqgUM= From 4ac3888f661523784671e18969239f479f7e4d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Thu, 30 Apr 2015 15:52:45 +0200 Subject: [PATCH 19/62] Update DepthFirstSearch.java StackFrameNode visibility changed to public --- .../java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java index 7215324..2820640 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java @@ -47,7 +47,7 @@ public DepthFirstSearch(N initialNode, NodeExpander expander) { this.initialNode = initialNode; } - private class StackFrameNode { + public class StackFrameNode { // Iterable used to compute neighbors of the current node java.util.Iterator successors; // Current search node From 44dc4f748e4721ea989e907dceece285f67214ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Thu, 30 Apr 2015 16:43:47 +0200 Subject: [PATCH 20/62] Update .travis.yml auto-deploy of snapshots activated for 1.0X --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 88453c7..9e25fa4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,10 +8,10 @@ branches: - master - development after_success: -- chmod +x .config/deploy-site.sh +#- chmod +x .config/deploy-site.sh #- .config/deploy-site.sh - chmod +x .config/deploy-artifacts.sh -#- .config/deploy-artifacts.sh +- .config/deploy-artifacts.sh env: global: - secure: ILdOYjPt+8g5rlexXBYAhECtn5Zm26FRf0/nwCxUU303qtzFQyMcxinIC93aun880OnINjA7fzQeBkG4P+LSVOAXbmGTGhtyPBzMOGcnZouJM/RyXHUft6tAXPimXQ7JjDFjyv7EzSeStk/4WEp0mkxheIryZS3X1pbED1TqgUM= From 00293f5a72a2b5954a4b33b60fba135205dcdc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Thu, 30 Apr 2015 16:44:35 +0200 Subject: [PATCH 21/62] Update deploy-artifacts.sh Updated script to deploy snapshots to sonatype --- .config/deploy-artifacts.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.config/deploy-artifacts.sh b/.config/deploy-artifacts.sh index 7834d28..2db58d2 100644 --- a/.config/deploy-artifacts.sh +++ b/.config/deploy-artifacts.sh @@ -8,7 +8,7 @@ if [ "$TRAVIS_REPO_SLUG" == "citiususc/hipster" ] && [ "$TRAVIS_JDK_VERSION" == # Deploy to CITIUS #mvn --settings .config/maven-settings.xml -P citius-snapshot-deploy deploy -DskipTests=true # Deploy to Sonatype Nexus OSS - mvn --settings .config/maven-settings.xml deploy -DskipTests=true + mvn --settings .config/maven-settings.xml -P sonatype-nexus-snapshots deploy -DskipTests=true else echo "Skipping deployment for this build..." fi From a05e72c35629949a811c0c43942e0e7deae0d7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Thu, 30 Apr 2015 16:46:39 +0200 Subject: [PATCH 22/62] Update DepthFirstSearch.java StackFrameNode changed to public --- .../java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java index 7215324..2820640 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java @@ -47,7 +47,7 @@ public DepthFirstSearch(N initialNode, NodeExpander expander) { this.initialNode = initialNode; } - private class StackFrameNode { + public class StackFrameNode { // Iterable used to compute neighbors of the current node java.util.Iterator successors; // Current search node From 7728a526543e72935cf6b9dbe2db32fa78722d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Thu, 30 Apr 2015 16:57:17 +0200 Subject: [PATCH 23/62] Update .travis.yml Removed travis branch blacklist --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9e25fa4..96ea8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,6 @@ jdk: - oraclejdk7 - oraclejdk8 - openjdk7 -branches: - only: - - master - - development after_success: #- chmod +x .config/deploy-site.sh #- .config/deploy-site.sh From 0d0e0f1562d8f5e0dc7594f7217dc8afc8a35db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Thu, 30 Apr 2015 18:38:53 +0200 Subject: [PATCH 24/62] added DFS.StackFrame getters --- .../hipster/algorithm/DepthFirstSearch.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java index 2820640..238dc3e 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/DepthFirstSearch.java @@ -20,6 +20,7 @@ import es.usc.citius.hipster.model.function.NodeExpander; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; import java.util.Stack; @@ -49,9 +50,9 @@ public DepthFirstSearch(N initialNode, NodeExpander expander) { public class StackFrameNode { // Iterable used to compute neighbors of the current node - java.util.Iterator successors; + private java.util.Iterator successors; // Current search node - N node; + private N node; // Boolean value to check if the node is still unvisited // in the stack or not boolean visited = false; @@ -67,6 +68,22 @@ public class StackFrameNode { this.node = node; this.successors = expander.expand(node).iterator(); } + + public N getNode() { + return node; + } + + public java.util.Iterator getSuccessors() { + return successors; + } + + public boolean isVisited() { + return visited; + } + + public boolean isProcessed() { + return processed; + } } /** From efd0308f89242c48683af7c0252cc98a8e15809e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Thu, 30 Apr 2015 18:50:02 +0200 Subject: [PATCH 25/62] version number back to 1.0.0-SNAPSHOT --- hipster-all/pom.xml | 2 +- hipster-core/pom.xml | 2 +- hipster-examples/pom.xml | 2 +- hipster-test/pom.xml | 2 +- hipster-third-party-graphs/pom.xml | 2 +- pom.xml | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/hipster-all/pom.xml b/hipster-all/pom.xml index 8b22b81..dc9eed4 100644 --- a/hipster-all/pom.xml +++ b/hipster-all/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT 4.0.0 hipster-all diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index 7b7d7da..06e77f9 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -5,7 +5,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT 4.0.0 hipster-core diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index 49de03e..7208490 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT 4.0.0 hipster-examples diff --git a/hipster-test/pom.xml b/hipster-test/pom.xml index db11f26..6f5efb9 100644 --- a/hipster-test/pom.xml +++ b/hipster-test/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT 4.0.0 hipster-test diff --git a/hipster-third-party-graphs/pom.xml b/hipster-third-party-graphs/pom.xml index d7dde60..028ad65 100644 --- a/hipster-third-party-graphs/pom.xml +++ b/hipster-third-party-graphs/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT 4.0.0 diff --git a/pom.xml b/pom.xml index adb1c3d..3a52d9c 100644 --- a/pom.xml +++ b/pom.xml @@ -15,13 +15,13 @@ http://hipster4j.org - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT es.usc.citius.hipster hipster-pom pom - 1.1.0-SNAPSHOT + 1.0.0-SNAPSHOT hipster-pom ${site.url} From 770bf03b2e35c06c69687ea53b01075de05ec60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Mon, 8 Jun 2015 16:48:00 +0200 Subject: [PATCH 26/62] Modified RomaniaProblemOptimalSearchTest structure --- ...ADStarRomaniaProblemOptimalSearchTest.java | 15 +++- .../AStarRomaniaProblemOptimalSearchTest.java | 16 +++-- ...jkstraRomaniaProblemOptimalSearchTest.java | 22 +++--- ...DAStarRomaniaProblemOptimalSearchTest.java | 22 +++++- .../RomaniaProblemOptimalSearchTest.java | 68 +++++++------------ 5 files changed, 84 insertions(+), 59 deletions(-) diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java index 3b07d37..8fd7457 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java @@ -1,5 +1,6 @@ package es.usc.citius.hipster.algorithm.problem.romanian; +import es.usc.citius.hipster.algorithm.ADStarForward; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; @@ -15,13 +16,23 @@ public class ADStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ @Override - public Iterator> createIterator() { + public void doSearch() { + //create search components SearchComponents components = GraphSearchProblem.startingFrom(RomanianProblem.City.Arad) .goalAt(RomanianProblem.City.Bucharest) .in(graph) .takeCostsFromEdges() .useHeuristicFunction(RomanianProblem.heuristicFunction()) .components(); - return Hipster.createADStar(components).iterator(); + //create iterator + ADStarForward.Iterator iterator = Hipster.createADStar(components).iterator(); + //find optimal solution + HeuristicNode node = null; + do{ + node = iterator.next(); + }while(iterator.hasNext() && !node.state().equals(GOAL)); + //set variables + this.optimalPathTested = node.path(); + this.expandedNodesTested = iterator.getClosed().values(); } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java index 4893d2b..8ae37bd 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java @@ -1,13 +1,12 @@ package es.usc.citius.hipster.algorithm.problem.romanian; +import es.usc.citius.hipster.algorithm.AStar; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; -import java.util.Iterator; - /** * @author Adrián González Sieira * @since 22/07/2014 @@ -15,7 +14,7 @@ public class AStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest { @Override - public Iterator> createIterator() { + public void doSearch() { SearchProblem p = GraphSearchProblem .startingFrom(RomanianProblem.City.Arad) .in(graph) @@ -23,6 +22,15 @@ public class AStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalS .useHeuristicFunction(RomanianProblem.heuristicFunction()) .build(); - return Hipster.createAStar(p).iterator(); + //create iterator + AStar.Iterator iterator = Hipster.createAStar(p).iterator(); + //find optimal solution + HeuristicNode node = null; + do{ + node = iterator.next(); + }while(iterator.hasNext() && !node.state().equals(GOAL)); + //set variables + this.optimalPathTested = node.path(); + this.expandedNodesTested = iterator.getClosed().values(); } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java index c956da6..e97c3a8 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java @@ -1,5 +1,6 @@ package es.usc.citius.hipster.algorithm.problem.romanian; +import es.usc.citius.hipster.algorithm.AStar; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; @@ -18,14 +19,22 @@ public class DijkstraRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ @Override - public Iterator> createIterator() { + public void doSearch() { SearchProblem p = GraphSearchProblem .startingFrom(RomanianProblem.City.Arad) .in(graph) .takeCostsFromEdges() .build(); - - return Hipster.createDijkstra(p).iterator(); + //create iterator + AStar.Iterator iterator = Hipster.createAStar(p).iterator(); + //find optimal solution + HeuristicNode node = null; + do{ + node = iterator.next(); + }while(iterator.hasNext() && !node.state().equals(GOAL)); + //set variables + this.optimalPathTested = node.path(); + this.expandedNodesTested = iterator.getClosed().values(); } @@ -35,15 +44,12 @@ public class DijkstraRomaniaProblemOptimalSearchTest extends RomaniaProblemOptim @Override @Test public void scoresFromAradToBucharest() { - HeuristicNode node; - //search optimal path - do{ - node = searchIterator.next(); + for(HeuristicNode node : expandedNodesTested){ //compare returned score with expected assertEquals( "Failed checking score of " + node.state().toString(), costsFromArad.get(node.state()), node.getScore() ); - }while(searchIterator.hasNext() && !node.state().equals(RomanianProblem.City.Bucharest)); + } } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java index e5e0c6d..4798d5b 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java @@ -1,12 +1,16 @@ package es.usc.citius.hipster.algorithm.problem.romanian; +import es.usc.citius.hipster.algorithm.AStar; import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.algorithm.IDAStar; import es.usc.citius.hipster.graph.GraphSearchProblem; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; /** * @author Adrián González Sieira @@ -15,7 +19,7 @@ public class IDAStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ @Override - public Iterator> createIterator() { + public void doSearch() { SearchProblem p = GraphSearchProblem .startingFrom(RomanianProblem.City.Arad) .in(graph) @@ -23,6 +27,20 @@ public class IDAStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptima .useHeuristicFunction(RomanianProblem.heuristicFunction()) .build(); - return Hipster.createIDAStar(p).iterator(); + List> expanded + = new ArrayList>(); + //create iterator + IDAStar.Iterator iterator = Hipster.createIDAStar(p).iterator(); + //find optimal solution + HeuristicNode node = null; + do{ + node = iterator.next(); + expanded.add(node); + }while(iterator.hasNext() && !node.state().equals(GOAL)); + //set variables + this.optimalPathTested = node.path(); + this.expandedNodesTested = expanded; } + + } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java index 6579e33..492c251 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java @@ -28,9 +28,12 @@ public abstract class RomaniaProblemOptimalSearchTest { protected final HipsterGraph graph = RomanianProblem.graph(); - protected Iterator> searchIterator; + protected Collection> expandedNodesTested; + protected List> optimalPathTested; + protected List optimalPath; protected final HashMap costsFromArad; protected final HashMap scoresFromArad; + protected static final RomanianProblem.City GOAL = RomanianProblem.City.Bucharest; public RomaniaProblemOptimalSearchTest(){ costsFromArad = new HashMap(); @@ -62,21 +65,8 @@ public RomaniaProblemOptimalSearchTest(){ scoresFromArad.put(RomanianProblem.City.Lugoj, 473d); scoresFromArad.put(RomanianProblem.City.Mehadia, 540d); scoresFromArad.put(RomanianProblem.City.Drobeta, 616d); - } - /** - * Check the returned path of the algorithm to be the optimal for the problem definition. - */ - @Test - public void optimalPathFromAradToBucharest() { - HeuristicNode node; - //search optimal path - do{ - // - node = searchIterator.next(); - }while(searchIterator.hasNext() && !node.state().equals(RomanianProblem.City.Bucharest)); - //list of cities in the optimal path - List optimalPath = + optimalPath = Arrays.asList( RomanianProblem.City.Arad, RomanianProblem.City.Sibiu, @@ -84,17 +74,22 @@ public void optimalPathFromAradToBucharest() { RomanianProblem.City.Pitesti, RomanianProblem.City.Bucharest ); - //path returned by the search algorithm - List> path = node.path(); + } + + /** + * Check the returned path of the algorithm to be the optimal for the problem definition. + */ + @Test + public void optimalPathFromAradToBucharest() { //check elements returned by the search algorithm - assertEquals("Solutions have not the same size", optimalPath.size(), path.size()); - for(int i = 0; i < path.size(); i++){ + assertEquals("Solutions have not the same size", optimalPathTested.size(), optimalPath.size()); + for(int i = 0; i < optimalPath.size(); i++){ //check if current element of the path is equals to the assertEquals( "Failed checking element " + i + " of the path. Expected: " + - optimalPath.get(i) + ", found: " + path.get(i).state(), - path.get(i).state(), - optimalPath.get(i) + optimalPath.get(i) + ", found: " + optimalPathTested.get(i).state(), + optimalPath.get(i), + optimalPathTested.get(i).state() ); } } @@ -104,10 +99,7 @@ public void optimalPathFromAradToBucharest() { */ @Test public void costsFromAradToBucharest() { - HeuristicNode node; - //search optimal path - do{ - node = searchIterator.next(); + for(HeuristicNode node : expandedNodesTested){ //compare returned cost with expected assertEquals( "Failed checking cost of " + node.state().toString() + ". Expected: " + @@ -115,7 +107,7 @@ public void costsFromAradToBucharest() { node.getCost(), costsFromArad.get(node.state()) ); - }while(searchIterator.hasNext() && !node.state().equals(RomanianProblem.City.Bucharest)); + } } /** @@ -123,31 +115,21 @@ public void costsFromAradToBucharest() { */ @Test public void scoresFromAradToBucharest() { - HeuristicNode node; - //search optimal path - do{ - node = searchIterator.next(); + for(HeuristicNode node : expandedNodesTested){ //compare returned score with expected assertEquals( "Failed checking score of " + node.state().toString(), scoresFromArad.get(node.state()), node.getScore() ); - }while(searchIterator.hasNext() && !node.state().equals(RomanianProblem.City.Bucharest)); - } - - /** - * Create a new instance of Iterator to run each test. - */ - @Before - public void initializeIterator(){ - this.searchIterator = createIterator(); + } } /** * Definition of abstract method to use the same test suite with different algorithms. - * - * @return instance of iterator for a search algorithm. + * This method fills the list of explored nodes and obtains the optimal path, wihch + * are stored in "expandedNodesTested" and "optmalPath" variables. */ - public abstract Iterator> createIterator(); + @Before + public abstract void doSearch(); } \ No newline at end of file From 4afe4a5b6717f2f521230321f4ccf3cea04b35b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Mon, 8 Jun 2015 18:51:02 +0200 Subject: [PATCH 27/62] Modified hierarchy of optimal search tests, added test for BellmanFord --- ...ADStarRomaniaProblemOptimalSearchTest.java | 2 +- .../AStarRomaniaProblemOptimalSearchTest.java | 2 +- ...anFordRomaniaProblemOptimalSearchTest.java | 45 ++++++++++++++++ ...jkstraRomaniaProblemOptimalSearchTest.java | 15 ------ ...DAStarRomaniaProblemOptimalSearchTest.java | 2 +- ...aniaProblemOptimalHeuristicSearchTest.java | 54 +++++++++++++++++++ .../RomaniaProblemOptimalSearchTest.java | 51 ++++++------------ 7 files changed, 118 insertions(+), 53 deletions(-) create mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java create mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java index 8fd7457..6dc824e 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java @@ -13,7 +13,7 @@ * @author Adrián González Sieira * @since 22/07/2014 */ -public class ADStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ +public class ADStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest{ @Override public void doSearch() { diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java index 8ae37bd..a0232d0 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java @@ -11,7 +11,7 @@ * @author Adrián González Sieira * @since 22/07/2014 */ -public class AStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest { +public class AStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest { @Override public void doSearch() { diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java new file mode 100644 index 0000000..b6bf2b1 --- /dev/null +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java @@ -0,0 +1,45 @@ +package es.usc.citius.hipster.algorithm.problem.romanian; + +import es.usc.citius.hipster.algorithm.BellmanFord; +import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; +import es.usc.citius.hipster.model.CostNode; +import es.usc.citius.hipster.model.problem.SearchProblem; +import es.usc.citius.hipster.util.examples.RomanianProblem; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +/** + * @author Adrián González Sieira + * @since 08/06/2015 + */ +public class BellmanFordRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ + + @Override + public void doSearch() { + SearchProblem p = GraphSearchProblem + .startingFrom(RomanianProblem.City.Arad) + .in(graph) + .takeCostsFromEdges() + .build(); + HashMap> expanded + = new HashMap>(); + //create iterator + BellmanFord.Iterator iterator = Hipster.createBellmanFord(p).iterator(); + //find optimal solution + CostNode node = null; + do{ + node = iterator.next(); + if(expanded.containsKey(node.state()) && expanded.get(node.state()).getCost() > node.getCost()) expanded.remove(node); + expanded.put(node.state(), node); + //update solution found + if(node.state().equals(GOAL) && (this.optimalPathTested == null || this.optimalPathTested.get(this.optimalPathTested.size() - 1).getCost() > node.getCost())){ + this.optimalPathTested = node.path(); + } + }while(iterator.hasNext()); + //set variables + this.expandedNodesTested = expanded.values(); + } +} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java index e97c3a8..ad12b3c 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java @@ -37,19 +37,4 @@ public void doSearch() { this.expandedNodesTested = iterator.getClosed().values(); } - - /** - * In the case of Dijkstra's Algorithm, - */ - @Override - @Test - public void scoresFromAradToBucharest() { - for(HeuristicNode node : expandedNodesTested){ - //compare returned score with expected - assertEquals( - "Failed checking score of " + node.state().toString(), - costsFromArad.get(node.state()), node.getScore() - ); - } - } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java index 4798d5b..c9becaf 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java @@ -16,7 +16,7 @@ * @author Adrián González Sieira * @since 31/07/2014 */ -public class IDAStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ +public class IDAStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest{ @Override public void doSearch() { diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java new file mode 100644 index 0000000..2b5efa4 --- /dev/null +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java @@ -0,0 +1,54 @@ +package es.usc.citius.hipster.algorithm.problem.romanian; + +import es.usc.citius.hipster.model.CostNode; +import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.util.examples.RomanianProblem; +import org.junit.Test; + +import java.util.Collection; +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +/** + * Created by adrian.gonzalez on 8/06/15. + */ +public abstract class RomaniaProblemOptimalHeuristicSearchTest extends RomaniaProblemOptimalSearchTest { + + protected final HashMap scoresFromArad; + protected Collection> expandedNodesTested; + + public RomaniaProblemOptimalHeuristicSearchTest(){ + super(); + //obtain score map for expanding nodes to Bucharest + scoresFromArad = new HashMap(); + scoresFromArad.put(RomanianProblem.City.Arad, 366d); + scoresFromArad.put(RomanianProblem.City.Zerind, 449d); + scoresFromArad.put(RomanianProblem.City.Timisoara, 447d); + scoresFromArad.put(RomanianProblem.City.Sibiu, 393d); + scoresFromArad.put(RomanianProblem.City.Oradea, 526d); + scoresFromArad.put(RomanianProblem.City.Fagaras, 415d); + scoresFromArad.put(RomanianProblem.City.Rimnicu_Vilcea, 413d); + scoresFromArad.put(RomanianProblem.City.Craiova, 526d); + scoresFromArad.put(RomanianProblem.City.Pitesti, 417d); + scoresFromArad.put(RomanianProblem.City.Bucharest, 418d); + scoresFromArad.put(RomanianProblem.City.Lugoj, 473d); + scoresFromArad.put(RomanianProblem.City.Mehadia, 540d); + scoresFromArad.put(RomanianProblem.City.Drobeta, 616d); + } + + /** + * Check the scores of the elements expanded by the algorithm. + */ + @Test + public void scoresFromAradToBucharest() { + for(HeuristicNode node : expandedNodesTested){ + //compare returned score with expected + assertEquals( + "Failed checking score of " + node.state().toString(), + scoresFromArad.get(node.state()), node.getScore() + ); + } + } + +} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java index 492c251..e6f88bc 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java @@ -1,6 +1,8 @@ package es.usc.citius.hipster.algorithm.problem.romanian; +import es.usc.citius.hipster.model.CostNode; import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.util.examples.RomanianProblem; import es.usc.citius.hipster.graph.HipsterGraph; import org.junit.Before; @@ -28,11 +30,10 @@ public abstract class RomaniaProblemOptimalSearchTest { protected final HipsterGraph graph = RomanianProblem.graph(); - protected Collection> expandedNodesTested; - protected List> optimalPathTested; + protected Collection> expandedNodesTested; + protected List> optimalPathTested; protected List optimalPath; protected final HashMap costsFromArad; - protected final HashMap scoresFromArad; protected static final RomanianProblem.City GOAL = RomanianProblem.City.Bucharest; public RomaniaProblemOptimalSearchTest(){ @@ -50,21 +51,13 @@ public RomaniaProblemOptimalSearchTest(){ costsFromArad.put(RomanianProblem.City.Lugoj, 229d); costsFromArad.put(RomanianProblem.City.Mehadia, 299d); costsFromArad.put(RomanianProblem.City.Drobeta, 374d); - //obtain score map for expanding nodes to Bucharest - scoresFromArad = new HashMap(); - scoresFromArad.put(RomanianProblem.City.Arad, 366d); - scoresFromArad.put(RomanianProblem.City.Zerind, 449d); - scoresFromArad.put(RomanianProblem.City.Timisoara, 447d); - scoresFromArad.put(RomanianProblem.City.Sibiu, 393d); - scoresFromArad.put(RomanianProblem.City.Oradea, 526d); - scoresFromArad.put(RomanianProblem.City.Fagaras, 415d); - scoresFromArad.put(RomanianProblem.City.Rimnicu_Vilcea, 413d); - scoresFromArad.put(RomanianProblem.City.Craiova, 526d); - scoresFromArad.put(RomanianProblem.City.Pitesti, 417d); - scoresFromArad.put(RomanianProblem.City.Bucharest, 418d); - scoresFromArad.put(RomanianProblem.City.Lugoj, 473d); - scoresFromArad.put(RomanianProblem.City.Mehadia, 540d); - scoresFromArad.put(RomanianProblem.City.Drobeta, 616d); + costsFromArad.put(RomanianProblem.City.Giurgiu, 508d); + costsFromArad.put(RomanianProblem.City.Urziceni, 503d); + costsFromArad.put(RomanianProblem.City.Hirsova, 601d); + costsFromArad.put(RomanianProblem.City.Eforie, 687d); + costsFromArad.put(RomanianProblem.City.Vaslui, 645d); + costsFromArad.put(RomanianProblem.City.Iasi, 737d); + costsFromArad.put(RomanianProblem.City.Neamt, 824d); optimalPath = Arrays.asList( @@ -99,31 +92,19 @@ public void optimalPathFromAradToBucharest() { */ @Test public void costsFromAradToBucharest() { - for(HeuristicNode node : expandedNodesTested){ + for(Node node : expandedNodesTested){ + CostNode costNode + = (CostNode) node; //compare returned cost with expected assertEquals( "Failed checking cost of " + node.state().toString() + ". Expected: " + - costsFromArad.get(node.state()) + ", found: " + node.getCost(), - node.getCost(), + costsFromArad.get(node.state()) + ", found: " + costNode.getCost(), + costNode.getCost(), costsFromArad.get(node.state()) ); } } - /** - * Check the scores of the elements expanded by the algorithm. - */ - @Test - public void scoresFromAradToBucharest() { - for(HeuristicNode node : expandedNodesTested){ - //compare returned score with expected - assertEquals( - "Failed checking score of " + node.state().toString(), - scoresFromArad.get(node.state()), node.getScore() - ); - } - } - /** * Definition of abstract method to use the same test suite with different algorithms. * This method fills the list of explored nodes and obtains the optimal path, wihch From d7f3d1682b0e202762c06e81842cbc2fa3a339f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Mon, 8 Jun 2015 18:51:02 +0200 Subject: [PATCH 28/62] Modified hierarchy of optimal search tests, added test for BellmanFord --- ...ADStarRomaniaProblemOptimalSearchTest.java | 2 +- .../AStarRomaniaProblemOptimalSearchTest.java | 2 +- ...anFordRomaniaProblemOptimalSearchTest.java | 45 +++++++++++++++ ...jkstraRomaniaProblemOptimalSearchTest.java | 15 ----- ...DAStarRomaniaProblemOptimalSearchTest.java | 7 ++- ...aniaProblemOptimalHeuristicSearchTest.java | 55 +++++++++++++++++++ .../RomaniaProblemOptimalSearchTest.java | 51 ++++++----------- 7 files changed, 122 insertions(+), 55 deletions(-) create mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java create mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java index 8fd7457..6dc824e 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java @@ -13,7 +13,7 @@ * @author Adrián González Sieira * @since 22/07/2014 */ -public class ADStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ +public class ADStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest{ @Override public void doSearch() { diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java index 8ae37bd..a0232d0 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java @@ -11,7 +11,7 @@ * @author Adrián González Sieira * @since 22/07/2014 */ -public class AStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest { +public class AStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest { @Override public void doSearch() { diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java new file mode 100644 index 0000000..b6bf2b1 --- /dev/null +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java @@ -0,0 +1,45 @@ +package es.usc.citius.hipster.algorithm.problem.romanian; + +import es.usc.citius.hipster.algorithm.BellmanFord; +import es.usc.citius.hipster.algorithm.Hipster; +import es.usc.citius.hipster.graph.GraphSearchProblem; +import es.usc.citius.hipster.model.CostNode; +import es.usc.citius.hipster.model.problem.SearchProblem; +import es.usc.citius.hipster.util.examples.RomanianProblem; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +/** + * @author Adrián González Sieira + * @since 08/06/2015 + */ +public class BellmanFordRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ + + @Override + public void doSearch() { + SearchProblem p = GraphSearchProblem + .startingFrom(RomanianProblem.City.Arad) + .in(graph) + .takeCostsFromEdges() + .build(); + HashMap> expanded + = new HashMap>(); + //create iterator + BellmanFord.Iterator iterator = Hipster.createBellmanFord(p).iterator(); + //find optimal solution + CostNode node = null; + do{ + node = iterator.next(); + if(expanded.containsKey(node.state()) && expanded.get(node.state()).getCost() > node.getCost()) expanded.remove(node); + expanded.put(node.state(), node); + //update solution found + if(node.state().equals(GOAL) && (this.optimalPathTested == null || this.optimalPathTested.get(this.optimalPathTested.size() - 1).getCost() > node.getCost())){ + this.optimalPathTested = node.path(); + } + }while(iterator.hasNext()); + //set variables + this.expandedNodesTested = expanded.values(); + } +} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java index e97c3a8..ad12b3c 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java @@ -37,19 +37,4 @@ public void doSearch() { this.expandedNodesTested = iterator.getClosed().values(); } - - /** - * In the case of Dijkstra's Algorithm, - */ - @Override - @Test - public void scoresFromAradToBucharest() { - for(HeuristicNode node : expandedNodesTested){ - //compare returned score with expected - assertEquals( - "Failed checking score of " + node.state().toString(), - costsFromArad.get(node.state()), node.getScore() - ); - } - } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java index 4798d5b..3ff8399 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java @@ -4,6 +4,7 @@ import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.algorithm.IDAStar; import es.usc.citius.hipster.graph.GraphSearchProblem; +import es.usc.citius.hipster.model.CostNode; import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; @@ -16,7 +17,7 @@ * @author Adrián González Sieira * @since 31/07/2014 */ -public class IDAStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ +public class IDAStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest{ @Override public void doSearch() { @@ -27,8 +28,8 @@ public void doSearch() { .useHeuristicFunction(RomanianProblem.heuristicFunction()) .build(); - List> expanded - = new ArrayList>(); + List> expanded + = new ArrayList>(); //create iterator IDAStar.Iterator iterator = Hipster.createIDAStar(p).iterator(); //find optimal solution diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java new file mode 100644 index 0000000..9d6432b --- /dev/null +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java @@ -0,0 +1,55 @@ +package es.usc.citius.hipster.algorithm.problem.romanian; + +import es.usc.citius.hipster.model.CostNode; +import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.util.examples.RomanianProblem; +import org.junit.Test; + +import java.util.Collection; +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +/** + * Created by adrian.gonzalez on 8/06/15. + */ +public abstract class RomaniaProblemOptimalHeuristicSearchTest extends RomaniaProblemOptimalSearchTest { + + protected final HashMap scoresFromArad; + + public RomaniaProblemOptimalHeuristicSearchTest(){ + super(); + //obtain score map for expanding nodes to Bucharest + scoresFromArad = new HashMap(); + scoresFromArad.put(RomanianProblem.City.Arad, 366d); + scoresFromArad.put(RomanianProblem.City.Zerind, 449d); + scoresFromArad.put(RomanianProblem.City.Timisoara, 447d); + scoresFromArad.put(RomanianProblem.City.Sibiu, 393d); + scoresFromArad.put(RomanianProblem.City.Oradea, 526d); + scoresFromArad.put(RomanianProblem.City.Fagaras, 415d); + scoresFromArad.put(RomanianProblem.City.Rimnicu_Vilcea, 413d); + scoresFromArad.put(RomanianProblem.City.Craiova, 526d); + scoresFromArad.put(RomanianProblem.City.Pitesti, 417d); + scoresFromArad.put(RomanianProblem.City.Bucharest, 418d); + scoresFromArad.put(RomanianProblem.City.Lugoj, 473d); + scoresFromArad.put(RomanianProblem.City.Mehadia, 540d); + scoresFromArad.put(RomanianProblem.City.Drobeta, 616d); + } + + /** + * Check the scores of the elements expanded by the algorithm. + */ + @Test + public void scoresFromAradToBucharest() { + for(CostNode node : expandedNodesTested){ + HeuristicNode heuristicNode = + (HeuristicNode) node; + //compare returned score with expected + assertEquals( + "Failed checking score of " + heuristicNode.state().toString(), + scoresFromArad.get(heuristicNode.state()), heuristicNode.getScore() + ); + } + } + +} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java index 492c251..e6f88bc 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java @@ -1,6 +1,8 @@ package es.usc.citius.hipster.algorithm.problem.romanian; +import es.usc.citius.hipster.model.CostNode; import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.util.examples.RomanianProblem; import es.usc.citius.hipster.graph.HipsterGraph; import org.junit.Before; @@ -28,11 +30,10 @@ public abstract class RomaniaProblemOptimalSearchTest { protected final HipsterGraph graph = RomanianProblem.graph(); - protected Collection> expandedNodesTested; - protected List> optimalPathTested; + protected Collection> expandedNodesTested; + protected List> optimalPathTested; protected List optimalPath; protected final HashMap costsFromArad; - protected final HashMap scoresFromArad; protected static final RomanianProblem.City GOAL = RomanianProblem.City.Bucharest; public RomaniaProblemOptimalSearchTest(){ @@ -50,21 +51,13 @@ public RomaniaProblemOptimalSearchTest(){ costsFromArad.put(RomanianProblem.City.Lugoj, 229d); costsFromArad.put(RomanianProblem.City.Mehadia, 299d); costsFromArad.put(RomanianProblem.City.Drobeta, 374d); - //obtain score map for expanding nodes to Bucharest - scoresFromArad = new HashMap(); - scoresFromArad.put(RomanianProblem.City.Arad, 366d); - scoresFromArad.put(RomanianProblem.City.Zerind, 449d); - scoresFromArad.put(RomanianProblem.City.Timisoara, 447d); - scoresFromArad.put(RomanianProblem.City.Sibiu, 393d); - scoresFromArad.put(RomanianProblem.City.Oradea, 526d); - scoresFromArad.put(RomanianProblem.City.Fagaras, 415d); - scoresFromArad.put(RomanianProblem.City.Rimnicu_Vilcea, 413d); - scoresFromArad.put(RomanianProblem.City.Craiova, 526d); - scoresFromArad.put(RomanianProblem.City.Pitesti, 417d); - scoresFromArad.put(RomanianProblem.City.Bucharest, 418d); - scoresFromArad.put(RomanianProblem.City.Lugoj, 473d); - scoresFromArad.put(RomanianProblem.City.Mehadia, 540d); - scoresFromArad.put(RomanianProblem.City.Drobeta, 616d); + costsFromArad.put(RomanianProblem.City.Giurgiu, 508d); + costsFromArad.put(RomanianProblem.City.Urziceni, 503d); + costsFromArad.put(RomanianProblem.City.Hirsova, 601d); + costsFromArad.put(RomanianProblem.City.Eforie, 687d); + costsFromArad.put(RomanianProblem.City.Vaslui, 645d); + costsFromArad.put(RomanianProblem.City.Iasi, 737d); + costsFromArad.put(RomanianProblem.City.Neamt, 824d); optimalPath = Arrays.asList( @@ -99,31 +92,19 @@ public void optimalPathFromAradToBucharest() { */ @Test public void costsFromAradToBucharest() { - for(HeuristicNode node : expandedNodesTested){ + for(Node node : expandedNodesTested){ + CostNode costNode + = (CostNode) node; //compare returned cost with expected assertEquals( "Failed checking cost of " + node.state().toString() + ". Expected: " + - costsFromArad.get(node.state()) + ", found: " + node.getCost(), - node.getCost(), + costsFromArad.get(node.state()) + ", found: " + costNode.getCost(), + costNode.getCost(), costsFromArad.get(node.state()) ); } } - /** - * Check the scores of the elements expanded by the algorithm. - */ - @Test - public void scoresFromAradToBucharest() { - for(HeuristicNode node : expandedNodesTested){ - //compare returned score with expected - assertEquals( - "Failed checking score of " + node.state().toString(), - scoresFromArad.get(node.state()), node.getScore() - ); - } - } - /** * Definition of abstract method to use the same test suite with different algorithms. * This method fills the list of explored nodes and obtains the optimal path, wihch From 0170aeea20bfc1ab33f456d90a7135102bf1f62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Tue, 9 Jun 2015 17:09:30 +0200 Subject: [PATCH 29/62] replaced path.separator by line.separator in NQueens --- .../java/es/usc/citius/hipster/examples/problem/NQueens.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/problem/NQueens.java b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/problem/NQueens.java index 59048dc..b9db824 100644 --- a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/problem/NQueens.java +++ b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/problem/NQueens.java @@ -30,7 +30,7 @@ * in Peter Alfeld's Web Page (University of Utah) */ public class NQueens { - private static final String LS = System.getProperty("path.separator"); + private static final String LS = System.getProperty("line.separator"); // if N = 8: From 70caf604c3732cd0491fbe16b6d8b9768ec115ee Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Wed, 10 Jun 2015 11:48:24 +0200 Subject: [PATCH 30/62] Improved the efficiency of HashBasedHipsterGraph (lazy flat iterator of edges) --- .../hipster/graph/HashBasedHipsterGraph.java | 99 ++++++++++++++++--- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 17d2e5e..34b7de3 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -16,14 +16,13 @@ package es.usc.citius.hipster.graph; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.util.*; /** * Implementation of a HipsterGraph using a Guava Hash Table. * * @author Adrián González Sieira <adrian.gonzalez@usc.es> + * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ public class HashBasedHipsterGraph implements HipsterGraph { protected HashMap>> connected; @@ -35,7 +34,7 @@ public HashBasedHipsterGraph(){ /** * Add a new node to the graph with no connections. * - * @param v + * @param v vertex to be added */ public void add(V v){ //add a new entry to the hash map if it does not exist @@ -44,6 +43,11 @@ public void add(V v){ } } + /** + * Remove a vertex from the graph. + * + * @param v vertex to be removed + */ public void remove(V v){ // Get vertices connected with this one for(GraphEdge edge : edgesOf(v)){ @@ -61,27 +65,90 @@ public void remove(V v){ connected.remove(v); // v no longer exists } + /** + * Connect to vertices in the graph. If the vertices are not in the graph, they are automatically + * added to the graph before connecting them. + * + * @param v1 source vertex + * @param v2 destination vertex + * @param value edge value + * @return + */ public GraphEdge connect(V v1, V v2, E value){ - //check input + // Check non-null arguments if(v1 == null || v2 == null) throw new IllegalArgumentException("Vertices cannot be null"); + // Ensure that the vertices are in the graph + add(v1); + add(v2); GraphEdge edge = new GraphEdge(v1, v2, value); - GraphEdge edgeReverse = new GraphEdge(v2, v1, value); - //add edges to the graph (if not present before) + GraphEdge reversedEdge = new GraphEdge(v2, v1, value); + // Add edges to the graph connected.get(v1).add(edge); - connected.get(v2).add(edgeReverse); + connected.get(v2).add(reversedEdge); return edge; } + /** + * Returns a list of the edges in the graph. + * @return edges of the graph. + */ @Override public Iterable> edges() { - ArrayList> edges = new ArrayList>(); - //store all edges in an array - for(List> current : connected.values()){ - edges.addAll(current); - } - return edges; + final Collection>> edges = connected.values(); + // TODO: Change this ugly lazy iterator with Java 8 streams and flatmaps + return new Iterable>() { + @Override + public Iterator> iterator() { + return new Iterator>() { + private Iterator>> it = edges.iterator(); + private Iterator> currentList = it.next().iterator(); + private GraphEdge nextElement = null; + + private GraphEdge loadNext(){ + // Preload the next element + GraphEdge next = null; + if (currentList.hasNext()) { + next = currentList.next(); + } else if (it.hasNext()){ + currentList = it.next().iterator(); + if (currentList.hasNext()) { + next = currentList.next(); + } + } + return next; + } + + @Override + public boolean hasNext() { + // There can be empty lists, so we need to pre-compute the next element in advance + // to check whether there exist a next element or not. + if (nextElement == null) { + nextElement = loadNext(); + } + return nextElement != null; + } + + @Override + public GraphEdge next() { + // Load the next element + if (nextElement != null) { + GraphEdge next = nextElement; + nextElement = null; + return next; + } else { + return loadNext(); + } + } + }; + } + }; } + /** + * Returns the vertices of the graph. Any changes in the + * returned iterator affect the underlying graph structure. + * @return iterator with the vertices of the graph + */ @Override public Iterable vertices() { return connected.keySet(); @@ -89,7 +156,9 @@ public Iterable vertices() { @Override public Iterable> edgesOf(V vertex) { - return connected.get(vertex); + List> list = connected.get(vertex); + if (list == null) list = Collections.emptyList(); + return list; } public static HashBasedHipsterGraph create() { From aba35f465aa2348566b9c04c0bd06c390b7efa79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Gonz=C3=A1lez=20Sieira?= Date: Thu, 11 Jun 2015 18:25:34 +0200 Subject: [PATCH 31/62] close #127 : corrected BellmanFord.search(), added coverage for this method in tests --- .../citius/hipster/algorithm/BellmanFord.java | 3 +- ...ADStarRomaniaProblemOptimalSearchTest.java | 28 ++++++--- .../AStarRomaniaProblemOptimalSearchTest.java | 29 ++++++--- ...anFordRomaniaProblemOptimalSearchTest.java | 34 +++++++---- ...jkstraRomaniaProblemOptimalSearchTest.java | 25 +++++--- ...DAStarRomaniaProblemOptimalSearchTest.java | 30 ++++++---- ...aniaProblemOptimalHeuristicSearchTest.java | 9 ++- .../RomaniaProblemOptimalSearchTest.java | 59 +++++++++++++++---- 8 files changed, 150 insertions(+), 67 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java index efd0dbe..ac520a2 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/BellmanFord.java @@ -137,11 +137,10 @@ public SearchResult search(Predicate condition){ N goalNode = null; while(it.hasNext()){ iteration++; - it.next(); + currentNode = it.next(); if (condition.apply(currentNode)) { goalNode = currentNode; } - } long end = System.currentTimeMillis(); if (goalNode != null) { diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java index 6dc824e..11ccc59 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/ADStarRomaniaProblemOptimalSearchTest.java @@ -1,38 +1,48 @@ package es.usc.citius.hipster.algorithm.problem.romanian; import es.usc.citius.hipster.algorithm.ADStarForward; +import es.usc.citius.hipster.algorithm.Algorithm; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.graph.GraphSearchProblem; -import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.model.problem.SearchComponents; import es.usc.citius.hipster.util.examples.RomanianProblem; import java.util.Iterator; +import java.util.List; /** + * Implementation of the Romania problem test for the AD* algorithm. + * * @author Adrián González Sieira * @since 22/07/2014 */ public class ADStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest{ @Override - public void doSearch() { - //create search components + public Algorithm> createAlgorithm() { + //initialize search problem SearchComponents components = GraphSearchProblem.startingFrom(RomanianProblem.City.Arad) .goalAt(RomanianProblem.City.Bucharest) .in(graph) .takeCostsFromEdges() .useHeuristicFunction(RomanianProblem.heuristicFunction()) .components(); - //create iterator - ADStarForward.Iterator iterator = Hipster.createADStar(components).iterator(); + + //create AD* algorithm + return Hipster.createADStar(components); + } + + @Override + public List> iterativeSearch(Iterator> iterator) { //find optimal solution - HeuristicNode node = null; + Node node = null; do{ node = iterator.next(); }while(iterator.hasNext() && !node.state().equals(GOAL)); - //set variables - this.optimalPathTested = node.path(); - this.expandedNodesTested = iterator.getClosed().values(); + //set variable of expanded nodes + this.expandedNodesTested = ((ADStarForward.Iterator) iterator).getClosed().values(); + //return optimal path + return node.path(); } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java index a0232d0..d15a318 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/AStarRomaniaProblemOptimalSearchTest.java @@ -1,20 +1,27 @@ package es.usc.citius.hipster.algorithm.problem.romanian; import es.usc.citius.hipster.algorithm.AStar; +import es.usc.citius.hipster.algorithm.Algorithm; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.graph.GraphSearchProblem; -import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; +import java.util.Iterator; +import java.util.List; + /** + * Implementation of the Romania problem test for the A* algorithm. + * * @author Adrián González Sieira * @since 22/07/2014 */ public class AStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest { @Override - public void doSearch() { + public Algorithm> createAlgorithm() { + //initialize search problem SearchProblem p = GraphSearchProblem .startingFrom(RomanianProblem.City.Arad) .in(graph) @@ -22,15 +29,21 @@ public void doSearch() { .useHeuristicFunction(RomanianProblem.heuristicFunction()) .build(); - //create iterator - AStar.Iterator iterator = Hipster.createAStar(p).iterator(); + //create A* algorithm + return Hipster.createAStar(p); + } + + @Override + public List> iterativeSearch(Iterator> iterator) { //find optimal solution - HeuristicNode node = null; + Node node = null; do{ node = iterator.next(); }while(iterator.hasNext() && !node.state().equals(GOAL)); - //set variables - this.optimalPathTested = node.path(); - this.expandedNodesTested = iterator.getClosed().values(); + //set variables of expanded nodes + this.expandedNodesTested = ((AStar.Iterator) iterator).getClosed().values(); + //return optimal path + return node.path(); } + } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java index b6bf2b1..2c980ef 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/BellmanFordRomaniaProblemOptimalSearchTest.java @@ -1,45 +1,55 @@ package es.usc.citius.hipster.algorithm.problem.romanian; -import es.usc.citius.hipster.algorithm.BellmanFord; +import es.usc.citius.hipster.algorithm.Algorithm; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.graph.GraphSearchProblem; -import es.usc.citius.hipster.model.CostNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; +import java.util.Iterator; +import java.util.List; /** + * Implementation of the Romania problem test for the Bellman-Ford algorithm. + * * @author Adrián González Sieira * @since 08/06/2015 */ public class BellmanFordRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ @Override - public void doSearch() { + public Algorithm> createAlgorithm() { SearchProblem p = GraphSearchProblem .startingFrom(RomanianProblem.City.Arad) .in(graph) .takeCostsFromEdges() .build(); - HashMap> expanded - = new HashMap>(); + //create iterator - BellmanFord.Iterator iterator = Hipster.createBellmanFord(p).iterator(); + return Hipster.createBellmanFord(p); + } + + @Override + public List> iterativeSearch(Iterator> iterator) { + HashMap> expanded + = new HashMap>(); //find optimal solution - CostNode node = null; + Node node = null; + Node goalNode = null; do{ node = iterator.next(); - if(expanded.containsKey(node.state()) && expanded.get(node.state()).getCost() > node.getCost()) expanded.remove(node); + if(expanded.containsKey(node.state())) expanded.remove(node); expanded.put(node.state(), node); //update solution found - if(node.state().equals(GOAL) && (this.optimalPathTested == null || this.optimalPathTested.get(this.optimalPathTested.size() - 1).getCost() > node.getCost())){ - this.optimalPathTested = node.path(); + if(node.state().equals(GOAL)){ + goalNode = node; } }while(iterator.hasNext()); //set variables this.expandedNodesTested = expanded.values(); + //return optimal path + return goalNode.path(); } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java index ad12b3c..0d6feda 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/DijkstraRomaniaProblemOptimalSearchTest.java @@ -1,40 +1,49 @@ package es.usc.citius.hipster.algorithm.problem.romanian; import es.usc.citius.hipster.algorithm.AStar; +import es.usc.citius.hipster.algorithm.Algorithm; import es.usc.citius.hipster.algorithm.Hipster; import es.usc.citius.hipster.graph.GraphSearchProblem; -import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; -import org.junit.Test; import java.util.Iterator; +import java.util.List; import static org.junit.Assert.assertEquals; /** + * Implementation of the Romania problem test for the Dijkstra algorithm. + * * @author Adrián González Sieira * @since 31/07/2014 */ public class DijkstraRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalSearchTest{ @Override - public void doSearch() { + public Algorithm> createAlgorithm() { + //initialize search problem SearchProblem p = GraphSearchProblem .startingFrom(RomanianProblem.City.Arad) .in(graph) .takeCostsFromEdges() .build(); - //create iterator - AStar.Iterator iterator = Hipster.createAStar(p).iterator(); + //create Dijkstra algorithm + return Hipster.createAStar(p); + } + + @Override + public List> iterativeSearch(Iterator> iterator) { //find optimal solution - HeuristicNode node = null; + Node node = null; do{ node = iterator.next(); }while(iterator.hasNext() && !node.state().equals(GOAL)); //set variables - this.optimalPathTested = node.path(); - this.expandedNodesTested = iterator.getClosed().values(); + this.expandedNodesTested = ((AStar.Iterator) iterator).getClosed().values(); + //return optimal path + return node.path(); } } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java index 3ff8399..3961c0d 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/IDAStarRomaniaProblemOptimalSearchTest.java @@ -1,11 +1,9 @@ package es.usc.citius.hipster.algorithm.problem.romanian; -import es.usc.citius.hipster.algorithm.AStar; +import es.usc.citius.hipster.algorithm.Algorithm; import es.usc.citius.hipster.algorithm.Hipster; -import es.usc.citius.hipster.algorithm.IDAStar; import es.usc.citius.hipster.graph.GraphSearchProblem; -import es.usc.citius.hipster.model.CostNode; -import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.model.problem.SearchProblem; import es.usc.citius.hipster.util.examples.RomanianProblem; @@ -14,13 +12,16 @@ import java.util.List; /** + * Implementation of the Romania problem test for the IDA* algorithm. + * * @author Adrián González Sieira * @since 31/07/2014 */ public class IDAStarRomaniaProblemOptimalSearchTest extends RomaniaProblemOptimalHeuristicSearchTest{ @Override - public void doSearch() { + public Algorithm> createAlgorithm() { + //initialize search problem SearchProblem p = GraphSearchProblem .startingFrom(RomanianProblem.City.Arad) .in(graph) @@ -28,20 +29,23 @@ public void doSearch() { .useHeuristicFunction(RomanianProblem.heuristicFunction()) .build(); - List> expanded - = new ArrayList>(); - //create iterator - IDAStar.Iterator iterator = Hipster.createIDAStar(p).iterator(); + //create IDA* algorithm + return Hipster.createIDAStar(p); + } + + @Override + public List> iterativeSearch(Iterator> iterator) { + List> expanded + = new ArrayList>(); //find optimal solution - HeuristicNode node = null; + Node node = null; do{ node = iterator.next(); expanded.add(node); }while(iterator.hasNext() && !node.state().equals(GOAL)); - //set variables - this.optimalPathTested = node.path(); this.expandedNodesTested = expanded; + //return optimal path + return node.path(); } - } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java index 9d6432b..0681094 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalHeuristicSearchTest.java @@ -1,11 +1,10 @@ package es.usc.citius.hipster.algorithm.problem.romanian; -import es.usc.citius.hipster.model.CostNode; import es.usc.citius.hipster.model.HeuristicNode; +import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.util.examples.RomanianProblem; import org.junit.Test; -import java.util.Collection; import java.util.HashMap; import static org.junit.Assert.assertEquals; @@ -41,9 +40,9 @@ public RomaniaProblemOptimalHeuristicSearchTest(){ */ @Test public void scoresFromAradToBucharest() { - for(CostNode node : expandedNodesTested){ - HeuristicNode heuristicNode = - (HeuristicNode) node; + for(Node node : expandedNodesTested){ + HeuristicNode heuristicNode = + (HeuristicNode) node; //compare returned score with expected assertEquals( "Failed checking score of " + heuristicNode.state().toString(), diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java index e6f88bc..1c656ef 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/algorithm/problem/romanian/RomaniaProblemOptimalSearchTest.java @@ -1,7 +1,7 @@ package es.usc.citius.hipster.algorithm.problem.romanian; +import es.usc.citius.hipster.algorithm.Algorithm; import es.usc.citius.hipster.model.CostNode; -import es.usc.citius.hipster.model.HeuristicNode; import es.usc.citius.hipster.model.Node; import es.usc.citius.hipster.util.examples.RomanianProblem; import es.usc.citius.hipster.graph.HipsterGraph; @@ -30,8 +30,9 @@ public abstract class RomaniaProblemOptimalSearchTest { protected final HipsterGraph graph = RomanianProblem.graph(); - protected Collection> expandedNodesTested; - protected List> optimalPathTested; + protected Collection> expandedNodesTested; + protected List> optimalPathIterator; + protected List> optimalPathSearchMethod; protected List optimalPath; protected final HashMap costsFromArad; protected static final RomanianProblem.City GOAL = RomanianProblem.City.Bucharest; @@ -75,14 +76,32 @@ public RomaniaProblemOptimalSearchTest(){ @Test public void optimalPathFromAradToBucharest() { //check elements returned by the search algorithm - assertEquals("Solutions have not the same size", optimalPathTested.size(), optimalPath.size()); + assertEquals("Solutions have not the same size", optimalPathIterator.size(), optimalPath.size()); for(int i = 0; i < optimalPath.size(); i++){ //check if current element of the path is equals to the assertEquals( "Failed checking element " + i + " of the path. Expected: " + - optimalPath.get(i) + ", found: " + optimalPathTested.get(i).state(), + optimalPath.get(i) + ", found: " + optimalPathIterator.get(i).state(), optimalPath.get(i), - optimalPathTested.get(i).state() + optimalPathIterator.get(i).state() + ); + } + } + + /** + * Check the returned path of the algorithm to be the optimal for the problem definition. + */ + @Test + public void optimalPathFromAradToBucharestSearchMethod() { + //check elements returned by the search algorithm + assertEquals("Solutions have not the same size", optimalPathSearchMethod.size(), optimalPath.size()); + for(int i = 0; i < optimalPath.size(); i++){ + //check if current element of the path is equals to the + assertEquals( + "Failed checking element " + i + " of the path. Expected: " + + optimalPath.get(i) + ", found: " + optimalPathSearchMethod.get(i).state(), + optimalPath.get(i), + optimalPathSearchMethod.get(i).state() ); } } @@ -92,9 +111,9 @@ public void optimalPathFromAradToBucharest() { */ @Test public void costsFromAradToBucharest() { - for(Node node : expandedNodesTested){ - CostNode costNode - = (CostNode) node; + for(Node node : expandedNodesTested){ + CostNode costNode + = (CostNode) node; //compare returned cost with expected assertEquals( "Failed checking cost of " + node.state().toString() + ". Expected: " + @@ -111,6 +130,26 @@ public void costsFromAradToBucharest() { * are stored in "expandedNodesTested" and "optmalPath" variables. */ @Before - public abstract void doSearch(); + public void doSearch(){ + Algorithm> algorithm = createAlgorithm(); + //execute iterative search + this.optimalPathIterator = iterativeSearch(algorithm.iterator()); + this.optimalPathSearchMethod = algorithm.search(RomanianProblem.City.Bucharest).getGoalNode().path(); + }; + + /** + * Method to execute the iterative search from a search iterator. + * + * @param iterator instance of {@link Iterator>} to search + * @return solution list obtained by sarching step-by-step + */ + public abstract List> iterativeSearch(Iterator> iterator); + + /** + * Method to initialize the search algorithm. + * + * @return instance of {@link Algorithm} to search over the graph + */ + public abstract Algorithm> createAlgorithm(); } \ No newline at end of file From dc463896605af4e2e6e01c7523f539da4146623c Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Fri, 12 Jun 2015 20:37:12 +0200 Subject: [PATCH 32/62] [WIP] Hipster graphs impl refactor. Added new tests --- hipster-core/pom.xml | 8 + .../citius/hipster/graph/DirectedEdge.java | 51 +++++ .../citius/hipster/graph/GraphBuilder.java | 192 ++++++++---------- .../usc/citius/hipster/graph/GraphEdge.java | 98 ++------- .../hipster/graph/GraphSearchProblem.java | 3 - .../graph/HashBasedHipsterDirectedGraph.java | 77 +++++-- .../hipster/graph/HashBasedHipsterGraph.java | 186 +++++++++++++---- .../hipster/graph/HipsterMutableGraph.java | 8 + .../es/usc/citius/hipster/graph/Pair.java | 48 +++++ .../citius/hipster/graph/UndirectedEdge.java | 52 +++++ .../citius/hipster/graph/UnorderedPair.java | 46 +++++ .../citius/hipster/graph/WeightedEdge.java | 44 ---- .../es/usc/citius/hipster/util/Iterators.java | 76 +++++++ .../graph/HashBasedHipsterGraphTest.java | 104 ++++++++++ .../hipster/graph/UndirectedEdgeTest.java | 29 +++ .../hipster/util/graph/GraphBuilderTest.java | 4 +- .../graph/HashBasedHipsterDirectedGraph.java | 3 +- .../graph/HashBasedHipsterGraph.java | 4 +- .../HashBasedHipsterDirectedGraphTest.java | 38 ++-- .../graph/HashBasedHipsterGraphTest.java | 21 +- 20 files changed, 753 insertions(+), 339 deletions(-) create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/graph/DirectedEdge.java create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/graph/UndirectedEdge.java create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java delete mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/graph/WeightedEdge.java create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java create mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterGraphTest.java create mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/graph/UndirectedEdgeTest.java diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index 06e77f9..dc0d5bf 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -14,4 +14,12 @@ ${project.basedir}/.. + + + com.google.guava + guava + test + + + \ No newline at end of file diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/DirectedEdge.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/DirectedEdge.java new file mode 100644 index 0000000..5899df5 --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/DirectedEdge.java @@ -0,0 +1,51 @@ +package es.usc.citius.hipster.graph; + +public class DirectedEdge implements GraphEdge { + + private Pair vertices; + private E value; + + public DirectedEdge(V vertex1, V vertex2, E value) { + this.vertices = new Pair(vertex1, vertex2); + this.value = value; + } + + @Override + public V getVertex1() { + return vertices.getE1(); + } + + @Override + public V getVertex2() { + return vertices.getE2(); + } + + @Override + public E getEdgeValue() { + return value; + } + + @Override + public Type getType() { + return Type.DIRECTED; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DirectedEdge that = (DirectedEdge) o; + + if (!vertices.equals(that.vertices)) return false; + return !(value != null ? !value.equals(that.value) : that.value != null); + + } + + @Override + public int hashCode() { + int result = vertices.hashCode(); + result = 31 * result + (value != null ? value.hashCode() : 0); + return result; + } +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java index 1476da3..4835ace 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java @@ -16,10 +16,6 @@ package es.usc.citius.hipster.graph; - -import es.usc.citius.hipster.graph.HashBasedHipsterDirectedGraph; -import es.usc.citius.hipster.graph.HashBasedHipsterGraph; - import java.util.LinkedList; import java.util.List; @@ -27,124 +23,100 @@ *

    Graph builder assistant to create a Hipster graph. Usage example:

    *
      * {@code
    - * HipsterDirectedGraph graph = GraphBuilder.create()
    - *     .connect("A").to("B").withEdge(4d)
    - *     .connect("A").to("C").withEdge(2d)
    - *     .connect("B").to("C").withEdge(5d)
    - *     .connect("B").to("D").withEdge(10d)
    - *     .connect("C").to("E").withEdge(3d)
    - *     .connect("D").to("F").withEdge(11d)
    - *     .connect("E").to("D").withEdge(4d)
    - *     .buildDirectedGraph();
    + * HipsterGraph =
    + * GraphBuilder.create()
    + * .connect("A").to("B").withEdge(4d)
    + * .connect("A").to("C").withEdge(2d)
    + * .connect("B").to("C").withEdge(5d)
    + * .createDirectedGraph();
      * }
      * 
    */ -public class GraphBuilder { +public class GraphBuilder { - public final static class Assistant { + private class Connection { + private V vertex1; + private V vertex2; + private E edge; - public final class Vertex1 { - V v1; + private Connection(V vertex1, V vertex2, E edge) { + this.vertex1 = vertex1; + this.vertex2 = vertex2; + this.edge = edge; + } + } - private Vertex1(V v1) { - this.v1 = v1; - } + private List connections = new LinkedList(); - public final class Vertex2 { - V v2; - - private Vertex2(V v2) { - this.v2 = v2; - } - - public final class Builder { - - private List connections = new LinkedList(); - - private class Connection { - private VT vertex1; - private VT vertex2; - private ET edge; - - private Connection(VT vertex1, VT vertex2, ET edge) { - this.vertex1 = vertex1; - this.vertex2 = vertex2; - this.edge = edge; - } - } - - public Builder(VT v1, VT v2, ET edge){ - connections.add(new Connection(v1, v2, edge)); - } - - public HipsterDirectedGraph buildDirectedGraph(){ - HashBasedHipsterDirectedGraph graph = HashBasedHipsterDirectedGraph.create(); - for(Connection c : connections){ - graph.add(c.vertex1); - graph.add(c.vertex2); - graph.connect(c.vertex1, c.vertex2, c.edge); - } - return graph; - } - - public HipsterGraph buildUndirectedGraph(){ - HashBasedHipsterGraph graph = HashBasedHipsterGraph.create(); - for(Connection c : connections){ - graph.add(c.vertex1); - graph.add(c.vertex2); - graph.connect(c.vertex1, c.vertex2, c.edge); - } - return graph; - } - - public final class Vertex1T { - private VT v1t; - - private Vertex1T(VT v1t) { - this.v1t = v1t; - } - - public final class Vertex2T { - private VT v2t; - - private Vertex2T(VT vertex) { - this.v2t = vertex; - } - - public Builder withEdge(ET edge){ - connections.add(new Connection(v1t, v2t, edge)); - return Builder.this; - } - - } - - public Vertex2T to(VT vertex){ - return new Vertex2T(vertex); - } - } - - public Vertex1T connect(VT vertex){ - return new Vertex1T(vertex); - } - - } - - public Builder withEdge(E edge){ - return new Builder(v1, v2, edge); - } - } + private GraphBuilder() {} - public Vertex2 to(V vertex){ - return new Vertex2(vertex); - } + public static GraphBuilder create() { + return new GraphBuilder(); + } + + + public Vertex1 connect(V vertex) { + return new Vertex1(vertex); + } + + public HipsterDirectedGraph createDirectedGraph() { + HashBasedHipsterDirectedGraph graph = HashBasedHipsterDirectedGraph.create(); + for (Connection c : connections) { + graph.connect(c.vertex1, c.vertex2, c.edge); } - public Vertex1 connect(V vertex){ - return new Vertex1(vertex); + return graph; + } + + public HipsterGraph createUndirectedGraph() { + HashBasedHipsterGraph graph = HashBasedHipsterGraph.create(); + for (Connection c : connections) { + graph.connect(c.vertex1, c.vertex2, c.edge); } + return graph; } - public static Assistant create(){ - return new Assistant(); + /** + * @see GraphBuilder#createDirectedGraph() + * @return type-erased directed graph + */ + @Deprecated + public HipsterDirectedGraph buildDirectedGraph(){ + return createDirectedGraph(); + } + + /** + * @see GraphBuilder#createUndirectedGraph() + * @return type-erased undirected graph + */ + @Deprecated + public HipsterGraph buildUndirectedGraph(){ + return createUndirectedGraph(); + } + + + public final class Vertex1 { + V vertex1; + + private Vertex1(V vertex) { + this.vertex1 = vertex; + } + + public Vertex2 to(V vertex) { + return new Vertex2(vertex); + } + + public class Vertex2 { + V vertex2; + + private Vertex2(V vertex) { + this.vertex2 = vertex; + } + + public GraphBuilder withEdge(E edge) { + connections.add(new Connection(vertex1, vertex2, edge)); + return GraphBuilder.this; + } + } } } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphEdge.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphEdge.java index 531d00f..b963038 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphEdge.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphEdge.java @@ -24,90 +24,16 @@ * @param vertex type. * @param edge type. */ -public final class GraphEdge { - private V vertex1; // endpoint 1 (source vertex in a directed graph) - private V vertex2; // endpoint 2 (target vertex in a directed graph) - private E edgeValue; // custom value associated to the edge - private boolean directed = false; - - public GraphEdge(V vertex1, V vertex2, E edgeValue) { - this(vertex1, vertex2, edgeValue, false); - } - - public GraphEdge(V vertex1, V vertex2, E edgeValue, boolean directed) { - if(vertex1 == null || vertex2 == null) throw new IllegalArgumentException("Vertices cannot be null"); - this.vertex1 = vertex1; - this.vertex2 = vertex2; - this.edgeValue = edgeValue; - this.directed = directed; - } - - /** - * Returns one of the endpoints of the edge. If the edge - * is directed, then the vertex1 corresponds with the source endpoint. - * @return vertex endpoint. - */ - public V getVertex1() { - return vertex1; - } - - /** - * Returns one of the endpoints of the edge. If the edge - * is directed, then the vertex2 corresponds with the target endpoint. - * @return vertex endpoint. - */ - public V getVertex2() { - return vertex2; - } - - public V getVertexAdjacentTo(V vertex){ - if (vertex1.equals(vertex)){ - return vertex2; - } else if (vertex2.equals(vertex)){ - return vertex1; - } else { - return null; - } - } - - /** - * Return the value assigned to the edge. - * @return value of the edge. - */ - public E getEdgeValue() { - return edgeValue; - } - - public boolean isDirected() { - return directed; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - GraphEdge graphEdge = (GraphEdge) o; - - if (directed != graphEdge.directed) return false; - if (edgeValue != null ? !edgeValue.equals(graphEdge.edgeValue) : graphEdge.edgeValue != null) return false; - if (!vertex1.equals(graphEdge.vertex1)) return false; - if (!vertex2.equals(graphEdge.vertex2)) return false; - - return true; - } - - @Override - public int hashCode() { - int result = vertex1.hashCode(); - result = 31 * result + vertex2.hashCode(); - result = 31 * result + (edgeValue != null ? edgeValue.hashCode() : 0); - result = 31 * result + (directed ? 1 : 0); - return result; - } - - @Override - public String toString() { - return vertex1 + " ---(" + edgeValue + ")---" + (directed ? "> " : " ") + vertex2; - } +public interface GraphEdge { + enum Type { DIRECTED, UNDIRECTED } + + V getVertex1(); + V getVertex2(); + E getEdgeValue(); + Type getType(); + +// @Override +// public String toString() { +// return getVertex1() + " ---(" + edgeValue + ")---" + (isDirected() ? "> " : " ") + getVertex2(); +// } } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java index 12e1987..5f95fc2 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java @@ -17,9 +17,6 @@ package es.usc.citius.hipster.graph; -import es.usc.citius.hipster.graph.GraphEdge; -import es.usc.citius.hipster.graph.HipsterDirectedGraph; -import es.usc.citius.hipster.graph.HipsterGraph; import es.usc.citius.hipster.model.Transition; import es.usc.citius.hipster.model.function.CostFunction; import es.usc.citius.hipster.model.function.HeuristicFunction; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java index bc907d9..801840e 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java @@ -16,44 +16,79 @@ package es.usc.citius.hipster.graph; -import java.util.ArrayList; -import java.util.List; +import es.usc.citius.hipster.util.Function; +import es.usc.citius.hipster.util.Iterators; + +import java.util.*; /** * Implementation of a HipsterDirectedGraph using a Guava Hash Table. * * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ -public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph implements HipsterDirectedGraph { +public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph implements HipsterMutableGraph, HipsterDirectedGraph { + @Override - public GraphEdge connect(V v1, V v2, E value){ - //check input - if(v1 == null || v2 == null) throw new IllegalArgumentException("Vertices cannot be null"); - GraphEdge edge = new GraphEdge(v1, v2, value, true); - connected.get(v1).add(edge); - return edge; + protected GraphEdge buildEdge(V v1, V v2, E value) { + return new DirectedEdge(v1, v2, value); } @Override - public Iterable> outgoingEdgesOf(V vertex) { - return connected.get(vertex); + public Iterable> outgoingEdgesOf(final V vertex) { + return new Iterable>() { + @Override + public Iterator> iterator() { + return Iterators.filter(edgesOf(vertex).iterator(), new Function, Boolean>() { + @Override + public Boolean apply(GraphEdge edge) { + return edge.getVertex1().equals(vertex); + } + }); + } + }; + } + + @Override + public Iterable> incomingEdgesOf(final V vertex) { + return new Iterable>() { + @Override + public Iterator> iterator() { + return Iterators.filter(edgesOf(vertex).iterator(), new Function, Boolean>() { + @Override + public Boolean apply(GraphEdge edge) { + return edge.getVertex2().equals(vertex); + } + }); + } + }; } @Override - public Iterable> incomingEdgesOf(V vertex) { - ArrayList> incomingEdges = new ArrayList>(); - for(List> edgesList : connected.values()){ - for(GraphEdge outgoingEdge : edgesList){ - if(outgoingEdge.getVertex2().equals(vertex)){ - incomingEdges.add(outgoingEdge); - } + public Iterable> edges() { + // TODO: [java-8-migration] use stream filter + return new Iterable>() { + @Override + public Iterator> iterator() { + return Iterators.map( + Iterators.filter(HashBasedHipsterDirectedGraph.super.vedges().iterator(), + new Function>, Boolean>() { + @Override + public Boolean apply(Map.Entry> input) { + return input.getKey().equals(input.getValue().getVertex1()); + } + }), + new Function>, GraphEdge>() { + @Override + public GraphEdge apply(Map.Entry> input) { + return input.getValue(); + } + }); } - } - return incomingEdges; + }; } - public static HashBasedHipsterDirectedGraph create() { + public static HashBasedHipsterDirectedGraph create() { return new HashBasedHipsterDirectedGraph(); } } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 34b7de3..43dfca5 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -16,6 +16,10 @@ package es.usc.citius.hipster.graph; + +import com.sun.corba.se.impl.orbutil.graph.Graph; +import es.usc.citius.hipster.util.Iterators; + import java.util.*; /** @@ -24,11 +28,11 @@ * @author Adrián González Sieira <adrian.gonzalez@usc.es> * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ -public class HashBasedHipsterGraph implements HipsterGraph { - protected HashMap>> connected; +public class HashBasedHipsterGraph implements HipsterMutableGraph { + protected HashMap>> connected; public HashBasedHipsterGraph(){ - this.connected = new HashMap>>(); + this.connected = new HashMap>>(); } /** @@ -36,11 +40,14 @@ public HashBasedHipsterGraph(){ * * @param v vertex to be added */ - public void add(V v){ + @Override + public boolean add(V v){ //add a new entry to the hash map if it does not exist if(!connected.containsKey(v)){ - connected.put(v, new ArrayList>()); + connected.put(v, new HashSet>()); + return true; } + return false; } /** @@ -48,21 +55,27 @@ public void add(V v){ * * @param v vertex to be removed */ - public void remove(V v){ - // Get vertices connected with this one - for(GraphEdge edge : edgesOf(v)){ - int edgeRemoval = 0; - List> edgesInverted = connected.get(edge.getVertex2()); - for(GraphEdge current : edgesInverted){ - edgeRemoval++; - if(current.getVertex2().equals(v)){ - break; + @Override + public boolean remove(V v){ + // Remove all edges related to v + Set> edges = this.connected.get(v); + if (edges == null) return false; + + for(Iterator> it = edges.iterator(); it.hasNext(); ){ + // Remove the edge in the list of the selected vertex + GraphEdge edge = it.next(); + it.remove(); + + V v2 = edge.getVertex1().equals(v) ? edge.getVertex2() : edge.getVertex1(); + for(Iterator> it2 = this.connected.get(v2).iterator(); it2.hasNext();){ + GraphEdge edge2 = it2.next(); + if (edge2.getVertex1().equals(v) || edge2.getVertex2().equals(v)){ + it2.remove(); } } - edgesInverted.remove(edgeRemoval); } - //remove vertices from connected - connected.remove(v); // v no longer exists + this.connected.remove(v); + return true; } /** @@ -72,50 +85,139 @@ public void remove(V v){ * @param v1 source vertex * @param v2 destination vertex * @param value edge value - * @return + * @return the generated edge */ + @Override public GraphEdge connect(V v1, V v2, E value){ // Check non-null arguments - if(v1 == null || v2 == null) throw new IllegalArgumentException("Vertices cannot be null"); + if(v1 == null || v2 == null) throw new IllegalArgumentException("Invalid vertices. A vertex cannot be null"); // Ensure that the vertices are in the graph - add(v1); - add(v2); - GraphEdge edge = new GraphEdge(v1, v2, value); - GraphEdge reversedEdge = new GraphEdge(v2, v1, value); - // Add edges to the graph + if (!connected.containsKey(v1)) throw new IllegalArgumentException(v1 + " is not a vertex of the graph"); + if (!connected.containsKey(v2)) throw new IllegalArgumentException(v2 + " is not a vertex of the graph"); + GraphEdge edge = buildEdge(v1, v2, value); + // Associate the vertices with their edge connected.get(v1).add(edge); - connected.get(v2).add(reversedEdge); + connected.get(v2).add(edge); return edge; } + protected GraphEdge buildEdge(V v1, V v2, E value){ + return new UndirectedEdge(v1, v2, value); + } + + private Map.Entry> createEntry(final V vertex, final GraphEdge edge){ + return new Map.Entry>() { + @Override + public V getKey() { + return vertex; + } + + @Override + public GraphEdge getValue() { + return edge; + } + + @Override + public GraphEdge setValue(GraphEdge value) { + throw new UnsupportedOperationException(); + } + }; + } + + protected Iterable>> vedges(){ + // TODO: [java-8-migration] Change this ugly lazy iterator with Java 8 streams and flatmaps + return new Iterable>>() { + @Override + public Iterator>> iterator() { + return new Iterator>>() { + private Iterator vertices = connected.keySet().iterator(); + private V currentVertex = vertices.hasNext() ? vertices.next() : null; + private Iterator> edges = + currentVertex != null ? connected.get(currentVertex).iterator() : Iterators.>empty(); + private GraphEdge nextElement = null; + + private GraphEdge loadNext(){ + // Preload the next element + if (edges.hasNext()){ + return edges.next(); + } else if (vertices.hasNext()){ + currentVertex = vertices.next(); + edges = connected.get(currentVertex).iterator(); + // skip empty edge lists + return loadNext(); + } + return null; + } + + @Override + public boolean hasNext() { + // There can be empty lists, so we need to pre-compute the next element in advance + // to check whether there exist a next element or not. + if (nextElement == null) { + nextElement = loadNext(); + } + return nextElement != null; + } + + @Override + public Map.Entry> next() { + // Load the next element + if (nextElement != null) { + GraphEdge next = nextElement; + nextElement = null; + return createEntry(currentVertex, next); + } else { + return createEntry(currentVertex, loadNext()); + } + } + }; + } + }; + } /** * Returns a list of the edges in the graph. * @return edges of the graph. */ @Override public Iterable> edges() { - final Collection>> edges = connected.values(); - // TODO: Change this ugly lazy iterator with Java 8 streams and flatmaps return new Iterable>() { @Override public Iterator> iterator() { return new Iterator>() { - private Iterator>> it = edges.iterator(); - private Iterator> currentList = it.next().iterator(); + private Iterator>> it = vedges().iterator(); + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public GraphEdge next() { + return it.next().getValue(); + } + }; + } + }; + /* + return new Iterable>() { + @Override + public Iterator> iterator() { + return new Iterator>() { + private Iterator vertices = connected.keySet().iterator(); + private Iterator> edges = + vertices.hasNext() ? connected.get(vertices.next()).iterator() : Iterators.>empty(); private GraphEdge nextElement = null; private GraphEdge loadNext(){ // Preload the next element - GraphEdge next = null; - if (currentList.hasNext()) { - next = currentList.next(); - } else if (it.hasNext()){ - currentList = it.next().iterator(); - if (currentList.hasNext()) { - next = currentList.next(); - } + if (edges.hasNext()){ + return edges.next(); + } else if (vertices.hasNext()){ + edges = connected.get(vertices.next()).iterator(); + // skip empty edge lists + return loadNext(); } - return next; + return null; } @Override @@ -141,7 +243,7 @@ public GraphEdge next() { } }; } - }; + };*/ } /** @@ -156,9 +258,9 @@ public Iterable vertices() { @Override public Iterable> edgesOf(V vertex) { - List> list = connected.get(vertex); - if (list == null) list = Collections.emptyList(); - return list; + Set> set = connected.get(vertex); + if (set == null) set = Collections.emptySet(); + return set; } public static HashBasedHipsterGraph create() { diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java new file mode 100644 index 0000000..ee3f7e8 --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java @@ -0,0 +1,8 @@ +package es.usc.citius.hipster.graph; + + +public interface HipsterMutableGraph extends HipsterGraph { + boolean add(V vertex); + boolean remove(V vertex); + GraphEdge connect(V vertex1, V vertex2, E edgeValue); +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java new file mode 100644 index 0000000..c78b04d --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java @@ -0,0 +1,48 @@ +package es.usc.citius.hipster.graph; + + +public class Pair { + private E e1, e2; + + public Pair(E e1, E e2) { + if (e1 == null) throw new IllegalArgumentException("First element cannot be null"); + this.e1 = e1; + if (e2 == null) throw new IllegalArgumentException("Second element cannot be null"); + this.e2 = e2; + } + + public E getE1() { + return e1; + } + + public void setE1(E e1) { + this.e1 = e1; + } + + public E getE2() { + return e2; + } + + public void setE2(E e2) { + this.e2 = e2; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Pair pair = (Pair) o; + + if (!e1.equals(pair.e1)) return false; + return e2.equals(pair.e2); + + } + + @Override + public int hashCode() { + int result = e1.hashCode(); + result = 31 * result + e2.hashCode(); + return result; + } +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/UndirectedEdge.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UndirectedEdge.java new file mode 100644 index 0000000..1159620 --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UndirectedEdge.java @@ -0,0 +1,52 @@ +package es.usc.citius.hipster.graph; + + +public class UndirectedEdge implements GraphEdge { + + private UnorderedPair vertices; + private E value; + + public UndirectedEdge(V vertex1, V vertex2, E value) { + this.vertices = new UnorderedPair(vertex1, vertex2); + this.value = value; + } + + @Override + public V getVertex1() { + return vertices.getE1(); + } + + @Override + public V getVertex2() { + return vertices.getE2(); + } + + @Override + public E getEdgeValue() { + return value; + } + + @Override + public Type getType() { + return Type.UNDIRECTED; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UndirectedEdge that = (UndirectedEdge) o; + + if (!vertices.equals(that.vertices)) return false; + return !(value != null ? !value.equals(that.value) : that.value != null); + + } + + @Override + public int hashCode() { + int result = vertices.hashCode(); + result = 31 * result + (value != null ? value.hashCode() : 0); + return result; + } +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java new file mode 100644 index 0000000..db7d048 --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java @@ -0,0 +1,46 @@ +package es.usc.citius.hipster.graph; + +public class UnorderedPair { + private E e1, e2; + + public UnorderedPair(E e1, E e2) { + if (e1 == null) throw new IllegalArgumentException("First element cannot be null"); + this.e1 = e1; + if (e2 == null) throw new IllegalArgumentException("Second element cannot be null"); + this.e2 = e2; + } + + public boolean contains(Object vertex){ + return e1.equals(vertex) || e2.equals(vertex); + } + + public E getE1() { + return e1; + } + + public void setE1(E e1) { + this.e1 = e1; + } + + public E getE2() { + return e2; + } + + public void setE2(E e2) { + this.e2 = e2; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UnorderedPair that = (UnorderedPair) o; + return that.contains(e1) && that.contains(e2); + } + + @Override + public int hashCode() { + return e1.hashCode() + e2.hashCode(); + } +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/WeightedEdge.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/WeightedEdge.java deleted file mode 100644 index 74cd7f0..0000000 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/WeightedEdge.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2014 CITIUS , University of Santiago de Compostela. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package es.usc.citius.hipster.graph; - - -/** - * Dumb class that can be used to generate unique weighted edges for a graph. - * Do not use this for production code! - * - * @author Pablo Rodríguez Mier - */ -public class WeightedEdge extends UniqueEdge { - - public WeightedEdge(Double value) { - super(value); - } - - public static WeightedEdge create(Double value){ - return new WeightedEdge(value); - } - - @Override - public String toString() { - return "WeightedEdge{" + - "value=" + this.getValue() + - ", edgeId='" + this.getEdgeId() + '\'' + - '}'; - } - -} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java new file mode 100644 index 0000000..f21993f --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java @@ -0,0 +1,76 @@ +package es.usc.citius.hipster.util; + + +import es.usc.citius.hipster.graph.GraphEdge; + +import java.util.Collections; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public final class Iterators { + + public static Iterator map(final Iterator it, final Function mapf){ + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public E next() { + return mapf.apply(it.next()); + } + }; + } + + public static Iterator filter(final Iterator it, final Function condition) { + return new Iterator() { + private T next = null; + + private T nextFiltered() { + T nextElem = null; + while (it.hasNext()) { + T elem = it.next(); + if (condition.apply(elem)) { + nextElem = elem; + break; + } + } + return nextElem; + } + + @Override + public boolean hasNext() { + if (next != null) return true; + // Preload the next edge + next = nextFiltered(); + return next != null; + } + + @Override + public T next() { + if (next != null) { + T elem = next; + next = null; + return elem; + } + return nextFiltered(); + } + }; + } + + + public static Iterator empty() { + return new Iterator() { + @Override + public boolean hasNext() { + return false; + } + + @Override + public E next() { + throw new NoSuchElementException("Iterator is empty"); + } + }; + } +} diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterGraphTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterGraphTest.java new file mode 100644 index 0000000..ab01224 --- /dev/null +++ b/hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterGraphTest.java @@ -0,0 +1,104 @@ +package es.usc.citius.hipster.graph; + +import com.google.common.collect.Sets; +import org.junit.Before; +import org.junit.Test; + +import java.util.Set; + +import static org.junit.Assert.*; + + +public class HashBasedHipsterGraphTest { + protected HashBasedHipsterGraph graph; + protected int size = 10; + + public static HashBasedHipsterGraph createStarGraph(int vertices){ + HashBasedHipsterGraph g = new HashBasedHipsterGraph(); + for(int i = 0; i < vertices; i++){ + g.add("v"+i); + for(int j=i-1; j>=0; j--){ + g.connect("v"+i, "v"+j, Math.random()); + } + } + return g; + } + + @Before + public void setUp(){ + graph = createStarGraph(size); + } + + private void testGraphEdges(){ + Iterable edges = graph.edges(); + int countEdgesV1 = 0; + int countAllEdges = 0; + for(GraphEdge e : edges){ + if (e.getVertex1().equals("v1") || e.getVertex2().equals("v1")) countEdgesV1++; + countAllEdges++; + } + assertEquals((size-1)*2, countEdgesV1); + assertEquals(size*(size-1), countAllEdges); + } + + @Test + public void testEdges() throws Exception { + testGraphEdges(); + } + + @Test + public void testEdgesWithDisconnectedVertices() throws Exception { + graph.add("X"); + graph.add("Y"); + testGraphEdges(); + } + + @Test + public void testAdd() throws Exception { + graph.add("X"); + Set vertices = Sets.newHashSet(graph.vertices()); + assertTrue(vertices.contains("X")); + assertTrue(vertices.size()==size+1); + } + + @Test + public void testRemove() throws Exception { + graph.remove("v1"); + assertFalse(Sets.newHashSet(graph.vertices()).contains("v1")); + } + + @Test + public void testRemoveAndCheckEdges() throws Exception { + graph.remove("v1"); + assertFalse(Sets.newHashSet(graph.vertices()).contains("v1")); + Iterable edges = graph.edges(); + int countEdges = 0; + for(GraphEdge e : edges){ + assertFalse(e.getVertex1().equals("v1") || e.getVertex2().equals("v1")); + countEdges++; + } + assertEquals((size-1)*(size-2), countEdges); + } + + @Test + public void testConnect() throws Exception { + graph.connect("X","Y",1.0d); + assertTrue(Sets.newHashSet(graph.vertices()).contains("X")); + assertTrue(Sets.newHashSet(graph.vertices()).contains("Y")); + } + + + @Test + public void testVertices() throws Exception { + Set vertices = Sets.newHashSet(graph.vertices()); + assertEquals(size, vertices.size()); + } + + @Test + public void testEdgesOf() throws Exception { + Set edges = Sets.newHashSet(graph.edgesOf("v1")); + assertEquals(size-1, edges.size()); + } + + +} \ No newline at end of file diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/graph/UndirectedEdgeTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/graph/UndirectedEdgeTest.java new file mode 100644 index 0000000..1b0f080 --- /dev/null +++ b/hipster-core/src/test/java/es/usc/citius/hipster/graph/UndirectedEdgeTest.java @@ -0,0 +1,29 @@ +package es.usc.citius.hipster.graph; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class UndirectedEdgeTest { + + @Test + public void testEqualsWithUndirectedEdgeSwapped() throws Exception { + UndirectedEdge e1 = new UndirectedEdge("a", "b", 1); + UndirectedEdge e2 = new UndirectedEdge("b", "a", 1); + assertEquals(e1, e2); + } + + @Test + public void testEqualsWithUndirectedEdge() throws Exception { + UndirectedEdge e1 = new UndirectedEdge("a", "b", 1); + UndirectedEdge e2 = new UndirectedEdge("a", "b", 1); + assertEquals(e1, e2); + } + + @Test + public void testNotEqualsWithUndirectedEdge() throws Exception { + UndirectedEdge e1 = new UndirectedEdge("a", "b", 1); + UndirectedEdge e2 = new UndirectedEdge("a", "c", 1); + assertNotEquals(e1, e2); + } +} \ No newline at end of file diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java index ce4a72e..7b37737 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/GraphBuilderTest.java @@ -35,7 +35,7 @@ public class GraphBuilderTest { @BeforeClass public static void setUp() throws Exception { - testGraph = GraphBuilder.create() + testGraph = GraphBuilder.create() .connect("A").to("B").withEdge(4d) .connect("A").to("C").withEdge(2d) .connect("B").to("C").withEdge(5d) @@ -43,7 +43,7 @@ public static void setUp() throws Exception { .connect("C").to("E").withEdge(3d) .connect("D").to("F").withEdge(11d) .connect("E").to("D").withEdge(4d) - .buildDirectedGraph(); + .createDirectedGraph(); } @Test diff --git a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java index 53ee549..64b3a4c 100644 --- a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java +++ b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java @@ -16,6 +16,7 @@ package es.usc.citius.hipster.extensions.graph; import com.google.common.base.Preconditions; +import es.usc.citius.hipster.graph.DirectedEdge; import es.usc.citius.hipster.graph.GraphEdge; import es.usc.citius.hipster.graph.HipsterDirectedGraph; @@ -29,7 +30,7 @@ public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph connect(V v1, V v2, E value){ Preconditions.checkArgument(v1 != null && v2 != null, "Vertices cannot be null"); - GraphEdge edge = new GraphEdge(v1, v2, value, true); + GraphEdge edge = new DirectedEdge(v1, v2, value); graphTable.put(v1, v2, edge); disconnected.remove(v1); disconnected.remove(v2); diff --git a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java index 9a6d561..ecb249c 100644 --- a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java +++ b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java @@ -18,8 +18,10 @@ import com.google.common.base.Preconditions; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Sets; +import es.usc.citius.hipster.graph.DirectedEdge; import es.usc.citius.hipster.graph.GraphEdge; import es.usc.citius.hipster.graph.HipsterGraph; +import es.usc.citius.hipster.graph.UndirectedEdge; import java.util.HashSet; import java.util.Set; @@ -80,7 +82,7 @@ public void remove(V v, GraphEdge edge){ public GraphEdge connect(V v1, V v2, E value){ Preconditions.checkArgument(v1 != null && v2 != null, "Vertices cannot be null"); - GraphEdge edge = new GraphEdge(v1, v2, value); + GraphEdge edge = new UndirectedEdge(v1, v2, value); graphTable.put(v1, v2, edge); graphTable.put(v2, v1, edge); disconnected.remove(v1); diff --git a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java index 5779347..0525a57 100644 --- a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java +++ b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java @@ -17,7 +17,7 @@ package es.usc.citius.hipster.extensions.graph; import com.google.common.collect.Sets; -import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.DirectedEdge; import org.junit.Before; import org.junit.Test; @@ -52,41 +52,41 @@ public void testConnect() throws Exception { @Test public void testOutgoingEdgesOf() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("B", "C", 5d, true)); - expected.add(new GraphEdge("B", "D", 10d, true)); + Set> expected = new HashSet>(); + expected.add(new DirectedEdge("B", "C", 5d)); + expected.add(new DirectedEdge("B", "D", 10d)); assertEquals(expected, Sets.newHashSet(directedGraph.outgoingEdgesOf("B"))); } @Test public void testIncomingEdgesOf() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("B", "C", 5d, true)); - expected.add(new GraphEdge("A", "C", 2d, true)); + Set> expected = new HashSet>(); + expected.add(new DirectedEdge("B", "C", 5d)); + expected.add(new DirectedEdge("A", "C", 2d)); assertEquals(expected, Sets.newHashSet(directedGraph.incomingEdgesOf("C"))); } @Test @Override public void testEdges() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("A", "B", 4d, true)); - expected.add(new GraphEdge("A", "C", 2d, true)); - expected.add(new GraphEdge("B", "C", 5d, true)); - expected.add(new GraphEdge("B", "D", 10d, true)); - expected.add(new GraphEdge("C", "E", 3d, true)); - expected.add(new GraphEdge("D", "F", 11d, true)); - expected.add(new GraphEdge("E", "D", 4d, true)); + Set> expected = new HashSet>(); + expected.add(new DirectedEdge("A", "B", 4d)); + expected.add(new DirectedEdge("A", "C", 2d)); + expected.add(new DirectedEdge("B", "C", 5d)); + expected.add(new DirectedEdge("B", "D", 10d)); + expected.add(new DirectedEdge("C", "E", 3d)); + expected.add(new DirectedEdge("D", "F", 11d)); + expected.add(new DirectedEdge("E", "D", 4d)); assertEquals(expected, Sets.newHashSet(graph.edges())); } @Test @Override public void testEdgesOf() throws Exception { - Set> expected = new HashSet>(); - expected.add(new GraphEdge("B", "D", 10d, true)); - expected.add(new GraphEdge("A", "B", 4d, true)); - expected.add(new GraphEdge("B", "C", 5d, true)); + Set> expected = new HashSet>(); + expected.add(new DirectedEdge("B", "D", 10d)); + expected.add(new DirectedEdge("A", "B", 4d)); + expected.add(new DirectedEdge("B", "C", 5d)); assertEquals(expected, graph.edgesOf("B")); } } diff --git a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java index 524000a..836fb3c 100644 --- a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java +++ b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java @@ -18,6 +18,7 @@ import com.google.common.collect.Sets; import es.usc.citius.hipster.graph.GraphEdge; +import es.usc.citius.hipster.graph.UndirectedEdge; import org.junit.Before; import org.junit.Test; @@ -71,13 +72,13 @@ public void testConnect() throws Exception { @Test public void testEdges() throws Exception { Set> expected = new HashSet>(); - expected.add(new GraphEdge("A","B",4d)); - expected.add(new GraphEdge("A","C",2d)); - expected.add(new GraphEdge("B","C",5d)); - expected.add(new GraphEdge("B","D",10d)); - expected.add(new GraphEdge("C","E",3d)); - expected.add(new GraphEdge("D","F",11d)); - expected.add(new GraphEdge("E","D",4d)); + expected.add(new UndirectedEdge("A","B",4d)); + expected.add(new UndirectedEdge("A","C",2d)); + expected.add(new UndirectedEdge("B","C",5d)); + expected.add(new UndirectedEdge("B","D",10d)); + expected.add(new UndirectedEdge("C","E",3d)); + expected.add(new UndirectedEdge("D","F",11d)); + expected.add(new UndirectedEdge("E","D",4d)); assertEquals(expected, Sets.newHashSet(graph.edges())); } @@ -90,9 +91,9 @@ public void testVertices() throws Exception { @Test public void testEdgesOf() throws Exception { Set> expected = new HashSet>(); - expected.add(new GraphEdge("B","D",10d)); - expected.add(new GraphEdge("A","B",4d)); - expected.add(new GraphEdge("B","C",5d)); + expected.add(new UndirectedEdge("B","D",10d)); + expected.add(new UndirectedEdge("A","B",4d)); + expected.add(new UndirectedEdge("B","C",5d)); assertEquals(expected, graph.edgesOf("B")); } From d0f7dbb3d6c60e9476cb905cc7b018719b440533 Mon Sep 17 00:00:00 2001 From: pablormier Date: Sun, 14 Jun 2015 23:20:57 +0200 Subject: [PATCH 33/62] #128 Refactored graph implementations. More unit tests added. --- .../citius/hipster/graph/GraphBuilder.java | 4 ++ .../hipster/graph/HashBasedHipsterGraph.java | 18 +++++++ .../hipster/graph/HipsterMutableGraph.java | 4 ++ .../HashBasedHipsterDirectedGraphTest.java | 54 +++++++++++++++++++ .../graph/HashBasedHipsterGraphTest.java | 8 +-- .../BlueprintsHipsterGraphAdapter.java | 24 ++++++--- .../jung/JUNGHipsterDirectedGraphAdapter.java | 12 +---- .../graphs/jung/JUNGHipsterGraphAdapter.java | 34 ++++++++---- pom.xml | 8 +-- 9 files changed, 132 insertions(+), 34 deletions(-) create mode 100644 hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraphTest.java diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java index 4835ace..9a9476e 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphBuilder.java @@ -62,6 +62,8 @@ public Vertex1 connect(V vertex) { public HipsterDirectedGraph createDirectedGraph() { HashBasedHipsterDirectedGraph graph = HashBasedHipsterDirectedGraph.create(); for (Connection c : connections) { + graph.add(c.vertex1); + graph.add(c.vertex2); graph.connect(c.vertex1, c.vertex2, c.edge); } return graph; @@ -70,6 +72,8 @@ public HipsterDirectedGraph createDirectedGraph() { public HipsterGraph createUndirectedGraph() { HashBasedHipsterGraph graph = HashBasedHipsterGraph.create(); for (Connection c : connections) { + graph.add(c.vertex1); + graph.add(c.vertex2); graph.connect(c.vertex1, c.vertex2, c.edge); } return graph; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 43dfca5..b7f925e 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -50,6 +50,15 @@ public boolean add(V v){ return false; } + @Override + public Set add(V... vertices) { + Set added = new HashSet(); + for(V v : vertices){ + if (add(v)) added.add(v); + } + return added; + } + /** * Remove a vertex from the graph. * @@ -78,6 +87,15 @@ public boolean remove(V v){ return true; } + @Override + public Set remove(V... vertices) { + Set removed = new HashSet(); + for(V v : vertices){ + if (remove(v)) removed.add(v); + } + return removed; + } + /** * Connect to vertices in the graph. If the vertices are not in the graph, they are automatically * added to the graph before connecting them. diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java index ee3f7e8..36029bc 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java @@ -1,8 +1,12 @@ package es.usc.citius.hipster.graph; +import java.util.Set; + public interface HipsterMutableGraph extends HipsterGraph { boolean add(V vertex); + Set add(V... vertices); boolean remove(V vertex); + Set remove(V... vertices); GraphEdge connect(V vertex1, V vertex2, E edgeValue); } diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraphTest.java b/hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraphTest.java new file mode 100644 index 0000000..2ca410d --- /dev/null +++ b/hipster-core/src/test/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraphTest.java @@ -0,0 +1,54 @@ +package es.usc.citius.hipster.graph; + +import com.google.common.collect.Sets; +import org.junit.Before; +import org.junit.Test; + +import java.util.Set; + +import static org.junit.Assert.*; + + +public class HashBasedHipsterDirectedGraphTest { + + protected HipsterDirectedGraph graph; + protected int size = 10; + + @Before + public void setUp(){ + graph = createStarGraph(size); + } + + protected HashBasedHipsterDirectedGraph createStarGraph(int vertices){ + HashBasedHipsterDirectedGraph g = new HashBasedHipsterDirectedGraph(); + for(int i = 0; i < vertices; i++){ + g.add("v"+i); + for(int j=0; j=0; j--){ - g.connect("v"+i, "v"+j, Math.random()); + for(int j=0; j> edgesOf(Vertex vertex) { return convertEdges(vertex.getEdges(Direction.BOTH)); } - protected static Iterable> convertEdges(Iterable edges){ - //initialize collection - ArrayList> convertedEdges = new ArrayList>(); - //convert edges - for(Edge current : edges){ - convertedEdges.add(new GraphEdge(current.getVertex(Direction.IN), current.getVertex(Direction.OUT), current)); - } - return convertedEdges; + protected static Iterable> convertEdges(final Iterable edges){ + return new Iterable>() { + @Override + public Iterator> iterator() { + return Iterators.map(edges.iterator(), new Function>() { + @Override + public GraphEdge apply(Edge edge) { + return new UndirectedEdge(edge.getVertex(Direction.OUT), edge.getVertex(Direction.IN), edge); + } + }); + } + }; } } diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java index 90c1e72..dc69ecd 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterDirectedGraphAdapter.java @@ -44,11 +44,7 @@ public Iterable> outgoingEdgesOf(V vertex) { if (outEdges == null || outEdges.isEmpty()) { return Collections.emptyList(); } - ArrayList> outEdgesTransformed = new ArrayList>(outEdges.size()); - for(E current : outEdges){ - outEdgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); - } - return outEdgesTransformed; + return adapt(outEdges); } catch (NullPointerException e){ return Collections.emptyList(); } @@ -62,11 +58,7 @@ public Iterable> incomingEdgesOf(V vertex) { if (inEdges == null || inEdges.isEmpty()) { return Collections.emptyList(); } - ArrayList> inEdgesTransformed = new ArrayList>(inEdges.size()); - for(E current : inEdges){ - inEdgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); - } - return inEdgesTransformed; + return adapt(inEdges); }catch(NullPointerException e){ return Collections.emptyList(); } diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java index 29ecccc..fbe97f0 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java @@ -17,12 +17,18 @@ package es.usc.citius.hipster.thirdparty.graphs.jung; import edu.uci.ics.jung.graph.Graph; +import edu.uci.ics.jung.graph.util.EdgeType; +import es.usc.citius.hipster.graph.DirectedEdge; import es.usc.citius.hipster.graph.GraphEdge; import es.usc.citius.hipster.graph.HipsterGraph; +import es.usc.citius.hipster.graph.UndirectedEdge; +import es.usc.citius.hipster.util.Function; +import es.usc.citius.hipster.util.Iterators; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; /** * An adapter to adapt a JUNG graph to a general HipsterGraph interface. @@ -36,17 +42,29 @@ public JUNGHipsterGraphAdapter(Graph graph) { this.graph = graph; } + protected Iterable> adapt(final Iterable iterable){ + return new Iterable>() { + @Override + public Iterator> iterator() { + return Iterators.map(iterable.iterator(), new Function>() { + @Override + public GraphEdge apply(E edge) { + if (graph.getEdgeType(edge).equals(EdgeType.DIRECTED)){ + return new DirectedEdge(graph.getSource(edge), graph.getDest(edge), edge); + } + return new UndirectedEdge(graph.getSource(edge), graph.getDest(edge), edge); + } + }); + } + }; + } @Override public Iterable> edges() { final Collection edges = graph.getEdges(); if (edges == null || edges.isEmpty()){ return Collections.emptyList(); } - ArrayList> edgesTransformed = new ArrayList>(edges.size()); - for(E current : edges){ - edgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); - } - return edgesTransformed; + return adapt(edges); } @Override @@ -60,11 +78,7 @@ public Iterable> edgesOf(V vertex) { if (edges == null || edges.isEmpty()){ return Collections.emptyList(); } - ArrayList> edgesTransformed = new ArrayList>(edges.size()); - for(E current : edges){ - edgesTransformed.add(new GraphEdge(graph.getSource(current), graph.getDest(current), current)); - } - return edgesTransformed; + return adapt(edges); } } diff --git a/pom.xml b/pom.xml index 3a52d9c..b94daf8 100644 --- a/pom.xml +++ b/pom.xml @@ -92,18 +92,18 @@ junit junit - 4.11 + 4.12 test com.google.guava guava - 16.0.1 + 18.0 com.google.code.findbugs jsr305 - 2.0.3 + 3.0.0 @@ -277,6 +277,7 @@ + From 358d42062200f21b287b0e8e047312a68626d04e Mon Sep 17 00:00:00 2001 From: pablormier Date: Sun, 14 Jun 2015 23:24:11 +0200 Subject: [PATCH 34/62] close #129 --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 96ea8cd..c11a535 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,9 @@ jdk: - oraclejdk7 - oraclejdk8 - openjdk7 +branches: + except: + - /^(?i:wip).*$/ after_success: #- chmod +x .config/deploy-site.sh #- .config/deploy-site.sh From 96b0babaf7275a5ad4a8ce09c5bacf9cabc7c70d Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 15 Jun 2015 17:37:27 +0200 Subject: [PATCH 35/62] Renamed the old Iterators class to "F". This class contains just a few functional methods (java < 8) to lazily process and transform iterators and iterables. --- .../graph/HashBasedHipsterDirectedGraph.java | 59 +++++--------- .../hipster/graph/HashBasedHipsterGraph.java | 69 ++-------------- .../java/es/usc/citius/hipster/util/F.java | 81 +++++++++++++++++++ .../es/usc/citius/hipster/util/Iterators.java | 55 ------------- .../BlueprintsHipsterGraphAdapter.java | 5 +- .../graphs/jung/JUNGHipsterGraphAdapter.java | 9 +-- 6 files changed, 115 insertions(+), 163 deletions(-) create mode 100644 hipster-core/src/main/java/es/usc/citius/hipster/util/F.java diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java index 801840e..75bbce6 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterDirectedGraph.java @@ -17,7 +17,7 @@ package es.usc.citius.hipster.graph; import es.usc.citius.hipster.util.Function; -import es.usc.citius.hipster.util.Iterators; +import es.usc.citius.hipster.util.F; import java.util.*; @@ -26,7 +26,7 @@ * * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ -public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph implements HipsterMutableGraph, HipsterDirectedGraph { +public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph implements HipsterMutableGraph, HipsterDirectedGraph { @Override @@ -36,56 +36,41 @@ protected GraphEdge buildEdge(V v1, V v2, E value) { @Override public Iterable> outgoingEdgesOf(final V vertex) { - return new Iterable>() { + return F.filter(edgesOf(vertex), new Function, Boolean>() { @Override - public Iterator> iterator() { - return Iterators.filter(edgesOf(vertex).iterator(), new Function, Boolean>() { - @Override - public Boolean apply(GraphEdge edge) { - return edge.getVertex1().equals(vertex); - } - }); + public Boolean apply(GraphEdge edge) { + return edge.getVertex1().equals(vertex); } - }; + }); } @Override public Iterable> incomingEdgesOf(final V vertex) { - return new Iterable>() { + return F.filter(edgesOf(vertex), new Function, Boolean>() { @Override - public Iterator> iterator() { - return Iterators.filter(edgesOf(vertex).iterator(), new Function, Boolean>() { - @Override - public Boolean apply(GraphEdge edge) { - return edge.getVertex2().equals(vertex); - } - }); + public Boolean apply(GraphEdge edge) { + return edge.getVertex2().equals(vertex); } - }; + }); } @Override public Iterable> edges() { // TODO: [java-8-migration] use stream filter - return new Iterable>() { - @Override - public Iterator> iterator() { - return Iterators.map( - Iterators.filter(HashBasedHipsterDirectedGraph.super.vedges().iterator(), - new Function>, Boolean>() { - @Override - public Boolean apply(Map.Entry> input) { - return input.getKey().equals(input.getValue().getVertex1()); - } - }), - new Function>, GraphEdge>() { + return F.map( + F.filter(HashBasedHipsterDirectedGraph.super.vedges(), + new Function>, Boolean>() { @Override - public GraphEdge apply(Map.Entry> input) { - return input.getValue(); + public Boolean apply(Map.Entry> input) { + return input.getKey().equals(input.getValue().getVertex1()); } - }); - } - }; + }), + new Function>, GraphEdge>() { + @Override + public GraphEdge apply(Map.Entry> input) { + return input.getValue(); + } + }); } public static HashBasedHipsterDirectedGraph create() { diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index b7f925e..9a5695a 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -17,7 +17,8 @@ package es.usc.citius.hipster.graph; -import com.sun.corba.se.impl.orbutil.graph.Graph; +import es.usc.citius.hipster.util.F; +import es.usc.citius.hipster.util.Function; import es.usc.citius.hipster.util.Iterators; import java.util.*; @@ -198,70 +199,12 @@ public Map.Entry> next() { */ @Override public Iterable> edges() { - return new Iterable>() { + return F.map(vedges(), new Function>, GraphEdge>() { @Override - public Iterator> iterator() { - return new Iterator>() { - private Iterator>> it = vedges().iterator(); - - @Override - public boolean hasNext() { - return it.hasNext(); - } - - @Override - public GraphEdge next() { - return it.next().getValue(); - } - }; - } - }; - /* - return new Iterable>() { - @Override - public Iterator> iterator() { - return new Iterator>() { - private Iterator vertices = connected.keySet().iterator(); - private Iterator> edges = - vertices.hasNext() ? connected.get(vertices.next()).iterator() : Iterators.>empty(); - private GraphEdge nextElement = null; - - private GraphEdge loadNext(){ - // Preload the next element - if (edges.hasNext()){ - return edges.next(); - } else if (vertices.hasNext()){ - edges = connected.get(vertices.next()).iterator(); - // skip empty edge lists - return loadNext(); - } - return null; - } - - @Override - public boolean hasNext() { - // There can be empty lists, so we need to pre-compute the next element in advance - // to check whether there exist a next element or not. - if (nextElement == null) { - nextElement = loadNext(); - } - return nextElement != null; - } - - @Override - public GraphEdge next() { - // Load the next element - if (nextElement != null) { - GraphEdge next = nextElement; - nextElement = null; - return next; - } else { - return loadNext(); - } - } - }; + public GraphEdge apply(Map.Entry> entry) { + return entry.getValue(); } - };*/ + }); } /** diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java new file mode 100644 index 0000000..3d42bab --- /dev/null +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java @@ -0,0 +1,81 @@ +package es.usc.citius.hipster.util; + + +import java.util.Iterator; + + +/** + * This class contains a few functional methods to lazily process iterables and iterators. + * Required due to the removal of Guava dependencies (issue #125 https://github.com/citiususc/hipster/issues/125) + */ +public final class F { + + public static Iterable map(final Iterable it, final Function mapf){ + return new Iterable() { + @Override + public Iterator iterator() { + return map(it.iterator(), mapf); + } + }; + } + + public static Iterator map(final Iterator it, final Function mapf){ + return new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public E next() { + return mapf.apply(it.next()); + } + }; + } + + public static Iterable filter(final Iterable it, final Function condition) { + return new Iterable() { + @Override + public Iterator iterator() { + return filter(it.iterator(), condition); + } + }; + } + + public static Iterator filter(final Iterator it, final Function condition) { + return new Iterator() { + private T next = null; + + private T nextFiltered() { + T nextElem = null; + while (it.hasNext()) { + T elem = it.next(); + if (condition.apply(elem)) { + nextElem = elem; + break; + } + } + return nextElem; + } + + @Override + public boolean hasNext() { + if (next != null) return true; + // Preload the next edge + next = nextFiltered(); + return next != null; + } + + @Override + public T next() { + if (next != null) { + T elem = next; + next = null; + return elem; + } + return nextFiltered(); + } + }; + } + +} diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java index f21993f..8565d2c 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java @@ -1,65 +1,10 @@ package es.usc.citius.hipster.util; -import es.usc.citius.hipster.graph.GraphEdge; - -import java.util.Collections; import java.util.Iterator; import java.util.NoSuchElementException; public final class Iterators { - - public static Iterator map(final Iterator it, final Function mapf){ - return new Iterator() { - @Override - public boolean hasNext() { - return it.hasNext(); - } - - @Override - public E next() { - return mapf.apply(it.next()); - } - }; - } - - public static Iterator filter(final Iterator it, final Function condition) { - return new Iterator() { - private T next = null; - - private T nextFiltered() { - T nextElem = null; - while (it.hasNext()) { - T elem = it.next(); - if (condition.apply(elem)) { - nextElem = elem; - break; - } - } - return nextElem; - } - - @Override - public boolean hasNext() { - if (next != null) return true; - // Preload the next edge - next = nextFiltered(); - return next != null; - } - - @Override - public T next() { - if (next != null) { - T elem = next; - next = null; - return elem; - } - return nextFiltered(); - } - }; - } - - public static Iterator empty() { return new Iterator() { @Override diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java index 05ed4e5..7a51ec2 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/blueprints/BlueprintsHipsterGraphAdapter.java @@ -25,9 +25,8 @@ import es.usc.citius.hipster.graph.HipsterGraph; import es.usc.citius.hipster.graph.UndirectedEdge; import es.usc.citius.hipster.util.Function; -import es.usc.citius.hipster.util.Iterators; +import es.usc.citius.hipster.util.F; -import java.util.ArrayList; import java.util.Iterator; /** @@ -59,7 +58,7 @@ protected static Iterable> convertEdges(final Iterable>() { @Override public Iterator> iterator() { - return Iterators.map(edges.iterator(), new Function>() { + return F.map(edges.iterator(), new Function>() { @Override public GraphEdge apply(Edge edge) { return new UndirectedEdge(edge.getVertex(Direction.OUT), edge.getVertex(Direction.IN), edge); diff --git a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java index fbe97f0..5280c66 100644 --- a/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java +++ b/hipster-third-party-graphs/src/main/java/es/usc/citius/hipster/thirdparty/graphs/jung/JUNGHipsterGraphAdapter.java @@ -23,9 +23,8 @@ import es.usc.citius.hipster.graph.HipsterGraph; import es.usc.citius.hipster.graph.UndirectedEdge; import es.usc.citius.hipster.util.Function; -import es.usc.citius.hipster.util.Iterators; +import es.usc.citius.hipster.util.F; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -46,10 +45,10 @@ protected Iterable> adapt(final Iterable iterable){ return new Iterable>() { @Override public Iterator> iterator() { - return Iterators.map(iterable.iterator(), new Function>() { + return F.map(iterable.iterator(), new Function>() { @Override - public GraphEdge apply(E edge) { - if (graph.getEdgeType(edge).equals(EdgeType.DIRECTED)){ + public GraphEdge apply(E edge) { + if (graph.getEdgeType(edge).equals(EdgeType.DIRECTED)) { return new DirectedEdge(graph.getSource(edge), graph.getDest(edge), edge); } return new UndirectedEdge(graph.getSource(edge), graph.getDest(edge), edge); From 873cc926ee26749bbdbbf6b43fcf6c5ef28f426c Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 15 Jun 2015 19:27:33 +0200 Subject: [PATCH 36/62] Added Iterator remove() methods for compatibility with Java < 8 --- .../hipster/graph/HashBasedHipsterGraph.java | 5 +++ .../java/es/usc/citius/hipster/util/F.java | 10 +++++ .../es/usc/citius/hipster/util/Iterators.java | 5 +++ pom.xml | 37 ++----------------- 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 9a5695a..82c1920 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -189,6 +189,11 @@ public Map.Entry> next() { return createEntry(currentVertex, loadNext()); } } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } }; } }; diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java index 3d42bab..f444adf 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java @@ -30,6 +30,11 @@ public boolean hasNext() { public E next() { return mapf.apply(it.next()); } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } }; } @@ -75,6 +80,11 @@ public T next() { } return nextFiltered(); } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } }; } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java index 8565d2c..2302ca0 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java @@ -16,6 +16,11 @@ public boolean hasNext() { public E next() { throw new NoSuchElementException("Iterator is empty"); } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } }; } } diff --git a/pom.xml b/pom.xml index b94daf8..d0f71a5 100644 --- a/pom.xml +++ b/pom.xml @@ -211,10 +211,10 @@ maven-compiler-plugin - 3.1 + 3.3 - 1.6 - 1.6 + 1.7 + 1.7 @@ -277,37 +277,6 @@ - - citius-snapshot-deploy From 755548a89bfcfec9cd747e3e7b95f9e732b88625 Mon Sep 17 00:00:00 2001 From: pablormier Date: Mon, 15 Jun 2015 22:33:13 +0200 Subject: [PATCH 37/62] Change HashSet to LinkedHashSet in HashBasedHipsterGraph to preserve the order in which the nodes are added --- .../java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 82c1920..07b64a3 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -45,7 +45,7 @@ public HashBasedHipsterGraph(){ public boolean add(V v){ //add a new entry to the hash map if it does not exist if(!connected.containsKey(v)){ - connected.put(v, new HashSet>()); + connected.put(v, new LinkedHashSet>()); return true; } return false; From 075f3dda0f0e45fce2358f8ee27ec5808a56a26b Mon Sep 17 00:00:00 2001 From: pablormier Date: Mon, 15 Jun 2015 22:49:19 +0200 Subject: [PATCH 38/62] Added an automatic cast from strings to numbers in method "takeCostFromEdges" --- .../usc/citius/hipster/graph/GraphSearchProblem.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java index 5f95fc2..8cf7c6a 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/GraphSearchProblem.java @@ -100,9 +100,21 @@ public HeuristicType takeCostsFromEdges() { @Override public Double evaluate(Transition transition) { E action = transition.getAction(); + if (action instanceof Number) { + // Try to cast to number automatically return ((Number) action).doubleValue(); + } else if (action instanceof String){ + // Try to parse to a number + try { + return Double.parseDouble((String) action); + } catch (NumberFormatException e){ + throw new IllegalArgumentException("Exception ocurred when trying" + + "to cast " + action + " to a number. Use the method " + + "extractCostsFromEdges to define a custom evaluation strategy.", e); + } } else { + // TODO: Throw exception instead? // Assume uniform costs. return 1d; /* From 98cffdc58de57f0a899709155c95876dece78428 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Tue, 16 Jun 2015 10:22:47 +0200 Subject: [PATCH 39/62] Changed usages of the deprecated method buildDirectedGraph / buildUndirectedGraph to createDirectedGraph / createUndirectedGraph --- README.md | 5 +++-- .../hipster/util/examples/RomanianProblem.java | 4 ++-- .../lab/hipster/algorithm/DepthFirstSearchTest.java | 12 ++++++------ .../hipster/examples/DirectedGraphSearchExample.java | 5 +++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0912366..53ebea5 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,8 @@ we use the GraphSearchProblem to create the required components to solve it usin ```java // Create a simple weighted directed graph with Hipster where // vertices are Strings and edge values are just doubles -HipsterDirectedGraph graph = GraphBuilder.create() +HipsterDirectedGraph graph = + GraphBuilder.create() .connect("A").to("B").withEdge(4d) .connect("A").to("C").withEdge(2d) .connect("B").to("C").withEdge(5d) @@ -109,7 +110,7 @@ HipsterDirectedGraph graph = GraphBuilder.create() .connect("C").to("E").withEdge(3d) .connect("D").to("F").withEdge(11d) .connect("E").to("D").withEdge(4d) - .buildDirectedGraph(); + .createDirectedGraph(); // Create the search problem. For graph problems, just use // the GraphSearchProblem util class to generate the problem with ease. diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java index 8fcf3c0..e25db93 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/examples/RomanianProblem.java @@ -57,7 +57,7 @@ public enum City{ heuristicMap.put(City.Neamt, 234d); heuristicMap.put(City.Bucharest, 0d); - graph = GraphBuilder.create() + graph = GraphBuilder.create() .connect(City.Arad).to(City.Zerind).withEdge(75d) .connect(City.Arad).to(City.Timisoara).withEdge(118d) .connect(City.Arad).to(City.Sibiu).withEdge(140d) @@ -81,7 +81,7 @@ public enum City{ .connect(City.Pitesti).to(City.Rimnicu_Vilcea).withEdge(97d) .connect(City.Rimnicu_Vilcea).to(City.Sibiu).withEdge(80d) .connect(City.Urziceni).to(City.Vaslui).withEdge(142d) - .buildUndirectedGraph(); + .createUndirectedGraph(); } diff --git a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java index 7b484ab..c5f98cb 100644 --- a/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java +++ b/hipster-core/src/test/java/es/usc/citius/lab/hipster/algorithm/DepthFirstSearchTest.java @@ -35,7 +35,7 @@ public class DepthFirstSearchTest { @Test public void testTree(){ HipsterDirectedGraph tree = - GraphBuilder.create() + GraphBuilder.create() .connect("A").to("B").withEdge("1") .connect("A").to("C").withEdge("2") .connect("B").to("D").withEdge("3") @@ -50,7 +50,7 @@ public void testTree(){ .connect("F").to("M").withEdge("12") .connect("G").to("N").withEdge("13") .connect("G").to("O").withEdge("14") - .buildDirectedGraph(); + .createDirectedGraph(); Iterator> iterator = Hipster.createDepthFirstSearch(GraphSearchProblem.startingFrom("A").in(tree).build()).iterator(); @@ -61,13 +61,13 @@ public void testTree(){ @Test public void testGraphWithoutCycles(){ HipsterDirectedGraph graph = - GraphBuilder.create() + GraphBuilder.create() .connect("A").to("B").withEdge("1") .connect("A").to("C").withEdge("2") .connect("B").to("D").withEdge("3") .connect("B").to("E").withEdge("4") .connect("E").to("C").withEdge("5") - .buildDirectedGraph(); + .createDirectedGraph(); Iterator> iterator = Hipster.createDepthFirstSearch(GraphSearchProblem.startingFrom("A").in(graph).build()).iterator(); @@ -78,14 +78,14 @@ public void testGraphWithoutCycles(){ @Test public void testGraph(){ HipsterDirectedGraph graph = - GraphBuilder.create() + GraphBuilder.create() .connect("A").to("B").withEdge("1") .connect("A").to("C").withEdge("2") .connect("B").to("D").withEdge("3") .connect("B").to("E").withEdge("4") .connect("E").to("C").withEdge("5") .connect("C").to("A").withEdge("6") - .buildDirectedGraph(); + .createDirectedGraph(); Iterator> iterator = Hipster.createDepthFirstSearch(GraphSearchProblem.startingFrom("A").in(graph).build()).iterator(); diff --git a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java index 401a1af..d0ff7f9 100644 --- a/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java +++ b/hipster-examples/src/main/java/es/usc/citius/hipster/examples/DirectedGraphSearchExample.java @@ -24,7 +24,8 @@ public class DirectedGraphSearchExample { public static void main(String[] args){ // Create a simple weighted directed graph with Hipster where // vertices are Strings and edge values are just doubles - HipsterDirectedGraph graph = GraphBuilder.create() + HipsterDirectedGraph graph = + GraphBuilder.create() .connect("A").to("B").withEdge(4d) .connect("A").to("C").withEdge(2d) .connect("B").to("C").withEdge(5d) @@ -32,7 +33,7 @@ public static void main(String[] args){ .connect("C").to("E").withEdge(3d) .connect("D").to("F").withEdge(11d) .connect("E").to("D").withEdge(4d) - .buildDirectedGraph(); + .createDirectedGraph(); // Create the search problem. For graph problems, just use From 306b583b44836dd39f818cb820fd3d38c7d61100 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Tue, 16 Jun 2015 16:27:05 +0200 Subject: [PATCH 40/62] Implemented a flatMap over iterables/iterators. Refactored HashBasedHipsterGraph to use flatMap. --- .../hipster/graph/HashBasedHipsterGraph.java | 58 +++------------- .../java/es/usc/citius/hipster/util/F.java | 66 +++++++++++++++++-- 2 files changed, 69 insertions(+), 55 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 07b64a3..5ad5ab8 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -19,7 +19,6 @@ import es.usc.citius.hipster.util.F; import es.usc.citius.hipster.util.Function; -import es.usc.citius.hipster.util.Iterators; import java.util.*; @@ -144,59 +143,18 @@ public GraphEdge setValue(GraphEdge value) { } protected Iterable>> vedges(){ - // TODO: [java-8-migration] Change this ugly lazy iterator with Java 8 streams and flatmaps - return new Iterable>>() { + // TODO: [java-8-migration] + return F.flatMap(connected.entrySet(), new Function>>, Iterable>>>() { @Override - public Iterator>> iterator() { - return new Iterator>>() { - private Iterator vertices = connected.keySet().iterator(); - private V currentVertex = vertices.hasNext() ? vertices.next() : null; - private Iterator> edges = - currentVertex != null ? connected.get(currentVertex).iterator() : Iterators.>empty(); - private GraphEdge nextElement = null; - - private GraphEdge loadNext(){ - // Preload the next element - if (edges.hasNext()){ - return edges.next(); - } else if (vertices.hasNext()){ - currentVertex = vertices.next(); - edges = connected.get(currentVertex).iterator(); - // skip empty edge lists - return loadNext(); - } - return null; - } - - @Override - public boolean hasNext() { - // There can be empty lists, so we need to pre-compute the next element in advance - // to check whether there exist a next element or not. - if (nextElement == null) { - nextElement = loadNext(); - } - return nextElement != null; - } - + public Iterable>> apply(final Map.Entry>> entry) { + return F.map(entry.getValue(), new Function, Map.Entry>>() { @Override - public Map.Entry> next() { - // Load the next element - if (nextElement != null) { - GraphEdge next = nextElement; - nextElement = null; - return createEntry(currentVertex, next); - } else { - return createEntry(currentVertex, loadNext()); - } + public Map.Entry> apply(GraphEdge input) { + return createEntry(entry.getKey(), input); } - - @Override - public void remove() { - throw new UnsupportedOperationException("remove"); - } - }; + }); } - }; + }); } /** * Returns a list of the edges in the graph. diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java index f444adf..6581528 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java @@ -5,12 +5,15 @@ /** - * This class contains a few functional methods to lazily process iterables and iterators. + * This class contains a very limited set of functional methods to process iterables and iterators. * Required due to the removal of Guava dependencies (issue #125 https://github.com/citiususc/hipster/issues/125) + * NOTE: This class may be removed in future versions to take advantage of Java 8 functional Streams + * + * @author Pablo Rodríguez Mier */ public final class F { - public static Iterable map(final Iterable it, final Function mapf){ + public static Iterable map(final Iterable it, final Function mapf){ return new Iterable() { @Override public Iterator iterator() { @@ -19,7 +22,7 @@ public Iterator iterator() { }; } - public static Iterator map(final Iterator it, final Function mapf){ + public static Iterator map(final Iterator it, final Function mapf){ return new Iterator() { @Override public boolean hasNext() { @@ -38,7 +41,7 @@ public void remove() { }; } - public static Iterable filter(final Iterable it, final Function condition) { + public static Iterable filter(final Iterable it, final Function condition) { return new Iterable() { @Override public Iterator iterator() { @@ -47,7 +50,7 @@ public Iterator iterator() { }; } - public static Iterator filter(final Iterator it, final Function condition) { + public static Iterator filter(final Iterator it, final Function condition) { return new Iterator() { private T next = null; @@ -88,4 +91,57 @@ public void remove() { }; } + public static Iterable flatMap(final Iterable it, final Function> mapf){ + return new Iterable() { + @Override + public Iterator iterator() { + return flatMap(it.iterator(), new Function>() { + @Override + public Iterator apply(E input) { + return mapf.apply(input).iterator(); + } + }); + } + }; + } + + public static Iterator flatMap(final Iterator it, final Function> mapf){ + return new Iterator() { + private Iterator> mapIt = map(it, mapf); + private Iterator current = mapIt.hasNext() ? mapIt.next() : Iterators.empty(); + private T t; + + private T loadNext(){ + if (current.hasNext()) return current.next(); + if (mapIt.hasNext()){ + current = mapIt.next(); + return loadNext(); + } + return null; + } + + @Override + public boolean hasNext() { + if (t == null) t = loadNext(); + return t != null; + } + + @Override + public T next() { + if (t != null) { + T next = t; + t = null; // consumed + return next; + } else { + return loadNext(); + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } + }; + } + } From dcb51ba75d0e26217281cf54e06485921658649e Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Tue, 16 Jun 2015 17:35:30 +0200 Subject: [PATCH 41/62] DRY F functions using a new abstract iterator --- .../java/es/usc/citius/hipster/util/F.java | 95 ++++--------------- .../es/usc/citius/hipster/util/Iterators.java | 37 ++++++++ 2 files changed, 57 insertions(+), 75 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java index 6581528..af0abb9 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java @@ -23,20 +23,13 @@ public Iterator iterator() { } public static Iterator map(final Iterator it, final Function mapf){ - return new Iterator() { + return new Iterators.AbstractIterator() { @Override - public boolean hasNext() { - return it.hasNext(); - } - - @Override - public E next() { - return mapf.apply(it.next()); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("remove"); + protected E computeNext() { + if (it.hasNext()){ + return mapf.apply(it.next()); + } + return null; } }; } @@ -51,42 +44,16 @@ public Iterator iterator() { } public static Iterator filter(final Iterator it, final Function condition) { - return new Iterator() { - private T next = null; - - private T nextFiltered() { - T nextElem = null; - while (it.hasNext()) { - T elem = it.next(); - if (condition.apply(elem)) { - nextElem = elem; - break; - } - } - return nextElem; - } - - @Override - public boolean hasNext() { - if (next != null) return true; - // Preload the next edge - next = nextFiltered(); - return next != null; - } - + return new Iterators.AbstractIterator() { @Override - public T next() { - if (next != null) { - T elem = next; - next = null; - return elem; + protected T computeNext() { + while(it.hasNext()){ + T next = it.next(); + if (condition.apply(next)){ + return next; + } } - return nextFiltered(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("remove"); + return null; } }; } @@ -106,41 +73,19 @@ public Iterator apply(E input) { } public static Iterator flatMap(final Iterator it, final Function> mapf){ - return new Iterator() { - private Iterator> mapIt = map(it, mapf); - private Iterator current = mapIt.hasNext() ? mapIt.next() : Iterators.empty(); - private T t; + return new Iterators.AbstractIterator() { + Iterator> mapIt = map(it, mapf); + Iterator current = mapIt.hasNext() ? mapIt.next() : Iterators.empty(); - private T loadNext(){ + @Override + protected T computeNext() { if (current.hasNext()) return current.next(); if (mapIt.hasNext()){ current = mapIt.next(); - return loadNext(); + return computeNext(); } return null; } - - @Override - public boolean hasNext() { - if (t == null) t = loadNext(); - return t != null; - } - - @Override - public T next() { - if (t != null) { - T next = t; - t = null; // consumed - return next; - } else { - return loadNext(); - } - } - - @Override - public void remove() { - throw new UnsupportedOperationException("remove"); - } }; } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java index 2302ca0..dd2347b 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java @@ -5,6 +5,43 @@ import java.util.NoSuchElementException; public final class Iterators { + + public static abstract class AbstractIterator implements Iterator { + protected E current; + + protected AbstractIterator(){} + + protected abstract E computeNext(); + + @Override + public boolean hasNext() { + if (current == null){ + current = computeNext(); + } + return current != null; + } + + protected void skip(){ + // Jump to the next element or terminate if done + + } + + @Override + public E next() { + if (current != null) { + E next = current; + current = null; + return next; + } + return computeNext(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } + } + public static Iterator empty() { return new Iterator() { @Override From f1cfe3f3365140f879a60725c7c57849152ae4bc Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Tue, 16 Jun 2015 18:54:56 +0200 Subject: [PATCH 42/62] Removed skip() (unused function). Iterators.empty() cleaned up --- .../java/es/usc/citius/hipster/util/F.java | 7 +++++-- .../es/usc/citius/hipster/util/Iterators.java | 21 +++---------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java index af0abb9..ccc550e 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/F.java @@ -5,10 +5,13 @@ /** - * This class contains a very limited set of functional methods to process iterables and iterators. - * Required due to the removal of Guava dependencies (issue #125 https://github.com/citiususc/hipster/issues/125) + * This class contains a very limited set of functions to process iterables and iterators in a lazy way. + * Guava / Java 8 is not an option due to current size / compatibility restrictions. + * Required since the removal of Guava dependencies (issue #125 https://github.com/citiususc/hipster/issues/125) * NOTE: This class may be removed in future versions to take advantage of Java 8 functional Streams * + * Pure functional programmers, please forgive us for this crime + * * @author Pablo Rodríguez Mier */ public final class F { diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java index dd2347b..a489792 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/util/Iterators.java @@ -21,11 +21,6 @@ public boolean hasNext() { return current != null; } - protected void skip(){ - // Jump to the next element or terminate if done - - } - @Override public E next() { if (current != null) { @@ -43,20 +38,10 @@ public void remove() { } public static Iterator empty() { - return new Iterator() { - @Override - public boolean hasNext() { - return false; - } - - @Override - public E next() { - throw new NoSuchElementException("Iterator is empty"); - } - + return new AbstractIterator() { @Override - public void remove() { - throw new UnsupportedOperationException("remove"); + protected E computeNext() { + return null; } }; } From c0f6f7cb93b3d2ca72e1f8ed5702b025e8a20f9a Mon Sep 17 00:00:00 2001 From: pablormier Date: Wed, 17 Jun 2015 23:26:12 +0200 Subject: [PATCH 43/62] Added coveralls.io maven plugin --- pom.xml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pom.xml b/pom.xml index d0f71a5..22cb7e1 100644 --- a/pom.xml +++ b/pom.xml @@ -268,6 +268,12 @@ maven-site-plugin
    + + org.eluder.coveralls + coveralls-maven-plugin + 3.0.1 + + org.codehaus.mojo @@ -277,6 +283,23 @@ + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.5.2 + + + + html + xml + + + + + + citius-snapshot-deploy From ba1a1c08c343dab8570a28ccca14ec66aaa40d84 Mon Sep 17 00:00:00 2001 From: pablormier Date: Wed, 17 Jun 2015 23:37:58 +0200 Subject: [PATCH 44/62] added cobertura report tas to travis.yml --- .travis.yml | 1 + pom.xml | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/.travis.yml b/.travis.yml index c11a535..c79885c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ after_success: #- .config/deploy-site.sh - chmod +x .config/deploy-artifacts.sh - .config/deploy-artifacts.sh +- mvn clean cobertura:cobertura coveralls:cobertura env: global: - secure: ILdOYjPt+8g5rlexXBYAhECtn5Zm26FRf0/nwCxUU303qtzFQyMcxinIC93aun880OnINjA7fzQeBkG4P+LSVOAXbmGTGhtyPBzMOGcnZouJM/RyXHUft6tAXPimXQ7JjDFjyv7EzSeStk/4WEp0mkxheIryZS3X1pbED1TqgUM= diff --git a/pom.xml b/pom.xml index 22cb7e1..415da8e 100644 --- a/pom.xml +++ b/pom.xml @@ -279,6 +279,13 @@ org.codehaus.mojo cobertura-maven-plugin 2.6 + + xml + 256m + + true + + From ceeeb7f95f6bf37ce99b3b084cfa23b3bd01b129 Mon Sep 17 00:00:00 2001 From: pablormier Date: Wed, 17 Jun 2015 23:54:31 +0200 Subject: [PATCH 45/62] fix #139 --- .travis.yml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c79885c..dbb13d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ after_success: #- .config/deploy-site.sh - chmod +x .config/deploy-artifacts.sh - .config/deploy-artifacts.sh -- mvn clean cobertura:cobertura coveralls:cobertura +- mvn clean cobertura:cobertura coveralls:report env: global: - secure: ILdOYjPt+8g5rlexXBYAhECtn5Zm26FRf0/nwCxUU303qtzFQyMcxinIC93aun880OnINjA7fzQeBkG4P+LSVOAXbmGTGhtyPBzMOGcnZouJM/RyXHUft6tAXPimXQ7JjDFjyv7EzSeStk/4WEp0mkxheIryZS3X1pbED1TqgUM= diff --git a/pom.xml b/pom.xml index 415da8e..6cbf113 100644 --- a/pom.xml +++ b/pom.xml @@ -271,7 +271,7 @@ org.eluder.coveralls coveralls-maven-plugin - 3.0.1 + 3.1.0 From 0a98613df30c2e62fb859767fa08a681e9381b12 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Thu, 18 Jun 2015 13:47:25 +0200 Subject: [PATCH 46/62] Moved graphs package-info --- .../es/usc/citius/hipster/{util => }/graph/package-info.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename hipster-core/src/main/java/es/usc/citius/hipster/{util => }/graph/package-info.java (87%) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/package-info.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/package-info.java similarity index 87% rename from hipster-core/src/main/java/es/usc/citius/hipster/util/graph/package-info.java rename to hipster-core/src/main/java/es/usc/citius/hipster/graph/package-info.java index 8e82879..3bbcdb4 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/util/graph/package-info.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/package-info.java @@ -15,6 +15,6 @@ */ /** - * Simple Graph model used for algorithm validation. + * Lightweight graph interfaces and in-memory graph implementations. */ -package es.usc.citius.hipster.util.graph; \ No newline at end of file +package es.usc.citius.hipster.graph; \ No newline at end of file From 56424a9de105fe63c6afeea2926c919d785fd46c Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Thu, 18 Jun 2015 13:56:58 +0200 Subject: [PATCH 47/62] completed testAradRoads --- .../hipster/util/graph/RomanianProblemGraph.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java index 62f7b52..30786fc 100644 --- a/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java +++ b/hipster-core/src/test/java/es/usc/citius/hipster/util/graph/RomanianProblemGraph.java @@ -17,11 +17,19 @@ package es.usc.citius.hipster.util.graph; +import com.google.common.collect.Sets; +import es.usc.citius.hipster.graph.GraphEdge; import es.usc.citius.hipster.graph.HipsterGraph; +import es.usc.citius.hipster.graph.UndirectedEdge; import es.usc.citius.hipster.util.examples.RomanianProblem; import org.junit.Before; import org.junit.Test; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; + public class RomanianProblemGraph { private HipsterGraph graph; @@ -32,7 +40,11 @@ public void setUp() { @Test public void testAradRoads() { - System.out.println(graph.edgesOf(RomanianProblem.City.Arad)); - System.out.println(graph.edgesOf(RomanianProblem.City.Sibiu)); + Set> roads = Sets.newHashSet(graph.edgesOf(RomanianProblem.City.Arad)); + Set> expected = new HashSet<>(); + expected.add(new UndirectedEdge(RomanianProblem.City.Arad, RomanianProblem.City.Zerind, 75d)); + expected.add(new UndirectedEdge(RomanianProblem.City.Arad, RomanianProblem.City.Sibiu, 140d)); + expected.add(new UndirectedEdge(RomanianProblem.City.Arad, RomanianProblem.City.Timisoara, 118d)); + assertEquals(expected, roads); } } From 634b48dd500ef84133d082b86ec764060c5bc3bd Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Thu, 18 Jun 2015 14:01:28 +0200 Subject: [PATCH 48/62] renamed Guava Table based implementation classes of graphs --- ...rectedGraph.java => HashTableHipsterDirectedGraph.java} | 6 +++--- ...shBasedHipsterGraph.java => HashTableHipsterGraph.java} | 7 +++---- ...aphTest.java => HashTableHipsterDirectedGraphTest.java} | 6 +++--- ...ipsterGraphTest.java => HashTableHipsterGraphTest.java} | 6 +++--- 4 files changed, 12 insertions(+), 13 deletions(-) rename hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/{HashBasedHipsterDirectedGraph.java => HashTableHipsterDirectedGraph.java} (87%) rename hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/{HashBasedHipsterGraph.java => HashTableHipsterGraph.java} (94%) rename hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/{HashBasedHipsterDirectedGraphTest.java => HashTableHipsterDirectedGraphTest.java} (94%) rename hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/{HashBasedHipsterGraphTest.java => HashTableHipsterGraphTest.java} (96%) diff --git a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterDirectedGraph.java similarity index 87% rename from hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java rename to hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterDirectedGraph.java index 64b3a4c..dc43f1d 100644 --- a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraph.java +++ b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterDirectedGraph.java @@ -25,7 +25,7 @@ * * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ -public class HashBasedHipsterDirectedGraph extends HashBasedHipsterGraph implements HipsterDirectedGraph { +public class HashTableHipsterDirectedGraph extends HashTableHipsterGraph implements HipsterDirectedGraph { @Override public GraphEdge connect(V v1, V v2, E value){ @@ -47,7 +47,7 @@ public Iterable> incomingEdgesOf(V vertex) { return graphTable.column(vertex).values(); } - public static HashBasedHipsterDirectedGraph create() { - return new HashBasedHipsterDirectedGraph(); + public static HashTableHipsterDirectedGraph create() { + return new HashTableHipsterDirectedGraph(); } } diff --git a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterGraph.java similarity index 94% rename from hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java rename to hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterGraph.java index ecb249c..9ce5c27 100644 --- a/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraph.java +++ b/hipster-extensions/src/main/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterGraph.java @@ -18,7 +18,6 @@ import com.google.common.base.Preconditions; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Sets; -import es.usc.citius.hipster.graph.DirectedEdge; import es.usc.citius.hipster.graph.GraphEdge; import es.usc.citius.hipster.graph.HipsterGraph; import es.usc.citius.hipster.graph.UndirectedEdge; @@ -31,7 +30,7 @@ * * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> */ -public class HashBasedHipsterGraph implements HipsterGraph { +public class HashTableHipsterGraph implements HipsterGraph { protected HashBasedTable> graphTable = HashBasedTable.create(); // keep extra info for all those disconnected vertices protected Set disconnected = new HashSet(); @@ -105,7 +104,7 @@ public Iterable> edgesOf(V vertex) { return Sets.union(Sets.newHashSet(graphTable.row(vertex).values()), Sets.newHashSet(graphTable.column(vertex).values())); } - public static HashBasedHipsterGraph create() { - return new HashBasedHipsterGraph(); + public static HashTableHipsterGraph create() { + return new HashTableHipsterGraph(); } } diff --git a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterDirectedGraphTest.java similarity index 94% rename from hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java rename to hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterDirectedGraphTest.java index 0525a57..e8232c9 100644 --- a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterDirectedGraphTest.java +++ b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterDirectedGraphTest.java @@ -27,13 +27,13 @@ import static org.junit.Assert.assertEquals; -public class HashBasedHipsterDirectedGraphTest extends HashBasedHipsterGraphTest { - private HashBasedHipsterDirectedGraph directedGraph; +public class HashTableHipsterDirectedGraphTest extends HashTableHipsterGraphTest { + private HashTableHipsterDirectedGraph directedGraph; @Before @Override public void setUp() { - directedGraph = HashBasedHipsterDirectedGraph.create(); + directedGraph = HashTableHipsterDirectedGraph.create(); directedGraph.connect("A", "B", 4d); directedGraph.connect("A", "C", 2d); directedGraph.connect("B", "C", 5d); diff --git a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterGraphTest.java similarity index 96% rename from hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java rename to hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterGraphTest.java index 836fb3c..81b9a9c 100644 --- a/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashBasedHipsterGraphTest.java +++ b/hipster-extensions/src/test/java/es/usc/citius/hipster/extensions/graph/HashTableHipsterGraphTest.java @@ -30,12 +30,12 @@ import static org.junit.Assert.assertTrue; -public class HashBasedHipsterGraphTest { - protected HashBasedHipsterGraph graph; +public class HashTableHipsterGraphTest { + protected HashTableHipsterGraph graph; @Before public void setUp(){ - graph = HashBasedHipsterGraph.create(); + graph = HashTableHipsterGraph.create(); graph.connect("A", "B", 4d); graph.connect("A", "C", 2d); graph.connect("B", "C", 5d); From ce84942109165351bb80d11f21298a87fe90fb75 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 22 Jun 2015 10:04:52 +0200 Subject: [PATCH 49/62] fixed version --- hipster-all/pom.xml | 2 +- hipster-core/pom.xml | 2 +- hipster-examples/pom.xml | 2 +- hipster-extensions/pom.xml | 2 +- hipster-test/pom.xml | 2 +- hipster-third-party-graphs/pom.xml | 2 +- pom.xml | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hipster-all/pom.xml b/hipster-all/pom.xml index dc9eed4..8b22b81 100644 --- a/hipster-all/pom.xml +++ b/hipster-all/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT 4.0.0 hipster-all diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index dc0d5bf..dd7e47a 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -5,7 +5,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT 4.0.0 hipster-core diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index 7208490..49de03e 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT 4.0.0 hipster-examples diff --git a/hipster-extensions/pom.xml b/hipster-extensions/pom.xml index 3686369..4d6d3f9 100644 --- a/hipster-extensions/pom.xml +++ b/hipster-extensions/pom.xml @@ -5,7 +5,7 @@ hipster-pom es.usc.citius.hipster - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT 4.0.0 diff --git a/hipster-test/pom.xml b/hipster-test/pom.xml index 6f5efb9..db11f26 100644 --- a/hipster-test/pom.xml +++ b/hipster-test/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT 4.0.0 hipster-test diff --git a/hipster-third-party-graphs/pom.xml b/hipster-third-party-graphs/pom.xml index 028ad65..d7dde60 100644 --- a/hipster-third-party-graphs/pom.xml +++ b/hipster-third-party-graphs/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT 4.0.0 diff --git a/pom.xml b/pom.xml index 6cbf113..c261824 100644 --- a/pom.xml +++ b/pom.xml @@ -15,13 +15,13 @@ http://hipster4j.org - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT es.usc.citius.hipster hipster-pom pom - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT hipster-pom ${site.url} From 76a50bbcb469ffdf06db535c7832ae01b6d099f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Mon, 22 Jun 2015 10:07:38 +0200 Subject: [PATCH 50/62] Update README.md fixed not closed tag --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 53ebea5..f5745ef 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ The current version of the library comes with some very well-known and wide used * Hill-Climbing. * Enforced-Hill-Climbing. * Other (experimental implementations) - * Multiobjective LS algorithm. Original paper: Martins, E. D. Q. V., & Santos, J. L. E. (1999). *"The labeling algorithm for the multiobjective shortest path problem"*. Departamento de Matematica, Universidade de Coimbra, Portugal, Tech. Rep. TR-99/005 + * Multiobjective LS algorithm. Original paper: Martins, E. D. Q. V., & Santos, J. L. E. (1999). *"The labeling algorithm for the multiobjective shortest path problem"*. Departamento de Matematica, Universidade de Coimbra, Portugal, Tech. Rep. TR-99/005 * 3rd party adapters: * [Java Universal/Graph (JUNG)](http://jung.sourceforge.net/) adapter. From 47ebf17e66e876194d109821b7cf391ca9ffe05e Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 13 Jul 2015 13:44:06 +0200 Subject: [PATCH 51/62] removed spaces in README.md [ci-skip] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f5745ef..9042a27 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ The current version of the library comes with some very well-known and wide used * Hill-Climbing. * Enforced-Hill-Climbing. * Other (experimental implementations) - * Multiobjective LS algorithm. Original paper: Martins, E. D. Q. V., & Santos, J. L. E. (1999). *"The labeling algorithm for the multiobjective shortest path problem"*. Departamento de Matematica, Universidade de Coimbra, Portugal, Tech. Rep. TR-99/005 + * Multiobjective LS algorithm. Original paper: Martins, E. D. Q. V., & Santos, J. L. E. (1999). *"The labeling algorithm for the multiobjective shortest path problem"*. Departamento de Matematica, Universidade de Coimbra, Portugal, Tech. Rep. TR-99/005 * 3rd party adapters: * [Java Universal/Graph (JUNG)](http://jung.sourceforge.net/) adapter. From 264625ee79672ce1fa209013076c008d1bf92c41 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 13 Jul 2015 17:53:15 +0200 Subject: [PATCH 52/62] changed dev version to the next future RC2 release [ci-skip] --- hipster-all/pom.xml | 2 +- hipster-core/pom.xml | 2 +- hipster-examples/pom.xml | 2 +- hipster-extensions/pom.xml | 2 +- hipster-test/pom.xml | 2 +- hipster-third-party-graphs/pom.xml | 2 +- pom.xml | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hipster-all/pom.xml b/hipster-all/pom.xml index 8b22b81..2fedae5 100644 --- a/hipster-all/pom.xml +++ b/hipster-all/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT 4.0.0 hipster-all diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index dd7e47a..0421218 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -5,7 +5,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT 4.0.0 hipster-core diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index 49de03e..11e2e4e 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT 4.0.0 hipster-examples diff --git a/hipster-extensions/pom.xml b/hipster-extensions/pom.xml index 4d6d3f9..b4d7867 100644 --- a/hipster-extensions/pom.xml +++ b/hipster-extensions/pom.xml @@ -5,7 +5,7 @@ hipster-pom es.usc.citius.hipster - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT 4.0.0 diff --git a/hipster-test/pom.xml b/hipster-test/pom.xml index db11f26..4714d5c 100644 --- a/hipster-test/pom.xml +++ b/hipster-test/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT 4.0.0 hipster-test diff --git a/hipster-third-party-graphs/pom.xml b/hipster-third-party-graphs/pom.xml index d7dde60..d056808 100644 --- a/hipster-third-party-graphs/pom.xml +++ b/hipster-third-party-graphs/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT 4.0.0 diff --git a/pom.xml b/pom.xml index c261824..ce6b5ee 100644 --- a/pom.xml +++ b/pom.xml @@ -15,13 +15,13 @@ http://hipster4j.org - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT es.usc.citius.hipster hipster-pom pom - 1.1.0-SNAPSHOT + 1.0.0-rc2-SNAPSHOT hipster-pom ${site.url} From 9ea4ebd6c5dd3eaf660dfb80bd613e9d5b4f0f5f Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Tue, 14 Jul 2015 11:51:20 +0200 Subject: [PATCH 53/62] Added getter/setter of the internal HashMap used in HashBasedHipsterGraph [skip-ci] --- .../citius/hipster/graph/HashBasedHipsterGraph.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 5ad5ab8..60f2dcf 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -187,6 +187,18 @@ public Iterable> edgesOf(V vertex) { return set; } + /** + * Returns the internal HashMap representation of the graph + * @return HashMap where keys are vertices and values a set with the connected edges + */ + public HashMap>> getConnected() { + return connected; + } + + public void setConnected(HashMap>> connected) { + this.connected = connected; + } + public static HashBasedHipsterGraph create() { return new HashBasedHipsterGraph(); } From 1517233b5b2b947ac6b4cdd06673de50b5461c14 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Thu, 16 Jul 2015 18:49:45 +0200 Subject: [PATCH 54/62] Improved Javadoc of the graph interfaces [skip-ci] --- .../hipster/graph/HashBasedHipsterGraph.java | 27 ++------------ .../citius/hipster/graph/HipsterGraph.java | 14 ++++---- .../hipster/graph/HipsterMutableGraph.java | 36 +++++++++++++++++++ 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java index 60f2dcf..102925e 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HashBasedHipsterGraph.java @@ -23,10 +23,8 @@ import java.util.*; /** - * Implementation of a HipsterGraph using a Guava Hash Table. - * - * @author Adrián González Sieira <adrian.gonzalez@usc.es> - * @author Pablo Rodríguez Mier <pablo.rodriguez.mier@usc.es> + * Lightweight implementation of an in-memory, mutable graph backed to a {@link HashMap} where + * keys are vertices and edges are {@link GraphEdge}s */ public class HashBasedHipsterGraph implements HipsterMutableGraph { protected HashMap>> connected; @@ -35,11 +33,6 @@ public HashBasedHipsterGraph(){ this.connected = new HashMap>>(); } - /** - * Add a new node to the graph with no connections. - * - * @param v vertex to be added - */ @Override public boolean add(V v){ //add a new entry to the hash map if it does not exist @@ -59,11 +52,6 @@ public Set add(V... vertices) { return added; } - /** - * Remove a vertex from the graph. - * - * @param v vertex to be removed - */ @Override public boolean remove(V v){ // Remove all edges related to v @@ -96,15 +84,6 @@ public Set remove(V... vertices) { return removed; } - /** - * Connect to vertices in the graph. If the vertices are not in the graph, they are automatically - * added to the graph before connecting them. - * - * @param v1 source vertex - * @param v2 destination vertex - * @param value edge value - * @return the generated edge - */ @Override public GraphEdge connect(V v1, V v2, E value){ // Check non-null arguments @@ -194,7 +173,7 @@ public Iterable> edgesOf(V vertex) { public HashMap>> getConnected() { return connected; } - + public void setConnected(HashMap>> connected) { this.connected = connected; } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterGraph.java index 3002b0d..9d28090 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterGraph.java @@ -17,27 +17,27 @@ package es.usc.citius.hipster.graph; /** - * Defines a graph in terms of edges and vertices. + * Basic definition of the read-only methods for a graph in terms of edges and vertices. * @param vertex type. * @param edge type. */ public interface HipsterGraph { /** - * Return the edges. - * @return + * Returns an {@link Iterable} of the edges in the graph. + * @return an iterable of {@link GraphEdge} in the graph */ Iterable> edges(); /** - * Return the vertices. - * @return + * Returns an iterable of the vertices in the graph. + * @return iterable of vertices */ Iterable vertices(); /** * Return all the edges that are connected with the given vertex. - * @param vertex - * @return + * @param vertex vertex to be queried + * @return an iterable of {@link GraphEdge}s connected to the vertex */ Iterable> edgesOf(V vertex); diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java index 36029bc..3b5af48 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/HipsterMutableGraph.java @@ -3,10 +3,46 @@ import java.util.Set; +/** + * Interface that defines the basic mutable methods to manipulate graphs + * @param vertex type. + * @param edge type. + */ public interface HipsterMutableGraph extends HipsterGraph { + /** + * Adds a new vertex to the graph. + * @param vertex vertex to be added + * @return true if the vertex was added to the graph, false if the vertex is already present + */ boolean add(V vertex); + + /** + * Adds multiple vertices to the graph. + * @param vertices vertices to be added + * @return set with the vertices added + */ Set add(V... vertices); + + /** + * Removes the vertex from the graph + * @param vertex vertex to be removed + * @return true if the vertex was removed, false if the vertex is not present + */ boolean remove(V vertex); + + /** + * Removes multiple vertices from the graph + * @param vertices vertices to be removed + * @return set of vertices removed from the graph + */ Set remove(V... vertices); + + /** + * Connects to vertices of the graph through an edge + * @param vertex1 source vertex + * @param vertex2 target (destination) vertex + * @param edgeValue value of the edge connecting vertex1 and vertex2 + * @return a new {@link GraphEdge} connecting both vertices + */ GraphEdge connect(V vertex1, V vertex2, E edgeValue); } From d1ab691b2556266b3042e4ee7941a1205cb1a415 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Thu, 16 Jul 2015 18:52:33 +0200 Subject: [PATCH 55/62] Scala-like syntactics to retrieve the elements of a Pair [skip-ci] --- .../src/main/java/es/usc/citius/hipster/graph/Pair.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java index c78b04d..5863bdd 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java @@ -11,6 +11,14 @@ public Pair(E e1, E e2) { this.e2 = e2; } + public E _1() { + return e1; + } + + public E _2() { + return e2; + } + public E getE1() { return e1; } From 763c327ac0f26ff26787cfa267872881d2c57487 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Fri, 17 Jul 2015 18:16:30 +0200 Subject: [PATCH 56/62] Added toString method in Pair / UnorderedPair --- .../src/main/java/es/usc/citius/hipster/graph/Pair.java | 5 +++++ .../main/java/es/usc/citius/hipster/graph/UnorderedPair.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java index 5863bdd..f1f0c86 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/Pair.java @@ -53,4 +53,9 @@ public int hashCode() { result = 31 * result + e2.hashCode(); return result; } + + @Override + public String toString() { + return "(" + e1 + ", " + e2 + ")"; + } } diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java index db7d048..74c04d0 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/graph/UnorderedPair.java @@ -43,4 +43,9 @@ public boolean equals(Object o) { public int hashCode() { return e1.hashCode() + e2.hashCode(); } + + @Override + public String toString() { + return "(" + e1 + ", " + e2 + ")"; + } } From abb4a1e721257f3e3f43e8b4689ba6abca660ebe Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 3 Aug 2015 18:48:01 +0200 Subject: [PATCH 57/62] Fixes #132 --- .../model/function/impl/LazyNodeExpander.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java index 770b01b..5b9bce7 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyNodeExpander.java @@ -21,6 +21,8 @@ import es.usc.citius.hipster.model.function.NodeExpander; import es.usc.citius.hipster.model.function.NodeFactory; import es.usc.citius.hipster.model.function.TransitionFunction; +import es.usc.citius.hipster.util.F; +import es.usc.citius.hipster.util.Function; import java.util.ArrayList; @@ -56,11 +58,12 @@ public Iterable expand(final N node) { // The default expansion of a node consists of // computing the successor transitions of the current state and // generating the associated nodes for each successor - ArrayList nodes = new ArrayList(); - for(Transition current : tf.transitionsFrom(node.state())){ - nodes.add(factory.makeNode(node, current)); - } - return nodes; + return F.map(tf.transitionsFrom(node.state()), new Function, N>() { + @Override + public N apply(Transition t) { + return factory.makeNode(node, t); + } + }); } /** From c4ed9c96c70c2e4f7553b2492bf1015851861bad Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 3 Aug 2015 18:50:28 +0200 Subject: [PATCH 58/62] Fixes #133 --- .../impl/LazyActionStateTransitionFunction.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java index 012d513..054abd2 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/LazyActionStateTransitionFunction.java @@ -21,6 +21,8 @@ import es.usc.citius.hipster.model.function.ActionFunction; import es.usc.citius.hipster.model.function.ActionStateTransitionFunction; import es.usc.citius.hipster.model.function.TransitionFunction; +import es.usc.citius.hipster.util.F; +import es.usc.citius.hipster.util.Function; import java.util.ArrayList; @@ -52,12 +54,11 @@ public LazyActionStateTransitionFunction(ActionFunction af, ActionStateTra @Override public Iterable> transitionsFrom(final S state) { - ArrayList> transitions = new ArrayList>(); - //generate set of actions - for(A applicableAction : af.actionsFor(state)){ - //generate transition for each action - transitions.add(new Transition(state, applicableAction, tf.apply(applicableAction, state))); - } - return transitions; + return F.map(af.actionsFor(state), new Function>() { + @Override + public Transition apply(A a) { + return Transition.create(state, a, tf.apply(a, state)); + } + }); } } From bb67d88c4ccfa5d93af1772f7dbc1ee515e2f33f Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Mon, 3 Aug 2015 19:05:31 +0200 Subject: [PATCH 59/62] Fixes #134 --- .../function/impl/StateTransitionFunction.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java index bd371a0..7b4c5f4 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/model/function/impl/StateTransitionFunction.java @@ -19,6 +19,8 @@ import es.usc.citius.hipster.model.Transition; import es.usc.citius.hipster.model.function.TransitionFunction; +import es.usc.citius.hipster.util.F; +import es.usc.citius.hipster.util.Function; import java.util.ArrayList; @@ -37,13 +39,13 @@ public abstract class StateTransitionFunction implements TransitionFunction> transitionsFrom(final S state) { - ArrayList> transitions = new ArrayList>(); //generate successor states - for(S current : successorsOf(state)){ - //generate successor transitions from the states - transitions.add(new Transition(state, null, current)); - } - return transitions; + return F.map(successorsOf(state), new Function>() { + @Override + public Transition apply(S current) { + return Transition.create(state, null, current); + } + }); } /** From 29d25b846366dd21cff62ff2cd2ae5ee8dd51b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Rodr=C3=ADguez=20Mier?= Date: Tue, 4 Aug 2015 12:52:24 +0200 Subject: [PATCH 60/62] Update .travis.yml Changed travis config to use the new container-based system. Fixes #135 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index dbb13d8..c20c2e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ jdk: branches: except: - /^(?i:wip).*$/ +sudo: false after_success: #- chmod +x .config/deploy-site.sh #- .config/deploy-site.sh From 57f5a9af1d54f373a2d5bd9ec7370b6bbc732d89 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Tue, 4 Aug 2015 17:52:51 +0200 Subject: [PATCH 61/62] Fixed typo in AStar description --- .../src/main/java/es/usc/citius/hipster/algorithm/AStar.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java index 25aae88..6748717 100644 --- a/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java +++ b/hipster-core/src/main/java/es/usc/citius/hipster/algorithm/AStar.java @@ -26,7 +26,7 @@ * Implementation of the A* algorithm. The A* algorithm extends the original * Dijkstra's algorithm by including heuristics to improve the search. By default, * the implementation uses a {@link java.util.PriorityQueue} for the nodes, which requires - * {@literal O(n*log n)} time for insertions. The queue can be changed to use another + * {@literal O(log n)} time for insertions. The queue can be changed to use another * type of queue, for example a fibonacci heap as a queue, which works with constant amortized * time for insertions. *

    From 75ea2fa35c35e9e26ecb49d465e9bb2c9e353894 Mon Sep 17 00:00:00 2001 From: "pablo.rodriguez.mier" Date: Wed, 12 Aug 2015 12:21:19 +0200 Subject: [PATCH 62/62] Version bumped to v1.0.0-rc2 --- hipster-all/pom.xml | 2 +- hipster-core/pom.xml | 2 +- hipster-examples/pom.xml | 2 +- hipster-extensions/pom.xml | 2 +- hipster-test/pom.xml | 2 +- hipster-third-party-graphs/pom.xml | 2 +- pom.xml | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hipster-all/pom.xml b/hipster-all/pom.xml index 2fedae5..0e6902f 100644 --- a/hipster-all/pom.xml +++ b/hipster-all/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 4.0.0 hipster-all diff --git a/hipster-core/pom.xml b/hipster-core/pom.xml index 0421218..5d17218 100644 --- a/hipster-core/pom.xml +++ b/hipster-core/pom.xml @@ -5,7 +5,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 4.0.0 hipster-core diff --git a/hipster-examples/pom.xml b/hipster-examples/pom.xml index 11e2e4e..188f85f 100644 --- a/hipster-examples/pom.xml +++ b/hipster-examples/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 4.0.0 hipster-examples diff --git a/hipster-extensions/pom.xml b/hipster-extensions/pom.xml index b4d7867..eefaefd 100644 --- a/hipster-extensions/pom.xml +++ b/hipster-extensions/pom.xml @@ -5,7 +5,7 @@ hipster-pom es.usc.citius.hipster - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 4.0.0 diff --git a/hipster-test/pom.xml b/hipster-test/pom.xml index 4714d5c..9ee9433 100644 --- a/hipster-test/pom.xml +++ b/hipster-test/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 4.0.0 hipster-test diff --git a/hipster-third-party-graphs/pom.xml b/hipster-third-party-graphs/pom.xml index d056808..614d071 100644 --- a/hipster-third-party-graphs/pom.xml +++ b/hipster-third-party-graphs/pom.xml @@ -21,7 +21,7 @@ es.usc.citius.hipster hipster-pom - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 4.0.0 diff --git a/pom.xml b/pom.xml index ce6b5ee..7daf610 100644 --- a/pom.xml +++ b/pom.xml @@ -15,13 +15,13 @@ http://hipster4j.org - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 es.usc.citius.hipster hipster-pom pom - 1.0.0-rc2-SNAPSHOT + 1.0.0-rc2 hipster-pom ${site.url}