#region License Information /* HeuristicLab * Copyright (C) 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 HEAL.Attic; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.DataAnalysis { [Item("RBF model (3d)", "Radial-basis-function model (wrapper for alglib.rbfmodel)")] [StorableType("74CE499A-884C-4280-B6BB-DF402692F40D")] public sealed class AlglibRbfModel : RegressionModel { // not storable! see persistence properties below private alglib.rbfmodel model; [Storable] private string x1; [Storable] private string x2; [Storable] private string x3; public override IEnumerable VariablesUsedForPrediction => new[] { x1, x2, x3 }; [StorableConstructor] private AlglibRbfModel(StorableConstructorFlag deserializing) : base(deserializing) { this.model = new alglib.rbfmodel(); } private AlglibRbfModel(AlglibRbfModel orig, Cloner cloner) : base(orig, cloner) { this.x1 = orig.x1; this.x2 = orig.x2; this.x3 = orig.x3; this.model = (alglib.rbfmodel)orig.model.make_copy(); } public AlglibRbfModel(alglib.rbfmodel model, string y, string x1, string x2, string x3) : base(y, $"Spline model ({x1}, {x2}, {x3})") { this.model = (alglib.rbfmodel)model.make_copy(); this.x1 = x1; this.x2 = x2; this.x3 = x3; } public override IDeepCloneable Clone(Cloner cloner) => new AlglibRbfModel(this, cloner); public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { var solution = new RegressionSolution(this, (IRegressionProblemData)problemData.Clone()); solution.Name = $"Alglib RBF model ({x1},{x2},{x3})"; return solution; } public override IEnumerable GetEstimatedValues(IDataset dataset, IEnumerable rows) { var x1 = dataset.GetDoubleValues(this.x1, rows).ToArray(); var x2 = dataset.GetDoubleValues(this.x2, rows).ToArray(); var x3 = dataset.GetDoubleValues(this.x3, rows).ToArray(); var res = new double[x1.Length]; for (int i = 0; i < x1.Length; i++) { res[i] = alglib.rbfcalc3(model, x1[i], x2[i], x3[i]); } return res; } #region persistence [Storable] public string SerializedInterpolant { get { alglib.rbfserialize(model, out var serialized); return serialized; } set { alglib.rbfunserialize(value, out var obj); model = obj; } } #endregion } }