pick map over hybrid, due to performance

This commit is contained in:
Leijurv 2018-12-02 19:39:10 -08:00
parent 36cfcbbbe3
commit 17207d44e6
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
5 changed files with 27 additions and 73 deletions

View File

@ -419,7 +419,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
failureTimeout = Baritone.settings().planAheadFailureTimeoutMS.get();
}
CalculationContext context = new CalculationContext(baritone, true); // not safe to create on the other thread, it looks up a lot of stuff in minecraft
AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, current == null ? null : current.getPath(), context, true);
AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, current == null ? null : current.getPath(), context);
if (!Objects.equals(pathfinder.getGoal(), goal)) {
logDebug("Simplifying " + goal.getClass() + " to GoalXZ due to distance");
}
@ -495,9 +495,9 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
});
}
public static AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal, IPath previous, CalculationContext context, boolean allowSimplifyUnloaded) {
public static AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal, IPath previous, CalculationContext context) {
Goal transformed = goal;
if (Baritone.settings().simplifyUnloadedYCoord.get() && goal instanceof IGoalRenderPos && allowSimplifyUnloaded) {
if (Baritone.settings().simplifyUnloadedYCoord.get() && goal instanceof IGoalRenderPos) {
BlockPos pos = ((IGoalRenderPos) goal).getGoalPos();
if (context.world().getChunk(pos) instanceof EmptyChunk) {
transformed = new GoalXZ(pos.getX(), pos.getZ());

View File

@ -76,9 +76,8 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
int numNodes = 0;
int numMovementsConsidered = 0;
int numEmptyChunk = 0;
boolean favoring = favored != null && !favored.isEmpty();
boolean favoring = !favored.isEmpty();
int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.get(); // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior
double favorCoeff = Baritone.settings().backtrackCostFavoringCoefficient.get();
boolean minimumImprovementRepropagation = Baritone.settings().minimumImprovementRepropagation.get();
while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && !cancelRequested) {
long now = System.nanoTime() / 1000000L;
@ -96,7 +95,6 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
mostRecentConsidered = currentNode;
numNodes++;
if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) {
favored.printStats();
logDebug("Took " + (System.nanoTime() / 1000000L - startTime) + "ms, " + numMovementsConsidered + " movements considered");
return Optional.of(new Path(startNode, currentNode, numNodes, goal, calcContext));
}
@ -139,7 +137,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
long hashCode = BetterBlockPos.longHash(res.x, res.y, res.z);
if (favoring) {
// see issue #18
actionCost *= favored.calculate(res.x, res.y, res.z, hashCode);
actionCost *= favored.calculate(hashCode);
}
PathNode neighbor = getNodeAtPosition(res.x, res.y, res.z, hashCode);
double tentativeCost = currentNode.cost + actionCost;
@ -177,7 +175,6 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
}
}
}
favored.printStats();
if (cancelRequested) {
return Optional.empty();
}

View File

