Writing down some algorithms for later analysis

This commit is contained in:
Pahimar 2016-05-16 15:28:23 -04:00
parent c0b7f76715
commit dfd674fce7
2 changed files with 66 additions and 65 deletions

View file

@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.ImmutableSortedMap;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.pahimar.ee3.api.exchange.EnergyValue; import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.handler.ConfigurationHandler;
import com.pahimar.ee3.util.EnergyValueHelper; import com.pahimar.ee3.util.EnergyValueHelper;
import com.pahimar.ee3.util.LogHelper; import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.util.SerializationHelper; import com.pahimar.ee3.util.SerializationHelper;
@ -229,10 +230,8 @@ public class NewEnergyValueRegistry {
firstPass = false; firstPass = false;
} }
long passDuration = System.nanoTime() - passStartTime; long passDuration = System.nanoTime() - passStartTime;
// TODO Tie this extra logging into a debug config option if (ConfigurationHandler.Settings.energyValueDebugLoggingEnabled) {
boolean debug = false;
if (debug) {
LogHelper.info(ENERGY_VALUE_MARKER, "Pass {}: Calculated {} values for objects in {} ns", passNumber, passComputed, passDuration); LogHelper.info(ENERGY_VALUE_MARKER, "Pass {}: Calculated {} values for objects in {} ns", passNumber, passComputed, passDuration);
} }
} }

View file

