- Timestamp:
- 03/06/17 16:40:33 (8 years ago)
- Location:
- branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs
r14540 r14717 228 228 // create a set of (virtual) values to match the number of weights 229 229 t.Symbol.VariableNames = new string[] { t.VariableName }; 230 t.Symbol.VariableValues = new KeyValuePair<string, List<string>>[] { new KeyValuePair<string, List<string>>(t.VariableName, weights.Select((_, i) => "X" + i).ToList()) }; 230 t.Symbol.VariableValues = new[] 231 { new KeyValuePair<string, Dictionary<string,int>>( 232 t.VariableName, 233 weights.Select((_, i) => Tuple.Create(_,i)).ToDictionary(tup=>"X" + tup.Item2, tup=>tup.Item2)) }; 231 234 return t; 232 235 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs
r14249 r14717 159 159 [StorableHook(HookType.AfterDeserialization)] 160 160 private void AfterDeserialization() { 161 if 161 if(!Parameters.ContainsKey(ApplyLinearScalingParameterName)) { 162 162 Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false))); 163 163 ApplyLinearScalingParameter.Hidden = true; … … 165 165 //it is assumed that for all symbolic regression algorithms linear scaling was set to true 166 166 //there is no possibility to determine the previous value of the parameter as it was stored in the evaluator 167 if 167 if(GetType().Name.Contains("SymbolicRegression")) 168 168 ApplyLinearScaling.Value = true; 169 169 } … … 213 213 grammar.MaximumFunctionArguments = MaximumFunctionArguments.Value; 214 214 grammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value; 215 foreach 216 if 215 foreach(var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableBase>()) { 216 if(!varSymbol.Fixed) { 217 217 varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<double>(x)); 218 218 varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<double>(x)); 219 219 } 220 220 } 221 foreach 222 if 221 foreach(var factorSymbol in grammar.Symbols.OfType<BinaryFactorVariable>()) { 222 if(!factorSymbol.Fixed) { 223 223 factorSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<string>(x)); 224 224 factorSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<string>(x)); … … 226 226 .ToDictionary(varName => varName, varName => ds.GetStringValues(varName).Distinct().ToList()); 227 227 } 228 } foreach (var factorSymbol in grammar.Symbols.OfType<FactorVariable>()) { 229 if (!factorSymbol.Fixed) { 228 } 229 foreach(var factorSymbol in grammar.Symbols.OfType<FactorVariable>()) { 230 if(!factorSymbol.Fixed) { 230 231 factorSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<string>(x)); 231 232 factorSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<string>(x)); 232 233 factorSymbol.VariableValues = factorSymbol.VariableNames 233 .ToDictionary(varName => varName, varName => ds.GetStringValues(varName).Distinct().ToList()); 234 .ToDictionary(varName => varName, 235 varName => ds.GetStringValues(varName).Distinct() 236 .Select((n, i) => Tuple.Create(n, i)) 237 .ToDictionary(tup => tup.Item1, tup => tup.Item2)); 234 238 } 235 239 } … … 276 280 277 281 private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) { 278 if 282 if(MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3) 279 283 MaximumSymbolicExpressionTreeDepth.Value = 3; 280 284 } … … 304 308 305 309 var handler = ProblemDataChanged; 306 if 310 if(handler != null) handler(this, EventArgs.Empty); 307 311 308 312 OnReset(); … … 313 317 var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList(); 314 318 315 foreach 319 foreach(var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) { 316 320 op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name; 317 321 } 318 foreach 322 foreach(var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) { 319 323 op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name; 320 324 op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name; 321 325 } 322 foreach 326 foreach(var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) { 323 327 op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name; 324 328 op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name; 325 329 } 326 foreach 330 foreach(var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) { 327 331 op.ProblemDataParameter.ActualName = ProblemDataParameterName; 328 332 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; … … 331 335 op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name; 332 336 } 333 foreach 337 foreach(var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) { 334 338 op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 335 339 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 336 340 } 337 foreach 338 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 339 } 340 foreach 341 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 342 } 343 foreach 341 foreach(var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) { 342 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 343 } 344 foreach(var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) { 345 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 346 } 347 foreach(var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) { 344 348 op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name; 345 349 } 346 foreach 350 foreach(var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) { 347 351 op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name; 348 352 } 349 foreach 350 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 351 } 352 foreach 353 foreach(var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) { 354 op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 355 } 356 foreach(var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) { 353 357 op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name; 354 358 op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name; 355 359 } 356 foreach 360 foreach(var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) { 357 361 op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name; 358 362 } 359 foreach 363 foreach(var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) { 360 364 op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name; 361 365 op.ProblemDataParameter.ActualName = ProblemDataParameter.Name; -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariable.cs
r14249 r14717 31 31 [Item("FactorVariable", "Represents a categorical variable (comparable to factors as in R).")] 32 32 public class FactorVariable : VariableBase { 33 private readonly Dictionary<string, List<string>> variableValues;33 private readonly Dictionary<string, Dictionary<string, int>> variableValues; // for each variable value also store a zero-based index 34 34 [Storable] 35 public IEnumerable<KeyValuePair<string, List<string>>> VariableValues {35 public IEnumerable<KeyValuePair<string, Dictionary<string, int>>> VariableValues { 36 36 get { return variableValues; } 37 37 set { 38 if 38 if(value == null) throw new ArgumentNullException(); 39 39 variableValues.Clear(); 40 foreach 41 variableValues.Add(kvp.Key, new List<string>(kvp.Value));40 foreach(var kvp in value) { 41 variableValues.Add(kvp.Key, new Dictionary<string, int>(kvp.Value)); 42 42 } 43 43 } … … 47 47 protected FactorVariable(bool deserializing) 48 48 : base(deserializing) { 49 variableValues = new Dictionary<string, List<string>>();49 variableValues = new Dictionary<string, Dictionary<string, int>>(); 50 50 } 51 51 protected FactorVariable(FactorVariable original, Cloner cloner) 52 52 : base(original, cloner) { 53 53 variableValues = 54 original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new List<string>(kvp.Value));54 original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new Dictionary<string, int>(kvp.Value)); 55 55 } 56 56 public FactorVariable() : this("FactorVariable", "Represents a categorical variable (comparable to factors as in R).") { } 57 57 public FactorVariable(string name, string description) 58 58 : base(name, description) { 59 variableValues = new Dictionary<string, List<string>>();59 variableValues = new Dictionary<string, Dictionary<string,int>>(); 60 60 } 61 61 … … 69 69 70 70 public IEnumerable<string> GetVariableValues(string variableName) { 71 return variableValues[variableName]; 71 return variableValues[variableName].Keys; 72 } 73 74 public int GetIndexForValue(string variableName, string variableValue) { 75 return variableValues[variableName][variableValue]; 72 76 } 73 77 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs
r14554 r14717 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Common; … … 51 52 : base(original, cloner) { 52 53 variableName = original.variableName; 53 if 54 if(original.weights != null) { 54 55 this.weights = new double[original.Weights.Length]; 55 56 Array.Copy(original.Weights, weights, weights.Length); … … 74 75 75 76 public override void ShakeLocalParameters(IRandom random, double shakingFactor) { 76 if 77 if(random.NextDouble() < 0.2) { 77 78 VariableName = Symbol.VariableNames.SampleRandom(random); 78 if 79 if(weights.Length != Symbol.GetVariableValues(VariableName).Count()) { 79 80 // if the length of the weight array does not match => re-initialize weights 80 81 weights = … … 87 88 var idx = random.Next(weights.Length); 88 89 // 50% additive & 50% multiplicative 89 if 90 if(random.NextDouble() < 0.5) { 90 91 double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, 91 92 Symbol.WeightManipulatorSigma); … … 103 104 104 105 public double GetValue(string cat) { 105 // TODO: perf 106 var s = Symbol; 107 int idx = 0; 108 foreach (var val in s.GetVariableValues(VariableName)) { 109 if (cat == val) return weights[idx]; 110 idx++; 111 } 112 throw new ArgumentOutOfRangeException("Found unknown value " + cat + " for variable " + VariableName); 106 return weights[Symbol.GetIndexForValue(VariableName, cat)]; 113 107 } 114 108
Note: See TracChangeset
for help on using the changeset viewer.