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