Recipe::sum working
parent
fd29e51a7c
commit
0d95b9b25a
|
|
@ -61,6 +61,15 @@ public class Test {
|
|||
|
||||
plot(Database.Screw, "screws", 100, de);
|
||||
plot(Database.ReinforcedIronPlate, "ReinforcedIronPlate", 100, de);
|
||||
Graph<Item, DefaultWeightedEdge> reif = Database.ReinforcedIronPlate.getRecipe().buildGraph(Database.ReinforcedIronPlate);
|
||||
de.exportGraph(reif, new File("ReinforcedIronPlate_raw.dot"));
|
||||
System.out.println("\nSUM_reif");
|
||||
Database.ReinforcedIronPlate.getRecipe().sum(Database.ReinforcedIronPlate, 100);
|
||||
System.out.println("\nSUM_screw");
|
||||
Database.Screw.getRecipe().sum(Database.Screw, 100);
|
||||
System.out.println("\nSUM_ACU");
|
||||
Database.AdaptiveControlUnit.getRecipe().sum(Database.AdaptiveControlUnit, 100);
|
||||
|
||||
}
|
||||
|
||||
private static void plot(Item target, String name, int amount, DOTExporter<Item, DefaultWeightedEdge> de) {
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ public abstract class Item {
|
|||
public String toString() {
|
||||
return "Item{" +
|
||||
"name='" + name + '\'' +
|
||||
", recipes=" + recipes +
|
||||
", #recipes=" + recipes.size() +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
|
@ -115,4 +115,7 @@ public abstract class Item {
|
|||
return map;
|
||||
}
|
||||
|
||||
public Graph<Item, DefaultWeightedEdge> hierarchy(){
|
||||
return getRecipe().buildGraph(this);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,15 +8,17 @@ import org.jgrapht.graph.DefaultDirectedWeightedGraph;
|
|||
import org.jgrapht.graph.DefaultWeightedEdge;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Recipe {
|
||||
private Map<Item, Integer> inputs;
|
||||
private Map<Item, Integer> outputs;
|
||||
private final Map<Item, Integer> inputs;
|
||||
private final Map<Item, Integer> outputs;
|
||||
private boolean isHandCraftable = true;
|
||||
private String name;
|
||||
private int duration;
|
||||
private final int duration;
|
||||
|
||||
public Recipe(int duration) {
|
||||
inputs = new HashMap<>();
|
||||
|
|
@ -92,9 +94,9 @@ public class Recipe {
|
|||
public Map<Item, Integer> getTotalRequirements() {
|
||||
return new TotalAccumulator(inputs).accumulate();
|
||||
}
|
||||
|
||||
public float getProductionRate(Item item) {
|
||||
Integer integer = outputs.get(item);
|
||||
float production = integer;
|
||||
float production = outputs.get(item);
|
||||
return production / duration;
|
||||
}
|
||||
|
||||
|
|
@ -161,7 +163,7 @@ public class Recipe {
|
|||
inputs.forEach((item, integer) -> {
|
||||
graph.addVertex(item);
|
||||
graph.addEdge(item, target);
|
||||
float rate = (amountPerSecond / outputs.get(target))/integer;
|
||||
float rate = (amountPerSecond / outputs.get(target)) / integer;
|
||||
graph.setEdgeWeight(item, target, rate);
|
||||
|
||||
Recipe recipe = item.getRecipe();
|
||||
|
|
@ -175,4 +177,41 @@ public class Recipe {
|
|||
return graph;
|
||||
}
|
||||
|
||||
public Map<Item, Double> sum(Item target, int prodPerMinute) {
|
||||
Graph<Item, DefaultWeightedEdge> buildGraph = buildGraph(target);
|
||||
Map<Item, Double> map = new HashMap<>();
|
||||
Queue<Item> queue = new LinkedList<>();
|
||||
queue.add(target);
|
||||
map.put(target, (double) prodPerMinute);
|
||||
while (!queue.isEmpty()) {
|
||||
Item item = queue.remove();
|
||||
// next items
|
||||
buildGraph.incomingEdgesOf(item)
|
||||
.stream()
|
||||
.map(buildGraph::getEdgeSource)
|
||||
.forEach(queue::add);
|
||||
// *this* item
|
||||
double sum = 0;
|
||||
for (DefaultWeightedEdge edge : buildGraph.outgoingEdgesOf(item)) {
|
||||
Item product = buildGraph.getEdgeTarget(edge);
|
||||
Double productWantedPerMinute = map.get(product);
|
||||
if (productWantedPerMinute == null) {
|
||||
sum = 0;
|
||||
break;
|
||||
}
|
||||
double amountNeeded = buildGraph.getEdgeWeight(edge);
|
||||
int productPerProcess = product.getRecipe().outputs.get(product);
|
||||
double requiredProcesRuns = productWantedPerMinute / productPerProcess;
|
||||
double requiredInput = amountNeeded * requiredProcesRuns;
|
||||
sum += requiredInput;
|
||||
}
|
||||
if (!map.containsKey(item) && sum > 0) {
|
||||
map.put(item, sum);
|
||||
}
|
||||
}
|
||||
map.forEach((item, aDouble) -> {
|
||||
System.out.println(item.getName() + ": " + aDouble);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,17 +23,18 @@ public class Utils {
|
|||
return raws;
|
||||
}
|
||||
|
||||
public static <E extends Number> Map<String, E> shorten(Map<Item, E> totals){
|
||||
public static <E extends Number> Map<String, E> shorten(Map<Item, E> totals) {
|
||||
Map<String, E> shortend = new HashMap<>();
|
||||
totals.forEach((item, e) -> shortend.put(item.getName(), e));
|
||||
return shortend;
|
||||
}
|
||||
|
||||
public static String dotID(Item item){
|
||||
String name = item.getName() +"_"+item.sum+"__"+(String.valueOf(item.getProductionRate()).replace(".","_"));
|
||||
name = name.replace(" ","").replace(".","");
|
||||
public static String dotID(Item item) {
|
||||
String name = item.getName() + "_" + item.sum + "__" + (String.valueOf(item.getProductionRate()).replace(".", "_"));
|
||||
name = name.replace(" ", "").replace(".", "");
|
||||
return name;
|
||||
}
|
||||
|
||||
public static void fixSums(Item target, Graph<Item, DefaultWeightedEdge> graph) {
|
||||
System.err.println(target);
|
||||
EdgeReversedGraph<Item, DefaultWeightedEdge> inverse = new EdgeReversedGraph<>(graph);
|
||||
|
|
@ -69,9 +70,10 @@ public class Utils {
|
|||
|
||||
}
|
||||
});
|
||||
while (iterator.hasNext()){
|
||||
while (iterator.hasNext()) {
|
||||
Item i = iterator.next();
|
||||
System.out.println(i.getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ItemTest {
|
||||
|
||||
|
|
@ -17,6 +17,15 @@ class ItemTest {
|
|||
assertEquals(25, production.get(Database.IronOre), "IronOre");
|
||||
}
|
||||
|
||||
@Test
|
||||
void productionScrews2() {
|
||||
Map<Item, Double> production = Database.Screw.getRecipe().sum(Database.Screw, 100);
|
||||
assertEquals(100, production.get(Database.Screw), "Screws (output)");
|
||||
assertEquals(25, production.get(Database.IronRod), "IronRod");
|
||||
assertEquals(25, production.get(Database.IronIngot), "IronIngot");
|
||||
assertEquals(25, production.get(Database.IronOre), "IronOre");
|
||||
}
|
||||
|
||||
@Test
|
||||
void productionReinforcedIronPlates() {
|
||||
Map<Item, Double> production = Database.ReinforcedIronPlate.production(100);
|
||||
|
|
|
|||
Loading…
Reference in New Issue