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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Selection {
|
---|
29 | public class ProportionalSelector : StochasticSelectorBase {
|
---|
30 | public override string Description {
|
---|
31 | get { return @"TODO\r\nOperator description still missing ..."; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public ProportionalSelector() {
|
---|
35 | AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
|
---|
36 | AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
|
---|
37 | AddVariableInfo(new VariableInfo("Windowing", "Apply windowing strategy (selection probability is proportional to the quality differences and not to the total quality)", typeof(BoolData), VariableKind.In));
|
---|
38 | GetVariableInfo("Windowing").Local = true;
|
---|
39 | AddVariable(new Variable("Windowing", new BoolData(true)));
|
---|
40 | GetVariable("CopySelected").GetValue<BoolData>().Data = true;
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
|
---|
44 | bool maximization = GetVariableValue<BoolData>("Maximization", source, true).Data;
|
---|
45 | IVariableInfo qualityInfo = GetVariableInfo("Quality");
|
---|
46 | bool windowing = GetVariableValue<BoolData>("Windowing", source, true).Data;
|
---|
47 |
|
---|
48 | double[] qualities;
|
---|
49 | double qualitySum;
|
---|
50 | double selectedQuality;
|
---|
51 | double sum;
|
---|
52 | int j;
|
---|
53 |
|
---|
54 | GenerateQualitiesArray(source, maximization, qualityInfo, windowing, out qualities, out qualitySum);
|
---|
55 |
|
---|
56 | // perform selection
|
---|
57 | for (int i = 0; i < selected; i++) {
|
---|
58 | selectedQuality = random.NextDouble() * qualitySum;
|
---|
59 | sum = 0;
|
---|
60 | j = 0;
|
---|
61 | while ((j < qualities.Length) && (sum <= selectedQuality)) {
|
---|
62 | sum += qualities[j];
|
---|
63 | j++;
|
---|
64 | }
|
---|
65 | IScope selectedScope = source.SubScopes[j - 1];
|
---|
66 | if (copySelected)
|
---|
67 | target.AddSubScope((IScope)selectedScope.Clone());
|
---|
68 | else {
|
---|
69 | source.RemoveSubScope(selectedScope);
|
---|
70 | target.AddSubScope(selectedScope);
|
---|
71 | GenerateQualitiesArray(source, maximization, qualityInfo, windowing, out qualities, out qualitySum);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void GenerateQualitiesArray(IScope source, bool maximization, IVariableInfo qualityInfo, bool windowing, out double[] qualities, out double qualitySum) {
|
---|
77 | int subScopes = source.SubScopes.Count;
|
---|
78 | qualities = new double[subScopes];
|
---|
79 | qualitySum = 0;
|
---|
80 |
|
---|
81 | if (subScopes < 1) throw new InvalidOperationException("No source scopes to select available.");
|
---|
82 |
|
---|
83 | double best = source.SubScopes[0].GetVariableValue<DoubleData>(qualityInfo.ActualName, false).Data;
|
---|
84 | double worst = source.SubScopes[subScopes - 1].GetVariableValue<DoubleData>(qualityInfo.ActualName, false).Data;
|
---|
85 | double limit = Math.Min(worst * 2, double.MaxValue);
|
---|
86 | double min = Math.Min(best, worst);
|
---|
87 | double max = Math.Max(best, worst);
|
---|
88 | double solutionQuality;
|
---|
89 |
|
---|
90 | // preprocess fitness values, apply windowing if desired
|
---|
91 | for (int i = 0; i < qualities.Length; i++) {
|
---|
92 | solutionQuality = source.SubScopes[i].GetVariableValue<DoubleData>(qualityInfo.ActualName, false).Data;
|
---|
93 | if (solutionQuality < min || solutionQuality > max) {
|
---|
94 | // something has obviously gone wrong here
|
---|
95 | string errorMessage = "There is a problem with the ordering of the source sub-scopes in " + Name + ".\r\n" +
|
---|
96 | "The quality of solution number " + i.ToString() + " is ";
|
---|
97 | if (solutionQuality < min) errorMessage += "below";
|
---|
98 | else errorMessage += "greater than";
|
---|
99 | errorMessage += " the calculated qualities range:\r\n";
|
---|
100 | errorMessage += solutionQuality.ToString() + " is outside the interval [ " + min.ToString() + " ; " + max.ToString() + " ].";
|
---|
101 | throw new InvalidOperationException(errorMessage);
|
---|
102 | }
|
---|
103 | if (best != worst) { // prevent division by zero
|
---|
104 | if (windowing) {
|
---|
105 | if (!maximization) {
|
---|
106 | qualities[i] = 1 - ((solutionQuality - best) / (worst - best));
|
---|
107 | } else {
|
---|
108 | qualities[i] = (solutionQuality - worst) / (best - worst);
|
---|
109 | }
|
---|
110 | } else {
|
---|
111 | if (!maximization) qualities[i] = limit - solutionQuality;
|
---|
112 | else qualities[i] = solutionQuality;
|
---|
113 | }
|
---|
114 | } else { // best == worst -> all fitness values are equal
|
---|
115 | qualities[i] = 1;
|
---|
116 | }
|
---|
117 | qualitySum += qualities[i];
|
---|
118 | }
|
---|
119 | if (double.IsInfinity(qualitySum)) qualitySum = double.MaxValue;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|