Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/08/12 23:59:16 (12 years ago)
Author:
ascheibe
Message:

#1861 merged changes from trunk into branch

Location:
branches/HeuristicLab.Mono
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Mono

  • branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis

  • branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4

    • Property svn:ignore
      •  

        old new  
        55*.vs10x
        66Plugin.cs
         7*.user
  • branches/HeuristicLab.Mono/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceSum.cs

    r8323 r8451  
    1 using System.Collections.Generic;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
    222using System.Linq;
     23using HeuristicLab.Common;
     24using HeuristicLab.Core;
     25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    326
    4 namespace HeuristicLab.Algorithms.DataAnalysis.GaussianProcess {
    5   public class CovarianceSum : ICovarianceFunction {
    6     private IList<ICovarianceFunction> covariances;
     27namespace HeuristicLab.Algorithms.DataAnalysis {
     28  [StorableClass]
     29  [Item(Name = "CovarianceSum",
     30    Description = "Sum covariance function for Gaussian processes.")]
     31  public class CovarianceSum : Item, ICovarianceFunction {
     32    [Storable]
     33    private ItemList<ICovarianceFunction> terms;
    734
    8     public int NumberOfParameters {
    9       get { return covariances.Sum(c => c.NumberOfParameters); }
     35    [Storable]
     36    private int numberOfVariables;
     37    public ItemList<ICovarianceFunction> Terms {
     38      get { return terms; }
    1039    }
    1140
    12     public CovarianceSum(IEnumerable<ICovarianceFunction> covariances) {
    13       this.covariances = covariances.ToList();
     41    [StorableConstructor]
     42    protected CovarianceSum(bool deserializing)
     43      : base(deserializing) {
    1444    }
    1545
    16     public void SetMatrix(double[,] x) {
    17       foreach (var covariance in covariances) {
    18         covariance.SetMatrix(x, x);
     46    protected CovarianceSum(CovarianceSum original, Cloner cloner)
     47      : base(original, cloner) {
     48      this.terms = cloner.Clone(original.terms);
     49      this.numberOfVariables = original.numberOfVariables;
     50    }
     51
     52    public CovarianceSum()
     53      : base() {
     54      this.terms = new ItemList<ICovarianceFunction>();
     55    }
     56
     57    public override IDeepCloneable Clone(Cloner cloner) {
     58      return new CovarianceSum(this, cloner);
     59    }
     60
     61    public int GetNumberOfParameters(int numberOfVariables) {
     62      this.numberOfVariables = numberOfVariables;
     63      return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();
     64    }
     65
     66    public void SetParameter(double[] hyp) {
     67      int offset = 0;
     68      foreach (var t in terms) {
     69        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
     70        t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());
     71        offset += numberOfParameters;
    1972      }
    2073    }
    21 
    22     public void SetMatrix(double[,] x, double[,] xt) {
    23       foreach (var covariance in covariances) {
    24         covariance.SetMatrix(x, xt);
    25       }
     74    public void SetData(double[,] x) {
     75      SetData(x, x);
    2676    }
    2777
    28     public void SetHyperparamter(double[] hyp) {
    29       int i = 0;
    30       foreach (var covariance in covariances) {
    31         int n = covariance.NumberOfParameters;
    32         covariance.SetHyperparamter(hyp.Skip(i).Take(n).ToArray());
    33         i += n;
     78    public void SetData(double[,] x, double[,] xt) {
     79      foreach (var t in terms) {
     80        t.SetData(x, xt);
    3481      }
    3582    }
    3683
    3784    public double GetCovariance(int i, int j) {
    38       return covariances.Select(c => c.GetCovariance(i, j)).Sum();
     85      return terms.Select(t => t.GetCovariance(i, j)).Sum();
    3986    }
    4087
    41 
    42     public double[] GetDiagonalCovariances() {
    43       return covariances
    44         .Select(c => c.GetDiagonalCovariances())
    45         .Aggregate((s, d) => s.Zip(d, (a, b) => a + b).ToArray())
    46         .ToArray();
    47     }
    48 
    49     public double[] GetDerivatives(int i, int j) {
    50       return covariances
    51         .Select(c => c.GetDerivatives(i, j))
    52         .Aggregate(Enumerable.Empty<double>(), (h0, h1) => h0.Concat(h1))
    53         .ToArray();
     88    public double[] GetGradient(int i, int j) {
     89      return terms.Select(t => t.GetGradient(i, j)).SelectMany(seq => seq).ToArray();
    5490    }
    5591  }
Note: See TracChangeset for help on using the changeset viewer.