in too deep
This commit is contained in:
@@ -17,6 +17,5 @@
|
||||
|
||||
package tenor;
|
||||
|
||||
public class TaskSerial extends TaskNode {
|
||||
|
||||
public class ActuallyCraftTask {
|
||||
}
|
||||
@@ -17,6 +17,52 @@
|
||||
|
||||
package tenor;
|
||||
|
||||
public class AquireCraftingItems extends Task{
|
||||
import java.util.List;
|
||||
|
||||
public class AquireCraftingItems extends QuantizedTaskNode implements ClaimProvider {
|
||||
CraftingTask output;
|
||||
|
||||
public AquireCraftingItems() {
|
||||
super(DependencyType.PARALLEL_ALL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity) {
|
||||
AquireItemTask resource = (AquireItemTask) child.childTask(); // all our dependents are aquire item tasks
|
||||
int amount = output.inputSizeFor(resource);
|
||||
|
||||
// they could provide us with quantity
|
||||
int actualQuantity = (int) Math.ceil(quantity * 1.0D / amount);
|
||||
// so we could do the crafting recipe this many times
|
||||
// how good would that be?
|
||||
return priority().value(actualQuantity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuantityRelationship priority() {
|
||||
return parents().get(0)::allocatedPriority; // gamer style
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuantityRelationship cost() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) {
|
||||
// our only parent is the crafting task
|
||||
int minCompletion = Integer.MAX_VALUE;
|
||||
for (IQuantizedChildTaskRelationship resource : (List<IQuantizedChildTaskRelationship>) (Object) children()) {
|
||||
int amountForUs = resource.quantityCompleted();
|
||||
|
||||
int amountPerCraft = output.inputSizeFor((AquireItemTask) resource.child());
|
||||
|
||||
int actualQuantity = (int) Math.ceil(amountForUs * 1.0D / amountPerCraft);
|
||||
|
||||
if (actualQuantity < minCompletion || minCompletion == Integer.MAX_VALUE) {
|
||||
minCompletion = actualQuantity; // any missing ingredient and we aren't really done
|
||||
}
|
||||
}
|
||||
return minCompletion;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,57 @@
|
||||
package tenor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AquireItemTask extends Task implements ClaimProvider {
|
||||
HashMap<TaskRelationship, Integer> allocation;
|
||||
public class AquireItemTask extends QuantizedTaskNode implements ClaimProvider, QuantizedDependentCostCalculator {
|
||||
|
||||
HashMap<IQuantizedChildTaskRelationship, Integer> allocation; // allocation of what tasks have claim over what items in our inventory i guess
|
||||
|
||||
public AquireItemTask() {
|
||||
super(DependencyType.ANY_ONE_OF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean provided(TaskRelationship parent, int quantity) {
|
||||
return allocation.get(parent) >= quantity;
|
||||
public int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship) {
|
||||
return allocation.get(relationship);
|
||||
}
|
||||
|
||||
public void reallocate() {
|
||||
allocation.clear();
|
||||
int amountToAllocate = getCurrentQuantityInInventory();
|
||||
int[] newAmounts = ScarceParentPriorityAllocator.priorityAllocation(amountToAllocate, parents());
|
||||
for (int i = 0; i < parents().size(); i++) {
|
||||
allocation.put(parents().get(i), newAmounts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public int getCurrentQuantityInInventory() {
|
||||
throw new UnsupportedOperationException("oppa");
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuantityRelationship priority() {
|
||||
return x -> {
|
||||
double sum = 0;
|
||||
for (Map.Entry<IQuantizedChildTaskRelationship, Integer> entry : allocation.entrySet()) {
|
||||
|
||||
IQuantizedChildTaskRelationship parentRelationship = entry.getKey();
|
||||
int quantityAssigned = entry.getValue();
|
||||
|
||||
sum += parentRelationship.allocatedPriority(quantityAssigned);
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuantityRelationship cost() {
|
||||
return QuantizedDependentCostCalculator.super.cost(); // oppa
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@
|
||||
package tenor;
|
||||
|
||||
public interface ClaimProvider {
|
||||
boolean provided(TaskRelationship parent, int quantity);
|
||||
int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,50 @@
|
||||
|
||||
package tenor;
|
||||
|
||||
public class CraftingTask {
|
||||
int outputQuantity;
|
||||
import net.minecraft.util.Tuple;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CraftingTask extends QuantizedTaskNode {
|
||||
int outputQuantity;
|
||||
List<Tuple<AquireItemTask, Integer>> recipe;
|
||||
AquireCraftingItems inputs;
|
||||
|
||||
|
||||
public CraftingTask() {
|
||||
super(DependencyType.SERIAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuantityRelationship cost() {
|
||||
return x -> {
|
||||
int actualQuantity = (int) Math.ceil(x * 1.0D / outputQuantity);
|
||||
return inputs.cost().value(actualQuantity);
|
||||
};
|
||||
}
|
||||
|
||||
public QuantityRelationship priority() {
|
||||
if (parents().size() != 1) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
// TODO this is a short circuit
|
||||
return ((QuantizedTask) (parents().get(0).parentTask())).priority();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity) {
|
||||
// how much priority would we give this child if they could provide us this quantity?
|
||||
int amountWeWouldProvide = quantity * outputQuantity;
|
||||
double desirability = priority().value(amountWeWouldProvide);
|
||||
return desirability;
|
||||
}
|
||||
|
||||
public int inputSizeFor(AquireItemTask task) {
|
||||
for (Tuple<AquireItemTask, Integer> tup : recipe) {
|
||||
if (tup.getFirst().equals(task)) {
|
||||
return tup.getSecond();
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,12 @@ public interface IQuantizedChildTaskRelationship extends ITaskRelationshipBase {
|
||||
}
|
||||
|
||||
double allocatedPriority(int quantity);
|
||||
|
||||
default boolean childProvidesClaimTo(int quantity) {
|
||||
return quantityCompleted() >= quantity;
|
||||
}
|
||||
|
||||
default int quantityCompleted() {
|
||||
return ((ClaimProvider) child()).quantityCompletedForParent(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,6 @@ public interface IQuantizedParentTaskRelationship extends ITaskRelationshipBase
|
||||
default QuantizedTaskNode parent() {
|
||||
return (QuantizedTaskNode) parentTask();
|
||||
}
|
||||
|
||||
QuantityRelationship cost();
|
||||
}
|
||||
|
||||
@@ -21,4 +21,6 @@ public interface ISingularChildTaskRelationship extends ITaskRelationshipBase {
|
||||
default SingularTask child() {
|
||||
return (SingularTask) childTask();
|
||||
}
|
||||
|
||||
double allocatedPriority();
|
||||
}
|
||||
|
||||
@@ -21,4 +21,6 @@ public interface ISingularParentTaskRelationship extends ITaskRelationshipBase {
|
||||
default SingularTask parent() {
|
||||
return (SingularTask) parentTask();
|
||||
}
|
||||
|
||||
double cost();
|
||||
}
|
||||
|
||||
26
src/main/java/tenor/ITaskNodeBase.java
Normal file
26
src/main/java/tenor/ITaskNodeBase.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package tenor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ITaskNodeBase extends ITask {
|
||||
List<TaskRelationship> childTasks();
|
||||
|
||||
DependencyType type();
|
||||
}
|
||||
@@ -18,6 +18,11 @@
|
||||
package tenor;
|
||||
|
||||
public class MineTask extends TaskLeaf implements QuantizedTask {
|
||||
@Override
|
||||
public QuantityRelationship priority() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuantityRelationship cost() {
|
||||
return null;
|
||||
|
||||
48
src/main/java/tenor/QuantizedDependentCostCalculator.java
Normal file
48
src/main/java/tenor/QuantizedDependentCostCalculator.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package tenor;
|
||||
|
||||
public interface QuantizedDependentCostCalculator extends ITaskNodeBase {
|
||||
default QuantityRelationship cost() {
|
||||
switch (type()) {
|
||||
case SERIAL:
|
||||
case PARALLEL_ALL:
|
||||
return q -> {
|
||||
double sum = 0;
|
||||
for (TaskRelationship relationship : childTasks()) {
|
||||
sum += ((IQuantizedParentTaskRelationship) relationship).cost().value(q);
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
case ANY_ONE_OF: // TODO this could be smarter about allocating
|
||||
return q -> {
|
||||
double min = -1;
|
||||
for (TaskRelationship relationship : childTasks()) {
|
||||
double cost = ((IQuantizedParentTaskRelationship) relationship).cost().value(q);
|
||||
if (min == -1 || cost < min) {
|
||||
min = cost;
|
||||
}
|
||||
}
|
||||
return min;
|
||||
};
|
||||
default:
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public interface QuantizedTask extends ITask {
|
||||
return (List<IQuantizedChildTaskRelationship>) (Object) parentTasks();
|
||||
}
|
||||
|
||||
default QuantityRelationship priority() {
|
||||
/*default QuantityRelationship priority() {
|
||||
return q -> {
|
||||
double sum = 0;
|
||||
for (IQuantizedChildTaskRelationship parent : parents()) {
|
||||
@@ -33,7 +33,8 @@ public interface QuantizedTask extends ITask {
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
}
|
||||
}*/
|
||||
QuantityRelationship priority();
|
||||
|
||||
QuantityRelationship cost();
|
||||
}
|
||||
|
||||
@@ -17,6 +17,16 @@
|
||||
|
||||
package tenor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class QuantizedTaskNode extends TaskNode implements QuantizedTask {
|
||||
public abstract double allocatedPriority(int quantity, TaskRelationship child);
|
||||
public QuantizedTaskNode(DependencyType type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
public List<IQuantizedParentTaskRelationship> children() {
|
||||
return (List<IQuantizedParentTaskRelationship>) (Object) childTasks();
|
||||
}
|
||||
|
||||
public abstract double priorityAllocatedTo(IQuantizedParentTaskRelationship child, int quantity);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,11 @@ package tenor;
|
||||
public class QuantizedToQuantizedTaskRelationship extends TaskRelationship implements IQuantizedChildTaskRelationship, IQuantizedParentTaskRelationship {
|
||||
@Override
|
||||
public double allocatedPriority(int quantity) {
|
||||
return parent().allocatedPriority(quantity, this);
|
||||
return parent().priorityAllocatedTo(this, quantity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QuantityRelationship cost() {
|
||||
return child().cost();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,4 +18,14 @@
|
||||
package tenor;
|
||||
|
||||
public class QuantizedToSingularTaskRelationship extends TaskRelationship implements ISingularChildTaskRelationship, IQuantizedParentTaskRelationship {
|
||||
|
||||
@Override
|
||||
public QuantityRelationship cost() {
|
||||
return x -> child().cost();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double allocatedPriority() {
|
||||
throw new UnsupportedOperationException("Cannot allocate priority from quantized parent to singular child");
|
||||
}
|
||||
}
|
||||
|
||||
68
src/main/java/tenor/ScarceParentPriorityAllocator.java
Normal file
68
src/main/java/tenor/ScarceParentPriorityAllocator.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package tenor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ScarceParentPriorityAllocator {
|
||||
public static int[] priorityAllocation(int quantity, List<IQuantizedChildTaskRelationship> parents) {
|
||||
if (quantity == 0) {
|
||||
return new int[parents.size()];
|
||||
}
|
||||
double[][] priorities = new double[parents.size()][quantity];
|
||||
for (int i = 0; i < parents.size(); i++) {
|
||||
for (int j = 1; j < quantity; j++) {
|
||||
priorities[i][j] = parents.get(i).allocatedPriority(j);
|
||||
}
|
||||
}
|
||||
|
||||
int filled = 0;
|
||||
double totalPriority = 0;
|
||||
int[] taken = new int[parents.size()];
|
||||
while (true) {
|
||||
|
||||
double bestRatio = Double.MIN_VALUE;
|
||||
int bestParent = -1;
|
||||
int bestQuantity = -1;
|
||||
for (int i = 0; i < priorities.length; i++) {
|
||||
for (int j = 1; j < quantity - filled; j++) {
|
||||
double ratio = priorities[i][j] / j;
|
||||
if (ratio > bestRatio || bestRatio == Double.MIN_VALUE) {
|
||||
bestRatio = ratio;
|
||||
bestParent = i;
|
||||
bestQuantity = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestParent == -1) {
|
||||
return taken;
|
||||
}
|
||||
taken[bestParent] += bestQuantity;
|
||||
filled += bestQuantity;
|
||||
double priorityTaken = priorities[bestParent][bestQuantity];
|
||||
totalPriority += priorityTaken;
|
||||
|
||||
double[] repl = new double[priorities[bestParent].length - bestQuantity];
|
||||
for (int i = 0; i < repl.length; i++) {
|
||||
repl[i] = priorities[bestParent][i + bestQuantity] - priorityTaken;
|
||||
}
|
||||
priorities[bestParent] = repl;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,14 @@
|
||||
|
||||
package tenor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SingularTask extends ITask {
|
||||
double priority();
|
||||
default List<ISingularChildTaskRelationship> parents() {
|
||||
return (List<ISingularChildTaskRelationship>) (Object) parentTasks();
|
||||
}
|
||||
|
||||
double priorityAllocatedToChild(ISingularParentTaskRelationship relationship);
|
||||
|
||||
double cost();
|
||||
}
|
||||
|
||||
21
src/main/java/tenor/SingularTaskNode.java
Normal file
21
src/main/java/tenor/SingularTaskNode.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package tenor;
|
||||
|
||||
public class SingularTaskNode {
|
||||
}
|
||||
@@ -22,6 +22,11 @@ public class SingularToQuantizedTaskRelationship extends TaskRelationship implem
|
||||
|
||||
@Override
|
||||
public double allocatedPriority(int quantity) {
|
||||
return quantity >= quantityRequired ? parent().priority() : 0;
|
||||
return quantity >= quantityRequired ? parent().priorityAllocatedToChild(this) : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double cost() {
|
||||
return child().cost().value(quantityRequired);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,4 +18,13 @@
|
||||
package tenor;
|
||||
|
||||
public class SingularToSingularTaskRelationship extends TaskRelationship implements ISingularChildTaskRelationship, ISingularParentTaskRelationship {
|
||||
@Override
|
||||
public double allocatedPriority() {
|
||||
return parent().priorityAllocatedToChild(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double cost() {
|
||||
return child().cost();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,10 @@
|
||||
|
||||
package tenor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Task implements ITask {
|
||||
ArrayList<TaskRelationship> parentRelationships;
|
||||
List<TaskRelationship> parentRelationships;
|
||||
|
||||
public List<TaskRelationship> parentTasks() {
|
||||
return parentRelationships;
|
||||
|
||||
@@ -17,9 +17,21 @@
|
||||
|
||||
package tenor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class TaskNode extends Task {
|
||||
ArrayList<TaskRelationship> childRelationships;
|
||||
public abstract class TaskNode extends Task implements ITaskNodeBase {
|
||||
List<TaskRelationship> childRelationships;
|
||||
DependencyType type;
|
||||
|
||||
public TaskNode(DependencyType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public List<TaskRelationship> childTasks() {
|
||||
return childRelationships;
|
||||
}
|
||||
|
||||
public DependencyType type() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,10 +22,6 @@ public class TaskRelationship implements ITaskRelationshipBase {
|
||||
Task child;
|
||||
DependencyType type;
|
||||
|
||||
public boolean childProvidesClaimTo(int quantity) {
|
||||
return ((ClaimProvider) child).provided(this, quantity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskNode parentTask() {
|
||||
return parent;
|
||||
|
||||
Reference in New Issue
Block a user