Archived
0

use new route searcher in market analyzer

This commit is contained in:
iMoHax
2015-07-16 13:22:23 +03:00
parent 100e6794f9
commit e654d87e3c
7 changed files with 334 additions and 422 deletions

View File

@@ -7,29 +7,32 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.TestUtil;
import ru.trader.graph.Path;
import ru.trader.analysis.FilteredMarket;
import ru.trader.analysis.graph.Edge;
import ru.trader.analysis.graph.PPath;
import ru.trader.store.simple.*;
import java.util.Collection;
import java.util.List;
public class MarketAnalyzerTest extends Assert {
private final static Logger LOG = LoggerFactory.getLogger(MarketAnalyzerTest.class);
private static Market market = new SimpleMarket();
private static Place v1;
private static Place v2;
private static Place v3;
private static Place v4;
private static Place v5;
private static Place v6;
private static Place v7;
private static Place v8;
private static Place v9;
private static Place v10;
private static Place v11;
private static Item ITEM1 = new SimpleItem("ITEM1");
private static Item ITEM2 = new SimpleItem("ITEM2");
private static Item ITEM3 = new SimpleItem("ITEM3");
private FilteredMarket market;
private Place v1;
private Place v2;
private Place v3;
private Place v4;
private Place v5;
private Place v6;
private Place v7;
private Place v8;
private Place v9;
private Place v10;
private Place v11;
private Item ITEM1 = new SimpleItem("ITEM1");
private Item ITEM2 = new SimpleItem("ITEM2");
private Item ITEM3 = new SimpleItem("ITEM3");
private void add(Place place, Offer offer){
if (place.isEmpty()){
@@ -41,6 +44,8 @@ public class MarketAnalyzerTest extends Assert {
@Before
public void setUp() throws Exception {
Market market = new SimpleMarket();
v1 = new SimplePlace("v1_x0y0z0",0,0,0);
v2 = new SimplePlace("v2_x1y0z0",1,0,0);
v3 = new SimplePlace("v3_x0y1z0",0,1,0);
@@ -80,29 +85,34 @@ public class MarketAnalyzerTest extends Assert {
add(v10, new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 140, 1));
add(v11, new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 500, 1));
MarketFilter filter = new MarketFilter();
this.market = new FilteredMarket(market, filter);
}
@Test
public void testPaths() throws Exception {
LOG.info("Start paths test");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(5);analyzer.setMaxDistance(1);analyzer.setTank(1);
// maxDist - 1, fulltank - 1
Ship ship = new Ship();
ship.setMass(340);ship.setTank(0.6);
Profile profile = new Profile(ship);
profile.setJumps(5);
MarketAnalyzer analyzer = new MarketAnalyzer(market, profile);
Collection<Path<Place>> paths = analyzer.getPaths(v1, v2);
TestUtil.assertCollectionEquals(paths, Path.toPath(v1, v2));
Collection<List<Edge<Place>>> paths = analyzer.getPaths(v1, v2);
TestUtil.assertPaths(paths, PPath.of(v1, v2));
paths = analyzer.getPaths(v1, v3);
TestUtil.assertCollectionEquals(paths, Path.toPath(v1, v3));
TestUtil.assertPaths(paths, PPath.of(v1, v3));
paths = analyzer.getPaths(v1, v4);
TestUtil.assertCollectionEquals(paths, Path.toPath(v1, v4));
TestUtil.assertPaths(paths, PPath.of(v1, v4));
paths = analyzer.getPaths(v1, v5);
assertTrue(paths.isEmpty());
paths = analyzer.getPaths(v2, v5);
TestUtil.assertCollectionEquals(paths, Path.toPath(v2, v5));
TestUtil.assertPaths(paths, PPath.of(v2, v5));
paths = analyzer.getPaths(v4, v3);
assertTrue(paths.isEmpty());
@@ -111,80 +121,95 @@ public class MarketAnalyzerTest extends Assert {
@Test
public void testPathsWithStock() throws Exception {
LOG.info("Start paths with stock test");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(5);analyzer.setMaxDistance(1);analyzer.setTank(2);
// jumpRange - 1, fulltank - 2
Ship ship = new Ship();
ship.setMass(340);ship.setTank(1.17);
Profile profile = new Profile(ship);
profile.setJumps(5);
MarketAnalyzer analyzer = new MarketAnalyzer(market, profile);
Collection<Path<Place>> paths = analyzer.getPaths(v1, v2);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v2));
Collection<List<Edge<Place>>> paths = analyzer.getPaths(v1, v2);
TestUtil.assertPaths(paths, PPath.of(v1, v2));
paths = analyzer.getPaths(v1, v3);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v3));
TestUtil.assertPaths(paths, PPath.of(v1, v3));
paths = analyzer.getPaths(v1, v4);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v4));
TestUtil.assertPaths(paths, PPath.of(v1, v4));
paths = analyzer.getPaths(v1, v5);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v2, v5), Path.toPath(v1, v3, v5));
TestUtil.assertPaths(paths, PPath.of(v1, v2, v5), PPath.of(v1, v3, v5));
paths = analyzer.getPaths(v2, v5);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v2, v5));
TestUtil.assertPaths(paths, PPath.of(v2, v5));
paths = analyzer.getPaths(v4, v3);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v4, v1, v3));
TestUtil.assertPaths(paths, PPath.of(v4, v1, v3));
}
@Test
public void testPathsWithStockAndRefill() throws Exception {
LOG.info("Start paths with stock and refill test");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(2);analyzer.setMaxDistance(10);analyzer.setTank(15);
// jumpRange - 10, fulltank - 15
Ship ship = new Ship();
ship.setMass(30);ship.setTank(0.7);
Profile profile = new Profile(ship);
profile.setJumps(2);
MarketAnalyzer analyzer = new MarketAnalyzer(market, profile);
Collection<Path<Place>> paths = analyzer.getPaths(v10, v6);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v6), Path.toPath(v10, v11, v6),
Path.toPath(v10, v8, v6));
Collection<List<Edge<Place>>> paths = analyzer.getPaths(v10, v6);
TestUtil.assertPaths(paths, PPath.of(v10, v6), PPath.of(v10, v11, v6),
PPath.of(v10, v8, v6));
paths = analyzer.getPaths(v1, v3);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v1, v3), Path.toPath(v1, v2, v3),
Path.toPath(v1, v4, v3), Path.toPath(v1, v5, v3)
TestUtil.assertPaths(paths, PPath.of(v1, v3), PPath.of(v1, v2, v3),
PPath.of(v1, v4, v3), PPath.of(v1, v5, v3)
);
}
@Test
public void testPathsWithStockAndRefill2() throws Exception {
LOG.info("Start paths with stock and refill test 2");
MarketAnalyzer analyzer = new MarketAnalyzer(market);
analyzer.setJumps(3);analyzer.setMaxDistance(10);analyzer.setTank(15);
// jumpRange - 10, fulltank - 15
Ship ship = new Ship();
ship.setMass(30);ship.setTank(0.7);
Profile profile = new Profile(ship);
profile.setJumps(3);
MarketAnalyzer analyzer = new MarketAnalyzer(market, profile);
Collection<Path<Place>> paths = analyzer.getPaths(v10, v6);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v6), Path.toPath(v10, v11, v6), Path.toPath(v10, v11, v10, v6),
Path.toPath(v10, v8, v6), Path.toPath(v10, v8, v10, v6), Path.toPath(v10, v8, v11, v6));
Collection<List<Edge<Place>>> paths = analyzer.getPaths(v10, v6);
TestUtil.assertPaths(paths, PPath.of(v10, v6), PPath.of(v10, v8, v6), PPath.of(v10, v11, v6),
PPath.of(v10, v8, v11, v6), PPath.of(v10, v8, v10, v6), PPath.of(v10, v11, v10, v6),
PPath.of(v10, v6, v7, v6), PPath.of(v10, v6, v8, v6), PPath.of(v10, v6, v11, v6),
PPath.of(v10, v6, v10, v6));
paths = analyzer.getPaths(v10, v7);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v6, v7), Path.toPath(v10, v11, v6, v7),
Path.toPath(v10, v8, v6, v7)
TestUtil.assertPaths(paths, PPath.of(v10, v6, v7), PPath.of(v10, v11, v6, v7),
PPath.of(v10, v8, v6, v7)
);
paths = analyzer.getPaths(v10, v8);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v8), Path.toPath(v10, v11, v8),
Path.toPath(v10, v11, v6, v8), Path.toPath(v10, v6, v8), Path.toPath(v10, v6, v11, v8),
Path.toPath(v10, v11, v10, v8), Path.toPath(v10, v6, v10, v8));
TestUtil.assertPaths(paths, PPath.of(v10, v8), PPath.of(v10, v11, v8), PPath.of(v10, v6, v8),
PPath.of(v10, v8, v11, v8), PPath.of(v10, v8, v10, v8), PPath.of(v10, v8, v6, v8),
PPath.of(v10, v11, v10, v8), PPath.of(v10, v11, v6, v8), PPath.of(v10, v6, v11, v8),
PPath.of(v10, v6, v10, v8));
paths = analyzer.getPaths(v10, v9);
assertTrue(paths.isEmpty());
paths = analyzer.getPaths(v10, v10);
TestUtil.assertCollectionContainAll(paths, Path.toPath(v10, v11, v10), Path.toPath(v10, v6, v10),
Path.toPath(v10, v11, v6, v10), Path.toPath(v10, v6, v11, v10),
Path.toPath(v10, v8, v10), Path.toPath(v10, v8, v11, v10),
Path.toPath(v10, v8, v6, v10), Path.toPath(v10, v8, v6, v10));
TestUtil.assertPaths(paths, PPath.of(v10, v11, v10), PPath.of(v10, v6, v10),
PPath.of(v10, v11, v6, v10), PPath.of(v10, v6, v11, v10),
PPath.of(v10, v8, v10), PPath.of(v10, v8, v11, v10),
PPath.of(v10, v8, v6, v10), PPath.of(v10, v8, v6, v10));
}
@After
public void tearDown() throws Exception {
market = new SimpleMarket();
market = null;
}
}

