forked from edemo/PDEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the Floyd Warshall algorithm edemo#258
- Loading branch information
Showing
7 changed files
with
155 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/org/rulez/demokracia/pdengine/FloydWarshall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import java.util.Collection; | ||
|
||
import jersey.repackaged.com.google.common.collect.Sets; | ||
|
||
public class FloydWarshall { | ||
|
||
private TransitiveClosable beatTable; | ||
|
||
public FloydWarshall(final TransitiveClosable beatTable) { | ||
this.beatTable = beatTable; | ||
} | ||
|
||
public void computeTransitiveClosure() { | ||
Collection<Choice> keyCollection = beatTable.getKeyCollection(); | ||
keyCollection.forEach(i -> keyCollection.forEach(j -> computeMinimalRoute(i, j))); | ||
} | ||
|
||
private void computeMinimalRoute(final Choice i, final Choice j) { | ||
if (!i.equals(j)) { | ||
beatTable.getKeyCollection().forEach(k -> selectShorterPath(i, j, k)); | ||
} | ||
} | ||
|
||
private void selectShorterPath(final Choice i, final Choice j, final Choice k) { | ||
if (!Sets.newHashSet(i, j).contains(k)) { | ||
beatTable.selectShorterPath(i, j, k); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/rulez/demokracia/pdengine/TransitiveClosable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import org.rulez.demokracia.pdengine.dataobjects.Pair; | ||
|
||
import jersey.repackaged.com.google.common.collect.Sets; | ||
|
||
public interface TransitiveClosable extends ContainingBeats { | ||
|
||
default void computeTransitiveClosure() { | ||
FloydWarshall floydWarshall = new FloydWarshall(this); | ||
floydWarshall.computeTransitiveClosure(); | ||
} | ||
|
||
default void selectShorterPath(final Choice i, final Choice j, final Choice k) { | ||
if (!Sets.newHashSet(i, j).contains(k)) { | ||
setElement(i, j, | ||
compareBeats(getElement(i, j), | ||
lessBeat(getElement(i, k), getElement(k, j)))); | ||
} | ||
} | ||
|
||
default Pair lessBeat(final Pair a, final Pair b) { | ||
Pair greater = this.compareBeats(a, b); | ||
return greater.equals(a) ? b : a; | ||
} | ||
} |
19 changes: 13 additions & 6 deletions
19
src/test/java/org/rulez/demokracia/pdengine/BeatTableCompareBeatsTieTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; | ||
import org.rulez.demokracia.pdengine.annotations.TestedFeature; | ||
import org.rulez.demokracia.pdengine.annotations.TestedOperation; | ||
import org.rulez.demokracia.pdengine.dataobjects.Pair; | ||
import org.rulez.demokracia.pdengine.exception.ReportedException; | ||
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable; | ||
|
||
@TestedFeature("Unimplemented") | ||
@TestedOperation("Unimplemented") | ||
@TestedBehaviour("Unimplemented") | ||
@TestedFeature("Schulze method") | ||
@TestedOperation("compare beats") | ||
@TestedBehaviour("if beats and loses are tie, give back the first option") | ||
public class BeatTableCompareBeatsTieTest extends CreatedBeatTable { | ||
|
||
@Override | ||
@Before | ||
public void setUp() throws ReportedException { | ||
super.setUp(); | ||
} | ||
|
||
@Test | ||
public void compareBeats_is_not_yet_implemented_when_the_result_is_tie() { | ||
assertUnimplemented(() -> beatTable.compareBeats(beats1, beats1)); | ||
public void compareBeats_returns_the_first_beat_when_the_result_is_tie() { | ||
Pair beat1 = new Pair(4, 3); | ||
Pair beat2 = new Pair(4, 3); | ||
|
||
assertTrue(beatTable.compareBeats(beat1, beat2) == beat1); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/test/java/org/rulez/demokracia/pdengine/BeatTableTransitiveClosureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Collection; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; | ||
import org.rulez.demokracia.pdengine.annotations.TestedFeature; | ||
import org.rulez.demokracia.pdengine.annotations.TestedOperation; | ||
import org.rulez.demokracia.pdengine.dataobjects.Pair; | ||
import org.rulez.demokracia.pdengine.exception.ReportedException; | ||
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTable; | ||
|
||
import jersey.repackaged.com.google.common.collect.Sets; | ||
|
||
@TestedFeature("Schulze method") | ||
@TestedOperation("compare beats") | ||
@TestedBehaviour("implements the Floyd-Warshall algorithm") | ||
public class BeatTableTransitiveClosureTest extends CreatedBeatTable { | ||
|
||
@Override | ||
@Before | ||
public void setUp() throws ReportedException { | ||
super.setUp(); | ||
createNewBeatTableWithComplexData(); | ||
} | ||
|
||
@Test | ||
public void transitive_closure_on_empty_beat_table_results_empty_result() { | ||
BeatTable actual = new BeatTable(); | ||
actual.computeTransitiveClosure(); | ||
assertTrue(actual.getKeyCollection().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void transitive_closure_computes_the_shortest_paths_by_pairs() { | ||
beatTable.computeTransitiveClosure(); | ||
Collection<Choice> keyCollection = beatTable.getKeyCollection(); | ||
for (Choice i : keyCollection) { | ||
for (Choice j : keyCollection) { | ||
if (i.equals(j)) { | ||
continue; | ||
} | ||
for (Choice k: keyCollection) { | ||
if (Sets.newHashSet(i, j).contains(k)) { | ||
continue; | ||
} | ||
Pair greater = beatTable.getElement(i, j); | ||
Pair less = beatTable.lessBeat(beatTable.getElement(i, k), beatTable.getElement(k, j)); | ||
assertEquals(greater, beatTable.compareBeats(less, greater)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void less_beat_returns_the_less_beat() { | ||
Pair pair1 = new Pair(20, 10); | ||
Pair pair2 = new Pair(10, 10); | ||
|
||
Pair lessBeat = beatTable.lessBeat(pair1, pair2); | ||
assertEquals(pair2, lessBeat); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters