Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScopedAlgorithms/HeuristicLab.Problems.Scheduling/3.3/Analyzers/BestSchedulingSolutionAnalyzer.cs @ 14815

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

#2521: Intermediate version of schedule encoding refactoring.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.ScheduleEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.Scheduling {
33
34  [Item("BestSchedulingSolutionAnalyzer", "An operator for analyzing the best solution of Scheduling Problems given in schedule-representation.")]
35  [StorableClass]
36  public sealed class BestSchedulingSolutionAnalyzer : SchedulingAnalyzer, IStochasticOperator {
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
53    public BestSchedulingSolutionAnalyzer()
54      : base() {
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;
60      ItemArray<Schedule> solutions = ScheduleParameter.ActualValue;
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);
74        BestKnownSolutionParameter.ActualValue = (Schedule)solutions[i].Clone();
75      }
76
77      Schedule bestSolution = BestSolutionParameter.ActualValue;
78      if (bestSolution == null) {
79        bestSolution = (Schedule)solutions[i].Clone();
80        bestSolution.Quality = qualities[i].Value;
81        BestSolutionParameter.ActualValue = bestSolution;
82        results.Add(new Result("Best Scheduling Solution", bestSolution));
83      } else {
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());
89        }
90      }
91
92      return base.Apply();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.