fix distance in sort routes
This commit is contained in:
@@ -7,10 +7,7 @@ import ru.trader.graph.Path;
|
||||
import ru.trader.graph.PathRoute;
|
||||
import ru.trader.graph.RouteGraph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.TreeSet;
|
||||
import java.util.*;
|
||||
|
||||
public class MarketAnalyzer {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(MarketAnalyzer.class);
|
||||
@@ -135,13 +132,15 @@ public class MarketAnalyzer {
|
||||
public Collection<PathRoute> getPaths(Vendor from, double balance){
|
||||
setSource(from);
|
||||
graph.setBalance(balance);
|
||||
Collection<PathRoute> res = new ArrayList<>();
|
||||
for (Vendor vendor : market.get()) {
|
||||
Collection<Vendor> vendors = market.get();
|
||||
List<PathRoute> res = new ArrayList<>(vendors.size()*10);
|
||||
for (Vendor vendor : vendors) {
|
||||
Collection<Path<Vendor>> paths = graph.getPathsTo(vendor, 10);
|
||||
for (Path<Vendor> path : paths) {
|
||||
res.add((PathRoute) path);
|
||||
}
|
||||
}
|
||||
Collections.sort(res, RouteGraph.comparator);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -157,16 +156,13 @@ public class MarketAnalyzer {
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getTopPaths(int limit, double balance){
|
||||
TreeSet<PathRoute> top = new TreeSet<>((p1, p2) -> Double.compare(p2.getProfit()/p2.getLandsCount(), p1.getProfit()/p1.getLandsCount()));
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
for (Vendor vendor : market.get()) {
|
||||
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();
|
||||
}
|
||||
RouteGraph.addToTop(top, (PathRoute)path, limit, RouteGraph.comparator);
|
||||
}
|
||||
}
|
||||
return top;
|
||||
|
||||
@@ -263,7 +263,16 @@ public class PathRoute extends Path<Vendor> {
|
||||
}
|
||||
|
||||
public double getDistance(){
|
||||
return isRoot() ? 0 : getPrevious().get().getDistance(get());
|
||||
if (isRoot()){
|
||||
double res = 0;
|
||||
PathRoute p = this;
|
||||
while (p.hasNext()){
|
||||
p = p.getNext();
|
||||
res += p.getDistance();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else return getPrevious().get().getDistance(get());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,10 +9,9 @@ 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();
|
||||
public static Comparator<PathRoute> comparator = (p1, p2) -> {
|
||||
PathRoute r1 = p1.getRoot();
|
||||
PathRoute r2 = p2.getRoot();
|
||||
int cmp = Double.compare(r2.getProfit()/r2.getLandsCount(), r1.getProfit()/r1.getLandsCount());
|
||||
if (cmp != 0 ) return cmp;
|
||||
cmp = Double.compare(r1.getDistance(), r2.getDistance());
|
||||
@@ -39,23 +38,25 @@ public class RouteGraph extends Graph<Vendor> {
|
||||
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 < 0) index = -1 - index;
|
||||
if (index == max) return false;
|
||||
paths.add(index, path);
|
||||
paths.remove(max);
|
||||
|
||||
} else {
|
||||
if (paths.size() < max-1){
|
||||
paths.add(route);
|
||||
} else {
|
||||
paths.add(route);
|
||||
paths.sort(comparator);
|
||||
}
|
||||
}
|
||||
addToTop(paths, route, max, (r1, r2) -> comparator.compare((PathRoute)r1, (PathRoute)r2));
|
||||
return false;
|
||||
}
|
||||
|
||||
public static <T> void addToTop(List<T> list, T entry, int limit, Comparator<T> comparator){
|
||||
if (list.size() == limit){
|
||||
int index = Collections.binarySearch(list, entry, comparator);
|
||||
if (index < 0) index = -1 - index;
|
||||
if (index == limit) return;
|
||||
list.add(index, entry);
|
||||
list.remove(limit);
|
||||
|
||||
} else {
|
||||
if (list.size() < limit-1){
|
||||
list.add(entry);
|
||||
} else {
|
||||
list.add(entry);
|
||||
list.sort(comparator);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user