[6121] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[8603] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System.Linq;
|
---|
[6406] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.ScheduleEncoding;
|
---|
[6121] | 27 | using HeuristicLab.Optimization;
|
---|
[6406] | 28 | using HeuristicLab.Parameters;
|
---|
[6121] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[6406] | 31 | namespace HeuristicLab.Problems.Scheduling {
|
---|
[6121] | 32 |
|
---|
| 33 | [Item("BestSchedulingSolutionAnalyzer", "An operator for analyzing the best solution of Scheduling Problems given in schedule-representation.")]
|
---|
| 34 | [StorableClass]
|
---|
[6266] | 35 | public sealed class BestSchedulingSolutionAnalyzer : SchedulingAnalyzer, IStochasticOperator {
|
---|
[6121] | 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 |
|
---|
[6406] | 52 | public BestSchedulingSolutionAnalyzer()
|
---|
| 53 | : base() {
|
---|
[6121] | 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;
|
---|
[6406] | 59 | ItemArray<Schedule> solutions = ScheduleParameter.ActualValue;
|
---|
[6121] | 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);
|
---|
[6177] | 73 | BestKnownSolutionParameter.ActualValue = (Schedule)solutions[i].Clone();
|
---|
[6121] | 74 | }
|
---|
| 75 |
|
---|
[6177] | 76 | Schedule bestSolution = BestSolutionParameter.ActualValue;
|
---|
[6121] | 77 | if (bestSolution == null) {
|
---|
[6177] | 78 | bestSolution = (Schedule)solutions[i].Clone();
|
---|
[6406] | 79 | bestSolution.Quality = (DoubleValue)qualities[i].Clone();
|
---|
[6121] | 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;
|
---|
[6260] | 86 | bestSolution.Resources = (ItemList<Resource>)solutions[i].Resources.Clone();
|
---|
[6121] | 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return base.Apply();
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|