- fix lands count
- fix memory leak on big jumps count - fix remove order from route - add path adding
This commit is contained in:
@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
|
||||
import ru.trader.graph.Graph;
|
||||
import ru.trader.graph.Path;
|
||||
import ru.trader.graph.PathRoute;
|
||||
import ru.trader.graph.RouteGraph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -15,11 +16,11 @@ public class MarketAnalyzer {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(MarketAnalyzer.class);
|
||||
|
||||
private Market market;
|
||||
private Graph<Vendor> graph;
|
||||
private RouteGraph graph;
|
||||
private double tank;
|
||||
private double maxDistance;
|
||||
private int jumps;
|
||||
private long cargo;
|
||||
private int cargo;
|
||||
|
||||
|
||||
public MarketAnalyzer(Market market) {
|
||||
@@ -31,6 +32,7 @@ public class MarketAnalyzer {
|
||||
TreeSet<Order> top = new TreeSet<>();
|
||||
for (Vendor vendor : market.get()) {
|
||||
LOG.trace("Check vendor {}", vendor);
|
||||
setSource(vendor);
|
||||
for (Offer sell : vendor.getAllSellOffers()) {
|
||||
long count = Math.min(cargo, (long) Math.floor(balance / sell.getPrice()));
|
||||
LOG.trace("Sell offer {}, count = {}", sell, count);
|
||||
@@ -38,6 +40,10 @@ public class MarketAnalyzer {
|
||||
Iterator<Offer> buyers = market.getStatBuy(sell.getItem()).getOffers().descendingIterator();
|
||||
while (buyers.hasNext()){
|
||||
Offer buy = buyers.next();
|
||||
if (!graph.isAccessible(buy.getVendor())){
|
||||
LOG.trace("Is inaccessible buyer, skip");
|
||||
continue;
|
||||
}
|
||||
Order order = new Order(sell, buy, count);
|
||||
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
|
||||
if (order.getProfit() <= 0 ) break;
|
||||
@@ -62,6 +68,7 @@ public class MarketAnalyzer {
|
||||
|
||||
public Collection<Order> getOrders(Vendor vendor, double balance) {
|
||||
Collection<Order> res = new ArrayList<>();
|
||||
setSource(vendor);
|
||||
for (Offer sell : vendor.getAllSellOffers()) {
|
||||
long count = Math.min(cargo, (long) Math.floor(balance / sell.getPrice()));
|
||||
LOG.trace("Sell offer {}, count = {}", sell, count);
|
||||
@@ -69,6 +76,10 @@ public class MarketAnalyzer {
|
||||
Iterator<Offer> buyers = market.getStatBuy(sell.getItem()).getOffers().descendingIterator();
|
||||
while (buyers.hasNext()){
|
||||
Offer buy = buyers.next();
|
||||
if (!graph.isAccessible(buy.getVendor())){
|
||||
LOG.trace("Is inaccessible buyer, skip");
|
||||
continue;
|
||||
}
|
||||
Order order = new Order(sell, buy, count);
|
||||
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
|
||||
res.add(order);
|
||||
@@ -79,6 +90,11 @@ public class MarketAnalyzer {
|
||||
|
||||
public Collection<Order> getOrders(Vendor from, Vendor to, double balance) {
|
||||
Collection<Order> res = new ArrayList<>();
|
||||
setSource(from);
|
||||
if (!graph.isAccessible(to)){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return res;
|
||||
}
|
||||
for (Offer sell : from.getAllSellOffers()) {
|
||||
long count = Math.min(cargo, (long) Math.floor(balance / sell.getPrice()));
|
||||
LOG.trace("Sell offer {}, count = {}", sell, count);
|
||||
@@ -97,7 +113,8 @@ public class MarketAnalyzer {
|
||||
|
||||
|
||||
private void rebuild(Vendor source){
|
||||
graph = new Graph<>(source, market.get(), tank, maxDistance, true, jumps, PathRoute::new);
|
||||
graph = new RouteGraph(source, market.get(), tank, maxDistance, true, jumps);
|
||||
graph.setLimit(cargo);
|
||||
}
|
||||
|
||||
private void setSource(Vendor source){
|
||||
@@ -107,42 +124,46 @@ public class MarketAnalyzer {
|
||||
|
||||
public Collection<Path<Vendor>> getPaths(Vendor from, Vendor to){
|
||||
setSource(from);
|
||||
return graph.getPathsTo(to, true);
|
||||
return graph.getPathsTo(to);
|
||||
}
|
||||
|
||||
public Path<Vendor> getPath(Vendor from, Vendor to){
|
||||
public PathRoute getPath(Vendor from, Vendor to){
|
||||
setSource(from);
|
||||
return graph.getFastPathTo(to);
|
||||
return (PathRoute) graph.getFastPathTo(to);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, double balance){
|
||||
setSource(from);
|
||||
graph.setBalance(balance);
|
||||
Collection<PathRoute> res = new ArrayList<>();
|
||||
for (Vendor vendor : market.get()) {
|
||||
PathRoute path = (PathRoute) getPath(from, vendor);
|
||||
if (path == null) continue;
|
||||
path.sort(balance, cargo);
|
||||
res.add(path);
|
||||
Collection<Path<Vendor>> paths = graph.getPathsTo(vendor, 10);
|
||||
for (Path<Vendor> path : paths) {
|
||||
res.add((PathRoute) path);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, Vendor to, double balance){
|
||||
Collection<Path<Vendor>> paths = getPaths(from, to);
|
||||
setSource(from);
|
||||
graph.setBalance(balance);
|
||||
Collection<Path<Vendor>> paths = graph.getPathsTo(to);
|
||||
Collection<PathRoute> res = new ArrayList<>(paths.size());
|
||||
for (Path<Vendor> path : paths) {
|
||||
PathRoute p = (PathRoute) path;
|
||||
p.sort(balance, cargo);
|
||||
res.add(p);
|
||||
res.add((PathRoute) path);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getTopPaths(int limit, double balance){
|
||||
TreeSet<PathRoute> top = new TreeSet<>((p1, p2) -> Double.compare(p2.getProfit(), p1.getProfit()));
|
||||
TreeSet<PathRoute> top = new TreeSet<>((p1, p2) -> Double.compare(p2.getProfit()/p2.getLandsCount(), p1.getProfit()/p1.getLandsCount()));
|
||||
for (Vendor vendor : market.get()) {
|
||||
Collection<PathRoute> paths = getPaths(vendor, vendor, balance);
|
||||
for (PathRoute path : paths) {
|
||||
top.add(path);
|
||||
setSource(vendor);
|
||||
graph.setBalance(balance);
|
||||
Collection<Path<Vendor>> paths = graph.getPathsTo(vendor, 10);
|
||||
for (Path<Vendor> path : paths) {
|
||||
top.add((PathRoute) path);
|
||||
if (top.size() > limit) {
|
||||
top.pollLast();
|
||||
}
|
||||
@@ -167,12 +188,10 @@ public class MarketAnalyzer {
|
||||
this.graph = null;
|
||||
}
|
||||
|
||||
public void setCargo(long cargo) {
|
||||
public void setCargo(int cargo) {
|
||||
if (graph != null) graph.setLimit(cargo);
|
||||
this.cargo = cargo;
|
||||
}
|
||||
|
||||
public long getCargo() {
|
||||
return cargo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -82,6 +82,10 @@ public class Order implements Comparable<Order> {
|
||||
|
||||
}
|
||||
|
||||
public boolean equalsIgnoreCount(Order order) {
|
||||
return buy.equals(order.buy) && sell.equals(order.sell);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result;
|
||||
|
||||
@@ -97,29 +97,32 @@ public class Graph<T extends Connectable<T>> {
|
||||
return vertexes.get(entry);
|
||||
}
|
||||
|
||||
public Collection<Path<T>> getPathsTo(T entry, boolean all){
|
||||
public Collection<Path<T>> getPathsTo(T entry){
|
||||
return getPathsTo(entry, 200);
|
||||
}
|
||||
|
||||
public Collection<Path<T>> getPathsTo(T entry, int max){
|
||||
Vertex<T> target = getVertex(entry);
|
||||
return findPaths(pathFabric.build(root), target, root.getLevel()-1, stock, all);
|
||||
ArrayList<Path<T>> paths = new ArrayList<>(max);
|
||||
findPaths(paths, max, pathFabric.build(root), target, root.getLevel()-1, stock);
|
||||
return paths;
|
||||
}
|
||||
|
||||
|
||||
private Collection<Path<T>> findPaths(Path<T> head, Vertex<T> target, int deep, double limit, boolean all){
|
||||
Collection<Path<T>> paths = new ArrayList<>();
|
||||
if (target == null) return paths;
|
||||
boolean found =false;
|
||||
private boolean findPaths(ArrayList<Path<T>> paths, int max, Path<T> head, Vertex<T> target, int deep, double limit){
|
||||
if (target == null) return true;
|
||||
Vertex<T> source = head.getTarget();
|
||||
LOG.trace("Find path to deep from {} to {}, deep {}, limit {}, head {}", source, target, deep, limit, head);
|
||||
Edge<T> edge = source.getEdge(target);
|
||||
if (edge != null ){
|
||||
if (!(withRefill && Math.min(limit, maxDistance) < edge.getLength() && !source.getEntry().canRefill())){
|
||||
found = true;
|
||||
Path<T> path = head.connectTo(edge.getTarget(), limit < edge.getLength());
|
||||
path.finish();
|
||||
LOG.trace("Last edge find, add path {}", path);
|
||||
paths.add(path);
|
||||
if (onFindPath(paths, max, path)) return true;
|
||||
}
|
||||
}
|
||||
if ((all || !found) && deep > 0 ){
|
||||
if (deep > 0 ){
|
||||
if (source.getEdgesCount() > 0){
|
||||
LOG.trace("Search around");
|
||||
for (Edge<T> next : source.getEdges()) {
|
||||
@@ -131,12 +134,17 @@ public class Graph<T extends Connectable<T>> {
|
||||
double nextLimit = withRefill ? limit - next.getLength(): stock;
|
||||
// refill
|
||||
if (nextLimit < 0 ) nextLimit = maxDistance - next.getLength();
|
||||
paths.addAll(findPaths(path, target, deep - 1, nextLimit, all));
|
||||
if (!all && !paths.isEmpty()) break;
|
||||
if (findPaths(paths, max, path, target, deep - 1, nextLimit)) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return paths;
|
||||
return false;
|
||||
}
|
||||
|
||||
// if is true, then break search
|
||||
protected boolean onFindPath(ArrayList<Path<T>> paths, int max, Path<T> path){
|
||||
paths.add(path);
|
||||
return paths.size() >= max;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -94,6 +94,10 @@ public class Path<T extends Connectable<T>> {
|
||||
return refill;
|
||||
}
|
||||
|
||||
public void setRefill(boolean refill) {
|
||||
this.refill = refill;
|
||||
}
|
||||
|
||||
protected Path<T> getPrevious(){
|
||||
return head;
|
||||
}
|
||||
|
||||
@@ -12,34 +12,21 @@ public class PathRoute extends Path<Vendor> {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(PathRoute.class);
|
||||
|
||||
private final ArrayList<Order> orders = new ArrayList<>();
|
||||
private final boolean expand;
|
||||
private final int index;
|
||||
private double profit = 0;
|
||||
private double balance = 0;
|
||||
private PathRoute tail;
|
||||
private int ordersCount = 0;
|
||||
public final static Order TRANSIT = null;
|
||||
|
||||
public PathRoute(Vertex<Vendor> source, boolean expand) {
|
||||
super(source);
|
||||
this.expand = expand;
|
||||
index = 0;
|
||||
}
|
||||
|
||||
public PathRoute(Vertex<Vendor> source) {
|
||||
super(source);
|
||||
expand = false;
|
||||
index = 0;
|
||||
}
|
||||
|
||||
private PathRoute(PathRoute head, Vertex<Vendor> vertex, boolean refill) {
|
||||
super(head, vertex, refill);
|
||||
assert head.tail == null;
|
||||
head.tail = this;
|
||||
expand = head.expand;
|
||||
//transit
|
||||
orders.add(ordersCount++, TRANSIT);
|
||||
index = head.index+1;
|
||||
orders.add(TRANSIT);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -48,56 +35,53 @@ public class PathRoute extends Path<Vendor> {
|
||||
return new PathRoute(this.getCopy(), vertex, refill);
|
||||
}
|
||||
|
||||
public void add(PathRoute path, boolean withOrders) {
|
||||
LOG.trace("Add path {} to {}", path, this);
|
||||
PathRoute res = this;
|
||||
path = path.getRoot();
|
||||
if (!path.getTarget().equals(getTarget())){
|
||||
res = new PathRoute(res, path.getTarget(), true);
|
||||
}
|
||||
while (path.hasNext()){
|
||||
path = path.getNext();
|
||||
res = new PathRoute(res, path.getTarget(), res == this || path.isRefill());
|
||||
if (withOrders){
|
||||
res.orders.clear();
|
||||
res.orders.addAll(path.getOrders());
|
||||
}
|
||||
}
|
||||
if (withOrders){
|
||||
update();
|
||||
} else {
|
||||
res.finish();
|
||||
}
|
||||
}
|
||||
|
||||
public PathRoute getCopy(){
|
||||
return getCopy(false);
|
||||
}
|
||||
|
||||
public PathRoute getCopy(boolean withOrders){
|
||||
PathRoute path = getRoot();
|
||||
PathRoute res = new PathRoute(path.getTarget());
|
||||
if (withOrders) {
|
||||
res.orders.clear();
|
||||
res.orders.addAll(path.getOrders());
|
||||
}
|
||||
while (path.hasNext()){
|
||||
path = path.getNext();
|
||||
res = new PathRoute(res, path.getTarget(), path.isRefill());
|
||||
if (withOrders) {
|
||||
res.orders.clear();
|
||||
res.orders.addAll(path.getOrders());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private void addOrder(Order order){
|
||||
LOG.trace("Add order {} to path {}", order, this);
|
||||
orders.add(ordersCount++, order);
|
||||
if (expand) expand(order);
|
||||
}
|
||||
|
||||
private void expand(Order order){
|
||||
LOG.trace("Expand orders");
|
||||
if (hasNext()){
|
||||
PathRoute next = getNext();
|
||||
LOG.trace("Add {} clone of order", next.ordersCount - 1);
|
||||
for (int i = 1; i < next.ordersCount; i++) {
|
||||
orders.add(ordersCount++, new Order(order.getSell(), order.getBuy()));
|
||||
addTransitsToHead();
|
||||
}
|
||||
cloneTailOrders(next.ordersCount);
|
||||
}
|
||||
addTransitsToHead();
|
||||
}
|
||||
|
||||
private void addTransitsToHead(){
|
||||
PathRoute p = getPrevious();
|
||||
while (!p.isRoot()) {
|
||||
LOG.trace("Add transit order to path {}", p);
|
||||
p.orders.add(p.ordersCount++, TRANSIT);
|
||||
p = p.getPrevious();
|
||||
}
|
||||
}
|
||||
|
||||
private void cloneTailOrders(int count){
|
||||
if (hasNext()) {
|
||||
PathRoute p = getNext();
|
||||
LOG.trace("Duplicate {} orders of path {}", count, p);
|
||||
for (int i = 0; i < count; i++) {
|
||||
Order o = p.orders.get(i);
|
||||
if (o == TRANSIT) p.orders.add(TRANSIT);
|
||||
else p.orders.add(new Order(o.getSell(), o.getBuy()));
|
||||
}
|
||||
p.cloneTailOrders(count);
|
||||
}
|
||||
orders.add(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,6 +93,8 @@ public class PathRoute extends Path<Vendor> {
|
||||
}
|
||||
|
||||
private void fillOrders(){
|
||||
orders.clear();
|
||||
orders.add(TRANSIT);
|
||||
LOG.trace("Fill orders of path {}", this);
|
||||
Vendor seller = getPrevious().get();
|
||||
for (Offer sell : seller.getAllSellOffers()) {
|
||||
@@ -126,7 +112,7 @@ public class PathRoute extends Path<Vendor> {
|
||||
}
|
||||
|
||||
public boolean isEmpty(){
|
||||
return ordersCount <= 1;
|
||||
return orders.size() <= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,6 +128,19 @@ public class PathRoute extends Path<Vendor> {
|
||||
return tail != null;
|
||||
}
|
||||
|
||||
public void update(){
|
||||
PathRoute p = this;
|
||||
p.updateBalance();
|
||||
while (p.hasNext()){
|
||||
p = p.getNext();
|
||||
p.updateBalance();
|
||||
}
|
||||
while (p != this){
|
||||
p.updateProfit();
|
||||
p = p.getPrevious();
|
||||
}
|
||||
}
|
||||
|
||||
public void sort(double balance, long limit){
|
||||
// start on root only
|
||||
if (isRoot()){
|
||||
@@ -250,27 +249,17 @@ public class PathRoute extends Path<Vendor> {
|
||||
return Double.compare(profit2, profit1);
|
||||
}
|
||||
|
||||
public PathRoute getPath(int index){
|
||||
if (this.index == index) return this;
|
||||
if (this.index > index){
|
||||
return isRoot() ? null : getPrevious().getPath(index);
|
||||
} else {
|
||||
return hasNext() ? getNext().getPath(index) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PathRoute getRoot() {
|
||||
return (PathRoute) super.getRoot();
|
||||
}
|
||||
|
||||
public Order getBest(){
|
||||
return orders.get(0);
|
||||
public PathRoute getEnd() {
|
||||
return hasNext()? getNext().getEnd() : this;
|
||||
}
|
||||
|
||||
public double getMaxProfit(){
|
||||
Order o = orders.get(0);
|
||||
return o != TRANSIT ? o.getProfit() : 0;
|
||||
public Order getBest(){
|
||||
return orders.get(0);
|
||||
}
|
||||
|
||||
public double getDistance(){
|
||||
@@ -280,9 +269,7 @@ public class PathRoute extends Path<Vendor> {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int step = !hasNext() || tail.ordersCount == 0 ? 1 : tail.ordersCount;
|
||||
for (int i = 0; i < ordersCount; i += step) {
|
||||
Order order = orders.get(i);
|
||||
for (Order order : orders) {
|
||||
if (order == TRANSIT) continue;
|
||||
if (sb.length() > 0) sb.append(", ");
|
||||
sb.append(order.getBuy().getItem());
|
||||
@@ -302,4 +289,49 @@ public class PathRoute extends Path<Vendor> {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void setOrder(Order order) {
|
||||
orders.set(0, order);
|
||||
}
|
||||
|
||||
public int getLandsCount(){
|
||||
int res = 0;
|
||||
PathRoute p = this.isRoot() ? getNext() : this;
|
||||
Order o = p.getBest();
|
||||
while (p.hasNext()){
|
||||
p = p.getNext();
|
||||
// lands for sell
|
||||
if (o != null && p.isPathFrom(o.getBuyer())){
|
||||
LOG.trace("{} is lands for sell by order {}", p, o);
|
||||
o = p.getBest();
|
||||
res++;
|
||||
} else {
|
||||
if (o == null){
|
||||
o = p.getBest();
|
||||
if (o!= null){
|
||||
LOG.trace("{} is lands for by by order {}", p, o);
|
||||
res++;
|
||||
}
|
||||
} else {
|
||||
if (p.isRefill()){
|
||||
LOG.trace("{} is lands for refill", p);
|
||||
res++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
LOG.trace("{} is end, landing", p);
|
||||
res++;
|
||||
return res;
|
||||
}
|
||||
|
||||
public PathRoute dropTo(Vendor vendor){
|
||||
PathRoute p = getCopy(true).getEnd();
|
||||
while (!p.isRoot() && !p.get().equals(vendor)){
|
||||
p = p.getPrevious();
|
||||
}
|
||||
p.tail = null;
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
55
core/src/main/java/ru/trader/graph/RouteGraph.java
Normal file
55
core/src/main/java/ru/trader/graph/RouteGraph.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package ru.trader.graph;
|
||||
|
||||
import ru.trader.core.Vendor;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class RouteGraph extends Graph<Vendor> {
|
||||
|
||||
private double balance;
|
||||
private int limit;
|
||||
|
||||
private Comparator<Path<Vendor>> comparator = (p1, p2) -> {
|
||||
|
||||
PathRoute r1 = (PathRoute) p1.getRoot();
|
||||
PathRoute r2 = (PathRoute) p2.getRoot();
|
||||
return Double.compare(r2.getProfit()/r2.getLandsCount(), r1.getProfit()/r1.getLandsCount());
|
||||
};
|
||||
|
||||
|
||||
public RouteGraph(Vendor start, Collection<Vendor> set, double stock, double maxDistance, boolean withRefill, int maxDeep) {
|
||||
super(start, set, stock, maxDistance, withRefill, maxDeep, PathRoute::new);
|
||||
}
|
||||
|
||||
public void setBalance(double balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public void setLimit(int limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onFindPath(ArrayList<Path<Vendor>> paths, int max, Path<Vendor> path) {
|
||||
PathRoute route = (PathRoute) path;
|
||||
route.sort(balance, limit);
|
||||
if (paths.size() == max){
|
||||
int index = Collections.binarySearch(paths, route, comparator);
|
||||
if (index == -1) return false;
|
||||
if (index < -1) index = -1 - index;
|
||||
paths.add(index, path);
|
||||
paths.remove(max);
|
||||
|
||||
} else {
|
||||
if (paths.size() < max-1){
|
||||
paths.add(route);
|
||||
} else {
|
||||
paths.add(route);
|
||||
paths.sort(comparator);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -208,13 +208,13 @@ public class GraphTest extends Assert {
|
||||
Graph<Point> graph = new Graph<>(x5, entrys, 5.1, 2);
|
||||
// x5 <-> x4, x5 <-> x6
|
||||
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x4, true);
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x4);
|
||||
TestUtil.assertCollectionEquals(paths, Path.toPath(x5, x4));
|
||||
|
||||
paths = graph.getPathsTo(x6, true);
|
||||
paths = graph.getPathsTo(x6);
|
||||
TestUtil.assertCollectionEquals(paths, Path.toPath(x5, x6));
|
||||
|
||||
paths = graph.getPathsTo(x7, true);
|
||||
paths = graph.getPathsTo(x7);
|
||||
assertEquals(paths.size(), 0);
|
||||
|
||||
}
|
||||
@@ -226,21 +226,21 @@ public class GraphTest extends Assert {
|
||||
// x5 <-> x4 <-> x3 <-> x2, x5 <-> x6 <-> x7 <-> x8
|
||||
// x5 <-> x3, x4 <-> x2, x3 <-> x6, x4 <-> x6
|
||||
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x8, true);
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x8);
|
||||
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6, x7, x8));
|
||||
|
||||
paths = graph.getPathsTo(x7, true);
|
||||
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, false);
|
||||
paths = graph.getPathsTo(x7, 1);
|
||||
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x3, x6, x7));
|
||||
|
||||
paths = graph.getPathsTo(x4, true);
|
||||
paths = graph.getPathsTo(x4);
|
||||
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4), Path.toPath(x5, x6, x4), Path.toPath(x5, x3, x4),
|
||||
Path.toPath(x5, x6, x3, x4), Path.toPath(x5, x3, x2, x4), Path.toPath(x5, x3, x6, x4));
|
||||
|
||||
|
||||
paths = graph.getPathsTo(x5, true);
|
||||
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));
|
||||
@@ -264,13 +264,13 @@ public class GraphTest extends Assert {
|
||||
// x5 <-> x4 <- refill -> x3 <- refill -> x2, x5 <-> x6
|
||||
// x5 <-> x3 <- refill -> x2, x5 <-> x4 <- refill -> x6
|
||||
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x1, true);
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x1);
|
||||
assertTrue(paths.isEmpty());
|
||||
|
||||
paths = graph.getPathsTo(x2, true);
|
||||
paths = graph.getPathsTo(x2);
|
||||
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x4, x3, x2), Path.toPath(x5, x3, x2));
|
||||
|
||||
paths = graph.getPathsTo(x6, true);
|
||||
paths = graph.getPathsTo(x6);
|
||||
TestUtil.assertCollectionContainAll(paths, Path.toPath(x5, x6), Path.toPath(x5, x4, x6), Path.toPath(x5, x3, x4, x6));
|
||||
|
||||
Path<Point> fast = graph.getFastPathTo(x2);
|
||||
@@ -287,19 +287,19 @@ public class GraphTest extends Assert {
|
||||
// x5 <-> x3 <- refill -> x2
|
||||
// x5 <-> x4 <- refill -> x6
|
||||
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x1, true);
|
||||
Collection<Path<Point>> paths = graph.getPathsTo(x1);
|
||||
assertTrue(paths.isEmpty());
|
||||
|
||||
paths = graph.getPathsTo(x2, true);
|
||||
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, x6, x4, x2), Path.toPath(x5, x6, x4, x3, x2));
|
||||
|
||||
paths = graph.getPathsTo(x6, true);
|
||||
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));
|
||||
|
||||
paths = graph.getPathsTo(x7, true);
|
||||
paths = graph.getPathsTo(x7);
|
||||
assertTrue(paths.isEmpty());
|
||||
|
||||
Path<Point> fast = graph.getFastPathTo(x2);
|
||||
|
||||
@@ -45,7 +45,9 @@ public class PathRouteTest extends Assert {
|
||||
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();
|
||||
@@ -85,6 +87,7 @@ public class PathRouteTest extends Assert {
|
||||
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();
|
||||
@@ -137,6 +140,7 @@ public class PathRouteTest extends Assert {
|
||||
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();
|
||||
@@ -201,6 +205,7 @@ public class PathRouteTest extends Assert {
|
||||
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();
|
||||
@@ -266,11 +271,13 @@ public class PathRouteTest extends Assert {
|
||||
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();
|
||||
@@ -302,5 +309,86 @@ public class PathRouteTest extends Assert {
|
||||
|
||||
}
|
||||
|
||||
private PathRoute initTest6A(){
|
||||
LOG.info("Init test 6A");
|
||||
v1 = new SimpleVendor("v1");
|
||||
v2 = new SimpleVendor("v2");
|
||||
|
||||
v1.add(new Offer(OFFER_TYPE.SELL, ITEM1, 100));
|
||||
v1.add(new Offer(OFFER_TYPE.SELL, ITEM2, 200));
|
||||
v1.add(new Offer(OFFER_TYPE.SELL, ITEM3, 300));
|
||||
v2.add(new Offer(OFFER_TYPE.SELL, ITEM1, 150));
|
||||
v2.add(new Offer(OFFER_TYPE.SELL, ITEM3, 320));
|
||||
|
||||
v2.add(new Offer(OFFER_TYPE.BUY, ITEM2, 225));
|
||||
|
||||
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(){
|
||||
LOG.info("Init test 6B");
|
||||
v3 = new SimpleVendor("v3");
|
||||
v4 = new SimpleVendor("v4");
|
||||
|
||||
v3.add(new Offer(OFFER_TYPE.SELL, ITEM3, 390));
|
||||
|
||||
v3.add(new Offer(OFFER_TYPE.BUY, ITEM1, 200));
|
||||
v4.add(new Offer(OFFER_TYPE.BUY, ITEM3, 450));
|
||||
|
||||
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(500, 5);
|
||||
return res.getRoot();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAddPathRoute() throws Exception {
|
||||
LOG.info("Start add path route test");
|
||||
PathRoute path = initTest6A();
|
||||
PathRoute pathB = initTest6B();
|
||||
|
||||
path.getEnd().add(pathB, false);
|
||||
path.sort(500, 5);
|
||||
path = path.getRoot();
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user