From a549dce54aaaf5d5864dc49b464df299dbe475c1 Mon Sep 17 00:00:00 2001 From: iMoHax Date: Thu, 21 May 2015 14:54:55 +0300 Subject: [PATCH] implement route searcher --- .../ru/trader/analysis/FilteredMarket.java | 9 + .../main/java/ru/trader/analysis/Route.java | 32 +- .../java/ru/trader/analysis/RouteEntry.java | 12 +- .../ru/trader/analysis/RouteSearcher.java | 104 ++++ .../main/java/ru/trader/analysis/Scorer.java | 6 +- .../java/ru/trader/analysis/VendorsGraph.java | 146 ++++++ .../ru/trader/analysis/graph/CCrawler.java | 47 +- .../ru/trader/analysis/graph/Crawler.java | 118 +---- .../java/ru/trader/analysis/graph/Path.java | 24 + .../ru/trader/analysis/graph/PathEntry.java | 19 + .../java/ru/trader/analysis/graph/Vertex.java | 4 +- core/src/main/java/ru/trader/core/Place.java | 11 + .../src/main/java/ru/trader/core/Profile.java | 9 + core/src/main/java/ru/trader/core/Ship.java | 4 +- .../java/ru/trader/core/TransitVendor.java | 113 ++++ .../java/ru/trader/core/VendorsIterator.java | 113 ---- .../ru/trader/analysis/RouteFillerTest.java | 46 +- .../RouteSearcherTest.java | 56 +- .../java/ru/trader/analysis/RouteTest.java | 12 +- .../ru/trader/analysis/VendorsGraphTest.java | 78 +++ .../ru/trader/analysis/graph/CrawlerTest.java | 41 +- .../analysis/graph/SimpleCollector.java | 40 ++ .../test/java/ru/trader/graph/GraphTest.java | 321 ------------ .../java/ru/trader/graph/PathRouteTest.java | 488 ------------------ .../java/ru/trader/graph/RouteGraphTest.java | 113 ---- .../java/ru/trader/graph/TopListTest.java | 87 ---- .../test/java/ru/trader/graph/VertexTest.java | 30 -- core/src/test/resources/log4j.properties | 2 +- 28 files changed, 693 insertions(+), 1392 deletions(-) create mode 100644 core/src/main/java/ru/trader/analysis/RouteSearcher.java create mode 100644 core/src/main/java/ru/trader/analysis/VendorsGraph.java create mode 100644 core/src/main/java/ru/trader/analysis/graph/Path.java create mode 100644 core/src/main/java/ru/trader/analysis/graph/PathEntry.java create mode 100644 core/src/main/java/ru/trader/core/TransitVendor.java rename core/src/test/java/ru/trader/{graph => analysis}/RouteSearcherTest.java (63%) create mode 100644 core/src/test/java/ru/trader/analysis/VendorsGraphTest.java create mode 100644 core/src/test/java/ru/trader/analysis/graph/SimpleCollector.java delete mode 100644 core/src/test/java/ru/trader/graph/GraphTest.java delete mode 100644 core/src/test/java/ru/trader/graph/PathRouteTest.java delete mode 100644 core/src/test/java/ru/trader/graph/RouteGraphTest.java delete mode 100644 core/src/test/java/ru/trader/graph/TopListTest.java delete mode 100644 core/src/test/java/ru/trader/graph/VertexTest.java diff --git a/core/src/main/java/ru/trader/analysis/FilteredMarket.java b/core/src/main/java/ru/trader/analysis/FilteredMarket.java index 0803b9d..bf79861 100644 --- a/core/src/main/java/ru/trader/analysis/FilteredMarket.java +++ b/core/src/main/java/ru/trader/analysis/FilteredMarket.java @@ -25,6 +25,15 @@ public class FilteredMarket { .filter(v -> !filter.isFiltered(v)); } + public Stream getMarkets(boolean withTransit){ + return get().flatMap(p -> p.get(true).stream()) + .filter(v -> { + if (withTransit && v instanceof TransitVendor) return true; + if (!v.has(SERVICE_TYPE.MARKET) && !v.has(SERVICE_TYPE.BLACK_MARKET)) return false; + return !filter.isFiltered(v); + }); + } + public Collection getItems(){ return market.getItems(); } diff --git a/core/src/main/java/ru/trader/analysis/Route.java b/core/src/main/java/ru/trader/analysis/Route.java index 306f688..cf8dc6c 100644 --- a/core/src/main/java/ru/trader/analysis/Route.java +++ b/core/src/main/java/ru/trader/analysis/Route.java @@ -13,6 +13,8 @@ public class Route { private double profit = 0; private double balance = 0; private double distance = 0; + private double score = 0; + private double fuel = 0; private int lands = 0; public Route(RouteEntry root) { @@ -20,14 +22,9 @@ public class Route { entries.add(root); } - public Route(List edges) { - //TODO: move to RouteCrawler - entries = new ArrayList<>(edges.size()+1); - for (int i = 0; i < edges.size(); i++) { - VendorsGraph.VendorsEdge edge = edges.get(i); - if (i==0) entries.add(new RouteEntry(edge.getSource().getEntry(), false, 0)); - entries.add(new RouteEntry(edge.getTarget().getEntry(), edge.isRefill(), edge.getFuel())); - } + public Route(List entries) { + this.entries = new ArrayList<>(entries); + updateStats(); } public List getEntries() { @@ -100,7 +97,7 @@ public class Route { } void updateStats(){ - LOG.trace("Update stats, old: profit={}, distance={}, lands={}", profit, distance, lands); + LOG.trace("Update stats, old: profit={}, distance={}, lands={}, fuel={}, score={}", profit, distance, lands, fuel, score); profit = 0; distance = 0; lands = 0; if (entries.isEmpty()) return; RouteEntry entry = entries.get(0); @@ -108,13 +105,26 @@ public class Route { RouteEntry next = entries.get(i); distance += entry.getVendor().getDistance(next.getVendor()); profit += entry.getProfit(); + score += entry.getScore(); + fuel += entry.getFuel(); if (entry.isLand()){ lands++; } entry = next; } - LOG.trace("new stats profit={}, distance={}, lands={}", profit, distance, lands); + LOG.trace("new stats profit={}, distance={}, lands={}, fuel={}, score={}", profit, distance, lands, fuel, score); } - + @Override + public String toString() { + return "Route{" + + "entries=" + entries + + ", profit=" + profit + + ", balance=" + balance + + ", distance=" + distance + + ", score=" + score + + ", fuel=" + fuel + + ", lands=" + lands + + '}'; + } } diff --git a/core/src/main/java/ru/trader/analysis/RouteEntry.java b/core/src/main/java/ru/trader/analysis/RouteEntry.java index 2abad47..08ce73d 100644 --- a/core/src/main/java/ru/trader/analysis/RouteEntry.java +++ b/core/src/main/java/ru/trader/analysis/RouteEntry.java @@ -10,16 +10,18 @@ import java.util.List; public class RouteEntry { private final Vendor vendor; private final double fuel; + private final double score; private final List orders; private boolean land; private boolean refill; - public RouteEntry(Vendor vendor, boolean refill, double fuel) { + public RouteEntry(Vendor vendor, boolean refill, double fuel, double score) { orders = new ArrayList<>(); this.vendor = vendor; this.refill = refill; this.fuel = fuel; + this.score = score; } public Vendor getVendor() { @@ -42,6 +44,10 @@ public class RouteEntry { return fuel; } + public double getScore() { + return score; + } + public void add(Order order){ orders.add(order); } @@ -74,4 +80,8 @@ public class RouteEntry { return !isLand(); } + @Override + public String toString() { + return vendor + (isRefill() ? " (R)":""); + } } diff --git a/core/src/main/java/ru/trader/analysis/RouteSearcher.java b/core/src/main/java/ru/trader/analysis/RouteSearcher.java new file mode 100644 index 0000000..a85a027 --- /dev/null +++ b/core/src/main/java/ru/trader/analysis/RouteSearcher.java @@ -0,0 +1,104 @@ +package ru.trader.analysis; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.trader.analysis.graph.Crawler; +import ru.trader.analysis.graph.Edge; +import ru.trader.core.Order; +import ru.trader.core.TransitVendor; +import ru.trader.core.Vendor; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class RouteSearcher { + private final static Logger LOG = LoggerFactory.getLogger(RouteSearcher.class); + private final VendorsGraph vGraph; + + public RouteSearcher(Scorer scorer) { + vGraph = new VendorsGraph(scorer); + } + + public List getRoutes(Vendor from, Vendor to, Collection vendors){ + return search(from, to, vendors); + } + + public List getRoutes(Vendor from, Collection vendors){ + return search(from, null, vendors); + } + + private List search(Vendor source, Vendor target, Collection vendors){ + LOG.trace("Start search route to {} from {}", source, target); + vGraph.build(source, vendors); + + RouteCollector collector = new RouteCollector(); + Crawler crawler = vGraph.crawler(collector::add); + if (target == null){ + int count = vGraph.getProfile().getRoutesCount() / vendors.size(); + for (Vendor vendor : vendors) { + crawler.findMin(vendor, count); + } + } else { + crawler.findMin(target, vGraph.getProfile().getRoutesCount()); + } + return collector.get(); + + } + + private class RouteCollector { + private List routes = new ArrayList<>(); + + public void add(List> edges){ + Route route = toRoute(edges); + if (route.getProfit() > 0) { + routes.add(route); + } + } + + public List get() { + return routes; + } + + private Route toRoute(List> edges){ + List entries = new ArrayList<>(edges.size()+1); + Vendor buyer = null; + for (int i = 0; i < edges.size(); i++) { + VendorsGraph.VendorsEdge edge = (VendorsGraph.VendorsEdge) edges.get(i); + if (i==0){ + RouteEntry entry = new RouteEntry(edge.getSource().getEntry(), false, 0, 0); + List orders = edge.getOrders(); + if (!orders.isEmpty()){ + buyer = orders.get(0).getBuyer(); + } + entry.addAll(orders); + entries.add(entry); + } + Vendor vendor = edge.getTarget().getEntry(); + RouteEntry entry = new RouteEntry(vendor, edge.isRefill(), edge.getFuel(), edge.getWeight()); + if (buyer != null && vendor.equals(buyer)){ + entry.setLand(true); + } + List orders = edge.getOrders(); + if (!orders.isEmpty()){ + buyer = orders.get(0).getBuyer(); + if (vendor instanceof TransitVendor){ + Vendor seller = orders.get(0).getSell().getVendor(); + for (int j = i-1; j <= 0; j--) { + RouteEntry sEntry = entries.get(j); + if (sEntry.is(seller)){ + sEntry.addAll(orders); + break; + } + } + } else { + entry.addAll(orders); + } + } + entries.add(entry); + } + return new Route(entries); + } + + } +} diff --git a/core/src/main/java/ru/trader/analysis/Scorer.java b/core/src/main/java/ru/trader/analysis/Scorer.java index 7fddca3..c05d6bd 100644 --- a/core/src/main/java/ru/trader/analysis/Scorer.java +++ b/core/src/main/java/ru/trader/analysis/Scorer.java @@ -28,7 +28,7 @@ public class Scorer { buyOffers = new HashMap<>(100, 0.9f); market.getItems().parallelStream().forEach(this::fillOffers); DoubleSummaryStatistics statProfit = computeProfit(); - avgProfit = statProfit.getAverage()/profile.getShip().getCargo(); + avgProfit = statProfit.getAverage(); avgDistance = computeAvgDistance(); maxScore = getScore(0, statProfit.getMax()*2, 0,0,0); } @@ -77,8 +77,8 @@ public class Scorer { public double getScore(double distance, double profit, int jumps, int lands, double fuel){ LOG.trace("Compute score distance={}, profit={}, jumps={}, lands={}, fuel={}", distance, profit, jumps, lands, fuel); - double score = profit/profile.getShip().getCargo(); - if (avgDistance > 0) { + double score = profit; + if (avgDistance > 0 && profit > 0) { score -= profile.getDistanceMult() * getAvgProfit() * (distance - avgDistance) / avgDistance; } score -= profile.getLandMult() * lands * getAvgProfit(); diff --git a/core/src/main/java/ru/trader/analysis/VendorsGraph.java b/core/src/main/java/ru/trader/analysis/VendorsGraph.java new file mode 100644 index 0000000..eeafb4a --- /dev/null +++ b/core/src/main/java/ru/trader/analysis/VendorsGraph.java @@ -0,0 +1,146 @@ +package ru.trader.analysis; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.trader.analysis.graph.*; +import ru.trader.core.Order; +import ru.trader.core.TransitVendor; +import ru.trader.core.Vendor; + +import java.util.Collection; +import java.util.List; +import java.util.function.Consumer; + + +public class VendorsGraph extends ConnectibleGraph { + private final static Logger LOG = LoggerFactory.getLogger(VendorsGraph.class); + + private final Scorer scorer; + + public VendorsGraph(Scorer scorer) { + super(scorer.getProfile()); + this.scorer = scorer; + } + + public VendorsGraph(Scorer scorer, AnalysisCallBack callback) { + super(scorer.getProfile(), callback); + this.scorer = scorer; + } + + public VendorsCrawler crawler(Consumer>> onFoundFunc){ + return new VendorsCrawler(onFoundFunc); + } + + @Override + protected GraphBuilder createGraphBuilder(Vertex vertex, Collection set, int deep, double limit) { + return new VendorsGraphBuilder(vertex, set, deep, limit); + } + + protected class VendorsGraphBuilder extends ConnectibleGraphBuilder { + protected VendorsGraphBuilder(Vertex vertex, Collection set, int deep, double limit) { + super(vertex, set, deep, limit); + } + + @Override + protected double onConnect(Vendor entry) { + double nextlimit = super.onConnect(entry); + if (entry instanceof TransitVendor && vertex.getEntry().getPlace().equals(entry.getPlace())) nextlimit = -1; + return nextlimit; + } + + @Override + protected ConnectibleEdge createEdge(Vertex target) { + return new VendorsEdge(vertex, target, refill, fuelCost, false); + } + } + + public class VendorsEdge extends ConnectibleEdge { + private List orders; + private boolean isTarget; + + protected VendorsEdge(Vertex source, Vertex target, boolean refill, double fuel, boolean isTarget) { + super(source, target, refill, fuel); + this.isTarget = isTarget; + } + + protected void setOrders(List orders){ + this.orders = orders; + } + + public double getProfit(){ + return getOrders().stream().mapToDouble(Order::getProfit).sum(); + } + + public List getOrders(){ + if (orders == null){ + Vendor seller = source.getEntry(); + Vendor buyer = target.getEntry(); + orders = MarketUtils.getOrders(seller, buyer); + } + return orders; + } + + @Override + protected double computeWeight() { + int jumps = source.getEntry().getPlace().equals(target.getEntry().getPlace())? 0 : 1; + int lands = refill && orders.isEmpty() || isTarget ? 1 : 0; + double score = scorer.getScore(target.getEntry(), getProfit(), jumps, lands, fuel); + return scorer.getMaxScore() - score; + } + } + + public class VendorsCrawler extends CCrawler { + protected VendorsCrawler(Consumer>> onFoundFunc) { + super(VendorsGraph.this, onFoundFunc); + } + + @Override + protected CostTraversalEntry start(Vertex vertex) { + double balance = scorer.getProfile().getBalance(); + return new VendorsTraversalEntry((CCostTraversalEntry) super.start(vertex), balance); + } + + @Override + protected CostTraversalEntry travers(CostTraversalEntry entry, List> head, Edge edge, Vendor target) { + VendorsTraversalEntry ve = (VendorsTraversalEntry)entry; + double balance = ve.balance; + Vendor buyer = edge.getTarget().getEntry(); + List orders = ((VendorsEdge) edge).getOrders(); + if (edge.getSource().getEntry() instanceof TransitVendor && + !(buyer instanceof TransitVendor)){ + LOG.trace("{} is transit, search seller", edge.getSource().getEntry()); + for (int i = head.size() - 1; i >= 0; i--) { + Vendor seller = head.get(i).getSource().getEntry(); + if (!(seller instanceof TransitVendor)){ + orders = MarketUtils.getOrders(seller, buyer); + break; + } + } + } + orders = MarketUtils.getStack(orders, balance, scorer.getProfile().getShip().getCargo()); + + CCostTraversalEntry ce = (CCostTraversalEntry) super.travers(entry, head, edge, target); + ConnectibleEdge cedge = (ConnectibleEdge) ce.getEdge(); + VendorsEdge addingEdge = new VendorsEdge(cedge.getSource(), cedge.getTarget(), cedge.isRefill(), cedge.getFuel(), target.equals(buyer)); + addingEdge.setOrders(orders); + return new VendorsTraversalEntry(ce, head, addingEdge, balance+addingEdge.getProfit()); + } + + protected class VendorsTraversalEntry extends CCostTraversalEntry { + private final double balance; + + protected VendorsTraversalEntry(CCostTraversalEntry entry, double balance) { + super(entry.getHead(), entry.getVertex(), entry.getFuel()); + this.balance = balance; + } + + protected VendorsTraversalEntry(CCostTraversalEntry entry, List> head, Edge edge, double balance) { + super(head, edge, entry.getWeight(), entry.getFuel()); + this.balance = balance; + } + + } + + + } +} 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 4351251..42cfa9c 100644 --- a/core/src/main/java/ru/trader/analysis/graph/CCrawler.java +++ b/core/src/main/java/ru/trader/analysis/graph/CCrawler.java @@ -27,35 +27,13 @@ public class CCrawler> extends Crawler { } @Override - protected Crawler.TraversalEntry start(Vertex vertex) { - double fuel = getShip().getTank(); - return new CTraversalEntry(new ArrayList<>(), vertex, fuel); - } - - @Override - protected Crawler.CostTraversalEntry costStart(Vertex vertex) { + protected CostTraversalEntry start(Vertex vertex) { double fuel = getShip().getTank(); return new CCostTraversalEntry(new ArrayList<>(), vertex, fuel); } @Override - protected Crawler.TraversalEntry travers(TraversalEntry entry, Edge edge) { - T source = entry.vertex.getEntry(); - double distance = source.getDistance(edge.target.getEntry()); - double fuelCost = getShip().getFuelCost(((CTraversalEntry)entry).fuel, distance); - double nextLimit = getProfile().withRefill() ? ((CTraversalEntry)entry).fuel - fuelCost : getShip().getTank(); - boolean refill = nextLimit < 0 && source.canRefill(); - if (refill) { - LOG.trace("Refill"); - refill = true; - nextLimit = getShip().getTank() - getShip().getFuelCost(distance); - } - edge = new ConnectibleEdge<>(edge.getSource(), edge.getTarget(), refill, fuelCost); - return new CTraversalEntry(getCopyList(entry.head, edge), edge.getTarget(), nextLimit); - } - - @Override - protected Crawler.CostTraversalEntry costTravers(Crawler.CostTraversalEntry entry, List> head, Edge edge) { + protected CostTraversalEntry travers(CostTraversalEntry entry, List> head, Edge edge, T target) { T source = entry.vertex.getEntry(); double distance = source.getDistance(edge.target.getEntry()); double fuelCost = getShip().getFuelCost(((CCostTraversalEntry)entry).fuel, distance); @@ -71,23 +49,6 @@ public class CCrawler> extends Crawler { return new CCostTraversalEntry(head, edge, entry.getWeight(), nextLimit); } - protected class CTraversalEntry extends TraversalEntry { - private final double fuel; - - protected CTraversalEntry(List> head, Vertex vertex, double fuel) { - super(head, vertex); - this.fuel = fuel; - } - - @Override - protected Iterator> getIteratorInstance() { - return vertex.getEdges().stream().filter(e -> { - ConnectibleEdge edge = (ConnectibleEdge) e; - return edge.getFuel() <= fuel || e.getSource().getEntry().canRefill(); - }).iterator(); - } - } - protected class CCostTraversalEntry extends CostTraversalEntry { private final double fuel; @@ -101,6 +62,10 @@ public class CCrawler> extends Crawler { this.fuel = fuel; } + public double getFuel() { + return fuel; + } + @Override protected Iterator> getIteratorInstance() { return vertex.getEdges().stream().filter(e -> { 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 5ea60a3..a9333fe 100644 --- a/core/src/main/java/ru/trader/analysis/graph/Crawler.java +++ b/core/src/main/java/ru/trader/analysis/graph/Crawler.java @@ -5,12 +5,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.*; -import java.util.concurrent.ForkJoinPool; import java.util.function.Consumer; public class Crawler { - private final static ForkJoinPool POOL = new ForkJoinPool(); - private final static int THRESHOLD = 4; private final static Logger LOG = LoggerFactory.getLogger(Crawler.class); protected final Graph graph; @@ -61,32 +58,20 @@ public class Crawler { Vertex t = graph.getVertex(target); int found = 0; if (t != null) { - found = ucs(costStart(graph.getRoot()), target, 0, count); + found = ucs(start(graph.getRoot()), target, 0, count); } LOG.debug("Found {} paths", found); } - protected TraversalEntry start(Vertex vertex){ - return new TraversalEntry(new ArrayList<>(), vertex); - } - - protected CostTraversalEntry costStart(Vertex vertex){ + protected CostTraversalEntry start(Vertex vertex){ return new CostTraversalEntry(new ArrayList<>(), vertex); } - protected TraversalEntry travers(TraversalEntry entry, Edge edge){ - return new TraversalEntry(getCopyList(entry.head, edge), edge.getTarget()); - } - - private CostTraversalEntry costTravers(CostTraversalEntry entry, Edge edge){ - return costTravers(entry, getCopyList(entry.head, edge), edge); - } - - protected CostTraversalEntry costTravers(CostTraversalEntry entry, List> head, Edge edge){ + protected CostTraversalEntry travers(CostTraversalEntry entry, List> head, Edge edge, T target){ return new CostTraversalEntry(head, edge, entry.getWeight()); } - private int dfs(TraversalEntry entry, T target, int deep, int count) { + private int dfs(CostTraversalEntry entry, T target, int deep, int count) { int found = 0; List> head = entry.head; Vertex source = entry.vertex; @@ -107,7 +92,7 @@ public class Crawler { LOG.trace("Search around"); for (Edge edge : entry.getEdges()) { if (edge.getTarget().isSingle()) continue; - found += dfs(travers(entry, edge), target, deep, count-found); + found += dfs(travers(entry, getCopyList(head, edge), edge, target), target, deep, count-found); if (found >= count) break; } } @@ -115,13 +100,13 @@ public class Crawler { return found; } - private int bfs(TraversalEntry root, T target, int deep, int count) { + private int bfs(CostTraversalEntry root, T target, int deep, int count) { LOG.trace("BFS from {} to {}, deep {}, count {}", root.vertex, target, deep, count); int found = 0; - LinkedList queue = new LinkedList<>(); + LinkedList queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty() && count > found){ - TraversalEntry entry = queue.poll(); + CostTraversalEntry entry = queue.poll(); List> head = entry.head; Vertex source = entry.vertex; if (head.size() >= maxSize){ @@ -142,7 +127,7 @@ public class Crawler { if (edge.getTarget().isSingle()) continue; if (deep < source.getLevel()) { edge.getTarget().sortEdges(); - queue.add(travers(entry, edge)); + queue.add(travers(entry, getCopyList(head, edge), edge, target)); } } } @@ -183,7 +168,7 @@ public class Crawler { edge = iterator.next(); if (deep < source.getLevel() && !edge.getTarget().isSingle() || edge.isConnect(target)) { LOG.trace("Add edge {} to queue", edge); - queue.add(costTravers(entry, head, edge)); + queue.add(travers(entry, head, edge, target)); } } } @@ -217,7 +202,7 @@ public class Crawler { if (edge.getTarget().isSingle()) continue; if (deep < source.getLevel() && head.size() < maxSize-1) { edge.getTarget().sortEdges(); - queue.add(costTravers(entry, head, edge)); + queue.add(travers(entry, head, edge, target)); i++; } } @@ -236,6 +221,14 @@ public class Crawler { this.vertex = vertex; } + public List> getHead() { + return head; + } + + public Vertex getVertex() { + return vertex; + } + public Iterator> iterator(){ if (iterator == null){ iterator = getIteratorInstance(); @@ -247,7 +240,7 @@ public class Crawler { return vertex.getEdges().iterator(); } - private Iterable> getEdges(){ + protected Iterable> getEdges(){ return this::iterator; } } @@ -269,13 +262,17 @@ public class Crawler { this.cost = cost; } - protected double getWeight(){ + public double getWeight(){ if (weight == null){ weight = cost + (edge !=null ? edge.getWeight() : 0); } return weight; } + public Edge getEdge() { + return edge; + } + @Override public int compareTo(@NotNull CostTraversalEntry other) { int cmp = Double.compare(getWeight(), other.getWeight()); @@ -283,69 +280,4 @@ public class Crawler { return Integer.compare(head.size(), other.head.size()); } } -/* - private class PathFinder extends RecursiveAction { - private final TopList> paths; - private final Path head; - private final Vertex target; - - private PathFinder(TopList> paths, Path head, Vertex target) { - this.paths = paths; - this.head = head; - this.target = target; - } - - @Override - protected void compute() { - if (target == null || isCancelled()) return; - Vertex source = head.getTarget(); - LOG.trace("Find path to deep from {} to {}, head {}", source, target, head); - Edge edge = source.getEdge(target); - if (edge != null){ - Path path = head.connectTo(edge.getTarget(), limit < edge.getLength()); - path.finish(); - LOG.trace("Last edge find, add path {}", path); - synchronized (paths){ - if (!paths.add(path)) complete(null); - } - callback.onFound(); - } - if (!source.isSingle()){ - LOG.trace("Search around"); - ArrayList subTasks = new ArrayList<>(source.getEdges().size()); - Iterator> iterator = source.getEdges().iterator(); - while (iterator.hasNext()) { - Edge next = iterator.next(); - if (isDone() || callback.isCancel()) break; - // target already added if source consist edge - if (next.isConnect(target)) continue; - Path path = head.connectTo(next.getTarget(), limit < next.getLength()); - //Recursive search - PathFinder task = new PathFinder(paths, path, target); - task.fork(); - subTasks.add(task); - if (subTasks.size() == THRESHOLD || !iterator.hasNext()){ - for (PathFinder subTask : subTasks) { - if (isDone() || callback.isCancel()) { - subTask.cancel(callback.isCancel()); - } else { - subTask.join(); - } - } - subTasks.clear(); - } - } - if (!subTasks.isEmpty()){ - for (PathFinder subTask : subTasks) { - if (isDone() || callback.isCancel()) { - subTask.cancel(callback.isCancel()); - } else { - subTask.join(); - } - } - subTasks.clear(); - } - } - } - }*/ } diff --git a/core/src/main/java/ru/trader/analysis/graph/Path.java b/core/src/main/java/ru/trader/analysis/graph/Path.java new file mode 100644 index 0000000..e652d48 --- /dev/null +++ b/core/src/main/java/ru/trader/analysis/graph/Path.java @@ -0,0 +1,24 @@ +package ru.trader.analysis.graph; + +import ru.trader.graph.Connectable; + +import java.util.ArrayList; +import java.util.List; + +public class Path> { + private final List> entries; + + public Path(List> edges) { + entries = new ArrayList<>(edges.size()); + for (int i = 0; i < edges.size(); i++) { + ConnectibleEdge edge = edges.get(i); + if (i==0) entries.add(new PathEntry<>(edge.getSource().getEntry(), false)); + entries.add(new PathEntry<>(edge.getTarget().getEntry(), edge.isRefill())); + } + } + + public PathEntry get(int index){ + return entries.get(index); + } + +} diff --git a/core/src/main/java/ru/trader/analysis/graph/PathEntry.java b/core/src/main/java/ru/trader/analysis/graph/PathEntry.java new file mode 100644 index 0000000..2007be6 --- /dev/null +++ b/core/src/main/java/ru/trader/analysis/graph/PathEntry.java @@ -0,0 +1,19 @@ +package ru.trader.analysis.graph; + +public class PathEntry { + private final T entry; + private boolean refill; + + public PathEntry(T entry, boolean refill) { + this.entry = entry; + this.refill = refill; + } + + public T getEntry() { + return entry; + } + + public boolean isRefill() { + return refill; + } +} diff --git a/core/src/main/java/ru/trader/analysis/graph/Vertex.java b/core/src/main/java/ru/trader/analysis/graph/Vertex.java index 4847afb..af63b29 100644 --- a/core/src/main/java/ru/trader/analysis/graph/Vertex.java +++ b/core/src/main/java/ru/trader/analysis/graph/Vertex.java @@ -33,7 +33,9 @@ public class Vertex { public void connect(Edge edge){ assert this == edge.getSource(); synchronized (edges){ - edges.add(edge); + if (!edges.contains(edge)){ + edges.add(edge); + } } } diff --git a/core/src/main/java/ru/trader/core/Place.java b/core/src/main/java/ru/trader/core/Place.java index afbf2e8..aef92b1 100644 --- a/core/src/main/java/ru/trader/core/Place.java +++ b/core/src/main/java/ru/trader/core/Place.java @@ -17,6 +17,17 @@ public interface Place extends Connectable { void setPosition(double x, double y, double z); Collection get(); + default Collection get(boolean withTransit){ + if (withTransit){ + Collection vendors = new ArrayList<>(); + vendors.add(new TransitVendor(this)); + vendors.addAll(get()); + return vendors; + } else { + return get(); + } + } + default Vendor get(String name){ Optional vendor = get().stream().filter(p -> name.equalsIgnoreCase(p.getName())).findFirst(); return vendor.isPresent() ? vendor.get() : null; diff --git a/core/src/main/java/ru/trader/core/Profile.java b/core/src/main/java/ru/trader/core/Profile.java index cd28934..9bb5af3 100644 --- a/core/src/main/java/ru/trader/core/Profile.java +++ b/core/src/main/java/ru/trader/core/Profile.java @@ -6,6 +6,7 @@ public class Profile { private int jumps; private Ship ship; private boolean refill; + private int routesCount; //Scorer multipliers private int scoreOrdersCount; private double distanceMult; @@ -55,6 +56,14 @@ public class Profile { this.refill = refill; } + public int getRoutesCount() { + return routesCount; + } + + public void setRoutesCount(int routesCount) { + this.routesCount = routesCount; + } + public int getScoreOrdersCount() { return scoreOrdersCount; } diff --git a/core/src/main/java/ru/trader/core/Ship.java b/core/src/main/java/ru/trader/core/Ship.java index 74dcc6a..5b60cae 100644 --- a/core/src/main/java/ru/trader/core/Ship.java +++ b/core/src/main/java/ru/trader/core/Ship.java @@ -122,8 +122,8 @@ public class Ship { {90.0, 75.0, 60.0, 54.0, 48.0}, {150.0, 125.0, 100.0, 90.0, 80.0}, {525.0, 438.0, 350.0, 315.0, 280.0}, - {1,050.0, 875.0, 700.0, 630.0, 560.0}, - {1,800.0, 1,500.0, 1,200.0, 1,080.0, 960.0} + {1050.0, 875.0, 700.0, 630.0, 560.0}, + {1800.0, 1500.0, 1200.0, 1080.0, 960.0} }; //FSD Max fuel per jump [class][rating] private final static double[][] FSD_MAX_FUEL= { diff --git a/core/src/main/java/ru/trader/core/TransitVendor.java b/core/src/main/java/ru/trader/core/TransitVendor.java new file mode 100644 index 0000000..05e1580 --- /dev/null +++ b/core/src/main/java/ru/trader/core/TransitVendor.java @@ -0,0 +1,113 @@ +package ru.trader.core; + +import ru.trader.graph.Connectable; + +import java.util.Collection; +import java.util.Collections; + +public class TransitVendor implements Vendor { + protected Place place; + + public TransitVendor(Place place) { + this.place = place; + } + + @Override + public String getName() { + return ""; + } + + @Override + public void setName(String name) { + throw new UnsupportedOperationException("Is fake vendor, change unsupported"); + } + + @Override + public Place getPlace() { + return place; + } + + @Override + public double getDistance() { + return 0; + } + + @Override + public void setDistance(double distance) { + throw new UnsupportedOperationException("Is fake vendor, change unsupported"); + } + + @Override + public void add(SERVICE_TYPE service) { + throw new UnsupportedOperationException("Is fake vendor, change unsupported"); + } + + @Override + public void remove(SERVICE_TYPE service) { + throw new UnsupportedOperationException("Is fake vendor, change unsupported"); + } + + @Override + public boolean has(SERVICE_TYPE service) { + return false; + } + + @Override + public Collection getServices() { + return Collections.emptyList(); + } + + @Override + public Offer addOffer(OFFER_TYPE type, Item item, double price, long count) { + throw new UnsupportedOperationException("Is fake vendor, change unsupported"); + } + + @Override + public void add(Offer offer) { + throw new UnsupportedOperationException("Is fake vendor, change unsupported"); + } + + @Override + public void remove(Offer offer) { + throw new UnsupportedOperationException("Is fake vendor, change unsupported"); + } + + @Override + public Collection get(OFFER_TYPE type) { + return Collections.emptyList(); + } + + @Override + public Offer get(OFFER_TYPE type, Item item) { + return null; + } + + @Override + public boolean has(OFFER_TYPE type, Item item) { + return false; + } + + @Override + public int compareTo(Connectable o) { + double d = getDistance((Vendor)o); + return d == 0 ? 0 : d > 0 ? 1 : -1; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TransitVendor)) return false; + TransitVendor that = (TransitVendor) o; + return place.equals(that.place); + } + + @Override + public int hashCode() { + return place.hashCode(); + } + + @Override + public String toString() { + return "Transit - "+place; + } +} diff --git a/core/src/main/java/ru/trader/core/VendorsIterator.java b/core/src/main/java/ru/trader/core/VendorsIterator.java index a5a83fe..da96c63 100644 --- a/core/src/main/java/ru/trader/core/VendorsIterator.java +++ b/core/src/main/java/ru/trader/core/VendorsIterator.java @@ -1,9 +1,6 @@ package ru.trader.core; -import ru.trader.graph.Connectable; - import java.util.Collection; -import java.util.Collections; import java.util.Iterator; public class VendorsIterator implements Iterator { @@ -65,114 +62,4 @@ public class VendorsIterator implements Iterator { } return current; } - - - private class TransitVendor implements Vendor { - - private Place place; - - private TransitVendor(Place place) { - this.place = place; - } - - @Override - public String getName() { - return ""; - } - - @Override - public void setName(String name) { - throw new UnsupportedOperationException("Is fake vendor, change unsupported"); - } - - @Override - public Place getPlace() { - return place; - } - - @Override - public double getDistance() { - return 0; - } - - @Override - public void setDistance(double distance) { - throw new UnsupportedOperationException("Is fake vendor, change unsupported"); - } - - @Override - public void add(SERVICE_TYPE service) { - throw new UnsupportedOperationException("Is fake vendor, change unsupported"); - } - - @Override - public void remove(SERVICE_TYPE service) { - throw new UnsupportedOperationException("Is fake vendor, change unsupported"); - } - - @Override - public boolean has(SERVICE_TYPE service) { - return false; - } - - @Override - public Collection getServices() { - return Collections.emptyList(); - } - - @Override - public Offer addOffer(OFFER_TYPE type, Item item, double price, long count) { - throw new UnsupportedOperationException("Is fake vendor, change unsupported"); - } - - @Override - public void add(Offer offer) { - throw new UnsupportedOperationException("Is fake vendor, change unsupported"); - } - - @Override - public void remove(Offer offer) { - throw new UnsupportedOperationException("Is fake vendor, change unsupported"); - } - - @Override - public Collection get(OFFER_TYPE type) { - return Collections.emptyList(); - } - - @Override - public Offer get(OFFER_TYPE type, Item item) { - return null; - } - - @Override - public boolean has(OFFER_TYPE type, Item item) { - return false; - } - - @Override - public int compareTo(Connectable o) { - double d = getDistance((Vendor)o); - return d == 0 ? 0 : d > 0 ? 1 : -1; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof TransitVendor)) return false; - TransitVendor that = (TransitVendor) o; - return place.equals(that.place); - } - - @Override - public int hashCode() { - return place.hashCode(); - } - - @Override - public String toString() { - return "Transit"; - } - } - } diff --git a/core/src/test/java/ru/trader/analysis/RouteFillerTest.java b/core/src/test/java/ru/trader/analysis/RouteFillerTest.java index d229384..1f940ea 100644 --- a/core/src/test/java/ru/trader/analysis/RouteFillerTest.java +++ b/core/src/test/java/ru/trader/analysis/RouteFillerTest.java @@ -49,8 +49,8 @@ public class RouteFillerTest extends Assert { v2.addOffer(OFFER_TYPE.BUY, ITEM2, 350, -1); v2.addOffer(OFFER_TYPE.BUY, ITEM3, 400, -1); - Route route = new Route(new RouteEntry(v1, false, 0)); - route.add(new RouteEntry(v2, false, 0)); + Route route = new Route(new RouteEntry(v1, false, 0,0)); + route.add(new RouteEntry(v2, false, 0,0)); return route; } @@ -91,9 +91,9 @@ public class RouteFillerTest extends Assert { v3.addOffer(OFFER_TYPE.BUY, ITEM2, 350, -1); v3.addOffer(OFFER_TYPE.BUY, ITEM3, 400, -1); - Route route = new Route(new RouteEntry(v1, false, 0)); - route.add(new RouteEntry(v2, false, 0)); - route.add(new RouteEntry(v3, false, 0)); + Route route = new Route(new RouteEntry(v1, false, 0,0)); + route.add(new RouteEntry(v2, false, 0,0)); + route.add(new RouteEntry(v3, false, 0,0)); return route; } @@ -140,10 +140,10 @@ public class RouteFillerTest extends Assert { v3.addOffer(OFFER_TYPE.BUY, ITEM1, 200, -1); v4.addOffer(OFFER_TYPE.BUY, ITEM3, 450, -1); - Route route = new Route(new RouteEntry(v1, false, 0)); - route.add(new RouteEntry(v2, false, 0)); - route.add(new RouteEntry(v3, false, 0)); - route.add(new RouteEntry(v4, false, 0)); + Route route = new Route(new RouteEntry(v1, false, 0,0)); + route.add(new RouteEntry(v2, false, 0,0)); + route.add(new RouteEntry(v3, false, 0,0)); + route.add(new RouteEntry(v4, false, 0,0)); return route; } @@ -222,11 +222,11 @@ public class RouteFillerTest extends Assert { v4.addOffer(OFFER_TYPE.BUY, ITEM3, 370, -1); v5.addOffer(OFFER_TYPE.BUY, ITEM1, 400, -1); - Route route = new Route(new RouteEntry(v1, false, 0)); - route.add(new RouteEntry(v2, false, 0)); - route.add(new RouteEntry(v3, false, 0)); - route.add(new RouteEntry(v4, false, 0)); - route.add(new RouteEntry(v5, false, 0)); + Route route = new Route(new RouteEntry(v1, false, 0,0)); + route.add(new RouteEntry(v2, false, 0,0)); + route.add(new RouteEntry(v3, false, 0,0)); + route.add(new RouteEntry(v4, false, 0,0)); + route.add(new RouteEntry(v5, false, 0,0)); return route; } @@ -285,10 +285,10 @@ public class RouteFillerTest extends Assert { v3.addOffer(OFFER_TYPE.BUY, ITEM1, 200, -1); v4.addOffer(OFFER_TYPE.BUY, ITEM3, 450, -1); - Route route = new Route(new RouteEntry(v1, false, 0)); - route.add(new RouteEntry(v2, false, 0)); - route.add(new RouteEntry(v3, false, 0)); - route.add(new RouteEntry(v4, false, 0)); + Route route = new Route(new RouteEntry(v1, false, 0,0)); + route.add(new RouteEntry(v2, false, 0,0)); + route.add(new RouteEntry(v3, false, 0,0)); + route.add(new RouteEntry(v4, false, 0,0)); return route; } @@ -367,8 +367,8 @@ public class RouteFillerTest extends Assert { v2.addOffer(OFFER_TYPE.BUY, ITEM2, 225, -1); - Route route = new Route(new RouteEntry(v1, false, 0)); - route.add(new RouteEntry(v2, false, 0)); + Route route = new Route(new RouteEntry(v1, false, 0,0)); + route.add(new RouteEntry(v2, false, 0,0)); return route; @@ -384,9 +384,9 @@ public class RouteFillerTest extends Assert { v3.addOffer(OFFER_TYPE.BUY, ITEM1, 200, -1); v4.addOffer(OFFER_TYPE.BUY, ITEM3, 450, -1); - Route route = new Route(new RouteEntry(v2, false, 0)); - route.add(new RouteEntry(v3, false, 0)); - route.add(new RouteEntry(v4, false, 0)); + Route route = new Route(new RouteEntry(v2, false, 0,0)); + route.add(new RouteEntry(v3, false, 0,0)); + route.add(new RouteEntry(v4, false, 0,0)); return route; } diff --git a/core/src/test/java/ru/trader/graph/RouteSearcherTest.java b/core/src/test/java/ru/trader/analysis/RouteSearcherTest.java similarity index 63% rename from core/src/test/java/ru/trader/graph/RouteSearcherTest.java rename to core/src/test/java/ru/trader/analysis/RouteSearcherTest.java index 1dcf14b..880e177 100644 --- a/core/src/test/java/ru/trader/graph/RouteSearcherTest.java +++ b/core/src/test/java/ru/trader/analysis/RouteSearcherTest.java @@ -1,23 +1,35 @@ -package ru.trader.graph; +package ru.trader.analysis; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import ru.trader.core.Market; -import ru.trader.core.Place; -import ru.trader.core.Vendor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.trader.core.*; import ru.trader.store.simple.Store; import java.io.InputStream; import java.util.List; +import java.util.stream.Collectors; -public class RouteSearcherTest extends Assert { - private static Market market; +public class RouteSearcherTest extends Assert{ + private final static Logger LOG = LoggerFactory.getLogger(RouteSearcherTest.class); + + private Market world; + private FilteredMarket fWorld; + + private Place ithaca; + private Place lhs3262; @Before public void setUp() throws Exception { InputStream is = getClass().getResourceAsStream("/world.xml"); - market = Store.loadFromFile(is); + world = Store.loadFromFile(is); + ithaca = world.get("Ithaca"); + lhs3262 = world.get("LHS 3262"); + + MarketFilter filter = new MarketFilter(); + fWorld = new FilteredMarket(world, filter); } @Test @@ -25,23 +37,24 @@ public class RouteSearcherTest extends Assert { // 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 ithaca_st = ithaca.get().iterator().next(); + Ship ship = new Ship(); + ship.setCargo(440); ship.setTank(16); + ship.setEngine(5, 'A'); ship.setMass(466); + Profile profile = new Profile(ship); + profile.setBalance(6000000); profile.setJumps(6); + profile.setRoutesCount(10); + Scorer scorer = new Scorer(fWorld, profile); - RouteSearcher searcher = new RouteSearcher(13.4, 40); - RouteGraph graph = new RouteGraph(ithaca, market.getVendors(true), 40, 13.4, true, 6); - graph.setCargo(440); - graph.setBalance(6000000); + LOG.info("Start test routes"); + RouteSearcher searcher = new RouteSearcher(scorer); - - List> epaths = graph.getPathsTo(ithaca, 10); - PathRoute expect = epaths.stream().map(p -> (PathRoute) p).findFirst().get(); - - List apaths = searcher.getPaths(ithaca, ithaca, market.getVendors(true), 6, 6000000, 440, 10); - PathRoute actual = apaths.stream().findFirst().get(); - assertTrue("Routes is different",expect.isRoute(actual)); + List apaths = searcher.getRoutes(ithaca_st, ithaca_st, fWorld.getMarkets(true).collect(Collectors.toList())); + Route actual = apaths.stream().findFirst().get(); + //assertTrue("Routes is different",expect.isRoute(actual)); } - +/* @Test public void testRoutes2() throws Exception { // Balance: 6000000, cargo: 440, tank: 40, distance: 13.6, jumps: 6 @@ -72,6 +85,5 @@ public class RouteSearcherTest extends Assert { assertEquals("Routes is different",expect.getAvgProfit(), actual.getAvgProfit(), 0.00001); } - - +*/ } diff --git a/core/src/test/java/ru/trader/analysis/RouteTest.java b/core/src/test/java/ru/trader/analysis/RouteTest.java index c17bfce..07e099a 100644 --- a/core/src/test/java/ru/trader/analysis/RouteTest.java +++ b/core/src/test/java/ru/trader/analysis/RouteTest.java @@ -28,9 +28,9 @@ public class RouteTest extends Assert { v3 = new SimpleVendor("v3",0,0,0); v4 = new SimpleVendor("v4",0,0,0); - Route path = new Route(new RouteEntry(v1, false, 0)); - path.add(new RouteEntry(v2, false, 0)); - path.add(new RouteEntry(v3, false, 0)); + Route path = new Route(new RouteEntry(v1, false, 0,0)); + path.add(new RouteEntry(v2, false, 0,0)); + path.add(new RouteEntry(v3, false, 0,0)); TestUtil.assertCollectionContainAll(path.getVendors(), v1, v2, v3); } @@ -42,9 +42,9 @@ public class RouteTest extends Assert { v3 = new SimpleVendor("v3",0,0,0); v4 = new SimpleVendor("v4",0,0,0); - Route path = new Route(new RouteEntry(v1, false, 0)); - path.add(new RouteEntry(v2, false, 0)); - path.add(new RouteEntry(v3, false, 0)); + Route path = new Route(new RouteEntry(v1, false, 0,0)); + path.add(new RouteEntry(v2, false, 0,0)); + path.add(new RouteEntry(v3, false, 0,0)); Collection vendors = new ArrayList<>(); Collections.addAll(vendors, v1, v2, v3); assertTrue(path.contains(vendors)); diff --git a/core/src/test/java/ru/trader/analysis/VendorsGraphTest.java b/core/src/test/java/ru/trader/analysis/VendorsGraphTest.java new file mode 100644 index 0000000..34859f3 --- /dev/null +++ b/core/src/test/java/ru/trader/analysis/VendorsGraphTest.java @@ -0,0 +1,78 @@ +package ru.trader.analysis; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.trader.analysis.graph.Crawler; +import ru.trader.analysis.graph.SimpleCollector; +import ru.trader.analysis.graph.Vertex; +import ru.trader.core.*; +import ru.trader.store.simple.Store; + +import java.io.InputStream; +import java.util.stream.Collectors; + + +public class VendorsGraphTest extends Assert { + private final static Logger LOG = LoggerFactory.getLogger(VendorsGraphTest.class); + + private Market world; + private FilteredMarket fWorld; + + private Place breksta; + private Place bhadaba; + private Place lhs1541; + private Place itza; + + @Before + public void setUp() throws Exception { + InputStream is = getClass().getResourceAsStream("/test3.xml"); + world = Store.loadFromFile(is); + breksta = world.get("Breksta"); + bhadaba = world.get("Bhadaba"); + lhs1541 = world.get("LHS 1541"); + itza = world.get("Itza"); + + MarketFilter filter = new MarketFilter(); + fWorld = new FilteredMarket(world, filter); + } + + @Test + public void testBuild() throws Exception { + Vendor grantTerminal = breksta.get("Grant Terminal"); + Vendor perezMarket = breksta.get("Perez market"); + Vendor kandelRing = bhadaba.get("Kandel Ring"); + Vendor robertsHub = bhadaba.get("Roberts Hub"); + Vendor cabreraDock = lhs1541.get("Cabrera Dock"); + Vendor hallerPort = lhs1541.get("Haller Port"); + Vendor luikenPort = itza.get("Luiken Port"); + Ship ship = new Ship(); + ship.setCargo(24); ship.setEngine(2,'A'); + Profile profile = new Profile(ship); + LOG.info("Start build test"); + profile.setBalance(100000); profile.setJumps(6); + Scorer scorer = new Scorer(fWorld, profile); + LOG.info("Build vendors graph"); + VendorsGraph vGraph = new VendorsGraph(scorer); + vGraph.build(cabreraDock, fWorld.getMarkets(true).collect(Collectors.toList())); + + SimpleCollector paths = new SimpleCollector<>(); + Crawler crawler = vGraph.crawler(paths::add); + + crawler.findMin(cabreraDock, 100); + assertEquals(100, paths.get().size()); + paths.clear(); + + Vertex x = vGraph.getRoot(); + assertNotNull(x); + } + + @After + public void tearDown() throws Exception { + world = null; + fWorld = null; + } +} diff --git a/core/src/test/java/ru/trader/analysis/graph/CrawlerTest.java b/core/src/test/java/ru/trader/analysis/graph/CrawlerTest.java index ac1e9ce..17867b8 100644 --- a/core/src/test/java/ru/trader/analysis/graph/CrawlerTest.java +++ b/core/src/test/java/ru/trader/analysis/graph/CrawlerTest.java @@ -12,9 +12,7 @@ import ru.trader.core.Ship; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; public class CrawlerTest extends Assert { private final static Logger LOG = LoggerFactory.getLogger(CrawlerTest.class); @@ -89,7 +87,7 @@ public class CrawlerTest extends Assert { graph.build(x5, entrys); // x5 <-> x4, x5 <-> x6 - SimpleCollector paths = new SimpleCollector(); + SimpleCollector paths = new SimpleCollector<>(); Crawler crawler = new CCrawler<>(graph, paths::add); crawler.findMin(x4, 10); assertPaths(paths.get(), PPath.of(x5, x4)); @@ -118,7 +116,7 @@ public class CrawlerTest extends Assert { graph.build(x5, entrys); // x5 <-> x4 <-> x3 <-> x2, x5 <-> x6 <-> x7 <-> x8 // x5 <-> x3, x4 <-> x2, x3 <-> x6, x4 <-> x6 - SimpleCollector paths = new SimpleCollector(); + SimpleCollector paths = new SimpleCollector<>(); Crawler crawler = new CCrawler<>(graph, paths::add); crawler.findMin(x8, 10); @@ -140,7 +138,7 @@ public class CrawlerTest extends Assert { PPath.of(x5, x4, x6, x4), PPath.of(x5, x6, x3, x4), PPath.of(x5, x3, x5, x4), PPath.of(x5, x4, x2, x4), PPath.of(x5, x3, x2, x4), PPath.of(x5, x3, x6, x4) - ); + ); TestUtil.assertCollectionEquals(paths.getWeights(), 5.0, 15.0, 15.0, 15.0, 15.0, 15.0, 25.0, 25.0, @@ -182,7 +180,7 @@ public class CrawlerTest extends Assert { graph.build(x5, entrys); // x5 <-> x4 <- refill -> x3 <- refill -> x2, x5 <-> x6 // x5 <-> x3 <- refill -> x2, x5 <-> x4 <- refill -> x6 - SimpleCollector paths = new SimpleCollector(); + SimpleCollector paths = new SimpleCollector<>(); Crawler crawler = new CCrawler<>(graph, paths::add); crawler.findMin(x1, 10); @@ -225,7 +223,7 @@ public class CrawlerTest extends Assert { // x5 <-> x6 <-> x4 <-refill -> x2 // x5 <-> x3 <- refill -> x2 // x5 <-> x4 <- refill -> x6 - SimpleCollector paths = new SimpleCollector(); + SimpleCollector paths = new SimpleCollector<>(); Crawler crawler = new CCrawler<>(graph, paths::add); crawler.findMin(x1, 10); @@ -268,33 +266,4 @@ public class CrawlerTest extends Assert { entrys.clear(); } - private class SimpleCollector { - private List>> paths = new ArrayList<>(); - - public void add(List> path){ - paths.add(path); - } - - public List>> get() { - return paths; - } - - public List> get(int index) { - if (index >= paths.size()) return Collections.emptyList(); - return paths.get(index); - } - public void clear(){ - paths.clear(); - } - - public double getWeight(int index){ - if (index >= paths.size()) return 0; - return paths.get(index).stream().mapToDouble(Edge::getWeight).sum(); - } - - public Collection getWeights(){ - return paths.stream().map(p -> p.stream().mapToDouble(Edge::getWeight).sum()).collect(Collectors.toList()); - } - } - } \ No newline at end of file diff --git a/core/src/test/java/ru/trader/analysis/graph/SimpleCollector.java b/core/src/test/java/ru/trader/analysis/graph/SimpleCollector.java new file mode 100644 index 0000000..eed87bc --- /dev/null +++ b/core/src/test/java/ru/trader/analysis/graph/SimpleCollector.java @@ -0,0 +1,40 @@ +package ru.trader.analysis.graph; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Created by Mo on 20.05.2015. + */ +public class SimpleCollector { + private List>> paths = new ArrayList<>(); + + public void add(List> path){ + paths.add(path); + } + + public List>> get() { + return paths; + } + + public List> get(int index) { + if (index >= paths.size()) return Collections.emptyList(); + return paths.get(index); + } + + public void clear(){ + paths.clear(); + } + + public double getWeight(int index){ + if (index >= paths.size()) return 0; + return paths.get(index).stream().mapToDouble(Edge::getWeight).sum(); + } + + public Collection getWeights(){ + return paths.stream().map(p -> p.stream().mapToDouble(Edge::getWeight).sum()).collect(Collectors.toList()); + } +} diff --git a/core/src/test/java/ru/trader/graph/GraphTest.java b/core/src/test/java/ru/trader/graph/GraphTest.java deleted file mode 100644 index bb9ef80..0000000 --- a/core/src/test/java/ru/trader/graph/GraphTest.java +++ /dev/null @@ -1,321 +0,0 @@ -package ru.trader.graph; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import ru.trader.TestUtil; - -import java.util.ArrayList; -import java.util.Collection; - -public class GraphTest extends Assert { - private final static Logger LOG = LoggerFactory.getLogger(GraphTest.class); - - private final static ArrayList entrys = new ArrayList<>(); - private final static Point x1 = new Point("x1",-40); - private final static Point x2 = new Point("x2",-20); - private final static Point x3 = new Point("x3",-10, true); - private final static Point x4 = new Point("x4",-5, true); - private final static Point x5 = new Point("x5",0); - private final static Point x6 = new Point("x6",5); - private final static Point x7 = new Point("x7",20); - private final static Point x8 = new Point("x8",30); - private final static Point x9 = new Point("x9",40); - private final static Point x10 = new Point("x10",50); - - @Before - public void setUp() throws Exception { - entrys.add(x1); - entrys.add(x2); - entrys.add(x3); - entrys.add(x4); - entrys.add(x5); - entrys.add(x6); - entrys.add(x7); - entrys.add(x8); - entrys.add(x9); - entrys.add(x10); - } - - @Test - public void testBuild0() throws Exception { - LOG.info("Start graph build test0"); - - Graph graph = new Graph<>(x5, entrys, 4.9, 10); - // x5 - assertFalse(graph.isAccessible(x1)); - assertFalse(graph.isAccessible(x2)); - assertFalse(graph.isAccessible(x3)); - assertFalse(graph.isAccessible(x4)); - assertTrue(graph.isAccessible(x5)); - assertFalse(graph.isAccessible(x6)); - assertFalse(graph.isAccessible(x7)); - assertFalse(graph.isAccessible(x8)); - assertFalse(graph.isAccessible(x9)); - assertFalse(graph.isAccessible(x10)); - } - - - @Test - public void testBuild1() throws Exception { - LOG.info("Start graph build test1"); - Graph graph = new Graph<>(x5, entrys, 5.1, true, 2); - // x5 <-> x4 <-refill-> x3, x5 -> x6 - assertFalse(graph.isAccessible(x1)); - assertFalse(graph.isAccessible(x2)); - assertTrue(graph.isAccessible(x3)); - assertTrue(graph.isAccessible(x4)); - assertTrue(graph.isAccessible(x5)); - assertTrue(graph.isAccessible(x6)); - assertFalse(graph.isAccessible(x7)); - assertFalse(graph.isAccessible(x8)); - assertFalse(graph.isAccessible(x9)); - assertFalse(graph.isAccessible(x10)); - - Vertex x = graph.getVertex(x5); - // x5 -> x4, x5 -> x6 - checkEdges(x, new Point[]{x4, x6}, new Point[]{x1, x2, x3, x7, x8, x9, x10}); - // x4 -> x5 - x = graph.getVertex(x4); - checkEdges(x, new Point[]{x5, x3}, new Point[]{x1, x2, x6, x7, x8, x9, x10}); - // x6 <- x5 - x = graph.getVertex(x6); - checkEdges(x, new Point[]{}, new Point[]{x1, x2, x3, x4, x5, x7, x8, x9, x10}); - - } - - private void checkEdges(Vertex vertex, Point[] trueEdge, Point[] falseEdge){ - for (Point point : trueEdge) { - assertTrue(String.format("%s must have edge to %s", vertex, point), vertex.isConnected(point)); - } - for (Point point : falseEdge) { - assertFalse(String.format("%s must not have edge to %s", vertex, point), vertex.isConnected(point)); - } - } - - @Test - public void testBuild2() throws Exception { - LOG.info("Start graph build test2"); - Graph graph = new Graph<>(x5, entrys, 5.1, 3); - // x5 <-> x4 <-> x3, x5 <-> x6 - assertFalse(graph.isAccessible(x1)); - assertFalse(graph.isAccessible(x2)); - assertTrue(graph.isAccessible(x3)); - assertTrue(graph.isAccessible(x4)); - assertTrue(graph.isAccessible(x5)); - assertTrue(graph.isAccessible(x6)); - assertFalse(graph.isAccessible(x7)); - assertFalse(graph.isAccessible(x8)); - assertFalse(graph.isAccessible(x9)); - assertFalse(graph.isAccessible(x10)); - - Vertex x = graph.getVertex(x5); - // x5 -> x4, x5 -> x6 - checkEdges(x, new Point[]{x4, x6}, new Point[]{x1, x2, x3, x7, x8, x9, x10}); - // x3 -> x4 - x = graph.getVertex(x3); - checkEdges(x, new Point[]{x4}, new Point[]{x1, x2, x5, x6, x7, x8, x9, x10}); - // x4 -> x5, x4 -> x3 - x = graph.getVertex(x4); - checkEdges(x, new Point[]{x3, x5}, new Point[]{x1, x2, x6, x7, x8, x9, x10}); - // x6 -> x5 - x = graph.getVertex(x6); - checkEdges(x, new Point[]{x5}, new Point[]{x1, x2, x3, x4, x7, x8, x9, x10}); - } - - - @Test - public void testBuild3() throws Exception { - LOG.info("Start graph build test3"); - Graph graph = new Graph<>(x5, entrys, 5.1, 3); - // x5 <-> x4 <-> x3, x5 <-> x6 - assertFalse(graph.isAccessible(x1)); - assertFalse(graph.isAccessible(x2)); - assertTrue(graph.isAccessible(x3)); - assertTrue(graph.isAccessible(x4)); - assertTrue(graph.isAccessible(x5)); - assertTrue(graph.isAccessible(x6)); - assertFalse(graph.isAccessible(x7)); - assertFalse(graph.isAccessible(x8)); - assertFalse(graph.isAccessible(x9)); - assertFalse(graph.isAccessible(x10)); - - Vertex x = graph.getVertex(x5); - // x5 -> x4, x5 -> x6 - checkEdges(x, new Point[]{x4, x6}, new Point[]{x1, x2, x3, x7, x8, x9, x10}); - // x3 -> x4 - x = graph.getVertex(x3); - checkEdges(x, new Point[]{x4}, new Point[]{x1, x2, x5, x6, x7, x8, x9, x10}); - // x4 -> x5, x4 -> x3 - x = graph.getVertex(x4); - checkEdges(x, new Point[]{x3, x5}, new Point[]{x1, x2, x6, x7, x8, x9, x10}); - // x6 -> x5 - x = graph.getVertex(x6); - checkEdges(x, new Point[]{x5}, new Point[]{x1, x2, x3, x4, x7, x8, x9, x10}); - - - } - - - @Test - public void testBuild4() throws Exception { - LOG.info("Start graph build test4"); - Graph graph = new Graph<>(x5, entrys, 15.1, 3); - // x5 <-> x4 <-> x3 -> x2, x5 <-> x6 <-> x7 -> x8 - // x5 <-> x3, x5 <-> x4 <-> x2, x3 <-> x6, x4 <-> x6 - assertFalse(graph.isAccessible(x1)); - assertTrue(graph.isAccessible(x2)); - assertTrue(graph.isAccessible(x3)); - assertTrue(graph.isAccessible(x4)); - assertTrue(graph.isAccessible(x5)); - assertTrue(graph.isAccessible(x6)); - assertTrue(graph.isAccessible(x7)); - assertTrue(graph.isAccessible(x8)); - assertFalse(graph.isAccessible(x9)); - assertFalse(graph.isAccessible(x10)); - - Vertex x = graph.getVertex(x5); - // x5 -> x4, x5 -> x3, x5 -> x6 - checkEdges(x, new Point[]{x3, x4, x6}, new Point[]{x1, x2, x7, x8, x9, x10}); - // x2 -> x3, x2 -> x4 - x = graph.getVertex(x2); - checkEdges(x, new Point[]{x3, x4}, new Point[]{x1, x5, x6, x7, x8, x9, x10}); - // x3 -> x4, x3 -> x2, x3 -> x5, x3 -> x6 - x = graph.getVertex(x3); - checkEdges(x, new Point[]{x2, x4, x5, x6}, new Point[]{x1, x7, x8, x9, x10}); - // x4 -> x5, x4 -> x3, x4 -> x2, x4 -> x6 - x = graph.getVertex(x4); - checkEdges(x, new Point[]{x2, x3, x5, x6}, new Point[]{x1, x7, x8, x9, x10}); - // x6 -> x5, x6 -> x7, x6 -> x3, x6 -> x4 - x = graph.getVertex(x6); - checkEdges(x, new Point[]{x5, x7, x3, x4}, new Point[]{x1, x2, x8, x9, x10}); - // x7 -> x6, x7 -> x8 - x = graph.getVertex(x7); - checkEdges(x, new Point[]{x6, x8}, new Point[]{x1, x2, x3, x4, x5, x9, x10}); - // x8 <- x7 - x = graph.getVertex(x8); - checkEdges(x, new Point[]{}, new Point[]{x1, x2, x3, x4, x5, x6, x7, x9, x10}); - - } - - - @Test - public void testGetPaths() throws Exception { - LOG.info("Start get paths test"); - Graph graph = new Graph<>(x5, entrys, 5.1, 2); - // x5 <-> x4, x5 <-> x6 - - Collection> paths = graph.getPathsTo(x4); - TestUtil.assertCollectionEquals(paths, Path.toPath(x5, x4)); - - paths = graph.getPathsTo(x6); - TestUtil.assertCollectionEquals(paths, Path.toPath(x5, x6)); - - paths = graph.getPathsTo(x7); - assertEquals(paths.size(), 0); - - } - - @Test - public void testGetPaths2() throws Exception { - LOG.info("Start get paths test2"); - Graph graph = new Graph<>(x5, entrys, 15.1, 3); - // x5 <-> x4 <-> x3 <-> x2, x5 <-> x6 <-> x7 <-> x8 - // x5 <-> x3, x4 <-> x2, x3 <-> x6, x4 <-> x6 - - Collection> paths = graph.getPathsTo(x8); - TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6, x7, x8)); - - paths = graph.getPathsTo(x7); - 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); - 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), - Path.toPath(x5, x3, x5, x4), Path.toPath(x5, x6, x3, x4), Path.toPath(x5, x3, x2, x4), Path.toPath(x5, x3, x6, x4), - Path.toPath(x5, x6, x5, x4)); - - - paths = graph.getPathsTo(x5); - TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4, x5), Path.toPath(x5, x4, x6, x5), Path.toPath(x5, x4, x3, x5), - Path.toPath(x5, x6, x5), Path.toPath(x5, x6, x4, x5), Path.toPath(x5, x6, x3, x5), Path.toPath(x5, x3, x5), - Path.toPath(x5, x3, x4, x5), Path.toPath(x5, x3, x6, x5)); - - - Path fast = graph.getFastPathTo(x8); - assertEquals(fast, Path.toPath(x5, x6, x7, x8)); - - fast = graph.getFastPathTo(x7); - assertEquals(fast, Path.toPath(x5, x6, x7)); - - fast = graph.getFastPathTo(x4); - assertEquals(fast, Path.toPath(x5, x4)); - } - - - @Test - public void testGetRefillPaths() throws Exception { - LOG.info("Start get refill paths"); - Graph graph = new Graph<>(x5, entrys, 10.1, 3); - // x5 <-> x4 <- refill -> x3 <- refill -> x2, x5 <-> x6 - // x5 <-> x3 <- refill -> x2, x5 <-> x4 <- refill -> x6 - - Collection> paths = graph.getPathsTo(x1); - assertTrue(paths.isEmpty()); - - paths = graph.getPathsTo(x2); - TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4, x3, x2), Path.toPath(x5, x3, x2)); - - paths = graph.getPathsTo(x6); - TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6), Path.toPath(x5, x4, x6), Path.toPath(x5, x3, x5, x6), - Path.toPath(x5, x4, x5, x6), Path.toPath(x5, x3, x4, x6)); - - Path fast = graph.getFastPathTo(x2); - assertEquals(fast, Path.toPath(x5, x3, x2)); - - } - - @Test - public void testGetRefillPaths2() throws Exception { - LOG.info("Start get refill paths 2 "); - Graph graph = new Graph<>(x5, entrys, 15.1, true, 4); - // x5 <-> x4 <-> x3 - refill -> x2, - // x5 <-> x6 <-> x4 <-refill -> x2 - // x5 <-> x3 <- refill -> x2 - // x5 <-> x4 <- refill -> x6 - - Collection> paths = graph.getPathsTo(x1); - assertTrue(paths.isEmpty()); - - paths = graph.getPathsTo(x2); - TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x3, x4, x2), Path.toPath(x5, x3, x2), - Path.toPath(x5, x4, x3, x2), Path.toPath(x5, x4, x2), Path.toPath(x5, x3, x5, x4, x2), - Path.toPath(x5, x6, x4, x2), Path.toPath(x5, x6, x4, x3, x2), Path.toPath(x5, x4, x3, x4, x2), - Path.toPath(x5, x4, x5, x4, x2), Path.toPath(x5, x6, x5, x4, x2), Path.toPath(x5, x3, x4, x3, x2)); - - paths = graph.getPathsTo(x6); - TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6), Path.toPath(x5, x4, x6), - Path.toPath(x5, x3, x4, x6), Path.toPath(x5, x3, x6), Path.toPath(x5, x4, x3, x6), - Path.toPath(x5, x3, x4, x3, x6), Path.toPath(x5, x3, x4, x5, x6), Path.toPath(x5, x3, x5, x6), - Path.toPath(x5, x3, x5, x4, x6), Path.toPath(x5, x4, x3, x4, x6), Path.toPath(x5, x4, x3, x5, x6), - Path.toPath(x5, x4, x5, x6), Path.toPath(x5, x4, x5, x4, x6)); - - paths = graph.getPathsTo(x7); - assertTrue(paths.isEmpty()); - - Path fast = graph.getFastPathTo(x2); - assertEquals(fast, Path.toPath(x5, x3, x2)); - - } - - @After - public void tearDown() throws Exception { - entrys.clear(); - } -} diff --git a/core/src/test/java/ru/trader/graph/PathRouteTest.java b/core/src/test/java/ru/trader/graph/PathRouteTest.java deleted file mode 100644 index 05c8039..0000000 --- a/core/src/test/java/ru/trader/graph/PathRouteTest.java +++ /dev/null @@ -1,488 +0,0 @@ -package ru.trader.graph; - -import org.junit.Assert; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import ru.trader.TestUtil; -import ru.trader.core.*; -import ru.trader.store.simple.SimpleItem; -import ru.trader.store.simple.SimpleOffer; -import ru.trader.store.simple.SimpleVendor; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; - -public class PathRouteTest extends Assert { - private final static Logger LOG = LoggerFactory.getLogger(PathRouteTest.class); - - private final static Item ITEM1 = new SimpleItem("ITEM1"); - private final static Item ITEM2 = new SimpleItem("ITEM2"); - private final static Item ITEM3 = new SimpleItem("ITEM3"); - private static Vendor v1; - private static Vendor v2; - private static Vendor v3; - private static Vendor v4; - private static Vendor v5; - - private PathRoute initTest1(){ - LOG.info("Init test 1"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,0,0); - - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 100, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 200, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM2, 350, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 400, -1)); - - PathRoute res = new PathRoute(new Vertex<>(v1)); - res = (PathRoute) res.connectTo(new Vertex<>(v2), false); - res.finish(); - res.sort(10000, 5); - return res.getRoot(); - } - - - @Test - public void testPathRoute1() throws Exception { - LOG.info("Start path route test 1"); - PathRoute path = initTest1(); - - assertEquals(1000, path.getProfit(), 0.0001); - assertEquals(1, path.getLandsCount()); - - path = path.getNext(); - Collection orders = path.getOrders(); - - Order order1 = new Order(v1.getSell(ITEM1), v2.getBuy(ITEM1), 5); - Order order2 = new Order(v1.getSell(ITEM2), v2.getBuy(ITEM2), 5); - Order order3 = new Order(v1.getSell(ITEM3), v2.getBuy(ITEM3), 5); - - assertEquals(10000, path.getBalance(), 0.0001); - assertEquals(1000, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order1, order2, order3, PathRoute.TRANSIT); - } - - private PathRoute initTest2(){ - LOG.info("Init test 2"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,0,0); - v3 = new SimpleVendor("v3",0,0,0); - - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 100, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 200, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 300, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM2, 350, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 400, -1)); - - PathRoute res = new PathRoute(new Vertex<>(v1)); - res = (PathRoute) res.connectTo(new Vertex<>(v2), false); - res = (PathRoute) res.connectTo(new Vertex<>(v3), false); - res.finish(); - res.sort(10000, 5); - return res.getRoot(); - } - - @Test - public void testPathRoute2() throws Exception { - LOG.info("Start path route test 2"); - PathRoute path = initTest2(); - assertEquals(1000, path.getProfit(), 0.0001); - assertEquals(1, path.getLandsCount()); - - path = path.getNext(); - Collection orders = path.getOrders(); - - Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5); - Order order2 = new Order(v2.getSell(ITEM2), v3.getBuy(ITEM2), 5); - Order order3 = new Order(v1.getSell(ITEM3), v3.getBuy(ITEM3), 5); - - assertEquals(10000, path.getBalance(), 0.0001); - assertEquals(1000, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order1, PathRoute.TRANSIT, order3); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(10000, path.getBalance(), 0.0001); - assertEquals(750, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order2, PathRoute.TRANSIT); - } - - private PathRoute initTest3(){ - LOG.info("Init test 3"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,0,0); - v3 = new SimpleVendor("v3",0,0,0); - v4 = new SimpleVendor("v4",0,0,0); - - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 100, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 200, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 150, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 320, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 390, -1)); - - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM2, 225, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 200, -1)); - v4.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 450, -1)); - - PathRoute res = new PathRoute(new Vertex<>(v1)); - res = (PathRoute) res.connectTo(new Vertex<>(v2), false); - res = (PathRoute) res.connectTo(new Vertex<>(v3), false); - res = (PathRoute) res.connectTo(new Vertex<>(v4), false); - res.finish(); - res.sort(10000, 5); - return res.getRoot(); - } - - @Test - public void testPathRoute3() throws Exception { - LOG.info("Start path route test 3"); - PathRoute path = initTest3(); - assertEquals(800, path.getProfit(), 0.0001); - assertEquals(2, path.getLandsCount()); - - path = path.getNext(); - Collection orders = path.getOrders(); - - Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5); - Order order2 = new Order(v1.getSell(ITEM2), v2.getBuy(ITEM2), 5); - Order order3 = new Order(v1.getSell(ITEM3), v4.getBuy(ITEM3), 5); - Order order4 = new Order(v2.getSell(ITEM1), v3.getBuy(ITEM1), 5); - Order order5 = new Order(v2.getSell(ITEM3), v4.getBuy(ITEM3), 5); - Order order7 = new Order(v3.getSell(ITEM3), v4.getBuy(ITEM3), 5); - - assertEquals(10000, path.getBalance(), 0.0001); - assertEquals(800, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order1, order2, order3, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(10125, path.getBalance(), 0.0001); - assertEquals(650, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order5, order4, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(10500, path.getBalance(), 0.0001); - assertEquals(300, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order7, PathRoute.TRANSIT); - } - - private PathRoute initTest4(){ - LOG.info("Init test 4"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,0,0); - v3 = new SimpleVendor("v3",0,0,0); - v4 = new SimpleVendor("v4",0,0,0); - v5 = new SimpleVendor("v5",0,0,0); - - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 410, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 200, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 270, -1)); - v4.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 300, -1)); - - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 470, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM2, 300, -1)); - v4.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 370, -1)); - v5.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 400, -1)); - - PathRoute res = new PathRoute(new Vertex<>(v1)); - res = (PathRoute) res.connectTo(new Vertex<>(v2), false); - res = (PathRoute) res.connectTo(new Vertex<>(v3), false); - res = (PathRoute) res.connectTo(new Vertex<>(v4), false); - res = (PathRoute) res.connectTo(new Vertex<>(v5), false); - res.finish(); - res.sort(10000, 5); - return res.getRoot(); - } - - @Test - public void testPathRoute4() throws Exception { - LOG.info("Start path route test 4"); - PathRoute path = initTest4(); - assertEquals(1000, path.getProfit(), 0.0001); - assertEquals(3, path.getLandsCount()); - - path = path.getNext(); - Collection orders = path.getOrders(); - - Order order1 = new Order(v1.getSell(ITEM1), v2.getBuy(ITEM1), 5); - Order order2 = new Order(v1.getSell(ITEM1), v5.getBuy(ITEM1), 5); - Order order3 = new Order(v1.getSell(ITEM2), v3.getBuy(ITEM2), 5); - Order order4 = new Order(v1.getSell(ITEM3), v4.getBuy(ITEM3), 5); - Order order5 = new Order(v2.getSell(ITEM2), v3.getBuy(ITEM2), 5); - Order order6 = new Order(v4.getSell(ITEM1), v5.getBuy(ITEM1), 5); - - - assertEquals(10000, path.getBalance(), 0.0001); - assertEquals(1000, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order3, order1, order4, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(10300, path.getBalance(), 0.0001); - assertEquals(650, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order5, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(10500, path.getBalance(), 0.0001); - assertEquals(500, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(10500, path.getBalance(), 0.0001); - assertEquals(500, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order6, PathRoute.TRANSIT); - } - - private PathRoute initTest5(){ - LOG.info("Init test 5"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,0,0); - v3 = new SimpleVendor("v3",0,0,0); - v4 = new SimpleVendor("v4",0,0,0); - - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 100, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 200, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 150, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 320, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 390, -1)); - - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM2, 225, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 200, -1)); - v4.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 450, -1)); - - PathRoute res = new PathRoute(new Vertex<>(v1)); - res = (PathRoute) res.connectTo(new Vertex<>(v2), false); - res = (PathRoute) res.connectTo(new Vertex<>(v3), false); - res = (PathRoute) res.connectTo(new Vertex<>(v4), false); - res.finish(); - res.sort(500, 5); - return res.getRoot(); - } - - - @Test - public void testPathRoute5() throws Exception { - LOG.info("Start path route test 5"); - PathRoute path = initTest5(); - assertEquals(620, path.getProfit(), 0.0001); - assertEquals(2, path.getLandsCount()); - - path = path.getNext(); - Collection orders = path.getOrders(); - - Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5); - Order order2 = new Order(v1.getSell(ITEM2), v2.getBuy(ITEM2), 2); - Order order3 = new Order(v1.getSell(ITEM3), v4.getBuy(ITEM3), 1); - Order order4 = new Order(v2.getSell(ITEM1), v3.getBuy(ITEM1), 3); - Order order5 = new Order(v2.getSell(ITEM3), v4.getBuy(ITEM3), 1); - Order order7 = new Order(v3.getSell(ITEM3), v4.getBuy(ITEM3), 2); - - assertEquals(500, path.getBalance(), 0.0001); - assertEquals(620, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order1, order2, PathRoute.TRANSIT, order3); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(550, path.getBalance(), 0.0001); - assertEquals(270, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order4, order5, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(1000, path.getBalance(), 0.0001); - assertEquals(120, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order7, PathRoute.TRANSIT); - - } - - private PathRoute initTest6A(){ - LOG.info("Init test 6A"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,1,0); - - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 100, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 200, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 150, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 320, -1)); - - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM2, 225, -1)); - - PathRoute res = new PathRoute(new Vertex<>(v1)); - res = (PathRoute) res.connectTo(new Vertex<>(v2), false); - res.finish(); - res.sort(500, 5); - return res.getRoot(); - } - - private PathRoute initTest6B(double balance){ - LOG.info("Init test 6B"); - v3 = new SimpleVendor("v3",0,1,1); - v4 = new SimpleVendor("v4",1,1,1); - - v3.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 390, -1)); - - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 200, -1)); - v4.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 450, -1)); - - PathRoute res = new PathRoute(new Vertex<>(v2)); - res = (PathRoute) res.connectTo(new Vertex<>(v3), false); - res = (PathRoute) res.connectTo(new Vertex<>(v4), false); - res.finish(); - res.sort(balance, 5); - return res.getRoot(); - } - - - @Test - public void testAddPathRoute() throws Exception { - LOG.info("Start add path route test"); - PathRoute path = initTest6A(); - PathRoute pathB = initTest6B(500); - - path.getEnd().add(pathB, false); - path.sort(500, 5); - path = path.getRoot(); - - assertEquals(620, path.getProfit(), 0.0001); - assertEquals(2, path.getLandsCount()); - assertEquals(3, path.getDistance(), 0.0001); - - path = path.getNext(); - Collection orders = path.getOrders(); - - Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5); - Order order2 = new Order(v1.getSell(ITEM2), v2.getBuy(ITEM2), 2); - Order order3 = new Order(v1.getSell(ITEM3), v4.getBuy(ITEM3), 1); - Order order4 = new Order(v2.getSell(ITEM1), v3.getBuy(ITEM1), 3); - Order order5 = new Order(v2.getSell(ITEM3), v4.getBuy(ITEM3), 1); - Order order7 = new Order(v3.getSell(ITEM3), v4.getBuy(ITEM3), 2); - - assertEquals(500, path.getBalance(), 0.0001); - assertEquals(620, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order1, order2, PathRoute.TRANSIT, order3); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(550, path.getBalance(), 0.0001); - assertEquals(270, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order4, order5, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(1000, path.getBalance(), 0.0001); - assertEquals(120, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order7, PathRoute.TRANSIT); - - } - - @Test - public void testAddPathRouteNoSort() throws Exception { - LOG.info("Start add path route test"); - PathRoute path = initTest6A(); - PathRoute pathB = initTest6B(550); - - path.getEnd().add(pathB, true); - path = path.getRoot(); - - assertEquals(260, path.getProfit(), 0.0001); - assertEquals(3, path.getLandsCount()); - assertEquals(3, path.getDistance(), 0.0001); - - path = path.getNext(); - Collection orders = path.getOrders(); - - Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5); - Order order2 = new Order(v1.getSell(ITEM2), v2.getBuy(ITEM2), 2); - Order order3 = new Order(v1.getSell(ITEM3), v4.getBuy(ITEM3), 1); - Order order4 = new Order(v2.getSell(ITEM1), v3.getBuy(ITEM1), 3); - Order order5 = new Order(v2.getSell(ITEM3), v4.getBuy(ITEM3), 1); - Order order7 = new Order(v3.getSell(ITEM3), v4.getBuy(ITEM3), 1); - - assertEquals(500, path.getBalance(), 0.0001); - assertEquals(260, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order2, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(550, path.getBalance(), 0.0001); - assertEquals(210, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order4, order5, PathRoute.TRANSIT); - - path = path.getNext(); - orders = path.getOrders(); - - assertEquals(700, path.getBalance(), 0.0001); - assertEquals(60, path.getProfit(), 0.0001); - TestUtil.assertCollectionEquals(orders, order7, PathRoute.TRANSIT); - - } - - @Test - public void testEntries() throws Exception { - LOG.info("Start test get entries"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,0,0); - v3 = new SimpleVendor("v3",0,0,0); - v4 = new SimpleVendor("v4",0,0,0); - - PathRoute path = new PathRoute(new Vertex<>(v1)); - path = (PathRoute) path.connectTo(new Vertex<>(v2), false); - path = (PathRoute) path.connectTo(new Vertex<>(v3), false); - path.finish(); - TestUtil.assertCollectionContainAll(path.getEntries(), v1, v2, v3); - } - - @Test - public void testContains() throws Exception { - LOG.info("Start test get entries"); - v1 = new SimpleVendor("v1",0,0,0); - v2 = new SimpleVendor("v2",0,0,0); - v3 = new SimpleVendor("v3",0,0,0); - v4 = new SimpleVendor("v4",0,0,0); - - PathRoute path = new PathRoute(new Vertex<>(v1)); - path = (PathRoute) path.connectTo(new Vertex<>(v2), false); - path = (PathRoute) path.connectTo(new Vertex<>(v3), false); - path.finish(); - Collection vendors = new ArrayList<>(); - Collections.addAll(vendors, v1, v2, v3); - assertTrue(path.contains(vendors)); - vendors.clear(); - Collections.addAll(vendors, v2); - assertTrue(path.contains(vendors)); - vendors.clear(); - Collections.addAll(vendors, v4); - assertFalse(path.contains(vendors)); - vendors.clear(); - Collections.addAll(vendors, v3, v2, v4, v1); - assertFalse(path.contains(vendors)); - vendors.clear(); - Collections.addAll(vendors, v1, v2, v3, v4); - assertFalse(path.contains(vendors)); - - } - -} \ No newline at end of file diff --git a/core/src/test/java/ru/trader/graph/RouteGraphTest.java b/core/src/test/java/ru/trader/graph/RouteGraphTest.java deleted file mode 100644 index 8ab0426..0000000 --- a/core/src/test/java/ru/trader/graph/RouteGraphTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package ru.trader.graph; - -import org.junit.Assert; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import ru.trader.core.*; -import ru.trader.store.simple.*; - -import java.util.ArrayList; - -public class RouteGraphTest extends Assert { - private final static Logger LOG = LoggerFactory.getLogger(RouteGraphTest.class); - private final static Market market = new SimpleMarket(); - private final static Item ITEM1 = new SimpleItem("ITEM1"); - private final static Item ITEM2 = new SimpleItem("ITEM2"); - private final static Item ITEM3 = new SimpleItem("ITEM3"); - private final static Item ITEM4 = new SimpleItem("ITEM4"); - private final static Item ITEM5 = new SimpleItem("ITEM5"); - private static Vendor v1; - private static Vendor v2; - private static Vendor v3; - private static Vendor v4; - private static Vendor v5; - private static Vendor v6; - private static Place p1; - private static Place p2; - private static Place p3; - private static Place p4; - private static Place p5; - - - static { - p1 = new SimplePlace("v1"); - p2 = new SimplePlace("v2"); - p3 = new SimplePlace("v3"); - p4 = new SimplePlace("v4"); - p5 = new SimplePlace("p5",5,5,5); - - v1 = new SimpleVendor("v1"); - v2 = new SimpleVendor("v2"); - v3 = new SimpleVendor("v3"); - v4 = new SimpleVendor("v4"); - v5 = new SimpleVendor("v5"); - v6 = new SimpleVendor("v6"); - - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 100, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM2, 200, -1)); - v1.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 300, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM1, 150, -1)); - v2.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 320, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM3, 390, -1)); - v5.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM4, 100, -1)); - v6.add(new SimpleOffer(OFFER_TYPE.SELL, ITEM5, 100, -1)); - - v2.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM2, 225, -1)); - v3.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM1, 200, -1)); - v4.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM3, 450, -1)); - v5.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM5, 500, -1)); - v6.add(new SimpleOffer(OFFER_TYPE.BUY, ITEM4, 500, -1)); - - p1.add(v1);p2.add(v2);p3.add(v3);p4.add(v4);p5.add(v5);p5.add(v6); - market.add(p1);market.add(p2);market.add(p3);market.add(p4);market.add(p5); - } - - @Test - public void testRoutes() throws Exception { - RouteGraph graph = new RouteGraph(v1, market.getVendors(true), 1, 1, true, 4); - graph.setBalance(500); - 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 - ArrayList> routes = (ArrayList>) graph.getPathsTo(v4, 5); - assertEquals(5, routes.size()); - - PathRoute path = (PathRoute) routes.get(0).getRoot(); - assertEquals(950, path.getProfit(), 0.001); - assertEquals(3, path.getLandsCount()); - - path = (PathRoute) routes.get(1).getRoot(); - assertEquals(950, path.getProfit(), 0.001); - assertEquals(3, path.getLandsCount()); - - path = (PathRoute) routes.get(2).getRoot(); - assertEquals(950, path.getProfit(), 0.001); - assertEquals(3, path.getLandsCount()); - - path = (PathRoute) routes.get(3).getRoot(); - assertEquals(620, path.getProfit(), 0.001); - assertEquals(2, path.getLandsCount()); - - path = (PathRoute) routes.get(4).getRoot(); - assertEquals(620, path.getProfit(), 0.001); - assertEquals(2, path.getLandsCount()); - } - - @Test - public void testRoutes2() throws Exception { - RouteGraph graph = new RouteGraph(v5, market.getVendors(true), 1, 15, true, 4); - graph.setBalance(500); - graph.setCargo(5); - ArrayList> routes = (ArrayList>) graph.getPathsTo(v1, 5); - assertEquals(5, routes.size()); - - //v5 -> v6 -> v5 -> v6 -> v1 - PathRoute path = (PathRoute) routes.get(0).getRoot(); - assertEquals(6000, path.getProfit(), 0.001); - assertEquals(4, path.getLandsCount()); - - } - -} diff --git a/core/src/test/java/ru/trader/graph/TopListTest.java b/core/src/test/java/ru/trader/graph/TopListTest.java deleted file mode 100644 index 4c45ca7..0000000 --- a/core/src/test/java/ru/trader/graph/TopListTest.java +++ /dev/null @@ -1,87 +0,0 @@ -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 getGroup = (o1) -> Math.floorDiv(o1, 10); - - private static final Comparator 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 list, Integer entry){ - TopList.addToGroupTop(list, entry, 10, groupcomp, getGroup, 1); - } - - @Test - public void testAddToGroup() throws Exception { - ArrayList 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 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 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()); - } - -} diff --git a/core/src/test/java/ru/trader/graph/VertexTest.java b/core/src/test/java/ru/trader/graph/VertexTest.java deleted file mode 100644 index 5532749..0000000 --- a/core/src/test/java/ru/trader/graph/VertexTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package ru.trader.graph; - -import org.junit.Assert; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class VertexTest extends Assert { - private final static Logger LOG = LoggerFactory.getLogger(VertexTest.class); - - private final static Point x1 = new Point("x1",1); - private final static Point x2 = new Point("x2",4); - private final static Point x3 = new Point("x3",-2); - private final static Point x4 = new Point("x4",5); - - - @Test - public void testContains() throws Exception { - LOG.info("Start vertex contains test"); - - Vertex vertex = new Vertex<>(x1); - vertex.addEdge(new Edge<>(vertex, new Vertex<>(x2))); - vertex.addEdge(new Edge<>(vertex, x3)); - - assertFalse("Vertex must contains entry",vertex.isConnected(x1)); - assertTrue("Vertex must contains entry",vertex.isConnected(new Vertex<>(x3))); - assertTrue("Vertex must contains entry",vertex.isConnected(x2)); - assertFalse("Vertex not must contains entry", vertex.isConnected(new Vertex<>(x4))); - } -} diff --git a/core/src/test/resources/log4j.properties b/core/src/test/resources/log4j.properties index 65da310..e94868c 100644 --- a/core/src/test/resources/log4j.properties +++ b/core/src/test/resources/log4j.properties @@ -4,4 +4,4 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%p: %d{dd.MM.yyyy HH:mm:ss} (%F:%L) - %m%n -#log4j.logger.ru.trader.analysis.graph.Crawler = TRACE +log4j.logger.ru.trader.analysis.graph = TRACE