small refactoring
This commit is contained in:
@@ -13,22 +13,28 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class CCrawler<T extends Connectable<T>> extends Crawler<T> {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(CCrawler.class);
|
||||
private double startFuel;
|
||||
|
||||
public CCrawler(ConnectibleGraph<T> graph, Consumer<List<Edge<T>>> onFoundFunc) {
|
||||
super(graph, onFoundFunc);
|
||||
startFuel = getShip().getTank();
|
||||
}
|
||||
|
||||
protected Ship getShip(){
|
||||
return ((ConnectibleGraph<T>)graph).getProfile().getShip();
|
||||
return ((ConnectibleGraph<T>)graph).getShip();
|
||||
}
|
||||
|
||||
protected Profile getProfile(){
|
||||
return ((ConnectibleGraph<T>)graph).getProfile();
|
||||
}
|
||||
|
||||
public void setStartFuel(double startFuel) {
|
||||
this.startFuel = startFuel;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CCostTraversalEntry start(Vertex<T> vertex) {
|
||||
double fuel = getShip().getTank();
|
||||
double fuel = startFuel;
|
||||
return new CCostTraversalEntry(vertex, fuel);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.analysis.AnalysisCallBack;
|
||||
import ru.trader.core.Profile;
|
||||
import ru.trader.core.Ship;
|
||||
import ru.trader.graph.Connectable;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -27,6 +28,10 @@ public class ConnectibleGraph<T extends Connectable<T>> extends AbstractGraph<T>
|
||||
public Profile getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
protected Ship getShip(){
|
||||
return profile.getShip();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GraphBuilder createGraphBuilder(Vertex<T> vertex, Collection<T> set, int deep, double limit) {
|
||||
@@ -34,7 +39,7 @@ public class ConnectibleGraph<T extends Connectable<T>> extends AbstractGraph<T>
|
||||
}
|
||||
|
||||
public void build(T start, Collection<T> set){
|
||||
super.build(start, set, profile.getJumps(), profile.getShip().getTank());
|
||||
super.build(start, set, profile.getJumps(), getShip().getTank());
|
||||
}
|
||||
|
||||
private class DistanceFilter implements Predicate<Double> {
|
||||
@@ -48,7 +53,7 @@ public class ConnectibleGraph<T extends Connectable<T>> extends AbstractGraph<T>
|
||||
|
||||
@Override
|
||||
public boolean test(Double distance) {
|
||||
return distance <= profile.getShip().getJumpRange(limit) || (profile.withRefill() && distance <= profile.getShip().getJumpRange() && source.canRefill());
|
||||
return distance <= getShip().getJumpRange(limit) || (profile.withRefill() && distance <= getShip().getJumpRange() && source.canRefill());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,13 +74,13 @@ public class ConnectibleGraph<T extends Connectable<T>> extends AbstractGraph<T>
|
||||
LOG.trace("Vertex {} is far away, {}", entry, distance);
|
||||
return -1;
|
||||
}
|
||||
fuelCost = profile.getShip().getFuelCost(limit, distance);
|
||||
double nextLimit = profile.withRefill() ? limit - fuelCost : profile.getShip().getTank();
|
||||
fuelCost = getShip().getFuelCost(limit, distance);
|
||||
double nextLimit = profile.withRefill() ? limit - fuelCost : getShip().getTank();
|
||||
if (nextLimit < 0) {
|
||||
LOG.trace("Refill");
|
||||
refill = true;
|
||||
fuelCost = profile.getShip().getFuelCost(distance);
|
||||
nextLimit = profile.getShip().getTank() - fuelCost;
|
||||
fuelCost = getShip().getFuelCost(distance);
|
||||
nextLimit = getShip().getTank() - fuelCost;
|
||||
} else {
|
||||
refill = false;
|
||||
}
|
||||
|
||||
@@ -42,14 +42,29 @@ public class Crawler<T> {
|
||||
}
|
||||
|
||||
public void findFast(T target, int count){
|
||||
findFast(graph.getRoot(), target, count);
|
||||
}
|
||||
|
||||
public void findFast(T source, T target){
|
||||
findFast(source, target, 1);
|
||||
}
|
||||
|
||||
public void findFast(T source, T target, int count){
|
||||
Optional<Vertex<T>> s = graph.getVertex(source);
|
||||
if (s.isPresent()){
|
||||
findFast(s.get(), target, count);
|
||||
}
|
||||
}
|
||||
|
||||
private void findFast(Vertex<T> s, T target, int count){
|
||||
Optional<Vertex<T>> t = graph.getVertex(target);
|
||||
int found = 0;
|
||||
if (t.isPresent()) {
|
||||
if (count > 1) {
|
||||
int maxDeep = maxSize - (graph.getRoot().isEntry(target) ? graph.getMinJumps() * 2 : graph.getMinJumps());
|
||||
found = bfs(start(graph.getRoot()), target, maxDeep, count);
|
||||
int maxDeep = maxSize - (s.isEntry(target) ? graph.getMinJumps() * 2 : graph.getMinJumps());
|
||||
found = bfs(start(s), target, maxDeep, count);
|
||||
} else {
|
||||
found = dfs(start(graph.getRoot()), target, t.get().getLevel() + 1, count);
|
||||
found = dfs(start(s), target, t.get().getLevel() + 1, count);
|
||||
}
|
||||
}
|
||||
LOG.debug("Found {} paths", found);
|
||||
@@ -60,15 +75,30 @@ public class Crawler<T> {
|
||||
}
|
||||
|
||||
public void findMin(T target, int count){
|
||||
findMin(graph.getRoot(), target, count);
|
||||
}
|
||||
|
||||
public void findMin(T source, T target){
|
||||
findMin(source, target, 1);
|
||||
}
|
||||
|
||||
public void findMin(T source, T target, int count){
|
||||
Optional<Vertex<T>> s = graph.getVertex(source);
|
||||
if (s.isPresent()){
|
||||
findMin(s.get(), target, count);
|
||||
}
|
||||
}
|
||||
|
||||
public void findMin(Vertex<T> s, T target, int count){
|
||||
Optional<Vertex<T>> t = graph.getVertex(target);
|
||||
int found = 0;
|
||||
if (t.isPresent()) {
|
||||
int maxDeep = graph.getRoot().isEntry(target) ? maxSize - graph.getMinJumps() * 2 : graph.getMinLevel();
|
||||
int maxDeep = s.isEntry(target) ? maxSize - graph.getMinJumps() * 2 : graph.getMinLevel();
|
||||
if (maxDeep < 0) maxDeep = 0;
|
||||
if (maxSize - maxDeep <= SPLIT_SIZE){
|
||||
found = ucs(start(graph.getRoot()), target, maxDeep, count);
|
||||
found = ucs(start(s), target, maxDeep, count);
|
||||
} else {
|
||||
found = ucs2(start(graph.getRoot()), target, maxDeep, count);
|
||||
found = ucs2(start(s), target, maxDeep, count);
|
||||
}
|
||||
}
|
||||
LOG.debug("Found {} paths", found);
|
||||
|
||||
@@ -4,6 +4,7 @@ public class Profile {
|
||||
|
||||
private double balance;
|
||||
private int jumps;
|
||||
private int lands;
|
||||
private Ship ship;
|
||||
private boolean refill;
|
||||
private int routesCount;
|
||||
@@ -18,6 +19,7 @@ public class Profile {
|
||||
this.ship = ship;
|
||||
refill = true;
|
||||
jumps = 6;
|
||||
lands = 4;
|
||||
scoreOrdersCount = 5;
|
||||
distanceMult = 0.8;
|
||||
landMult = 0.95;
|
||||
@@ -41,6 +43,14 @@ public class Profile {
|
||||
this.jumps = jumps;
|
||||
}
|
||||
|
||||
public int getLands() {
|
||||
return lands;
|
||||
}
|
||||
|
||||
public void setLands(int lands) {
|
||||
this.lands = lands;
|
||||
}
|
||||
|
||||
public Ship getShip() {
|
||||
return ship;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user