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