Archived
0

Improved segment search

This commit is contained in:
iMoHax
2014-09-19 14:50:38 +04:00
parent ece5389f6f
commit d2196c6be1
11 changed files with 460 additions and 174 deletions

View File

@@ -8,6 +8,7 @@ import ru.trader.graph.PathRoute;
import ru.trader.store.Store;
import java.io.InputStream;
import java.util.Collection;
import java.util.Optional;
public class MarketAnalyzerTest2 extends Assert {
private static MarketAnalyzer analyzer;
@@ -32,11 +33,13 @@ public class MarketAnalyzerTest2 extends Assert {
analyzer.setCargo(440);analyzer.setTank(40);analyzer.setMaxDistance(13.4);analyzer.setJumps(6);
Collection<PathRoute> paths = analyzer.getPaths(ithaca, ithaca, 6000000);
PathRoute expect = PathRoute.toPathRoute(ithaca, morgor, lhs3006, lhs3262, lhs3006, morgor, ithaca);
PathRoute actual = paths.stream().filter((p)->p.equals(expect)).findFirst().get().getRoot();
Optional<PathRoute> path = paths.stream().filter((p)->p.equals(expect)).findFirst();
assertTrue(path.isPresent());
PathRoute actual = path.get().getRoot();
TestUtil.assertCollectionContain(paths, expect);
assertEquals(981200, actual.getProfit(), 0.00001);
assertEquals(72.42, actual.getDistance(), 0.01);
assertEquals(2, actual.getLandsCount());
assertEquals(490600, actual.getProfit()/actual.getLandsCount() , 0.00001);
assertEquals(490600, actual.getAvgProfit() , 0.00001);
}
}

View File

@@ -43,7 +43,7 @@ public class RouteGraphTest extends Assert {
public void testRoutes() throws Exception {
RouteGraph graph = new RouteGraph(v1, market.get(), 1, 1, true, 4);
graph.setBalance(500);
graph.setLimit(5);
graph.setCargo(5);
//Profit: 150 180 200 230 670 620 950 890 620 950 1015 1180 890 950 930
//Landings: 1 2 3 4 4 2 3 3 2 3 4 4 3 3 4
//Prof: 150 90 66.66 57.5 167.5 310 316.66 296.66 310 316.66 253.75 295 296.66 316.66 232.5

View File

@@ -25,11 +25,10 @@ public class RouteSearcherTest extends Assert {
// 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);
RouteSearcher searcher = new RouteSearcher(13.4, 40);
RouteGraph graph = new RouteGraph(ithaca, market.get(), 40, 13.4, true, 6);
graph.setLimit(440);
graph.setCargo(440);
graph.setBalance(6000000);
@@ -40,13 +39,38 @@ public class RouteSearcherTest extends Assert {
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);
}
@Test
public void testRoutes2() throws Exception {
// Balance: 6000000, cargo: 440, tank: 40, distance: 13.6, 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.6, 40);
RouteGraph graph = new RouteGraph(ithaca, market.get(), 40, 13.6, true, 6);
graph.setCargo(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.6, true, 6);
graph.setCargo(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));
apaths = searcher.getPaths(lhs3262, lhs3262, market.get(), 6, 6000000, 440, 10);
actual = apaths.stream().findFirst().get();
assertEquals("Routes is different",expect.getAvgProfit(), actual.getAvgProfit(), 0.00001);
}
}

View File

@@ -0,0 +1,87 @@
package ru.trader.graph;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
public class TopListTest extends Assert {
private static final Function<Integer, Integer> getGroup = (o1) -> Math.floorDiv(o1, 10);
private static final Comparator<Integer> groupcomp = (o1, o2) -> {
int cmp = Integer.compare(getGroup.apply(o1), getGroup.apply(o2));
if (cmp !=0 ) return cmp;
return o1.compareTo(o2);
};
private void add(List<Integer> list, Integer entry){
TopList.addToGroupTop(list, entry, 10, groupcomp, getGroup, 1);
}
@Test
public void testAddToGroup() throws Exception {
ArrayList<Integer> top = new ArrayList<>(10);
add(top, 5);
add(top, 15);
add(top, 22);
add(top, 34);
add(top, 36);
add(top, 21);
add(top, 7);
add(top, 6);
add(top, 3);
assertEquals(4, top.size());
assertEquals(3, top.get(0).intValue());
assertEquals(15, top.get(1).intValue());
assertEquals(21, top.get(2).intValue());
assertEquals(34, top.get(3).intValue());
}
@Test
public void testAddToGroup2() throws Exception {
ArrayList<Integer> top = new ArrayList<>(10);
add(top, 36);
add(top, 15);
add(top, 22);
add(top, 6);
add(top, 34);
add(top, 5);
add(top, 21);
add(top, 7);
add(top, 3);
assertEquals(4, top.size());
assertEquals(3, top.get(0).intValue());
assertEquals(15, top.get(1).intValue());
assertEquals(21, top.get(2).intValue());
assertEquals(34, top.get(3).intValue());
}
@Test
public void testAddToGroup3() throws Exception {
ArrayList<Integer> top = new ArrayList<>(10);
add(top, 21);
add(top, 34);
add(top, 36);
add(top, 3);
add(top, 15);
add(top, 22);
add(top, 6);
add(top, 34);
add(top, 5);
add(top, 7);
assertEquals(4, top.size());
assertEquals(3, top.get(0).intValue());
assertEquals(15, top.get(1).intValue());
assertEquals(21, top.get(2).intValue());
assertEquals(34, top.get(3).intValue());
}
}