Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionCombinationMethod.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.9 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.Encodings.BinaryVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.ScatterSearch {
31  /// <summary>
32  /// An operator that combines solutions to three trial solutions.
33  /// </summary>
34  [Item("SolutionCombinationMethod", "An operator that combines solutions to three trial solutions.")]
35  [StorableClass]
36  public sealed class SolutionCombinationMethod : SingleSuccessorOperator {
37    #region Parameter properties
38    private ScopeParameter CurrentScopeParameter {
39      get { return (ScopeParameter)Parameters["CurrentScope"]; }
40    }
41    #endregion
42
43    #region Properties
44    public IScope CurrentScope {
45      get { return CurrentScopeParameter.ActualValue; }
46    }
47    #endregion
48
49    [StorableConstructor]
50    private SolutionCombinationMethod(bool deserializing) : base(deserializing) { }
51    private SolutionCombinationMethod(SolutionCombinationMethod original, Cloner cloner) : base(original, cloner) { }
52    public SolutionCombinationMethod() : base() { Initialize(); }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new SolutionCombinationMethod(this, cloner);
56    }
57
58    private void Initialize() {
59      #region Create parameters
60      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
61      #endregion
62
63      #region Create operators
64      #endregion
65
66      #region Create operator graph
67      #endregion
68    }
69
70    public override IOperation Apply() {
71      IScope mother = CurrentScope.SubScopes[0];
72      IScope father = CurrentScope.SubScopes[1];
73      if (mother != null && father != null) {
74        BinaryVector motherSolution = mother.Variables["KnapsackSolution"].Value as BinaryVector;
75        BinaryVector fatherSolution = father.Variables["KnapsackSolution"].Value as BinaryVector;
76        int stepSize = motherSolution.Length / 3;
77        IVariable[] children = {
78          new Variable("KnapsackSolution", new BinaryVector(motherSolution.Take(stepSize).Concat(motherSolution.Skip(stepSize).Take(stepSize)).Concat(fatherSolution.Skip(2 * stepSize)).ToArray())),
79          new Variable("KnapsackSolution", new BinaryVector(motherSolution.Take(stepSize).Concat(fatherSolution.Skip(stepSize).Take(stepSize)).Concat(motherSolution.Skip(2 * stepSize)).ToArray())),
80          new Variable("KnapsackSolution", new BinaryVector(fatherSolution.Take(stepSize).Concat(motherSolution.Skip(stepSize).Take(stepSize)).Concat(motherSolution.Skip(2 * stepSize)).ToArray())),
81        };
82        IScope offspring = new Scope("Offspring");
83        foreach (var child in children) {
84          IScope solution = new Scope();
85          solution.Variables.Add(child);
86          offspring.SubScopes.Add(solution);
87        }
88        CurrentScope.SubScopes.Add(offspring);
89      }
90      return base.Apply();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.