1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Selection;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.EvolutionStrategy {
|
---|
32 | [Item("WithoutRepeatingBatchedRandomSelector", "Selects m batches of n parents where in each batch the n parents are drawn without repeating.")]
|
---|
33 | [StorableClass]
|
---|
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 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected WithoutRepeatingBatchedRandomSelector(bool deserializing) : base(deserializing) { }
|
---|
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 |
|
---|
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 | }
|
---|