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 sealed 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  [StorableConstructor]


40  private MeanSum(bool deserializing) : base(deserializing) { }


41  private MeanSum(MeanSum original, Cloner cloner)


42  : base(original, cloner) {


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


44  this.numberOfVariables = original.numberOfVariables;


45  }


46  public MeanSum() {


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


48  }


49 


50  public override IDeepCloneable Clone(Cloner cloner) {


51  return new MeanSum(this, cloner);


52  }


53 


54  public int GetNumberOfParameters(int numberOfVariables) {


55  this.numberOfVariables = numberOfVariables;


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


57  }


58 


59  public void SetParameter(double[] hyp) {


60  int offset = 0;


61  foreach (var t in terms) {


62  var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);


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


64  offset += numberOfParameters;


65  }


66  }


67 


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


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


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


71  var a = t.GetMean(x);


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


73  }


74  return res;


75  }


76 


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


78  int i = 0;


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


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


81  i++;


82  }


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


84  }


85  }


86  }