@ -175,107 +175,109 @@ public class EnergyValueHelper {
return null; return null;
} }
public static EnergyValue computeEnergyValueFromRecipe(Map<WrappedStack, EnergyValue> stackValueMappings, WrappedStack outputStack, List<WrappedStack> inputStacks) // FIXME PRIORITY NUMBER 1
{ public static EnergyValue computeEnergyValueFromRecipe(Map<WrappedStack, EnergyValue> valueMap, WrappedStack wrappedOutput, List<WrappedStack> wrappedInputs) {
float computedValue = 0f; float computedValue = 0f;
for (WrappedStack wrappedStack : inputStacks) // TODO We should do some verification that every input has an energy value - if not there is no point doing the calculation
{
EnergyValue wrappedStackValue; /**
* Basic algorithm:
* Set sumOfInputValues to 0
* For every input, add (input's Unit Value) * (input's stackSize) to sumOfInputs
* return new EnergyValue(sumOfInputs / output's stackSize)
*
* Caveats are for ItemStacks that act as containers. You only want to catch the difference in values between
* pre-crafting and post-crafting
*
* e.g., A recipe that uses up the water in a bucket (empty bucket left) vs uses up the entire water bucket (nothing left)
*/
for (WrappedStack wrappedInput : wrappedInputs) {
EnergyValue wrappedInputValue;
int stackSize = -1; int stackSize = -1;
if (wrappedStack.getWrappedObject() instanceof ItemStack)
{ if (wrappedInput.getWrappedObject() instanceof ItemStack) {
ItemStack itemStack = (ItemStack) wrappedStack.getWrappedObject();
ItemStack itemStack = (ItemStack) wrappedInput.getWrappedObject();
// Check if we are dealing with a potential fluid // Check if we are dealing with a potential fluid
if (FluidContainerRegistry.getFluidForFilledItem(itemStack) != null) if (FluidContainerRegistry.getFluidForFilledItem(itemStack) != null) {
{
if (itemStack.getItem().getContainerItem(itemStack) != null) if (itemStack.getItem().getContainerItem(itemStack) != null) {
{ stackSize = FluidContainerRegistry.getFluidForFilledItem(itemStack).amount * wrappedInput.getStackSize();
stackSize = FluidContainerRegistry.getFluidForFilledItem(itemStack).amount * wrappedStack.getStackSize(); wrappedInputValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, FluidContainerRegistry.getFluidForFilledItem(itemStack));
wrappedStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, FluidContainerRegistry.getFluidForFilledItem(itemStack));
} }
else else {
{ wrappedInputValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, wrappedInput);
wrappedStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, wrappedStack);
} }
} }
else if (itemStack.getItem().getContainerItem(itemStack) != null) else if (itemStack.getItem().getContainerItem(itemStack) != null) {
{
ItemStack containerItemStack = itemStack.getItem().getContainerItem(itemStack); ItemStack containerItemStack = itemStack.getItem().getContainerItem(itemStack);
if (EnergyValueRegistry.getInstance().hasEnergyValue(itemStack) && EnergyValueRegistry.getInstance().hasEnergyValue(containerItemStack)) if (EnergyValueRegistry.getInstance().hasEnergyValue(itemStack) && EnergyValueRegistry.getInstance().hasEnergyValue(containerItemStack)) {
{ float itemStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, itemStack).getValue();
float itemStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, itemStack).getValue(); float containerStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, containerItemStack).getValue();
float containerStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, containerItemStack).getValue(); wrappedInputValue = new EnergyValue(itemStackValue - containerStackValue);
wrappedStackValue = new EnergyValue(itemStackValue - containerStackValue);
} }
else else {
{ wrappedInputValue = new EnergyValue(0);
wrappedStackValue = new EnergyValue(0);
} }
} }
else if (!itemStack.getItem().doesContainerItemLeaveCraftingGrid(itemStack)) else if (!itemStack.getItem().doesContainerItemLeaveCraftingGrid(itemStack)) {
{ wrappedInputValue = new EnergyValue(0);
wrappedStackValue = new EnergyValue(0);
} }
else if (OreDictionary.getOreIDs(itemStack).length > 0) else if (OreDictionary.getOreIDs(itemStack).length > 0) {
{ wrappedInputValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, wrappedInput, true);
wrappedStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, wrappedStack, true);
} }
else else {
{ wrappedInputValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, wrappedInput);
wrappedStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, wrappedStack);
} }
} }
else if (wrappedStack.getWrappedObject() instanceof OreStack) else if (wrappedInput.getWrappedObject() instanceof OreStack) {
{
OreStack oreStack = (OreStack) wrappedStack.getWrappedObject(); OreStack oreStack = (OreStack) wrappedInput.getWrappedObject();
wrappedStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, wrappedStack); wrappedInputValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, wrappedInput);
for (ItemStack itemStack : OreDictionary.getOres(oreStack.oreName)) for (ItemStack itemStack : OreDictionary.getOres(oreStack.oreName))
{ {
if (!itemStack.getItem().doesContainerItemLeaveCraftingGrid(itemStack)) if (!itemStack.getItem().doesContainerItemLeaveCraftingGrid(itemStack))
{ {
wrappedStackValue = new EnergyValue(0); wrappedInputValue = new EnergyValue(0);
} }
} }
} }
else else {
{ wrappedInputValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(valueMap, wrappedInput);
wrappedStackValue = EnergyValueRegistry.getInstance().getEnergyValueFromMap(stackValueMappings, wrappedStack);
} }
if (wrappedStackValue != null) if (wrappedInputValue != null) {
{
if (stackSize == -1) if (stackSize == -1) {
{ stackSize = wrappedInput.getStackSize();
stackSize = wrappedStack.getStackSize();
} }
computedValue += wrappedStackValue.getValue() * stackSize; computedValue += wrappedInputValue.getValue() * stackSize;
} }
else else {
{
return null; return null;
} }
} }
return factor(new EnergyValue(computedValue), outputStack.getStackSize()); return factor(new EnergyValue(computedValue), wrappedOutput.getStackSize());
} }
public static EnergyValue factor(EnergyValue energyValue, int factor) public static EnergyValue factor(EnergyValue energyValue, int factor) {
{
return factor(energyValue, (float) factor); return factor(energyValue, (float) factor);
} }
public static EnergyValue factor(EnergyValue energyValue, float factor) public static EnergyValue factor(EnergyValue energyValue, float factor) {
{
if ((Float.compare(factor, 0f) != 0) && (energyValue != null)) if ((Float.compare(factor, 0f) != 0) && (energyValue != null)) {
{
return new EnergyValue(new BigDecimal(energyValue.getValue() * 1f / factor).setScale(3, BigDecimal.ROUND_HALF_EVEN).floatValue()); return new EnergyValue(new BigDecimal(energyValue.getValue() * 1f / factor).setScale(3, BigDecimal.ROUND_HALF_EVEN).floatValue());
} }
else else {
{
return null; return null;
} }
} }