fix limit on refill in findPaths
This commit is contained in:
@@ -132,7 +132,7 @@ public class Graph<T extends Connectable<T>> {
|
||||
Path<T> path = head.connectTo(next.getTarget(), limit < next.getLength());
|
||||
double nextLimit = withRefill ? limit - next.getLength(): stock;
|
||||
// refill
|
||||
if (nextLimit < 0 ) nextLimit = maxDistance - next.getLength();
|
||||
if (nextLimit < 0 ) nextLimit = stock - next.getLength();
|
||||
if (findPaths(paths, max, path, target, deep - 1, nextLimit)) return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,7 +317,7 @@ public class PathRoute extends Path<Vendor> {
|
||||
if (o == null){
|
||||
o = p.getBest();
|
||||
if (o!= null){
|
||||
LOG.trace("{} is lands for by by order {}", p, o);
|
||||
LOG.trace("{} is lands for buy by order {}", p, o);
|
||||
res++;
|
||||
}
|
||||
} else {
|
||||
@@ -343,4 +343,13 @@ public class PathRoute extends Path<Vendor> {
|
||||
return p;
|
||||
}
|
||||
|
||||
public static PathRoute toPathRoute(Vendor... items){
|
||||
Vendor t = items[0];
|
||||
PathRoute path = new PathRoute(new Vertex<>(t));
|
||||
for (int i = 1; i < items.length; i++) {
|
||||
t = items[i];
|
||||
path = new PathRoute(path, new Vertex<>(t), false);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ public class MarketAnalyzerTest extends Assert {
|
||||
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), Path.toPath(v10, v8, v6, v10));
|
||||
}
|
||||
|
||||
|
||||
|
||||
42
core/src/test/java/ru/trader/core/MarketAnalyzerTest2.java
Normal file
42
core/src/test/java/ru/trader/core/MarketAnalyzerTest2.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import ru.trader.TestUtil;
|
||||
import ru.trader.graph.PathRoute;
|
||||
import ru.trader.store.Store;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
|
||||
public class MarketAnalyzerTest2 extends Assert {
|
||||
private static MarketAnalyzer analyzer;
|
||||
private static Market market;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
InputStream is = getClass().getResourceAsStream("/world.xml");
|
||||
market = Store.loadFromFile(is);
|
||||
analyzer = new MarketAnalyzer(market);
|
||||
}
|
||||
|
||||
@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 morgor = market.get().stream().filter((v)->v.getName().equals("Morgor")).findFirst().get();
|
||||
Vendor lhs3006 = market.get().stream().filter((v)->v.getName().equals("LHS 3006")).findFirst().get();
|
||||
Vendor lhs3262 = market.get().stream().filter((v)->v.getName().equals("LHS 3262")).findFirst().get();
|
||||
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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
1
core/src/test/resources/world.xml
Normal file
1
core/src/test/resources/world.xml
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user