Caching results

This commit is contained in:
Cavallium 2018-10-12 23:20:12 +02:00
parent 494f8e854f
commit b7407886dd
7 changed files with 51 additions and 70 deletions

View File

@ -9,6 +9,8 @@ import it.cavallium.warppi.util.Utils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public abstract class FunctionDynamic implements Function { public abstract class FunctionDynamic implements Function {
private boolean simplified;
public FunctionDynamic(final MathContext root) { public FunctionDynamic(final MathContext root) {
this.root = root; this.root = root;
functions = new Function[] {}; functions = new Function[] {};

View File

@ -6,6 +6,8 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public abstract class FunctionSingle implements Function { public abstract class FunctionSingle implements Function {
private boolean simplified;
/** /**
* Create a new instance of FunctionSingle. The Math Context will be the * Create a new instance of FunctionSingle. The Math Context will be the
* same of <strong>value</strong>'s. * same of <strong>value</strong>'s.

View File

@ -1,59 +0,0 @@
package it.cavallium.warppi.math.functions;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class EmptyNumber implements Function {
public EmptyNumber(final MathContext root) {
this.root = root;
}
private final MathContext root;
@Override
public ObjectArrayList<Function> simplify(final Rule rule) throws Error, InterruptedException {
return rule.execute(this);
}
@Override
public MathContext getMathContext() {
return root;
}
@Override
public boolean equals(final Object o) {
return o instanceof EmptyNumber;
}
@Override
public Function clone() {
return new EmptyNumber(root);
}
@Override
public Function clone(MathContext c) {
return new EmptyNumber(c);
}
@Override
public Function setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}
@Override
public Function getParameter(final int index) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();
}
@Override
public ObjectArrayList<Block> toBlock(final MathContext context) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -13,6 +13,7 @@ public class Variable implements Function {
protected char var; protected char var;
protected final MathContext root; protected final MathContext root;
protected V_TYPE type = V_TYPE.CONSTANT; protected V_TYPE type = V_TYPE.CONSTANT;
private boolean simplified;
public Variable(final MathContext root, final char val, final V_TYPE type) { public Variable(final MathContext root, final char val, final V_TYPE type) {
this.root = root; this.root = root;

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.solver; package it.cavallium.warppi.math.solver;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import it.cavallium.warppi.Engine; import it.cavallium.warppi.Engine;
@ -9,6 +10,9 @@ import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.rules.Rule; import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.math.rules.RuleType; import it.cavallium.warppi.math.rules.RuleType;
import it.cavallium.warppi.util.Error; import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class MathSolver { public class MathSolver {
@ -17,6 +21,7 @@ public class MathSolver {
private final AtomicInteger stepState = new AtomicInteger(0); private final AtomicInteger stepState = new AtomicInteger(0);
private int stepStateRepetitions = 0; private int stepStateRepetitions = 0;
private int consecutiveNullSteps = 0; private int consecutiveNullSteps = 0;
private final Object2ObjectOpenHashMap<Function, ObjectArrayList<Rule>> simplificationCache;
private enum StepState { private enum StepState {
_1_CALCULATION, _2_EXPANSION, _3_CALCULATION, _4_REDUCTION _1_CALCULATION, _2_EXPANSION, _3_CALCULATION, _4_REDUCTION
@ -28,6 +33,7 @@ public class MathSolver {
public MathSolver(final Function initialFunction) { public MathSolver(final Function initialFunction) {
this.initialFunction = initialFunction; this.initialFunction = initialFunction;
this.simplificationCache = new Object2ObjectOpenHashMap<Function, ObjectArrayList<Rule>>();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -214,6 +220,7 @@ public class MathSolver {
for (final Function fnc : fncs) { for (final Function fnc : fncs) {
boolean didSomething = false; boolean didSomething = false;
for (final Rule rule : rules) { for (final Rule rule : rules) {
if (isSimplified(fnc, rule) == false) {
final List<Function> ruleResults = fnc.simplify(rule); final List<Function> ruleResults = fnc.simplify(rule);
if (ruleResults != null && !ruleResults.isEmpty()) { if (ruleResults != null && !ruleResults.isEmpty()) {
if (results == null) { if (results == null) {
@ -221,10 +228,12 @@ public class MathSolver {
} }
results.addAll(ruleResults); results.addAll(ruleResults);
appliedRules.add(rule); appliedRules.add(rule);
setSimplified(fnc, rule);
didSomething = true; didSomething = true;
break; break;
} }
} }
}
if (!didSomething && fncs.size() > 1) { if (!didSomething && fncs.size() > 1) {
if (results == null) { if (results == null) {
results = new ObjectArrayList<>(); results = new ObjectArrayList<>();
@ -248,4 +257,30 @@ public class MathSolver {
} }
return results; return results;
} }
private boolean isSimplified(Function fnc, Rule rule) {
if (simplificationCache.containsKey(fnc)) {
List<Rule> alreadySimplifiedRules = simplificationCache.get(fnc);
if (alreadySimplifiedRules.contains(rule)) {
return true;
} else {
return false;
}
} else {
simplificationCache.put(fnc, new ObjectArrayList<Rule>());
}
return false;
}
private void setSimplified(Function fnc, Rule rule) {
ObjectArrayList<Rule> oar;
if (simplificationCache.containsKey(fnc)) {
oar = new ObjectArrayList<>();
simplificationCache.put(fnc, oar);
} else {
oar = simplificationCache.get(fnc);
if (oar.contains(rule)) return;
}
oar.add(rule);
}
} }