Changeset 17995 for branches/3119_AdditionalShapeConstraintFeatures/HeuristicLab.Problems.DataAnalysis
- Timestamp:
- 06/22/21 18:28:36 (3 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
r17946 r17995 107 107 108 108 [Storable] 109 private Interval threshold ;109 private Interval threshold = new Interval(0, 0); 110 110 public Interval Threshold { 111 111 get => threshold; … … 114 114 return; 115 115 threshold = value; 116 OnToStringChanged(); 117 OnChanged(); 118 } 119 } 120 121 122 [Storable] 123 private Interval targetInterval; 124 public Interval TargetInterval { 125 get => targetInterval; 126 set { 127 if (targetInterval == value) 128 return; 129 targetInterval = value; 130 OnToStringChanged(); 131 OnChanged(); 132 } 133 } 134 135 [Storable] 136 private Interval dynInterval = new Interval(double.NegativeInfinity, double.PositiveInfinity); 137 public Interval DynInterval { 138 get => dynInterval; 139 set { 140 if (dynInterval == value) 141 return; 142 dynInterval = value; 116 143 OnToStringChanged(); 117 144 OnChanged(); … … 125 152 private void AfterDeserialization() { 126 153 if (regions != null) regions.Changed += regions_Changed; 154 if (TargetInterval == null) 155 TargetInterval = new Interval(interval.LowerBound, interval.UpperBound); 127 156 } 128 157 129 158 // without derivation 130 public ShapeConstraint(Interval interval, double weight, Interval threshold )159 public ShapeConstraint(Interval interval, double weight, Interval threshold, Interval dynInterval) 131 160 : this(string.Empty, 0, 132 interval, new IntervalCollection(), weight, threshold ) { }133 134 public ShapeConstraint(Interval interval, IntervalCollection regions, double weight, Interval threshold )161 interval, new IntervalCollection(), weight, threshold, dynInterval) { } 162 163 public ShapeConstraint(Interval interval, IntervalCollection regions, double weight, Interval threshold, Interval dynInterval) 135 164 : this(string.Empty, 0, 136 interval, regions, weight, threshold ) { }165 interval, regions, weight, threshold, dynInterval) { } 137 166 138 167 public ShapeConstraint(string variable, int numberOfDerivations, 139 Interval interval, double weight, Interval threshold )168 Interval interval, double weight, Interval threshold, Interval dynInterval) 140 169 : this(variable, numberOfDerivations, 141 interval, new IntervalCollection(), weight, threshold ) { }170 interval, new IntervalCollection(), weight, threshold, dynInterval) { } 142 171 143 172 public ShapeConstraint(string variable, int numberOfDerivations, 144 Interval interval, IntervalCollection regions, double weight, Interval threshold ) {173 Interval interval, IntervalCollection regions, double weight, Interval threshold, Interval dynInterval) { 145 174 Variable = variable; 146 175 NumberOfDerivations = numberOfDerivations; … … 149 178 Weight = weight; 150 179 Threshold = threshold; 180 DynInterval = dynInterval; 181 TargetInterval = new Interval(interval.LowerBound, interval.UpperBound); 151 182 } 152 183 … … 162 193 Regions = cloner.Clone(original.Regions); 163 194 Weight = original.weight; 164 } 165 195 Threshold = original.Threshold; 196 DynInterval = original.DynInterval; 197 TargetInterval = original.TargetInterval; 198 } 166 199 167 200 public event EventHandler Changed; … … 188 221 string write(double val) => double.IsPositiveInfinity(val) ? "inf." : double.IsNegativeInfinity(val) ? "-inf." : $"{val}"; 189 222 if (!IsDerivative) { 190 expression = string.Format($"f in [{write( Interval.LowerBound)} .. {write(Interval.UpperBound)}]");223 expression = string.Format($"f in [{write(TargetInterval.LowerBound)} .. {write(TargetInterval.UpperBound)}]"); 191 224 } else { 192 225 var derivationString = string.Empty; … … 199 232 derivationString = "³"; break; 200 233 } 201 expression = string.Format($"∂{derivationString}f/∂{Variable}{derivationString} in [{write( Interval.LowerBound)} .. {write(Interval.UpperBound)}]");234 expression = string.Format($"∂{derivationString}f/∂{Variable}{derivationString} in [{write(TargetInterval.LowerBound)} .. {write(TargetInterval.UpperBound)}]"); 202 235 } 203 236 … … 209 242 expression += $" weight: {weight}"; 210 243 } 244 211 245 if (!double.IsNegativeInfinity(Threshold.LowerBound) || !double.IsPositiveInfinity(Threshold.UpperBound)) 212 expression += $" threshold: [{write(Threshold.LowerBound)} .. {write(Threshold.UpperBound)}]"; 246 expression += $" threshold in [{write(Threshold.LowerBound)} .. {write(Threshold.UpperBound)}]"; 247 248 if (!double.IsNegativeInfinity(DynInterval.LowerBound) && !double.IsPositiveInfinity(DynInterval.UpperBound)) 249 expression += $" start in [{write(DynInterval.LowerBound)} .. {write(DynInterval.UpperBound)}]"; 213 250 214 251 return expression; -
branches/3119_AdditionalShapeConstraintFeatures/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/ShapeConstraintsParser.cs
r17946 r17995 76 76 private const string weightRegex = @"\s*(weight:\s*(?<weight>\S*))?"; 77 77 private const string thresholdRegex = @"\s*(threshold\s*in\s*(?<threshold> "+ intervalRegex + @"))?"; 78 private const string dynIntervalStartRegex = @"\s*(start\s*in\s*(?<dynInterval> " + intervalRegex + @"))?"; 78 79 public static ShapeConstraint ParseFunctionRangeConstraint(string expr) { 79 80 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] weight: 2.0 91 92 // df / d'x' in [0..10], 'x' in [1 .. 3] 92 // df / d'x' in [0..10], 'x' in [1 .. 3], yin [10..30] weight: 1.293 // df / d'x' in [0..10], 'x' in [1 .. 3], yin [10..30] weight: 1.2 threshold in [-10 .. 10]93 // df / d'x' in [0..10], 'x' in [1 .. 3], 'y' in [10..30] weight: 1.2 94 // df / d'x' in [0..10], 'x' in [1 .. 3], 'y' in [10..30] weight: 1.2 threshold in [-10 .. 10] 94 95 var match = Regex.Match(targetConstraint, 95 96 @"\s*\bin\b" + … … 101 102 @")*" + 102 103 weightRegex + 103 thresholdRegex 104 thresholdRegex + 105 dynIntervalStartRegex 104 106 ); 105 107 … … 112 114 var interval = new Interval(lowerBound, upperBound); 113 115 var weight = 1.0; 114 var threshold = new Interval(double.NegativeInfinity, double.PositiveInfinity); 116 var threshold = new Interval(0, 0); 117 Interval dynInterval = new Interval(double.NegativeInfinity, double.PositiveInfinity); 118 int intervalIdx = 1; 119 var lowerboundCount = match.Groups["lowerBound"].Captures.Count; 120 var upperboundCount = match.Groups["upperBound"].Captures.Count; 115 121 116 122 if (match.Groups["weight"].Success && !string.IsNullOrWhiteSpace(match.Groups["weight"].Value)) 117 123 weight = ParseAndValidateDouble(match.Groups["weight"].Value); 118 124 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); 125 if (match.Groups["dynInterval"].Success) { 126 var dynIntervalLb = ParseIntervalBounds(match.Groups["lowerBound"].Captures[lowerboundCount - intervalIdx].Value); 127 var dynIntervalUb = ParseIntervalBounds(match.Groups["upperBound"].Captures[upperboundCount - intervalIdx].Value); 128 intervalIdx++; 129 dynInterval = new Interval(dynIntervalLb, dynIntervalUb); 130 } 131 132 if (match.Groups["threshold"].Success) { 133 var thresholdLb = ParseIntervalBounds(match.Groups["lowerBound"].Captures[lowerboundCount - intervalIdx].Value); 134 var thresholdUb = ParseIntervalBounds(match.Groups["upperBound"].Captures[upperboundCount - intervalIdx].Value); 135 intervalIdx++; 124 136 threshold = new Interval(thresholdLb, thresholdUb); 125 137 } 138 126 139 127 140 if (match.Groups["varName"].Success) { … … 138 151 throw new ArgumentException($"The constraint {expr} has multiple regions of the same variable."); 139 152 } 140 return new ShapeConstraint(interval, regions, weight, threshold );153 return new ShapeConstraint(interval, regions, weight, threshold, dynInterval); 141 154 } else 142 return new ShapeConstraint(interval, weight, threshold );155 return new ShapeConstraint(interval, weight, threshold, dynInterval); 143 156 } else 144 157 throw new ArgumentException($"The target constraint {expr} is not valid."); … … 160 173 @")*" + 161 174 weightRegex + 162 thresholdRegex 175 thresholdRegex + 176 dynIntervalStartRegex 163 177 ); 164 178 … … 184 198 var interval = new Interval(lowerBound, upperBound); 185 199 var weight = 1.0; 186 var threshold = new Interval(double.NegativeInfinity, double.PositiveInfinity); 200 var threshold = new Interval(0, 0); 201 Interval dynInterval = new Interval(double.NegativeInfinity, double.PositiveInfinity); 202 int intervalIdx = 1; 203 var lowerboundCount = match.Groups["lowerBound"].Captures.Count; 204 var upperboundCount = match.Groups["upperBound"].Captures.Count; 187 205 188 206 if (match.Groups["weight"].Success && !string.IsNullOrWhiteSpace(match.Groups["weight"].Value)) 189 207 weight = ParseAndValidateDouble(match.Groups["weight"].Value); 190 208 209 if (match.Groups["dynInterval"].Success) { 210 var dynIntervalLb = ParseIntervalBounds(match.Groups["lowerBound"].Captures[lowerboundCount - intervalIdx].Value); 211 var dynIntervalUb = ParseIntervalBounds(match.Groups["upperBound"].Captures[upperboundCount - intervalIdx].Value); 212 intervalIdx++; 213 dynInterval = new Interval(dynIntervalLb, dynIntervalUb); 214 } 215 191 216 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); 217 var thresholdLb = ParseIntervalBounds(match.Groups["lowerBound"].Captures[lowerboundCount - intervalIdx].Value); 218 var thresholdUb = ParseIntervalBounds(match.Groups["upperBound"].Captures[upperboundCount - intervalIdx].Value); 219 intervalIdx++; 196 220 threshold = new Interval(thresholdLb, thresholdUb); 197 221 } … … 210 234 throw new ArgumentException($"The constraint {expr} has multiple regions of the same variable."); 211 235 } 212 return new ShapeConstraint(variable, numberOfDerivation, interval, regions, weight, threshold );236 return new ShapeConstraint(variable, numberOfDerivation, interval, regions, weight, threshold, dynInterval); 213 237 } else 214 return new ShapeConstraint(variable, numberOfDerivation, interval, weight, threshold );238 return new ShapeConstraint(variable, numberOfDerivation, interval, weight, threshold, dynInterval); 215 239 } else 216 240 throw new ArgumentException($"The derivation constraint {expr} is not valid.");
Note: See TracChangeset
for help on using the changeset viewer.