Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2645_ContextAlgorithms/HeuristicLab.Problems.Scheduling/3.3/Analyzers/BestSchedulingSolutionAnalyzer.cs @ 16752

Last change on this file since 16752 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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