diff --git a/core/src/main/java/ru/trader/analysis/graph/CCrawler.java b/core/src/main/java/ru/trader/analysis/graph/CCrawler.java index f8b5a97..4fa455f 100644 --- a/core/src/main/java/ru/trader/analysis/graph/CCrawler.java +++ b/core/src/main/java/ru/trader/analysis/graph/CCrawler.java @@ -13,22 +13,28 @@ import java.util.stream.Collectors; public class CCrawler> extends Crawler { private final static Logger LOG = LoggerFactory.getLogger(CCrawler.class); + private double startFuel; public CCrawler(ConnectibleGraph graph, Consumer>> onFoundFunc) { super(graph, onFoundFunc); + startFuel = getShip().getTank(); } protected Ship getShip(){ - return ((ConnectibleGraph)graph).getProfile().getShip(); + return ((ConnectibleGraph)graph).getShip(); } protected Profile getProfile(){ return ((ConnectibleGraph)graph).getProfile(); } + public void setStartFuel(double startFuel) { + this.startFuel = startFuel; + } + @Override protected CCostTraversalEntry start(Vertex vertex) { - double fuel = getShip().getTank(); + double fuel = startFuel; return new CCostTraversalEntry(vertex, fuel); } diff --git a/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java b/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java index 4c94adb..eebe293 100644 --- a/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java +++ b/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.trader.analysis.AnalysisCallBack; import ru.trader.core.Profile; +import ru.trader.core.Ship; import ru.trader.graph.Connectable; import java.util.Collection; @@ -27,6 +28,10 @@ public class ConnectibleGraph> extends AbstractGraph public Profile getProfile() { return profile; } + + protected Ship getShip(){ + return profile.getShip(); + } @Override protected GraphBuilder createGraphBuilder(Vertex vertex, Collection set, int deep, double limit) { @@ -34,7 +39,7 @@ public class ConnectibleGraph> extends AbstractGraph } public void build(T start, Collection set){ - super.build(start, set, profile.getJumps(), profile.getShip().getTank()); + super.build(start, set, profile.getJumps(), getShip().getTank()); } private class DistanceFilter implements Predicate { @@ -48,7 +53,7 @@ public class ConnectibleGraph> extends AbstractGraph @Override public boolean test(Double distance) { - return distance <= profile.getShip().getJumpRange(limit) || (profile.withRefill() && distance <= profile.getShip().getJumpRange() && source.canRefill()); + return distance <= getShip().getJumpRange(limit) || (profile.withRefill() && distance <= getShip().getJumpRange() && source.canRefill()); } } @@ -69,13 +74,13 @@ public class ConnectibleGraph> extends AbstractGraph LOG.trace("Vertex {} is far away, {}", entry, distance); return -1; } - fuelCost = profile.getShip().getFuelCost(limit, distance); - double nextLimit = profile.withRefill() ? limit - fuelCost : profile.getShip().getTank(); + fuelCost = getShip().getFuelCost(limit, distance); + double nextLimit = profile.withRefill() ? limit - fuelCost : getShip().getTank(); if (nextLimit < 0) { LOG.trace("Refill"); refill = true; - fuelCost = profile.getShip().getFuelCost(distance); - nextLimit = profile.getShip().getTank() - fuelCost; + fuelCost = getShip().getFuelCost(distance); + nextLimit = getShip().getTank() - fuelCost; } else { refill = false; } diff --git a/core/src/main/java/ru/trader/analysis/graph/Crawler.java b/core/src/main/java/ru/trader/analysis/graph/Crawler.java index 8996483..4b1a71d 100644 --- a/core/src/main/java/ru/trader/analysis/graph/Crawler.java +++ b/core/src/main/java/ru/trader/analysis/graph/Crawler.java @@ -42,14 +42,29 @@ public class Crawler { } public void findFast(T target, int count){ + findFast(graph.getRoot(), target, count); + } + + public void findFast(T source, T target){ + findFast(source, target, 1); + } + + public void findFast(T source, T target, int count){ + Optional> s = graph.getVertex(source); + if (s.isPresent()){ + findFast(s.get(), target, count); + } + } + + private void findFast(Vertex s, T target, int count){ Optional> t = graph.getVertex(target); int found = 0; if (t.isPresent()) { if (count > 1) { - int maxDeep = maxSize - (graph.getRoot().isEntry(target) ? graph.getMinJumps() * 2 : graph.getMinJumps()); - found = bfs(start(graph.getRoot()), target, maxDeep, count); + int maxDeep = maxSize - (s.isEntry(target) ? graph.getMinJumps() * 2 : graph.getMinJumps()); + found = bfs(start(s), target, maxDeep, count); } else { - found = dfs(start(graph.getRoot()), target, t.get().getLevel() + 1, count); + found = dfs(start(s), target, t.get().getLevel() + 1, count); } } LOG.debug("Found {} paths", found); @@ -60,15 +75,30 @@ public class Crawler { } public void findMin(T target, int count){ + findMin(graph.getRoot(), target, count); + } + + public void findMin(T source, T target){ + findMin(source, target, 1); + } + + public void findMin(T source, T target, int count){ + Optional> s = graph.getVertex(source); + if (s.isPresent()){ + findMin(s.get(), target, count); + } + } + + public void findMin(Vertex s, T target, int count){ Optional> t = graph.getVertex(target); int found = 0; if (t.isPresent()) { - int maxDeep = graph.getRoot().isEntry(target) ? maxSize - graph.getMinJumps() * 2 : graph.getMinLevel(); + int maxDeep = s.isEntry(target) ? maxSize - graph.getMinJumps() * 2 : graph.getMinLevel(); if (maxDeep < 0) maxDeep = 0; if (maxSize - maxDeep <= SPLIT_SIZE){ - found = ucs(start(graph.getRoot()), target, maxDeep, count); + found = ucs(start(s), target, maxDeep, count); } else { - found = ucs2(start(graph.getRoot()), target, maxDeep, count); + found = ucs2(start(s), target, maxDeep, count); } } LOG.debug("Found {} paths", found); diff --git a/core/src/main/java/ru/trader/core/Profile.java b/core/src/main/java/ru/trader/core/Profile.java index c23dcf3..e317ea6 100644 --- a/core/src/main/java/ru/trader/core/Profile.java +++ b/core/src/main/java/ru/trader/core/Profile.java @@ -4,6 +4,7 @@ public class Profile { private double balance; private int jumps; + private int lands; private Ship ship; private boolean refill; private int routesCount; @@ -18,6 +19,7 @@ public class Profile { this.ship = ship; refill = true; jumps = 6; + lands = 4; scoreOrdersCount = 5; distanceMult = 0.8; landMult = 0.95; @@ -41,6 +43,14 @@ public class Profile { this.jumps = jumps; } + public int getLands() { + return lands; + } + + public void setLands(int lands) { + this.lands = lands; + } + public Ship getShip() { return ship; }