Changeset 16773


Ignore:
Timestamp:
04/09/19 14:58:24 (3 months ago)
Author:
chaider
Message:

#2971

  • Fixes and updates in IntervalConstraintView
  • Changed Parser
Location:
branches/2971_named_intervals
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs

    r16756 r16773  
    8585    }
    8686
    87     static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model,
     87    public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model,
    8888      IRegressionProblemData problemData) {
    89       //var constraints = IntervalConstraintsParser.Parse(((RegressionProblemData)problemData).IntervalConstraints.Value);
    9089      var constraints = ((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Constraints;
    9190      var constraintViolations = new Dictionary<IntervalConstraint, bool>();
     
    101100    }
    102101
    103     static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model,
     102    public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model,
    104103      IRegressionProblemData problemData) {
    105104      return CheckConstraints(model.SymbolicExpressionTree, problemData);
    106105    }
    107106
    108     static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges,
     107    public static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges,
    109108      IntervalConstraint constraint) {
    110109      var intervalInterpreter = new IntervalInterpreter();
     
    148147      var constraintViolations = new Dictionary<string, int>();
    149148
    150       //var constraints = IntervalConstraintsParser.Parse(problemData.IntervalConstraints.Value);
    151149      var constraints = problemData.IntervalConstraintsParameter.Value.Constraints;
    152150
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/ParsedConstraintView.cs

    r16772 r16773  
    11using System;
    2 using System.Collections.Generic;
    3 using System.ComponentModel;
    4 using System.Drawing;
    5 using System.Data;
    6 using System.Linq;
    7 using System.Text;
    8 using System.Threading.Tasks;
    9 using System.Windows.Forms;
     2
    103using HeuristicLab.Core;
    114using HeuristicLab.MainForm;
     
    2114      set => base.Content = value;
    2215    }
     16
    2317    public ParsedConstraintView() {
    2418      InitializeComponent();
     
    3832
    3933    private ItemList<IntervalConstraint> ParseConstraints(string input) {
    40       ItemList<IntervalConstraint> constraints = new ItemList<IntervalConstraint>(IntervalConstraintsParser.Parse(input));
     34      ItemList<IntervalConstraint> constraints = new ItemList<IntervalConstraint>(IntervalConstraintsParser.ParseInput(input, Content.TargetVariable, Content.AllowedVariables));
    4135      return constraints;
    42     }
    43 
    44     private string ConstraintsOutput(IEnumerable<IntervalConstraint> constraints) {
    45       var result = "";
    46       var count = 1;
    47       foreach (var constraint in constraints) {
    48         result += "Constraint #" + count + ": " + Environment.NewLine;
    49         result += constraint.Expression + Environment.NewLine + Environment.NewLine;
    50         count++;
    51       }
    52 
    53       return result;
    5436    }
    5537
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs

    r16772 r16773  
    1111  [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")]
    1212  [Item("Interval Constraint", "Constraint on intervals.")]
    13   public class IntervalConstraint : Item {
     13  public class IntervalConstraint : NamedItem {
    1414    public string Expression { get; set; }
    1515    public string Definition { get; set; }
     
    2323    public IntervalConstraint() {}
    2424
     25    public IntervalConstraint(string name) {
     26      base.name = name;
     27    }
     28
    2529    public IntervalConstraint(string expression, string definition, Interval interval, bool inclusiveLowerBound,
    2630      bool inclusiveUpperBound, bool isDerivation, string variable, int numberOfDerivation) {
     31      base.name = expression;
    2732      Expression = expression;
    2833      Definition = definition;
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs

    r16732 r16773  
    22using System.Collections.Generic;
    33using System.Globalization;
     4using System.Linq;
    45using System.Text.RegularExpressions;
    56
     
    4647    }
    4748
     49    public static IEnumerable<IntervalConstraint> ParseInput(string input, string target = "", IEnumerable<string> variables = null) {
     50      var lines = input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
     51      foreach (var line in lines) {
     52        var trimmedLine = line.TrimStart();
     53        //Check for target-variable constraint
     54        if (trimmedLine.StartsWith("Target:")) {
     55          var start = "Target:".Length;
     56          var end = trimmedLine.Length;
     57          var targetConstraint = trimmedLine.Substring(start, end-start);
     58          var match = Regex.Match(targetConstraint,
     59            @"([^\s]*)\s*(\bin\b)\s*([\[\]])\s*(\S*)\s*(..)\s*(\S*)\s*([\[\]])");
     60          if (match.Success) {
     61            if (match.Groups.Count != 8) {
     62              throw new ArgumentException("The given target-constraint is not complete!");
     63            } else {
     64              if (target != "") {
     65                if (match.Groups[1].Value.Trim() != target) {
     66                  throw new ArgumentException("The given target variable is not in the given dataset!");
     67                }
     68              }
     69              var lowerBound = ParseIntervalBounds(match.Groups[4].Value);
     70              var upperBound = ParseIntervalBounds(match.Groups[6].Value);
     71              var constraint = new IntervalConstraint(match.Groups[0].Value);
     72              constraint.Expression = match.Groups[0].Value;
     73              constraint.Definition = "";
     74              constraint.Variable = match.Groups[1].Value.Trim();
     75              constraint.InclusiveLowerBound = match.Groups[3].Value.Trim() == "[";
     76              constraint.InclusiveUpperBound = match.Groups[7].Value.Trim() == "]";
     77              constraint.IsDerivation = false;
     78              constraint.NumberOfDerivation = 0;
     79              constraint.Interval = new Interval(lowerBound, upperBound);
     80
     81              yield return constraint;
     82            }
     83          } else {
     84            throw new ArgumentException("The inserted target constraint is not valid!");
     85          }
     86          //Check for derivation
     87        } else if (trimmedLine.StartsWith("d") || trimmedLine.StartsWith("\u2202")) {
     88          var match = Regex.Match(trimmedLine,
     89            @"([d∂])([²³])?\s*(\S*)\s*(\/)([d∂])\s*([^²³]*)([²³])?\s*\bin\b\s*([\[\]])\s*(\S*)\s*(..)\s*(\S*)\s*([\[\]])");
     90
     91          if (match.Success) {
     92            if (match.Groups.Count != 13) {
     93              throw new ArgumentException("The given derivation-constraint is not complete");
     94            } else {
     95              if (target != "") {
     96                if (match.Groups[3].Value != target)
     97                  throw new ArgumentException("The given target variable is not given in the dataset!");
     98              }
     99
     100              if (variables != null && variables.Any()) {
     101                if (variables.All(v => v != match.Groups[6].Value.Trim())) {
     102                  throw new ArgumentException("The given variable does not exist in the dataset!");
     103                }
     104              }
     105
     106              if (match.Groups[2].Value.Trim() != "" || match.Groups[7].Value.Trim() != "") {
     107                if (match.Groups[2].Value.Trim() == "" || match.Groups[7].Value.Trim() == "")
     108                  throw new ArgumentException("Number of derivation has to be written on both sides!");
     109                if (match.Groups[2].Value.Trim() != match.Groups[7].Value.Trim())
     110                  throw new ArgumentException("Derivation number is not equal on both sides!");
     111              }
     112
     113              var lowerBound = ParseIntervalBounds(match.Groups[9].Value);
     114              var upperBound = ParseIntervalBounds(match.Groups[11].Value);
     115              var constraint = new IntervalConstraint(match.Groups[0].Value);
     116              constraint.Expression = match.Groups[0].Value;
     117              constraint.Definition = match.Groups[1].Value + match.Groups[2].Value + match.Groups[3].Value +
     118                                      match.Groups[4].Value + match.Groups[5].Value + match.Groups[6].Value + match.Groups[7].Value;
     119              constraint.IsDerivation = true;
     120              constraint.InclusiveLowerBound = match.Groups[8].Value.Trim() == "[";
     121              constraint.InclusiveUpperBound = match.Groups[12].Value.Trim() == "]";
     122              constraint.Variable = match.Groups[6].Value.Trim();
     123              constraint.NumberOfDerivation = ParseDerivationCount(match.Groups[2].Value);
     124              constraint.Interval = new Interval(lowerBound, upperBound);
     125
     126              yield return constraint;
     127            }
     128          } else {
     129            throw new ArgumentException("The inserted derivation constraint is not valid!");
     130          }
     131          //Check for comment
     132        } else if (trimmedLine.StartsWith("#") || trimmedLine == "") {
     133          //If it is a comment just continue without saving anything
     134          //TODO maybe save comment too.
     135          continue;
     136        } else {
     137          throw new ArgumentException("Error at your constraints definition constraints have to start with Target: | d | \u2202 | #");
     138        }
     139      }
     140    }
     141
    48142    private static double ParseIntervalBounds(string input) {
    49143      input = input.ToLower();
     
    58152              return value;
    59153            } else {
    60               throw new ArgumentException("The given Interval in the constraint is not valid.");
     154              throw new ArgumentException("The given boundary is not a double value!");
    61155            }
    62156          }
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs

    r16759 r16773  
    165165      var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
    166166      Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
    167 
     167     
    168168      NamedIntervals namedIntervals = new NamedIntervals();
    169169      foreach (var variable in variables) {
     
    172172        namedIntervals.Add(variable.Value, variableInterval);
    173173      }
    174 
     174     
    175175      var commentMessage = "#Example of a target variable constraint" + Environment.NewLine +
    176176                           "#F1: TargetVariable" + Environment.NewLine +
     
    181181                           "#double: upperbound of the interval" + Environment.NewLine +
    182182                           "#bracket: open or closed regarding to open or closed interval definition" + Environment.NewLine +
    183                            "#F1 in [0 .. 1.2]" + Environment.NewLine + Environment.NewLine +
     183                           "#Y2 in [0 .. 1.2]" + Environment.NewLine + Environment.NewLine +
    184184                           "#Constraints on model parameters:" + Environment.NewLine +
    185185                           "#derivation symbol: d or ∂" + Environment.NewLine +
    186186                           "#derivation in leibniz notation" + Environment.NewLine +
    187187                           "#in and interval syntax is the same like above" + Environment.NewLine +
    188                            "#∂F2/∂X1 in [0 .. inf.[" + Environment.NewLine +
    189                            "#∂²F2/∂X1² in [0 .. inf.[" + Environment.NewLine +
    190                            "#∂F2/∂X9 in [-100 .. 0]" + Environment.NewLine +
    191                            "#∂²F2/∂X9² in [0 .. inf.[";
     188                           "#∂Y2/∂X1 in [0 .. inf.[" + Environment.NewLine +
     189                           "#∂²Y2/∂X1² in [0 .. inf.[" + Environment.NewLine +
     190                           "#∂Y2/∂X9 in [-100 .. 0]" + Environment.NewLine +
     191                           "#∂²Y2/∂X9² in [0 .. inf.[";
    192192
    193193      Parameters.Add(new FixedValueParameter<NamedIntervals>(VariableRangesParameterName, namedIntervals));
    194       Parameters.Add(new FixedValueParameter<ParsedConstraint>(IntervalConstraintsParameterName, new ParsedConstraint(commentMessage)));
     194      Parameters.Add(new FixedValueParameter<ParsedConstraint>(IntervalConstraintsParameterName, new ParsedConstraint(commentMessage, targetVariable, allowedInputVariables)));
    195195      RegisterParameterEvents();
    196196    }
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/ParsedConstraint.cs

    r16772 r16773  
    55using System.Text;
    66using System.Threading.Tasks;
     7using System.Windows.Markup;
    78using HeuristicLab.Common;
    89using HeuristicLab.Core;
    910using HeuristicLab.Data;
     11using HeuristicLab.Parameters;
    1012
    1113namespace HeuristicLab.Problems.DataAnalysis {
     
    2628    }
    2729
     30    protected string targetVariable;
     31    public string TargetVariable {
     32      get => targetVariable;
     33      set => targetVariable = value;
     34    }
     35
     36    protected IEnumerable<string> allowedVariables;
     37
     38    public IEnumerable<string> AllowedVariables {
     39      get => allowedVariables;
     40      set => allowedVariables = value;
     41    }
     42
    2843    protected ParsedConstraint(ParsedConstraint original, Cloner cloner)
    2944      : base(original, cloner) {
    3045      this.input = original.Input ?? string.Empty;
    3146      this.constraints = original.Constraints ?? new ItemList<IntervalConstraint>();
     47     
    3248    }
    3349
     
    4157    }
    4258
    43     public ParsedConstraint(string input) {
     59    public ParsedConstraint(string input, string targetVariable, IEnumerable<string> allowedVariables) {
    4460      this.input = input;
    4561      this.constraints = new ItemList<IntervalConstraint>();
     62      this.targetVariable = targetVariable;
     63      this.allowedVariables = allowedVariables;
    4664    }
    4765
Note: See TracChangeset for help on using the changeset viewer.