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.Collections;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization.Operators {
|
---|
31 | /// <summary>
|
---|
32 | /// An operator which collects the actual values of parameters and adds them to a collection of results.
|
---|
33 | /// </summary>
|
---|
34 | [Item("ResultsCollector", "An operator which collects the actual values of parameters and adds them to a collection of results.")]
|
---|
35 | [StorableClass("4D8A214A-C0E4-4450-8179-53B15D9DC4FC")]
|
---|
36 | public class ResultsCollector : ValuesCollector {
|
---|
37 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
38 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
39 | }
|
---|
40 | public ValueParameter<BoolValue> CopyValueParameter {
|
---|
41 | get { return (ValueParameter<BoolValue>)Parameters["CopyValue"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public BoolValue CopyValue {
|
---|
45 | get { return CopyValueParameter.Value; }
|
---|
46 | set { CopyValueParameter.Value = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected ResultsCollector(bool deserializing) : base(deserializing) { }
|
---|
51 | protected ResultsCollector(ResultsCollector original, Cloner cloner) : base(original, cloner) { }
|
---|
52 | public ResultsCollector()
|
---|
53 | : base() {
|
---|
54 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the collected values should be stored."));
|
---|
55 | Parameters.Add(new ValueParameter<BoolValue>("CopyValue", "True if the collected result value should be copied, otherwise false.", new BoolValue(false)));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new ResultsCollector(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override IOperation Apply() {
|
---|
63 | bool copy = CopyValueParameter.Value.Value;
|
---|
64 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
65 | IResult result;
|
---|
66 | foreach (IParameter param in CollectedValues) {
|
---|
67 | IItem value = param.ActualValue;
|
---|
68 | if (value != null) {
|
---|
69 | ILookupParameter lookupParam = param as ILookupParameter;
|
---|
70 | string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
|
---|
71 |
|
---|
72 | IScopeTreeLookupParameter scopeTreeLookupParam = param as IScopeTreeLookupParameter;
|
---|
73 | if ((scopeTreeLookupParam != null) && (scopeTreeLookupParam.Depth == 0)) {
|
---|
74 | IEnumerator enumerator = ((IEnumerable)value).GetEnumerator();
|
---|
75 | if (enumerator.MoveNext())
|
---|
76 | value = (IItem)enumerator.Current;
|
---|
77 | }
|
---|
78 |
|
---|
79 | results.TryGetValue(name, out result);
|
---|
80 | if (result != null)
|
---|
81 | result.Value = copy ? (IItem)value.Clone() : value;
|
---|
82 | else
|
---|
83 | results.Add(new Result(name, param.Description, copy ? (IItem)value.Clone() : value));
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return base.Apply();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|