Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1331:

  • added custom crossover operator (NChildCrossover)
  • added parameters and adjusted types
  • replaced SolutionCombinationMethod with a placeholder
  • adjusted event handling
  • changed access levels
  • minor code improvements
File size: 5.3 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.ScatterSearch {
33  /// <summary>
34  /// An operator that updates the solution pool.
35  /// </summary>
36  [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
37  [StorableClass]
38  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator {
39    #region Parameter properties
40    public ScopeParameter CurrentScopeParameter {
41      get { return (ScopeParameter)Parameters["CurrentScope"]; }
42    }
43    public IValueLookupParameter<BoolValue> NewSolutionsParameter {
44      get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
45    }
46    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
47      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
48    }
49    public IValueLookupParameter<IItem> QualityParameter {
50      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
51    }
52    public IValueLookupParameter<IItem> SolutionParameter {
53      get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
54    }
55    #endregion
56
57    #region Properties
58    private IScope CurrentScope {
59      get { return CurrentScopeParameter.ActualValue; }
60    }
61    private BoolValue NewSolutions {
62      get { return NewSolutionsParameter.ActualValue; }
63    }
64    private IntValue ReferenceSetSize {
65      get { return ReferenceSetSizeParameter.ActualValue; }
66      set { ReferenceSetSizeParameter.ActualValue = value; }
67    }
68    private IItem Quality {
69      get { return QualityParameter.ActualValue; }
70    }
71    #endregion
72
73    [StorableConstructor]
74    private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { }
75    private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
76    public SolutionPoolUpdateMethod() : base() { Initialize(); }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new SolutionPoolUpdateMethod(this, cloner);
80    }
81
82    private void Initialize() {
83      #region Create parameters
84      Parameters.Add(new ScopeParameter("CurrentScope"));
85      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions"));
86      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
87      Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
88      Parameters.Add(new ValueLookupParameter<IItem>("Solution"));
89      #endregion
90      SolutionParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
91    }
92
93    public override IOperation Apply() {
94      IScope parents = new Scope("Parents");
95      IScope offspring = new Scope("Offspring");
96      foreach (var scope in CurrentScope.SubScopes) {
97        parents.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
98        offspring.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
99      }
100      CurrentScope.SubScopes.Clear();
101      CurrentScope.SubScopes.AddRange(parents.SubScopes.OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value));
102      if ((offspring.SubScopes.OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value).First().Variables[QualityParameter.ActualName].Value as DoubleValue).Value
103          > (parents.SubScopes.OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value).Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value) {
104        CurrentScope.SubScopes.Replace(parents.SubScopes.Concat(offspring.SubScopes.Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[SolutionParameter.ActualName].Value.ToString()))).OrderByDescending(o => o.Variables[QualityParameter.ActualName].Value).Take(ReferenceSetSize.Value));
105        NewSolutions.Value = true;
106      }
107
108      return base.Apply();
109    }
110
111    private class KeyEqualityComparer<T> : IEqualityComparer<T> {
112      private readonly Func<T, object> keyExtractor;
113
114      public KeyEqualityComparer(Func<T, object> keyExtractor) {
115        this.keyExtractor = keyExtractor;
116      }
117
118      public bool Equals(T x, T y) {
119        return keyExtractor(x).Equals(keyExtractor(y));
120      }
121
122      public int GetHashCode(T obj) {
123        return keyExtractor(obj).GetHashCode();
124      }
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.