#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Algorithms.DataAnalysis {
[StorableClass]
[Item(Name = "CovarianceSquaredExponentialIso",
Description = "Isotropic squared exponential covariance function for Gaussian processes.")]
public sealed class CovarianceSquaredExponentialIso : ParameterizedNamedItem, ICovarianceFunction {
public IValueParameter ScaleParameter {
get { return (IValueParameter)Parameters["Scale"]; }
}
public IValueParameter InverseLengthParameter {
get { return (IValueParameter)Parameters["InverseLength"]; }
}
private bool HasFixedInverseLengthParameter {
get { return InverseLengthParameter.Value != null; }
}
private bool HasFixedScaleParameter {
get { return ScaleParameter.Value != null; }
}
[StorableConstructor]
private CovarianceSquaredExponentialIso(bool deserializing)
: base(deserializing) {
}
private CovarianceSquaredExponentialIso(CovarianceSquaredExponentialIso original, Cloner cloner)
: base(original, cloner) {
}
public CovarianceSquaredExponentialIso()
: base() {
Name = ItemName;
Description = ItemDescription;
Parameters.Add(new OptionalValueParameter("Scale", "The scale parameter of the isometric squared exponential covariance function."));
Parameters.Add(new OptionalValueParameter("InverseLength", "The inverse length parameter of the isometric squared exponential covariance function."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new CovarianceSquaredExponentialIso(this, cloner);
}
public int GetNumberOfParameters(int numberOfVariables) {
return
(HasFixedScaleParameter ? 0 : 1) +
(HasFixedInverseLengthParameter ? 0 : 1);
}
public void SetParameter(double[] p) {
double scale, inverseLength;
GetParameterValues(p, out scale, out inverseLength);
ScaleParameter.Value = new DoubleValue(scale);
InverseLengthParameter.Value = new DoubleValue(inverseLength);
}
private void GetParameterValues(double[] p, out double scale, out double inverseLength) {
// gather parameter values
int c = 0;
if (HasFixedInverseLengthParameter) {
inverseLength = InverseLengthParameter.Value.Value;
} else {
inverseLength = 1.0 / Math.Exp(p[c]);
c++;
}
if (HasFixedScaleParameter) {
scale = ScaleParameter.Value.Value;
} else {
scale = Math.Exp(2 * p[c]);
c++;
}
if (p.Length != c) throw new ArgumentException("The length of the parameter vector does not match the number of free parameters for CovarianceSquaredExponentialIso", "p");
}
public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
double inverseLength, scale;
GetParameterValues(p, out scale, out inverseLength);
var fixedInverseLength = HasFixedInverseLengthParameter;
var fixedScale = HasFixedScaleParameter;
// create functions
var cov = new ParameterizedCovarianceFunction();
cov.Covariance = (x, i, j) => {
double d = i == j
? 0.0
: Util.SqrDist(x, i, j, columnIndices, inverseLength);
return scale * Math.Exp(-d / 2.0);
};
cov.CrossCovariance = (x, xt, i, j) => {
double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
return scale * Math.Exp(-d / 2.0);
};
cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices,
fixedInverseLength, fixedScale);
return cov;
}
// order of returned gradients must match the order in GetParameterValues!
private static IList GetGradient(double[,] x, int i, int j, double sf2, double inverseLength, int[] columnIndices,
bool fixedInverseLength, bool fixedScale) {
double d = i == j
? 0.0
: Util.SqrDist(x, i, j, columnIndices, inverseLength);
double g = Math.Exp(-d / 2.0);
var gr = new List(2);
if (!fixedInverseLength) gr.Add(sf2 * g * d);
if (!fixedScale) gr.Add(2.0 * sf2 * g);
return gr;
}
}
}