Recipe::sum working
parent
fd29e51a7c
commit
0d95b9b25a
|
|
@ -61,6 +61,15 @@ public class Test {
|
||||||
|
|
||||||
plot(Database.Screw, "screws", 100, de);
|
plot(Database.Screw, "screws", 100, de);
|
||||||
plot(Database.ReinforcedIronPlate, "ReinforcedIronPlate", 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) {
|
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() {
|
public String toString() {
|
||||||
return "Item{" +
|
return "Item{" +
|
||||||
"name='" + name + '\'' +
|
"name='" + name + '\'' +
|
||||||
", recipes=" + recipes +
|
", #recipes=" + recipes.size() +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,4 +115,7 @@ public abstract class Item {
|
||||||
return map;
|
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 org.jgrapht.graph.DefaultWeightedEdge;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Queue;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Recipe {
|
public class Recipe {
|
||||||
private Map<Item, Integer> inputs;
|
private final Map<Item, Integer> inputs;
|
||||||
private Map<Item, Integer> outputs;
|
private final Map<Item, Integer> outputs;
|
||||||
private boolean isHandCraftable = true;
|
private boolean isHandCraftable = true;
|
||||||
private String name;
|
private String name;
|
||||||
private int duration;
|
private final int duration;
|
||||||
|
|
||||||
public Recipe(int duration) {
|
public Recipe(int duration) {
|
||||||
inputs = new HashMap<>();
|
inputs = new HashMap<>();
|
||||||
|
|
@ -92,9 +94,9 @@ public class Recipe {
|
||||||
public Map<Item, Integer> getTotalRequirements() {
|
public Map<Item, Integer> getTotalRequirements() {
|
||||||
return new TotalAccumulator(inputs).accumulate();
|
return new TotalAccumulator(inputs).accumulate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getProductionRate(Item item) {
|
public float getProductionRate(Item item) {
|
||||||
Integer integer = outputs.get(item);
|
float production = outputs.get(item);
|
||||||
float production = integer;
|
|
||||||
return production / duration;
|
return production / duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,4 +177,41 @@ public class Recipe {
|
||||||
return graph;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ public class Utils {
|
||||||
name = name.replace(" ", "").replace(".", "");
|
name = name.replace(" ", "").replace(".", "");
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fixSums(Item target, Graph<Item, DefaultWeightedEdge> graph) {
|
public static void fixSums(Item target, Graph<Item, DefaultWeightedEdge> graph) {
|
||||||
System.err.println(target);
|
System.err.println(target);
|
||||||
EdgeReversedGraph<Item, DefaultWeightedEdge> inverse = new EdgeReversedGraph<>(graph);
|
EdgeReversedGraph<Item, DefaultWeightedEdge> inverse = new EdgeReversedGraph<>(graph);
|
||||||
|
|
@ -74,4 +75,5 @@ public class Utils {
|
||||||
System.out.println(i.getName());
|
System.out.println(i.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
class ItemTest {
|
class ItemTest {
|
||||||
|
|
||||||
|
|
@ -17,6 +17,15 @@ class ItemTest {
|
||||||
assertEquals(25, production.get(Database.IronOre), "IronOre");
|
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
|
@Test
|
||||||
void productionReinforcedIronPlates() {
|
void productionReinforcedIronPlates() {
|
||||||
Map<Item, Double> production = Database.ReinforcedIronPlate.production(100);
|
Map<Item, Double> production = Database.ReinforcedIronPlate.production(100);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue