Changeset 17946 for branches/3119_AdditionalShapeConstraintFeatures
- Timestamp:
- 04/16/21 16:04:02 (4 years ago)
- Location:
- branches/3119_AdditionalShapeConstraintFeatures/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3119_AdditionalShapeConstraintFeatures/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/ShapeConstraint.cs
r17937 r17946 106 106 } 107 107 108 [Storable] 109 private Interval threshold; 110 public Interval Threshold { 111 get => threshold; 112 set { 113 if (threshold == value) 114 return; 115 threshold = value; 116 OnToStringChanged(); 117 OnChanged(); 118 } 119 } 120 108 121 [StorableConstructor] 109 122 private ShapeConstraint(StorableConstructorFlag _) : base(_) { } … … 115 128 116 129 // without derivation 117 public ShapeConstraint(Interval interval, double weight )130 public ShapeConstraint(Interval interval, double weight, Interval threshold) 118 131 : this(string.Empty, 0, 119 interval, new IntervalCollection(), weight ) { }120 121 public ShapeConstraint(Interval interval, IntervalCollection regions, double weight )132 interval, new IntervalCollection(), weight, threshold) { } 133 134 public ShapeConstraint(Interval interval, IntervalCollection regions, double weight, Interval threshold) 122 135 : this(string.Empty, 0, 123 interval, regions, weight ) { }136 interval, regions, weight, threshold) { } 124 137 125 138 public ShapeConstraint(string variable, int numberOfDerivations, 126 Interval interval, double weight )139 Interval interval, double weight, Interval threshold) 127 140 : this(variable, numberOfDerivations, 128 interval, new IntervalCollection(), weight ) { }141 interval, new IntervalCollection(), weight, threshold) { } 129 142 130 143 public ShapeConstraint(string variable, int numberOfDerivations, 131 Interval interval, IntervalCollection regions, double weight ) {144 Interval interval, IntervalCollection regions, double weight, Interval threshold) { 132 145 Variable = variable; 133 146 NumberOfDerivations = numberOfDerivations; … … 135 148 Regions = regions; 136 149 Weight = weight; 150 Threshold = threshold; 137 151 } 138 152 … … 175 189 if (!IsDerivative) { 176 190 expression = string.Format($"f in [{write(Interval.LowerBound)} .. {write(Interval.UpperBound)}]"); 177 if (Regions != null) { 178 foreach (var region in Regions.GetReadonlyDictionary()) 179 expression += $", {region.Key} in [{write(region.Value.LowerBound)} .. {write(region.Value.UpperBound)}]"; 191 } else { 192 var derivationString = string.Empty; 193 switch (numberOfDerivations) { 194 case 1: 195 derivationString = ""; break; 196 case 2: 197 derivationString = "²"; break; 198 case 3: 199 derivationString = "³"; break; 180 200 } 181 if (Weight != 1.0) { 182 expression += $" weight: {weight}"; 183 } 184 185 return expression; 186 } 187 188 var derivationString = string.Empty; 189 switch (numberOfDerivations) { 190 case 1: 191 derivationString = ""; break; 192 case 2: 193 derivationString = "²"; break; 194 case 3: 195 derivationString = "³"; break; 196 } 197 expression = string.Format($"∂{derivationString}f/∂{Variable}{derivationString} in [{write(Interval.LowerBound)} .. {write(Interval.UpperBound)}]"); 201 expression = string.Format($"∂{derivationString}f/∂{Variable}{derivationString} in [{write(Interval.LowerBound)} .. {write(Interval.UpperBound)}]"); 202 } 203 198 204 if (Regions != null) { 199 205 foreach (var region in Regions.GetReadonlyDictionary()) … … 203 209 expression += $" weight: {weight}"; 204 210 } 211 if (!double.IsNegativeInfinity(Threshold.LowerBound) || !double.IsPositiveInfinity(Threshold.UpperBound)) 212 expression += $" threshold: [{write(Threshold.LowerBound)} .. {write(Threshold.UpperBound)}]"; 213 205 214 return expression; 206 215 } -
branches/3119_AdditionalShapeConstraintFeatures/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/ShapeConstraintsParser.cs
r17911 r17946 75 75 private const string variableRegex = @"(['](?<varName>.*)[']|(?<varName>[^\s²³]+))\s*"; 76 76 private const string weightRegex = @"\s*(weight:\s*(?<weight>\S*))?"; 77 private const string thresholdRegex = @"\s*(threshold\s*in\s*(?<threshold> "+ intervalRegex + @"))?"; 77 78 public static ShapeConstraint ParseFunctionRangeConstraint(string expr) { 78 79 if (!expr.StartsWith("f")) throw new ArgumentException($"Invalid function range constraint {expr} (e.g. f in [1..2])"); … … 90 91 // df / d'x' in [0..10], 'x' in [1 .. 3] 91 92 // df / d'x' in [0..10], 'x' in [1 .. 3], y in [10..30] weight: 1.2 92 93 // df / d'x' in [0..10], 'x' in [1 .. 3], y in [10..30] weight: 1.2 threshold in [-10 .. 10] 93 94 var match = Regex.Match(targetConstraint, 94 95 @"\s*\bin\b" + … … 99 100 intervalRegex + 100 101 @")*" + 101 weightRegex 102 weightRegex + 103 thresholdRegex 102 104 ); 103 105 … … 110 112 var interval = new Interval(lowerBound, upperBound); 111 113 var weight = 1.0; 114 var threshold = new Interval(double.NegativeInfinity, double.PositiveInfinity); 112 115 113 116 if (match.Groups["weight"].Success && !string.IsNullOrWhiteSpace(match.Groups["weight"].Value)) 114 117 weight = ParseAndValidateDouble(match.Groups["weight"].Value); 118 119 if(match.Groups["threshold"].Success) { 120 var lowerboundCount = match.Groups["lowerBound"].Captures.Count; 121 var upperboundCount = match.Groups["upperBound"].Captures.Count; 122 var thresholdLb = ParseIntervalBounds(match.Groups["lowerBound"].Captures[lowerboundCount - 1].Value); 123 var thresholdUb = ParseIntervalBounds(match.Groups["upperBound"].Captures[upperboundCount - 1].Value); 124 threshold = new Interval(thresholdLb, thresholdUb); 125 } 115 126 116 127 if (match.Groups["varName"].Success) { … … 127 138 throw new ArgumentException($"The constraint {expr} has multiple regions of the same variable."); 128 139 } 129 return new ShapeConstraint(interval, regions, weight );140 return new ShapeConstraint(interval, regions, weight, threshold); 130 141 } else 131 return new ShapeConstraint(interval, weight );142 return new ShapeConstraint(interval, weight, threshold); 132 143 } else 133 144 throw new ArgumentException($"The target constraint {expr} is not valid."); … … 148 159 intervalRegex + 149 160 @")*" + 150 weightRegex 161 weightRegex + 162 thresholdRegex 151 163 ); 152 164 … … 172 184 var interval = new Interval(lowerBound, upperBound); 173 185 var weight = 1.0; 186 var threshold = new Interval(double.NegativeInfinity, double.PositiveInfinity); 174 187 175 188 if (match.Groups["weight"].Success && !string.IsNullOrWhiteSpace(match.Groups["weight"].Value)) 176 189 weight = ParseAndValidateDouble(match.Groups["weight"].Value); 190 191 if (match.Groups["threshold"].Success) { 192 var lowerboundCount = match.Groups["lowerBound"].Captures.Count; 193 var upperboundCount = match.Groups["upperBound"].Captures.Count; 194 var thresholdLb = ParseIntervalBounds(match.Groups["lowerBound"].Captures[lowerboundCount - 1].Value); 195 var thresholdUb = ParseIntervalBounds(match.Groups["upperBound"].Captures[upperboundCount - 1].Value); 196 threshold = new Interval(thresholdLb, thresholdUb); 197 } 177 198 178 199 if (match.Groups["varName"].Captures.Count > 1) { … … 189 210 throw new ArgumentException($"The constraint {expr} has multiple regions of the same variable."); 190 211 } 191 return new ShapeConstraint(variable, numberOfDerivation, interval, regions, weight );212 return new ShapeConstraint(variable, numberOfDerivation, interval, regions, weight, threshold); 192 213 } else 193 return new ShapeConstraint(variable, numberOfDerivation, interval, weight );214 return new ShapeConstraint(variable, numberOfDerivation, interval, weight, threshold); 194 215 } else 195 216 throw new ArgumentException($"The derivation constraint {expr} is not valid.");
Note: See TracChangeset
for help on using the changeset viewer.