Changeset 17294
- Timestamp:
- 10/02/19 17:06:47 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/Interpreter.cs
r17292 r17294 422 422 // we assume that the for variable nodes ( v(x,w) = w * x ) the gradient is returned for parameter w 423 423 instruction.value = new AlgebraicInterval( 424 low: new MultivariateDual<AlgebraicDouble>(v: lower, key: variable, dv: intervals[variable.VariableName].LowerBound), 424 low: new MultivariateDual<AlgebraicDouble>(v: lower, key: variable, dv: intervals[variable.VariableName].LowerBound), 425 425 high: new MultivariateDual<AlgebraicDouble>(v: upper, key: variable, dv: intervals[variable.VariableName].UpperBound) 426 426 ); … … 433 433 434 434 public interface IAlgebraicType<T> { 435 T Zero { get; } 435 T Zero { get; } // Zero and One must create new objects 436 436 T One { get; } 437 437 … … 453 453 T AssignIntRoot(T a, int r); 454 454 T AssignSgn(T a); // set this to sign(a) 455 T AssignMin(T other); // set this min(this, other) 456 T AssignMax(T other); // set this max(this, other) 455 457 T Clone(); 456 458 } … … 468 470 public static T IntRoot<T>(this T a, int r) where T : IAlgebraicType<T> { a.AssignIntRoot(a.Clone(), r); return a; } 469 471 470 public static T Max<T>(T a, T b) where T : IAlgebraicType<T> { 471 // ((a + b) + abs(b - a)) / 2 472 return a.Clone().Add(b).Add(b.Clone().Sub(a).Abs()).Scale(1.0 / 2.0); 473 } 474 public static T Min<T>(T a, T b) where T : IAlgebraicType<T> { 475 // ((a + b) - abs(a - b)) / 2 476 return a.Clone().Add(b).Sub(a.Clone().Sub(b).Abs()).Scale(1.0 / 2.0); 477 } 472 internal static T Min<T>(T a, T b) where T : IAlgebraicType<T> { return a.Clone().AssignMin(b); } 473 internal static T Max<T>(T a, T b) where T : IAlgebraicType<T> { return a.Clone().AssignMax(b); } 474 475 // public static T Max<T>(T a, T b) where T : IAlgebraicType<T> { 476 // // ((a + b) + abs(b - a)) / 2 477 // return a.Clone().Add(b).Add(b.Clone().Sub(a).Abs()).Scale(1.0 / 2.0); 478 // } 479 // public static T Min<T>(T a, T b) where T : IAlgebraicType<T> { 480 // // ((a + b) - abs(a - b)) / 2 481 // return a.Clone().Add(b).Sub(a.Clone().Sub(b).Abs()).Scale(1.0 / 2.0); 482 // } 478 483 } 479 484 … … 511 516 public AlgebraicDouble AssignIntPower(AlgebraicDouble a, int p) { Value = Math.Pow(a.Value, p); return this; } 512 517 public AlgebraicDouble AssignIntRoot(AlgebraicDouble a, int r) { Value = IntRoot(a.Value, r); return this; } 518 public AlgebraicDouble AssignMin(AlgebraicDouble other) { Value = Math.Min(Value, other.Value); return this; } 519 public AlgebraicDouble AssignMax(AlgebraicDouble other) { Value = Math.Max(Value, other.Value); return this; } 513 520 514 521 // helper … … 563 570 public AlgebraicDoubleVector AssignIntPower(AlgebraicDoubleVector a, int p) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Pow(a.arr[i], p); } return this; } 564 571 public AlgebraicDoubleVector AssignIntRoot(AlgebraicDoubleVector a, int r) { for (int i = 0; i < arr.Length; ++i) { arr[i] = IntRoot(a.arr[i], r); } return this; } 572 public AlgebraicDoubleVector AssignMin(AlgebraicDoubleVector other) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Min(arr[i], other.arr[i]); } return this; } 573 public AlgebraicDoubleVector AssignMax(AlgebraicDoubleVector other) { for (int i = 0; i < arr.Length; ++i) { arr[i] = Math.Max(arr[i], other.arr[i]); } return this; } 565 574 566 575 // helper … … 778 787 } 779 788 789 public AlgebraicSparseVector<K, T> AssignMin(AlgebraicSparseVector<K, T> other) { 790 // assumes that keys without a matching key in other are zero and vice versa 791 foreach (var kvp in elems) if (!other.elems.ContainsKey(kvp.Key)) kvp.Value.AssignMin(kvp.Value.Zero); // min(v, 0) 792 foreach (var kvp in other.elems) { 793 if (elems.TryGetValue(kvp.Key, out T value)) 794 value.AssignMin(kvp.Value); 795 else 796 elems.Add(kvp.Key, kvp.Value.Zero.AssignMin(kvp.Value)); 797 } 798 return this; 799 } 800 801 public AlgebraicSparseVector<K, T> AssignMax(AlgebraicSparseVector<K, T> other) { 802 // assumes that keys without a matching key in other are zero and vice versa 803 foreach (var kvp in elems) if (!other.elems.ContainsKey(kvp.Key)) kvp.Value.AssignMax(kvp.Value.Zero); // max(v, 0) 804 foreach (var kvp in other.elems) { 805 if (elems.TryGetValue(kvp.Key, out T value)) 806 value.AssignMax(kvp.Value); 807 else 808 elems.Add(kvp.Key, kvp.Value.Zero.AssignMax(kvp.Value)); 809 } 810 return this; 811 } 812 813 780 814 public AlgebraicSparseVector<K, T> Clone() { 781 815 return new AlgebraicSparseVector<K, T>(this); … … 836 870 } 837 871 838 // algebraic min() / max() do not work for infinities 839 // detect and handle infinite values explicitly 872 840 873 private static MultivariateDual<AlgebraicDouble> Min(MultivariateDual<AlgebraicDouble> a, MultivariateDual<AlgebraicDouble> b) { 841 if (a.Value.IsInfinity || b.Value.IsInfinity) return a.Value < b.Value ? a : b; // NOTE: this is not differentiable but for infinite values we do not care 842 else return Algebraic.Min(a, b); // differentiable statement 874 return a.Value < b.Value ? a : b; 843 875 } 844 876 private static MultivariateDual<AlgebraicDouble> Max(MultivariateDual<AlgebraicDouble> a, MultivariateDual<AlgebraicDouble> b) { 845 if (a.Value.IsInfinity || b.Value.IsInfinity) return a.Value >= b.Value ? a : b; // NOTE: this is not differentiable but for infinite values we do not care 846 else return Algebraic.Max(a, b); // differentiable statement 877 return a.Value > b.Value ? a : b; 847 878 } 848 879 … … 919 950 if (a.Contains(0.0)) { 920 951 low = new MultivariateDual<AlgebraicDouble>(0.0); 921 high = Algebraic.Max(a.low.IntPower(p),a.high.IntPower(p));952 high = a.low.IntPower(p).AssignMax(a.high.IntPower(p)); 922 953 } else { 923 954 var lowPower = a.low.IntPower(p); 924 955 var highPower = a.high.IntPower(p); 925 low = Algebraic.Min(lowPower,highPower);926 high = Algebraic.Max(lowPower,highPower);956 low = lowPower.AssignMin(highPower); 957 high = lowPower.AssignMax(highPower); 927 958 } 928 959 } else { … … 934 965 var lowPower = a.low.IntPower(p); 935 966 var highPower = a.high.IntPower(p); 936 low = Algebraic.Min(lowPower,highPower);937 high = Algebraic.Max(lowPower,highPower);967 low = lowPower.AssignMin(highPower); 968 high = lowPower.AssignMax(highPower); 938 969 } 939 970 } … … 1064 1095 return this; 1065 1096 } 1097 1098 public AlgebraicInterval AssignMin(AlgebraicInterval other) { 1099 low.AssignMin(other.low); 1100 high.AssignMin(other.high); 1101 return this; 1102 } 1103 1104 public AlgebraicInterval AssignMax(AlgebraicInterval other) { 1105 low.AssignMax(other.low); 1106 high.AssignMax(other.high); 1107 return this; 1108 } 1066 1109 } 1067 1110 … … 1116 1159 public Dual<V> Clone() { return new Dual<V>(v.Clone(), dv.Clone()); } 1117 1160 1161 public Dual<V> AssignMin(Dual<V> other) { 1162 throw new NotImplementedException(); 1163 } 1164 1165 public Dual<V> AssignMax(Dual<V> other) { 1166 throw new NotImplementedException(); 1167 } 1118 1168 } 1119 1169 … … 1199 1249 public MultivariateDual<V> AssignSgn(MultivariateDual<V> a) { v.AssignSgn(a.v); dv = a.dv.Zero; return this; } // sign(f(x))' = 0; 1200 1250 1251 public MultivariateDual<V> AssignMin(MultivariateDual<V> other) { 1252 XXX 1253 } 1254 1255 public MultivariateDual<V> AssignMax(MultivariateDual<V> other) { 1256 XXX 1257 } 1201 1258 } 1202 1259 }
Note: See TracChangeset
for help on using the changeset viewer.