View File

@@ -4,7 +4,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import ru.trader.TestUtil;
import ru.trader.graph.PathRoute;
import ru.trader.analysis.*;
import ru.trader.store.simple.Store;
import java.io.InputStream;
import java.util.Collection;
@@ -16,25 +16,40 @@ public class MarketAnalyzerTest2 extends Assert {
public void testRoutes() throws Exception {
InputStream is = getClass().getResourceAsStream("/world.xml");
Market market = Store.loadFromFile(is);
MarketAnalyzer analyzer = new MarketAnalyzer(market);
MarketFilter filter = new MarketFilter();
FilteredMarket fWorld = new FilteredMarket(market, filter);
// 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().get().iterator().next();
Vendor morgor = market.get().stream().filter((v)->v.getName().equals("Morgor")).findFirst().get().get().iterator().next();
Vendor lhs3006 = market.get().stream().filter((v)->v.getName().equals("LHS 3006")).findFirst().get().get().iterator().next();
Vendor lhs3262 = market.get().stream().filter((v)->v.getName().equals("LHS 3262")).findFirst().get().get().iterator().next();
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);
Optional<PathRoute> path = paths.stream().filter((p)->p.equals(expect)).findFirst();
Ship ship = new Ship();
ship.setCargo(440); ship.setTank(15);
ship.setEngine(5, 'A'); ship.setMass(466);
Profile profile = new Profile(ship);
profile.setBalance(6000000); profile.setJumps(6);
profile.setRoutesCount(100);
MarketAnalyzer analyzer = new MarketAnalyzer(fWorld, profile);
Vendor ithaca = market.get("Ithaca").get().iterator().next();
Vendor morgor = market.get("Morgor").asTransit();
Vendor lhs3006 = market.get("LHS 3006").asTransit();
Vendor lhs3262 = market.get("LHS 3262").get().iterator().next();
Collection<Route> paths = analyzer.getRoutes(ithaca, ithaca);
Route expect = new Route(new RouteEntry(ithaca, false, 3.3789702637348586d, 0));
expect.add(new RouteEntry(morgor, false, 4.137765020523591d, 0));
expect.add(new RouteEntry(lhs3006, false, 4.0674474942172765d, 0));
expect.add(new RouteEntry(lhs3262, true, 4.149937831634785d, 0));
expect.add(new RouteEntry(lhs3006, false, 4.1292528548103d, 0));
expect.add(new RouteEntry(morgor, false, 3.3050364899848566, 0));
expect.add(new RouteEntry(ithaca, false, 0, 0));
RouteFiller filler = new RouteFiller(new Scorer(fWorld, profile));
filler.fill(expect);
Optional<Route> path = paths.stream().findFirst();
assertTrue(path.isPresent());
PathRoute actual = path.get().getRoot();
TestUtil.assertCollectionContain(paths, expect);
Route actual = path.get();
assertEquals(expect, actual);
assertEquals(981200, actual.getProfit(), 0.00001);
assertEquals(72.42, actual.getDistance(), 0.01);
assertEquals(2, actual.getLandsCount());
assertEquals(490600, actual.getAvgProfit() , 0.00001);
assertEquals(2, actual.getLands());
}
//test best avg profit
@@ -42,27 +57,51 @@ public class MarketAnalyzerTest2 extends Assert {
public void testRoutes2() throws Exception {
InputStream is = getClass().getResourceAsStream("/test2.xml");
Market market = Store.loadFromFile(is);
MarketAnalyzer analyzer = new MarketAnalyzer(market);
MarketFilter filter = new MarketFilter();
FilteredMarket fWorld = new FilteredMarket(market, filter);
// Balance: 6000000, cargo: 104 tank: 150, distance: 12.6, jumps: 4
// LHS 21 (Resonatic Separator to Sui Xing) -> Bonde -> Sui Xing (Palladium to LHS 21) -> Bonde -> LHS 21
// Profit: 981200, avg: 490600, distance: 67.5, lands: 2
Place lhs21 = market.get().stream().filter((v)->v.getName().equals("LHS 21")).findFirst().get();
Place suiXing = market.get().stream().filter((v)->v.getName().equals("Sui Xing")).findFirst().get();
analyzer.setCargo(104);analyzer.setTank(150);analyzer.setMaxDistance(12.6);analyzer.setJumps(4);analyzer.setPathsCount(100);
Collection<PathRoute> paths = analyzer.getPaths(lhs21, lhs21, 6000000);
Optional<PathRoute> path = paths.stream().findFirst();
// LHS 21 -> Bonde -> LHS 21
// Profit: 114816, distance: 8.16, lands: 2
Ship ship = new Ship();
ship.setCargo(104); ship.setTank(150);
ship.setEngine(5, 'A'); ship.setMass(865);
Profile profile = new Profile(ship);
profile.setBalance(6000000); profile.setJumps(4);
profile.setRoutesCount(100);
MarketAnalyzer analyzer = new MarketAnalyzer(fWorld, profile);
Place lhs21 = market.get("LHS 21");
Place bonde = market.get("Bonde");
Place suiXing = market.get("Sui Xing");
Collection<Route> paths = analyzer.getRoutes(lhs21, lhs21);
Optional<Route> path = paths.stream().findFirst();
assertTrue(path.isPresent());
PathRoute actual = path.get().getRoot();
Route actual = path.get();
assertEquals(114816, actual.getProfit(), 0.00001);
assertEquals(8.16, actual.getDistance(), 0.01);
assertEquals(2, actual.getLands());
Place aPlace = actual.get(0).getVendor().getPlace();
assertEquals(lhs21, aPlace);
aPlace = actual.get(1).getVendor().getPlace();
assertEquals(bonde, aPlace);
// If distance to station has small mult
// LHS 21 (Resonatic Separator to Sui Xing) -> Bonde -> Sui Xing (Palladium to LHS 21) -> Bonde -> LHS 21
// Profit: 199056, distance: 28.72, lands: 2
profile.setDistanceMult(0.1);
paths = analyzer.getRoutes(lhs21, lhs21);
path = paths.stream().findFirst();
assertTrue(path.isPresent());
actual = path.get();
assertEquals(199056, actual.getProfit(), 0.00001);
assertEquals(28.72, actual.getDistance(), 0.01);
assertEquals(2, actual.getLandsCount());
assertEquals(99528, actual.getAvgProfit() , 0.00001);
assertEquals(2, actual.getLands());
Place aPlace = actual.get().getPlace();
aPlace = actual.get(0).getVendor().getPlace();
assertEquals(lhs21, aPlace);
actual = actual.getNext().getNext();
aPlace = actual.get().getPlace();
aPlace = actual.get(2).getVendor().getPlace();
assertEquals(suiXing, aPlace);
}