public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows, IntRange evaluationPartition, int horizon, bool applyLinearScaling) { var horizions = rows.Select(r => Math.Min(horizon, evaluationPartition.End - r)); IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows.Zip(horizions, Enumerable.Range).SelectMany(r => r)); IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows, horizions).SelectMany(x => x); OnlineCalculatorError errorState; double mse; if (applyLinearScaling && horizon == 1) { //perform normal evaluation and afterwards scale the solution and calculate the fitness value var mseCalculator = new OnlineMeanSquaredErrorCalculator(); CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows * horizon); errorState = mseCalculator.ErrorState; mse = mseCalculator.MeanSquaredError; } else if (applyLinearScaling) { //first create model to perform linear scaling and afterwards calculate fitness for the scaled model var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)solution.Clone(), interpreter, lowerEstimationLimit, upperEstimationLimit); model.Scale(problemData); var scaledSolution = model.SymbolicExpressionTree; estimatedValues = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset, rows, horizions).SelectMany(x => x); var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); } else { var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); } if (errorState != OnlineCalculatorError.None) return Double.NaN; else return mse; }
public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable <int> rows, int horizon) { var allPredictedContinuations = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, problemData.TargetVariables.ToArray(), rows, horizon).ToArray(); var meanCalculator = new OnlineMeanAndVarianceCalculator(); int i = 0; foreach (var targetVariable in problemData.TargetVariables) { var actualContinuations = from r in rows select problemData.Dataset.GetDoubleValues(targetVariable, Enumerable.Range(r, horizon)); var startValues = problemData.Dataset.GetDoubleValues(targetVariable, rows.Select(r => r - 1)); OnlineCalculatorError errorState; meanCalculator.Add(OnlineTheilsUStatisticCalculator.Calculate( startValues, allPredictedContinuations.Select(v => v.ElementAt(i)), actualContinuations, out errorState)); if (errorState != OnlineCalculatorError.None) { return(double.NaN); } i++; } return(meanCalculator.Mean); }
private void TestFullGrammarPerformance(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) { var twister = new MersenneTwister(31415); var dataset = Util.CreateRandomDataset(twister, Rows, Columns); var grammar = new FullFunctionalExpressionGrammar(); grammar.MaximumFunctionArguments = 0; grammar.MaximumFunctionDefinitions = 0; grammar.MinimumFunctionArguments = 0; grammar.MinimumFunctionDefinitions = 0; var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); foreach (ISymbolicExpressionTree tree in randomTrees) { Util.InitTree(tree, twister, new List<string>(dataset.VariableNames)); } double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3); //mkommend: commented due to performance issues on the builder //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec }
private void TestFullGrammarPerformance(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) { var twister = new MersenneTwister(31415); var dataset = Util.CreateRandomDataset(twister, Rows, Columns); var grammar = new FullFunctionalExpressionGrammar(); grammar.MaximumFunctionArguments = 0; grammar.MaximumFunctionDefinitions = 0; grammar.MinimumFunctionArguments = 0; grammar.MinimumFunctionDefinitions = 0; var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); foreach (ISymbolicExpressionTree tree in randomTrees) { Util.InitTree(tree, twister, new List <string>(dataset.VariableNames)); } double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3); //mkommend: commented due to performance issues on the builder //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec }
public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows, int horizon) { var allPredictedContinuations = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, problemData.TargetVariables.ToArray(), rows, horizon).ToArray(); var meanCalculator = new OnlineMeanAndVarianceCalculator(); int i = 0; foreach (var targetVariable in problemData.TargetVariables) { var actualContinuations = from r in rows select problemData.Dataset.GetDoubleValues(targetVariable, Enumerable.Range(r, horizon)); var startValues = problemData.Dataset.GetDoubleValues(targetVariable, rows.Select(r => r - 1)); OnlineCalculatorError errorState; meanCalculator.Add(OnlineTheilsUStatisticCalculator.Calculate( startValues, allPredictedContinuations.Select(v => v.ElementAt(i)), actualContinuations, out errorState)); if (errorState != OnlineCalculatorError.None) return double.NaN; i++; } return meanCalculator.Mean; }
public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable <int> rows, IntRange evaluationPartition, int horizon, bool applyLinearScaling) { var horizions = rows.Select(r => Math.Min(horizon, evaluationPartition.End - r)); IEnumerable <double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows.Zip(horizions, Enumerable.Range).SelectMany(r => r)); IEnumerable <double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows, horizions).SelectMany(x => x); OnlineCalculatorError errorState; double mse; if (applyLinearScaling && horizon == 1) //perform normal evaluation and afterwards scale the solution and calculate the fitness value { var mseCalculator = new OnlineMeanSquaredErrorCalculator(); CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows * horizon); errorState = mseCalculator.ErrorState; mse = mseCalculator.MeanSquaredError; } else if (applyLinearScaling) //first create model to perform linear scaling and afterwards calculate fitness for the scaled model { var model = new SymbolicTimeSeriesPrognosisModel(problemData.TargetVariable, (ISymbolicExpressionTree)solution.Clone(), interpreter, lowerEstimationLimit, upperEstimationLimit); model.Scale(problemData); var scaledSolution = model.SymbolicExpressionTree; estimatedValues = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset, rows, horizions).SelectMany(x => x); var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); } else { var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); } if (errorState != OnlineCalculatorError.None) { return(Double.NaN); } else { return(mse); } }
public static double CalculateEvaluatedNodesPerSec(ISymbolicExpressionTree[] trees, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) { interpreter.TargetVariable = dataset.VariableNames.First(); // warm up IEnumerable<int> rows = Enumerable.Range(0, dataset.Rows - horizon); long nNodes = 0; for (int i = 0; i < trees.Length; i++) { nNodes += trees[i].Length * (dataset.Rows - horizon) * horizon; interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows, horizon).Count(); // count needs to evaluate all rows } Stopwatch watch = new Stopwatch(); for (int rep = 0; rep < repetitions; rep++) { watch.Start(); for (int i = 0; i < trees.Length; i++) { interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows, horizon).Count(); // count needs to evaluate all rows } watch.Stop(); } Console.WriteLine("Random tree evaluation performance of " + interpreter.GetType() + ": " + watch.ElapsedMilliseconds + "ms " + Util.NodesPerSecond(nNodes * repetitions, watch) + " nodes/sec"); return Util.NodesPerSecond(nNodes * repetitions, watch); }
public static double CalculateEvaluatedNodesPerSec(ISymbolicExpressionTree[] trees, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) { interpreter.TargetVariable = dataset.VariableNames.First(); // warm up IEnumerable <int> rows = Enumerable.Range(0, dataset.Rows - horizon); long nNodes = 0; for (int i = 0; i < trees.Length; i++) { nNodes += trees[i].Length * (dataset.Rows - horizon) * horizon; interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows, horizon).Count(); // count needs to evaluate all rows } Stopwatch watch = new Stopwatch(); for (int rep = 0; rep < repetitions; rep++) { watch.Start(); for (int i = 0; i < trees.Length; i++) { interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows, horizon).Count(); // count needs to evaluate all rows } watch.Stop(); } Console.WriteLine("Random tree evaluation performance of " + interpreter.GetType() + ": " + watch.ElapsedMilliseconds + "ms " + Util.NodesPerSecond(nNodes * repetitions, watch) + " nodes/sec"); return(Util.NodesPerSecond(nNodes * repetitions, watch)); }
public SymbolicTimeSeriesPrognosisModel(string targetVariable, ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue) : base(targetVariable, tree, interpreter, lowerLimit, upperLimit) { }
public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue) : base(tree, interpreter, lowerLimit, upperLimit) { }