implement check station in one system on find paths
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import ru.trader.graph.Connectable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
@@ -112,6 +114,11 @@ public abstract class AbstractItemStat implements ItemStat {
|
||||
public void remove(Vendor vendor) {
|
||||
throw new UnsupportedOperationException("Is fake place, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Connectable<Place> o) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
private static Vendor NONE_VENDOR = new Vendor() {
|
||||
@@ -189,5 +196,10 @@ public abstract class AbstractItemStat implements ItemStat {
|
||||
public boolean has(OFFER_TYPE type, Item item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Connectable<Vendor> o) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@ package ru.trader.core;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.graph.Connectable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class AbstractPlace implements Place, Comparable<Place> {
|
||||
public abstract class AbstractPlace implements Place {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(AbstractPlace.class);
|
||||
private AbstractMarket market;
|
||||
|
||||
@@ -84,8 +85,9 @@ public abstract class AbstractPlace implements Place, Comparable<Place> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull Place other) {
|
||||
Objects.requireNonNull(other, "Not compare with null");
|
||||
public int compareTo(@NotNull Connectable<Place> o) {
|
||||
Objects.requireNonNull(o, "Not compare with null");
|
||||
Place other = (Place) o;
|
||||
if (this == other) return 0;
|
||||
String name = getName();
|
||||
String otherName = other.getName();
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.graph.Connectable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class AbstractVendor implements Vendor {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(AbstractVendor.class);
|
||||
@@ -107,4 +111,15 @@ public abstract class AbstractVendor implements Vendor {
|
||||
return getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull Connectable<Vendor> o) {
|
||||
Objects.requireNonNull(o, "Not compare with null");
|
||||
Vendor other = (Vendor) o;
|
||||
if (this == other) return 0;
|
||||
int cmp = Double.compare(getDistance(), other.getDistance());
|
||||
if (cmp!=0) return cmp;
|
||||
String name = getName();
|
||||
String otherName = other.getName();
|
||||
return name != null ? otherName != null ? name.compareTo(otherName) : -1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ public interface Market {
|
||||
void remove(Item item);
|
||||
|
||||
Collection<Place> get();
|
||||
default Collection<Vendor> getVendors(){
|
||||
return new PlacesWrapper(get());
|
||||
}
|
||||
Collection<Group> getGroups();
|
||||
Collection<Item> getItems();
|
||||
ItemStat getStat(OFFER_TYPE type, Item item);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.graph.*;
|
||||
@@ -44,7 +45,7 @@ public class MarketAnalyzer {
|
||||
private Collection<Order> getOrders(Place place, double balance, double lowProfit) {
|
||||
List<Order> res = new ArrayList<>(20);
|
||||
Collection<Place> places = market.get();
|
||||
RouteGraph graph = new RouteGraph(place, places, tank, maxDistance, true, jumps);
|
||||
Graph<Place> graph = new Graph<>(place, places, tank, maxDistance, true, jumps, Path::new);
|
||||
for (Vendor vendor : place.get()) {
|
||||
for (Offer sell : vendor.getAllSellOffers()) {
|
||||
LOG.trace("Sell offer {}", sell);
|
||||
@@ -76,7 +77,7 @@ public class MarketAnalyzer {
|
||||
|
||||
public Collection<Order> getOrders(Place from, Place to, double balance) {
|
||||
Collection<Order> res = new ArrayList<>();
|
||||
RouteGraph graph = new RouteGraph(from, market.get(), tank, maxDistance, true, jumps);
|
||||
Graph<Place> graph = new Graph<>(from, market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to)){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return res;
|
||||
@@ -102,7 +103,7 @@ public class MarketAnalyzer {
|
||||
|
||||
public Collection<Order> getOrders(Vendor from, Vendor to, double balance) {
|
||||
Collection<Order> res = new ArrayList<>();
|
||||
RouteGraph graph = new RouteGraph(from.getPlace(), market.get(), tank, maxDistance, true, jumps);
|
||||
Graph<Place> graph = new Graph<>(from.getPlace(), market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to.getPlace())){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return res;
|
||||
@@ -124,31 +125,60 @@ public class MarketAnalyzer {
|
||||
}
|
||||
|
||||
public Collection<Path<Place>> getPaths(Place from, Place to){
|
||||
RouteGraph graph = new RouteGraph(from, market.get(), tank, maxDistance, true, jumps);
|
||||
Graph<Place> graph = new Graph<>(from, market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
return graph.getPathsTo(to);
|
||||
}
|
||||
|
||||
public PathRoute getPath(Place from, Place to){
|
||||
RouteGraph graph = new RouteGraph(from, market.get(), tank, maxDistance, true, jumps);
|
||||
return (PathRoute) graph.getFastPathTo(to);
|
||||
public Path<Place> getPath(Place from, Place to){
|
||||
Graph<Place> graph = new Graph<>(from, market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
return graph.getFastPathTo(to);
|
||||
}
|
||||
|
||||
public PathRoute getPath(Vendor from, Vendor to){
|
||||
RouteGraph graph = new RouteGraph(from, market.getVendors(), tank, maxDistance, true, jumps);
|
||||
return (PathRoute)graph.getFastPathTo(to);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
return searcher.getPaths(from, market.get(), jumps, balance, cargo, limit);
|
||||
Collection<Vendor> vendors = market.getVendors();
|
||||
for (Vendor vendor : from.get()) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(vendor, vendors, jumps, balance, cargo, limit);
|
||||
if (paths.size()>0){
|
||||
return paths;
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, Vendor to, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
return searcher.getPaths(from, to, market.getVendors(), jumps, balance, cargo, limit);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, Place to, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
return searcher.getPaths(from, to, market.get(), jumps, balance, cargo, limit);
|
||||
Collection<Vendor> vendors = market.getVendors();
|
||||
Collection<Vendor> fVendors = from.get();
|
||||
Collection<Vendor> toVendors = to.get();
|
||||
for (Vendor fromVendor : fVendors) {
|
||||
for (Vendor toVendor : toVendors) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(fromVendor, toVendor, vendors, jumps, balance, cargo, limit);
|
||||
if (paths.size()>0){
|
||||
return paths;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getTopPaths(double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Place> places = market.get();
|
||||
for (Place place : places) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(place, place, places, jumps, balance, cargo, 3);
|
||||
Collection<Vendor> vendors = new PlacesWrapper(market.get());
|
||||
for (Vendor vendor : vendors) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(vendor, vendor, vendors, jumps, balance, cargo, 3);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
return top;
|
||||
|
||||
@@ -19,6 +19,10 @@ public interface Place extends Connectable<Place> {
|
||||
Vendor addVendor(String name);
|
||||
void remove(Vendor vendor);
|
||||
|
||||
default int count(){
|
||||
return get().size();
|
||||
}
|
||||
|
||||
default boolean isEmpty(){
|
||||
return get().isEmpty();
|
||||
}
|
||||
|
||||
32
core/src/main/java/ru/trader/core/PlacesWrapper.java
Normal file
32
core/src/main/java/ru/trader/core/PlacesWrapper.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.AbstractCollection;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class PlacesWrapper extends AbstractCollection<Vendor> {
|
||||
private final Collection<Place> places;
|
||||
private int size;
|
||||
|
||||
public PlacesWrapper(Collection<Place> places) {
|
||||
this.places = places;
|
||||
size = 0;
|
||||
for (Place place : places) {
|
||||
int count = place.count();
|
||||
size += count > 0 ? count : 1;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<Vendor> iterator() {
|
||||
return new VendorsIterator(places);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import ru.trader.graph.Connectable;
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
|
||||
public interface Vendor extends Comparable<Vendor> {
|
||||
public interface Vendor extends Connectable<Vendor> {
|
||||
|
||||
String getName();
|
||||
void setName(String name);
|
||||
@@ -53,14 +51,18 @@ public interface Vendor extends Comparable<Vendor> {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
default int compareTo(@NotNull Vendor other) {
|
||||
Objects.requireNonNull(other, "Not compare with null");
|
||||
if (this == other) return 0;
|
||||
int cmp = Double.compare(getDistance(), other.getDistance());
|
||||
if (cmp!=0) return cmp;
|
||||
String name = getName();
|
||||
String otherName = other.getName();
|
||||
return name != null ? otherName != null ? name.compareTo(otherName) : -1 : 0;
|
||||
static double LS = 0.00000003169;
|
||||
|
||||
default double getDistance(Vendor other){
|
||||
Place place = getPlace();
|
||||
Place otherPlace = other.getPlace();
|
||||
if (!place.equals(otherPlace)){
|
||||
return getPlace().getDistance(other.getPlace()) + other.getDistance() * LS;
|
||||
}
|
||||
return (getDistance() + other.getDistance() + Math.abs(getDistance() - other.getDistance())) * LS / 2;
|
||||
}
|
||||
|
||||
default boolean canRefill(){
|
||||
return getPlace().canRefill();
|
||||
}
|
||||
}
|
||||
|
||||
162
core/src/main/java/ru/trader/core/VendorsIterator.java
Normal file
162
core/src/main/java/ru/trader/core/VendorsIterator.java
Normal file
@@ -0,0 +1,162 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import ru.trader.graph.Connectable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class VendorsIterator implements Iterator<Vendor> {
|
||||
|
||||
private final Iterator<Place> places;
|
||||
private Iterator<Vendor> vendors;
|
||||
private Vendor next;
|
||||
|
||||
public VendorsIterator(Collection<Place> places) {
|
||||
this.places = places.iterator();
|
||||
nextPlace();
|
||||
}
|
||||
|
||||
private void nextPlace(){
|
||||
if (places.hasNext()){
|
||||
Place place = places.next();
|
||||
Collection<Vendor> v = place.get();
|
||||
if (place.count() > 0){
|
||||
vendors = v.iterator();
|
||||
nextVendor();
|
||||
} else {
|
||||
next = new TransitVendor(place);
|
||||
}
|
||||
} else {
|
||||
next = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void nextVendor(){
|
||||
if (vendors != null && vendors.hasNext()) {
|
||||
next = vendors.next();
|
||||
} else {
|
||||
next = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vendor next() {
|
||||
Vendor current = next;
|
||||
nextVendor();
|
||||
if (next == null){
|
||||
nextPlace();
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
|
||||
private class TransitVendor implements Vendor {
|
||||
|
||||
private Place place;
|
||||
|
||||
private TransitVendor(Place place) {
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
throw new UnsupportedOperationException("Is fake vendor, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Place getPlace() {
|
||||
return place;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDistance() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDistance(double distance) {
|
||||
throw new UnsupportedOperationException("Is fake vendor, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(SERVICE_TYPE service) {
|
||||
throw new UnsupportedOperationException("Is fake vendor, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(SERVICE_TYPE service) {
|
||||
throw new UnsupportedOperationException("Is fake vendor, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(SERVICE_TYPE service) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SERVICE_TYPE> getServices() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Offer addOffer(OFFER_TYPE type, Item item, double price, long count) {
|
||||
throw new UnsupportedOperationException("Is fake vendor, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Offer offer) {
|
||||
throw new UnsupportedOperationException("Is fake vendor, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Offer offer) {
|
||||
throw new UnsupportedOperationException("Is fake vendor, change unsupported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Offer> get(OFFER_TYPE type) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Offer get(OFFER_TYPE type, Item item) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(OFFER_TYPE type, Item item) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Connectable<Vendor> o) {
|
||||
double d = getDistance((Vendor)o);
|
||||
return d == 0 ? 0 : d > 0 ? 1 : -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof TransitVendor)) return false;
|
||||
TransitVendor that = (TransitVendor) o;
|
||||
return place.equals(that.place);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return place.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.trader.graph;
|
||||
|
||||
public interface Connectable<T> {
|
||||
public interface Connectable<T> extends Comparable<Connectable<T>>{
|
||||
|
||||
public double getDistance(T other);
|
||||
|
||||
|
||||
@@ -4,12 +4,11 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.core.Offer;
|
||||
import ru.trader.core.Order;
|
||||
import ru.trader.core.Place;
|
||||
import ru.trader.core.Vendor;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class PathRoute extends Path<Place> {
|
||||
public class PathRoute extends Path<Vendor> {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(PathRoute.class);
|
||||
|
||||
private final ArrayList<Order> orders = new ArrayList<>();
|
||||
@@ -21,21 +20,21 @@ public class PathRoute extends Path<Place> {
|
||||
private PathRoute tail;
|
||||
public final static Order TRANSIT = null;
|
||||
|
||||
public PathRoute(Vertex<Place> source) {
|
||||
public PathRoute(Vertex<Vendor> source) {
|
||||
this(source, false);
|
||||
}
|
||||
|
||||
public static PathRoute buildAvg(Vertex<Place> source){
|
||||
public static PathRoute buildAvg(Vertex<Vendor> source){
|
||||
return new PathRoute(source, true);
|
||||
}
|
||||
|
||||
private PathRoute(Vertex<Place> source, boolean byAvg) {
|
||||
private PathRoute(Vertex<Vendor> source, boolean byAvg) {
|
||||
super(source);
|
||||
this.byAvg = byAvg;
|
||||
}
|
||||
|
||||
|
||||
private PathRoute(PathRoute head, Vertex<Place> vertex, boolean refill) {
|
||||
private PathRoute(PathRoute head, Vertex<Vendor> vertex, boolean refill) {
|
||||
super(head, vertex, refill);
|
||||
assert head.tail == null;
|
||||
head.tail = this;
|
||||
@@ -45,7 +44,7 @@ public class PathRoute extends Path<Place> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path<Place> connectTo(Vertex<Place> vertex, boolean refill) {
|
||||
public Path<Vendor> connectTo(Vertex<Vendor> vertex, boolean refill) {
|
||||
LOG.trace("Connect path {} to {}", this, vertex);
|
||||
return new PathRoute(this.getCopy(), vertex, refill);
|
||||
}
|
||||
@@ -127,27 +126,17 @@ public class PathRoute extends Path<Place> {
|
||||
orders.clear();
|
||||
orders.add(TRANSIT);
|
||||
LOG.trace("Fill orders of path {}", this);
|
||||
Place placeSeller = getPrevious().get();
|
||||
for (Vendor seller : placeSeller.get()) {
|
||||
for (Offer sell : seller.getAllSellOffers()) {
|
||||
PathRoute p = this;
|
||||
while (p != null) {
|
||||
Place placeBuyer = p.get();
|
||||
Order best = null;
|
||||
for (Vendor buyer : placeBuyer.get()) {
|
||||
Offer buy = buyer.getBuy(sell.getItem());
|
||||
if (buy != null){
|
||||
Order order = new Order(sell, buy);
|
||||
if (best == null || best.compareTo(order) < 0){
|
||||
best = order;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (best != null){
|
||||
addOrder(best);
|
||||
}
|
||||
p = p.getNext();
|
||||
Vendor seller = getPrevious().get();
|
||||
for (Offer sell : seller.getAllSellOffers()) {
|
||||
PathRoute p = this;
|
||||
while (p != null) {
|
||||
Vendor buyer = p.get();
|
||||
Offer buy = buyer.getBuy(sell.getItem());
|
||||
if (buy != null){
|
||||
Order order = new Order(sell, buy);
|
||||
addOrder(order);
|
||||
}
|
||||
p = p.getNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,7 +214,7 @@ public class PathRoute extends Path<Place> {
|
||||
PathRoute p = getPrevious();
|
||||
balance = p.balance;
|
||||
if (!p.isRoot()) {
|
||||
Place buyer = p.get();
|
||||
Vendor buyer = p.get();
|
||||
while (!p.isRoot()){
|
||||
for (Order order : p.orders) {
|
||||
if (order == TRANSIT) continue;
|
||||
@@ -267,7 +256,7 @@ public class PathRoute extends Path<Place> {
|
||||
|
||||
public double getProfit(Order order){
|
||||
if (order == TRANSIT) return getTransitProfit();
|
||||
if (isPathFrom(order.getBuyer().getPlace())) return order.getProfit() + profit;
|
||||
if (isPathFrom(order.getBuyer())) return order.getProfit() + profit;
|
||||
return hasNext() ? getNext().getProfit(order) : order.getProfit();
|
||||
}
|
||||
|
||||
@@ -340,7 +329,7 @@ public class PathRoute extends Path<Place> {
|
||||
while (p.hasNext()){
|
||||
p = p.getNext();
|
||||
// lands for sell
|
||||
if (order != null && p.isPathFrom(order.getBuyer().getPlace())){
|
||||
if (order != null && p.isPathFrom(order.getBuyer())){
|
||||
LOG.trace("{} is lands for sell by order {}", p, order);
|
||||
return res + p.getLandsCount() + 1;
|
||||
} else {
|
||||
@@ -403,15 +392,15 @@ public class PathRoute extends Path<Place> {
|
||||
|
||||
public PathRoute dropTo(Vendor vendor){
|
||||
PathRoute p = getCopy(true).getEnd();
|
||||
while (!p.isRoot() && !p.get().equals(vendor.getPlace())){
|
||||
while (!p.isRoot() && !p.get().equals(vendor)){
|
||||
p = p.getPrevious();
|
||||
}
|
||||
p.tail = null;
|
||||
return p;
|
||||
}
|
||||
|
||||
public static PathRoute toPathRoute(Place... items){
|
||||
Place t = items[0];
|
||||
public static PathRoute toPathRoute(Vendor... items){
|
||||
Vendor t = items[0];
|
||||
PathRoute path = new PathRoute(new Vertex<>(t));
|
||||
for (int i = 1; i < items.length; i++) {
|
||||
t = items[i];
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package ru.trader.graph;
|
||||
|
||||
import ru.trader.core.Place;
|
||||
import ru.trader.core.Vendor;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class RouteGraph extends Graph<Place> {
|
||||
public class RouteGraph extends Graph<Vendor> {
|
||||
|
||||
private double balance;
|
||||
private int cargo;
|
||||
@@ -28,11 +28,11 @@ public class RouteGraph extends Graph<Place> {
|
||||
return byProfitComparator.compare(p1, p2);
|
||||
};
|
||||
|
||||
public RouteGraph(Place start, Collection<Place> set, double stock, double maxDistance, boolean withRefill, int maxDeep) {
|
||||
public RouteGraph(Vendor start, Collection<Vendor> set, double stock, double maxDistance, boolean withRefill, int maxDeep) {
|
||||
this(start, set, stock, maxDistance, withRefill, maxDeep, false);
|
||||
}
|
||||
|
||||
public RouteGraph(Place start, Collection<Place> set, double stock, double maxDistance, boolean withRefill, int maxDeep, boolean groupRes) {
|
||||
public RouteGraph(Vendor start, Collection<Vendor> set, double stock, double maxDistance, boolean withRefill, int maxDeep, boolean groupRes) {
|
||||
super(start, set, stock, maxDistance, withRefill, maxDeep, groupRes ? PathRoute::buildAvg : PathRoute::new);
|
||||
if (groupRes){
|
||||
this.groupRes = maxDeep > minJumps;
|
||||
@@ -48,7 +48,7 @@ public class RouteGraph extends Graph<Place> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TopList<Path<Place>> newTopList(int count) {
|
||||
protected TopList<Path<Vendor>> newTopList(int count) {
|
||||
int groupSize = 0;
|
||||
if (groupRes && getMinJumps() > 1){
|
||||
groupSize = Math.floorDiv(count, root.getLevel());
|
||||
@@ -56,7 +56,7 @@ public class RouteGraph extends Graph<Place> {
|
||||
return new TopRoutes(count, groupSize);
|
||||
}
|
||||
|
||||
private class TopRoutes extends TopList<Path<Place>> {
|
||||
private class TopRoutes extends TopList<Path<Vendor>> {
|
||||
private final int groupSize;
|
||||
|
||||
public TopRoutes(int limit, int groupSize) {
|
||||
@@ -65,7 +65,7 @@ public class RouteGraph extends Graph<Place> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Path<Place> entry) {
|
||||
public boolean add(Path<Vendor> entry) {
|
||||
if (comparator != null){
|
||||
((PathRoute)entry).sort(balance, cargo);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package ru.trader.graph;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.core.Place;
|
||||
import ru.trader.core.Vendor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -29,27 +30,27 @@ public class RouteSearcher {
|
||||
this.segmentSize = segmentSize;
|
||||
}
|
||||
|
||||
public List<PathRoute> getPaths(Place from, Place to, Collection<Place> places, int jumps, double balance, int cargo, int limit){
|
||||
return POOL.invoke(new SegmentSearcher(from, to, places, jumps, balance, cargo, limit));
|
||||
public List<PathRoute> getPaths(Vendor from, Vendor to, Collection<Vendor> vendors, int jumps, double balance, int cargo, int limit){
|
||||
return POOL.invoke(new SegmentSearcher(from, to, vendors, jumps, balance, cargo, limit));
|
||||
}
|
||||
|
||||
public List<PathRoute> getPaths(Place from, Collection<Place> places, int jumps, double balance, int cargo, int limit){
|
||||
return POOL.invoke(new SegmentSearcher(from, null, places, jumps, balance, cargo, limit));
|
||||
public List<PathRoute> getPaths(Vendor from, Collection<Vendor> vendors, int jumps, double balance, int cargo, int limit){
|
||||
return POOL.invoke(new SegmentSearcher(from, null, vendors, jumps, balance, cargo, limit));
|
||||
}
|
||||
|
||||
public class SegmentSearcher extends RecursiveTask<List<PathRoute>> {
|
||||
protected final Place source;
|
||||
protected final Place target;
|
||||
protected final Collection<Place> places;
|
||||
protected final Vendor source;
|
||||
protected final Vendor target;
|
||||
protected final Collection<Vendor> vendors;
|
||||
protected final int jumps;
|
||||
protected final double balance;
|
||||
protected final int cargo;
|
||||
protected int limit;
|
||||
|
||||
public SegmentSearcher(Place source, Place target, Collection<Place> places, int jumps, double balance, int cargo, int limit) {
|
||||
public SegmentSearcher(Vendor source, Vendor target, Collection<Vendor> vendors, int jumps, double balance, int cargo, int limit) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
this.places = places;
|
||||
this.vendors = vendors;
|
||||
this.jumps = jumps;
|
||||
this.balance = balance;
|
||||
this.cargo = cargo;
|
||||
@@ -59,7 +60,7 @@ public class RouteSearcher {
|
||||
@Override
|
||||
protected List<PathRoute> compute() {
|
||||
LOG.trace("Start search route to {} from {}, jumps {}", source, target, jumps);
|
||||
RouteGraph sGraph = new RouteGraph(source, places, stock, maxDistance, true, jumps, true);
|
||||
RouteGraph sGraph = new RouteGraph(source, vendors, stock, maxDistance, true, jumps, true);
|
||||
int jumpsToAll = sGraph.getMinJumps();
|
||||
LOG.trace("Segment jumps {}", jumpsToAll);
|
||||
sGraph.setCargo(cargo);
|
||||
@@ -67,18 +68,18 @@ public class RouteSearcher {
|
||||
List<PathRoute> res = new ArrayList<>(limit);
|
||||
if (jumps <= jumpsToAll){
|
||||
LOG.trace("Is last segment");
|
||||
List<Path<Place>> paths;
|
||||
List<Path<Vendor>> paths;
|
||||
if (target == null){
|
||||
paths = sGraph.getPaths(limit);
|
||||
} else {
|
||||
paths = sGraph.getPathsTo(target, limit);
|
||||
}
|
||||
for (Path<Place> path : paths) {
|
||||
for (Path<Vendor> path : paths) {
|
||||
res.add((PathRoute) path);
|
||||
}
|
||||
} else {
|
||||
LOG.trace("Split to segments");
|
||||
List<Path<Place>> paths = sGraph.getPaths(getPathsOnSegmentCount(sGraph), jumpsToAll-1).getList();
|
||||
List<Path<Vendor>> paths = sGraph.getPaths(getPathsOnSegmentCount(sGraph), jumpsToAll-1).getList();
|
||||
int i = 0;
|
||||
ArrayList<SegmentSearcher> subTasks = new ArrayList<>(THRESHOLD);
|
||||
while (i < paths.size()) {
|
||||
@@ -86,7 +87,7 @@ public class RouteSearcher {
|
||||
for (int taskIndex = 0; taskIndex < THRESHOLD && i+taskIndex < paths.size(); taskIndex++) {
|
||||
PathRoute path = (PathRoute) paths.get(i+taskIndex);
|
||||
double newBalance = balance + path.getRoot().getProfit();
|
||||
SegmentSearcher task = new SegmentSearcher(path.get(), target, places, jumps - path.getLength(), newBalance, cargo, 1);
|
||||
SegmentSearcher task = new SegmentSearcher(path.get(), target, vendors, jumps - path.getLength(), newBalance, cargo, 1);
|
||||
task.fork();
|
||||
subTasks.add(task);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user