Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Algorithms.GradientDescent/HeuristicLab.Algorithms.GradientDescent/3.3/LevenbergMarquardtMove.cs @ 7332

Last change on this file since 7332 was 5520, checked in by gkronber, 14 years ago

#1423 Fixed problem with progress reporting and stored quality values in a datatable.

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
21using System.Linq;
22
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.TestFunctions;
32using HeuristicLab.Analysis;
33
34namespace HeuristicLab.Algorithms.GradientDescent {
35  public class LevenbergMarquardtMove : SingleSuccessorOperator {
36    private const string EvaluatorParameterName = "Evaluator";
37    private const string ResultsParameterName = "Results";
38    private const string RealVectorParameterName = "RealVector";
39    private const string QualityParameterName = "Quality";
40
41    private const string QualityResultsName = "Quality";
42
43
44    public ILookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
45      get { return (ILookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters[EvaluatorParameterName]; }
46    }
47    public ILookupParameter<RealVector> RealVectorParameter {
48      get { return (ILookupParameter<RealVector>)Parameters[RealVectorParameterName]; }
49    }
50    public ILookupParameter<ResultCollection> ResultParameter {
51      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
52    }
53    public ILookupParameter<DoubleValue> QualityParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
55    }
56
57    [StorableConstructor]
58    protected LevenbergMarquardtMove(bool deserializing) : base(deserializing) { }
59    protected LevenbergMarquardtMove(LevenbergMarquardtMove original, Cloner cloner)
60      : base(original, cloner) {
61    }
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new LevenbergMarquardtMove(this, cloner);
64    }
65
66    public LevenbergMarquardtMove()
67      : base() {
68      Parameters.Add(new LookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>(EvaluatorParameterName, ""));
69      Parameters.Add(new LookupParameter<RealVector>(RealVectorParameterName, ""));
70      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, ""));
71      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, ""));
72    }
73
74    public override IOperation Apply() {
75      double epsg = 0;
76      double epsf = 0;
77      double epsx = 0;
78      int maxits = 0;
79      double diffstep = .1;
80
81      double[] solution = RealVectorParameter.ActualValue.ToArray();
82
83      alglib.minlmstate state;
84      alglib.minlmreport report;
85     
86      alglib.minlmcreatev(solution.Length, solution, diffstep, out state);
87      alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);
88      alglib.minlmsetxrep(state, true);
89      alglib.minlmoptimize(state, CreateCallBack(EvaluatorParameter.ActualValue), CreateReportProgress(ResultParameter.ActualValue), null);
90      alglib.minlmresults(state, out solution, out report);
91
92      RealVectorParameter.ActualValue = new RealVector(solution);
93      return base.Apply();
94    }
95
96    private alglib.ndimensional_fvec CreateCallBack(ISingleObjectiveTestFunctionProblemEvaluator evaluator) {
97      return (double[] arg, double[] fi, object obj) => {
98        RealVector r = new RealVector(arg);
99        RealVectorParameter.ActualValue = r;
100
101        IExecutionContext context = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
102        evaluator.Execute(context, CancellationToken);
103        QualityParameter.ExecutionContext = context;
104        fi[0] = QualityParameter.ActualValue.Value;
105      };
106    }
107
108    private alglib.ndimensional_rep CreateReportProgress(ResultCollection results) {
109      return (double[] arg, double func, object obj) => {
110        if (!results.ContainsKey(QualityResultsName)) {
111          DataTable table = new DataTable(QualityResultsName);
112          table.Rows.Add(new DataRow("Quality"));
113          results.Add(new Result(QualityResultsName, table));
114        }
115
116        DataTable resultsTable = (DataTable)results[QualityResultsName].Value;
117        resultsTable.Rows["Quality"].Values.Add(func);
118      };
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.