1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Threading;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Selection {
|
---|
12 | /// <summary>
|
---|
13 | /// A selector which tries to select two parents which differ in quality.
|
---|
14 | /// </summary>
|
---|
15 | [Item("NoSameMatesSelector", "A selector which tries to select two parents which differ in quality.")]
|
---|
16 | [StorableClass]
|
---|
17 | public class NoSameMatesSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
|
---|
18 | private const string SelectorParameterName = "Selector";
|
---|
19 | private const string QualityDifferencePercentageParameterName = "QualityDifferencePercentage";
|
---|
20 | private const string QualityDifferenceMaxAttemptsParameterName = "QualityDifferenceMaxAttempts";
|
---|
21 |
|
---|
22 | #region Parameters
|
---|
23 | public IValueParameter<ISelector> SelectorParameter {
|
---|
24 | get { return (IValueParameter<ISelector>)Parameters[SelectorParameterName]; }
|
---|
25 | }
|
---|
26 | public IValueParameter<PercentValue> QualityDifferencePercentageParameter {
|
---|
27 | get { return (IValueParameter<PercentValue>)Parameters[QualityDifferencePercentageParameterName]; }
|
---|
28 | }
|
---|
29 | public IValueParameter<IntValue> QualityDifferenceMaxAttemptsParameter {
|
---|
30 | get { return (IValueParameter<IntValue>)Parameters[QualityDifferenceMaxAttemptsParameterName]; }
|
---|
31 | }
|
---|
32 | #endregion
|
---|
33 |
|
---|
34 | #region Properties
|
---|
35 | public IntValue NumberOfSelectedSubScopes {
|
---|
36 | get { return NumberOfSelectedSubScopesParameter.ActualValue; }
|
---|
37 | }
|
---|
38 | public ISelector Selector {
|
---|
39 | get { return SelectorParameter.Value; }
|
---|
40 | set { SelectorParameter.Value = value; }
|
---|
41 | }
|
---|
42 | public PercentValue QualityDifferencePercentage {
|
---|
43 | get { return QualityDifferencePercentageParameter.Value; }
|
---|
44 | }
|
---|
45 | public IntValue QualityDifferenceMaxAttempts {
|
---|
46 | get { return QualityDifferenceMaxAttemptsParameter.Value; }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected NoSameMatesSelector(bool deserializing) : base(deserializing) { }
|
---|
52 | protected NoSameMatesSelector(NoSameMatesSelector original, Cloner cloner) : base(original, cloner) { }
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new NoSameMatesSelector(this, cloner);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public NoSameMatesSelector() : base() {
|
---|
58 | #region Create parameters
|
---|
59 | Parameters.Add(new ValueParameter<ISelector>(SelectorParameterName, "The inner selection operator to select the parents."));
|
---|
60 | Parameters.Add(new ValueParameter<PercentValue>(QualityDifferencePercentageParameterName, "The minimum quality difference from parent1 to parent2 to accept the selection.", new PercentValue(0.05)));
|
---|
61 | Parameters.Add(new ValueParameter<IntValue>(QualityDifferenceMaxAttemptsParameterName, "The maximum number of attempts to find parents which differ in quality.", new IntValue(5)));
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | Initialize();
|
---|
65 | }
|
---|
66 |
|
---|
67 | [StorableHook(HookType.AfterDeserialization)]
|
---|
68 | private void AfterDeserialization() {
|
---|
69 | Initialize();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
73 | int count = NumberOfSelectedSubScopes.Value;
|
---|
74 | if (count % 2 > 0) throw new InvalidOperationException(Name + ": There must be an equal number of sub-scopes to be selected.");
|
---|
75 | IScope[] selected = new IScope[count];
|
---|
76 |
|
---|
77 | double qualityDifferencePercentage = QualityDifferencePercentage.Value;
|
---|
78 | int qualityDifferenceMaxAttempts = QualityDifferenceMaxAttempts.Value;
|
---|
79 |
|
---|
80 | int attempts = 1;
|
---|
81 | int j = 0;
|
---|
82 | while (j < count - 1) { // repeat until enough parents are selected
|
---|
83 | ApplyInnerSelector();
|
---|
84 | ScopeList parents = CurrentScope.SubScopes[1].SubScopes;
|
---|
85 |
|
---|
86 | for (int i = 0; j < count - 1 && i < parents.Count / 2; i++) {
|
---|
87 | double qualityParent1 = ((DoubleValue)parents[i * 2].Variables[QualityParameter.ActualName].Value).Value;
|
---|
88 | double qualityParent2 = ((DoubleValue)parents[i * 2 + 1].Variables[QualityParameter.ActualName].Value).Value;
|
---|
89 |
|
---|
90 | bool parentsDifferent = (qualityParent2 > qualityParent1 * (1.0 + qualityDifferencePercentage) ||
|
---|
91 | qualityParent2 < qualityParent1 * (1.0 - qualityDifferencePercentage));
|
---|
92 | // parents meet difference criterion or max attempts reached
|
---|
93 | if (attempts >= qualityDifferenceMaxAttempts || parentsDifferent) {
|
---|
94 |
|
---|
95 | // inner selector already copied scopes
|
---|
96 | selected[j++] = parents[i * 2];
|
---|
97 | selected[j++] = parents[i * 2 + 1];
|
---|
98 | if (!CopySelected.Value) {
|
---|
99 | scopes.Remove(parents[i * 2]);
|
---|
100 | scopes.Remove(parents[i * 2 + 1]);
|
---|
101 | }
|
---|
102 | attempts = 1;
|
---|
103 | } else { // skip parents
|
---|
104 | attempts++;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | // modify scopes
|
---|
108 | ScopeList remaining = CurrentScope.SubScopes[0].SubScopes;
|
---|
109 | CurrentScope.SubScopes.Clear();
|
---|
110 | CurrentScope.SubScopes.AddRange(remaining);
|
---|
111 | }
|
---|
112 | return selected;
|
---|
113 | }
|
---|
114 |
|
---|
115 | #region Events
|
---|
116 | private void SelectorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
117 | IValueParameter<ISelector> selectorParam = (sender as IValueParameter<ISelector>);
|
---|
118 | if (selectorParam != null)
|
---|
119 | ParameterizeSelector(selectorParam.Value);
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 |
|
---|
123 | #region Helpers
|
---|
124 | private void ApplyInnerSelector() {
|
---|
125 | IOperation next;
|
---|
126 | IAtomicOperation operation;
|
---|
127 | OperationCollection coll;
|
---|
128 | Stack<IOperation> executionStack = new Stack<IOperation>();
|
---|
129 |
|
---|
130 | executionStack.Push(ExecutionContext.CreateChildOperation(Selector));
|
---|
131 | while (executionStack.Count > 0) {
|
---|
132 | CancellationToken.ThrowIfCancellationRequested();
|
---|
133 | next = executionStack.Pop();
|
---|
134 | if (next is OperationCollection) {
|
---|
135 | coll = (OperationCollection)next;
|
---|
136 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
137 | if (coll[i] != null) executionStack.Push(coll[i]);
|
---|
138 | } else if (next is IAtomicOperation) {
|
---|
139 | operation = (IAtomicOperation)next;
|
---|
140 | next = operation.Operator.Execute((IExecutionContext)operation, CancellationToken);
|
---|
141 | if (next != null) executionStack.Push(next);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void Initialize() {
|
---|
147 | SelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged);
|
---|
148 | if (Selector == null) Selector = new TournamentSelector();
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void ParameterizeSelector(ISelector selector) {
|
---|
152 | selector.CopySelected = new BoolValue(true); // must always be true
|
---|
153 | IStochasticOperator stoOp = (selector as IStochasticOperator);
|
---|
154 | if (stoOp != null) stoOp.RandomParameter.ActualName = RandomParameter.Name;
|
---|
155 | ISingleObjectiveSelector soSelector = (selector as ISingleObjectiveSelector);
|
---|
156 | if (soSelector != null) {
|
---|
157 | soSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
158 | soSelector.QualityParameter.ActualName = QualityParameter.Name;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | #endregion
|
---|
162 | }
|
---|
163 | }
|
---|