performance and testing

This commit is contained in:
Leijurv 2018-08-19 09:37:35 -07:00
parent 494a4a8d18
commit ff108a55e6
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
4 changed files with 41 additions and 29 deletions

View File

@ -95,7 +95,7 @@ public class PathNode {
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
throw new IllegalStateException(); return pos.hashCode() * 7 + 3;
} }
@Override @Override

View File

@ -53,27 +53,41 @@ public class BinaryHeapOpenSet implements IOpenSet {
} }
@Override @Override
public void insert(PathNode value) { public final void insert(PathNode value) {
if (size >= array.length - 1) { if (size >= array.length - 1) {
array = Arrays.copyOf(array, array.length * 2); array = Arrays.copyOf(array, array.length * 2);
} }
size++; size++;
value.heapPosition = size; value.heapPosition = size;
array[size] = value; array[size] = value;
upHeap(size); update(value);
}
public void update(PathNode node) {
upHeap(node.heapPosition);
} }
@Override @Override
public boolean isEmpty() { public final void update(PathNode node) {
int index = node.heapPosition;
int parentInd = index >>> 1;
PathNode val = array[index];
double cost = val.combinedCost;
PathNode parentNode = array[parentInd];
while (index > 1 && parentNode.combinedCost > cost) {
array[index] = parentNode;
array[parentInd] = val;
val.heapPosition = parentInd;
parentNode.heapPosition = index;
index = parentInd;
parentInd = index >>> 1;
parentNode = array[parentInd];
}
}
@Override
public final boolean isEmpty() {
return size == 0; return size == 0;
} }
@Override @Override
public PathNode removeLowest() { public final PathNode removeLowest() {
if (size == 0) { if (size == 0) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@ -115,20 +129,4 @@ public class BinaryHeapOpenSet implements IOpenSet {
} while (smallerChild <= size); } while (smallerChild <= size);
return result; return result;
} }
private void upHeap(int index) {
int parentInd = index >>> 1;
PathNode val = array[index];
double cost = val.combinedCost;
PathNode parentNode = array[parentInd];
while (index > 1 && parentNode.combinedCost > cost) {
array[index] = parentNode;
array[parentInd] = val;
val.heapPosition = parentInd;
parentNode.heapPosition = index;
index = parentInd;
parentInd = index >>> 1;
parentNode = array[parentInd];
}
}
} }

View File

@ -60,12 +60,12 @@ public class BetterBlockPos extends BlockPos {
} }
@Override @Override
public int hashCode() { public final int hashCode() {
return hashCode; return hashCode;
} }
@Override @Override
public boolean equals(Object o) { public final boolean equals(Object o) {
if (o == null) { if (o == null) {
return false; return false;
} }

View File

@ -18,8 +18,9 @@
package baritone.bot.pathing.calc.openset; package baritone.bot.pathing.calc.openset;
import baritone.bot.pathing.calc.PathNode; import baritone.bot.pathing.calc.PathNode;
import baritone.bot.pathing.goals.GoalBlock; import baritone.bot.pathing.goals.Goal;
import baritone.bot.utils.pathing.BetterBlockPos; import baritone.bot.utils.pathing.BetterBlockPos;
import net.minecraft.util.math.BlockPos;
import org.junit.Test; import org.junit.Test;
import java.util.*; import java.util.*;
@ -73,7 +74,20 @@ public class OpenSetsTest {
// generate the pathnodes that we'll be testing the sets on // generate the pathnodes that we'll be testing the sets on
PathNode[] toInsert = new PathNode[size]; PathNode[] toInsert = new PathNode[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
PathNode pn = new PathNode(new BetterBlockPos(0, 0, 0), new GoalBlock(new BetterBlockPos(0, 0, 0))); // can't use an existing goal
// because they use Baritone.settings()
// and we can't do that because Minecraft itself isn't initted
PathNode pn = new PathNode(new BetterBlockPos(0, 0, 0), new Goal() {
@Override
public boolean isInGoal(BlockPos pos) {
return false;
}
@Override
public double heuristic(BlockPos pos) {
return 0;
}
});
pn.combinedCost = Math.random(); pn.combinedCost = Math.random();
toInsert[i] = pn; toInsert[i] = pn;
} }