implement recompute, refresh path, add station to path
This commit is contained in:
@@ -363,6 +363,23 @@ public class MarketAnalyzer {
|
||||
return top;
|
||||
}
|
||||
|
||||
public PathRoute getPath(Collection<Vendor> vendors, double balance) {
|
||||
PathRoute res = null;
|
||||
callback.setMax(vendors.size());
|
||||
for (Vendor from : vendors) {
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize, callback.onStartSearch());
|
||||
//TODO: implement search with constant length
|
||||
Collection<PathRoute> paths = searcher.getPaths(from, vendors, jumps, balance, cargo, limit);
|
||||
Optional<PathRoute> route = paths.stream().filter(p -> p.contains(vendors)).findFirst();
|
||||
if (route.isPresent() && (res == null || RouteGraph.byProfitComparator.compare(res, route.get()) < 0)){
|
||||
res = route.get();
|
||||
}
|
||||
callback.inc();
|
||||
}
|
||||
callback.onEndSearch();
|
||||
return res;
|
||||
}
|
||||
|
||||
private Collection<Place> getPlaces(){
|
||||
if (filter != null){
|
||||
return filter.filtered(market.get());
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package ru.trader.graph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class Path<T extends Connectable<T>> {
|
||||
private final Path<T> head;
|
||||
private final Vertex<T> target;
|
||||
@@ -110,5 +115,24 @@ public class Path<T extends Connectable<T>> {
|
||||
return isRoot() ? 0 : 1 + getPrevious().getLength();
|
||||
}
|
||||
|
||||
public Collection<T> getEntries(){
|
||||
Collection<T> res = new HashSet<>();
|
||||
res.add(target.getEntry());
|
||||
Path<T> p = getPrevious();
|
||||
while (p != null){
|
||||
res.add(p.target.getEntry());
|
||||
p = p.getPrevious();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public boolean contains(Collection<T> items){
|
||||
if (items.isEmpty()) return true;
|
||||
Collection<T> remains = new ArrayList<>(items);
|
||||
remains.remove(target.getEntry());
|
||||
if (isRoot()) {
|
||||
return remains.isEmpty();
|
||||
}
|
||||
return getPrevious().contains(remains);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class PathRoute extends Path<Vendor> {
|
||||
return new PathRoute(this.getCopy(), vertex, refill);
|
||||
}
|
||||
|
||||
public void add(PathRoute path, boolean noSort) {
|
||||
public PathRoute add(PathRoute path, boolean noSort) {
|
||||
LOG.trace("Add path {} to {}", path, this);
|
||||
PathRoute res = this;
|
||||
path = path.getRoot();
|
||||
@@ -70,6 +70,7 @@ public class PathRoute extends Path<Vendor> {
|
||||
} else {
|
||||
res.finish();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private void copyField(PathRoute source, PathRoute dest){
|
||||
@@ -106,6 +107,10 @@ public class PathRoute extends Path<Vendor> {
|
||||
orders.add(order);
|
||||
}
|
||||
|
||||
public void refresh(){
|
||||
getEnd().finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finish() {
|
||||
if (!isRoot()){
|
||||
|
||||
@@ -131,9 +131,9 @@ public class RouteSearcher {
|
||||
if (tail.isEmpty()){
|
||||
LOG.trace("Not found route from {} to {}, jumps {}", task.source, task.target, task.jumps);
|
||||
} else {
|
||||
path.add(tail.get(0), false);
|
||||
path = path.add(tail.get(0), false);
|
||||
path.sort(balance, cargo);
|
||||
res.add(path.getEnd());
|
||||
res.add(path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,9 @@ import ru.trader.store.simple.SimpleItem;
|
||||
import ru.trader.store.simple.SimpleOffer;
|
||||
import ru.trader.store.simple.SimpleVendor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
public class PathRouteTest extends Assert {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(PathRouteTest.class);
|
||||
@@ -437,4 +439,50 @@ public class PathRouteTest extends Assert {
|
||||
TestUtil.assertCollectionEquals(orders, order7, PathRoute.TRANSIT);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntries() throws Exception {
|
||||
LOG.info("Start test get entries");
|
||||
v1 = new SimpleVendor("v1",0,0,0);
|
||||
v2 = new SimpleVendor("v2",0,0,0);
|
||||
v3 = new SimpleVendor("v3",0,0,0);
|
||||
v4 = new SimpleVendor("v4",0,0,0);
|
||||
|
||||
PathRoute path = new PathRoute(new Vertex<>(v1));
|
||||
path = (PathRoute) path.connectTo(new Vertex<>(v2), false);
|
||||
path = (PathRoute) path.connectTo(new Vertex<>(v3), false);
|
||||
path.finish();
|
||||
TestUtil.assertCollectionContainAll(path.getEntries(), v1, v2, v3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() throws Exception {
|
||||
LOG.info("Start test get entries");
|
||||
v1 = new SimpleVendor("v1",0,0,0);
|
||||
v2 = new SimpleVendor("v2",0,0,0);
|
||||
v3 = new SimpleVendor("v3",0,0,0);
|
||||
v4 = new SimpleVendor("v4",0,0,0);
|
||||
|
||||
PathRoute path = new PathRoute(new Vertex<>(v1));
|
||||
path = (PathRoute) path.connectTo(new Vertex<>(v2), false);
|
||||
path = (PathRoute) path.connectTo(new Vertex<>(v3), false);
|
||||
path.finish();
|
||||
Collection<Vendor> vendors = new ArrayList<>();
|
||||
Collections.addAll(vendors, v1, v2, v3);
|
||||
assertTrue(path.contains(vendors));
|
||||
vendors.clear();
|
||||
Collections.addAll(vendors, v2);
|
||||
assertTrue(path.contains(vendors));
|
||||
vendors.clear();
|
||||
Collections.addAll(vendors, v4);
|
||||
assertFalse(path.contains(vendors));
|
||||
vendors.clear();
|
||||
Collections.addAll(vendors, v3, v2, v4, v1);
|
||||
assertFalse(path.contains(vendors));
|
||||
vendors.clear();
|
||||
Collections.addAll(vendors, v1, v2, v3, v4);
|
||||
assertFalse(path.contains(vendors));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user