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