Changeset 18075 for branches/3136_Structural_GP
- Timestamp:
- 10/29/21 16:56:22 (3 years ago)
- Location:
- branches/3136_Structural_GP
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18072 r18075 12 12 using HeuristicLab.Data; 13 13 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 14 using HeuristicLab.PluginInfrastructure; 14 15 15 16 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { … … 17 18 [Item(Name = "Structured Symbolic Regression Single Objective Problem (single-objective)", Description = "A problem with a structural definition and unfixed subfunctions.")] 18 19 [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)] 19 public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer< RegressionProblemData> {20 public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData> { 20 21 21 22 #region Constants 22 23 private const string ProblemDataParameterName = "ProblemData"; 23 private const string StructureDefinitionParameterName = "Structure Definition";24 24 private const string StructureTemplateParameterName = "Structure Template"; 25 private const string InterpreterParameterName = "Interpreter"; 25 26 26 27 private const string StructureTemplateDescriptionText = … … 33 34 #region Parameters 34 35 public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; 35 public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName];36 36 public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName]; 37 public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter => (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; 37 38 #endregion 38 39 … … 46 47 } 47 48 48 public string StructureDefinition { 49 get => StructureDefinitionParameter.Value.Value; 50 set => StructureDefinitionParameter.Value.Value = value; 51 } 52 53 public StructureTemplate StructureTemplate { 54 get => StructureTemplateParameter.Value; 55 } 56 57 public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; } = new SymbolicDataAnalysisExpressionTreeInterpreter(); 49 public StructureTemplate StructureTemplate => StructureTemplateParameter.Value; 50 51 public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter => InterpreterParameter.Value; 58 52 59 53 IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter; … … 74 68 structureTemplate.Changed += OnTemplateChanged; 75 69 76 Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData)); 77 Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, 78 StructureTemplateDescriptionText, structureTemplate)); 79 80 70 Parameters.Add(new ValueParameter<IRegressionProblemData>( 71 ProblemDataParameterName, 72 problemData)); 73 74 Parameters.Add(new FixedValueParameter<StructureTemplate>( 75 StructureTemplateParameterName, 76 StructureTemplateDescriptionText, 77 structureTemplate)); 78 79 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>( 80 InterpreterParameterName, 81 new SymbolicDataAnalysisExpressionTreeInterpreter()) 82 { Hidden = true }); 83 84 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged; 81 85 } 82 86 … … 93 97 #endregion 94 98 99 private void ProblemDataParameterValueChanged(object sender, EventArgs e) { 100 StructureTemplate.Reset(); 101 // InfoBox for Reset? 102 } 103 95 104 private void OnTemplateChanged(object sender, EventArgs args) { 96 105 SetupStructureTemplate(); … … 103 112 foreach (var f in StructureTemplate.SubFunctions.Values) { 104 113 SetupVariables(f); 105 if(!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice 106 Encoding.Add(new SymbolicExpressionTreeEncoding(f.Name, f.Grammar, f.MaximumSymbolicExpressionTreeLength, f.MaximumSymbolicExpressionTreeDepth)); 114 if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice 115 Encoding.Add(new SymbolicExpressionTreeEncoding( 116 f.Name, 117 f.Grammar, 118 f.MaximumSymbolicExpressionTreeLength, 119 f.MaximumSymbolicExpressionTreeDepth)); 107 120 } 108 121 } … … 133 146 results.Add(new Result("Best Tree", tree)); 134 147 } 135 136 148 } 137 149 … … 168 180 169 181 private ISymbolicExpressionTree BuildTree(Individual individual) { 182 if (StructureTemplate.Tree == null) 183 throw new ArgumentException("No structure template defined!"); 184 170 185 var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone(); 171 186 … … 175 190 var subFunctionTreeNode = n as SubFunctionTreeNode; 176 191 var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name); 177 //var parent = n.Parent;178 179 // remove SubFunctionTreeNode180 //parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode));181 192 182 193 // add new tree 183 194 var subTree = subFunctionTree.Root.GetSubtree(0) // Start 184 195 .GetSubtree(0); // Offset 185 //parent.AddSubtree(subTree);186 196 subFunctionTreeNode.AddSubtree(subTree); 187 197 } … … 223 233 } 224 234 225 public void Load(RegressionProblemData data) { 226 ProblemData = data; 227 SetupStructureTemplate(); 228 } 235 public void Load(IRegressionProblemData data) => ProblemData = data; 229 236 } 230 237 } -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs
r18073 r18075 2 2 using System.Collections.Generic; 3 3 using System.Drawing; 4 using System.Linq; 4 5 using System.Text; 5 6 using System.Windows.Forms; … … 25 26 InitializeComponent(); 26 27 infoLabel.Text = ""; 28 this.Resize += StructureTemplateViewResize; 27 29 treeChart.SymbolicExpressionTreeNodeClicked += SymbolicExpressionTreeNodeClicked; 28 30 31 } 32 33 private void StructureTemplateViewResize(object sender, EventArgs e) { 34 PaintTree(); 29 35 } 30 36 … … 83 89 infoLabel.Text = "Template structure successfully parsed."; 84 90 infoLabel.ForeColor = Color.DarkGreen; 91 } catch (AggregateException ex) { 92 infoLabel.Text = string.Join("\n", ex.InnerExceptions.Select(x => x.Message)); 93 infoLabel.ForeColor = Color.DarkRed; 85 94 } catch (Exception ex) { 86 95 infoLabel.Text = ex.Message; -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs
r18074 r18075 14 14 #region Properties 15 15 [Storable] 16 private string template = "";16 private string template; 17 17 public string Template { 18 18 get => template; … … 38 38 39 39 [Storable] 40 public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } = new Dictionary<string, SubFunction>();40 public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } 41 41 42 42 [Storable] 43 private bool applyLinearScaling = false;43 private bool applyLinearScaling; 44 44 public bool ApplyLinearScaling { 45 45 get => applyLinearScaling; … … 61 61 62 62 #region Constructors 63 public StructureTemplate() { } 63 public StructureTemplate() { 64 Reset(); 65 } 64 66 65 67 [StorableConstructor] … … 79 81 #endregion 80 82 81 83 public void Reset() { 84 SubFunctions = new Dictionary<string, SubFunction>(); 85 treeWithoutLinearScaling = null; 86 treeWithLinearScaling = null; 87 template = ""; 88 applyLinearScaling = false; 89 OnChanged(); 90 } 82 91 83 92 private Dictionary<string, SubFunction> GetSubFunctions() {
Note: See TracChangeset
for help on using the changeset viewer.