1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Selection;
|
---|
10 | using HeuristicLab.PluginInfrastructure;
|
---|
11 | using HeuristicLab.Operators;
|
---|
12 | using System;
|
---|
13 | using HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
16 | [Item("NeutralSelector", "A selection operator that explores neutral areas.")]
|
---|
17 | [StorableClass]
|
---|
18 | public class NeutralSelector : SingleSuccessorOperator, ISingleObjectiveSelector {
|
---|
19 |
|
---|
20 | public override bool CanChangeName {
|
---|
21 | get { return false; }
|
---|
22 | }
|
---|
23 |
|
---|
24 | #region Parameters
|
---|
25 | public ScopeParameter CurrentScopeParameter {
|
---|
26 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
27 | }
|
---|
28 | protected IValueParameter<BoolValue> CopySelectedParameter {
|
---|
29 | get { return (IValueParameter<BoolValue>)Parameters["CopySelected"]; }
|
---|
30 | }
|
---|
31 | public IValueLookupParameter<IntValue> NumberOfSelectedSubScopesParameter {
|
---|
32 | get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfSelectedSubScopes"]; }
|
---|
33 | }
|
---|
34 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
35 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
36 | }
|
---|
37 | public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {
|
---|
38 | get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Quality"]; }
|
---|
39 | }
|
---|
40 | public ScopePathLookupParameter<DoubleValue> BaseQualityParameter {
|
---|
41 | get { return (ScopePathLookupParameter<DoubleValue>)Parameters["BaseQuality"]; }
|
---|
42 | }
|
---|
43 | public ScopePathLookupParameter<IItem> BaseSolutionParameter {
|
---|
44 | get { return (ScopePathLookupParameter<IItem>)Parameters["BaseSolution"]; }
|
---|
45 | }
|
---|
46 | public ScopePathLookupParameter<IItem> StartingPointParameter {
|
---|
47 | get { return (ScopePathLookupParameter<IItem>)Parameters["StartingPoint"]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<ItemArray<IItem>> SolutionParameter {
|
---|
50 | get { return (ILookupParameter<ItemArray<IItem>>)Parameters["Solution"]; }
|
---|
51 | }
|
---|
52 | public ConstrainedValueParameter<IItemDistanceCalculator> SolutionDistanceCalculatorParameter {
|
---|
53 | get { return (ConstrainedValueParameter<IItemDistanceCalculator>)Parameters["SolutionDistanceCalculator"]; }
|
---|
54 | }
|
---|
55 | public ValueLookupParameter<DoubleValue> EpsilonParameter {
|
---|
56 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Epsilon"]; }
|
---|
57 | }
|
---|
58 | public LookupParameter<DoubleValue> CurrentNeutralDistanceParameter {
|
---|
59 | get { return (LookupParameter<DoubleValue>)Parameters["CurrentNeutralDistance"]; }
|
---|
60 | }
|
---|
61 | public LookupParameter<IRandom> RandomParameter {
|
---|
62 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | #region Parameter Values
|
---|
67 | protected IScope CurrentScope {
|
---|
68 | get { return CurrentScopeParameter.ActualValue; }
|
---|
69 | }
|
---|
70 | public BoolValue CopySelected {
|
---|
71 | get { return CopySelectedParameter.Value; }
|
---|
72 | set { CopySelectedParameter.Value = value; }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region Construction & Cloning
|
---|
77 | [StorableConstructor]
|
---|
78 | protected NeutralSelector(bool deserializing) : base(deserializing) { }
|
---|
79 | protected NeutralSelector(NeutralSelector original, Cloner cloner) : base(original, cloner) { }
|
---|
80 | public NeutralSelector() {
|
---|
81 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which sub-scopes should be selected."));
|
---|
82 | Parameters.Add(new ValueParameter<BoolValue>("CopySelected", "True if the selected sub-scopes should be copied, otherwise false.", new BoolValue(true)));
|
---|
83 | Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSelectedSubScopes", "The number of sub-scopes which should be selected."));
|
---|
84 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the current problem is a maximization problem, otherwise false."));
|
---|
85 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality value contained in each sub-scope which is used for selection."));
|
---|
86 | Parameters.Add(new ScopePathLookupParameter<DoubleValue>("BaseQuality", "The base quality value to compare to. This is required to determine wheter a local optimum has been found and the direction should be reversed."));
|
---|
87 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Epsilon", "Maximum delta accepted as neutral mutation.", new DoubleValue(0)));
|
---|
88 | Parameters.Add(new ScopePathLookupParameter<IItem>("BaseSolution", "Previous solution in neutral walk."));
|
---|
89 | Parameters.Add(new ScopePathLookupParameter<IItem>("StartingPoint", "Starting point of neutral walk (broken by non neutral transition through fallback selector)"));
|
---|
90 | Parameters.Add(new ScopeTreeLookupParameter<IItem>("Solution", "Current solution"));
|
---|
91 | Parameters.Add(new ConstrainedValueParameter<IItemDistanceCalculator>("SolutionDistanceCalculator", "Operator to calculate distances between the base item and the current item."));
|
---|
92 | Parameters.Add(new LookupParameter<DoubleValue>("CurrentNeutralDistance", "The distance of the current item to the starting point of the neutral (portion of the) walk."));
|
---|
93 | Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator for breaking ties in the ordering."));
|
---|
94 | CopySelectedParameter.Hidden = true;
|
---|
95 | MaximizationParameter.Hidden = true;
|
---|
96 | NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
97 | BaseQualityParameter.Hidden = false;
|
---|
98 | QualityParameter.Hidden = false;
|
---|
99 | BaseSolutionParameter.Hidden = false;
|
---|
100 | StartingPointParameter.Hidden = true;
|
---|
101 | SolutionParameter.Hidden = false;
|
---|
102 | SolutionDistanceCalculatorParameter.Hidden = false;
|
---|
103 | StartingPointParameter.Hidden = false;
|
---|
104 | BaseQualityParameter.ActualName = "Quality";
|
---|
105 | BaseSolutionParameter.Path = "../Remaining/{0}";
|
---|
106 | BaseQualityParameter.Path = "../Remaining/{0}";
|
---|
107 | StartingPointParameter.Path = "..";
|
---|
108 | foreach (var v in ApplicationManager.Manager.GetInstances<IItemDistanceCalculator>())
|
---|
109 | SolutionDistanceCalculatorParameter.ValidValues.Add(v);
|
---|
110 | }
|
---|
111 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
112 | return new NeutralSelector(this, cloner);
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | private static double Squash(double d, double eps) {
|
---|
117 | return d <= eps ? 0 : d;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public sealed override IOperation Apply() {
|
---|
121 | IItemDistanceCalculator calc = (IItemDistanceCalculator)SolutionDistanceCalculatorParameter.ActualValue;
|
---|
122 | double baseQuality = BaseQualityParameter.ActualValue.Value;
|
---|
123 | IItem startingPoint = StartingPointParameter.ActualValue;
|
---|
124 | IItem baseSolution = BaseSolutionParameter.ActualValue;
|
---|
125 | ItemArray<IItem> items = SolutionParameter.ActualValue;
|
---|
126 | double eps = EpsilonParameter.ActualValue.Value;
|
---|
127 | IRandom random = RandomParameter.ActualValue;
|
---|
128 | double baseDistance = 0;
|
---|
129 | if (startingPoint != null) {
|
---|
130 | baseDistance = calc.Distance(baseSolution, startingPoint);
|
---|
131 | baseSolution = startingPoint;
|
---|
132 | }
|
---|
133 |
|
---|
134 | var neighbors = QualityParameter.ActualValue
|
---|
135 | .Zip(items, (q, i) => new { Quality=q.Value, Item=i })
|
---|
136 | .Select((p, i) => new {
|
---|
137 | Idx=i,
|
---|
138 | Diff=Squash(Math.Abs(baseQuality-p.Quality), eps),
|
---|
139 | Dist=calc.Distance(baseSolution, p.Item)
|
---|
140 | })
|
---|
141 | .ToList();
|
---|
142 | if (random != null)
|
---|
143 | neighbors.Shuffle(random);
|
---|
144 | var mostDistantNeutralNeighbor = neighbors.Where(n => n.Diff == 0).OrderByDescending(n => n.Dist).FirstOrDefault();
|
---|
145 | if (mostDistantNeutralNeighbor != null && mostDistantNeutralNeighbor.Dist > baseDistance) {
|
---|
146 | Select(mostDistantNeutralNeighbor.Idx);
|
---|
147 | if (startingPoint == null)
|
---|
148 | StartingPointParameter.ActualValue = (IItem)baseSolution.Clone();
|
---|
149 | CurrentNeutralDistanceParameter.ActualValue = new DoubleValue(mostDistantNeutralNeighbor.Dist);
|
---|
150 | } else {
|
---|
151 | var mostDistantNeighbor = neighbors.OrderByDescending(n => n.Dist).FirstOrDefault();
|
---|
152 | Select(mostDistantNeighbor.Idx);
|
---|
153 | StartingPointParameter.ActualValue = (IItem)items[mostDistantNeighbor.Idx].Clone();
|
---|
154 | CurrentNeutralDistanceParameter.ActualValue = new DoubleValue(0);
|
---|
155 | }
|
---|
156 | return base.Apply();
|
---|
157 | }
|
---|
158 |
|
---|
159 | private void Select(int i) {
|
---|
160 | List<IScope> scopes = new List<IScope>(CurrentScope.SubScopes);
|
---|
161 | bool copy = CopySelectedParameter.Value.Value;
|
---|
162 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
163 | IScope[] selected = new IScope[1];
|
---|
164 | if (copy) {
|
---|
165 | selected[0] = (IScope)scopes[i].Clone();
|
---|
166 | } else {
|
---|
167 | selected[0] = scopes[i];
|
---|
168 | scopes[i] = null;
|
---|
169 | scopes.RemoveAll(x => x == null);
|
---|
170 | }
|
---|
171 | CurrentScope.SubScopes.Clear();
|
---|
172 | IScope remainingScope = new Scope("Remaining");
|
---|
173 | remainingScope.SubScopes.AddRange(scopes);
|
---|
174 | CurrentScope.SubScopes.Add(remainingScope);
|
---|
175 | IScope selectedScope = new Scope("Selected");
|
---|
176 | selectedScope.SubScopes.AddRange(selected);
|
---|
177 | CurrentScope.SubScopes.Add(selectedScope);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|