Changeset 8982 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanConst.cs
- Timestamp:
- 12/01/12 19:02:47 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanFunctions/MeanConst.cs
r8929 r8982 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 30 … … 31 33 [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")] 32 34 public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction { 33 [Storable] 34 private double c; 35 [Storable] 36 private readonly HyperParameter<DoubleValue> valueParameter; 37 public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } } 35 public IValueParameter<DoubleValue> ValueParameter { 36 get { return (IValueParameter<DoubleValue>)Parameters["Value"]; } 37 } 38 38 39 39 [StorableConstructor] … … 41 41 private MeanConst(MeanConst original, Cloner cloner) 42 42 : base(original, cloner) { 43 this.c = original.c;44 this.valueParameter = cloner.Clone(original.valueParameter);45 RegisterEvents();46 43 } 47 44 public MeanConst() … … 50 47 this.description = ItemDescription; 51 48 52 this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function."); 53 Parameters.Add(valueParameter); 54 RegisterEvents(); 49 Parameters.Add(new OptionalValueParameter<DoubleValue>("Value", "The constant value for the constant mean function.")); 55 50 } 56 51 … … 59 54 } 60 55 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 RegisterEvents(); 56 public int GetNumberOfParameters(int numberOfVariables) { 57 return ValueParameter.Value != null ? 0 : 1; 64 58 } 65 59 66 private void RegisterEvents() { 67 Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; }); 60 public void SetParameter(double[] p) { 61 double c; 62 GetParameters(p, out c); 63 ValueParameter.Value = new DoubleValue(c); 68 64 } 69 65 70 public int GetNumberOfParameters(int numberOfVariables) { 71 return valueParameter.Fixed ? 0 : 1; 66 private void GetParameters(double[] p, out double c) { 67 if (ValueParameter.Value == null) { 68 c = p[0]; 69 } else { 70 if (p.Length > 0) 71 throw new ArgumentException( 72 "The length of the parameter vector does not match the number of free parameters for the constant mean function.", 73 "p"); 74 c = ValueParameter.Value.Value; 75 } 72 76 } 73 77 74 public void SetParameter(double[] hyp) { 75 if (!valueParameter.Fixed) { 76 valueParameter.SetValue(new DoubleValue(hyp[0])); 77 } else if (hyp.Length > 0) 78 throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for the constant mean function.", "hyp"); 79 } 80 81 public double[] GetMean(double[,] x) { 82 return Enumerable.Repeat(c, x.GetLength(0)).ToArray(); 83 } 84 85 public double[] GetGradients(int k, double[,] x) { 86 if (k > 0) throw new ArgumentException(); 87 return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray(); 78 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) { 79 double c; 80 GetParameters(p, out c); 81 var mf = new ParameterizedMeanFunction(); 82 mf.Mean = (x, i) => c; 83 mf.Gradient = (x, i, k) => { 84 if (k > 0) throw new ArgumentException(); 85 return 1.0; 86 }; 87 return mf; 88 88 } 89 89 }
Note: See TracChangeset
for help on using the changeset viewer.