Archived
0

- update paths layout

- add top routers
- add orders button
This commit is contained in:
iMoHax
2014-08-19 17:20:30 +04:00
parent 2604939f96
commit 14c2021e0c
16 changed files with 419 additions and 133 deletions

View File

@@ -60,6 +60,42 @@ public class MarketAnalyzer {
return top;
}
public Collection<Order> getOrders(Vendor vendor, double balance) {
Collection<Order> res = new ArrayList<>();
for (Offer sell : vendor.getAllSellOffers()) {
long count = Math.min(cargo, (long) Math.floor(balance / sell.getPrice()));
LOG.trace("Sell offer {}, count = {}", sell, count);
if (count == 0) continue;
Iterator<Offer> buyers = market.getStatBuy(sell.getItem()).getOffers().descendingIterator();
while (buyers.hasNext()){
Offer buy = buyers.next();
Order order = new Order(sell, buy, count);
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
res.add(order);
}
}
return res;
}
public Collection<Order> getOrders(Vendor from, Vendor to, double balance) {
Collection<Order> res = new ArrayList<>();
for (Offer sell : from.getAllSellOffers()) {
long count = Math.min(cargo, (long) Math.floor(balance / sell.getPrice()));
LOG.trace("Sell offer {}, count = {}", sell, count);
if (count == 0) continue;
Offer buy = to.getBuy(sell.getItem());
if (buy != null){
Order order = new Order(sell, buy, count);
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
res.add(order);
}
}
return res;
}
private void rebuild(Vendor source){
graph = new Graph<>(source, market.get(), tank, maxDistance, true, jumps, PathRoute::new);
}
@@ -74,6 +110,22 @@ public class MarketAnalyzer {
return graph.getPathsTo(to, true);
}
public Path<Vendor> getPath(Vendor from, Vendor to){
setSource(from);
return graph.getFastPathTo(to);
}
public Collection<PathRoute> getPaths(Vendor from, double 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);
}
return res;
}
public Collection<PathRoute> getPaths(Vendor from, Vendor to, double balance){
Collection<Path<Vendor>> paths = getPaths(from, to);
Collection<PathRoute> res = new ArrayList<>(paths.size());
@@ -85,6 +137,21 @@ public class MarketAnalyzer {
return res;
}
public Collection<PathRoute> getTopPaths(int limit, double balance){
TreeSet<PathRoute> top = new TreeSet<>((p1, p2) -> Double.compare(p2.getProfit(), p1.getProfit()));
for (Vendor vendor : market.get()) {
Collection<PathRoute> paths = getPaths(vendor, vendor, balance);
for (PathRoute path : paths) {
top.add(path);
if (top.size() > limit) {
top.pollLast();
}
}
}
return top;
}
public void setTank(double tank) {
this.tank = tank;
this.graph = null;
@@ -107,4 +174,5 @@ public class MarketAnalyzer {
public long getCargo() {
return cargo;
}
}

View File

@@ -142,6 +142,7 @@ public class Graph<T extends Connectable<T>> {
public Path<T> getFastPathTo(T entry){
Vertex<T> target = getVertex(entry);
if (target == null) return null;
return findFastPath(pathFabric.build(root), target, target.getLevel()+1, stock);
}

View File

@@ -49,7 +49,7 @@ public class PathRoute extends Path<Vendor> {
}
public PathRoute getCopy(){
PathRoute path = (PathRoute) getRoot();
PathRoute path = getRoot();
PathRoute res = new PathRoute(path.getTarget());
while (path.hasNext()){
path = path.getNext();
@@ -180,11 +180,11 @@ public class PathRoute extends Path<Vendor> {
}
private void backwardSort(){
if (isRoot()) return;
orders.sort(this::compareOrders);
LOG.trace("New order of orders {}", orders);
updateProfit();
getPrevious().backwardSort();
if (!isRoot())
getPrevious().backwardSort();
}
private void updateBalance() {
@@ -209,10 +209,10 @@ public class PathRoute extends Path<Vendor> {
private void updateProfit() {
Order best = orders.get(0);
Order best = orders.isEmpty()? TRANSIT : orders.get(0);
if (best == TRANSIT) profit = getTransitProfit();
else profit = getProfit(best);
LOG.trace("Max profit from {} = {}", getPrevious().get(), profit);
LOG.trace("Max profit from {} = {}", isRoot() ? get() : getPrevious().get(), profit);
}
private double getTransitProfit(){

View File

@@ -37,14 +37,17 @@ public class PathRouteTest extends Assert {
res = (PathRoute) res.connectTo(new Vertex<>(v2), false);
res.finish();
res.sort(10000, 5);
return (PathRoute) res.getRoot();
return res.getRoot();
}
@Test
public void testPathRoute1() throws Exception {
LOG.info("Start path route test 1");
PathRoute path = initTest1().getNext();
PathRoute path = initTest1();
assertEquals(1000, path.getProfit(), 0.0001);
path = path.getNext();
Collection<Order> orders = path.getOrders();
Order order1 = new Order(v1.getSell(ITEM1), v2.getBuy(ITEM1), 5);
@@ -74,13 +77,16 @@ public class PathRouteTest extends Assert {
res = (PathRoute) res.connectTo(new Vertex<>(v3), false);
res.finish();
res.sort(10000, 5);
return (PathRoute) res.getRoot();
return res.getRoot();
}
@Test
public void testPathRoute2() throws Exception {
LOG.info("Start path route test 2");
PathRoute path = initTest2().getNext();
PathRoute path = initTest2();
assertEquals(1000, path.getProfit(), 0.0001);
path = path.getNext();
Collection<Order> orders = path.getOrders();
Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5);
@@ -123,13 +129,16 @@ public class PathRouteTest extends Assert {
res = (PathRoute) res.connectTo(new Vertex<>(v4), false);
res.finish();
res.sort(10000, 5);
return (PathRoute) res.getRoot();
return res.getRoot();
}
@Test
public void testPathRoute3() throws Exception {
LOG.info("Start path route test 3");
PathRoute path = initTest3().getNext();
PathRoute path = initTest3();
assertEquals(800, path.getProfit(), 0.0001);
path = path.getNext();
Collection<Order> orders = path.getOrders();
Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5);
@@ -184,13 +193,16 @@ public class PathRouteTest extends Assert {
res = (PathRoute) res.connectTo(new Vertex<>(v5), false);
res.finish();
res.sort(10000, 5);
return (PathRoute) res.getRoot();
return res.getRoot();
}
@Test
public void testPathRoute4() throws Exception {
LOG.info("Start path route test 4");
PathRoute path = initTest4().getNext();
PathRoute path = initTest4();
assertEquals(1000, path.getProfit(), 0.0001);
path = path.getNext();
Collection<Order> orders = path.getOrders();
Order order1 = new Order(v1.getSell(ITEM1), v2.getBuy(ITEM1), 5);
@@ -251,13 +263,16 @@ public class PathRouteTest extends Assert {
res = (PathRoute) res.connectTo(new Vertex<>(v4), false);
res.finish();
res.sort(500, 5);
return (PathRoute) res.getRoot();
return res.getRoot();
}
@Test
public void testPathRoute5() throws Exception {
LOG.info("Start path route test 5");
PathRoute path = initTest5().getNext();
PathRoute path = initTest5();
assertEquals(620, path.getProfit(), 0.0001);
path = path.getNext();
Collection<Order> orders = path.getOrders();
Order order1 = new Order(v1.getSell(ITEM1), v3.getBuy(ITEM1), 5);