Changeset 15531
 Timestamp:
 12/18/17 12:27:14 (20 months ago)
 Location:
 branches/Weighted TSNE/3.4/TSNE
 Files:

 2 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 } 
branches/Weighted TSNE/3.4/TSNE/TSNEAlgorithm.cs
r15487 r15531 195 195 set { RandomInitializationParameter.Value.Value = value; } 196 196 } 197 198 197 public int UpdateInterval { 199 198 get { return UpdateIntervalParameter.Value.Value; } … … 219 218 [StorableHook(HookType.AfterDeserialization)] 220 219 private void AfterDeserialization() { 220 if (Parameters.ContainsKey(RandomInitializationParameterName)) 221 Parameters.Add(new FixedValueParameter<BoolValue>(RandomInitializationParameterName, "Wether data points should be randomly initialized or according to the first 2 dimensions", new BoolValue(true))); 221 222 RegisterParameterEvents(); 222 223 } … … 279 280 var problemData = Problem.ProblemData; 280 281 // set up and initialize everything if necessary 282 var wdist = DistanceFunction as WeightedEuclideanDistance; 283 if (wdist != null) wdist.Initialize(problemData); 281 284 if (state == null) { 282 285 if (SetSeedRandomly) Seed = new System.Random().Next(); … … 299 302 300 303 if (Normalization) data = NormalizeInputData(data); 301 var randomInit = Parameters.ContainsKey(RandomInitializationParameterName) ? RandomInitialization : true; 302 state = TSNEStatic<double[]>.CreateState(data, DistanceFunction, random, NewDimensions, Perplexity, Theta, StopLyingIteration, MomentumSwitchIteration, InitialMomentum, FinalMomentum, Eta, randomInit); 304 state = TSNEStatic<double[]>.CreateState(data, DistanceFunction, random, NewDimensions, Perplexity, Theta, StopLyingIteration, MomentumSwitchIteration, InitialMomentum, FinalMomentum, Eta, RandomInitialization); 303 305 SetUpResults(allindices); 304 306 iter = 0; … … 338 340 if (Problem == null  Problem.ProblemData == null) return; 339 341 OnPerplexityChanged(this, null); 342 OnColumnsChanged(this, null); 340 343 Problem.ProblemData.Changed += OnPerplexityChanged; 341 344 Problem.ProblemData.Changed += OnColumnsChanged; … … 349 352 private void OnColumnsChanged(object sender, EventArgs e) { 350 353 if (Problem == null  Problem.ProblemData == null  Problem.ProblemData.Dataset == null  !Parameters.ContainsKey(DistanceFunctionParameterName)) return; 351 DistanceFunctionParameter.ValidValues.OfType<WeightedEuclideanDistance>().Single(). Weights = new DoubleArray(Problem.ProblemData.AllowedInputVariables.Select(x => 1.0).ToArray());354 DistanceFunctionParameter.ValidValues.OfType<WeightedEuclideanDistance>().Single().AdaptWeightVector(Problem.ProblemData); 352 355 } 353 356 … … 546 549 var colorGradient = ColorGradient.Colors; 547 550 var range = high  low; 548 var h = cell / range * colorGradient.Count;551 var h = Math.Min(cell / range * colorGradient.Count, colorGradient.Count  1); 549 552 return colorGradient[(int) h]; 550 553 }
Note: See TracChangeset
for help on using the changeset viewer.