1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.TS {
|
---|
34 | [Item("TS", "A tabu search algorithm.")]
|
---|
35 | public sealed class TS : EngineAlgorithm {
|
---|
36 | /*#region Parameter Properties
|
---|
37 | private ValueParameter<IntData> SeedParameter {
|
---|
38 | get { return (ValueParameter<IntData>)Parameters["Seed"]; }
|
---|
39 | }
|
---|
40 | private ValueParameter<BoolData> SetSeedRandomlyParameter {
|
---|
41 | get { return (ValueParameter<BoolData>)Parameters["SetSeedRandomly"]; }
|
---|
42 | }
|
---|
43 | private ConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter {
|
---|
44 | get { return (ConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
|
---|
45 | }
|
---|
46 | private ValueParameter<IntData> MaximumIterationsParameter {
|
---|
47 | get { return (ValueParameter<IntData>)Parameters["MaximumIterations"]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Properties
|
---|
52 | public IntData Seed {
|
---|
53 | get { return SeedParameter.Value; }
|
---|
54 | set { SeedParameter.Value = value; }
|
---|
55 | }
|
---|
56 | public BoolData SetSeedRandomly {
|
---|
57 | get { return SetSeedRandomlyParameter.Value; }
|
---|
58 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
59 | }
|
---|
60 | public IMoveGenerator MoveGenerator {
|
---|
61 | get { return MoveGeneratorParameter.Value; }
|
---|
62 | set { MoveGeneratorParameter.Value = value; }
|
---|
63 | }
|
---|
64 | public IntData MaximumIterations {
|
---|
65 | get { return MaximumIterationsParameter.Value; }
|
---|
66 | set { MaximumIterationsParameter.Value = value; }
|
---|
67 | }
|
---|
68 | private RandomCreator RandomCreator {
|
---|
69 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
70 | }
|
---|
71 | private SolutionsCreator SolutionsCreator {
|
---|
72 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
73 | }
|
---|
74 | private TSMainLoop TSMainLoop {
|
---|
75 | get { return (TSMainLoop)SolutionsCreator.Successor; }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | public TS()
|
---|
80 | : base() {
|
---|
81 | Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
|
---|
82 | Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
|
---|
83 | Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
|
---|
84 | Parameters.Add(new ValueParameter<IntData>("MaximumIterations", "The maximum number of generations which should be processed.", new IntData(1000)));
|
---|
85 |
|
---|
86 | RandomCreator randomCreator = new RandomCreator();
|
---|
87 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
88 | TSMainLoop tsMainLoop = new TSMainLoop();
|
---|
89 | OperatorGraph.InitialOperator = randomCreator;
|
---|
90 |
|
---|
91 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
92 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
93 | randomCreator.SeedParameter.Value = null;
|
---|
94 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
95 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
96 | randomCreator.Successor = solutionsCreator;
|
---|
97 |
|
---|
98 | solutionsCreator.NumberOfSolutions = new IntData(1);
|
---|
99 | solutionsCreator.Successor = tsMainLoop;
|
---|
100 |
|
---|
101 | tsMainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
102 | tsMainLoop.MoveMakerParameter.ActualName = MoveGenerator..Name;
|
---|
103 | tsMainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
104 | tsMainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
105 | tsMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
106 | tsMainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
107 | tsMainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
108 | tsMainLoop.ResultsParameter.ActualName = "Results";
|
---|
109 |
|
---|
110 | Initialze();
|
---|
111 | }
|
---|
112 | [StorableConstructor]
|
---|
113 | private TS(bool deserializing) : base() { }
|
---|
114 |
|
---|
115 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
116 | TS clone = (TS)base.Clone(cloner);
|
---|
117 | clone.Initialze();
|
---|
118 | return clone;
|
---|
119 | }
|
---|
120 |
|
---|
121 | #region Events
|
---|
122 | protected override void OnProblemChanged() {
|
---|
123 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
124 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
125 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
126 | ParameterizeSolutionsCreator();
|
---|
127 | ParameterizeSGAMainLoop();
|
---|
128 | ParameterizeSelectors();
|
---|
129 | UpdateCrossovers();
|
---|
130 | UpdateMutators();
|
---|
131 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
132 | base.OnProblemChanged();
|
---|
133 | }
|
---|
134 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
135 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
136 | ParameterizeSolutionsCreator();
|
---|
137 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
138 | }
|
---|
139 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
140 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
141 | ParameterizeSolutionsCreator();
|
---|
142 | ParameterizeSGAMainLoop();
|
---|
143 | ParameterizeSelectors();
|
---|
144 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
145 | base.Problem_EvaluatorChanged(sender, e);
|
---|
146 | }
|
---|
147 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
148 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
149 | UpdateCrossovers();
|
---|
150 | UpdateMutators();
|
---|
151 | base.Problem_OperatorsChanged(sender, e);
|
---|
152 | }
|
---|
153 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
154 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
155 | ParameterizeSelectors();
|
---|
156 | }
|
---|
157 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
158 | ParameterizeSelectors();
|
---|
159 | }
|
---|
160 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
161 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
162 | ParameterizeSelectors();
|
---|
163 | }
|
---|
164 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
165 | ParameterizeSelectors();
|
---|
166 | }
|
---|
167 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
168 | ParameterizeSGAMainLoop();
|
---|
169 | ParameterizeSelectors();
|
---|
170 | }
|
---|
171 | #endregion
|
---|
172 |
|
---|
173 | #region Helpers
|
---|
174 | [StorableHook(HookType.AfterDeserialization)]
|
---|
175 | private void Initialze() {
|
---|
176 | InitializeSelectors();
|
---|
177 | UpdateSelectors();
|
---|
178 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
179 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
180 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
181 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
182 | if (Problem != null)
|
---|
183 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void ParameterizeSolutionsCreator() {
|
---|
187 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
188 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
189 | }
|
---|
190 | private void ParameterizeSGAMainLoop() {
|
---|
191 | TSMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
192 | TSMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
193 | TSMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
194 | }
|
---|
195 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
196 | if (op is IStochasticOperator)
|
---|
197 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
198 | }
|
---|
199 | private void InitializeSelectors() {
|
---|
200 | selectors = new List<ISelector>();
|
---|
201 | if (ApplicationManager.Manager != null) {
|
---|
202 | selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
203 | ParameterizeSelectors();
|
---|
204 | }
|
---|
205 | }
|
---|
206 | private void ParameterizeSelectors() {
|
---|
207 | foreach (ISelector selector in Selectors) {
|
---|
208 | selector.CopySelected = new BoolData(true);
|
---|
209 | selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
|
---|
210 | ParameterizeStochasticOperator(selector);
|
---|
211 | }
|
---|
212 | if (Problem != null) {
|
---|
213 | foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
|
---|
214 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
215 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | private void UpdateSelectors() {
|
---|
220 | if (ApplicationManager.Manager != null) {
|
---|
221 | ISelector oldSelector = SelectorParameter.Value;
|
---|
222 | SelectorParameter.ValidValues.Clear();
|
---|
223 | foreach (ISelector selector in Selectors.OrderBy(x => x.Name))
|
---|
224 | SelectorParameter.ValidValues.Add(selector);
|
---|
225 | if (oldSelector != null)
|
---|
226 | SelectorParameter.Value = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
227 | }
|
---|
228 | }
|
---|
229 | private void UpdateCrossovers() {
|
---|
230 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
231 | CrossoverParameter.ValidValues.Clear();
|
---|
232 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
233 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
234 | if (oldCrossover != null)
|
---|
235 | CrossoverParameter.Value = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
236 | }
|
---|
237 | private void UpdateMutators() {
|
---|
238 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
239 | MutatorParameter.ValidValues.Clear();
|
---|
240 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
241 | MutatorParameter.ValidValues.Add(mutator);
|
---|
242 | if (oldMutator != null)
|
---|
243 | MutatorParameter.Value = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
244 | }
|
---|
245 | #endregion
|
---|
246 | */
|
---|
247 | }
|
---|
248 | }
|
---|