[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[4068] | 26 | using HeuristicLab.Parameters;
|
---|
[16559] | 27 | using HEAL.Attic;
|
---|
[2] | 28 |
|
---|
[3413] | 29 | namespace HeuristicLab.Selection {
|
---|
| 30 | [Item("ConditionalSelector", "Selects sub-scopes where a certain boolean variable is true.")]
|
---|
[16462] | 31 | [StorableType("ACF05DDC-50DC-41CF-BD5D-CF89AD309A38")]
|
---|
[3413] | 32 | public class ConditionalSelector : Selector {
|
---|
[3659] | 33 | public ScopeTreeLookupParameter<BoolValue> ConditionParameter {
|
---|
| 34 | get { return (ScopeTreeLookupParameter<BoolValue>)Parameters["Condition"]; }
|
---|
[2] | 35 | }
|
---|
[6051] | 36 | protected IValueParameter<BoolValue> CopySelectedParameter {
|
---|
| 37 | get { return (IValueParameter<BoolValue>)Parameters["CopySelected"]; }
|
---|
[3413] | 38 | }
|
---|
[2] | 39 |
|
---|
[3413] | 40 | public BoolValue CopySelected {
|
---|
| 41 | get { return CopySelectedParameter.Value; }
|
---|
| 42 | set { CopySelectedParameter.Value = value; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[4722] | 45 | [StorableConstructor]
|
---|
[16462] | 46 | protected ConditionalSelector(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 47 | protected ConditionalSelector(ConditionalSelector original, Cloner cloner)
|
---|
| 48 | : base(original, cloner) {
|
---|
| 49 | }
|
---|
| 50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 51 | return new ConditionalSelector(this, cloner);
|
---|
| 52 | }
|
---|
[2] | 53 | public ConditionalSelector()
|
---|
| 54 | : base() {
|
---|
[3659] | 55 | Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("Condition", "The boolean variable based on which the scopes are selected into a true scope-branch and a false scope-branch."));
|
---|
[3413] | 56 | Parameters.Add(new ValueParameter<BoolValue>("CopySelected", "The parameter that decides whether the selected scopes should be copied or moved.", new BoolValue(true)));
|
---|
[6051] | 57 | CopySelectedParameter.Hidden = true;
|
---|
[2] | 58 | }
|
---|
| 59 |
|
---|
[3413] | 60 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
| 61 | ItemArray<BoolValue> conditions = ConditionParameter.ActualValue;
|
---|
| 62 | List<IScope> selected = new List<IScope>();
|
---|
| 63 | if (CopySelected.Value) {
|
---|
| 64 | for (int i = 0; i < scopes.Count; i++) {
|
---|
| 65 | if (conditions[i].Value) {
|
---|
| 66 | selected.Add((IScope)scopes[i].Clone());
|
---|
[2] | 67 | }
|
---|
| 68 | }
|
---|
| 69 | } else {
|
---|
[3413] | 70 | for (int i = 0; i < scopes.Count; i++) {
|
---|
| 71 | if (conditions[i + selected.Count].Value) {
|
---|
| 72 | selected.Add(scopes[i]);
|
---|
| 73 | scopes.RemoveAt(i);
|
---|
| 74 | i--;
|
---|
| 75 | }
|
---|
[2] | 76 | }
|
---|
| 77 | }
|
---|
[3413] | 78 | return selected.ToArray();
|
---|
[2] | 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|