[18061] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Optimization;
|
---|
| 8 | using HEAL.Attic;
|
---|
| 9 | using HeuristicLab.Common;
|
---|
| 10 | using HeuristicLab.Problems.Instances;
|
---|
| 11 | using HeuristicLab.Parameters;
|
---|
| 12 | using HeuristicLab.Data;
|
---|
[18062] | 13 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[18061] | 14 |
|
---|
[18063] | 15 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[18061] | 16 | [StorableType("7464E84B-65CC-440A-91F0-9FA920D730F9")]
|
---|
[18063] | 17 | [Item(Name = "Structured Symbolic Regression Single Objective Problem (single-objective)", Description = "A problem with a structural definition and unfixed subfunctions.")]
|
---|
| 18 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)]
|
---|
[18061] | 19 | public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<RegressionProblemData> {
|
---|
| 20 |
|
---|
| 21 | #region Constants
|
---|
| 22 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 23 | private const string StructureDefinitionParameterName = "Structure Definition";
|
---|
[18063] | 24 | private const string StructureTemplateParameterName = "Structure Template";
|
---|
[18061] | 25 | #endregion
|
---|
| 26 |
|
---|
| 27 | #region Parameter
|
---|
| 28 | public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
|
---|
| 29 | public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName];
|
---|
[18063] | 30 | public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
|
---|
[18061] | 31 | #endregion
|
---|
| 32 |
|
---|
| 33 | #region Properties
|
---|
| 34 | public IRegressionProblemData ProblemData {
|
---|
| 35 | get => ProblemDataParameter.Value;
|
---|
| 36 | set {
|
---|
| 37 | ProblemDataParameter.Value = value;
|
---|
| 38 | ProblemDataChanged?.Invoke(this, EventArgs.Empty);
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public string StructureDefinition {
|
---|
| 43 | get => StructureDefinitionParameter.Value.Value;
|
---|
| 44 | set => StructureDefinitionParameter.Value.Value = value;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[18063] | 47 | public StructureTemplate StructureTemplate {
|
---|
| 48 | get => StructureTemplateParameter.Value;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[18061] | 51 | IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
|
---|
| 52 | IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
|
---|
| 53 |
|
---|
[18066] | 54 | public override bool Maximization => true;
|
---|
[18061] | 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | #region EventHandlers
|
---|
| 58 | public event EventHandler ProblemDataChanged;
|
---|
| 59 | #endregion
|
---|
| 60 |
|
---|
| 61 | #region Constructors & Cloning
|
---|
| 62 | public StructuredSymbolicRegressionSingleObjectiveProblem() {
|
---|
[18062] | 63 | var problemData = new ShapeConstrainedRegressionProblemData();
|
---|
| 64 |
|
---|
[18065] | 65 | var structureTemplate = new StructureTemplate();
|
---|
[18066] | 66 | structureTemplate.Changed += OnTemplateChanged;
|
---|
[18065] | 67 |
|
---|
[18066] | 68 | Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData));
|
---|
[18065] | 69 | Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate));
|
---|
[18066] | 70 |
|
---|
[18061] | 71 | }
|
---|
| 72 |
|
---|
[18063] | 73 | public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, Cloner cloner) { }
|
---|
[18061] | 74 |
|
---|
| 75 | [StorableConstructor]
|
---|
[18063] | 76 | protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[18065] | 77 | #endregion
|
---|
[18061] | 78 |
|
---|
[18065] | 79 | #region Cloning
|
---|
[18061] | 80 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
| 81 | new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
|
---|
| 82 | #endregion
|
---|
| 83 |
|
---|
[18066] | 84 | private void OnTemplateChanged(object sender, EventArgs args) {
|
---|
[18068] | 85 | SetupStructureTemplate();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private void SetupStructureTemplate() {
|
---|
[18066] | 89 | foreach (var e in Encoding.Encodings.ToArray())
|
---|
| 90 | Encoding.Remove(e);
|
---|
| 91 |
|
---|
[18068] | 92 | foreach (var f in StructureTemplate.SubFunctions.Values) {
|
---|
| 93 | SetupVariables(f);
|
---|
| 94 | if(!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice
|
---|
| 95 | Encoding.Add(new SymbolicExpressionTreeEncoding(f.Name, f.Grammar, f.MaximumSymbolicExpressionTreeLength, f.MaximumSymbolicExpressionTreeDepth));
|
---|
[18066] | 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 100 | base.Analyze(individuals, qualities, results, random);
|
---|
| 101 |
|
---|
| 102 | int bestIdx = 0;
|
---|
| 103 | double bestQuality = Maximization ? double.MinValue : double.MaxValue;
|
---|
| 104 | for(int idx = 0; idx < qualities.Length; ++idx) {
|
---|
| 105 | if((Maximization && qualities[idx] > bestQuality) ||
|
---|
| 106 | (!Maximization && qualities[idx] < bestQuality)) {
|
---|
| 107 | bestQuality = qualities[idx];
|
---|
| 108 | bestIdx = idx;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | if (results.TryGetValue("Best Tree", out IResult result))
|
---|
| 113 | result.Value = BuildTree(individuals[bestIdx]);
|
---|
| 114 | else
|
---|
| 115 | results.Add(new Result("Best Tree", BuildTree(individuals[bestIdx])));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[18065] | 118 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
[18066] | 119 | var tree = BuildTree(individual);
|
---|
| 120 | var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 121 | var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable);
|
---|
| 122 | var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
|
---|
| 123 | interpreter, tree,
|
---|
| 124 | estimationInterval.LowerBound, estimationInterval.UpperBound,
|
---|
| 125 | ProblemData, ProblemData.TrainingIndices, false);
|
---|
| 126 |
|
---|
| 127 | return quality;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | private ISymbolicExpressionTree BuildTree(Individual individual) {
|
---|
[18065] | 131 | var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
|
---|
[18066] | 132 |
|
---|
[18065] | 133 | // build main tree
|
---|
| 134 | foreach (var n in templateTree.IterateNodesPrefix()) {
|
---|
[18066] | 135 | if (n.Symbol is SubFunctionSymbol) {
|
---|
| 136 | var subFunctionTreeNode = n as SubFunctionTreeNode;
|
---|
[18068] | 137 | var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
|
---|
[18066] | 138 | var parent = n.Parent;
|
---|
[18062] | 139 |
|
---|
[18066] | 140 | // remove SubFunctionTreeNode
|
---|
| 141 | parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode));
|
---|
[18065] | 142 |
|
---|
[18066] | 143 | // add new tree
|
---|
| 144 | var subTree = subFunctionTree.Root.GetSubtree(0) // Start
|
---|
| 145 | .GetSubtree(0); // Offset
|
---|
| 146 | parent.AddSubtree(subTree);
|
---|
[18065] | 147 | }
|
---|
| 148 | }
|
---|
[18066] | 149 | return templateTree;
|
---|
[18061] | 150 | }
|
---|
| 151 |
|
---|
[18068] | 152 | private void SetupVariables(SubFunction subFunction) {
|
---|
| 153 | var varSym = (Variable)subFunction.Grammar.GetSymbol("Variable");
|
---|
| 154 | if (varSym == null) {
|
---|
| 155 | varSym = new Variable();
|
---|
| 156 | subFunction.Grammar.AddSymbol(varSym);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | var allVariables = ProblemData.InputVariables.Select(x => x.Value);
|
---|
| 160 | var allInputs = allVariables.Where(x => x != ProblemData.TargetVariable);
|
---|
| 161 |
|
---|
| 162 | // set all variables
|
---|
| 163 | varSym.AllVariableNames = allVariables;
|
---|
| 164 |
|
---|
| 165 | // set all allowed variables
|
---|
| 166 | if (subFunction.Arguments.Contains("_")) {
|
---|
| 167 | varSym.VariableNames = allInputs;
|
---|
| 168 | } else {
|
---|
| 169 | var vars = new List<string>();
|
---|
| 170 | var exceptions = new List<Exception>();
|
---|
| 171 | foreach (var arg in subFunction.Arguments) {
|
---|
| 172 | if (allInputs.Contains(arg))
|
---|
| 173 | vars.Add(arg);
|
---|
| 174 | else
|
---|
| 175 | exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{subFunction.Name}' is not a valid variable."));
|
---|
| 176 | }
|
---|
| 177 | if (exceptions.Any())
|
---|
| 178 | throw new AggregateException(exceptions);
|
---|
| 179 | varSym.VariableNames = vars;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | varSym.Enabled = true;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[18061] | 185 | public void Load(RegressionProblemData data) {
|
---|
| 186 | ProblemData = data;
|
---|
[18068] | 187 | SetupStructureTemplate();
|
---|
[18061] | 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|