Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.LS/3.3/LSMainLoop.cs @ 3078

Last change on this file since 3078 was 3078, checked in by abeham, 15 years ago

Added a simple local search / hill climber algorithm similar to the TS #921
Added a QualityComparator operator in HeuristicLab.Optimization.Operators which acts like a comparator and injects a bool variable depending on whether the left side quality is better than the right side quality

File size: 9.4 KB
Line 
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
22using HeuristicLab.Analysis;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.LS {
32  /// <summary>
33  /// An operator which represents a tabu search.
34  /// </summary>
35  [Item("LSMainLoop", "An operator which represents the main loop of a best improvement local search.")]
36  [StorableClass]
37  public class LSMainLoop : AlgorithmOperator {
38    #region Parameter properties
39    public ValueLookupParameter<IRandom> RandomParameter {
40      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    public ValueLookupParameter<BoolValue> MaximizationParameter {
43      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
44    }
45    public LookupParameter<DoubleValue> QualityParameter {
46      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48    public LookupParameter<DoubleValue> MoveQualityParameter {
49      get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
50    }
51    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
52      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
53    }
54    public ValueLookupParameter<VariableCollection> ResultsParameter {
55      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
56    }
57    public ValueLookupParameter<IOperator> MoveGeneratorParameter {
58      get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
59    }
60    public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
61      get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
62    }
63    public ValueLookupParameter<IOperator> MoveMakerParameter {
64      get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
65    }
66
67    private ScopeParameter CurrentScopeParameter {
68      get { return (ScopeParameter)Parameters["CurrentScope"]; }
69    }
70    public IScope CurrentScope {
71      get { return CurrentScopeParameter.ActualValue; }
72    }
73    #endregion
74
75    public LSMainLoop()
76      : base() {
77      #region Create parameters
78      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
79      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
80      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
81      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
82      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
83      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
84
85      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
86      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
87      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
88     
89      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied."));
90      #endregion
91
92      #region Create operators
93      VariableCreator variableCreator = new VariableCreator();
94      ResultsCollector resultsCollector = new ResultsCollector();
95      UniformSequentialSubScopesProcessor mainProcessor = new UniformSequentialSubScopesProcessor();
96      Placeholder moveGenerator = new Placeholder();
97      UniformSequentialSubScopesProcessor moveEvaluationProcessor = new UniformSequentialSubScopesProcessor();
98      Placeholder moveEvaluator = new Placeholder();
99      SubScopesSorter moveQualitySorter = new SubScopesSorter();
100      LeftSelector leftSelector = new LeftSelector();
101      RightReducer rightReducer = new RightReducer();
102      UniformSequentialSubScopesProcessor moveMakingProcessor = new UniformSequentialSubScopesProcessor();
103      QualityComparator qualityComparator = new QualityComparator();
104      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
105      Placeholder moveMaker = new Placeholder();
106      SubScopesRemover subScopesRemover = new SubScopesRemover();
107      DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
108      IntCounter iterationsCounter = new IntCounter();
109      Comparator iterationsComparator = new Comparator();
110      ConditionalBranch iterationsTermination = new ConditionalBranch();
111      EmptyOperator finished = new EmptyOperator();
112
113      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
114      variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
115
116      mainProcessor.Name = "Solution processor (UniformSequentialSubScopesProcessor)";
117
118      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
119      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Quality"));
120      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
121      resultsCollector.ResultsParameter.ActualName = "Results";
122
123      moveGenerator.Name = "MoveGenerator (placeholder)";
124      moveGenerator.OperatorParameter.ActualName = "MoveGenerator";
125
126      moveEvaluator.Name = "MoveEvaluator (placeholder)";
127      moveEvaluator.OperatorParameter.ActualName = "MoveEvaluator";
128
129      moveQualitySorter.DescendingParameter.ActualName = "Maximization";
130      moveQualitySorter.ValueParameter.ActualName = "MoveQuality";
131
132      leftSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
133
134      moveMakingProcessor.Name = "MoveMaking processor (UniformSequentialSubScopesProcessor)";
135
136      qualityComparator.LeftSideParameter.ActualName = "MoveQuality";
137      qualityComparator.RightSideParameter.ActualName = "Quality";
138      qualityComparator.ResultParameter.ActualName = "IsBetter";
139
140      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
141
142      moveMaker.Name = "MoveMaker (placeholder)";
143      moveMaker.OperatorParameter.ActualName = "MoveMaker";
144
145      subScopesRemover.RemoveAllSubScopes = true;
146
147      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Quality"));
148      valuesCollector.DataTableParameter.ActualName = "Qualities";
149
150      iterationsCounter.Name = "Iterations Counter";
151      iterationsCounter.Increment = new IntValue(1);
152      iterationsCounter.ValueParameter.ActualName = "Iterations";
153
154      iterationsComparator.Name = "Iterations Comparator";
155      iterationsComparator.Comparison = new Comparison(ComparisonType.Less);
156      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
157      iterationsComparator.RightSideParameter.ActualName = "MaximumIterations";
158      iterationsComparator.ResultParameter.ActualName = "IterationsCondition";
159
160      iterationsTermination.Name = "Iterations Termination Condition";
161      iterationsTermination.ConditionParameter.ActualName = "IterationsCondition";
162      #endregion
163
164      #region Create operator graph
165      OperatorGraph.InitialOperator = variableCreator;
166      variableCreator.Successor = mainProcessor;
167      mainProcessor.Operator = resultsCollector;
168      mainProcessor.Successor = iterationsCounter;
169      resultsCollector.Successor = moveGenerator;
170      moveGenerator.Successor = moveEvaluationProcessor;
171      moveEvaluationProcessor.Operator = moveEvaluator;
172      moveEvaluationProcessor.Successor = moveQualitySorter;
173      moveQualitySorter.Successor = leftSelector;
174      leftSelector.Successor = rightReducer;
175      rightReducer.Successor = moveMakingProcessor;
176      moveMakingProcessor.Operator = qualityComparator;
177      moveMakingProcessor.Successor = subScopesRemover;
178      subScopesRemover.Successor = valuesCollector;
179      qualityComparator.Successor = improvesQualityBranch;
180      improvesQualityBranch.TrueBranch = moveMaker;
181      iterationsCounter.Successor = iterationsComparator;
182      iterationsComparator.Successor = iterationsTermination;
183      iterationsTermination.TrueBranch = mainProcessor;
184      iterationsTermination.FalseBranch = finished;
185      #endregion
186    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.