Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs @ 7722

Last change on this file since 7722 was 7722, checked in by jkarder, 12 years ago

#1331: Scatter Search initial commit

File size: 3.8 KB
Line 
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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.ScatterSearch {
31  /// <summary>
32  /// An operator that updates the solution pool.
33  /// </summary>
34  [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
35  [StorableClass]
36  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator {
37    #region Parameter properties
38    private ScopeParameter CurrentScopeParameter {
39      get { return (ScopeParameter)Parameters["CurrentScope"]; }
40    }
41    private ValueLookupParameter<BoolValue> NewSolutionsParameter {
42      get { return (ValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
43    }
44    #endregion
45
46    #region Properties
47    public IScope CurrentScope {
48      get { return CurrentScopeParameter.ActualValue; }
49    }
50    public BoolValue NewSolutions {
51      get { return NewSolutionsParameter.ActualValue; }
52    }
53    #endregion
54
55    [StorableConstructor]
56    private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { }
57    private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
58    public SolutionPoolUpdateMethod() : base() { Initialize(); }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new SolutionPoolUpdateMethod(this, cloner);
62    }
63
64    private void Initialize() {
65      #region Create parameters
66      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "Indicates if new solutions have been found."));
67      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
68      #endregion
69
70      #region Create operators
71      #endregion
72
73      #region Create operator graph
74      #endregion
75    }
76
77    public override IOperation Apply() {
78      IScope parents = new Scope("Parents");
79      IScope offspring = new Scope("Offspring");
80      foreach (var scope in CurrentScope.SubScopes) {
81        parents.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
82        offspring.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
83      }
84      CurrentScope.SubScopes.Clear();
85      CurrentScope.SubScopes.AddRange(parents.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value));
86      if ((offspring.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value).First().Variables["Quality"].Value as DoubleValue).Value
87          > (parents.SubScopes.OrderByDescending(o => o.Variables["Quality"].Value).First().Variables["Quality"].Value as DoubleValue).Value) {
88        CurrentScope.SubScopes.Replace(CurrentScope.SubScopes.Union(offspring.SubScopes).OrderByDescending(o => o.Variables["Quality"].Value).Take(10));
89        NewSolutions.Value = true;
90      }
91      return base.Apply();
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.