From e81b01a8f3f15e13fd8be717ceb3150c40c5faf5 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Tue, 30 Oct 2018 17:18:08 -0700 Subject: [PATCH] fix priority allocation method --- src/main/java/tenor/AquireItemTask.java | 28 ++++++++----------- .../tenor/ScarceParentPriorityAllocator.java | 8 ++++-- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/main/java/tenor/AquireItemTask.java b/src/main/java/tenor/AquireItemTask.java index 469907d5a..3acd4e5fc 100644 --- a/src/main/java/tenor/AquireItemTask.java +++ b/src/main/java/tenor/AquireItemTask.java @@ -37,12 +37,14 @@ public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, public void reallocate() { List parents = parentTasks(); - allocation.clear(); + allocation = null; + HashMap tmp = new HashMap<>(); int amountToAllocate = getCurrentQuantityInInventory(); - int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents); + int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents).getSecond(); for (int i = 0; i < parents.size(); i++) { - allocation.put(parents.get(i), newAmounts[i]); + tmp.put(parents.get(i), newAmounts[i]); } + allocation = tmp; } public int getCurrentQuantityInInventory() { @@ -50,18 +52,8 @@ public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, } @Override - public IQuantityRelationship priority() { - return x -> { - double sum = 0; - for (Map.Entry entry : allocation.entrySet()) { - - IQuantizedChildTaskRelationship parentRelationship = entry.getKey(); - int quantityAssigned = entry.getValue(); - - sum += parentRelationship.allocatedPriority(quantityAssigned); - } - return sum; - }; + public IQuantityRelationship priority() { // TODO cache + return x -> ScarceParentPriorityAllocator.priorityAllocation(x, parentTasks()).getFirst(); } @Override @@ -72,6 +64,10 @@ public class AquireItemTask extends QuantizedTaskNode implements IClaimProvider, @Override public double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity) { // how much of our priority would go to this child if it could provide us with quantity of the item we need - return priority().value(quantity); + + // here's the thing honey, we *already have* some, so you're really asking what's the priority of getting quantity MORE + int curr = allocation.entrySet().stream().mapToInt(Map.Entry::getValue).sum(); + + return priority().value(quantity + curr) - priority().value(curr); } } diff --git a/src/main/java/tenor/ScarceParentPriorityAllocator.java b/src/main/java/tenor/ScarceParentPriorityAllocator.java index 22be4779f..b6aa1f2d5 100644 --- a/src/main/java/tenor/ScarceParentPriorityAllocator.java +++ b/src/main/java/tenor/ScarceParentPriorityAllocator.java @@ -17,12 +17,14 @@ package tenor; +import net.minecraft.util.Tuple; + import java.util.List; public class ScarceParentPriorityAllocator { - public static int[] priorityAllocation(int quantity, List parents) { + public static Tuple priorityAllocation(int quantity, List parents) { if (quantity == 0) { - return new int[parents.size()]; + return new Tuple<>(0D, new int[parents.size()]); } double[][] priorities = new double[parents.size()][quantity]; for (int i = 0; i < parents.size(); i++) { @@ -51,7 +53,7 @@ public class ScarceParentPriorityAllocator { } if (bestParent == -1) { - return taken; + return new Tuple<>(totalPriority, taken); } taken[bestParent] += bestQuantity; filled += bestQuantity;