Archived
0

implement loop specificator

This commit is contained in:
iMoHax
2015-08-20 17:35:30 +03:00
parent 2d6c0f8f9e
commit 88fb2fcaf9
9 changed files with 131 additions and 44 deletions

View File

@@ -10,10 +10,16 @@ import java.util.function.Consumer;
public abstract class AbstractCrawlerSpecification implements CrawlerSpecification {
private final RouteSpecification<Vendor> routeSpecification;
private final Consumer<List<Edge<Vendor>>> onFoundFunc;
private final boolean loop;
protected AbstractCrawlerSpecification(RouteSpecification<Vendor> routeSpecification, Consumer<List<Edge<Vendor>>> onFoundFunc) {
protected AbstractCrawlerSpecification(RouteSpecification<Vendor> routeSpecification, Consumer<List<Edge<Vendor>>> onFoundFunc, boolean loop) {
this.routeSpecification = routeSpecification;
this.onFoundFunc = onFoundFunc;
this.loop = loop;
}
protected boolean isLoop() {
return loop;
}
@Override

View File

@@ -10,28 +10,33 @@ import java.util.function.Consumer;
public class CrawlerSpecificationByProfit extends AbstractCrawlerSpecification {
public CrawlerSpecificationByProfit(Consumer<List<Edge<Vendor>>> onFoundFunc) {
super(null, onFoundFunc);
super(null, onFoundFunc, false);
}
public CrawlerSpecificationByProfit(RouteSpecification<Vendor> routeSpecification, Consumer<List<Edge<Vendor>>> onFoundFunc) {
super(routeSpecification, onFoundFunc);
public CrawlerSpecificationByProfit(RouteSpecification<Vendor> routeSpecification, Consumer<List<Edge<Vendor>>> onFoundFunc, boolean loop) {
super(routeSpecification, onFoundFunc, loop);
}
@Override
public double computeWeight(VendorsCrawler.VendorsEdge edge) {
return edge.getTime()/edge.getProfitByTonne();
double profit = edge.getProfitByTonne();
return profit > 0 ? edge.getTime()/profit : edge.getTime();
}
@Override
public double computeWeight(VendorsCrawler.VendorsTraversalEntry entry) {
double profit = 0; double time = 0;
Iterator<Edge<Vendor>> iterator = entry.routeIterator();
boolean first = true;
while (iterator.hasNext()){
VendorsCrawler.VendorsEdge edge = (VendorsCrawler.VendorsEdge)iterator.next();
if (edge != null){
profit += edge.getProfitByTonne();
time += edge.getTime();
if (!isLoop() || first || iterator.hasNext()){
profit += edge.getProfitByTonne();
time += edge.getTime();
}
}
first = false;
}
return profit > 1 ? time / profit : time;
}

View File

@@ -9,29 +9,35 @@ import java.util.function.Consumer;
public class CrawlerSpecificationByTime extends AbstractCrawlerSpecification {
public CrawlerSpecificationByTime(Consumer<List<Edge<Vendor>>> onFoundFunc) {
super(null, onFoundFunc);
super(null, onFoundFunc, false);
}
public CrawlerSpecificationByTime(RouteSpecification<Vendor> routeSpecification, Consumer<List<Edge<Vendor>>> onFoundFunc) {
super(routeSpecification, onFoundFunc);
public CrawlerSpecificationByTime(RouteSpecification<Vendor> routeSpecification, Consumer<List<Edge<Vendor>>> onFoundFunc, boolean loop) {
super(routeSpecification, onFoundFunc, loop);
}
@Override
public double computeWeight(VendorsCrawler.VendorsEdge edge) {
return edge.getTime();
double profit = edge.getProfitByTonne();
return profit > 0 ? edge.getTime() + 1/profit : edge.getTime();
}
@Override
public double computeWeight(VendorsCrawler.VendorsTraversalEntry entry) {
double time = 0;
double profit = 0; long time = 0;
Iterator<Edge<Vendor>> iterator = entry.routeIterator();
boolean first = true;
while (iterator.hasNext()){
VendorsCrawler.VendorsEdge edge = (VendorsCrawler.VendorsEdge)iterator.next();
if (edge != null){
time += edge.getTime();
if (!isLoop() || first || iterator.hasNext()){
profit += edge.getProfitByTonne();
time += edge.getTime();
}
}
first = false;
}
return time;
return profit > 0 ? time + 1/profit : time + 0.999999999999999d;
}
}

View File

@@ -64,7 +64,7 @@ public class CrawlerSpecificator {
this.offers.addAll(offers);
}
public CrawlerSpecification build(Consumer<List<Edge<Vendor>>> onFoundFunc, RouteSpecification<Vendor> andSpec){
public CrawlerSpecification build(Consumer<List<Edge<Vendor>>> onFoundFunc, RouteSpecification<Vendor> andSpec, boolean loop){
RouteSpecification<Vendor> spec;
RouteSpecification<Vendor> res = null;
if (!all.isEmpty()){
@@ -95,13 +95,13 @@ public class CrawlerSpecificator {
}
}
if (byTime){
return new CrawlerSpecificationByTime(res, onFoundFunc);
return new CrawlerSpecificationByTime(res, onFoundFunc, loop);
}
return new CrawlerSpecificationByProfit(res, onFoundFunc);
return new CrawlerSpecificationByProfit(res, onFoundFunc, loop);
}
public CrawlerSpecification build(Consumer<List<Edge<Vendor>>> onFoundFunc){
return build(onFoundFunc, null);
return build(onFoundFunc, null, false);
}
}

View File

@@ -114,13 +114,19 @@ public class RouteSearcher {
vGraph.build(source, vendors);
LOG.trace("Graph is builds");
RouteCollector collector = new RouteCollector();
Crawler<Vendor> crawler = vGraph.crawler(specificator.build(collector::add, new LoopRouteSpecification<>(true)), callback);
Crawler<Vendor> crawler = vGraph.crawler(specificator.build(collector::add, new LoopRouteSpecification<>(true), true), callback);
crawler.setMaxSize(scorer.getProfile().getLands());
crawler.findMin(source, count);
crawler = vGraph.crawler(specificator.build(collector::add, new RouteSpecificationByTarget<>(source)), callback);
crawler = vGraph.crawler(specificator.build(collector::add, new RouteSpecificationByTarget<>(source), false), callback);
crawler.setMaxSize(scorer.getProfile().getLands());
crawler.findMin(source, 1);
return collector.get();
List<Route> routes = collector.get();
routes.sort((r1, r2) -> {
double s1 = (r1.getProfit() - r1.getEntries().get(0).getProfit()) / r1.getTime();
double s2 = (r2.getProfit() - r2.getEntries().get(0).getProfit()) / r2.getTime();
return Double.compare(s2, s1);
});
return routes;
}
private class RouteCollector {

View File

@@ -6,16 +6,17 @@ import ru.trader.core.Order;
import ru.trader.core.Vendor;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class VendorsCrawler extends Crawler<Vendor> {
private double startFuel;
private double startBalance;
private final CrawlerSpecification specification;
public VendorsCrawler(VendorsGraph graph, CrawlerSpecification specification, AnalysisCallBack callback) {
super(graph, specification.routeSpecification(), specification.onFoundFunc(), callback);
this.specification = specification;
startFuel = graph.getProfile().getShip().getTank();
startBalance = graph.getProfile().getBalance();
}
@@ -83,16 +84,7 @@ public class VendorsCrawler extends Crawler<Vendor> {
@Override
public double getWeight() {
if (weight == null){
double profit = 0; double time = 0;
Iterator<Edge<Vendor>> iterator = routeIterator();
while (iterator.hasNext()){
VendorsEdge edge = (VendorsEdge)iterator.next();
if (edge != null){
profit += edge.getProfitByTonne();
time += edge.getTime();
}
}
weight = profit > 1 ? time / profit : time;
weight = specification.computeWeight(this);
}
return weight;
}
@@ -191,7 +183,7 @@ public class VendorsCrawler extends Crawler<Vendor> {
@Override
protected double computeWeight() {
return getTime()/getProfitByTonne();
return specification.computeWeight(this);
}
@Override

View File

@@ -28,18 +28,10 @@ public class VendorsGraph extends ConnectibleGraph<Vendor> {
return new VendorsCrawler(this, new CrawlerSpecificationByProfit(onFoundFunc), callback);
}
public VendorsCrawler crawler(RouteSpecification<Vendor> specification, Consumer<List<Edge<Vendor>>> onFoundFunc, AnalysisCallBack callback){
return new VendorsCrawler(this, new CrawlerSpecificationByProfit(specification, onFoundFunc), callback);
}
public VendorsCrawler crawlerByTime(Consumer<List<Edge<Vendor>>> onFoundFunc, AnalysisCallBack callback){
return new VendorsCrawler(this, new CrawlerSpecificationByTime(onFoundFunc), callback);
}
public VendorsCrawler crawlerByTime(RouteSpecification<Vendor> specification, Consumer<List<Edge<Vendor>>> onFoundFunc, AnalysisCallBack callback){
return new VendorsCrawler(this, new CrawlerSpecificationByTime(specification, onFoundFunc), callback);
}
public VendorsCrawler crawler(CrawlerSpecification specification, AnalysisCallBack callback){
return new VendorsCrawler(this, specification, callback);
}