Archived
0

implement multi-threading route search

This commit is contained in:
iMoHax
2014-09-12 15:23:35 +04:00
parent c4bdc3894e
commit ece5389f6f
12 changed files with 506 additions and 195 deletions

View File

@@ -84,6 +84,20 @@ public class TestUtil {
checkContains(collection, false, items);
}
@SafeVarargs
public static <T> void assertCollectionContainAny(Collection<T> collection, T... items){
boolean contain = false;
for (T item : items) {
if (collection.contains(item)){
contain = true;
break;
}
}
if (!contain){
Assert.fail(String.format("Collection should include any item from %s", items));
}
}
@SafeVarargs
public static <T> void assertCollectionContainAll(Collection<T> collection, T... items){
checkContains(collection, true, items);

View File

@@ -233,7 +233,8 @@ public class GraphTest extends Assert {
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6, x7), Path.toPath(x5, x4, x6, x7), Path.toPath(x5, x3, x6, x7));
paths = graph.getPathsTo(x7, 1);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x3, x6, x7));
assertEquals(1, paths.size());
TestUtil.assertCollectionContainAny(paths, Path.toPath(x5, x6, x7), Path.toPath(x5, x4, x6, x7), Path.toPath(x5, x3, x6, x7));
paths = graph.getPathsTo(x4);
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4), Path.toPath(x5, x6, x4), Path.toPath(x5, x3, x4),

View File

@@ -0,0 +1,52 @@
package ru.trader.graph;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import ru.trader.core.Market;
import ru.trader.core.Vendor;
import ru.trader.store.Store;
import java.io.InputStream;
import java.util.List;
public class RouteSearcherTest extends Assert {
private static Market market;
@Before
public void setUp() throws Exception {
InputStream is = getClass().getResourceAsStream("/world.xml");
market = Store.loadFromFile(is);
}
@Test
public void testRoutes() throws Exception {
// Balance: 6000000, cargo: 440, tank: 40, distance: 13.4, jumps: 6
// Ithaca (Palladium to LHS 3262) -> Morgor -> LHS 3006 -> LHS 3262 (Consumer Technology to Ithaca) -> LHS 3006 -> Morgor -> Ithaca
// Profit: 981200, avg: 490600, distance: 67.5, lands: 2
Vendor ithaca = market.get().stream().filter((v)->v.getName().equals("Ithaca")).findFirst().get();
Vendor lhs3262 = market.get().stream().filter((v)->v.getName().equals("LHS 3262")).findFirst().get();
RouteSearcher searcher = new RouteSearcher(13.4, 40, 50);
RouteGraph graph = new RouteGraph(ithaca, market.get(), 40, 13.4, true, 6);
graph.setLimit(440);
graph.setBalance(6000000);
List<Path<Vendor>> epaths = graph.getPathsTo(ithaca, 10);
PathRoute expect = epaths.stream().map(p -> (PathRoute) p).findFirst().get();
List<PathRoute> apaths = searcher.getPaths(ithaca, ithaca, market.get(), 6, 6000000, 440, 10);
PathRoute actual = apaths.stream().findFirst().get();
assertTrue("Routes is different",expect.isRoute(actual));
graph = new RouteGraph(lhs3262, market.get(), 40, 13.4, true, 6);
graph.setLimit(440);
graph.setBalance(6000000);
expect = graph.getPathsTo(lhs3262, 10).stream().map(p -> (PathRoute)p).findFirst().get();
actual = searcher.getPaths(lhs3262, lhs3262, market.get(), 6, 6000000, 440, 10).stream().findFirst().get();
assertTrue("Routes is different",expect.isRoute(actual));
}
}