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.PathRoute;
|
||||||
import ru.trader.graph.RouteGraph;
|
import ru.trader.graph.RouteGraph;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
|
|
||||||
public class MarketAnalyzer {
|
public class MarketAnalyzer {
|
||||||
private final static Logger LOG = LoggerFactory.getLogger(MarketAnalyzer.class);
|
private final static Logger LOG = LoggerFactory.getLogger(MarketAnalyzer.class);
|
||||||
@@ -135,13 +132,15 @@ public class MarketAnalyzer {
|
|||||||
public Collection<PathRoute> getPaths(Vendor from, double balance){
|
public Collection<PathRoute> getPaths(Vendor from, double balance){
|
||||||
setSource(from);
|
setSource(from);
|
||||||
graph.setBalance(balance);
|
graph.setBalance(balance);
|
||||||
Collection<PathRoute> res = new ArrayList<>();
|
Collection<Vendor> vendors = market.get();
|
||||||
for (Vendor vendor : market.get()) {
|
List<PathRoute> res = new ArrayList<>(vendors.size()*10);
|
||||||
|
for (Vendor vendor : vendors) {
|
||||||
Collection<Path<Vendor>> paths = graph.getPathsTo(vendor, 10);
|
Collection<Path<Vendor>> paths = graph.getPathsTo(vendor, 10);
|
||||||
for (Path<Vendor> path : paths) {
|
for (Path<Vendor> path : paths) {
|
||||||
res.add((PathRoute) path);
|
res.add((PathRoute) path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Collections.sort(res, RouteGraph.comparator);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,16 +156,13 @@ public class MarketAnalyzer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Collection<PathRoute> getTopPaths(int limit, double balance){
|
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()) {
|
for (Vendor vendor : market.get()) {
|
||||||
setSource(vendor);
|
setSource(vendor);
|
||||||
graph.setBalance(balance);
|
graph.setBalance(balance);
|
||||||
Collection<Path<Vendor>> paths = graph.getPathsTo(vendor, 10);
|
Collection<Path<Vendor>> paths = graph.getPathsTo(vendor, 10);
|
||||||
for (Path<Vendor> path : paths) {
|
for (Path<Vendor> path : paths) {
|
||||||
top.add((PathRoute) path);
|
RouteGraph.addToTop(top, (PathRoute)path, limit, RouteGraph.comparator);
|
||||||
if (top.size() > limit) {
|
|
||||||
top.pollLast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return top;
|
return top;
|
||||||
|
|||||||
@@ -263,7 +263,16 @@ public class PathRoute extends Path<Vendor> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public double getDistance(){
|
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
|
@Override
|
||||||
|
|||||||
@@ -9,10 +9,9 @@ public class RouteGraph extends Graph<Vendor> {
|
|||||||
private double balance;
|
private double balance;
|
||||||
private int limit;
|
private int limit;
|
||||||
|
|
||||||
private Comparator<Path<Vendor>> comparator = (p1, p2) -> {
|
public static Comparator<PathRoute> comparator = (p1, p2) -> {
|
||||||
|
PathRoute r1 = p1.getRoot();
|
||||||
PathRoute r1 = (PathRoute) p1.getRoot();
|
PathRoute r2 = p2.getRoot();
|
||||||
PathRoute r2 = (PathRoute) p2.getRoot();
|
|
||||||
int cmp = Double.compare(r2.getProfit()/r2.getLandsCount(), r1.getProfit()/r1.getLandsCount());
|
int cmp = Double.compare(r2.getProfit()/r2.getLandsCount(), r1.getProfit()/r1.getLandsCount());
|
||||||
if (cmp != 0 ) return cmp;
|
if (cmp != 0 ) return cmp;
|
||||||
cmp = Double.compare(r1.getDistance(), r2.getDistance());
|
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) {
|
protected boolean onFindPath(ArrayList<Path<Vendor>> paths, int max, Path<Vendor> path) {
|
||||||
PathRoute route = (PathRoute) path;
|
PathRoute route = (PathRoute) path;
|
||||||
route.sort(balance, limit);
|
route.sort(balance, limit);
|
||||||
if (paths.size() == max){
|
addToTop(paths, route, max, (r1, r2) -> comparator.compare((PathRoute)r1, (PathRoute)r2));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
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