Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Logging/3.2/BestSolutionStorer.cs @ 2787

Last change on this file since 2787 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
27using HeuristicLab.Operators;
28
29namespace HeuristicLab.Logging {
30  /// <summary>
31  /// Keeps a variable in the global scope that contains the scope representing the best solution.
32  /// </summary>
33  public class BestSolutionStorer : DelegatingOperator {
34    /// <inheritdoc select="summary"/>
35    public override string Description {
36      get { return @"Keeps a variable in the global scope that contains the scope representing the best of run solution."; }
37    }
38
39    /// <summary>
40    /// Initializes a new instance of <see cref="BestSolutionStorer"/> with three variable infos
41    /// (<c>Quality</c>, <c>Maximization</c> and <c>BestSolution</c>).
42    /// </summary>
43    public BestSolutionStorer()
44      : base() {
45      AddVariableInfo(new VariableInfo("Quality", "Quality value of a solution", typeof(DoubleData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("BestSolution", "The best solution of the run", typeof(IScope), VariableKind.New | VariableKind.In | VariableKind.Out));
48    }
49
50    /// <summary>
51    /// Keeps a variable in the global scope that contains the scope representing the best solution.
52    /// </summary>
53    /// <param name="scope">The scope whose populations to check for the best solution.</param>
54    /// <returns><c>null</c>.</returns>
55    public override IOperation Apply(IScope scope) {
56      if(scope.GetVariable(Guid.ToString() + "-Active") == null) {
57        double[] qualities = new double[scope.SubScopes.Count];
58        bool maximization = scope.GetVariableValue<BoolData>("Maximization", true).Data;
59        for(int i = 0; i < scope.SubScopes.Count; i++)
60          qualities[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
61
62        double smallest = qualities[0]; int smallestIndex = 0;
63        double biggest = qualities[0]; int biggestIndex = 0;
64        for(int i = 1; i < qualities.Length; i++) {
65          if(qualities[i] < smallest) {
66            smallest = qualities[i];
67            smallestIndex = i;
68          }
69          if(qualities[i] > biggest) {
70            biggest = qualities[i];
71            biggestIndex = i;
72          }
73        }
74        IVariableInfo qualityInfo = GetVariableInfo("Quality");
75        IVariable bestSolutionVariable = scope.GetVariable(scope.TranslateName("BestSolution"));
76        if(bestSolutionVariable != null) {
77
78          double bestQuality = ((IScope)bestSolutionVariable.Value).GetVariableValue<DoubleData>(qualityInfo.ActualName, false).Data;
79
80          // do nothing if the best solution of the current scope is not better than the best solution of the whole run so far.
81          if((maximization && biggest <= bestQuality) ||
82            (!maximization && smallest >= bestQuality)) return null;
83        }
84
85        IScope bestSolutionClone;
86        if(maximization) {
87          bestSolutionClone = (IScope)scope.SubScopes[biggestIndex].Clone();
88        } else {
89          bestSolutionClone = (IScope)scope.SubScopes[smallestIndex].Clone();
90        }
91
92        if(SubOperators.Count > 0) {
93          scope.AddSubScope(bestSolutionClone);
94          scope.AddVariable(new Variable(Guid.ToString() + "-Active", new BoolData(true)));
95
96          CompositeOperation compOp = new CompositeOperation();
97          AtomicOperation operation = new AtomicOperation(SubOperators[0], bestSolutionClone);
98          AtomicOperation continuation = new AtomicOperation(this, scope);
99
100          compOp.AddOperation(operation);
101          compOp.AddOperation(continuation);
102          return compOp;
103        } else {
104          StoreBestSolution(bestSolutionClone, scope);
105          return null;
106        }
107      } else {  // operator already executed
108        scope.RemoveVariable(Guid.ToString() + "-Active");
109        IScope bestSolutionClone = scope.SubScopes[scope.SubScopes.Count - 1];
110        scope.RemoveSubScope(bestSolutionClone);
111
112        StoreBestSolution(bestSolutionClone, scope);
113        return null;
114      }
115    }
116
117    private void StoreBestSolution(IScope bestSolution, IScope scope) {
118      SetValue(GetVariableInfo("BestSolution"), bestSolution, scope);
119    }
120
121    private void SetValue(IVariableInfo info, IScope data, IScope scope) {
122      IVariable var = scope.GetVariable(info.ActualName);
123      if(var == null) {
124        var = new Variable(scope.TranslateName(info.FormalName), data);
125        scope.AddVariable(var);
126      } else {
127        var.Value = data;
128      }
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.