1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.Knapsack;
|
---|
32 | using HeuristicLab.Problems.TestFunctions;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.ScatterSearch.TestFunctions {
|
---|
35 | /// <summary>
|
---|
36 | /// An operator that improves test functions solutions.
|
---|
37 | /// </summary>
|
---|
38 | [Item("TestFunctionsImprovementOperator", "An operator that improves test functions solutions.")]
|
---|
39 | [StorableClass]
|
---|
40 | public abstract class TestFunctionsImprovementOperator : SingleSuccessorOperator, ILocalImprovementOperator, IScatterSearchTargetProcessor {
|
---|
41 | #region Problem properties
|
---|
42 | public Type ProblemType {
|
---|
43 | get { return typeof(KnapsackProblem); }
|
---|
44 | }
|
---|
45 | [Storable]
|
---|
46 | private KnapsackProblem problem;
|
---|
47 | public IProblem Problem {
|
---|
48 | get { return problem; }
|
---|
49 | set { problem = (KnapsackProblem)value; }
|
---|
50 | }
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | #region Parameter properties
|
---|
54 | public ScopeParameter CurrentScopeParameter {
|
---|
55 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
56 | }
|
---|
57 | public IValueLookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
58 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
59 | }
|
---|
60 | public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter {
|
---|
61 | get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; }
|
---|
62 | }
|
---|
63 | public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
|
---|
64 | get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
|
---|
65 | }
|
---|
66 | public IValueLookupParameter<IRandom> RandomParameter {
|
---|
67 | get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
68 | }
|
---|
69 | public IValueLookupParameter<IItem> TargetParameter {
|
---|
70 | get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
|
---|
71 | }
|
---|
72 | public IValueParameter<DoubleValue> AlphaParameter {
|
---|
73 | get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }
|
---|
74 | }
|
---|
75 | public IValueParameter<DoubleValue> BetaParameter {
|
---|
76 | get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }
|
---|
77 | }
|
---|
78 | public IValueParameter<DoubleValue> GammaParameter {
|
---|
79 | get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
|
---|
80 | }
|
---|
81 | public IValueParameter<DoubleValue> DeltaParameter {
|
---|
82 | get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; }
|
---|
83 | }
|
---|
84 | #region ILocalImprovementOperator Parameters
|
---|
85 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
86 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
87 | }
|
---|
88 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
89 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
90 | }
|
---|
91 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
92 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | #region Properties
|
---|
98 | private IItem Target {
|
---|
99 | get { return TargetParameter.ActualValue; }
|
---|
100 | }
|
---|
101 | private DoubleValue Alpha {
|
---|
102 | get { return AlphaParameter.Value; }
|
---|
103 | }
|
---|
104 | private DoubleValue Beta {
|
---|
105 | get { return BetaParameter.Value; }
|
---|
106 | }
|
---|
107 | private DoubleValue Gamma {
|
---|
108 | get { return GammaParameter.Value; }
|
---|
109 | }
|
---|
110 | private DoubleValue Delta {
|
---|
111 | get { return DeltaParameter.Value; }
|
---|
112 | }
|
---|
113 | protected Func<RealVector, double> FunctionEvaluator { get; set; }
|
---|
114 | public IScope CurrentScope {
|
---|
115 | get { return CurrentScopeParameter.ActualValue; }
|
---|
116 | }
|
---|
117 | public DoubleMatrix Coordinates {
|
---|
118 | get { return CoordinatesParameter.ActualValue; }
|
---|
119 | set { CoordinatesParameter.ActualValue = value; }
|
---|
120 | }
|
---|
121 | public ISingleObjectiveTestFunctionProblemEvaluator Evaluator {
|
---|
122 | get { return EvaluatorParameter.ActualValue; }
|
---|
123 | set { EvaluatorParameter.ActualValue = value; }
|
---|
124 | }
|
---|
125 | public IntValue ImprovementAttempts {
|
---|
126 | get { return ImprovementAttemptsParameter.ActualValue; }
|
---|
127 | set { ImprovementAttemptsParameter.ActualValue = value; }
|
---|
128 | }
|
---|
129 | public IRandom Random {
|
---|
130 | get { return RandomParameter.ActualValue; }
|
---|
131 | set { RandomParameter.ActualValue = value; }
|
---|
132 | }
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | [StorableConstructor]
|
---|
136 | protected TestFunctionsImprovementOperator(bool deserializing) : base(deserializing) { }
|
---|
137 | protected TestFunctionsImprovementOperator(TestFunctionsImprovementOperator original, Cloner cloner)
|
---|
138 | : base(original, cloner) {
|
---|
139 | this.problem = cloner.Clone(original.problem);
|
---|
140 | }
|
---|
141 | public TestFunctionsImprovementOperator()
|
---|
142 | : base() {
|
---|
143 | #region Create parameters
|
---|
144 | Parameters.Add(new ScopeParameter("CurrentScope"));
|
---|
145 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Coordinates"));
|
---|
146 | Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator"));
|
---|
147 | Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", new IntValue(100)));
|
---|
148 | Parameters.Add(new ValueLookupParameter<IRandom>("Random"));
|
---|
149 | Parameters.Add(new ValueLookupParameter<IItem>("Target"));
|
---|
150 | Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0)));
|
---|
151 | Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0)));
|
---|
152 | Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5)));
|
---|
153 | Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5)));
|
---|
154 | #endregion
|
---|
155 | TargetParameter.ActualName = "Point"; // temporary solution for the test functions problem
|
---|
156 | }
|
---|
157 |
|
---|
158 | public override IOperation Apply() {
|
---|
159 | var bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector;
|
---|
160 | var bestSolQuality = FunctionEvaluator(bestSol);
|
---|
161 |
|
---|
162 | // create perturbed solutions
|
---|
163 | var simplex = new RealVector[bestSol.Length];
|
---|
164 | for (int i = 0; i < simplex.Length; i++) {
|
---|
165 | simplex[i] = bestSol.Clone() as RealVector;
|
---|
166 | simplex[i][i] += 0.1 * (Evaluator.Bounds[0, 1] - Evaluator.Bounds[0, 0]);
|
---|
167 | if (simplex[i][i] > Evaluator.Bounds[0, 1]) simplex[i][i] = Evaluator.Bounds[0, 1];
|
---|
168 | if (simplex[i][i] < Evaluator.Bounds[0, 0]) simplex[i][i] = Evaluator.Bounds[0, 0];
|
---|
169 | }
|
---|
170 |
|
---|
171 | // improve solutions
|
---|
172 | for (int i = 0; i < ImprovementAttempts.Value; i++) {
|
---|
173 | // order according to their objective function value
|
---|
174 | Array.Sort(simplex, (x, y) => FunctionEvaluator(x).CompareTo(FunctionEvaluator(y)));
|
---|
175 |
|
---|
176 | // calculate centroid
|
---|
177 | var centroid = new RealVector(bestSol.Length);
|
---|
178 | foreach (var vector in simplex)
|
---|
179 | for (int j = 0; j < centroid.Length; j++)
|
---|
180 | centroid[j] += vector[j];
|
---|
181 | for (int j = 0; j < centroid.Length; j++)
|
---|
182 | centroid[j] /= simplex.Length;
|
---|
183 |
|
---|
184 | // reflection
|
---|
185 | var reflectionPoint = new RealVector(bestSol.Length);
|
---|
186 | for (int j = 0; j < reflectionPoint.Length; j++)
|
---|
187 | reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]);
|
---|
188 | double reflectionPointQuality = FunctionEvaluator(reflectionPoint);
|
---|
189 | if (FunctionEvaluator(simplex[0]) <= reflectionPointQuality
|
---|
190 | && reflectionPointQuality < FunctionEvaluator(simplex[simplex.Length - 2]))
|
---|
191 | simplex[simplex.Length - 1] = reflectionPoint;
|
---|
192 |
|
---|
193 | // expansion
|
---|
194 | if (reflectionPointQuality < FunctionEvaluator(simplex[0])) {
|
---|
195 | var expansionPoint = new RealVector(bestSol.Length);
|
---|
196 | for (int j = 0; j < expansionPoint.Length; j++)
|
---|
197 | expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]);
|
---|
198 | simplex[simplex.Length - 1] = FunctionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;
|
---|
199 | }
|
---|
200 |
|
---|
201 | // contraction
|
---|
202 | if (FunctionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality
|
---|
203 | && reflectionPointQuality < FunctionEvaluator(simplex[simplex.Length - 1])) {
|
---|
204 | var outsideContractionPoint = new RealVector(bestSol.Length);
|
---|
205 | for (int j = 0; j < outsideContractionPoint.Length; j++)
|
---|
206 | outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]);
|
---|
207 | if (FunctionEvaluator(outsideContractionPoint) <= reflectionPointQuality) {
|
---|
208 | simplex[simplex.Length - 1] = outsideContractionPoint;
|
---|
209 | if (FunctionEvaluator(reflectionPoint) >= FunctionEvaluator(simplex[simplex.Length - 1])) {
|
---|
210 | var insideContractionPoint = new RealVector(bestSol.Length);
|
---|
211 | for (int j = 0; j < insideContractionPoint.Length; j++)
|
---|
212 | insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]);
|
---|
213 | if (FunctionEvaluator(insideContractionPoint) < FunctionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | // reduction
|
---|
219 | for (int j = 1; j < simplex.Length; j++)
|
---|
220 | for (int k = 0; k < simplex[j].Length; k++)
|
---|
221 | simplex[j][k] = simplex[0][k] + Delta.Value * (simplex[j][k] - simplex[0][k]);
|
---|
222 | }
|
---|
223 |
|
---|
224 | for (int i = 0; i < simplex[0].Length; i++) {
|
---|
225 | if (simplex[0][i] > Evaluator.Bounds[0, 1]) simplex[0][i] = Evaluator.Bounds[0, 1];
|
---|
226 | if (simplex[0][i] < Evaluator.Bounds[0, 0]) simplex[0][i] = Evaluator.Bounds[0, 0];
|
---|
227 | }
|
---|
228 |
|
---|
229 | CurrentScope.Variables[TargetParameter.ActualName].Value = simplex[0];
|
---|
230 |
|
---|
231 | return base.Apply();
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|