1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 | using HeuristicLab.Problems.Scheduling.Encodings;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.Scheduling.Analyzers {
|
---|
35 |
|
---|
36 | [Item("BestSchedulingSolutionAnalyzer", "An operator for analyzing the best solution of Scheduling Problems given in schedule-representation.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class BestSchedulingSolutionAnalyzer : JSSPOperator, IAnalyzer, IStochasticOperator {
|
---|
39 | [StorableConstructor]
|
---|
40 | private BestSchedulingSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
41 | private BestSchedulingSolutionAnalyzer(BestSchedulingSolutionAnalyzer original, Cloner cloner)
|
---|
42 | : base(original, cloner) {
|
---|
43 | }
|
---|
44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
45 | return new BestSchedulingSolutionAnalyzer(this, cloner);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | public ILookupParameter<IRandom> RandomParameter {
|
---|
50 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
51 | }
|
---|
52 | public IRandom Random {
|
---|
53 | get { return RandomParameter.ActualValue; }
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
58 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
59 | }
|
---|
60 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
61 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
62 | }
|
---|
63 | public LookupParameter<Schedule> BestSolutionParameter {
|
---|
64 | get { return (LookupParameter<Schedule>)Parameters["BestSolution"]; }
|
---|
65 | }
|
---|
66 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
67 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
68 | }
|
---|
69 | public LookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
70 | get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
71 | }
|
---|
72 | public LookupParameter<Schedule> BestKnownSolutionParameter {
|
---|
73 | get { return (LookupParameter<Schedule>)Parameters["BestKnownSolution"]; }
|
---|
74 | }
|
---|
75 | public ScopeTreeLookupParameter<Schedule> SchedulingSolutionParameter {
|
---|
76 | get { return (ScopeTreeLookupParameter<Schedule>)Parameters["DecodedSchedulingSolution"]; }
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 | public BestSchedulingSolutionAnalyzer () {
|
---|
82 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
|
---|
83 |
|
---|
84 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
|
---|
85 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the JSSP solutions which should be analyzed."));
|
---|
86 | Parameters.Add(new ScopeTreeLookupParameter<Schedule>("DecodedSchedulingSolution", "The solutions from which the best solution has to be chosen from."));
|
---|
87 | Parameters.Add(new LookupParameter<Schedule>("BestSolution", "The best JSSP solution."));
|
---|
88 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best JSSP solution should be stored."));
|
---|
89 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this JSSP instance."));
|
---|
90 | Parameters.Add(new LookupParameter<Schedule>("BestKnownSolution", "The best known solution of this JSSP instance."));
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | public override IOperation Apply() {
|
---|
95 | ItemList<JSSPJob> jobs = Jobs;
|
---|
96 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
97 | ItemArray<Schedule> solutions = SchedulingSolutionParameter.ActualValue;
|
---|
98 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
99 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
100 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
101 |
|
---|
102 | int i = -1;
|
---|
103 | if (!max)
|
---|
104 | i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
105 | else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
|
---|
106 |
|
---|
107 | if (bestKnownQuality == null ||
|
---|
108 | max && qualities[i].Value > bestKnownQuality.Value ||
|
---|
109 | !max && qualities[i].Value < bestKnownQuality.Value) {
|
---|
110 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
|
---|
111 | BestKnownSolutionParameter.ActualValue = (Schedule)solutions[i].Clone();
|
---|
112 | }
|
---|
113 |
|
---|
114 | Schedule bestSolution = BestSolutionParameter.ActualValue;
|
---|
115 | if (bestSolution == null) {
|
---|
116 | bestSolution = (Schedule)solutions[i].Clone();
|
---|
117 | bestSolution.Quality.Value = qualities [i].Value;
|
---|
118 | BestSolutionParameter.ActualValue = bestSolution;
|
---|
119 | results.Add(new Result("Best Scheduling Solution", bestSolution));
|
---|
120 | } else {
|
---|
121 | if (max && bestSolution.Quality.Value < qualities[i].Value ||
|
---|
122 | !max && bestSolution.Quality.Value > qualities[i].Value) {
|
---|
123 | bestSolution = (Schedule)solutions[i].Clone();
|
---|
124 | bestSolution.Quality.Value = qualities[i].Value;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | return base.Apply();
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|
132 | }
|
---|