1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Analysis;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
35 | [StorableClass]
|
---|
36 | [Item(Name = "BFGSAnalyzer", Description = "Analyzer to collect results for the BFGS algorithm.")]
|
---|
37 | public sealed class BFGSAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
38 | private const string PointParameterName = "Point";
|
---|
39 | private const string QualityGradientsParameterName = "QualityGradients";
|
---|
40 | private const string QualityParameterName = "Quality";
|
---|
41 | private const string ResultCollectionParameterName = "Results";
|
---|
42 | private const string QualitiesTableParameterName = "Qualities";
|
---|
43 | private const string PointsTableParameterName = "PointTable";
|
---|
44 | private const string QualityGradientsTableParameterName = "QualityGradientsTable";
|
---|
45 | private const string BFGSStateParameterName = "BFGSState";
|
---|
46 |
|
---|
47 | #region Parameter Properties
|
---|
48 | public ILookupParameter<DoubleArray> QualityGradientsParameter {
|
---|
49 | get { return (ILookupParameter<DoubleArray>)Parameters[QualityGradientsParameterName]; }
|
---|
50 | }
|
---|
51 | public ILookupParameter<DoubleArray> PointParameter {
|
---|
52 | get { return (ILookupParameter<DoubleArray>)Parameters[PointParameterName]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
55 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<ResultCollection> ResultCollectionParameter {
|
---|
58 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
|
---|
59 | }
|
---|
60 | public ILookupParameter<DataTable> QualitiesTableParameter {
|
---|
61 | get { return (ILookupParameter<DataTable>)Parameters[QualitiesTableParameterName]; }
|
---|
62 | }
|
---|
63 | public ILookupParameter<DataTable> PointsTableParameter {
|
---|
64 | get { return (ILookupParameter<DataTable>)Parameters[PointsTableParameterName]; }
|
---|
65 | }
|
---|
66 | public ILookupParameter<DataTable> QualityGradientsTableParameter {
|
---|
67 | get { return (ILookupParameter<DataTable>)Parameters[QualityGradientsTableParameterName]; }
|
---|
68 | }
|
---|
69 | public ILookupParameter<BFGSState> BFGSStateParameter {
|
---|
70 | get { return (ILookupParameter<BFGSState>)Parameters[BFGSStateParameterName]; }
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region Properties
|
---|
75 | private DoubleArray QualityGradients { get { return QualityGradientsParameter.ActualValue; } }
|
---|
76 | private DoubleArray Point { get { return PointParameter.ActualValue; } }
|
---|
77 | private DoubleValue Quality { get { return QualityParameter.ActualValue; } }
|
---|
78 | private ResultCollection ResultCollection { get { return ResultCollectionParameter.ActualValue; } }
|
---|
79 |
|
---|
80 | public bool EnabledByDefault {
|
---|
81 | get { return true; }
|
---|
82 | }
|
---|
83 |
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | [StorableConstructor]
|
---|
87 | private BFGSAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
88 | private BFGSAnalyzer(BFGSAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
89 | public BFGSAnalyzer()
|
---|
90 | : base() {
|
---|
91 | // in
|
---|
92 | Parameters.Add(new LookupParameter<DoubleArray>(PointParameterName, "The current point of the function to optimize."));
|
---|
93 | Parameters.Add(new LookupParameter<DoubleArray>(QualityGradientsParameterName, "The current gradients of the function to optimize."));
|
---|
94 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The current value of the function to optimize."));
|
---|
95 | Parameters.Add(new LookupParameter<DataTable>(QualitiesTableParameterName, "The table of all visited quality values."));
|
---|
96 | Parameters.Add(new LookupParameter<DataTable>(PointsTableParameterName, "The table of all visited points."));
|
---|
97 | Parameters.Add(new LookupParameter<DataTable>(QualityGradientsTableParameterName, "The table of all visited gradient values."));
|
---|
98 | Parameters.Add(new LookupParameter<BFGSState>(BFGSStateParameterName, "The state of the BFGS optimization algorithm."));
|
---|
99 | // in & out
|
---|
100 | Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, "The result collection of the algorithm."));
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
104 | return new BFGSAnalyzer(this, cloner);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public override IOperation Apply() {
|
---|
108 | if (BFGSStateParameter.ActualValue.State.xupdated) {
|
---|
109 | var f = Quality.Value;
|
---|
110 | var g = QualityGradients.ToArray();
|
---|
111 | var x = Point.ToArray();
|
---|
112 | var resultCollection = ResultCollection;
|
---|
113 |
|
---|
114 | // create and add tables on the first time
|
---|
115 | if (QualitiesTableParameter.ActualValue == null) {
|
---|
116 | QualitiesTableParameter.ActualValue = new DataTable(QualityParameter.ActualName);
|
---|
117 | PointsTableParameter.ActualValue = new DataTable(PointParameter.ActualName);
|
---|
118 | QualityGradientsTableParameter.ActualValue = new DataTable(QualityGradientsParameter.ActualName);
|
---|
119 |
|
---|
120 | QualitiesTableParameter.ActualValue.Rows.Add(new DataRow(QualityParameter.ActualName));
|
---|
121 |
|
---|
122 | resultCollection.Add(new Result(QualitiesTableParameter.ActualName,
|
---|
123 | QualitiesTableParameter.ActualValue));
|
---|
124 | resultCollection.Add(new Result(PointsTableParameter.ActualName,
|
---|
125 | PointsTableParameter.ActualValue));
|
---|
126 | resultCollection.Add(new Result(QualityGradientsTableParameter.ActualName,
|
---|
127 | QualityGradientsTableParameter.ActualValue));
|
---|
128 | resultCollection.Add(new Result(QualityParameter.ActualName, QualityParameter.ActualValue));
|
---|
129 | }
|
---|
130 |
|
---|
131 | // update
|
---|
132 | var functionValueRow = QualitiesTableParameter.ActualValue.Rows[QualityParameter.ActualName];
|
---|
133 | resultCollection[QualityParameter.ActualName].Value = Quality;
|
---|
134 | functionValueRow.Values.Add(f);
|
---|
135 |
|
---|
136 | AddValues(g, QualityGradientsTableParameter.ActualValue);
|
---|
137 | AddValues(x, PointsTableParameter.ActualValue);
|
---|
138 | }
|
---|
139 | return base.Apply();
|
---|
140 | }
|
---|
141 |
|
---|
142 | private void AddValues(double[] x, DataTable dataTable) {
|
---|
143 | if (!dataTable.Rows.Any()) {
|
---|
144 | for (int i = 0; i < x.Length; i++) {
|
---|
145 | var newRow = new DataRow("x" + i);
|
---|
146 | newRow.Values.Add(x[i]);
|
---|
147 | dataTable.Rows.Add(newRow);
|
---|
148 | }
|
---|
149 | } else {
|
---|
150 | for (int i = 0; i < x.Length; i++) {
|
---|
151 | dataTable.Rows.ElementAt(i).Values.Add(x[i]);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|