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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
|
---|
30 |
|
---|
31 | namespace 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 | protected Selector()
|
---|
63 | : base() {
|
---|
64 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which sub-scopes should be selected."));
|
---|
65 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
|
---|
66 | }
|
---|
67 |
|
---|
68 | public sealed override IOperation Apply() {
|
---|
69 | List<IScope> scopes = new List<IScope>(CurrentScope.SubScopes);
|
---|
70 |
|
---|
71 | if (GlobalCloneMap == null) {
|
---|
72 | var gScope = ExecutionContext.Scope;
|
---|
73 | while (gScope.Parent != null) gScope = gScope.Parent;
|
---|
74 | gScope.Variables.Add(new Variable(GlobalCloneMapParameterName, new CloneMapType()));
|
---|
75 | }
|
---|
76 | IScope[] selected = Select(scopes);
|
---|
77 |
|
---|
78 | CurrentScope.SubScopes.Clear();
|
---|
79 | IScope remainingScope = new Scope("Remaining");
|
---|
80 | remainingScope.SubScopes.AddRange(scopes);
|
---|
81 | CurrentScope.SubScopes.Add(remainingScope);
|
---|
82 | IScope selectedScope = new Scope("Selected");
|
---|
83 | selectedScope.SubScopes.AddRange(selected);
|
---|
84 | CurrentScope.SubScopes.Add(selectedScope);
|
---|
85 |
|
---|
86 | return base.Apply();
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected abstract IScope[] Select(List<IScope> scopes);
|
---|
90 | }
|
---|
91 | }
|
---|