in too deep

This commit is contained in:
Leijurv
2018-10-30 11:51:49 -07:00
parent e1bb8fd570
commit c887b27df9
24 changed files with 393 additions and 25 deletions

View File

@@ -17,6 +17,5 @@
package tenor;
public class TaskSerial extends TaskNode {
public class ActuallyCraftTask {
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -18,5 +18,5 @@
package tenor;
public interface ClaimProvider {
boolean provided(TaskRelationship parent, int quantity);
int quantityCompletedForParent(IQuantizedChildTaskRelationship relationship);
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -21,4 +21,6 @@ public interface IQuantizedParentTaskRelationship extends ITaskRelationshipBase
default QuantizedTaskNode parent() {
return (QuantizedTaskNode) parentTask();
}
QuantityRelationship cost();
}

View File

@@ -21,4 +21,6 @@ public interface ISingularChildTaskRelationship extends ITaskRelationshipBase {
default SingularTask child() {
return (SingularTask) childTask();
}
double allocatedPriority();
}

View File

@@ -21,4 +21,6 @@ public interface ISingularParentTaskRelationship extends ITaskRelationshipBase {
default SingularTask parent() {
return (SingularTask) parentTask();
}
double cost();
}

View 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();
}

View File

@@ -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;

View 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();
}
}
}

View File

@@ -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();
}

View File

@@ -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);
}

View File

@@ -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();
}
}

View File

@@ -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");
}
}

View 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;
}
}
}

View File

@@ -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();
}

View 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 {
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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;