[13368] | 1 | #region License Information
|
---|
[7789] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7789] | 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.Linq;
|
---|
| 24 | using HeuristicLab.Analysis;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Optimization.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.Random;
|
---|
| 34 | using HeuristicLab.Selection;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.ScatterSearch {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// A scatter search algorithm.
|
---|
| 39 | /// </summary>
|
---|
[13173] | 40 | [Item("Scatter Search (SS)", "A scatter search algorithm.")]
|
---|
[12504] | 41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 500)]
|
---|
[14711] | 42 | [StorableType("25F04931-BC61-4940-AEA8-C826AFC47DF7")]
|
---|
[7789] | 43 | public sealed class ScatterSearch : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
| 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
| 46 | #region Problem Properties
|
---|
| 47 | public override Type ProblemType {
|
---|
| 48 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
| 49 | }
|
---|
| 50 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 51 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
| 52 | set { base.Problem = value; }
|
---|
| 53 | }
|
---|
| 54 | #endregion
|
---|
| 55 |
|
---|
| 56 | #region Parameter Properties
|
---|
| 57 | public IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 58 | get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 59 | }
|
---|
[8332] | 60 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 61 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
[7789] | 62 | }
|
---|
| 63 | public IValueParameter<BoolValue> ExecutePathRelinkingParameter {
|
---|
[11404] | 64 | get { return (IValueParameter<BoolValue>)Parameters["ExecutePathRelinking"]; }
|
---|
[7789] | 65 | }
|
---|
[8332] | 66 | public IConstrainedValueParameter<IImprovementOperator> ImproverParameter {
|
---|
| 67 | get { return (IConstrainedValueParameter<IImprovementOperator>)Parameters["Improver"]; }
|
---|
[7789] | 68 | }
|
---|
| 69 | public IValueParameter<IntValue> MaximumIterationsParameter {
|
---|
| 70 | get { return (IValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 71 | }
|
---|
| 72 | public IValueParameter<IntValue> NumberOfHighQualitySolutionsParameter {
|
---|
| 73 | get { return (IValueParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
|
---|
| 74 | }
|
---|
[8332] | 75 | public IConstrainedValueParameter<IPathRelinker> PathRelinkerParameter {
|
---|
| 76 | get { return (IConstrainedValueParameter<IPathRelinker>)Parameters["PathRelinker"]; }
|
---|
[7789] | 77 | }
|
---|
| 78 | public IValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 79 | get { return (IValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 80 | }
|
---|
| 81 | public IValueParameter<IntValue> ReferenceSetSizeParameter {
|
---|
| 82 | get { return (IValueParameter<IntValue>)Parameters["ReferenceSetSize"]; }
|
---|
| 83 | }
|
---|
| 84 | public IValueParameter<IntValue> SeedParameter {
|
---|
| 85 | get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
| 86 | }
|
---|
| 87 | public IValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 88 | get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
| 89 | }
|
---|
[12102] | 90 | public IConstrainedValueParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 91 | get { return (IConstrainedValueParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
[7789] | 92 | }
|
---|
| 93 | #endregion
|
---|
| 94 |
|
---|
| 95 | #region Properties
|
---|
[8332] | 96 | public MultiAnalyzer Analyzer {
|
---|
[7789] | 97 | get { return AnalyzerParameter.Value; }
|
---|
| 98 | set { AnalyzerParameter.Value = value; }
|
---|
| 99 | }
|
---|
[8332] | 100 | public ICrossover Crossover {
|
---|
[7789] | 101 | get { return CrossoverParameter.Value; }
|
---|
| 102 | set { CrossoverParameter.Value = value; }
|
---|
| 103 | }
|
---|
[8332] | 104 | public BoolValue ExecutePathRelinking {
|
---|
[7789] | 105 | get { return ExecutePathRelinkingParameter.Value; }
|
---|
| 106 | set { ExecutePathRelinkingParameter.Value = value; }
|
---|
| 107 | }
|
---|
[8332] | 108 | public IImprovementOperator Improver {
|
---|
[7789] | 109 | get { return ImproverParameter.Value; }
|
---|
| 110 | set { ImproverParameter.Value = value; }
|
---|
| 111 | }
|
---|
[8332] | 112 | public IntValue MaximumIterations {
|
---|
[7789] | 113 | get { return MaximumIterationsParameter.Value; }
|
---|
| 114 | set { MaximumIterationsParameter.Value = value; }
|
---|
| 115 | }
|
---|
[8332] | 116 | public IntValue NumberOfHighQualitySolutions {
|
---|
[7789] | 117 | get { return NumberOfHighQualitySolutionsParameter.Value; }
|
---|
| 118 | set { NumberOfHighQualitySolutionsParameter.Value = value; }
|
---|
| 119 | }
|
---|
[8332] | 120 | public IPathRelinker PathRelinker {
|
---|
[7789] | 121 | get { return PathRelinkerParameter.Value; }
|
---|
| 122 | set { PathRelinkerParameter.Value = value; }
|
---|
| 123 | }
|
---|
[8332] | 124 | public IntValue PopulationSize {
|
---|
[7789] | 125 | get { return PopulationSizeParameter.Value; }
|
---|
| 126 | set { PopulationSizeParameter.Value = value; }
|
---|
| 127 | }
|
---|
[8332] | 128 | public IntValue ReferenceSetSize {
|
---|
[7789] | 129 | get { return ReferenceSetSizeParameter.Value; }
|
---|
| 130 | set { ReferenceSetSizeParameter.Value = value; }
|
---|
| 131 | }
|
---|
[8332] | 132 | public IntValue Seed {
|
---|
[7789] | 133 | get { return SeedParameter.Value; }
|
---|
| 134 | set { SeedParameter.Value = value; }
|
---|
| 135 | }
|
---|
[8332] | 136 | public BoolValue SetSeedRandomly {
|
---|
[7789] | 137 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 138 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 139 | }
|
---|
[12102] | 140 | public ISolutionSimilarityCalculator SimilarityCalculator {
|
---|
[7789] | 141 | get { return SimilarityCalculatorParameter.Value; }
|
---|
| 142 | set { SimilarityCalculatorParameter.Value = value; }
|
---|
| 143 | }
|
---|
| 144 | private RandomCreator RandomCreator {
|
---|
| 145 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 146 | }
|
---|
| 147 | private SolutionsCreator SolutionsCreator {
|
---|
| 148 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 149 | }
|
---|
| 150 | private ScatterSearchMainLoop MainLoop {
|
---|
| 151 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | [Storable]
|
---|
| 155 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
| 156 | #endregion
|
---|
| 157 |
|
---|
| 158 | [StorableConstructor]
|
---|
| 159 | private ScatterSearch(bool deserializing) : base(deserializing) { }
|
---|
| 160 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 161 | private void AfterDeserialization() {
|
---|
[12102] | 162 | // BackwardsCompatibility3.3
|
---|
| 163 | #region Backwards compatible code, remove with 3.4
|
---|
[12772] | 164 | if (Parameters.ContainsKey("SimilarityCalculator") && Parameters["SimilarityCalculator"] is IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>) {
|
---|
[12106] | 165 | #pragma warning disable 0618
|
---|
[12102] | 166 | var oldParameter = (IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"];
|
---|
[12106] | 167 | #pragma warning restore 0618
|
---|
[12102] | 168 | Parameters.Remove(oldParameter);
|
---|
| 169 | var newParameter = new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions.", new ItemSet<ISolutionSimilarityCalculator>(oldParameter.ValidValues));
|
---|
| 170 | var selectedSimilarityCalculator = newParameter.ValidValues.SingleOrDefault(x => x.GetType() == oldParameter.Value.GetType());
|
---|
| 171 | newParameter.Value = selectedSimilarityCalculator;
|
---|
| 172 | Parameters.Add(newParameter);
|
---|
| 173 | }
|
---|
| 174 | #endregion
|
---|
[7789] | 175 | Initialize();
|
---|
| 176 | }
|
---|
| 177 | private ScatterSearch(ScatterSearch original, Cloner cloner)
|
---|
| 178 | : base(original, cloner) {
|
---|
| 179 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 180 | Initialize();
|
---|
| 181 | }
|
---|
| 182 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 183 | return new ScatterSearch(this, cloner);
|
---|
| 184 | }
|
---|
| 185 | public ScatterSearch()
|
---|
| 186 | : base() {
|
---|
| 187 | #region Create parameters
|
---|
[8086] | 188 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The analyzer used to analyze each iteration.", new MultiAnalyzer()));
|
---|
| 189 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
[11839] | 190 | Parameters.Add(new ValueParameter<BoolValue>("ExecutePathRelinking", "True if path relinking should be executed instead of crossover, otherwise false.", new BoolValue(false)));
|
---|
[8086] | 191 | Parameters.Add(new ConstrainedValueParameter<IImprovementOperator>("Improver", "The operator used to improve solutions."));
|
---|
| 192 | Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed.", new IntValue(100)));
|
---|
| 193 | Parameters.Add(new ValueParameter<IntValue>("NumberOfHighQualitySolutions", "The number of high quality solutions in the reference set.", new IntValue(5)));
|
---|
| 194 | Parameters.Add(new ConstrainedValueParameter<IPathRelinker>("PathRelinker", "The operator used to execute path relinking."));
|
---|
| 195 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(50)));
|
---|
| 196 | Parameters.Add(new ValueParameter<IntValue>("ReferenceSetSize", "The size of the reference set.", new IntValue(20)));
|
---|
| 197 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 198 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[12102] | 199 | Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
|
---|
[7789] | 200 | #endregion
|
---|
| 201 |
|
---|
| 202 | #region Create operators
|
---|
| 203 | RandomCreator randomCreator = new RandomCreator();
|
---|
| 204 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
| 205 | UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
|
---|
| 206 | Placeholder solutionEvaluator = new Placeholder();
|
---|
| 207 | Placeholder solutionImprover = new Placeholder();
|
---|
[8086] | 208 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 209 | DataReducer dataReducer = new DataReducer();
|
---|
[7954] | 210 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
[7789] | 211 | BestSelector bestSelector = new BestSelector();
|
---|
| 212 | ScatterSearchMainLoop mainLoop = new ScatterSearchMainLoop();
|
---|
| 213 | #endregion
|
---|
| 214 |
|
---|
| 215 | #region Create operator graph
|
---|
| 216 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 217 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 218 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
[8774] | 219 | randomCreator.SeedParameter.Value = null;
|
---|
[7789] | 220 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
[8774] | 221 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
[7789] | 222 | randomCreator.Successor = solutionsCreator;
|
---|
| 223 |
|
---|
| 224 | solutionsCreator.Name = "DiversificationGenerationMethod";
|
---|
| 225 | solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize";
|
---|
| 226 | solutionsCreator.Successor = uniformSubScopesProcessor;
|
---|
| 227 |
|
---|
| 228 | uniformSubScopesProcessor.Operator = solutionImprover;
|
---|
[8380] | 229 | uniformSubScopesProcessor.ParallelParameter.Value = new BoolValue(true);
|
---|
[8086] | 230 | uniformSubScopesProcessor.Successor = variableCreator;
|
---|
[7789] | 231 |
|
---|
| 232 | solutionImprover.Name = "SolutionImprover";
|
---|
| 233 | solutionImprover.OperatorParameter.ActualName = "Improver";
|
---|
| 234 | solutionImprover.Successor = solutionEvaluator;
|
---|
| 235 |
|
---|
| 236 | solutionEvaluator.Name = "SolutionEvaluator";
|
---|
| 237 | solutionEvaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
| 238 | solutionEvaluator.Successor = null;
|
---|
| 239 |
|
---|
[8086] | 240 | variableCreator.Name = "Initialize EvaluatedSolutions";
|
---|
| 241 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
|
---|
| 242 | variableCreator.Successor = dataReducer;
|
---|
[7954] | 243 |
|
---|
[8086] | 244 | dataReducer.Name = "Increment EvaluatedSolutions";
|
---|
| 245 | dataReducer.ParameterToReduce.ActualName = "LocalEvaluatedSolutions";
|
---|
| 246 | dataReducer.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
| 247 | dataReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 248 | dataReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Sum);
|
---|
| 249 | dataReducer.Successor = resultsCollector;
|
---|
| 250 |
|
---|
[7954] | 251 | resultsCollector.Name = "ResultsCollector";
|
---|
| 252 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions", null, "EvaluatedSolutions"));
|
---|
| 253 | resultsCollector.Successor = bestSelector;
|
---|
| 254 |
|
---|
[7789] | 255 | bestSelector.NumberOfSelectedSubScopesParameter.ActualName = NumberOfHighQualitySolutionsParameter.Name;
|
---|
| 256 | bestSelector.CopySelected = new BoolValue(false);
|
---|
| 257 | bestSelector.Successor = mainLoop;
|
---|
| 258 |
|
---|
| 259 | mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 260 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
| 261 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
| 262 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 263 | mainLoop.IterationsParameter.ActualName = "Iterations";
|
---|
[8086] | 264 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
[7789] | 265 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 266 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 267 | mainLoop.NumberOfHighQualitySolutionsParameter.ActualName = NumberOfHighQualitySolutionsParameter.Name;
|
---|
| 268 | mainLoop.Successor = null;
|
---|
| 269 | #endregion
|
---|
| 270 |
|
---|
| 271 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 272 | ParameterizeAnalyzers();
|
---|
| 273 | UpdateAnalyzers();
|
---|
| 274 |
|
---|
| 275 | Initialize();
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | public override void Prepare() {
|
---|
[8746] | 279 | if (Problem != null && Improver != null && (PathRelinker != null || ExecutePathRelinking.Value == false) && SimilarityCalculator != null)
|
---|
[7789] | 280 | base.Prepare();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | #region Events
|
---|
| 284 | protected override void OnProblemChanged() {
|
---|
| 285 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 286 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
[8086] | 287 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[7789] | 288 | ParameterizeAnalyzers();
|
---|
| 289 | ParameterizeSolutionsCreator();
|
---|
| 290 | ParameterizeBestSelector();
|
---|
| 291 | UpdateAnalyzers();
|
---|
| 292 | UpdateCrossovers();
|
---|
[8348] | 293 | UpdateImprovers();
|
---|
[7789] | 294 | UpdatePathRelinkers();
|
---|
| 295 | UpdateSimilarityCalculators();
|
---|
| 296 | ParameterizeMainLoop();
|
---|
| 297 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 298 | base.OnProblemChanged();
|
---|
| 299 | }
|
---|
| 300 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 301 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 302 | ParameterizeSolutionsCreator();
|
---|
| 303 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 304 | }
|
---|
| 305 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 306 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 307 | ParameterizeSolutionsCreator();
|
---|
| 308 | ParameterizeMainLoop();
|
---|
| 309 | ParameterizeAnalyzers();
|
---|
| 310 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 311 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 312 | }
|
---|
| 313 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[8086] | 314 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[7789] | 315 | UpdateAnalyzers();
|
---|
| 316 | UpdateCrossovers();
|
---|
| 317 | UpdatePathRelinkers();
|
---|
| 318 | UpdateSimilarityCalculators();
|
---|
| 319 | UpdateImprovers();
|
---|
| 320 | ParameterizeMainLoop();
|
---|
| 321 | ParameterizeAnalyzers();
|
---|
| 322 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 323 | }
|
---|
[11839] | 324 | private void PathRelinkerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 325 | ExecutePathRelinking.Value = PathRelinkerParameter.Value != null;
|
---|
| 326 | }
|
---|
[8628] | 327 | private void SimilarityCalculatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 328 | ParameterizeMainLoop();
|
---|
| 329 | }
|
---|
[7789] | 330 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 331 | ParameterizeMainLoop();
|
---|
| 332 | ParameterizeAnalyzers();
|
---|
| 333 | ParameterizeBestSelector();
|
---|
[8628] | 334 | ParameterizeSimilarityCalculators();
|
---|
[7789] | 335 | }
|
---|
| 336 | #endregion
|
---|
| 337 |
|
---|
| 338 | #region Helpers
|
---|
| 339 | private void Initialize() {
|
---|
[11839] | 340 | PathRelinkerParameter.ValueChanged += new EventHandler(PathRelinkerParameter_ValueChanged);
|
---|
[8628] | 341 | SimilarityCalculatorParameter.ValueChanged += new EventHandler(SimilarityCalculatorParameter_ValueChanged);
|
---|
[8346] | 342 | if (Problem != null)
|
---|
[7789] | 343 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 344 | }
|
---|
| 345 | private void UpdateAnalyzers() {
|
---|
| 346 | Analyzer.Operators.Clear();
|
---|
| 347 | if (Problem != null) {
|
---|
| 348 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 349 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 350 | param.Depth = 1;
|
---|
| 351 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
| 355 | }
|
---|
| 356 | private void UpdateCrossovers() {
|
---|
| 357 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
| 358 | CrossoverParameter.ValidValues.Clear();
|
---|
| 359 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 360 |
|
---|
| 361 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 362 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 363 |
|
---|
| 364 | if (oldCrossover != null) {
|
---|
| 365 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 366 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
| 367 | else oldCrossover = null;
|
---|
| 368 | }
|
---|
| 369 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 370 | CrossoverParameter.Value = defaultCrossover;
|
---|
| 371 | }
|
---|
| 372 | private void UpdateImprovers() {
|
---|
| 373 | IImprovementOperator oldImprover = ImproverParameter.Value;
|
---|
| 374 | ImproverParameter.ValidValues.Clear();
|
---|
| 375 | IImprovementOperator defaultImprover = Problem.Operators.OfType<IImprovementOperator>().FirstOrDefault();
|
---|
| 376 |
|
---|
| 377 | foreach (IImprovementOperator improver in Problem.Operators.OfType<IImprovementOperator>().OrderBy(x => x.Name))
|
---|
| 378 | ImproverParameter.ValidValues.Add(improver);
|
---|
| 379 |
|
---|
| 380 | if (oldImprover != null) {
|
---|
| 381 | IImprovementOperator improver = ImproverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldImprover.GetType());
|
---|
| 382 | if (improver != null) ImproverParameter.Value = improver;
|
---|
| 383 | else oldImprover = null;
|
---|
| 384 | }
|
---|
| 385 | if (oldImprover == null && defaultImprover != null)
|
---|
| 386 | ImproverParameter.Value = defaultImprover;
|
---|
| 387 | }
|
---|
| 388 | private void UpdatePathRelinkers() {
|
---|
| 389 | IPathRelinker oldPathRelinker = PathRelinkerParameter.Value;
|
---|
| 390 | PathRelinkerParameter.ValidValues.Clear();
|
---|
| 391 | IPathRelinker defaultPathRelinker = Problem.Operators.OfType<IPathRelinker>().FirstOrDefault();
|
---|
| 392 |
|
---|
| 393 | foreach (IPathRelinker pathRelinker in Problem.Operators.OfType<IPathRelinker>().OrderBy(x => x.Name))
|
---|
| 394 | PathRelinkerParameter.ValidValues.Add(pathRelinker);
|
---|
| 395 |
|
---|
| 396 | if (oldPathRelinker != null) {
|
---|
| 397 | IPathRelinker pathRelinker = PathRelinkerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldPathRelinker.GetType());
|
---|
| 398 | if (pathRelinker != null) PathRelinkerParameter.Value = pathRelinker;
|
---|
| 399 | else oldPathRelinker = null;
|
---|
| 400 | }
|
---|
| 401 | if (oldPathRelinker == null && defaultPathRelinker != null)
|
---|
| 402 | PathRelinkerParameter.Value = defaultPathRelinker;
|
---|
| 403 | }
|
---|
| 404 | private void UpdateSimilarityCalculators() {
|
---|
[12102] | 405 | ISolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
|
---|
[7789] | 406 | SimilarityCalculatorParameter.ValidValues.Clear();
|
---|
[12102] | 407 | ISolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISolutionSimilarityCalculator>().FirstOrDefault();
|
---|
[7789] | 408 |
|
---|
[12102] | 409 | foreach (ISolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISolutionSimilarityCalculator>())
|
---|
[8319] | 410 | SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
|
---|
[7789] | 411 |
|
---|
[12113] | 412 | if (!SimilarityCalculatorParameter.ValidValues.OfType<QualitySimilarityCalculator>().Any())
|
---|
| 413 | SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator {
|
---|
| 414 | QualityVariableName = Problem.Evaluator.QualityParameter.ActualName
|
---|
| 415 | });
|
---|
| 416 | if (!SimilarityCalculatorParameter.ValidValues.OfType<NoSimilarityCalculator>().Any())
|
---|
[12106] | 417 | SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator());
|
---|
| 418 |
|
---|
[8319] | 419 | if (oldSimilarityCalculator != null) {
|
---|
[12102] | 420 | ISolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
|
---|
[8319] | 421 | if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
|
---|
| 422 | else oldSimilarityCalculator = null;
|
---|
[7789] | 423 | }
|
---|
[8319] | 424 | if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
|
---|
| 425 | SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
|
---|
[7789] | 426 | }
|
---|
| 427 | private void ParameterizeBestSelector() {
|
---|
| 428 | OperatorGraph.Operators.OfType<ISingleObjectiveSelector>()
|
---|
| 429 | .First().QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 430 | }
|
---|
| 431 | private void ParameterizeSolutionsCreator() {
|
---|
| 432 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 433 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 434 | }
|
---|
| 435 | private void ParameterizeMainLoop() {
|
---|
| 436 | if (Problem != null && Improver != null) {
|
---|
| 437 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 438 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 439 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[8319] | 440 | MainLoop.OperatorGraph.Operators.OfType<PopulationRebuildMethod>().Single().QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 441 | MainLoop.OperatorGraph.Operators.OfType<SolutionPoolUpdateMethod>().Single().QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[7789] | 442 | }
|
---|
| 443 | }
|
---|
| 444 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 445 | if (op is IStochasticOperator) {
|
---|
| 446 | IStochasticOperator stOp = (IStochasticOperator)op;
|
---|
| 447 | stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 448 | stOp.RandomParameter.Hidden = true;
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 | private void ParameterizeAnalyzers() {
|
---|
| 452 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 453 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
| 454 | if (Problem != null) {
|
---|
| 455 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 456 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
| 457 | qualityAnalyzer.QualityParameter.Hidden = false;
|
---|
| 458 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 459 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
| 460 | } else {
|
---|
| 461 | qualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
| 462 | qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
[8628] | 465 | private void ParameterizeSimilarityCalculators() {
|
---|
[12102] | 466 | foreach (ISolutionSimilarityCalculator calc in SimilarityCalculatorParameter.ValidValues) {
|
---|
[8628] | 467 | calc.QualityVariableName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
[7789] | 470 | private ScatterSearchMainLoop FindMainLoop(IOperator start) {
|
---|
| 471 | IOperator mainLoop = start;
|
---|
| 472 | while (mainLoop != null && !(mainLoop is ScatterSearchMainLoop))
|
---|
| 473 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 474 | return mainLoop as ScatterSearchMainLoop;
|
---|
| 475 | }
|
---|
| 476 | #endregion
|
---|
| 477 | }
|
---|
| 478 | }
|
---|