Changeset 16590
- Timestamp:
- 02/06/19 14:37:02 (6 years ago)
- Location:
- branches/2971_named_intervals
- Files:
-
- 4 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r16589 r16590 184 184 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" /> 185 185 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs" /> 186 <Compile Include="SingleObjective\SymbolicRegressionConstraintAnalyzer.cs" /> 186 187 <Compile Include="SingleObjective\SymbolicRegressionSolutionsAnalyzer.cs" /> 187 188 <Compile Include="SymbolicRegressionPhenotypicDiversityAnalyzer.cs" /> -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs
r16582 r16590 28 28 using HeuristicLab.Core; 29 29 using HeuristicLab.Data; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 31 using HeuristicLab.Operators; 31 32 using HeuristicLab.Optimization; … … 35 36 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 36 37 [StorableClass] 37 public class SymbolicRegression SolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {38 public class SymbolicRegressionConstraintAnalyzer : SingleSuccessorOperator, IAnalyzer { 38 39 private const string ResultCollectionParameterName = "Results"; 39 private const string RegressionSolutionQualitiesResultName = "Regression Solution Qualities"; 40 private const string TrainingQualityParameterName = "TrainingRSquared"; 41 private const string TestQualityParameterName = "TestRSquared"; 40 private const string RegressionSolutionQualitiesResultName = "Constraint Violations"; 41 42 42 43 43 public ILookupParameter<ResultCollection> ResultCollectionParameter { 44 44 get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; } 45 45 } 46 public ILookupParameter<DoubleValue> TrainingQualityParameter { 47 get { return (ILookupParameter<DoubleValue>)Parameters[TrainingQualityParameterName]; } 48 } 49 public ILookupParameter<DoubleValue> TestQualityParameter { 50 get { return (ILookupParameter<DoubleValue>)Parameters[TestQualityParameterName]; } 51 } 46 52 47 53 48 public virtual bool EnabledByDefault { … … 56 51 57 52 [StorableConstructor] 58 protected SymbolicRegressionSolutionsAnalyzer(bool deserializing) : base(deserializing) { } 59 protected SymbolicRegressionSolutionsAnalyzer(SymbolicRegressionSolutionsAnalyzer original, Cloner cloner) 60 : base(original, cloner) { } 61 public override IDeepCloneable Clone(Cloner cloner) { 62 return new SymbolicRegressionSolutionsAnalyzer(this, cloner); 53 protected SymbolicRegressionConstraintAnalyzer(bool deserializing) : base(deserializing) { 63 54 } 64 55 65 public SymbolicRegressionSolutionsAnalyzer() { 66 Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, "The result collection to store the analysis results.")); 67 Parameters.Add(new LookupParameter<DoubleValue>(TrainingQualityParameterName)); 68 Parameters.Add(new LookupParameter<DoubleValue>(TestQualityParameterName)); 56 protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) 57 : base(original, cloner) { 58 } 59 60 public override IDeepCloneable Clone(Cloner cloner) { 61 return new SymbolicRegressionConstraintAnalyzer(this, cloner); 62 } 63 64 public SymbolicRegressionConstraintAnalyzer() { 65 Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, 66 "The result collection to store the analysis results.")); 67 69 68 } 70 69 … … 72 71 private void AfterDeserialization() { 73 72 // BackwardsCompatibility3.3 74 75 #region Backwards compatible code, remove with 3.476 if (!Parameters.ContainsKey(TrainingQualityParameterName))77 Parameters.Add(new LookupParameter<DoubleValue>(TrainingQualityParameterName));78 if (!Parameters.ContainsKey(TestQualityParameterName))79 Parameters.Add(new LookupParameter<DoubleValue>(TestQualityParameterName));80 #endregion81 73 } 82 74 83 75 public override IOperation Apply() { 84 76 var results = ResultCollectionParameter.ActualValue; 85 77 IntervalConstraintsParser parser = new IntervalConstraintsParser(); 78 var intervalInterpreter = new IntervalInterpreter(); 86 79 if (!results.ContainsKey(RegressionSolutionQualitiesResultName)) { 87 80 var newDataTable = new DataTable(RegressionSolutionQualitiesResultName); 88 results.Add(new Result(RegressionSolutionQualitiesResultName, "Chart displaying the training and test qualities of the regression solutions.", newDataTable)); 81 results.Add(new Result(RegressionSolutionQualitiesResultName, "Chart displaying the constraint violatoins.", 82 newDataTable)); 89 83 } 90 84 91 85 var dataTable = (DataTable)results[RegressionSolutionQualitiesResultName].Value; 92 86 93 // only if the parameters are available (not available in old persisted code)94 ILookupParameter<DoubleValue> trainingQualityParam = null;95 ILookupParameter<DoubleValue> testQualityParam = null;96 // store actual names of parameter because it is changed below97 trainingQualityParam = TrainingQualityParameter;98 string prevTrainingQualityParamName = trainingQualityParam.ActualName;99 testQualityParam = TestQualityParameter;100 string prevTestQualityParamName = testQualityParam.ActualName;101 87 foreach (var result in results.Where(r => r.Value is IRegressionSolution)) { 102 var solution = (IRegressionSolution)result.Value; 88 var solution = (ISymbolicRegressionSolution)result.Value; 89 var constraints = 90 parser.Parse(((RegressionProblemData)solution.ProblemData).IntervalConstraintsParameter.Value.Value); 91 var variableRanges = ((RegressionProblemData)solution.ProblemData).VariableRangesParameter.Value 92 .VariableIntervals; 103 93 104 var trainingR2Name = result.Name + " Training R²"; 105 if (!dataTable.Rows.ContainsKey(trainingR2Name)) 106 dataTable.Rows.Add(new DataRow(trainingR2Name)); 94 if (dataTable.Rows.Count == 0) { 95 foreach (var constraint in constraints) { 96 if (!dataTable.Rows.ContainsKey(constraint.Derivaiton)) { 97 dataTable.Rows.Add(new DataRow(constraint.Derivaiton)); 98 } 99 } 100 } 107 101 108 var testR2Name = result.Name + " Test R²"; 109 if (!dataTable.Rows.ContainsKey(testR2Name)) 110 dataTable.Rows.Add(new DataRow(testR2Name)); 102 foreach (var constraint in constraints) { 103 if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable)) 104 throw new ArgumentException( 105 $"The given variable {constraint.Variable} in the constraint does not exists in the model.", 106 nameof(IntervalConstraintsParser)); 107 var numberOfViolations = dataTable.Rows[constraint.Derivaiton].Values.Count > 0 108 ? dataTable.Rows[constraint.Derivaiton].Values.Last() 109 : 0; 110 if (!constraint.IsDerivation) { 111 var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution.Model.SymbolicExpressionTree, 112 variableRanges); 113 if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound, 114 constraint.InclusiveUpperBound)) { 115 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations + 1); 116 } else { 117 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations); 118 } 119 } else { 120 var tree = solution.Model.SymbolicExpressionTree; 121 for (var i = 0; i < constraint.NumberOfDerivation; ++i) { 122 tree = DerivativeCalculator.Derive(tree, constraint.Variable); 123 } 111 124 112 dataTable.Rows[trainingR2Name].Values.Add(solution.TrainingRSquared); 113 dataTable.Rows[testR2Name].Values.Add(solution.TestRSquared); 125 var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution.Model.SymbolicExpressionTree, 126 variableRanges); 127 if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound, 128 constraint.InclusiveUpperBound)) { 129 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations + 1); 130 } else { 131 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations); 132 } 133 } 134 } 114 135 115 // also add training and test R² to the scope using the parameters 116 // HACK: we change the ActualName of the parameter to write two variables for each solution in the results collection 117 trainingQualityParam.ActualName = trainingR2Name; 118 trainingQualityParam.ActualValue = new DoubleValue(solution.TrainingRSquared); 119 testQualityParam.ActualName = testR2Name; 120 testQualityParam.ActualValue = new DoubleValue(solution.TestRSquared); 136 } 137 return base.Apply(); 138 } 139 140 private static bool IntervalInBoundaries(Interval i1, Interval i2, bool inclusiveLower, bool inclusiveUpper) { 141 if (double.IsNegativeInfinity(i1.LowerBound) && double.IsPositiveInfinity(i1.UpperBound)) 142 return true; 143 //Left-unbounded and right-bounded: 144 if (double.IsNegativeInfinity(i1.LowerBound)) { 145 if (inclusiveUpper) 146 return i2.LowerBound <= i1.UpperBound && i2.UpperBound <= i1.UpperBound; 147 return i2.LowerBound < i1.UpperBound && i2.UpperBound < i1.UpperBound; 121 148 } 122 149 123 trainingQualityParam.ActualName = prevTrainingQualityParamName; 124 testQualityParam.ActualName = prevTestQualityParamName; 150 //Left-bounded and right-unbounded: 151 if (double.IsPositiveInfinity(i1.UpperBound)) { 152 if (inclusiveLower) 153 return i2.LowerBound >= i1.LowerBound && i2.UpperBound >= i1.LowerBound; 154 return i2.LowerBound > i1.LowerBound && i2.UpperBound > i1.LowerBound; 155 } 125 156 126 return base.Apply(); 157 //Proper and bounded: 158 //Closed: 159 if (inclusiveLower && inclusiveUpper) { 160 return i1.LowerBound <= i2.LowerBound && i2.UpperBound <= i1.UpperBound; 161 } 162 163 //Open: 164 if (!inclusiveLower && !inclusiveUpper) { 165 return i1.LowerBound < i2.LowerBound && i2.UpperBound < i1.UpperBound; 166 } 167 168 //Left-closed, right-open: 169 if (inclusiveLower) { 170 return i1.LowerBound <= i2.LowerBound && i2.UpperBound < i1.UpperBound; 171 } 172 173 //Left-open, right-closed: 174 return i1.LowerBound < i2.LowerBound && i2.UpperBound <= i1.UpperBound; 127 175 } 128 176 } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/NamedIntervals.cs
r16588 r16590 13 13 [Item("NamedIntervals", "Represents variables with their interval ranges.")] 14 14 [StorableClass] 15 public class NamedIntervals : Item { 15 public class NamedIntervals : Item { 16 16 Dictionary<string, Interval> variableIntervals = new Dictionary<string, Interval>(); 17 17 public Dictionary<string, Interval> VariableIntervals => variableIntervals; 18 18 19 19 [Storable(Name = "StorableIntervalInformation")] 20 private IEnumerable<Tuple<string, double, double>>StorableIntervalInformation {20 private KeyValuePair<string, double[]>[] StorableIntervalInformation { 21 21 get { 22 var l = new List<KeyValuePair<string, double[]>>(); 22 23 foreach (var varInt in variableIntervals) 23 yield return Tuple.Create(varInt.Key, varInt.Value.LowerBound, varInt.Value.UpperBound); 24 25 l.Add(new KeyValuePair<string, double[]>(varInt.Key, 26 new double[] { varInt.Value.LowerBound, varInt.Value.UpperBound })); 27 return l.ToArray(); 24 28 } 25 29 26 30 set { 27 31 foreach (var varInt in value) 28 variableIntervals.Add(varInt. Item1,new Interval(varInt.Item2,varInt.Item3));32 variableIntervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1])); 29 33 } 30 34 } 31 35 32 public NamedIntervals() : base() {}36 public NamedIntervals() : base() { } 33 37 protected NamedIntervals(bool deserializing) : base(deserializing) { } 34 38 … … 62 66 return true; 63 67 } 64 68 65 69 } 66 70 } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs
r16587 r16590 7 7 namespace HeuristicLab.Problems.DataAnalysis { 8 8 public class IntervalConstraint { 9 public string Derivaiton { get; set; } 9 10 public string Definition { get; set; } 10 11 public Interval Interval { get; set; } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs
r16587 r16590 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Globalization; 3 4 using System.IO; 4 5 using System.Linq; … … 14 15 public List<IntervalConstraint> Parse(string input) { 15 16 var options = RegexOptions.Multiline | RegexOptions.IgnoreCase; 16 var matches = Regex.Matches(input, @"^(.*)\bin\b\s*([\[\]])(.*[^\s])(\s*\.\.\s*)([^\s].*)([\[\]])\r? $", options);17 var matches = Regex.Matches(input, @"^(.*)\bin\b\s*([\[\]])(.*[^\s])(\s*\.\.\s*)([^\s].*)([\[\]])\r?\s*$", options); 17 18 18 19 for (var i = 0; i < matches.Count; ++i) { … … 22 23 var definition = Regex.Replace(matches[i].Groups[1].Value, @"\s *", ""); 23 24 if (Regex.IsMatch(definition, @"\/")) { 24 var splitted = Regex.Split(definition. ToLower().Replace(" ", string.Empty), @"\/");25 var splitted = Regex.Split(definition.Replace(" ", string.Empty), @"\/"); 25 26 var match = Regex.Match(splitted[0], @"([d∂])([0-9]|[²³])?(.*[^\s*])"); 26 27 if (match.Success) { … … 28 29 intervalConstraint.Definition = match.Groups[3].Value; 29 30 intervalConstraint.IsDerivation = true; 30 var formulation = Regex.Match(splitted[1], @"([d∂])(.*[^ \s*][^²³])([²³])?");31 var formulation = Regex.Match(splitted[1], @"([d∂])(.*[^²³])([²³])?"); 31 32 if (formulation.Success) { 32 33 intervalConstraint.Variable = formulation.Groups[2].Success ? formulation.Groups[2].Value : ""; … … 38 39 intervalConstraint.Definition = Regex.Match(definition, @".*[^.\s]*").Value; 39 40 intervalConstraint.IsDerivation = false; 40 } 41 } 42 intervalConstraint.Derivaiton = matches[i].Groups[0].Value; 41 43 intervalConstraint.InclusiveLowerBound = (matches[i].Groups[2].Value == "["); 42 44 intervalConstraint.InclusiveUpperBound = (matches[i].Groups[6].Value == "]"); … … 62 64 return double.NegativeInfinity; 63 65 default: { 64 if (double.TryParse(input, out var value)) {66 if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) { 65 67 return value; 66 68 } else {
Note: See TracChangeset
for help on using the changeset viewer.