improve route search
This commit is contained in:
@@ -157,6 +157,11 @@ public class VendorsIterator implements Iterator<Vendor> {
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return place.hashCode();
|
return place.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Transit";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ public class Graph<T extends Connectable<T>> {
|
|||||||
Vertex<T> target = getVertex(entry);
|
Vertex<T> target = getVertex(entry);
|
||||||
TopList<Path<T>> paths = newTopList(max);
|
TopList<Path<T>> paths = newTopList(max);
|
||||||
findPathsTo(target, paths, deep);
|
findPathsTo(target, paths, deep);
|
||||||
|
paths.finish();
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,6 +113,7 @@ public class Graph<T extends Connectable<T>> {
|
|||||||
paths.add(path);
|
paths.add(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
paths.finish();
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,9 @@ package ru.trader.graph;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import ru.trader.core.Place;
|
|
||||||
import ru.trader.core.Vendor;
|
import ru.trader.core.Vendor;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.ForkJoinPool;
|
import java.util.concurrent.ForkJoinPool;
|
||||||
import java.util.concurrent.RecursiveTask;
|
import java.util.concurrent.RecursiveTask;
|
||||||
|
|
||||||
@@ -65,7 +62,7 @@ public class RouteSearcher {
|
|||||||
LOG.trace("Segment jumps {}", jumpsToAll);
|
LOG.trace("Segment jumps {}", jumpsToAll);
|
||||||
sGraph.setCargo(cargo);
|
sGraph.setCargo(cargo);
|
||||||
sGraph.setBalance(balance);
|
sGraph.setBalance(balance);
|
||||||
List<PathRoute> res = new ArrayList<>(limit);
|
TopList<PathRoute> res = new TopList<>(limit, RouteGraph.byProfitComparator);
|
||||||
if (jumps <= jumpsToAll){
|
if (jumps <= jumpsToAll){
|
||||||
LOG.trace("Is last segment");
|
LOG.trace("Is last segment");
|
||||||
List<Path<Vendor>> paths;
|
List<Path<Vendor>> paths;
|
||||||
@@ -83,6 +80,13 @@ public class RouteSearcher {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
ArrayList<SegmentSearcher> subTasks = new ArrayList<>(THRESHOLD);
|
ArrayList<SegmentSearcher> subTasks = new ArrayList<>(THRESHOLD);
|
||||||
while (i < paths.size()) {
|
while (i < paths.size()) {
|
||||||
|
if (target != null){
|
||||||
|
PathRoute path = (PathRoute) paths.get(i);
|
||||||
|
if (path.getTarget().isEntry(target)){
|
||||||
|
LOG.trace("Is path to target, add to res");
|
||||||
|
res.add(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
subTasks.clear();
|
subTasks.clear();
|
||||||
for (int taskIndex = 0; taskIndex < THRESHOLD && i+taskIndex < paths.size(); taskIndex++) {
|
for (int taskIndex = 0; taskIndex < THRESHOLD && i+taskIndex < paths.size(); taskIndex++) {
|
||||||
PathRoute path = (PathRoute) paths.get(i+taskIndex);
|
PathRoute path = (PathRoute) paths.get(i+taskIndex);
|
||||||
@@ -98,8 +102,8 @@ public class RouteSearcher {
|
|||||||
i+=subTasks.size();
|
i+=subTasks.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finish(res);
|
res.finish();
|
||||||
return res;
|
return res.getList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getPathsOnSegmentCount(RouteGraph graph){
|
private int getPathsOnSegmentCount(RouteGraph graph){
|
||||||
@@ -111,21 +115,18 @@ public class RouteSearcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void add(SegmentSearcher task, PathRoute path, List<PathRoute> res){
|
private void add(SegmentSearcher task, PathRoute path, TopList<PathRoute> res){
|
||||||
List<PathRoute> tail = task.join();
|
List<PathRoute> tail = task.join();
|
||||||
if (tail.isEmpty()){
|
if (tail.isEmpty()){
|
||||||
LOG.trace("Not found route from {} to {}, jumps {}", task.source, task.target, task.jumps);
|
LOG.trace("Not found route from {} to {}, jumps {}", task.source, task.target, task.jumps);
|
||||||
} else {
|
} else {
|
||||||
path.add(tail.get(0), false);
|
path.add(tail.get(0), false);
|
||||||
path.sort(balance, cargo);
|
path.sort(balance, cargo);
|
||||||
TopList.addToTop(res, path.getEnd(), limit, RouteGraph.byProfitComparator);
|
res.add(path.getEnd());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void finish(List<PathRoute> res){
|
|
||||||
if (res.size() < limit)
|
|
||||||
res.sort(RouteGraph.byProfitComparator);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,11 @@ public class TopList<T> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void finish(){
|
||||||
|
if (comparator != null && list.size() < limit)
|
||||||
|
list.sort(comparator);
|
||||||
|
}
|
||||||
|
|
||||||
public List<T> getList() {
|
public List<T> getList() {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@@ -55,7 +60,7 @@ public class TopList<T> {
|
|||||||
if (list.size() == limit) {
|
if (list.size() == limit) {
|
||||||
int index = Collections.binarySearch(list, entry, comparator);
|
int index = Collections.binarySearch(list, entry, comparator);
|
||||||
if (index < 0) index = -1 - index;
|
if (index < 0) index = -1 - index;
|
||||||
if (index == limit) return null;
|
if (index == limit || list.get(index).equals(entry)) return null;
|
||||||
list.add(index, entry);
|
list.add(index, entry);
|
||||||
return list.remove(limit);
|
return list.remove(limit);
|
||||||
|
|
||||||
@@ -75,7 +80,7 @@ public class TopList<T> {
|
|||||||
if (list.size() == limit) {
|
if (list.size() == limit) {
|
||||||
int index = Collections.binarySearch(list, entry, comparator);
|
int index = Collections.binarySearch(list, entry, comparator);
|
||||||
if (index < 0) index = -1 - index;
|
if (index < 0) index = -1 - index;
|
||||||
if (index == limit) return;
|
if (index == limit || list.get(index).equals(entry)) return;
|
||||||
list.add(index, entry);
|
list.add(index, entry);
|
||||||
list.remove(limit);
|
list.remove(limit);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ public class Vertex<T extends Connectable<T>> {
|
|||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEntry(T entry){
|
||||||
|
return this.entry.equals(entry);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isConnected(Vertex<T> other){
|
public boolean isConnected(Vertex<T> other){
|
||||||
return isConnected(other.entry);
|
return isConnected(other.entry);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user