Archived
0

add paths layout

This commit is contained in:
iMoHax
2014-08-17 22:49:37 +04:00
committed by iMoHax
parent b4baf8512e
commit 2604939f96
12 changed files with 364 additions and 30 deletions

View File

@@ -6,6 +6,7 @@ import ru.trader.graph.Graph;
import ru.trader.graph.Path;
import ru.trader.graph.PathRoute;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;
@@ -18,19 +19,20 @@ public class MarketAnalyzer {
private double tank;
private double maxDistance;
private int jumps;
private long cargo;
public MarketAnalyzer(Market market) {
this.market = market;
}
public Collection<Order> getTop(int limit, double balance, long max){
public Collection<Order> getTop(int limit, double balance){
LOG.debug("Get top {}", limit);
TreeSet<Order> top = new TreeSet<>();
for (Vendor vendor : market.get()) {
LOG.trace("Check vendor {}", vendor);
for (Offer sell : vendor.getAllSellOffers()) {
long count = Math.min(max, (long) Math.floor(balance / sell.getPrice()));
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();
@@ -72,16 +74,37 @@ public class MarketAnalyzer {
return graph.getPathsTo(to, true);
}
public Collection<PathRoute> getPaths(Vendor from, Vendor to, double balance){
Collection<Path<Vendor>> paths = getPaths(from, to);
Collection<PathRoute> res = new ArrayList<>(paths.size());
for (Path<Vendor> path : paths) {
PathRoute p = (PathRoute) path;
p.sort(balance, cargo);
res.add(p);
}
return res;
}
public void setTank(double tank) {
this.tank = tank;
this.graph = null;
}
public void setMaxDistance(double maxDistance) {
this.maxDistance = maxDistance;
this.graph = null;
}
public void setJumps(int jumps) {
this.jumps = jumps;
this.graph = null;
}
public void setCargo(long cargo) {
this.cargo = cargo;
}
public long getCargo() {
return cargo;
}
}

View File

@@ -237,7 +237,7 @@ public class PathRoute extends Path<Vendor> {
if (o1 != TRANSIT && o2 != TRANSIT){
return o2.compareTo(o1);
}
return o1 == TRANSIT ? o2 == TRANSIT ? 0 : 1 : -1;
return o1 == TRANSIT ? o2 == TRANSIT ? 0 : Double.compare(o2.getProfit(), 0) : Double.compare(0, o1.getProfit());
}
private int compareOrders(Order o1, Order o2){
@@ -264,6 +264,19 @@ public class PathRoute extends Path<Vendor> {
return (PathRoute) super.getRoot();
}
public Order getBest(){
return orders.get(0);
}
public double getMaxProfit(){
Order o = orders.get(0);
return o != TRANSIT ? o.getProfit() : 0;
}
public double getDistance(){
return isRoot() ? 0 : getPrevious().get().getDistance(get());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();