1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022012 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  using System.Linq;


22  using HeuristicLab.Common;


23  using HeuristicLab.Core;


24  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


25 


26  namespace HeuristicLab.Algorithms.DataAnalysis {


27  [StorableClass]


28  [Item(Name = "MeanSum", Description = "Sum of mean functions for Gaussian processes.")]


29  public class MeanSum : Item, IMeanFunction {


30  [Storable]


31  private ItemList<IMeanFunction> terms;


32 


33  [Storable]


34  private int numberOfVariables;


35  public ItemList<IMeanFunction> Terms {


36  get { return terms; }


37  }


38 


39  public int GetNumberOfParameters(int numberOfVariables) {


40  this.numberOfVariables = numberOfVariables;


41  return terms.Select(t => t.GetNumberOfParameters(numberOfVariables)).Sum();


42  }


43  [StorableConstructor]


44  protected MeanSum(bool deserializing) : base(deserializing) { }


45  protected MeanSum(MeanSum original, Cloner cloner)


46  : base(original, cloner) {


47  this.terms = cloner.Clone(original.terms);


48  this.numberOfVariables = original.numberOfVariables;


49  }


50  public MeanSum() {


51  this.terms = new ItemList<IMeanFunction>();


52  }


53 


54  public void SetParameter(double[] hyp) {


55  int offset = 0;


56  foreach (var t in terms) {


57  var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);


58  t.SetParameter(hyp.Skip(offset).Take(numberOfParameters).ToArray());


59  offset += numberOfParameters;


60  }


61  }


62 


63  public void SetData(double[,] x) {


64  foreach (var t in terms) t.SetData(x);


65  }


66 


67  public double[] GetMean(double[,] x) {


68  var res = terms.First().GetMean(x);


69  foreach (var t in terms.Skip(1)) {


70  var a = t.GetMean(x);


71  for (int i = 0; i < res.Length; i++) res[i] += a[i];


72  }


73  return res;


74  }


75 


76  public double[] GetGradients(int k, double[,] x) {


77  int i = 0;


78  while (k >= terms[i].GetNumberOfParameters(numberOfVariables)) {


79  k = terms[i].GetNumberOfParameters(numberOfVariables);


80  i++;


81  }


82  return terms[i].GetGradients(k, x);


83  }


84 


85  public override IDeepCloneable Clone(Cloner cloner) {


86  return new MeanSum(this, cloner);


87  }


88  }


89  }

