Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/ConditionalSelector.cs @ 4401

Last change on this file since 4401 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Selection {
29  [Item("ConditionalSelector", "Selects sub-scopes where a certain boolean variable is true.")]
30  [StorableClass]
31  public class ConditionalSelector : Selector {
32    public ScopeTreeLookupParameter<BoolValue> ConditionParameter {
33      get { return (ScopeTreeLookupParameter<BoolValue>)Parameters["Condition"]; }
34    }
35    public ValueParameter<BoolValue> CopySelectedParameter {
36      get { return (ValueParameter<BoolValue>)Parameters["CopySelected"]; }
37    }
38
39    public BoolValue CopySelected {
40      get { return CopySelectedParameter.Value; }
41      set { CopySelectedParameter.Value = value; }
42    }
43
44    public ConditionalSelector()
45      : base() {
46      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."));
47      Parameters.Add(new ValueParameter<BoolValue>("CopySelected", "The parameter that decides whether the selected scopes should be copied or moved.", new BoolValue(true)));
48    }
49
50    protected override IScope[] Select(List<IScope> scopes) {
51      ItemArray<BoolValue> conditions = ConditionParameter.ActualValue;
52      List<IScope> selected = new List<IScope>();
53      if (CopySelected.Value) {
54        for (int i = 0; i < scopes.Count; i++) {
55          if (conditions[i].Value) {
56            selected.Add((IScope)scopes[i].Clone());
57          }
58        }
59      } else {
60        for (int i = 0; i < scopes.Count; i++) {
61          if (conditions[i + selected.Count].Value) {
62            selected.Add(scopes[i]);
63            scopes.RemoveAt(i);
64            i--;
65          }
66        }
67      }
68      return selected.ToArray();
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.