#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HEAL.Attic;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[Item("Constant Optimization Evaluator (new)", "Calculates Pearson R² of a symbolic regression solution and optimizes the constant used.")]
[StorableType("1D5361E9-EF73-47D2-9211-FDD39BBC1018")]
public class SymbolicRegressionNewConstantOptimizationEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
private const string ConstantOptimizationIterationsParameterName = "ConstantOptimizationIterations";
private const string ConstantOptimizationImprovementParameterName = "ConstantOptimizationImprovement";
private const string ConstantOptimizationProbabilityParameterName = "ConstantOptimizationProbability";
private const string ConstantOptimizationRowsPercentageParameterName = "ConstantOptimizationRowsPercentage";
private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree";
private const string UpdateVariableWeightsParameterName = "Update Variable Weights";
private const string FunctionEvaluationsResultParameterName = "Constants Optimization Function Evaluations";
private const string GradientEvaluationsResultParameterName = "Constants Optimization Gradient Evaluations";
private const string CountEvaluationsParameterName = "Count Function and Gradient Evaluations";
public IFixedValueParameter ConstantOptimizationIterationsParameter {
get { return (IFixedValueParameter)Parameters[ConstantOptimizationIterationsParameterName]; }
}
public IFixedValueParameter ConstantOptimizationImprovementParameter {
get { return (IFixedValueParameter)Parameters[ConstantOptimizationImprovementParameterName]; }
}
public IFixedValueParameter ConstantOptimizationProbabilityParameter {
get { return (IFixedValueParameter)Parameters[ConstantOptimizationProbabilityParameterName]; }
}
public IFixedValueParameter ConstantOptimizationRowsPercentageParameter {
get { return (IFixedValueParameter)Parameters[ConstantOptimizationRowsPercentageParameterName]; }
}
public IFixedValueParameter UpdateConstantsInTreeParameter {
get { return (IFixedValueParameter)Parameters[UpdateConstantsInTreeParameterName]; }
}
public IFixedValueParameter UpdateVariableWeightsParameter {
get { return (IFixedValueParameter)Parameters[UpdateVariableWeightsParameterName]; }
}
public IResultParameter FunctionEvaluationsResultParameter {
get { return (IResultParameter)Parameters[FunctionEvaluationsResultParameterName]; }
}
public IResultParameter GradientEvaluationsResultParameter {
get { return (IResultParameter)Parameters[GradientEvaluationsResultParameterName]; }
}
public IFixedValueParameter CountEvaluationsParameter {
get { return (IFixedValueParameter)Parameters[CountEvaluationsParameterName]; }
}
public IntValue ConstantOptimizationIterations {
get { return ConstantOptimizationIterationsParameter.Value; }
}
public DoubleValue ConstantOptimizationImprovement {
get { return ConstantOptimizationImprovementParameter.Value; }
}
public PercentValue ConstantOptimizationProbability {
get { return ConstantOptimizationProbabilityParameter.Value; }
}
public PercentValue ConstantOptimizationRowsPercentage {
get { return ConstantOptimizationRowsPercentageParameter.Value; }
}
public bool UpdateConstantsInTree {
get { return UpdateConstantsInTreeParameter.Value.Value; }
set { UpdateConstantsInTreeParameter.Value.Value = value; }
}
public bool UpdateVariableWeights {
get { return UpdateVariableWeightsParameter.Value.Value; }
set { UpdateVariableWeightsParameter.Value.Value = value; }
}
public bool CountEvaluations {
get { return CountEvaluationsParameter.Value.Value; }
set { CountEvaluationsParameter.Value.Value = value; }
}
public override bool Maximization {
get { return true; }
}
[StorableConstructor]
protected SymbolicRegressionNewConstantOptimizationEvaluator(StorableConstructorFlag _) : base(_) { }
protected SymbolicRegressionNewConstantOptimizationEvaluator(SymbolicRegressionNewConstantOptimizationEvaluator original, Cloner cloner)
: base(original, cloner) {
}
public SymbolicRegressionNewConstantOptimizationEvaluator()
: base() {
Parameters.Add(new FixedValueParameter(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
Parameters.Add(new FixedValueParameter(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0)) { Hidden = true });
Parameters.Add(new FixedValueParameter(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1)));
Parameters.Add(new FixedValueParameter(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1)));
Parameters.Add(new FixedValueParameter(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)) { Hidden = true });
Parameters.Add(new FixedValueParameter(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be optimized.", new BoolValue(true)) { Hidden = true });
Parameters.Add(new FixedValueParameter(CountEvaluationsParameterName, "Determines if function and gradient evaluation should be counted.", new BoolValue(false)));
Parameters.Add(new ResultParameter(FunctionEvaluationsResultParameterName, "The number of function evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
Parameters.Add(new ResultParameter(GradientEvaluationsResultParameterName, "The number of gradient evaluations performed by the constants optimization evaluator", "Results", new IntValue()));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicRegressionNewConstantOptimizationEvaluator(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() { }
private static readonly object locker = new object();
public override IOperation InstrumentedApply() {
var solution = SymbolicExpressionTreeParameter.ActualValue;
double quality;
if (RandomParameter.ActualValue.NextDouble() < ConstantOptimizationProbability.Value) {
IEnumerable constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value);
var counter = new EvaluationsCounter();
quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree, counter: counter);
if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
var evaluationRows = GenerateRowsToEvaluate();
quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
}
if (CountEvaluations) {
lock (locker) {
FunctionEvaluationsResultParameter.ActualValue.Value += counter.FunctionEvaluations;
GradientEvaluationsResultParameter.ActualValue.Value += counter.GradientEvaluations;
}
}
} else {
var evaluationRows = GenerateRowsToEvaluate();
quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
}
QualityParameter.ActualValue = new DoubleValue(quality);
return base.InstrumentedApply();
}
public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable rows) {
SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
EstimationLimitsParameter.ExecutionContext = context;
ApplyLinearScalingParameter.ExecutionContext = context;
FunctionEvaluationsResultParameter.ExecutionContext = context;
GradientEvaluationsResultParameter.ExecutionContext = context;
// Pearson R² evaluator is used on purpose instead of the const-opt evaluator,
// because Evaluate() is used to get the quality of evolved models on
// different partitions of the dataset (e.g., best validation model)
double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
EstimationLimitsParameter.ExecutionContext = null;
ApplyLinearScalingParameter.ExecutionContext = null;
FunctionEvaluationsResultParameter.ExecutionContext = null;
GradientEvaluationsResultParameter.ExecutionContext = null;
return r2;
}
public class EvaluationsCounter {
public int FunctionEvaluations = 0;
public int GradientEvaluations = 0;
}
private static void GetParameterNodes(ISymbolicExpressionTree tree, out List thetaNodes, out List thetaValues) {
thetaNodes = new List();
thetaValues = new List();
var nodes = tree.IterateNodesPrefix().ToArray();
for (int i = 0; i < nodes.Length; ++i) {
var node = nodes[i];
if (node is VariableTreeNode variableTreeNode) {
thetaValues.Add(variableTreeNode.Weight);
thetaNodes.Add(node);
} else if (node is ConstantTreeNode constantTreeNode) {
thetaNodes.Add(node);
thetaValues.Add(constantTreeNode.Value);
}
}
}
public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable rows, bool applyLinearScaling,
int maxIterations, bool updateVariableWeights = true,
double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
bool updateConstantsInTree = true, Action iterationCallback = null, EvaluationsCounter counter = null) {
if (!updateVariableWeights) throw new NotSupportedException();
// // numeric constants in the tree become variables for constant opt
// // variables in the tree become parameters (fixed values) for constant opt
// // for each parameter (variable in the original tree) we store the
// // variable name, variable value (for factor vars) and lag as a DataForVariable object.
// // A dictionary is used to find parameters
// double[] initialConstants;
// var parameters = new List();
//
// TreeToAutoDiffTermConverter.ParametricFunction func;
// TreeToAutoDiffTermConverter.ParametricFunctionGradient func_grad;
// if (!TreeToAutoDiffTermConverter.TryConvertToAutoDiff(tree, updateVariableWeights, applyLinearScaling, out parameters, out initialConstants, out func, out func_grad))
// throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
// if (parameters.Count == 0) return 0.0; // gkronber: constant expressions always have a R² of 0.0
// var parameterEntries = parameters.ToArray(); // order of entries must be the same for x
GetParameterNodes(tree, out List thetaNodes, out List thetaValues);
var initialConstants = thetaValues.ToArray();
//extract inital constants
double[] c;
if (applyLinearScaling) {
c = new double[initialConstants.Length + 2];
c[0] = 0.0;
c[1] = 1.0;
Array.Copy(initialConstants, 0, c, 2, initialConstants.Length);
} else {
c = (double[])initialConstants.Clone();
}
double originalQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
if (counter == null) counter = new EvaluationsCounter();
var rowEvaluationsCounter = new EvaluationsCounter();
alglib.minlmstate state;
alglib.minlmreport rep;
IDataset ds = problemData.Dataset;
double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray();
int n = y.Length;
int k = c.Length;
var trainRows = problemData.TrainingIndices.ToArray();
var parameterNodes = thetaNodes.ToArray();
alglib.ndimensional_fvec function_cx_1_func = CreateFunc(tree, new VectorEvaluator(), parameterNodes, ds, problemData.TargetVariable, trainRows);
alglib.ndimensional_jac function_cx_1_jac = CreateJac(tree, new VectorAutoDiffEvaluator(), parameterNodes, ds, problemData.TargetVariable, trainRows);
alglib.ndimensional_rep xrep = (p, f, obj) => iterationCallback(p, f, obj);
try {
alglib.minlmcreatevj(n, c, out state);
alglib.minlmsetcond(state, 0.0, maxIterations);
alglib.minlmsetxrep(state, iterationCallback != null);
// alglib.minlmsetgradientcheck(state, 0.001);
alglib.minlmoptimize(state, function_cx_1_func, function_cx_1_jac, xrep, rowEvaluationsCounter);
alglib.minlmresults(state, out c, out rep);
} catch (ArithmeticException) {
return originalQuality;
} catch (alglib.alglibexception) {
return originalQuality;
}
counter.FunctionEvaluations += rowEvaluationsCounter.FunctionEvaluations / n;
counter.GradientEvaluations += rowEvaluationsCounter.GradientEvaluations / n;
//retVal == -7 => constant optimization failed due to wrong gradient
if (rep.terminationtype != -7) {
if (applyLinearScaling) {
var tmp = new double[c.Length - 2];
Array.Copy(c, 2, tmp, 0, tmp.Length);
UpdateConstants(parameterNodes, tmp);
} else UpdateConstants(parameterNodes, c);
}
var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
if (!updateConstantsInTree) UpdateConstants(parameterNodes, initialConstants);
if (originalQuality - quality > 0.001 || double.IsNaN(quality)) {
UpdateConstants(parameterNodes, initialConstants);
return originalQuality;
}
return quality;
}
private static void UpdateConstants(ISymbolicExpressionTreeNode[] nodes, double[] constants) {
if (nodes.Length != constants.Length) throw new InvalidOperationException();
for(int i = 0;i {
UpdateConstants(parameterNodes, c);
var pred = eval.Evaluate(tree, ds, rows);
for (int i = 0; i < fi.Length; i++)
fi[i] = pred[i] - y[i];
var counter = (EvaluationsCounter)o;
counter.FunctionEvaluations++;
};
}
private static alglib.ndimensional_jac CreateJac(ISymbolicExpressionTree tree, VectorAutoDiffEvaluator eval, ISymbolicExpressionTreeNode[] parameterNodes, IDataset ds, string targetVar, int[] rows) {
var y = ds.GetDoubleValues(targetVar, rows).ToArray();
return (double[] c, double[] fi, double[,] jac, object o) => {
UpdateConstants(parameterNodes, c);
eval.Evaluate(tree, ds, rows, parameterNodes, fi, jac);
for (int i = 0; i < fi.Length; i++)
fi[i] -= y[i];
var counter = (EvaluationsCounter)o;
counter.GradientEvaluations++;
};
}
public static bool CanOptimizeConstants(ISymbolicExpressionTree tree) {
return TreeToAutoDiffTermConverter.IsCompatible(tree);
}
}
}