Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3484 was 3413, checked in by abeham, 15 years ago

Updated Gender Specific Selection, Offspring Selector and other changes related to the OSGA #976
Removed old OS plugin, removed SelectedSubScopesLookupParameter

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