Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/3.2/SimpleBestSolutionStorer.cs @ 2402

Last change on this file since 2402 was 1782, checked in by abeham, 16 years ago

Added SimpleBestSolutionStorer (#614)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Logging {
29  public class SimpleBestSolutionStorer : OperatorBase {
30
31    public override string Description {
32      get { return @"The simple best solution storer, stores the best solution.
33If the rolling horizon is <= 0 the best solution is never forgotten. If this horizon is 1 only the actual best solution is stored.
34If the horizon is n always the best of the last n solutions is stored.
35
36The operator expects the solutions to be sorted so that the best solution is in the first scope."; }
37    }
38
39    public SimpleBestSolutionStorer()
40      : base() {
41      AddVariableInfo(new VariableInfo("Quality", "Quality value of a solution", typeof(DoubleData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("Maximization", "Whether the problem is a maximization problem or a minimization problem", typeof(BoolData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("BestSolution", "The scope representing the best solution", typeof(IScope), VariableKind.New | VariableKind.Out | VariableKind.In));
44      AddVariableInfo(new VariableInfo("LastBestScopes", "Store a maximum of RollingHorizon solutions", typeof(ItemList<IScope>), VariableKind.New | VariableKind.Out | VariableKind.In));
45      AddVariableInfo(new VariableInfo("RollingHorizon", "The rolling time horizon", typeof(IntData), VariableKind.In));
46      GetVariableInfo("RollingHorizon").Local = true;
47      AddVariable(new Variable("RollingHorizon", new IntData(0)));
48    }
49
50    public override IOperation Apply(IScope scope) {
51      int horizon = GetVariableValue<IntData>("RollingHorizon", scope, true).Data;
52      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
53      double currentBestQuality, bestQuality;
54      IVariable bestSolutionVar = null;
55      IScope bestSolutionScope = null;
56      IScope currentSolutionScope = (IScope)scope.SubScopes[0].Clone();
57
58      #region variable setting
59      IVariableInfo bestSolutionInfo = GetVariableInfo("BestSolution");
60      if (bestSolutionInfo.Local == true)
61        bestSolutionVar = GetVariable(bestSolutionInfo.ActualName);
62      else bestSolutionVar = scope.GetVariable(scope.TranslateName("BestSolution"));
63     
64      if (bestSolutionVar != null) {
65        bestSolutionScope = bestSolutionVar.Value as IScope;
66        bestQuality = bestSolutionScope.GetVariableValue<DoubleData>(scope.TranslateName("Quality"), false).Data;
67        currentBestQuality = currentSolutionScope.GetVariableValue<DoubleData>("Quality", false).Data;
68      } else { // if no best solution exists then the first scope is added as the best solution and the operator is finished
69        if (bestSolutionInfo.Local) AddVariable(new Variable(bestSolutionInfo.ActualName, currentSolutionScope));
70        else scope.AddVariable(new Variable(scope.TranslateName("BestSolution"), currentSolutionScope));
71        return null;
72      }
73      #endregion
74
75      if (horizon <= 0) { // never forget the best quality
76        if (currentBestQuality < bestQuality) {
77          bestSolutionVar.Value = currentSolutionScope;
78        }
79      } else { // forget the best found quality after a certain time (given as horizon)
80        ItemList<IScope> lastNBestScopes = scope.GetVariableValue<ItemList<IScope>>("LastBestScopes", false, false);
81        if (lastNBestScopes == null) {
82          lastNBestScopes = new ItemList<IScope>();
83          scope.AddVariable(new Variable(scope.TranslateName("LastBestScopes"), lastNBestScopes));
84        }
85        bool currentIsNewBest = maximization && currentBestQuality > bestQuality || !maximization && currentBestQuality < bestQuality;
86        if (currentIsNewBest) {
87          bestSolutionScope = currentSolutionScope;
88          bestSolutionVar.Value = bestSolutionScope;
89          lastNBestScopes.Clear(); // the solutions that came before cannot be better, we don't need to keep them
90        }
91        lastNBestScopes.Add(currentSolutionScope); // add the current solution to the memory
92        if (lastNBestScopes.Count > horizon) {
93          if (!currentIsNewBest && lastNBestScopes.IndexOf(bestSolutionScope) == 0) { // if the best is to be "forgotton" the new best has to be found
94            bool bestChanged = false;
95            if (maximization) bestQuality = Double.MinValue;
96            else bestQuality = Double.MaxValue;
97            for (int i = 1; i < lastNBestScopes.Count; i++) {
98              double quality = lastNBestScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
99              if (maximization && quality > bestQuality
100                || !maximization && quality < bestQuality) {
101                bestSolutionScope = lastNBestScopes[i];
102                bestQuality = quality;
103                bestChanged = true;
104              }
105            }
106            if (bestChanged) bestSolutionVar.Value = bestSolutionScope;
107          }
108          lastNBestScopes.RemoveAt(0);
109        }
110      }
111      return null;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.