[6577] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6577] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 31 | /// <summary>
|
---|
[6580] | 32 | /// Represents a neural network ensembel model for regression and classification
|
---|
[6577] | 33 | /// </summary>
|
---|
| 34 | [StorableClass]
|
---|
[6580] | 35 | [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")]
|
---|
[14027] | 36 | public sealed class NeuralNetworkEnsembleModel : ClassificationModel, INeuralNetworkEnsembleModel {
|
---|
[6577] | 37 |
|
---|
[6580] | 38 | private alglib.mlpensemble mlpEnsemble;
|
---|
| 39 | public alglib.mlpensemble MultiLayerPerceptronEnsemble {
|
---|
| 40 | get { return mlpEnsemble; }
|
---|
[6577] | 41 | set {
|
---|
[6580] | 42 | if (value != mlpEnsemble) {
|
---|
[6577] | 43 | if (value == null) throw new ArgumentNullException();
|
---|
[6580] | 44 | mlpEnsemble = value;
|
---|
[6577] | 45 | OnChanged(EventArgs.Empty);
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[14027] | 50 | public override IEnumerable<string> VariablesUsedForPrediction {
|
---|
| 51 | get { return allowedInputVariables; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[6577] | 54 | [Storable]
|
---|
| 55 | private string targetVariable;
|
---|
| 56 | [Storable]
|
---|
| 57 | private string[] allowedInputVariables;
|
---|
| 58 | [Storable]
|
---|
| 59 | private double[] classValues;
|
---|
| 60 | [StorableConstructor]
|
---|
[6580] | 61 | private NeuralNetworkEnsembleModel(bool deserializing)
|
---|
[6577] | 62 | : base(deserializing) {
|
---|
| 63 | if (deserializing)
|
---|
[6580] | 64 | mlpEnsemble = new alglib.mlpensemble();
|
---|
[6577] | 65 | }
|
---|
[6580] | 66 | private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
|
---|
[6577] | 67 | : base(original, cloner) {
|
---|
[6580] | 68 | mlpEnsemble = new alglib.mlpensemble();
|
---|
[7694] | 69 | string serializedEnsemble;
|
---|
| 70 | alglib.mlpeserialize(original.mlpEnsemble, out serializedEnsemble);
|
---|
| 71 | alglib.mlpeunserialize(serializedEnsemble, out this.mlpEnsemble);
|
---|
[6577] | 72 | targetVariable = original.targetVariable;
|
---|
| 73 | allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
| 74 | if (original.classValues != null)
|
---|
| 75 | this.classValues = (double[])original.classValues.Clone();
|
---|
| 76 | }
|
---|
[6580] | 77 | public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
|
---|
[14027] | 78 | : base(targetVariable) {
|
---|
[6577] | 79 | this.name = ItemName;
|
---|
| 80 | this.description = ItemDescription;
|
---|
[6580] | 81 | this.mlpEnsemble = mlpEnsemble;
|
---|
[6577] | 82 | this.targetVariable = targetVariable;
|
---|
| 83 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
| 84 | if (classValues != null)
|
---|
| 85 | this.classValues = (double[])classValues.Clone();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[6580] | 89 | return new NeuralNetworkEnsembleModel(this, cloner);
|
---|
[6577] | 90 | }
|
---|
| 91 |
|
---|
[12702] | 92 | public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[15142] | 93 | double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
|
---|
[6577] | 94 |
|
---|
| 95 | int n = inputData.GetLength(0);
|
---|
| 96 | int columns = inputData.GetLength(1);
|
---|
| 97 | double[] x = new double[columns];
|
---|
| 98 | double[] y = new double[1];
|
---|
| 99 |
|
---|
| 100 | for (int row = 0; row < n; row++) {
|
---|
| 101 | for (int column = 0; column < columns; column++) {
|
---|
| 102 | x[column] = inputData[row, column];
|
---|
| 103 | }
|
---|
[6580] | 104 | alglib.mlpeprocess(mlpEnsemble, x, ref y);
|
---|
[6577] | 105 | yield return y[0];
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[14027] | 109 | public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[15142] | 110 | double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
|
---|
[6577] | 111 |
|
---|
| 112 | int n = inputData.GetLength(0);
|
---|
| 113 | int columns = inputData.GetLength(1);
|
---|
| 114 | double[] x = new double[columns];
|
---|
| 115 | double[] y = new double[classValues.Length];
|
---|
| 116 |
|
---|
| 117 | for (int row = 0; row < n; row++) {
|
---|
| 118 | for (int column = 0; column < columns; column++) {
|
---|
| 119 | x[column] = inputData[row, column];
|
---|
| 120 | }
|
---|
[6580] | 121 | alglib.mlpeprocess(mlpEnsemble, x, ref y);
|
---|
[6577] | 122 | // find class for with the largest probability value
|
---|
| 123 | int maxProbClassIndex = 0;
|
---|
| 124 | double maxProb = y[0];
|
---|
| 125 | for (int i = 1; i < y.Length; i++) {
|
---|
| 126 | if (maxProb < y[i]) {
|
---|
| 127 | maxProb = y[i];
|
---|
| 128 | maxProbClassIndex = i;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | yield return classValues[maxProbClassIndex];
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[14027] | 135 | public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
| 136 | return new NeuralNetworkEnsembleRegressionSolution(this, new RegressionEnsembleProblemData(problemData));
|
---|
[6603] | 137 | }
|
---|
[14027] | 138 | public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 139 | return new NeuralNetworkEnsembleClassificationSolution(this, new ClassificationEnsembleProblemData(problemData));
|
---|
[6603] | 140 | }
|
---|
| 141 |
|
---|
[6577] | 142 | #region events
|
---|
| 143 | public event EventHandler Changed;
|
---|
| 144 | private void OnChanged(EventArgs e) {
|
---|
| 145 | var handlers = Changed;
|
---|
| 146 | if (handlers != null)
|
---|
| 147 | handlers(this, e);
|
---|
| 148 | }
|
---|
| 149 | #endregion
|
---|
| 150 |
|
---|
| 151 | #region persistence
|
---|
| 152 | [Storable]
|
---|
[7694] | 153 | private string MultiLayerPerceptronEnsembleNetwork {
|
---|
[6577] | 154 | get {
|
---|
[7694] | 155 | string serializedNetwork;
|
---|
| 156 | alglib.mlpeserialize(this.mlpEnsemble, out serializedNetwork);
|
---|
| 157 | return serializedNetwork;
|
---|
[6577] | 158 | }
|
---|
| 159 | set {
|
---|
[7694] | 160 | alglib.mlpeunserialize(value, out this.mlpEnsemble);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | [Storable]
|
---|
| 165 | private double[] MultiLayerPerceptronEnsembleColumnMeans {
|
---|
| 166 | get { return mlpEnsemble.innerobj.columnmeans; }
|
---|
| 167 | set {
|
---|
[6580] | 168 | mlpEnsemble.innerobj.columnmeans = value;
|
---|
[7694] | 169 | mlpEnsemble.innerobj.network.columnmeans = value;
|
---|
[6577] | 170 | }
|
---|
| 171 | }
|
---|
| 172 | [Storable]
|
---|
[6580] | 173 | private double[] MultiLayerPerceptronEnsembleColumnSigmas {
|
---|
[7694] | 174 | get { return mlpEnsemble.innerobj.columnsigmas; }
|
---|
[6577] | 175 | set {
|
---|
[6580] | 176 | mlpEnsemble.innerobj.columnsigmas = value;
|
---|
[7694] | 177 | mlpEnsemble.innerobj.network.columnsigmas = value;
|
---|
[6577] | 178 | }
|
---|
| 179 | }
|
---|
[7694] | 180 | [Storable(AllowOneWay = true)]
|
---|
[6580] | 181 | private double[] MultiLayerPerceptronEnsembleDfdnet {
|
---|
[6577] | 182 | set {
|
---|
[7694] | 183 | mlpEnsemble.innerobj.network.dfdnet = value;
|
---|
[6577] | 184 | }
|
---|
| 185 | }
|
---|
| 186 | [Storable]
|
---|
[6580] | 187 | private int MultiLayerPerceptronEnsembleSize {
|
---|
[7694] | 188 | get { return mlpEnsemble.innerobj.ensemblesize; }
|
---|
[6577] | 189 | set {
|
---|
[6580] | 190 | mlpEnsemble.innerobj.ensemblesize = value;
|
---|
[7694] | 191 | mlpEnsemble.innerobj.ensemblesize = value;
|
---|
[6577] | 192 | }
|
---|
| 193 | }
|
---|
[7694] | 194 | [Storable(AllowOneWay = true)]
|
---|
[6580] | 195 | private double[] MultiLayerPerceptronEnsembleNeurons {
|
---|
[7694] | 196 | set { mlpEnsemble.innerobj.network.neurons = value; }
|
---|
[6577] | 197 | }
|
---|
[7694] | 198 | [Storable(AllowOneWay = true)]
|
---|
[6580] | 199 | private double[] MultiLayerPerceptronEnsembleSerializedMlp {
|
---|
| 200 | set {
|
---|
[7694] | 201 | mlpEnsemble.innerobj.network.dfdnet = value;
|
---|
[6580] | 202 | }
|
---|
| 203 | }
|
---|
[7694] | 204 | [Storable(AllowOneWay = true)]
|
---|
[6577] | 205 | private int[] MultiLayerPerceptronStuctinfo {
|
---|
| 206 | set {
|
---|
[7694] | 207 | mlpEnsemble.innerobj.network.structinfo = value;
|
---|
[6577] | 208 | }
|
---|
| 209 | }
|
---|
[6580] | 210 |
|
---|
| 211 | [Storable]
|
---|
| 212 | private double[] MultiLayerPerceptronWeights {
|
---|
| 213 | get {
|
---|
| 214 | return mlpEnsemble.innerobj.weights;
|
---|
| 215 | }
|
---|
| 216 | set {
|
---|
| 217 | mlpEnsemble.innerobj.weights = value;
|
---|
[7694] | 218 | mlpEnsemble.innerobj.network.weights = value;
|
---|
[6580] | 219 | }
|
---|
| 220 | }
|
---|
| 221 | [Storable]
|
---|
[6577] | 222 | private double[] MultiLayerPerceptronY {
|
---|
| 223 | get {
|
---|
[6580] | 224 | return mlpEnsemble.innerobj.y;
|
---|
[6577] | 225 | }
|
---|
| 226 | set {
|
---|
[6580] | 227 | mlpEnsemble.innerobj.y = value;
|
---|
[7694] | 228 | mlpEnsemble.innerobj.network.y = value;
|
---|
[6577] | 229 | }
|
---|
| 230 | }
|
---|
| 231 | #endregion
|
---|
| 232 | }
|
---|
| 233 | }
|
---|