Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Operators/3.3/ResultsCollector.cs @ 4040

Last change on this file since 4040 was 3690, checked in by swagner, 14 years ago

Fixed bug in DataTableValuesCollector and ResultsCollector (#999)

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