[11961] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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;
|
---|
[11396] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
[11949] | 32 | namespace HeuristicLab.Optimization {
|
---|
[11396] | 33 | [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")]
|
---|
| 34 | [StorableClass]
|
---|
[13336] | 35 | public sealed class SingleObjectiveAnalyzer<TSolution> : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator<TSolution>, IAnalyzer, IStochasticOperator
|
---|
| 36 | where TSolution : class, ISolution {
|
---|
[11396] | 37 | public bool EnabledByDefault { get { return true; } }
|
---|
| 38 |
|
---|
[13336] | 39 | public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
|
---|
| 40 | get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
|
---|
[11396] | 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 |
|
---|
[11880] | 51 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 52 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 53 | }
|
---|
[11739] | 54 |
|
---|
[13336] | 55 | public Action<TSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
|
---|
[11880] | 56 |
|
---|
[11396] | 57 | [StorableConstructor]
|
---|
[11739] | 58 | private SingleObjectiveAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[13336] | 59 | private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer<TSolution> original, Cloner cloner) : base(original, cloner) { }
|
---|
[11396] | 60 | public SingleObjectiveAnalyzer() {
|
---|
[13336] | 61 | Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
|
---|
[11396] | 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."));
|
---|
[11880] | 64 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
[11396] | 65 | }
|
---|
| 66 |
|
---|
| 67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13336] | 68 | return new SingleObjectiveAnalyzer<TSolution>(this, cloner);
|
---|
[11396] | 69 | }
|
---|
| 70 |
|
---|
| 71 | public override IOperation Apply() {
|
---|
[11598] | 72 | var encoding = EncodingParameter.ActualValue;
|
---|
[11396] | 73 | var results = ResultsParameter.ActualValue;
|
---|
[11880] | 74 | var random = RandomParameter.ActualValue;
|
---|
[11396] | 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 |
|
---|
[13336] | 80 | var individuals = scopes.Select(s => ScopeUtil.GetSolution(s, encoding)).ToArray();
|
---|
[11880] | 81 | AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random);
|
---|
[11396] | 82 | return base.Apply();
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|