Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Problems.Scheduling/3.3/Analyzers/BestSchedulingSolutionAnalyzer.cs @ 15104

Last change on this file since 15104 was 13435, checked in by mkommend, 9 years ago

#2521: Intermediate version of schedule encoding refactoring.

File size: 3.9 KB
RevLine 
[6121]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6121]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
[13435]22using System.Collections.Generic;
[6121]23using System.Linq;
[6406]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.ScheduleEncoding;
[6121]28using HeuristicLab.Optimization;
[6406]29using HeuristicLab.Parameters;
[6121]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[6406]32namespace HeuristicLab.Problems.Scheduling {
[6121]33
34  [Item("BestSchedulingSolutionAnalyzer", "An operator for analyzing the best solution of Scheduling Problems given in schedule-representation.")]
35  [StorableClass]
[6266]36  public sealed class BestSchedulingSolutionAnalyzer : SchedulingAnalyzer, IStochasticOperator {
[6121]37    [StorableConstructor]
38    private BestSchedulingSolutionAnalyzer(bool deserializing) : base(deserializing) { }
39    private BestSchedulingSolutionAnalyzer(BestSchedulingSolutionAnalyzer original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new BestSchedulingSolutionAnalyzer(this, cloner);
44    }
45
46    public ILookupParameter<IRandom> RandomParameter {
47      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
48    }
49    public IRandom Random {
50      get { return RandomParameter.ActualValue; }
51    }
52
[6406]53    public BestSchedulingSolutionAnalyzer()
54      : base() {
[6121]55      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
56    }
57
58    public override IOperation Apply() {
59      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
[6406]60      ItemArray<Schedule> solutions = ScheduleParameter.ActualValue;
[6121]61      ResultCollection results = ResultsParameter.ActualValue;
62      bool max = MaximizationParameter.ActualValue.Value;
63      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
64
65      int i = -1;
66      if (!max)
67        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
68      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
69
70      if (bestKnownQuality == null ||
71          max && qualities[i].Value > bestKnownQuality.Value ||
72          !max && qualities[i].Value < bestKnownQuality.Value) {
73        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
[6177]74        BestKnownSolutionParameter.ActualValue = (Schedule)solutions[i].Clone();
[6121]75      }
76
[6177]77      Schedule bestSolution = BestSolutionParameter.ActualValue;
[6121]78      if (bestSolution == null) {
[6177]79        bestSolution = (Schedule)solutions[i].Clone();
[13435]80        bestSolution.Quality = qualities[i].Value;
[6121]81        BestSolutionParameter.ActualValue = bestSolution;
82        results.Add(new Result("Best Scheduling Solution", bestSolution));
83      } else {
[13435]84        if (max && bestSolution.Quality < qualities[i].Value ||
85          !max && bestSolution.Quality > qualities[i].Value) {
86          bestSolution.Quality = qualities[i].Value;
87          bestSolution.Resources.Clear();
88          bestSolution.Resources.AddRange((IEnumerable<Resource>)solutions[i].Resources.Clone());
[6121]89        }
90      }
91
92      return base.Apply();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.