1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
31 | [Item("WorstQualityAnalyzer", "An operator that collects the worst quality found.")]
|
---|
32 | [StorableClass]
|
---|
33 | public class WorstQualityAnalyzer : SingleSuccessorOperator, IStatefulItem {
|
---|
34 | private const string GenerationsParameterName = "Generations";
|
---|
35 |
|
---|
36 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
37 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
38 | }
|
---|
39 | public ILookupParameter<ItemArray<DoubleValue>> ParentsQualityParameter {
|
---|
40 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ParentsQuality"]; }
|
---|
41 | }
|
---|
42 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
43 | get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<DoubleValue> WorstKnownQualityParameter {
|
---|
46 | get { return (ILookupParameter<DoubleValue>)Parameters["WorstKnownQuality"]; }
|
---|
47 | }
|
---|
48 | [Storable]
|
---|
49 | private double upperBound = double.MinValue;
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | private WorstQualityAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
53 | private WorstQualityAnalyzer(WorstQualityAnalyzer original, Cloner cloner)
|
---|
54 | : base(original, cloner) {
|
---|
55 | upperBound = (double)original.upperBound;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public WorstQualityAnalyzer()
|
---|
59 | : base() {
|
---|
60 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ParentsQuality", "The quality of the parent solutions."));
|
---|
61 | ParentsQualityParameter.ActualName = "TSPTourLength";
|
---|
62 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
63 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
64 | Parameters.Add(new LookupParameter<DoubleValue>("WorstKnownQuality", "The quality of the worst known solution of this problem."));
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
68 | return new WorstQualityAnalyzer(this, cloner);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IOperation Apply() {
|
---|
72 | var qualityParent1 = ParentsQualityParameter.ActualValue.First().Value;
|
---|
73 | var qualityParent2 = ParentsQualityParameter.ActualValue.Last().Value;
|
---|
74 |
|
---|
75 | if (GenerationsParameter.ActualValue.Value == 0) {
|
---|
76 | if (MaximizationParameter.ActualValue.Value) {
|
---|
77 | if (upperBound > qualityParent1) {
|
---|
78 | upperBound = qualityParent1;
|
---|
79 | }
|
---|
80 | if (upperBound > qualityParent2) {
|
---|
81 | upperBound = qualityParent2;
|
---|
82 | }
|
---|
83 | } else {
|
---|
84 | if (upperBound < qualityParent1) {
|
---|
85 | upperBound = qualityParent1;
|
---|
86 | }
|
---|
87 | if (upperBound < qualityParent2) {
|
---|
88 | upperBound = qualityParent2;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | } else {
|
---|
92 | if (WorstKnownQualityParameter.ActualValue == null)
|
---|
93 | AddVariableToGlobalScope("WorstKnownQuality", "The quality of the worst known solution of this problem.", new DoubleValue(upperBound));
|
---|
94 | }
|
---|
95 |
|
---|
96 | return base.Apply();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public override void ClearState() {
|
---|
100 | upperBound = double.MinValue;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override void InitializeState() { }
|
---|
104 |
|
---|
105 | private void AddVariableToGlobalScope(string name, string description, DoubleValue value) {
|
---|
106 | var globalScope = ExecutionContext;
|
---|
107 | while (globalScope.Parent != null) {
|
---|
108 | globalScope = globalScope.Parent;
|
---|
109 | }
|
---|
110 |
|
---|
111 | globalScope.Scope.Variables.Add(new Variable(name, description, value));
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|