Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Selection/3.3/Selector.cs @ 10372

Last change on this file since 10372 was 9835, checked in by bburlacu, 11 years ago

#1772: Merged remaining trunk changes into the EvolutionaryTracking branch.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
30
31namespace HeuristicLab.Selection {
32  /// <summary>
33  /// A base class for selection operators.
34  /// </summary>
35  [Item("Selector", "A base class for selection operators.")]
36  [StorableClass]
37  public abstract class Selector : SingleSuccessorOperator {
38    protected const string GlobalCloneMapParameterName = "GlobalCloneMap";
39
40    public override bool CanChangeName {
41      get { return false; }
42    }
43    protected ScopeParameter CurrentScopeParameter {
44      get { return (ScopeParameter)Parameters["CurrentScope"]; }
45    }
46
47    public IScope CurrentScope {
48      get { return CurrentScopeParameter.ActualValue; }
49    }
50
51    public LookupParameter<CloneMapType> GlobalCloneMapParameter {
52      get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
53    }
54
55    public CloneMapType GlobalCloneMap {
56      get { return GlobalCloneMapParameter.ActualValue; }
57    }
58    [StorableConstructor]
59    protected Selector(bool deserializing) : base(deserializing) { }
60    protected Selector(Selector original, Cloner cloner) : base(original, cloner) { }
61
62    [StorableHook(HookType.AfterDeserialization)]
63    private void AfterDeserialization() {
64      if (!Parameters.ContainsKey("CurrentScope"))
65        Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which sub-scopes should be selected."));
66      if (!Parameters.ContainsKey(GlobalCloneMapParameterName))
67        Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
68    }
69
70    protected Selector()
71      : base() {
72      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which sub-scopes should be selected."));
73      Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
74    }
75
76    public sealed override IOperation Apply() {
77      List<IScope> scopes = new List<IScope>(CurrentScope.SubScopes);
78
79      if (GlobalCloneMap == null) {
80        var gScope = ExecutionContext.Scope;
81        while (gScope.Parent != null) gScope = gScope.Parent;
82        gScope.Variables.Add(new Variable(GlobalCloneMapParameterName, new CloneMapType()));
83      }
84      IScope[] selected = Select(scopes);
85
86      CurrentScope.SubScopes.Clear();
87      IScope remainingScope = new Scope("Remaining");
88      remainingScope.SubScopes.AddRange(scopes);
89      CurrentScope.SubScopes.Add(remainingScope);
90      IScope selectedScope = new Scope("Selected");
91      selectedScope.SubScopes.AddRange(selected);
92      CurrentScope.SubScopes.Add(selectedScope);
93
94      return base.Apply();
95    }
96
97    protected abstract IScope[] Select(List<IScope> scopes);
98  }
99}
Note: See TracBrowser for help on using the repository browser.