Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.EvolutionStrategy/3.3/WithoutRepeatingBatchedRandomSelector.cs @ 17507

Last change on this file since 17507 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.7 KB
RevLine 
[3130]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3130]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;
[4722]24using HeuristicLab.Common;
[3130]25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
[16565]28using HEAL.Attic;
[3130]29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.EvolutionStrategy {
32  [Item("WithoutRepeatingBatchedRandomSelector", "Selects m batches of n parents where in each batch the n parents are drawn without repeating.")]
[16565]33  [StorableType("FF9A96DC-8B6D-40D2-99A2-D91A4C990E15")]
[3130]34  public class WithoutRepeatingBatchedRandomSelector : StochasticSelector {
35    public override bool CanChangeName {
36      get { return true; }
37    }
38    public IValueLookupParameter<IntValue> ChildrenParameter {
39      get { return (IValueLookupParameter<IntValue>)Parameters["Children"]; }
40    }
41    public IValueLookupParameter<IntValue> ParentsPerChildParameter {
42      get { return (IValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
43    }
44
[4722]45    [StorableConstructor]
[16565]46    protected WithoutRepeatingBatchedRandomSelector(StorableConstructorFlag _) : base(_) { }
[4722]47    protected WithoutRepeatingBatchedRandomSelector(WithoutRepeatingBatchedRandomSelector original, Cloner cloner)
48      : base(original, cloner) {
49    }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new WithoutRepeatingBatchedRandomSelector(this, cloner);
52    }
53
[3130]54    public WithoutRepeatingBatchedRandomSelector()
55      : base() {
56      Parameters.Add(new ValueLookupParameter<IntValue>("Children", "The number of children to select (number of batches)."));
57      Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", "The number of parents per child (size of each batch)."));
58    }
59
60    protected override IScope[] Select(List<IScope> scopes) {
61      IRandom random = RandomParameter.ActualValue;
62      int children = ChildrenParameter.ActualValue.Value;
63      int parents = ParentsPerChildParameter.ActualValue.Value;
64      int parentsAvailable = scopes.Count;
65
66      if (parents > parentsAvailable)
67        throw new InvalidOperationException("WithoutRepeatingBatchedRandomSelector: Cannot select more parents per child than there are parents available");
68
69      IScope[] result = new IScope[children * parents];
70      int count = 0;
71      HashSet<int> selectedParents = new HashSet<int>();
72      for (int i = 0; i < children; i++) {
73        selectedParents.Clear();
74        for (int j = 0; j < parents; j++) {
75          int nextParent = j; // will be used in case parents == parentsAvailable
76          if (parents < parentsAvailable) {
77            do {
78              nextParent = random.Next(parentsAvailable);
79            } while (selectedParents.Contains(nextParent));
80          }
81
82          result[count++] = (IScope)scopes[nextParent].Clone();
83          selectedParents.Add(nextParent);
84        }
85      }
86
87      return result;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.