Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Logging/BestSolutionStorer.cs @ 6748

Last change on this file since 6748 was 368, checked in by gkronber, 16 years ago

fixed a bug in BestSolutionStorer

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