[2805] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2805] | 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 |
|
---|
[2830] | 22 | using System.Collections.Generic;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2805] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[9835] | 29 | using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
|
---|
| 30 |
|
---|
[2805] | 31 | namespace HeuristicLab.Selection {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// A base class for selection operators.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("Selector", "A base class for selection operators.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[3129] | 37 | public abstract class Selector : SingleSuccessorOperator {
|
---|
[9835] | 38 | protected const string GlobalCloneMapParameterName = "GlobalCloneMap";
|
---|
| 39 |
|
---|
[3034] | 40 | public override bool CanChangeName {
|
---|
| 41 | get { return false; }
|
---|
| 42 | }
|
---|
[2805] | 43 | protected ScopeParameter CurrentScopeParameter {
|
---|
| 44 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public IScope CurrentScope {
|
---|
| 48 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[9835] | 51 | public LookupParameter<CloneMapType> GlobalCloneMapParameter {
|
---|
| 52 | get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public CloneMapType GlobalCloneMap {
|
---|
| 56 | get { return GlobalCloneMapParameter.ActualValue; }
|
---|
| 57 | }
|
---|
[4722] | 58 | [StorableConstructor]
|
---|
| 59 | protected Selector(bool deserializing) : base(deserializing) { }
|
---|
| 60 | protected Selector(Selector original, Cloner cloner) : base(original, cloner) { }
|
---|
| 61 |
|
---|
[9835] | 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 |
|
---|
[2805] | 70 | protected Selector()
|
---|
| 71 | : base() {
|
---|
| 72 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which sub-scopes should be selected."));
|
---|
[9835] | 73 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
|
---|
[2805] | 74 | }
|
---|
| 75 |
|
---|
[2834] | 76 | public sealed override IOperation Apply() {
|
---|
[2830] | 77 | List<IScope> scopes = new List<IScope>(CurrentScope.SubScopes);
|
---|
[9835] | 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 | }
|
---|
[2830] | 84 | IScope[] selected = Select(scopes);
|
---|
[2805] | 85 |
|
---|
| 86 | CurrentScope.SubScopes.Clear();
|
---|
[2817] | 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);
|
---|
[2805] | 93 |
|
---|
| 94 | return base.Apply();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[2830] | 97 | protected abstract IScope[] Select(List<IScope> scopes);
|
---|
[2805] | 98 | }
|
---|
| 99 | }
|
---|