Archived
0

small refactoring

This commit is contained in:
iMoHax
2015-06-30 16:32:06 +03:00
parent 42bd77288c
commit 3f2ed90df8
4 changed files with 65 additions and 14 deletions

View File

@@ -13,22 +13,28 @@ import java.util.stream.Collectors;
public class CCrawler<T extends Connectable<T>> extends Crawler<T> { public class CCrawler<T extends Connectable<T>> extends Crawler<T> {
private final static Logger LOG = LoggerFactory.getLogger(CCrawler.class); private final static Logger LOG = LoggerFactory.getLogger(CCrawler.class);
private double startFuel;
public CCrawler(ConnectibleGraph<T> graph, Consumer<List<Edge<T>>> onFoundFunc) { public CCrawler(ConnectibleGraph<T> graph, Consumer<List<Edge<T>>> onFoundFunc) {
super(graph, onFoundFunc); super(graph, onFoundFunc);
startFuel = getShip().getTank();
} }
protected Ship getShip(){ protected Ship getShip(){
return ((ConnectibleGraph<T>)graph).getProfile().getShip(); return ((ConnectibleGraph<T>)graph).getShip();
} }
protected Profile getProfile(){ protected Profile getProfile(){
return ((ConnectibleGraph<T>)graph).getProfile(); return ((ConnectibleGraph<T>)graph).getProfile();
} }
public void setStartFuel(double startFuel) {
this.startFuel = startFuel;
}
@Override @Override
protected CCostTraversalEntry start(Vertex<T> vertex) { protected CCostTraversalEntry start(Vertex<T> vertex) {
double fuel = getShip().getTank(); double fuel = startFuel;
return new CCostTraversalEntry(vertex, fuel); return new CCostTraversalEntry(vertex, fuel);
} }

View File

@@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ru.trader.analysis.AnalysisCallBack; import ru.trader.analysis.AnalysisCallBack;
import ru.trader.core.Profile; import ru.trader.core.Profile;
import ru.trader.core.Ship;
import ru.trader.graph.Connectable; import ru.trader.graph.Connectable;
import java.util.Collection; import java.util.Collection;
@@ -28,13 +29,17 @@ public class ConnectibleGraph<T extends Connectable<T>> extends AbstractGraph<T>
return profile; return profile;
} }
protected Ship getShip(){
return profile.getShip();
}
@Override @Override
protected GraphBuilder createGraphBuilder(Vertex<T> vertex, Collection<T> set, int deep, double limit) { protected GraphBuilder createGraphBuilder(Vertex<T> vertex, Collection<T> set, int deep, double limit) {
return new ConnectibleGraphBuilder(vertex, set, deep, limit); return new ConnectibleGraphBuilder(vertex, set, deep, limit);
} }
public void build(T start, Collection<T> set){ 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> { private class DistanceFilter implements Predicate<Double> {
@@ -48,7 +53,7 @@ public class ConnectibleGraph<T extends Connectable<T>> extends AbstractGraph<T>
@Override @Override
public boolean test(Double distance) { 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); LOG.trace("Vertex {} is far away, {}", entry, distance);
return -1; return -1;
} }
fuelCost = profile.getShip().getFuelCost(limit, distance); fuelCost = getShip().getFuelCost(limit, distance);
double nextLimit = profile.withRefill() ? limit - fuelCost : profile.getShip().getTank(); double nextLimit = profile.withRefill() ? limit - fuelCost : getShip().getTank();
if (nextLimit < 0) { if (nextLimit < 0) {
LOG.trace("Refill"); LOG.trace("Refill");
refill = true; refill = true;
fuelCost = profile.getShip().getFuelCost(distance); fuelCost = getShip().getFuelCost(distance);
nextLimit = profile.getShip().getTank() - fuelCost; nextLimit = getShip().getTank() - fuelCost;
} else { } else {
refill = false; refill = false;
} }

View File

@@ -42,14 +42,29 @@ public class Crawler<T> {
} }
public void findFast(T target, int count){ 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); Optional<Vertex<T>> t = graph.getVertex(target);
int found = 0; int found = 0;
if (t.isPresent()) { if (t.isPresent()) {
if (count > 1) { if (count > 1) {
int maxDeep = maxSize - (graph.getRoot().isEntry(target) ? graph.getMinJumps() * 2 : graph.getMinJumps()); int maxDeep = maxSize - (s.isEntry(target) ? graph.getMinJumps() * 2 : graph.getMinJumps());
found = bfs(start(graph.getRoot()), target, maxDeep, count); found = bfs(start(s), target, maxDeep, count);
} else { } 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); LOG.debug("Found {} paths", found);
@@ -60,15 +75,30 @@ public class Crawler<T> {
} }
public void findMin(T target, int count){ 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); Optional<Vertex<T>> t = graph.getVertex(target);
int found = 0; int found = 0;
if (t.isPresent()) { 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 (maxDeep < 0) maxDeep = 0;
if (maxSize - maxDeep <= SPLIT_SIZE){ if (maxSize - maxDeep <= SPLIT_SIZE){
found = ucs(start(graph.getRoot()), target, maxDeep, count); found = ucs(start(s), target, maxDeep, count);
} else { } else {
found = ucs2(start(graph.getRoot()), target, maxDeep, count); found = ucs2(start(s), target, maxDeep, count);
} }
} }
LOG.debug("Found {} paths", found); LOG.debug("Found {} paths", found);

View File

@@ -4,6 +4,7 @@ public class Profile {
private double balance; private double balance;
private int jumps; private int jumps;
private int lands;
private Ship ship; private Ship ship;
private boolean refill; private boolean refill;
private int routesCount; private int routesCount;
@@ -18,6 +19,7 @@ public class Profile {
this.ship = ship; this.ship = ship;
refill = true; refill = true;
jumps = 6; jumps = 6;
lands = 4;
scoreOrdersCount = 5; scoreOrdersCount = 5;
distanceMult = 0.8; distanceMult = 0.8;
landMult = 0.95; landMult = 0.95;
@@ -41,6 +43,14 @@ public class Profile {
this.jumps = jumps; this.jumps = jumps;
} }
public int getLands() {
return lands;
}
public void setLands(int lands) {
this.lands = lands;
}
public Ship getShip() { public Ship getShip() {
return ship; return ship;
} }