Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.Permutation/3.3/Moves/ExhaustiveTwoOptMoveGenerator.cs @ 3026

Last change on this file since 3026 was 3017, checked in by epitzer, 15 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Operators;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.Permutation {
29  [Item("ExhaustiveTwoOptMoveGenerator", "Generates all possible 2-opt moves from a given permutation.")]
30  [StorableClass]
31  public class ExhaustiveTwoOptMoveGenerator : SingleSuccessorOperator, IPermutationMoveGenerator {
32    public ILookupParameter<Permutation> PermutationParameter {
33      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
34    }
35    public LookupParameter<TwoOptMove> MoveParameter {
36      get { return (LookupParameter<TwoOptMove>)Parameters["Move"]; }
37    }
38    private ScopeParameter CurrentScopeParameter {
39      get { return (ScopeParameter)Parameters["CurrentScope"]; }
40    }
41
42    public ExhaustiveTwoOptMoveGenerator()
43      : base() {
44      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation for which moves should be generated."));
45      Parameters.Add(new LookupParameter<TwoOptMove>("Move", "The moves that should be generated in subscopes."));
46      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
47    }
48
49    public override IOperation Apply() {
50      Permutation p = PermutationParameter.ActualValue;
51      int length = p.Length;
52      int totalMoves = (length) * (length - 1) / 2 - 3;
53      Scope[] moveScopes = new Scope[totalMoves];
54      int count = 0;
55      for (int i = 0; i < length - 1; i++)
56        for (int j = i + 1; j < length; j++) {
57          // doesn't make sense to inverse the whole permutation or the whole but one
58          if (i == 0 && j >= length - 2) continue;
59          else if (i == 1 && j >= length - 1) continue;
60          Scope s = new Scope(count.ToString());
61          s.Variables.Add(new Variable(MoveParameter.ActualName, new TwoOptMove(i, j)));
62          moveScopes[count] = s;
63          count++;
64        }
65      // FIXME: remove the line below
66      if (count != totalMoves) throw new InvalidOperationException(Name + ": totalMoves != count");
67      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
68      return base.Apply();
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.