Changeset 16773
- Timestamp:
- 04/09/19 14:58:24 (6 years ago)
- 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 85 85 } 86 86 87 static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model,87 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model, 88 88 IRegressionProblemData problemData) { 89 //var constraints = IntervalConstraintsParser.Parse(((RegressionProblemData)problemData).IntervalConstraints.Value);90 89 var constraints = ((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Constraints; 91 90 var constraintViolations = new Dictionary<IntervalConstraint, bool>(); … … 101 100 } 102 101 103 static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model,102 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model, 104 103 IRegressionProblemData problemData) { 105 104 return CheckConstraints(model.SymbolicExpressionTree, problemData); 106 105 } 107 106 108 static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges,107 public static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges, 109 108 IntervalConstraint constraint) { 110 109 var intervalInterpreter = new IntervalInterpreter(); … … 148 147 var constraintViolations = new Dictionary<string, int>(); 149 148 150 //var constraints = IntervalConstraintsParser.Parse(problemData.IntervalConstraints.Value);151 149 var constraints = problemData.IntervalConstraintsParameter.Value.Constraints; 152 150 -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Views/3.4/ParsedConstraintView.cs
r16772 r16773 1 1 using 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 10 3 using HeuristicLab.Core; 11 4 using HeuristicLab.MainForm; … … 21 14 set => base.Content = value; 22 15 } 16 23 17 public ParsedConstraintView() { 24 18 InitializeComponent(); … … 38 32 39 33 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)); 41 35 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;54 36 } 55 37 -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs
r16772 r16773 11 11 [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")] 12 12 [Item("Interval Constraint", "Constraint on intervals.")] 13 public class IntervalConstraint : Item {13 public class IntervalConstraint : NamedItem { 14 14 public string Expression { get; set; } 15 15 public string Definition { get; set; } … … 23 23 public IntervalConstraint() {} 24 24 25 public IntervalConstraint(string name) { 26 base.name = name; 27 } 28 25 29 public IntervalConstraint(string expression, string definition, Interval interval, bool inclusiveLowerBound, 26 30 bool inclusiveUpperBound, bool isDerivation, string variable, int numberOfDerivation) { 31 base.name = expression; 27 32 Expression = expression; 28 33 Definition = definition; -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs
r16732 r16773 2 2 using System.Collections.Generic; 3 3 using System.Globalization; 4 using System.Linq; 4 5 using System.Text.RegularExpressions; 5 6 … … 46 47 } 47 48 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 48 142 private static double ParseIntervalBounds(string input) { 49 143 input = input.ToLower(); … … 58 152 return value; 59 153 } 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!"); 61 155 } 62 156 } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs
r16759 r16773 165 165 var variables = InputVariables.Select(x => x.AsReadOnly()).ToList(); 166 166 Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First())); 167 167 168 168 NamedIntervals namedIntervals = new NamedIntervals(); 169 169 foreach (var variable in variables) { … … 172 172 namedIntervals.Add(variable.Value, variableInterval); 173 173 } 174 174 175 175 var commentMessage = "#Example of a target variable constraint" + Environment.NewLine + 176 176 "#F1: TargetVariable" + Environment.NewLine + … … 181 181 "#double: upperbound of the interval" + Environment.NewLine + 182 182 "#bracket: open or closed regarding to open or closed interval definition" + Environment.NewLine + 183 "# F1in [0 .. 1.2]" + Environment.NewLine + Environment.NewLine +183 "#Y2 in [0 .. 1.2]" + Environment.NewLine + Environment.NewLine + 184 184 "#Constraints on model parameters:" + Environment.NewLine + 185 185 "#derivation symbol: d or ∂" + Environment.NewLine + 186 186 "#derivation in leibniz notation" + Environment.NewLine + 187 187 "#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.["; 192 192 193 193 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))); 195 195 RegisterParameterEvents(); 196 196 } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/ParsedConstraint.cs
r16772 r16773 5 5 using System.Text; 6 6 using System.Threading.Tasks; 7 using System.Windows.Markup; 7 8 using HeuristicLab.Common; 8 9 using HeuristicLab.Core; 9 10 using HeuristicLab.Data; 11 using HeuristicLab.Parameters; 10 12 11 13 namespace HeuristicLab.Problems.DataAnalysis { … … 26 28 } 27 29 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 28 43 protected ParsedConstraint(ParsedConstraint original, Cloner cloner) 29 44 : base(original, cloner) { 30 45 this.input = original.Input ?? string.Empty; 31 46 this.constraints = original.Constraints ?? new ItemList<IntervalConstraint>(); 47 32 48 } 33 49 … … 41 57 } 42 58 43 public ParsedConstraint(string input ) {59 public ParsedConstraint(string input, string targetVariable, IEnumerable<string> allowedVariables) { 44 60 this.input = input; 45 61 this.constraints = new ItemList<IntervalConstraint>(); 62 this.targetVariable = targetVariable; 63 this.allowedVariables = allowedVariables; 46 64 } 47 65
Note: See TracChangeset
for help on using the changeset viewer.