Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/10/12 13:28:55 (12 years ago)
Author:
gkronber
Message:

#1902 implemented all mean and covariance functions with parameters as ParameterizedNamedItems

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/MeanConst.cs

    r8473 r8612  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    2930  [StorableClass]
    3031  [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")]
    31   public class MeanConst : Item, IMeanFunction {
     32  public sealed class MeanConst : ParameterizedNamedItem, IMeanFunction {
    3233    [Storable]
    3334    private double c;
    34     public double Value { get { return c; } }
     35    [Storable]
     36    private readonly HyperParameter<DoubleValue> valueParameter;
     37    public IValueParameter<DoubleValue> ValueParameter { get { return valueParameter; } }
    3538
    36     public int GetNumberOfParameters(int numberOfVariables) {
    37       return 1;
    38     }
    3939    [StorableConstructor]
    40     protected MeanConst(bool deserializing) : base(deserializing) { }
    41     protected MeanConst(MeanConst original, Cloner cloner)
     40    private MeanConst(bool deserializing) : base(deserializing) { }
     41    private MeanConst(MeanConst original, Cloner cloner)
    4242      : base(original, cloner) {
    4343      this.c = original.c;
     44      this.valueParameter = cloner.Clone(original.valueParameter);
     45      RegisterEvents();
    4446    }
    4547    public MeanConst()
    4648      : base() {
     49      this.name = ItemName;
     50      this.description = ItemDescription;
     51
     52      this.valueParameter = new HyperParameter<DoubleValue>("Value", "The constant value for the constant mean function.");
     53      Parameters.Add(valueParameter);
     54      RegisterEvents();
     55    }
     56
     57    public override IDeepCloneable Clone(Cloner cloner) {
     58      return new MeanConst(this, cloner);
     59    }
     60
     61    [StorableHook(HookType.AfterDeserialization)]
     62    private void AfterDeserialization() {
     63      RegisterEvents();
     64    }
     65
     66    private void RegisterEvents() {
     67      Util.AttachValueChangeHandler<DoubleValue, double>(valueParameter, () => { c = valueParameter.Value.Value; });
     68    }
     69
     70    public int GetNumberOfParameters(int numberOfVariables) {
     71      return valueParameter.Fixed ? 0 : 1;
    4772    }
    4873
    4974    public void SetParameter(double[] hyp) {
    50       if (hyp.Length != 1) throw new ArgumentException("Only one hyper-parameter allowed for constant mean function.", "hyp");
    51       this.c = hyp[0];
    52     }
    53     public void SetData(double[,] x) {
    54       // nothing to do
     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");
    5579    }
    5680
     
    6387      return Enumerable.Repeat(1.0, x.GetLength(0)).ToArray();
    6488    }
    65 
    66     public override IDeepCloneable Clone(Cloner cloner) {
    67       return new MeanConst(this, cloner);
    68     }
    6989  }
    7090}
Note: See TracChangeset for help on using the changeset viewer.