- Timestamp:
- 03/12/21 14:00:36 (4 years ago)
- Location:
- branches/3073_IA_constraint_splitting_reintegration
- Files:
-
- 8 added
- 3 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/IntervalArithmeticGrammar.cs
r17880 r17887 35 35 var cos = new Cosine(); 36 36 var tan = new Tangent(); 37 var tanh = new HyperbolicTangent(); 37 38 var log = new Logarithm(); 38 var pow = new Power();39 var exp = new Exponential(); 39 40 var square = new Square(); 40 var root = new Root();41 41 var sqrt = new SquareRoot(); 42 var exp = new Exponential(); 43 var tanh = new HyperbolicTangent(); 42 var cube = new Cube(); 43 var cbrt = new CubeRoot(); 44 var abs = new Absolute(); 45 var aq = new AnalyticQuotient(); 44 46 var constant = new Constant(); 45 var aq = new AnalyticQuotient();46 47 constant.MinValue = -20; 47 48 constant.MaxValue = 20; … … 51 52 52 53 //Special symbols 53 var offset = new Addition { Name = "Offset"};54 var scaling = new Multiplication { Name = "Scaling"};54 var offset = new Addition { Name = "Offset" }; 55 var scaling = new Multiplication { Name = "Scaling" }; 55 56 //all other symbols 56 57 var allSymbols = new List<Symbol> { 57 add, sub, mul, div, constant, variableSymbol, sin, cos, tan, log, pow, square, root, sqrt, exp,58 tanh, aq 58 add, sub, mul, div, constant, variableSymbol, sin, cos, tan, log, square, sqrt, cube, cbrt, exp, 59 tanh, aq, abs 59 60 }; 60 61 61 var arithmeticSymbols = new List<Symbol> {add, sub, mul, div}; 62 var trigonometricSymbols = new List<Symbol> {sin, cos, tan, tanh}; 63 var exponentialAndLogarithmicSymbols = new List<Symbol> {exp, log}; 64 var powerSymbols = new List<Symbol> {square, pow, sqrt, root}; 65 var specialSymbols = new List<Symbol> {aq}; 66 var realValueSymbols = new List<Symbol> 67 {add, sub, mul, div, sin, cos, tan, tanh, exp, log, variableSymbol, constant, aq}; 62 var bivariateFuncs = new List<Symbol> { add, sub, mul, div, aq }; 63 var univariateFuncs = new List<Symbol> { sin, cos, tan, tanh, exp, log, abs, square, cube, sqrt, cbrt }; 64 var realValueSymbols = new List<Symbol> { 65 add, sub, mul, div, sin, cos, tan, tanh, exp, log, aq, abs, square, cube, sqrt, cbrt, 66 variableSymbol, constant, 67 }; 68 68 69 69 … … 76 76 #region define subtree count for special symbols 77 77 78 foreach (var symb in arithmeticSymbols) SetSubtreeCount(symb, 2, 2);78 foreach (var symb in bivariateFuncs) SetSubtreeCount(symb, 2, 2); 79 79 80 foreach (var symb in trigonometricSymbols) SetSubtreeCount(symb, 1, 1); 81 82 foreach (var symb in exponentialAndLogarithmicSymbols) SetSubtreeCount(symb, 1, 1); 80 foreach (var symb in univariateFuncs) SetSubtreeCount(symb, 1, 1); 83 81 84 82 SetSubtreeCount(offset, 2, 2); 85 83 SetSubtreeCount(scaling, 2, 2); 86 SetSubtreeCount(pow, 2, 2);87 SetSubtreeCount(root, 2, 2);88 SetSubtreeCount(square, 1, 1);89 SetSubtreeCount(sqrt, 1, 1);90 SetSubtreeCount(aq, 2, 2);91 84 92 85 #endregion … … 109 102 } 110 103 111 //define childs for power symbols112 foreach (var symb in powerSymbols) {113 AddAllowedChildSymbol(symb, variableSymbol, 0);114 AddAllowedChildSymbol(symb, constant, 1);115 }116 117 //define child for sqrt and square118 foreach (var c in realValueSymbols) {119 AddAllowedChildSymbol(square, c, 0);120 AddAllowedChildSymbol(sqrt, c, 0);121 }122 123 //define child for aq124 foreach (var c in realValueSymbols) {125 AddAllowedChildSymbol(aq, c, 0);126 AddAllowedChildSymbol(aq, c, 1);127 }128 129 104 #endregion 130 105 131 Symbols.First(s => s is Power).Enabled = false;132 Symbols.First(s => s is Root).Enabled = false;133 Symbols.First(s => s is A nalyticQuotient).Enabled = false;106 Symbols.First(s => s is Cube).Enabled = false; 107 Symbols.First(s => s is CubeRoot).Enabled = false; 108 Symbols.First(s => s is Absolute).Enabled = false; 134 109 } 135 110 } -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/IBoundsEstimator.cs
r17768 r17887 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 1 using System.Collections.Generic; 6 2 using HEAL.Attic; 7 3 using HeuristicLab.Core; 8 4 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 9 5 10 namespace HeuristicLab.Problems.DataAnalysis.Symbolic {6 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 11 7 [StorableType("C94A360D-5A9F-48A3-A6D3-CF920C77E50D")] 12 8 public interface IBoundsEstimator : INamedItem, IStatefulItem { 13 9 Interval GetModelBound(ISymbolicExpressionTree tree, IntervalCollection variableRanges); 14 10 15 IDictionary<ISymbolicExpressionTreeNode, Interval> GetModelNode sBounds(11 IDictionary<ISymbolicExpressionTreeNode, Interval> GetModelNodeBounds( 16 12 ISymbolicExpressionTree tree, IntervalCollection variableRanges); 17 13 14 // returns the size of the violation which is the distance to one of the bounds 18 15 double CheckConstraint( 19 ISymbolicExpressionTree tree, IntervalCollection variableRanges, IntervalConstraint constraint);16 ISymbolicExpressionTree tree, IntervalCollection variableRanges, ShapeConstraint constraint); 20 17 21 18 bool IsCompatible(ISymbolicExpressionTree tree); 22 19 23 20 int EvaluatedSolutions { get; set; } 24 21 } 25 22 } -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IABoundsEstimator.cs
r17880 r17887 461 461 } 462 462 463 public IDictionary<ISymbolicExpressionTreeNode, Interval> GetModelNode sBounds(463 public IDictionary<ISymbolicExpressionTreeNode, Interval> GetModelNodeBounds( 464 464 ISymbolicExpressionTree tree, IntervalCollection variableRanges) { 465 465 throw new NotImplementedException(); … … 467 467 468 468 public double CheckConstraint( 469 ISymbolicExpressionTree tree, IntervalCollection variableRanges, IntervalConstraint constraint) {469 ISymbolicExpressionTree tree, IntervalCollection variableRanges, ShapeConstraint constraint) { 470 470 var occuringVariableRanges = GetOccurringVariableRanges(tree, variableRanges); 471 471 var instructions = PrepareInterpreterState(tree, occuringVariableRanges); -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IACompiledExpressionBoundsEstimator.cs
r17772 r17887 104 104 105 105 106 public double CheckConstraint(ISymbolicExpressionTree tree, IntervalCollection variableRanges, IntervalConstraint constraint) {106 public double CheckConstraint(ISymbolicExpressionTree tree, IntervalCollection variableRanges, ShapeConstraint constraint) { 107 107 if (!UseIntervalSplitting) { 108 108 var modelBound = GetModelBound(tree, variableRanges); … … 166 166 } 167 167 168 public IDictionary<ISymbolicExpressionTreeNode, Interval> GetModelNode sBounds(ISymbolicExpressionTree tree, IntervalCollection variableRanges) {168 public IDictionary<ISymbolicExpressionTreeNode, Interval> GetModelNodeBounds(ISymbolicExpressionTree tree, IntervalCollection variableRanges) { 169 169 throw new NotSupportedException("Model nodes bounds are not supported."); 170 170 } -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs
r17760 r17887 24 24 using System; 25 25 using System.Collections.Generic; 26 using System.Collections.ObjectModel;27 26 using System.Linq; 28 27 using HEAL.Attic; … … 38 37 public sealed class IntervalInterpreter : ParameterizedNamedItem, IStatefulItem { 39 38 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; 40 private const string MinSplittingWidthParameterName = "MinSplittingWidth";41 private const string MaxSplittingDepthParameterName = "MaxSplittingDepth";42 private const string UseIntervalSplittingParameterName = "UseIntervalSplitting";43 44 39 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter => 45 40 (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; 46 47 public IFixedValueParameter<IntValue> MinSplittingWithParameter =>48 (IFixedValueParameter<IntValue>)Parameters[MinSplittingWidthParameterName];49 50 public IFixedValueParameter<IntValue> MaxSplittingDepthParameter =>51 (IFixedValueParameter<IntValue>)Parameters[MaxSplittingDepthParameterName];52 53 public IFixedValueParameter<BoolValue> UseIntervalSplittingParameter =>54 (IFixedValueParameter<BoolValue>)Parameters[UseIntervalSplittingParameterName];55 56 public int MinSplittingWidth {57 get => MinSplittingWithParameter.Value.Value;58 set => MinSplittingWithParameter.Value.Value = value;59 }60 61 public int MaxSplittingDepth {62 get => MaxSplittingDepthParameter.Value.Value;63 set => MaxSplittingDepthParameter.Value.Value = value;64 }65 66 public bool UseIntervalSplitting {67 get => UseIntervalSplittingParameter.Value.Value;68 set => UseIntervalSplittingParameter.Value.Value = value;69 }70 41 71 42 public int EvaluatedSolutions { … … 84 55 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, 85 56 "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 86 Parameters.Add(new FixedValueParameter<IntValue>(MinSplittingWidthParameterName,87 "Minimum interval width until splitting is stopped", new IntValue(0)));88 Parameters.Add(new FixedValueParameter<IntValue>(MaxSplittingDepthParameterName,89 "Maximum recursion depth of the splitting", new IntValue(5)));90 Parameters.Add(new FixedValueParameter<BoolValue>(UseIntervalSplittingParameterName, "", new BoolValue(false)));91 57 } 92 58 … … 131 97 Interval outputInterval; 132 98 133 if (UseIntervalSplitting) { 134 outputInterval = GetSymbolicExpressionTreeIntervals(tree, variableRanges, 135 out var _); 136 } else { 137 var instructionCount = 0; 138 var instructions = PrepareInterpreterState(tree, variableRanges); 139 outputInterval = Evaluate(instructions, ref instructionCount); 140 } 99 var instructionCount = 0; 100 var instructions = PrepareInterpreterState(tree, variableRanges); 101 outputInterval = Evaluate(instructions, ref instructionCount); 141 102 142 103 return outputInterval.LowerBound <= outputInterval.UpperBound … … 159 120 160 121 Interval outputInterval; 161 if (UseIntervalSplitting) { 162 //var variables = tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(x => x.VariableName).Distinct() 163 // .ToList(); 164 var containsDependencyProblem = ContainsVariableMultipleTimes(tree, out var variables); 165 166 if (containsDependencyProblem) { 167 var currIndex = 0; 168 var currDepth = 0; 169 IDictionary<string, Interval> writeableVariableRanges = 170 variableRanges.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); 171 //outputInterval = EvaluateRecursive(instructions, intervals, writeableVariableRanges, variables, MinSplittingWidth, MaxSplittingDepth, 172 // ref currIndex, ref currDepth, tree); 173 outputInterval = EvaluateWithSplitting(instructions, intervals, writeableVariableRanges, variables); 174 } else { 175 var instructionCount = 0; 176 outputInterval = Evaluate(instructions, ref instructionCount, intervals); 177 } 178 } else { 179 var instructionCount = 0; 180 outputInterval = Evaluate(instructions, ref instructionCount, intervals); 181 } 122 var instructionCount = 0; 123 outputInterval = Evaluate(instructions, ref instructionCount, intervals); 182 124 183 125 nodeIntervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); … … 219 161 } 220 162 221 public static Interval EvaluateWithSplitting(Instruction[] instructions, 222 IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, 223 IDictionary<string, Interval> variableIntervals, List<string> multipleOccurenceVariables) { 224 var savedIntervals = variableIntervals.ToDictionary(entry => entry.Key, entry => entry.Value); 225 var min = FindBound(instructions, nodeIntervals, variableIntervals, multipleOccurenceVariables, minimization: true); 226 var max = FindBound(instructions, nodeIntervals, savedIntervals, multipleOccurenceVariables, minimization: false); 227 228 return new Interval(min, max); 229 } 230 231 232 private static double FindBound(Instruction[] instructions, 233 IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, 234 IDictionary<string, Interval> variableIntervals, List<string> multipleOccurenceVariables, bool minimization = true) { 235 SortedSet<BoxBound> prioQ = new SortedSet<BoxBound>(); 236 237 var ic = 0; 238 //Calculate full box 239 IReadOnlyDictionary<string, Interval> readonlyRanges = variableIntervals.ToDictionary(k => k.Key, k => k.Value); 240 var interval = Evaluate(instructions, ref ic, nodeIntervals, readonlyRanges); 241 // the order of keys in a dictionary is guaranteed to be the same order as values in a dictionary 242 // https://docs.microsoft.com/en-us/dotnet/api/system.collections.idictionary.keys?view=netcore-3.1#remarks 243 //var box = variableIntervals.Values; 244 //Box only contains intervals from multiple occurence variables 245 var box = multipleOccurenceVariables.Select(k => variableIntervals[k]); 246 if (minimization) { 247 prioQ.Add(new BoxBound(box, interval.LowerBound)); 248 } else { 249 prioQ.Add(new BoxBound(box, -interval.UpperBound)); 250 } 251 252 // TODO a fixed limit for depth?! 253 for (var depth = 0; depth < 200; ++depth) { 254 var currentBound = prioQ.Min; 255 prioQ.Remove(currentBound); 256 257 var newBoxes = Split(currentBound.box); 258 259 foreach (var newBox in newBoxes) { 260 //var intervalEnum = newBox.GetEnumerator(); 261 //var keyEnum = readonlyRanges.Keys.GetEnumerator(); 262 //while (intervalEnum.MoveNext() & keyEnum.MoveNext()) { 263 // variableIntervals[keyEnum.Current] = intervalEnum.Current; 264 //} 265 //Set the splitted variables 266 var intervalEnum = newBox.GetEnumerator(); 267 foreach (var key in multipleOccurenceVariables) { 268 intervalEnum.MoveNext(); 269 variableIntervals[key] = intervalEnum.Current; 270 } 271 272 ic = 0; 273 var res = Evaluate(instructions, ref ic, nodeIntervals, 274 new ReadOnlyDictionary<string, Interval>(variableIntervals)); 275 if (minimization) { 276 prioQ.Add(new BoxBound(newBox, res.LowerBound)); 277 } else { 278 prioQ.Add(new BoxBound(newBox, -res.UpperBound)); 279 } 280 } 281 } 282 283 return minimization ? 284 prioQ.First().bound : 285 -prioQ.First().bound; 286 } 287 288 private static IEnumerable<IEnumerable<Interval>> Split(List<Interval> box) { 289 var boxes = box.Select(region => region.Split()).Select(split => new List<Interval> { split.Item1, split.Item2 }) 290 .ToList(); 291 292 return boxes.CartesianProduct(); 293 } 294 295 // a multi-dimensional box with an associated bound 296 // boxbounds are ordered first by bound (smaller first), then by size of box (larger first) then by distance of bottom left corner to origin 297 private class BoxBound : IComparable<BoxBound> { 298 public List<Interval> box; 299 public double bound; 300 public BoxBound(IEnumerable<Interval> box, double bound) { 301 this.box = new List<Interval>(box); 302 this.bound = bound; 303 } 304 public int CompareTo(BoxBound other) { 305 if (bound != other.bound) return bound.CompareTo(other.bound); 306 307 var thisSize = box.Aggregate(1.0, (current, dimExtent) => current * dimExtent.Width); 308 var otherSize = other.box.Aggregate(1.0, (current, dimExtent) => current * dimExtent.Width); 309 if (thisSize != otherSize) return -thisSize.CompareTo(otherSize); 310 311 var thisDist = box.Sum(dimExtent => dimExtent.LowerBound * dimExtent.LowerBound); 312 var otherDist = other.box.Sum(dimExtent => dimExtent.LowerBound * dimExtent.LowerBound); 313 if (thisDist != otherDist) return thisDist.CompareTo(otherDist); 314 315 // which is smaller first along the dimensions? 316 for (int i = 0; i < box.Count; i++) { 317 if (box[i].LowerBound != other.box[i].LowerBound) return box[i].LowerBound.CompareTo(other.box[i].LowerBound); 318 } 319 320 return 0; 321 } 322 } 323 324 public static Interval EvaluateRecursive( 325 Instruction[] instructions, 326 IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, 327 IDictionary<string, Interval> variableIntervals, IList<string> variables, 328 double minWidth, int maxDepth, ref int currIndex, ref int currDepth, 329 ISymbolicExpressionTree tree) { 330 Interval evaluate() { 331 var ic = 0; 332 IReadOnlyDictionary<string, Interval> readonlyRanges = 333 new ReadOnlyDictionary<string, Interval>(variableIntervals); 334 return Evaluate(instructions, ref ic, nodeIntervals, readonlyRanges); 335 } 336 337 Interval recurse(ref int idx, ref int depth) { 338 return EvaluateRecursive(instructions, nodeIntervals, variableIntervals, variables, minWidth, maxDepth, ref idx, 339 ref depth, tree); 340 } 341 342 343 var v = variables[currIndex]; 344 var x = variableIntervals[v]; 345 if (x.Width < minWidth || currDepth == maxDepth || !MultipleTimes(tree, v)) { 346 if (currIndex + 1 < variables.Count) { 347 currDepth = 0; 348 currIndex++; 349 var z = recurse(ref currIndex, ref currDepth); 350 currIndex--; 351 return z; 352 } 353 354 return evaluate(); 355 } 356 357 var t = x.Split(); 358 var xa = t.Item1; 359 var xb = t.Item2; 360 var d = currDepth; 361 currDepth = d + 1; 362 variableIntervals[v] = xa; 363 var ya = recurse(ref currIndex, ref currDepth); 364 currDepth = d + 1; 365 variableIntervals[v] = xb; 366 var yb = recurse(ref currIndex, ref currDepth); 367 variableIntervals[v] = x; // restore interval 368 return ya | yb; 369 } 370 163 // Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side 164 // Update instructionCounter, whenever Evaluate is called 371 165 public static Interval Evaluate( 372 166 Instruction[] instructions, ref int instructionCounter, 373 167 IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null, 374 168 IReadOnlyDictionary<string, Interval> variableIntervals = null) { 375 169 var currentInstr = instructions[instructionCounter]; 376 //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side 377 //Update instructionCounter, whenever Evaluate is called 170 378 171 instructionCounter++; 379 Interval result = null;172 Interval result; 380 173 381 174 switch (currentInstr.opCode) { 382 //Variables, Constants, ...383 175 case OpCodes.Variable: { 384 176 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 385 177 var weightInterval = new Interval(variableTreeNode.Weight, variableTreeNode.Weight); 386 //var variableInterval = (Interval)currentInstr.data;387 178 388 179 Interval variableInterval; … … 402 193 //Elementary arithmetic rules 403 194 case OpCodes.Add: { 404 //result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 405 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 406 for (var i = 1; i < currentInstr.nArguments; i++) { 407 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 195 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 196 for (var i = 1; i < currentInstr.nArguments; i++) { 408 197 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 409 198 result = Interval.Add(result, argumentInterval); … … 413 202 } 414 203 case OpCodes.Sub: { 415 //result = Evaluate(instructions, ref instructionCounter, nodeIntervals);416 204 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 417 205 if (currentInstr.nArguments == 1) … … 419 207 420 208 for (var i = 1; i < currentInstr.nArguments; i++) { 421 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);422 209 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 423 210 result = Interval.Subtract(result, argumentInterval); … … 427 214 } 428 215 case OpCodes.Mul: { 429 //result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 430 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 431 for (var i = 1; i < currentInstr.nArguments; i++) { 432 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 433 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 216 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 217 for (var i = 1; i < currentInstr.nArguments; i++) { 218 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 434 219 result = Interval.Multiply(result, argumentInterval); 435 220 } … … 438 223 } 439 224 case OpCodes.Div: { 440 //result = Evaluate(instructions, ref instructionCounter, nodeIntervals);441 225 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 442 226 if (currentInstr.nArguments == 1) … … 444 228 445 229 for (var i = 1; i < currentInstr.nArguments; i++) { 446 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);447 230 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 448 231 result = Interval.Divide(result, argumentInterval); … … 451 234 break; 452 235 } 453 //Trigonometric functions454 236 case OpCodes.Sin: { 455 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);456 237 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 457 238 result = Interval.Sine(argumentInterval); … … 459 240 } 460 241 case OpCodes.Cos: { 461 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);462 242 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 463 243 result = Interval.Cosine(argumentInterval); … … 465 245 } 466 246 case OpCodes.Tan: { 467 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);468 247 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 469 248 result = Interval.Tangens(argumentInterval); … … 471 250 } 472 251 case OpCodes.Tanh: { 473 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);474 252 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 475 253 result = Interval.HyperbolicTangent(argumentInterval); 476 254 break; 477 255 } 478 //Exponential functions479 256 case OpCodes.Log: { 480 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);481 257 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 482 258 result = Interval.Logarithm(argumentInterval); … … 484 260 } 485 261 case OpCodes.Exp: { 486 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);487 262 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 488 263 result = Interval.Exponential(argumentInterval); … … 490 265 } 491 266 case OpCodes.Square: { 492 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);493 267 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 494 268 result = Interval.Square(argumentInterval); … … 496 270 } 497 271 case OpCodes.SquareRoot: { 498 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);499 272 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 500 273 result = Interval.SquareRoot(argumentInterval); … … 502 275 } 503 276 case OpCodes.Cube: { 504 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);505 277 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 506 278 result = Interval.Cube(argumentInterval); … … 508 280 } 509 281 case OpCodes.CubeRoot: { 510 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);511 282 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 512 283 result = Interval.CubicRoot(argumentInterval); … … 514 285 } 515 286 case OpCodes.Absolute: { 516 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);517 287 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 518 288 result = Interval.Absolute(argumentInterval); … … 520 290 } 521 291 case OpCodes.AnalyticQuotient: { 522 //result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 523 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 524 for (var i = 1; i < currentInstr.nArguments; i++) { 525 //var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 292 result = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 293 for (var i = 1; i < currentInstr.nArguments; i++) { 526 294 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals, variableIntervals); 527 295 result = Interval.AnalyticalQuotient(result, argumentInterval); … … 538 306 539 307 return result; 540 }541 542 private static bool MultipleTimes(ISymbolicExpressionTree tree, string variable) {543 var varlist = tree.IterateNodesPrefix().OfType<VariableTreeNode>().GroupBy(x => x.VariableName);544 var group = varlist.Select(x => x.Key == variable).Count();545 546 return group > 1;547 }548 549 private static bool ContainsVariableMultipleTimes(ISymbolicExpressionTree tree, out List<String> variables) {550 variables = new List<string>();551 var varlist = tree.IterateNodesPrefix().OfType<VariableTreeNode>().GroupBy(x => x.VariableName);552 foreach (var group in varlist) {553 if (group.Count() > 1) {554 variables.Add(group.Key);555 }556 }557 558 return varlist.Any(group => group.Count() > 1);559 308 } 560 309 -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/IntervalUtil.cs
r17881 r17887 7 7 public static class IntervalUtil { 8 8 public static double IntervalConstraintViolation( 9 IntervalConstraint constraint, IBoundsEstimator estimator, IntervalCollection intervalCollection,9 ShapeConstraint constraint, IBoundsEstimator estimator, IntervalCollection intervalCollection, 10 10 ISymbolicExpressionTree solution) { 11 11 var variableRanges = intervalCollection.GetReadonlyDictionary(); … … 14 14 throw new ArgumentException( 15 15 $"The given variable {constraint.Variable} in the constraint does not exist in the model.", 16 nameof( IntervalConstraintsParser));16 nameof(ShapeConstraintsParser)); 17 17 } 18 18 … … 47 47 48 48 public static IEnumerable<double> IntervalConstraintsViolation( 49 IEnumerable< IntervalConstraint> constraints, IBoundsEstimator estimator, IntervalCollection intervalCollection,49 IEnumerable<ShapeConstraint> constraints, IBoundsEstimator estimator, IntervalCollection intervalCollection, 50 50 ISymbolicExpressionTree solution) { 51 51 return constraints.Select(constraint => IntervalConstraintViolation(constraint, estimator, intervalCollection, solution)).ToList(); -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Views/3.4/IntervalConstraintView.cs
r17607 r17887 30 30 namespace HeuristicLab.Problems.DataAnalysis.Views { 31 31 [View("Interval Constraint Detail View")] 32 [Content(typeof( IntervalConstraint), true)]32 [Content(typeof(ShapeConstraint), true)] 33 33 public sealed partial class IntervalConstraintView : ItemView { 34 public new IntervalConstraint Content {35 get => ( IntervalConstraint) base.Content;34 public new ShapeConstraint Content { 35 get => (ShapeConstraint) base.Content; 36 36 set => base.Content = value; 37 37 } -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Views/3.4/ProblemDataConstraintView.Designer.cs
r17607 r17887 58 58 this.errorOutput = new System.Windows.Forms.Label(); 59 59 this.label1 = new System.Windows.Forms.Label(); 60 this.intervalConstraintsView = new CheckedItemListView< IntervalConstraint>();60 this.intervalConstraintsView = new CheckedItemListView<ShapeConstraint>(); 61 61 ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); 62 62 this.splitContainer1.Panel1.SuspendLayout(); … … 183 183 private System.Windows.Forms.Button parseBtn; 184 184 private System.Windows.Forms.Label label1; 185 private HeuristicLab.Core.Views.CheckedItemListView< IntervalConstraint> intervalConstraintsView;185 private HeuristicLab.Core.Views.CheckedItemListView<ShapeConstraint> intervalConstraintsView; 186 186 private System.Windows.Forms.TextBox constraintsInput; 187 187 private Label errorOutput; -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis.Views/3.4/ProblemDataConstraintView.cs
r17607 r17887 32 32 namespace HeuristicLab.Problems.DataAnalysis.Views { 33 33 [View("ParsedConstraint View")] 34 [Content(typeof( ProblemDataConstraint), true)]34 [Content(typeof(ShapeConstraints), true)] 35 35 public partial class ProblemDataConstraintView : AsynchronousContentView { 36 private readonly CheckedItemList< IntervalConstraint>37 intervalConstraints = new CheckedItemList< IntervalConstraint>();36 private readonly CheckedItemList<ShapeConstraint> 37 intervalConstraints = new CheckedItemList<ShapeConstraint>(); 38 38 39 public new ProblemDataConstraintContent {40 get => ( ProblemDataConstraint) base.Content;39 public new ShapeConstraints Content { 40 get => (ShapeConstraints) base.Content; 41 41 set => base.Content = value; 42 42 } … … 73 73 74 74 75 private IEnumerable< IntervalConstraint> ParseConstraints(string input) {76 return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,75 private IEnumerable<ShapeConstraint> ParseConstraints(string input) { 76 return ShapeConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable, 77 77 Content.ProblemData.AllowedInputVariables); 78 78 } … … 90 90 } 91 91 catch (ArgumentException ex) { 92 Content.Constraints = new List< IntervalConstraint>();92 Content.Constraints = new List<ShapeConstraint>(); 93 93 Content.InfoText = ex.Message.Replace("Parameter name", "@Line"); 94 94 Content.InfoColor = Color.DarkRed; … … 117 117 118 118 private void constraint_Changed(object sender, EventArgs e) { 119 var constraint = ( IntervalConstraint) sender;119 var constraint = (ShapeConstraint) sender; 120 120 intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled); 121 121 } 122 122 123 123 private void constraints_Updated(object sender, 124 CollectionItemsChangedEventArgs<IndexedItem< IntervalConstraint>> e) {124 CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) { 125 125 foreach (var addedItem in e.Items) addedItem.Value.Changed += constraint_Changed; 126 126 } 127 127 128 private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem< IntervalConstraint>> e) {128 private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) { 129 129 foreach (var removedItem in e.Items) removedItem.Value.Changed -= constraint_Changed; 130 130 } 131 131 132 132 private void constraint_CheckedItemChanged(object sender, 133 CollectionItemsChangedEventArgs<IndexedItem< IntervalConstraint>> e) {134 ICheckedItemList< IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>) sender;133 CollectionItemsChangedEventArgs<IndexedItem<ShapeConstraint>> e) { 134 ICheckedItemList<ShapeConstraint> checkedItemList = (ICheckedItemList<ShapeConstraint>) sender; 135 135 foreach (var indexedItem in e.Items) indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value); 136 136 } -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj
r17768 r17887 190 190 <Compile Include="Implementation\Interval\Interval.cs" /> 191 191 <Compile Include="Implementation\Interval\IntervalCollection.cs" /> 192 <Compile Include="Implementation\Interval\ IntervalConstraint.cs" />193 <Compile Include="Implementation\Interval\ IntervalConstraintsParser.cs" />192 <Compile Include="Implementation\Interval\ShapeConstraint.cs" /> 193 <Compile Include="Implementation\Interval\ShapeConstraintsParser.cs" /> 194 194 <Compile Include="Implementation\Regression\ConfidenceBoundRegressionSolution.cs" /> 195 195 <Compile Include="Implementation\Regression\ConstantRegressionModel.cs" /> 196 196 <Compile Include="Implementation\Regression\ConstantRegressionSolution.cs" /> 197 <Compile Include="Implementation\Regression\ShapeConstraints.cs" /> 197 198 <Compile Include="Implementation\Regression\RegressionEnsembleProblemData.cs" /> 198 199 <Compile Include="Implementation\Regression\RegressionEnsembleModel.cs"> … … 306 307 <Compile Include="Interfaces\IDataAnalysisModel.cs" /> 307 308 <Compile Include="Interfaces\IDataAnalysisProblem.cs" /> 308 <Compile Include="ProblemDataConstraint.cs" />309 309 <Compile Include="Properties\AssemblyInfo.cs" /> 310 310 </ItemGroup> -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs
r17770 r17887 25 25 using HEAL.Attic; 26 26 using HeuristicLab.Common; 27 using HeuristicLab.Core; 27 28 28 29 namespace HeuristicLab.Problems.DataAnalysis { … … 80 81 81 82 return false; 82 }83 84 public Tuple<Interval, Interval> Split() {85 var midpoint = Width / 2;86 var left = new Interval(LowerBound, LowerBound + midpoint - double.Epsilon);87 var right = new Interval(LowerBound + midpoint, UpperBound);88 return Tuple.Create(left, right);89 }90 91 //Interval Intersection92 public static Interval operator &(Interval lhs, Interval rhs) {93 return new Interval(Math.Max(lhs.LowerBound, rhs.LowerBound), Math.Min(lhs.UpperBound, rhs.UpperBound));94 }95 96 //Interval Union97 public static Interval operator |(Interval lhs, Interval rhs) {98 return new Interval(Math.Min(lhs.LowerBound, rhs.LowerBound), Math.Max(lhs.UpperBound, rhs.UpperBound));99 83 } 100 84 -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalCollection.cs
r17631 r17887 34 34 get => HeuristicLab.Common.Resources.VSImageLibrary.Object; 35 35 } 36 private IDictionary<string, Interval> intervals { get; } = new Dictionary<string, Interval>();37 36 38 [Storable(Name = "StorableIntervalInformation")] 37 //[Storable] 38 private IDictionary<string, Interval> intervals { get; set; } = new Dictionary<string, Interval>(); 39 40 [Storable(OldName = "StorableIntervalInformation")] 39 41 private KeyValuePair<string, double[]>[] StorableIntervalInformation { 42 set { 43 foreach (var varInt in value) 44 intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1])); 45 } 46 } 47 48 [Storable] 49 private object[] StorableIntervals { 40 50 get { 41 var l = new List<KeyValuePair<string, double[]>>(); 42 foreach (var varInt in intervals) 51 var names = intervals.Keys.ToArray(); 52 var lowerBounds = intervals.Values.Select(i => i.LowerBound).ToArray(); 53 var upperBounds = intervals.Values.Select(i => i.UpperBound).ToArray(); 43 54 44 l.Add(new KeyValuePair<string, double[]>(varInt.Key, 45 new double[] { varInt.Value.LowerBound, varInt.Value.UpperBound })); 46 return l.ToArray(); 55 return new object[] { names, lowerBounds, upperBounds }; 47 56 } 48 57 49 58 set { 50 foreach (var varInt in value) 51 intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1])); 59 var names = (string[])value[0]; 60 var lowerBounds = (double[])value[1]; 61 var upperBounds = (double[])value[2]; 62 63 for (int i = 0; i < names.Length; i++) { 64 intervals.Add(names[i], new Interval(lowerBounds[i], upperBounds[i])); 65 } 52 66 } 53 67 } … … 80 94 public void SetInterval(string identifier, Interval interval) { 81 95 intervals[identifier] = interval; 96 RaiseChanged(); 82 97 } 83 98 84 99 public void AddInterval(string identifier, Interval interval) { 85 100 intervals.Add(identifier, interval); 101 RaiseChanged(); 86 102 } 87 103 88 104 public void DeleteInterval(string identifier) { 89 105 intervals.Remove(identifier); 106 RaiseChanged(); 90 107 } 91 108 … … 102 119 yield return Tuple.Create(variableInterval.Key, variableInterval.Value); 103 120 } 121 122 public event EventHandler Changed; 123 private void RaiseChanged() { 124 OnChanged(); 125 var handler = Changed; 126 if (handler != null) 127 handler(this, EventArgs.Empty); 128 } 129 130 protected virtual void OnChanged() { } 104 131 } 105 132 } -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs
r17607 r17887 94 94 problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>())); 95 95 problemData.Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, "", new IntervalCollection())); 96 problemData.Parameters.Add(new FixedValueParameter< ProblemDataConstraint>(IntervalConstraintsParameterName, "", new ProblemDataConstraint(problemData)));96 problemData.Parameters.Add(new FixedValueParameter<ShapeConstraints>(IntervalConstraintsParameterName, "", new ShapeConstraints())); 97 97 emptyProblemData = problemData; 98 98 } … … 109 109 } 110 110 111 public IFixedValueParameter< ProblemDataConstraint> IntervalConstraintsParameter =>112 (IFixedValueParameter< ProblemDataConstraint>) Parameters[IntervalConstraintsParameterName];113 114 public ProblemDataConstraintIntervalConstraints => IntervalConstraintsParameter.Value;111 public IFixedValueParameter<ShapeConstraints> IntervalConstraintsParameter => 112 (IFixedValueParameter<ShapeConstraints>) Parameters[IntervalConstraintsParameterName]; 113 114 public ShapeConstraints IntervalConstraints => IntervalConstraintsParameter.Value; 115 115 116 116 … … 148 148 149 149 if (!Parameters.ContainsKey(IntervalConstraintsParameterName)) { 150 Parameters.Add(new FixedValueParameter< ProblemDataConstraint>(IntervalConstraintsParameterName, new ProblemDataConstraint(this)));150 Parameters.Add(new FixedValueParameter<ShapeConstraints>(IntervalConstraintsParameterName, new ShapeConstraints())); 151 151 } 152 152 RegisterParameterEvents(); … … 173 173 } 174 174 175 public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable, IEnumerable<ITransformation> transformations = null) 175 public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable, 176 IEnumerable<ITransformation> transformations = null, 177 IntervalCollection variableRanges = null, 178 ShapeConstraints intervalConstraints = null) 176 179 : base(dataset, allowedInputVariables, transformations ?? Enumerable.Empty<ITransformation>()) { 177 180 var variables = InputVariables.Select(x => x.AsReadOnly()).ToList(); 178 181 Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First())); 179 var intervalCollection = CalculateDatasetIntervals(this.Dataset); 180 Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, intervalCollection)); 181 Parameters.Add(new FixedValueParameter<ProblemDataConstraint>(IntervalConstraintsParameterName, new ProblemDataConstraint(this))); 182 if (variableRanges == null) { 183 variableRanges = CalculateDatasetIntervals(this.Dataset); 184 } 185 Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, variableRanges)); 186 if (intervalConstraints == null) { 187 intervalConstraints = new ShapeConstraints(); 188 } 189 Parameters.Add(new FixedValueParameter<ShapeConstraints>(IntervalConstraintsParameterName, intervalConstraints)); 182 190 RegisterParameterEvents(); 183 191 } … … 185 193 private static IntervalCollection CalculateDatasetIntervals(IDataset dataset) { 186 194 IntervalCollection intervalCollection = new IntervalCollection(); 187 foreach (var variable in dataset.DoubleVariables) { // intervals are only possible for double variables195 foreach (var variable in dataset.DoubleVariables) { // intervals are only possible for double variables 188 196 var variableInterval = Interval.GetInterval(dataset.GetDoubleValues(variable)); 189 197 intervalCollection.AddInterval(variable, variableInterval); -
branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionProblemData.cs
r17607 r17887 30 30 string TargetVariable { get; set; } 31 31 32 IntervalCollection VariableRanges{ get; }33 ProblemDataConstraintIntervalConstraints { get; }32 IntervalCollection VariableRanges { get; } 33 ShapeConstraints IntervalConstraints { get; } 34 34 35 IEnumerable<double> TargetVariableValues 35 IEnumerable<double> TargetVariableValues { get; } 36 36 IEnumerable<double> TargetVariableTrainingValues { get; } 37 IEnumerable<double> TargetVariableTestValues 37 IEnumerable<double> TargetVariableTestValues { get; } 38 38 } 39 39 } -
branches/3073_IA_constraint_splitting_reintegration/IAConstraintsSplitting.sln
r17591 r17887 9 9 EndProject 10 10 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Problems.DataAnalysis.Views-3.4", "HeuristicLab.Problems.DataAnalysis.Views\3.4\HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj", "{3E9E8944-44FF-40BB-A622-3A4A7DD0F198}" 11 EndProject 12 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}" 11 13 EndProject 12 14 Global … … 56 58 {3E9E8944-44FF-40BB-A622-3A4A7DD0F198}.Release|x86.ActiveCfg = Release|x86 57 59 {3E9E8944-44FF-40BB-A622-3A4A7DD0F198}.Release|x86.Build.0 = Release|x86 60 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 61 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Debug|Any CPU.Build.0 = Debug|Any CPU 62 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Debug|x64.ActiveCfg = Debug|Any CPU 63 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Debug|x64.Build.0 = Debug|Any CPU 64 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Debug|x86.ActiveCfg = Debug|Any CPU 65 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Debug|x86.Build.0 = Debug|Any CPU 66 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Release|Any CPU.ActiveCfg = Release|Any CPU 67 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Release|Any CPU.Build.0 = Release|Any CPU 68 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Release|x64.ActiveCfg = Release|Any CPU 69 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Release|x64.Build.0 = Release|Any CPU 70 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Release|x86.ActiveCfg = Release|Any CPU 71 {74BBCE93-8C04-4652-A6A4-E6EFF2DD8175}.Release|x86.Build.0 = Release|Any CPU 58 72 EndGlobalSection 59 73 GlobalSection(SolutionProperties) = preSolution
Note: See TracChangeset
for help on using the changeset viewer.