@ -29,12 +29,12 @@ import java.util.Collections;
import java.util.List;
public class Avoidance {
public final int centerX;
public final int centerY;
public final int centerZ;
public final double coefficient;
public final int radius;
public final int radiusSq;
private final int centerX;
private final int centerY;
private final int centerZ;
private final double coefficient;
private final int radius;
private final int radiusSq;
public Avoidance(BlockPos center, double coefficient, int radius) {
this(center.getX(), center.getY(), center.getZ(), coefficient, radius);

View File

@ -22,79 +22,36 @@ import baritone.api.pathing.calc.IPath;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.IPlayerContext;
import it.unimi.dsi.fastutil.longs.Long2DoubleOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import java.util.List;
public class Favoring {
private final LongOpenHashSet backtrackFavored = new LongOpenHashSet();
private final double backtrackCoefficient;
private final List<Avoidance> avoidances;
private final Long2DoubleOpenHashMap favorings = new Long2DoubleOpenHashMap();
private long mapMethodNano = 0;
private long hybridMethodNano = 0;
public final class Favoring {
private List<Avoidance> avoidances;
private final Long2DoubleOpenHashMap favorings;
public Favoring(IPlayerContext ctx, IPath previous) {
long start = System.currentTimeMillis();
backtrackCoefficient = Baritone.settings().backtrackCostFavoringCoefficient.get();
if (backtrackCoefficient != 1D && previous != null) {
previous.positions().forEach(pos -> backtrackFavored.add(BetterBlockPos.longHash(pos)));
}
this(previous);
avoidances = Avoidance.create(ctx);
long end = System.currentTimeMillis();
System.out.println("Hybrid method init took " + (end - start) + "ms");
favorings.defaultReturnValue(1.0D);
backtrackFavored.forEach(l -> favorings.put((long) l, backtrackCoefficient));
for (Avoidance avoid : avoidances) {
avoid.applySpherical(favorings);
}
long end2 = System.currentTimeMillis();
System.out.println("Map method init took " + (end2 - end) + "ms");
System.out.println("Size: " + favorings.size());
System.out.println("Favoring size: " + favorings.size());
}
public void printStats() {
System.out.println("Map method nanos: " + mapMethodNano);
System.out.println("Hybrid method nano: " + hybridMethodNano);
public Favoring(IPath previous) { // create one just from previous path, no mob avoidances
favorings = new Long2DoubleOpenHashMap();
favorings.defaultReturnValue(1.0D);
double coeff = Baritone.settings().backtrackCostFavoringCoefficient.get();
if (coeff != 1D && previous != null) {
previous.positions().forEach(pos -> favorings.put(BetterBlockPos.longHash(pos), coeff));
}
}
public boolean isEmpty() {
return backtrackFavored.isEmpty() && favorings.isEmpty() && avoidances.isEmpty();
return favorings.isEmpty();
}
public double calculate(int x, int y, int z, long hash) {
long start = System.nanoTime();
double result = calculateMap(x, y, z, hash);
long mid = System.nanoTime();
double result2 = calculateHybrid(x, y, z, hash);
long end = System.nanoTime();
mapMethodNano += mid - start;
hybridMethodNano += end - mid;
if (result != result2) {
System.out.println("NO MATCH " + result + " " + result2);
}
return result;
}
public double calculateMap(int x, int y, int z, long hash) {
public double calculate(long hash) {
return favorings.get(hash);
}
public double calculateHybrid(int x, int y, int z, long hash) {
double result = 1.0D;
if (backtrackFavored.contains(hash)) {
result *= backtrackCoefficient;
}
for (Avoidance avoid : avoidances) {
result *= avoid.coefficient(x, y, z);
}
return result;
}
}

View File

@ -22,8 +22,8 @@ import baritone.api.pathing.calc.IPath;
import baritone.api.pathing.goals.Goal;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.PathCalculationResult;
import baritone.behavior.PathingBehavior;
import baritone.cache.CachedWorld;
import baritone.pathing.calc.AStarPathFinder;
import baritone.pathing.calc.AbstractNodeCostSearch;
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.path.SplicedPath;
@ -87,7 +87,7 @@ public class SegmentedCalculator {
private PathCalculationResult segment(Optional<IPath> previous) {
BetterBlockPos segmentStart = previous.map(IPath::getDest).orElse(start); // <-- e p i c
AbstractNodeCostSearch search = PathingBehavior.createPathfinder(segmentStart, goal, previous.orElse(null), context, false);
AbstractNodeCostSearch search = new AStarPathFinder(segmentStart.x, segmentStart.y, segmentStart.z, goal, new Favoring(previous.orElse(null)), context); // this is on another thread, so cannot include mob avoidances.
return search.calculate(Baritone.settings().primaryTimeoutMS.get(), Baritone.settings().failureTimeoutMS.get()); // use normal time settings, not the plan ahead settings, so as to not overwhelm the computer
}