implement route searcher
This commit is contained in:
@@ -25,6 +25,15 @@ public class FilteredMarket {
|
||||
.filter(v -> !filter.isFiltered(v));
|
||||
}
|
||||
|
||||
public Stream<Vendor> 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<Item> getItems(){
|
||||
return market.getItems();
|
||||
}
|
||||
|
||||
@@ -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<VendorsGraph.VendorsEdge> 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<RouteEntry> entries) {
|
||||
this.entries = new ArrayList<>(entries);
|
||||
updateStats();
|
||||
}
|
||||
|
||||
public List<RouteEntry> 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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Order> 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)":"");
|
||||
}
|
||||
}
|
||||
|
||||
104
core/src/main/java/ru/trader/analysis/RouteSearcher.java
Normal file
104
core/src/main/java/ru/trader/analysis/RouteSearcher.java
Normal file
@@ -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<Route> getRoutes(Vendor from, Vendor to, Collection<Vendor> vendors){
|
||||
return search(from, to, vendors);
|
||||
}
|
||||
|
||||
public List<Route> getRoutes(Vendor from, Collection<Vendor> vendors){
|
||||
return search(from, null, vendors);
|
||||
}
|
||||
|
||||
private List<Route> search(Vendor source, Vendor target, Collection<Vendor> vendors){
|
||||
LOG.trace("Start search route to {} from {}", source, target);
|
||||
vGraph.build(source, vendors);
|
||||
|
||||
RouteCollector collector = new RouteCollector();
|
||||
Crawler<Vendor> 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<Route> routes = new ArrayList<>();
|
||||
|
||||
public void add(List<Edge<Vendor>> edges){
|
||||
Route route = toRoute(edges);
|
||||
if (route.getProfit() > 0) {
|
||||
routes.add(route);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Route> get() {
|
||||
return routes;
|
||||
}
|
||||
|
||||
private Route toRoute(List<Edge<Vendor>> edges){
|
||||
List<RouteEntry> 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<Order> 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<Order> 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
146
core/src/main/java/ru/trader/analysis/VendorsGraph.java
Normal file
146
core/src/main/java/ru/trader/analysis/VendorsGraph.java
Normal file
@@ -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<Vendor> {
|
||||
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<List<Edge<Vendor>>> onFoundFunc){
|
||||
return new VendorsCrawler(onFoundFunc);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GraphBuilder createGraphBuilder(Vertex<Vendor> vertex, Collection<Vendor> set, int deep, double limit) {
|
||||
return new VendorsGraphBuilder(vertex, set, deep, limit);
|
||||
}
|
||||
|
||||
protected class VendorsGraphBuilder extends ConnectibleGraphBuilder {
|
||||
protected VendorsGraphBuilder(Vertex<Vendor> vertex, Collection<Vendor> 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<Vendor> createEdge(Vertex<Vendor> target) {
|
||||
return new VendorsEdge(vertex, target, refill, fuelCost, false);
|
||||
}
|
||||
}
|
||||
|
||||
public class VendorsEdge extends ConnectibleEdge<Vendor> {
|
||||
private List<Order> orders;
|
||||
private boolean isTarget;
|
||||
|
||||
protected VendorsEdge(Vertex<Vendor> source, Vertex<Vendor> target, boolean refill, double fuel, boolean isTarget) {
|
||||
super(source, target, refill, fuel);
|
||||
this.isTarget = isTarget;
|
||||
}
|
||||
|
||||
protected void setOrders(List<Order> orders){
|
||||
this.orders = orders;
|
||||
}
|
||||
|
||||
public double getProfit(){
|
||||
return getOrders().stream().mapToDouble(Order::getProfit).sum();
|
||||
}
|
||||
|
||||
public List<Order> 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<Vendor> {
|
||||
protected VendorsCrawler(Consumer<List<Edge<Vendor>>> onFoundFunc) {
|
||||
super(VendorsGraph.this, onFoundFunc);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CostTraversalEntry start(Vertex<Vendor> vertex) {
|
||||
double balance = scorer.getProfile().getBalance();
|
||||
return new VendorsTraversalEntry((CCostTraversalEntry) super.start(vertex), balance);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CostTraversalEntry travers(CostTraversalEntry entry, List<Edge<Vendor>> head, Edge<Vendor> edge, Vendor target) {
|
||||
VendorsTraversalEntry ve = (VendorsTraversalEntry)entry;
|
||||
double balance = ve.balance;
|
||||
Vendor buyer = edge.getTarget().getEntry();
|
||||
List<Order> 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<Vendor> cedge = (ConnectibleEdge<Vendor>) 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<Edge<Vendor>> head, Edge<Vendor> edge, double balance) {
|
||||
super(head, edge, entry.getWeight(), entry.getFuel());
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -27,35 +27,13 @@ public class CCrawler<T extends Connectable<T>> extends Crawler<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Crawler<T>.TraversalEntry start(Vertex<T> vertex) {
|
||||
double fuel = getShip().getTank();
|
||||
return new CTraversalEntry(new ArrayList<>(), vertex, fuel);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Crawler<T>.CostTraversalEntry costStart(Vertex<T> vertex) {
|
||||
protected CostTraversalEntry start(Vertex<T> vertex) {
|
||||
double fuel = getShip().getTank();
|
||||
return new CCostTraversalEntry(new ArrayList<>(), vertex, fuel);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Crawler<T>.TraversalEntry travers(TraversalEntry entry, Edge<T> 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<T>.CostTraversalEntry costTravers(Crawler<T>.CostTraversalEntry entry, List<Edge<T>> head, Edge<T> edge) {
|
||||
protected CostTraversalEntry travers(CostTraversalEntry entry, List<Edge<T>> head, Edge<T> 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<T extends Connectable<T>> extends Crawler<T> {
|
||||
return new CCostTraversalEntry(head, edge, entry.getWeight(), nextLimit);
|
||||
}
|
||||
|
||||
protected class CTraversalEntry extends TraversalEntry {
|
||||
private final double fuel;
|
||||
|
||||
protected CTraversalEntry(List<Edge<T>> head, Vertex<T> vertex, double fuel) {
|
||||
super(head, vertex);
|
||||
this.fuel = fuel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterator<Edge<T>> getIteratorInstance() {
|
||||
return vertex.getEdges().stream().filter(e -> {
|
||||
ConnectibleEdge<T> edge = (ConnectibleEdge<T>) 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<T extends Connectable<T>> extends Crawler<T> {
|
||||
this.fuel = fuel;
|
||||
}
|
||||
|
||||
public double getFuel() {
|
||||
return fuel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterator<Edge<T>> getIteratorInstance() {
|
||||
return vertex.getEdges().stream().filter(e -> {
|
||||
|
||||
@@ -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<T> {
|
||||
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<T> graph;
|
||||
@@ -61,32 +58,20 @@ public class Crawler<T> {
|
||||
Vertex<T> 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<T> vertex){
|
||||
return new TraversalEntry(new ArrayList<>(), vertex);
|
||||
}
|
||||
|
||||
protected CostTraversalEntry costStart(Vertex<T> vertex){
|
||||
protected CostTraversalEntry start(Vertex<T> vertex){
|
||||
return new CostTraversalEntry(new ArrayList<>(), vertex);
|
||||
}
|
||||
|
||||
protected TraversalEntry travers(TraversalEntry entry, Edge<T> edge){
|
||||
return new TraversalEntry(getCopyList(entry.head, edge), edge.getTarget());
|
||||
}
|
||||
|
||||
private CostTraversalEntry costTravers(CostTraversalEntry entry, Edge<T> edge){
|
||||
return costTravers(entry, getCopyList(entry.head, edge), edge);
|
||||
}
|
||||
|
||||
protected CostTraversalEntry costTravers(CostTraversalEntry entry, List<Edge<T>> head, Edge<T> edge){
|
||||
protected CostTraversalEntry travers(CostTraversalEntry entry, List<Edge<T>> head, Edge<T> 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<Edge<T>> head = entry.head;
|
||||
Vertex<T> source = entry.vertex;
|
||||
@@ -107,7 +92,7 @@ public class Crawler<T> {
|
||||
LOG.trace("Search around");
|
||||
for (Edge<T> 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<T> {
|
||||
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<TraversalEntry> queue = new LinkedList<>();
|
||||
LinkedList<CostTraversalEntry> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty() && count > found){
|
||||
TraversalEntry entry = queue.poll();
|
||||
CostTraversalEntry entry = queue.poll();
|
||||
List<Edge<T>> head = entry.head;
|
||||
Vertex<T> source = entry.vertex;
|
||||
if (head.size() >= maxSize){
|
||||
@@ -142,7 +127,7 @@ public class Crawler<T> {
|
||||
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<T> {
|
||||
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<T> {
|
||||
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<T> {
|
||||
this.vertex = vertex;
|
||||
}
|
||||
|
||||
public List<Edge<T>> getHead() {
|
||||
return head;
|
||||
}
|
||||
|
||||
public Vertex<T> getVertex() {
|
||||
return vertex;
|
||||
}
|
||||
|
||||
public Iterator<Edge<T>> iterator(){
|
||||
if (iterator == null){
|
||||
iterator = getIteratorInstance();
|
||||
@@ -247,7 +240,7 @@ public class Crawler<T> {
|
||||
return vertex.getEdges().iterator();
|
||||
}
|
||||
|
||||
private Iterable<Edge<T>> getEdges(){
|
||||
protected Iterable<Edge<T>> getEdges(){
|
||||
return this::iterator;
|
||||
}
|
||||
}
|
||||
@@ -269,13 +262,17 @@ public class Crawler<T> {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
protected double getWeight(){
|
||||
public double getWeight(){
|
||||
if (weight == null){
|
||||
weight = cost + (edge !=null ? edge.getWeight() : 0);
|
||||
}
|
||||
return weight;
|
||||
}
|
||||
|
||||
public Edge<T> getEdge() {
|
||||
return edge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull CostTraversalEntry other) {
|
||||
int cmp = Double.compare(getWeight(), other.getWeight());
|
||||
@@ -283,69 +280,4 @@ public class Crawler<T> {
|
||||
return Integer.compare(head.size(), other.head.size());
|
||||
}
|
||||
}
|
||||
/*
|
||||
private class PathFinder extends RecursiveAction {
|
||||
private final TopList<Path<T>> paths;
|
||||
private final Path<T> head;
|
||||
private final Vertex<T> target;
|
||||
|
||||
private PathFinder(TopList<Path<T>> paths, Path<T> head, Vertex<T> target) {
|
||||
this.paths = paths;
|
||||
this.head = head;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void compute() {
|
||||
if (target == null || isCancelled()) return;
|
||||
Vertex<T> source = head.getTarget();
|
||||
LOG.trace("Find path to deep from {} to {}, head {}", source, target, head);
|
||||
Edge<T> edge = source.getEdge(target);
|
||||
if (edge != null){
|
||||
Path<T> 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<PathFinder> subTasks = new ArrayList<>(source.getEdges().size());
|
||||
Iterator<Edge<T>> iterator = source.getEdges().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Edge<T> next = iterator.next();
|
||||
if (isDone() || callback.isCancel()) break;
|
||||
// target already added if source consist edge
|
||||
if (next.isConnect(target)) continue;
|
||||
Path<T> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
24
core/src/main/java/ru/trader/analysis/graph/Path.java
Normal file
24
core/src/main/java/ru/trader/analysis/graph/Path.java
Normal file
@@ -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<T extends Connectable<T>> {
|
||||
private final List<PathEntry<T>> entries;
|
||||
|
||||
public Path(List<ConnectibleEdge<T>> edges) {
|
||||
entries = new ArrayList<>(edges.size());
|
||||
for (int i = 0; i < edges.size(); i++) {
|
||||
ConnectibleEdge<T> 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<T> get(int index){
|
||||
return entries.get(index);
|
||||
}
|
||||
|
||||
}
|
||||
19
core/src/main/java/ru/trader/analysis/graph/PathEntry.java
Normal file
19
core/src/main/java/ru/trader/analysis/graph/PathEntry.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package ru.trader.analysis.graph;
|
||||
|
||||
public class PathEntry<T> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,11 @@ public class Vertex<T> {
|
||||
public void connect(Edge<T> edge){
|
||||
assert this == edge.getSource();
|
||||
synchronized (edges){
|
||||
if (!edges.contains(edge)){
|
||||
edges.add(edge);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<Edge<T>> getEdges() {
|
||||
return edges;
|
||||
|
||||
@@ -17,6 +17,17 @@ public interface Place extends Connectable<Place> {
|
||||
void setPosition(double x, double y, double z);
|
||||
|
||||
Collection<Vendor> get();
|
||||
default Collection<Vendor> get(boolean withTransit){
|
||||
if (withTransit){
|
||||
Collection<Vendor> vendors = new ArrayList<>();
|
||||
vendors.add(new TransitVendor(this));
|
||||
vendors.addAll(get());
|
||||
return vendors;
|
||||
} else {
|
||||
return get();
|
||||
}
|
||||
}
|
||||
|
||||
default Vendor get(String name){
|
||||
Optional<Vendor> vendor = get().stream().filter(p -> name.equalsIgnoreCase(p.getName())).findFirst();
|
||||
return vendor.isPresent() ? vendor.get() : null;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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= {
|
||||
|
||||
113
core/src/main/java/ru/trader/core/TransitVendor.java
Normal file
113
core/src/main/java/ru/trader/core/TransitVendor.java
Normal file
@@ -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<SERVICE_TYPE> 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<Offer> 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<Vendor> 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;
|
||||
}
|
||||
}
|
||||
@@ -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<Vendor> {
|
||||
@@ -65,114 +62,4 @@ public class VendorsIterator implements Iterator<Vendor> {
|
||||
}
|
||||
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<SERVICE_TYPE> 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<Offer> 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<Vendor> 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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
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<Path<Vendor>> epaths = graph.getPathsTo(ithaca, 10);
|
||||
PathRoute expect = epaths.stream().map(p -> (PathRoute) p).findFirst().get();
|
||||
|
||||
List<PathRoute> apaths = searcher.getPaths(ithaca, ithaca, market.getVendors(true), 6, 6000000, 440, 10);
|
||||
PathRoute actual = apaths.stream().findFirst().get();
|
||||
assertTrue("Routes is different",expect.isRoute(actual));
|
||||
List<Route> 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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
}
|
||||
@@ -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<Vendor> vendors = new ArrayList<>();
|
||||
Collections.addAll(vendors, v1, v2, v3);
|
||||
assertTrue(path.contains(vendors));
|
||||
|
||||
78
core/src/test/java/ru/trader/analysis/VendorsGraphTest.java
Normal file
78
core/src/test/java/ru/trader/analysis/VendorsGraphTest.java
Normal file
@@ -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<Vendor> paths = new SimpleCollector<>();
|
||||
Crawler<Vendor> crawler = vGraph.crawler(paths::add);
|
||||
|
||||
crawler.findMin(cabreraDock, 100);
|
||||
assertEquals(100, paths.get().size());
|
||||
paths.clear();
|
||||
|
||||
Vertex<Vendor> x = vGraph.getRoot();
|
||||
assertNotNull(x);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
world = null;
|
||||
fWorld = null;
|
||||
}
|
||||
}
|
||||
@@ -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<Point> paths = new SimpleCollector<>();
|
||||
Crawler<Point> 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<Point> paths = new SimpleCollector<>();
|
||||
Crawler<Point> crawler = new CCrawler<>(graph, paths::add);
|
||||
|
||||
crawler.findMin(x8, 10);
|
||||
@@ -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<Point> paths = new SimpleCollector<>();
|
||||
Crawler<Point> 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<Point> paths = new SimpleCollector<>();
|
||||
Crawler<Point> 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<List<Edge<Point>>> paths = new ArrayList<>();
|
||||
|
||||
public void add(List<Edge<Point>> path){
|
||||
paths.add(path);
|
||||
}
|
||||
|
||||
public List<List<Edge<Point>>> get() {
|
||||
return paths;
|
||||
}
|
||||
|
||||
public List<Edge<Point>> 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<Double> getWeights(){
|
||||
return paths.stream().map(p -> p.stream().mapToDouble(Edge::getWeight).sum()).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<T> {
|
||||
private List<List<Edge<T>>> paths = new ArrayList<>();
|
||||
|
||||
public void add(List<Edge<T>> path){
|
||||
paths.add(path);
|
||||
}
|
||||
|
||||
public List<List<Edge<T>>> get() {
|
||||
return paths;
|
||||
}
|
||||
|
||||
public List<Edge<T>> 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<Double> getWeights(){
|
||||
return paths.stream().map(p -> p.stream().mapToDouble(Edge::getWeight).sum()).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -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<Point> 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<Point> 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<Point> 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<Point> 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<Point> 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<Point> 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<Point> 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<Point> 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<Point> 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<Point> 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<Point> 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<Point> graph = new Graph<>(x5, entrys, 5.1, 2);
|
||||
// x5 <-> x4, x5 <-> x6
|
||||
|
||||
Collection<Path<Point>> 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<Point> 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<Path<Point>> 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<Point> 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<Point> 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<Path<Point>> 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<Point> 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<Point> 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<Path<Point>> 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<Point> fast = graph.getFastPathTo(x2);
|
||||
assertEquals(fast, Path.toPath(x5, x3, x2));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
entrys.clear();
|
||||
}
|
||||
}
|
||||
@@ -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<Order> 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<Order> 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<Order> 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<Order> 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<Order> 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<Order> 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<Order> 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<Vendor> 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));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Path<Vendor>> routes = (ArrayList<Path<Vendor>>) 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<Path<Vendor>> routes = (ArrayList<Path<Vendor>>) 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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Integer, Integer> getGroup = (o1) -> Math.floorDiv(o1, 10);
|
||||
|
||||
private static final Comparator<Integer> groupcomp = (o1, o2) -> {
|
||||
int cmp = Integer.compare(getGroup.apply(o1), getGroup.apply(o2));
|
||||
if (cmp !=0 ) return cmp;
|
||||
return o1.compareTo(o2);
|
||||
};
|
||||
|
||||
private void add(List<Integer> list, Integer entry){
|
||||
TopList.addToGroupTop(list, entry, 10, groupcomp, getGroup, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToGroup() throws Exception {
|
||||
ArrayList<Integer> top = new ArrayList<>(10);
|
||||
add(top, 5);
|
||||
add(top, 15);
|
||||
add(top, 22);
|
||||
add(top, 34);
|
||||
add(top, 36);
|
||||
add(top, 21);
|
||||
add(top, 7);
|
||||
add(top, 6);
|
||||
add(top, 3);
|
||||
|
||||
assertEquals(4, top.size());
|
||||
assertEquals(3, top.get(0).intValue());
|
||||
assertEquals(15, top.get(1).intValue());
|
||||
assertEquals(21, top.get(2).intValue());
|
||||
assertEquals(34, top.get(3).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToGroup2() throws Exception {
|
||||
ArrayList<Integer> top = new ArrayList<>(10);
|
||||
add(top, 36);
|
||||
add(top, 15);
|
||||
add(top, 22);
|
||||
add(top, 6);
|
||||
add(top, 34);
|
||||
add(top, 5);
|
||||
add(top, 21);
|
||||
add(top, 7);
|
||||
add(top, 3);
|
||||
|
||||
assertEquals(4, top.size());
|
||||
assertEquals(3, top.get(0).intValue());
|
||||
assertEquals(15, top.get(1).intValue());
|
||||
assertEquals(21, top.get(2).intValue());
|
||||
assertEquals(34, top.get(3).intValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAddToGroup3() throws Exception {
|
||||
ArrayList<Integer> top = new ArrayList<>(10);
|
||||
add(top, 21);
|
||||
add(top, 34);
|
||||
add(top, 36);
|
||||
add(top, 3);
|
||||
add(top, 15);
|
||||
add(top, 22);
|
||||
add(top, 6);
|
||||
add(top, 34);
|
||||
add(top, 5);
|
||||
add(top, 7);
|
||||
|
||||
assertEquals(4, top.size());
|
||||
assertEquals(3, top.get(0).intValue());
|
||||
assertEquals(15, top.get(1).intValue());
|
||||
assertEquals(21, top.get(2).intValue());
|
||||
assertEquals(34, top.get(3).intValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Point> 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)));
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user