source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs @ 17577

Last change on this file since 17577 was 17577, checked in by abeham, 2 years ago

#2521: remove scope-based contexts, add helper methods to ScopeUtil, adapt operators

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Optimization {
33  [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")]
34  [StorableType("3D20F8E2-CE11-4021-A05B-CFCB02C0FD6F")]
35  internal sealed class SingleObjectiveAnalyzer<TEncodedSolution> : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator<TEncodedSolution>, IAnalyzer, IStochasticOperator
36  where TEncodedSolution : class, IEncodedSolution {
37    public bool EnabledByDefault { get { return true; } }
38
39    public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter {
40      get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; }
41    }
42
43    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
44      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
45    }
46
47    public ILookupParameter<ResultCollection> ResultsParameter {
48      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
49    }
50
51    public ILookupParameter<IRandom> RandomParameter {
52      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
53    }
54
55    public Action<ISingleObjectiveSolutionContext<TEncodedSolution>[], ResultCollection, IRandom> Analyze { get; set; }
56
57    [StorableConstructor]
58    private SingleObjectiveAnalyzer(StorableConstructorFlag _) : base(_) { }
59    private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
60    public SingleObjectiveAnalyzer() {
61      Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding."));
62      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
63      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to."));
64      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new SingleObjectiveAnalyzer<TEncodedSolution>(this, cloner);
69    }
70
71    public override IOperation Apply() {
72      var encoding = EncodingParameter.ActualValue;
73      var results = ResultsParameter.ActualValue;
74      var random = RandomParameter.ActualValue;
75
76      IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope };
77      for (var i = 0; i < QualityParameter.Depth; i++)
78        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
79
80      var solutionContexts = scopes.Select(scope => {
81        return ScopeUtil.CreateSolutionContext(scope, encoding);
82      }).ToArray();
83
84      Analyze(solutionContexts, results, random);
85      foreach (var s in solutionContexts.Zip(scopes, Tuple.Create)) {
86        ScopeUtil.CopyToScope(s.Item2, s.Item1);
87      }
88      return base.Apply();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.