Changeset 15531 for branches/Weighted TSNE/3.4/TSNE/Distances
- Timestamp:
- 12/18/17 12:27:14 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Weighted TSNE/3.4/TSNE/Distances/WeightedEuclideanDistance.cs
r15487 r15531 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.DataAnalysis; 31 32 32 33 namespace HeuristicLab.Algorithms.DataAnalysis { … … 34 35 [Item("WeightedEuclideanDistance", "A weighted norm function that uses Euclidean distance √(Σ(w[i]²*(p1[i]-p2[i])²))")] 35 36 public class WeightedEuclideanDistance : ParameterizedNamedItem, IDistance<IEnumerable<double>> { 37 [Storable] 38 private double[] weights; 36 39 public const string WeightsParameterName = "Weights"; 37 public IValueParameter<DoubleArray> Weig thsParameter {40 public IValueParameter<DoubleArray> WeightsParameter { 38 41 get { return (IValueParameter<DoubleArray>) Parameters[WeightsParameterName]; } 39 42 } 40 43 41 44 public DoubleArray Weights { 42 get { return Weig thsParameter.Value; }43 set { Weig thsParameter.Value = value; }45 get { return WeightsParameter.Value; } 46 set { WeightsParameter.Value = value; } 44 47 } 45 48 … … 52 55 protected WeightedEuclideanDistance(WeightedEuclideanDistance original, Cloner cloner) : base(original, cloner) { 53 56 RegisterParameterEvents(); 57 weights = original.weights != null ? original.weights.ToArray() : null; 54 58 } 55 59 public override IDeepCloneable Clone(Cloner cloner) { … … 57 61 } 58 62 public WeightedEuclideanDistance() { 59 Parameters.Add(new ValueParameter<DoubleArray>(WeightsParameterName, "The weights used to modify the euclidean distance." ));63 Parameters.Add(new ValueParameter<DoubleArray>(WeightsParameterName, "The weights used to modify the euclidean distance.", new DoubleArray(new[] {1.0}))); 60 64 RegisterParameterEvents(); 61 65 } … … 76 80 77 81 public double Get(IEnumerable<double> a, IEnumerable<double> b) { 78 return GetDistance(a, b, Weights);82 return GetDistance(a, b, weights); 79 83 } 80 84 public IComparer<IEnumerable<double>> GetDistanceComparer(IEnumerable<double> item) { … … 88 92 } 89 93 94 public void AdaptToProblemData(IDataAnalysisProblemData problemData) { 95 Weights = new DoubleArray(problemData.AllowedInputVariables.Select(v => Weights.ElementNames.Contains(v) ? GetWeight(v) : 1).ToArray()) 96 {ElementNames = problemData.AllowedInputVariables}; 97 } 98 public void Initialize(IDataAnalysisProblemData problemData) { 99 if (Weights.Length != problemData.AllowedInputVariables.Count()) throw new ArgumentException("Number of Weights does not match the number of input variables"); 100 weights = Weights.ElementNames.All(v => v == null || v.Equals(string.Empty)) ? 101 Weights.ToArray() : 102 problemData.AllowedInputVariables.Select(GetWeight).ToArray(); 103 } 104 private double GetWeight(string v) { 105 var w = Weights; 106 var names = w.ElementNames.ToArray(); 107 for (var i = 0; i < w.Length; i++) if (names[i].Equals(v)) return w[i]; 108 throw new ArgumentException("weigth for " + v + " was requested but not specified."); 109 } 90 110 private void RegisterParameterEvents() { 91 Weig thsParameter.ValueChanged += OnWeightsArrayChanged;92 Weig thsParameter.Value.ItemChanged += OnWeightChanged;111 WeightsParameter.ValueChanged += OnWeightsArrayChanged; 112 WeightsParameter.Value.ItemChanged += OnWeightChanged; 93 113 } 94 114 private void OnWeightChanged(object sender, EventArgs<int> e) { 95 Weig thsParameter.Value.ItemChanged -= OnWeightChanged;115 WeightsParameter.Value.ItemChanged -= OnWeightChanged; 96 116 Weights[e.Value] = Math.Max(0, Weights[e.Value]); 97 Weig thsParameter.Value.ItemChanged -= OnWeightChanged;117 WeightsParameter.Value.ItemChanged -= OnWeightChanged; 98 118 } 99 119 private void OnWeightsArrayChanged(object sender, EventArgs e) { 100 for ( inti = 0; i < Weights.Length; i++)120 for (var i = 0; i < Weights.Length; i++) 101 121 Weights[i] = Math.Max(0, Weights[i]); 102 Weig thsParameter.Value.ItemChanged += OnWeightChanged;122 WeightsParameter.Value.ItemChanged += OnWeightChanged; 103 123 } 104 124 }
Note: See TracChangeset
for help on using the changeset viewer.