#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;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[Item("NLOpt Evaluator (with constraints)", "")]
[StorableType("5FADAE55-3516-4539-8A36-BC9B0D00880D")]
public class NLOptEvaluator : 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";
private const string AchievedQualityImprovementParameterName = "AchievedQualityImprovment";
private const string NumberOfConstraintViolationsBeforeOptimizationParameterName = "NumberOfConstraintViolationsBeforeOptimization";
private const string NumberOfConstraintViolationsAfterOptimizationParameterName = "NumberOfConstraintViolationsAfterOptimization";
private const string ConstraintsBeforeOptimizationParameterName = "ConstraintsBeforeOptimization";
private const string ViolationsAfterOptimizationParameterName = "ConstraintsAfterOptimization";
private const string OptimizationDurationParameterName = "OptimizationDuration";
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 IConstrainedValueParameter SolverParameter {
get { return (IConstrainedValueParameter)Parameters["Solver"]; }
}
public ILookupParameter AchievedQualityImprovementParameter {
get { return (ILookupParameter)Parameters[AchievedQualityImprovementParameterName]; }
}
public ILookupParameter NumberOfConstraintViolationsBeforeOptimizationParameter {
get { return (ILookupParameter)Parameters[NumberOfConstraintViolationsBeforeOptimizationParameterName]; }
}
public ILookupParameter NumberOfConstraintViolationsAfterOptimizationParameter {
get { return (ILookupParameter)Parameters[NumberOfConstraintViolationsAfterOptimizationParameterName]; }
}
public ILookupParameter ViolationsAfterOptimizationParameter {
get { return (ILookupParameter)Parameters[ViolationsAfterOptimizationParameterName]; }
}
public ILookupParameter ViolationsBeforeOptimizationParameter {
get { return (ILookupParameter)Parameters[ConstraintsBeforeOptimizationParameterName]; }
}
public ILookupParameter OptimizationDurationParameter {
get { return (ILookupParameter)Parameters[OptimizationDurationParameterName]; }
}
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 string Solver {
get { return SolverParameter.Value.Value; }
}
public override bool Maximization {
get { return false; }
}
[StorableConstructor]
protected NLOptEvaluator(StorableConstructorFlag _) : base(_) { }
protected NLOptEvaluator(NLOptEvaluator original, Cloner cloner)
: base(original, cloner) {
}
public NLOptEvaluator()
: 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)));
var validSolvers = new ItemSet(new[] { "MMA", "COBYLA", "CCSAQ", "ISRES" }.Select(s => new StringValue(s).AsReadOnly()));
Parameters.Add(new ConstrainedValueParameter("Solver", "The solver algorithm", validSolvers, validSolvers.First()));
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()));
Parameters.Add(new LookupParameter(AchievedQualityImprovementParameterName));
Parameters.Add(new LookupParameter(NumberOfConstraintViolationsBeforeOptimizationParameterName));
Parameters.Add(new LookupParameter(NumberOfConstraintViolationsAfterOptimizationParameterName));
Parameters.Add(new LookupParameter(ConstraintsBeforeOptimizationParameterName));
Parameters.Add(new LookupParameter(ViolationsAfterOptimizationParameterName));
Parameters.Add(new LookupParameter(OptimizationDurationParameterName));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new NLOptEvaluator(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey(AchievedQualityImprovementParameterName)) {
Parameters.Add(new LookupParameter(AchievedQualityImprovementParameterName));
Parameters.Add(new LookupParameter(NumberOfConstraintViolationsBeforeOptimizationParameterName));
Parameters.Add(new LookupParameter(NumberOfConstraintViolationsAfterOptimizationParameterName));
}
if(!Parameters.ContainsKey(ConstraintsBeforeOptimizationParameterName)) {
Parameters.Add(new LookupParameter(ConstraintsBeforeOptimizationParameterName));
Parameters.Add(new LookupParameter(ViolationsAfterOptimizationParameterName));
Parameters.Add(new LookupParameter(OptimizationDurationParameterName));
}
}
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();
if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
throw new NotSupportedException();
}
var sw = new Stopwatch();
sw.Start();
quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, Solver,
out double qDiff, out double[] constraintsBefore, out double[] constraintsAfter,
ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree, counter: counter);
AchievedQualityImprovementParameter.ActualValue = new DoubleValue(qDiff);
NumberOfConstraintViolationsBeforeOptimizationParameter.ActualValue = new DoubleValue(constraintsBefore.Count(cv => cv > 0));
NumberOfConstraintViolationsAfterOptimizationParameter.ActualValue = new DoubleValue(constraintsAfter.Count(cv => cv > 0));
ViolationsBeforeOptimizationParameter.ActualValue = new DoubleArray(constraintsBefore);
ViolationsAfterOptimizationParameter.ActualValue = new DoubleArray(constraintsAfter);
OptimizationDurationParameter.ActualValue = new DoubleValue(sw.Elapsed.TotalSeconds);
if (CountEvaluations) {
lock (locker) {
FunctionEvaluationsResultParameter.ActualValue.Value += counter.FunctionEvaluations;
GradientEvaluationsResultParameter.ActualValue.Value += counter.GradientEvaluations;
}
}
} else {
throw new NotSupportedException();
}
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;
// MSE 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 mse = SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, double.MinValue, double.MaxValue, problemData, rows, applyLinearScaling: false);
SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
EstimationLimitsParameter.ExecutionContext = null;
ApplyLinearScalingParameter.ExecutionContext = null;
FunctionEvaluationsResultParameter.ExecutionContext = null;
GradientEvaluationsResultParameter.ExecutionContext = null;
return mse;
}
public class EvaluationsCounter {
public int FunctionEvaluations = 0;
public int GradientEvaluations = 0;
}
public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable rows, bool applyLinearScaling,
string solver, out double qDiff, out double[] constraintsBefore, out double[] constraintsAfter,
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("not updating variable weights is not supported");
if (!updateConstantsInTree) throw new NotSupportedException("not updating tree parameters is not supported");
if (!applyLinearScaling) throw new NotSupportedException("application without linear scaling is not supported");
using (var state = new ConstrainedNLSInternal(solver, tree, maxIterations, problemData, 0, 0, 0)) {
constraintsBefore = state.BestConstraintValues;
double qBefore = state.BestError;
state.Optimize(ConstrainedNLSInternal.OptimizationMode.UpdateParameters);
constraintsAfter = state.BestConstraintValues;
var qOpt = state.BestError;
if (constraintsAfter.Any(cv => cv > 1e-8)) qOpt = qBefore;
qDiff = qOpt - qBefore;
return qOpt;
}
}
}
}