Changeset 18184
- Timestamp:
- 01/12/22 14:09:02 (3 years ago)
- Location:
- branches/3136_Structural_GP
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
r18182 r18184 1 1 using System; 2 using System.Collections.Generic;3 2 using System.Linq; 4 3 using HEAL.Attic; 5 4 using HeuristicLab.Common; 6 5 using HeuristicLab.Core; 7 using HeuristicLab.Data;8 6 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 9 7 using HeuristicLab.Optimization; … … 28 26 29 27 private const string SymbolicExpressionTreeName = "SymbolicExpressionTree"; 30 private const string VariableName = "Variable";31 28 32 29 private const string StructureTemplateDescriptionText = … … 127 124 } 128 125 129 public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, 130 Cloner cloner) : base(original, cloner) { 126 public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { 131 127 RegisterEventHandlers(); 132 128 } 129 130 public override IDeepCloneable Clone(Cloner cloner) => 131 new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner); 133 132 134 133 [StorableConstructor] … … 143 142 #endregion 144 143 145 #region Cloning 146 public override IDeepCloneable Clone(Cloner cloner) => 147 new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner); 148 #endregion 144 private void RegisterEventHandlers() { 145 if (StructureTemplate != null) { 146 StructureTemplate.Changed += OnTemplateChanged; 147 } 148 149 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged; 150 } 149 151 150 152 private void ProblemDataParameterValueChanged(object sender, EventArgs e) { … … 153 155 } 154 156 155 private void RegisterEventHandlers() {156 if (StructureTemplate != null) {157 StructureTemplate.Changed += OnTemplateChanged;158 }159 160 ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;161 }162 163 157 private void OnTemplateChanged(object sender, EventArgs args) { 164 Setup StructureTemplate();165 } 166 167 private void Setup StructureTemplate() {158 SetupEncoding(); 159 } 160 161 private void SetupEncoding() { 168 162 foreach (var e in Encoding.Encodings.ToArray()) 169 163 Encoding.Remove(e); 170 164 171 foreach (var fin StructureTemplate.SubFunctions) {172 SetupVariables(f);165 foreach (var subFunction in StructureTemplate.SubFunctions) { 166 subFunction.SetupVariables(ProblemData.AllowedInputVariables); 173 167 // to prevent the same encoding twice 174 if (!Encoding.Encodings.Any(x => x.Name == f.Name)) 175 Encoding.Add(CreateEncoding(f)); 176 } 177 178 ParameterizeEncoding(); 168 if (Encoding.Encodings.Any(x => x.Name == subFunction.Name)) continue; // duplicate subfunction 169 170 var encoding = new SymbolicExpressionTreeEncoding( 171 subFunction.Name, 172 subFunction.Grammar, 173 subFunction.MaximumSymbolicExpressionTreeLength, 174 subFunction.MaximumSymbolicExpressionTreeDepth); 175 Encoding.Add(encoding); 176 } 177 178 //set multi manipulator as default manipulator for all encoding parts 179 var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().FirstOrDefault(); 180 if (manipulator != null) { 181 foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) { 182 var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault(); 183 param.Value = m == null ? param.ValidValues.First() : m; 184 } 185 } 179 186 } 180 187 … … 198 205 199 206 public override double Evaluate(Individual individual, IRandom random) { 200 var tree = BuildTree(individual); 207 var templateTree = StructureTemplate.Tree; 208 if (templateTree == null) 209 throw new ArgumentException("No structure template defined!"); 210 211 var tree = BuildTree(templateTree, individual); 201 212 202 213 // NMSEConstraintsEvaluator sets linear scaling terms itself … … 233 244 } 234 245 235 private ISymbolicExpressionTree BuildTree(Individual individual) { 236 if (StructureTemplate.Tree == null) 237 throw new ArgumentException("No structure template defined!"); 238 239 var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone(); 246 private static ISymbolicExpressionTree BuildTree(ISymbolicExpressionTree template, Individual individual) { 247 var resolvedTree = (ISymbolicExpressionTree)template.Clone(); 240 248 241 249 // build main tree 242 foreach (var subFunctionTreeNode in templateTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {250 foreach (var subFunctionTreeNode in resolvedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) { 243 251 var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name); 244 252 245 // add newtree246 var subTree = subFunctionTree.Root.GetSubtree(0) // Start 247 .GetSubtree(0); // Offset253 // extract function tree 254 var subTree = subFunctionTree.Root.GetSubtree(0) // StartSymbol 255 .GetSubtree(0); // First Symbol 248 256 subTree = (ISymbolicExpressionTreeNode)subTree.Clone(); 249 257 subFunctionTreeNode.AddSubtree(subTree); 250 251 } 252 return templateTree; 253 } 254 255 private SymbolicExpressionTreeEncoding CreateEncoding(SubFunction subFunction) { 256 var encoding = new SymbolicExpressionTreeEncoding( 257 subFunction.Name, 258 subFunction.Grammar, 259 subFunction.MaximumSymbolicExpressionTreeLength, 260 subFunction.MaximumSymbolicExpressionTreeDepth); 261 return encoding; 262 } 263 264 private void ParameterizeEncoding() { 265 var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().FirstOrDefault(); 266 if (manipulator != null) { 267 foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) { 268 var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault(); 269 param.Value = m == null ? param.ValidValues.First() : m; 270 } 271 } 272 } 273 private void SetupVariables(SubFunction subFunction) { 274 var varSym = (Variable)subFunction.Grammar.GetSymbol(VariableName); 275 if (varSym == null) { 276 varSym = new Variable(); 277 subFunction.Grammar.AddSymbol(varSym); 278 } 279 280 var allVariables = ProblemData.InputVariables.Select(x => x.Value); 281 var allInputs = allVariables.Where(x => x != ProblemData.TargetVariable); 282 283 // set all variables 284 varSym.AllVariableNames = allVariables; 285 286 // set all allowed variables 287 if (subFunction.Arguments.Contains("_")) { 288 varSym.VariableNames = allInputs; 289 } else { 290 var vars = new List<string>(); 291 var exceptions = new List<Exception>(); 292 foreach (var arg in subFunction.Arguments) { 293 if (allInputs.Contains(arg)) 294 vars.Add(arg); 295 else 296 exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{subFunction.Name}' is not a valid variable.")); 297 } 298 if (exceptions.Any()) 299 throw new AggregateException(exceptions); 300 varSym.VariableNames = vars; 301 } 302 303 varSym.Enabled = true; 304 } 258 } 259 return resolvedTree; 260 } 261 305 262 306 263 public void Load(IRegressionProblemData data) { -
branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs
r18182 r18184 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 using System.Text;5 using System.Threading.Tasks;6 using HeuristicLab.Core;7 4 using HEAL.Attic; 8 5 using HeuristicLab.Common; 9 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 6 using HeuristicLab.Core; 7 using HeuristicLab.Data; 10 8 using HeuristicLab.Parameters; 11 using HeuristicLab.Data;12 9 13 10 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 65 62 [StorableConstructor] 66 63 protected SubFunction(StorableConstructorFlag _) : base(_) { } 64 public override IDeepCloneable Clone(Cloner cloner) => 65 new SubFunction(this, cloner); 67 66 68 67 … … 73 72 #endregion 74 73 75 #region Cloning76 public override IDeepCloneable Clone(Cloner cloner) =>77 new SubFunction(this, cloner);78 #endregion79 74 80 75 #region Event Handling … … 95 90 96 91 public override bool Equals(object obj) => (obj is SubFunction other) && other.ToString() == ToString(); 92 93 public void SetupVariables(IEnumerable<string> inputVariables) { 94 var allowedInputVariables = new List<string>(inputVariables); 95 96 foreach (var varSym in Grammar.Symbols.OfType<Variable>()) { 97 // set all variables 98 varSym.AllVariableNames = allowedInputVariables; 99 100 // set all allowed variables 101 if (Arguments.Contains("_")) { 102 varSym.VariableNames = allowedInputVariables; 103 } else { 104 var vars = new List<string>(); 105 var exceptions = new List<Exception>(); 106 foreach (var arg in Arguments) { 107 if (allowedInputVariables.Contains(arg)) 108 vars.Add(arg); 109 else 110 exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{Name}' is not a valid variable.")); 111 } 112 if (exceptions.Any()) 113 throw new AggregateException(exceptions); 114 varSym.VariableNames = vars; 115 } 116 117 varSym.Enabled = true; 118 } 119 } 97 120 } 98 121 }
Note: See TracChangeset
for help on using the changeset viewer.