Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/12 19:02:47 (11 years ago)
Author:
gkronber
Message:

#1902: removed class HyperParameter and changed implementations of covariance and mean functions to remove the parameter value caching and event handlers for parameter caching. Instead it is now possible to create the actual covariance and mean functions as Func from templates and specified parameter values. The instances of mean and covariance functions configured in the GUI are actually templates where the structure and fixed parameters can be specified.

File:
1 edited

Legend:

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

    r8929 r8982  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2830
     
    3133  [Item(Name = "MeanConst", Description = "Constant mean function for Gaussian processes.")]
    3234  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    }
    3838
    3939    [StorableConstructor]
     
    4141    private MeanConst(MeanConst original, Cloner cloner)
    4242      : base(original, cloner) {
    43       this.c = original.c;
    44       this.valueParameter = cloner.Clone(original.valueParameter);
    45       RegisterEvents();
    4643    }
    4744    public MeanConst()
     
    5047      this.description = ItemDescription;
    5148
    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."));
    5550    }
    5651
     
    5954    }
    6055
    61     [StorableHook(HookType.AfterDeserialization)]
    62     private void AfterDeserialization() {
    63       RegisterEvents();
     56    public int GetNumberOfParameters(int numberOfVariables) {
     57      return ValueParameter.Value != null ? 0 : 1;
    6458    }
    6559
    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);
    6864    }
    6965
    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      }
    7276    }
    7377
    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;
    8888    }
    8989  }
Note: See TracChangeset for help on using the changeset viewer.