Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7800 was 7779, checked in by bburlacu, 13 years ago

#1772: Implemented new View, improved functionality (tracking of fragments and operators)

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