Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16590


Ignore:
Timestamp:
02/06/19 14:37:02 (6 years ago)
Author:
chaider
Message:

#2971
Interval-Parser:

  • Changed Parser ==> after last group there can be now an infinite amount of whitepsaces
  • Save userinput as property
  • Save variable case-sensitive

NamedIntervals

  • Changed Storable attribute from IEnumerable to KeyValuePair, because old Persistance cant handle IEnumerable

Added SymbolicRegressionConstraintAnalyzer
SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator

  • Fixed checking if a given interval is in another interval
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  
    184184    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" />
    185185    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs" />
     186    <Compile Include="SingleObjective\SymbolicRegressionConstraintAnalyzer.cs" />
    186187    <Compile Include="SingleObjective\SymbolicRegressionSolutionsAnalyzer.cs" />
    187188    <Compile Include="SymbolicRegressionPhenotypicDiversityAnalyzer.cs" />
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs

    r16582 r16590  
    2828using HeuristicLab.Core;
    2929using HeuristicLab.Data;
     30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3031using HeuristicLab.Operators;
    3132using HeuristicLab.Optimization;
     
    3536namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3637  [StorableClass]
    37   public class SymbolicRegressionSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
     38  public class SymbolicRegressionConstraintAnalyzer : SingleSuccessorOperator, IAnalyzer {
    3839    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
    4242
    4343    public ILookupParameter<ResultCollection> ResultCollectionParameter {
    4444      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
    4545    }
    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
    5247
    5348    public virtual bool EnabledByDefault {
     
    5651
    5752    [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) {
    6354    }
    6455
    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
    6968    }
    7069
     
    7271    private void AfterDeserialization() {
    7372      // BackwardsCompatibility3.3
    74 
    75       #region Backwards compatible code, remove with 3.4
    76       if (!Parameters.ContainsKey(TrainingQualityParameterName))
    77         Parameters.Add(new LookupParameter<DoubleValue>(TrainingQualityParameterName));
    78       if (!Parameters.ContainsKey(TestQualityParameterName))
    79         Parameters.Add(new LookupParameter<DoubleValue>(TestQualityParameterName));
    80       #endregion
    8173    }
    8274
    8375    public override IOperation Apply() {
    8476      var results = ResultCollectionParameter.ActualValue;
    85 
     77      IntervalConstraintsParser parser = new IntervalConstraintsParser();
     78      var intervalInterpreter = new IntervalInterpreter();
    8679      if (!results.ContainsKey(RegressionSolutionQualitiesResultName)) {
    8780        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));
    8983      }
    9084
    9185      var dataTable = (DataTable)results[RegressionSolutionQualitiesResultName].Value;
    9286
    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 below
    97       trainingQualityParam = TrainingQualityParameter;
    98       string prevTrainingQualityParamName = trainingQualityParam.ActualName;
    99       testQualityParam = TestQualityParameter;
    100       string prevTestQualityParamName = testQualityParam.ActualName;
    10187      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;
    10393
    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        }
    107101
    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            }
    111124
    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        }
    114135
    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;
    121148      }
    122149
    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      }
    125156
    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;
    127175    }
    128176  }
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/NamedIntervals.cs

    r16588 r16590  
    1313  [Item("NamedIntervals", "Represents variables with their interval ranges.")]
    1414  [StorableClass]
    15   public class NamedIntervals : Item {   
     15  public class NamedIntervals : Item {
    1616    Dictionary<string, Interval> variableIntervals = new Dictionary<string, Interval>();
    1717    public Dictionary<string, Interval> VariableIntervals => variableIntervals;
    1818
    1919    [Storable(Name = "StorableIntervalInformation")]
    20     private IEnumerable<Tuple<string, double, double>> StorableIntervalInformation {
     20    private KeyValuePair<string, double[]>[] StorableIntervalInformation {
    2121      get {
     22        var l = new List<KeyValuePair<string, double[]>>();
    2223        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();
    2428      }
    2529
    2630      set {
    2731        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]));
    2933      }
    3034    }
    3135
    32     public NamedIntervals() : base(){}
     36    public NamedIntervals() : base() { }
    3337    protected NamedIntervals(bool deserializing) : base(deserializing) { }
    3438
     
    6266      return true;
    6367    }
    64    
     68
    6569  }
    6670}
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs

    r16587 r16590  
    77namespace HeuristicLab.Problems.DataAnalysis {
    88  public class IntervalConstraint {
     9    public string Derivaiton { get; set; }
    910    public string Definition { get; set; }
    1011    public Interval Interval { get; set; }
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs

    r16587 r16590  
    11using System;
    22using System.Collections.Generic;
     3using System.Globalization;
    34using System.IO;
    45using System.Linq;
     
    1415    public List<IntervalConstraint> Parse(string input) {
    1516      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);
    1718
    1819      for (var i = 0; i < matches.Count; ++i) {
     
    2223          var definition = Regex.Replace(matches[i].Groups[1].Value, @"\s *", "");
    2324          if (Regex.IsMatch(definition, @"\/")) {
    24             var splitted = Regex.Split(definition.ToLower().Replace(" ", string.Empty), @"\/");
     25            var splitted = Regex.Split(definition.Replace(" ", string.Empty), @"\/");
    2526            var match = Regex.Match(splitted[0], @"([d∂])([0-9]|[²³])?(.*[^\s*])");
    2627            if (match.Success) {
     
    2829              intervalConstraint.Definition = match.Groups[3].Value;
    2930              intervalConstraint.IsDerivation = true;
    30               var formulation = Regex.Match(splitted[1], @"([d∂])(.*[^\s*][^²³])([²³])?");
     31              var formulation = Regex.Match(splitted[1], @"([d∂])(.*[^²³])([²³])?");
    3132              if (formulation.Success) {
    3233                intervalConstraint.Variable = formulation.Groups[2].Success ? formulation.Groups[2].Value : "";
     
    3839            intervalConstraint.Definition = Regex.Match(definition, @".*[^.\s]*").Value;
    3940            intervalConstraint.IsDerivation = false;
    40           }     
     41          }
     42          intervalConstraint.Derivaiton = matches[i].Groups[0].Value;
    4143          intervalConstraint.InclusiveLowerBound = (matches[i].Groups[2].Value == "[");
    4244          intervalConstraint.InclusiveUpperBound = (matches[i].Groups[6].Value == "]");
     
    6264          return double.NegativeInfinity;
    6365        default: {
    64           if (double.TryParse(input, out var value)) {
     66          if (double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) {
    6567            return value;
    6668          } else {
Note: See TracChangeset for help on using the changeset viewer.