Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Selection/3.3/LeftSelector.cs @ 17223

Last change on this file since 17223 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.0 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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
[2830]22using System.Collections.Generic;
[4722]23using HeuristicLab.Common;
[2]24using HeuristicLab.Core;
[4043]25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
[17097]27using HEAL.Attic;
[2]28
29namespace HeuristicLab.Selection {
[817]30  /// <summary>
[2805]31  /// An operator which selects sub-scopes from left to right.
[817]32  /// </summary>
[2805]33  [Item("LeftSelector", "An operator which selects sub-scopes from left to right.")]
[17097]34  [StorableType("7D249BF4-0D6A-4AED-9F1A-128F3BEF66B4")]
[2805]35  public sealed class LeftSelector : Selector {
[6051]36    private IValueParameter<BoolValue> CopySelectedParameter {
37      get { return (IValueParameter<BoolValue>)Parameters["CopySelected"]; }
[4043]38    }
39    public IValueLookupParameter<IntValue> NumberOfSelectedSubScopesParameter {
40      get { return (ValueLookupParameter<IntValue>)Parameters["NumberOfSelectedSubScopes"]; }
41    }
[2]42
[4043]43    public BoolValue CopySelected {
44      get { return CopySelectedParameter.Value; }
45      set { CopySelectedParameter.Value = value; }
46    }
47
[4722]48    [StorableConstructor]
[17097]49    private LeftSelector(StorableConstructorFlag _) : base(_) { }
[4722]50    private LeftSelector(LeftSelector original, Cloner cloner)
51      : base(original, cloner) {
52    }
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new LeftSelector(this, cloner);
55    }
56
[4043]57    public LeftSelector()
58      : base() {
59      Parameters.Add(new ValueParameter<BoolValue>("CopySelected", "True if the selected sub-scopes should be copied, otherwise false.", new BoolValue(true)));
60      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSelectedSubScopes", "The number of sub-scopes which should be selected."));
[6051]61      CopySelectedParameter.Hidden = true;
[4043]62    }
63
[2830]64    protected override IScope[] Select(List<IScope> scopes) {
[2805]65      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
66      bool copy = CopySelectedParameter.Value.Value;
[2830]67      IScope[] selected = new IScope[count];
[2805]68
69      int j = 0;
70      for (int i = 0; i < count; i++) {
71        if (copy) {
[2830]72          selected[i] = (IScope)scopes[j].Clone();
[2805]73          j++;
[2817]74          if (j >= scopes.Count) j = 0;
[2]75        } else {
[2830]76          selected[i] = scopes[0];
[2817]77          scopes.RemoveAt(0);
[2]78        }
79      }
[2817]80      return selected;
[2]81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.