[12643] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12643] | 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.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
| 33 | [Item("Swap2MoveMaker", "Peforms a swap-2 move on a given grouping and updates the quality.")]
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public class Swap2MoveMaker : SingleSuccessorOperator, ILinearLinkageSwap2MoveOperator, IMoveMaker, ISingleObjectiveOperator {
|
---|
| 36 | public override bool CanChangeName {
|
---|
| 37 | get { return false; }
|
---|
| 38 | }
|
---|
| 39 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 40 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 41 | }
|
---|
| 42 | public ILookupParameter<DoubleValue> MoveQualityParameter {
|
---|
| 43 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
| 44 | }
|
---|
| 45 | public ILookupParameter<Swap2Move> Swap2MoveParameter {
|
---|
| 46 | get { return (ILookupParameter<Swap2Move>)Parameters["Swap2Move"]; }
|
---|
| 47 | }
|
---|
| 48 | public ILookupParameter<LinearLinkage> LLEParameter {
|
---|
| 49 | get { return (ILookupParameter<LinearLinkage>)Parameters["LLE"]; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [StorableConstructor]
|
---|
| 53 | protected Swap2MoveMaker(bool deserializing) : base(deserializing) { }
|
---|
| 54 | protected Swap2MoveMaker(Swap2MoveMaker original, Cloner cloner) : base(original, cloner) { }
|
---|
| 55 | public Swap2MoveMaker()
|
---|
| 56 | : base() {
|
---|
| 57 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the solution."));
|
---|
| 58 | Parameters.Add(new LookupParameter<Swap2Move>("Swap2Move", "The move to apply."));
|
---|
| 59 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The relative quality of the move."));
|
---|
| 60 | Parameters.Add(new LookupParameter<LinearLinkage>("LLE", "The linear linkage encoded solution to which the move should be applied."));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 64 | return new Swap2MoveMaker(this, cloner);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public static void Apply(LinearLinkage lle, Swap2Move move) {
|
---|
| 68 | var groups = lle.GetGroups().ToList();
|
---|
| 69 | int g1 = -1, g2 = -1, g1Idx = -1, g2Idx = -1;
|
---|
| 70 | for (var i = 0; i < groups.Count; i++) {
|
---|
| 71 | if (g1 < 0) {
|
---|
| 72 | g1Idx = groups[i].IndexOf(move.Item1);
|
---|
| 73 | if (g1Idx >= 0) {
|
---|
| 74 | g1 = i;
|
---|
| 75 | continue;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | if (g2 < 0) {
|
---|
| 79 | g2Idx = groups[i].IndexOf(move.Item2);
|
---|
| 80 | if (g2Idx >= 0) g2 = i;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | // can happen if (for some reason) the items belong to the same group
|
---|
| 85 | if (g1 < 0 || g2 < 0) throw new InvalidOperationException("Swap2MoveMaker: Cannot apply swap move, items are not found in different groups.");
|
---|
| 86 |
|
---|
| 87 | var h = groups[g1][g1Idx];
|
---|
| 88 | groups[g1][g1Idx] = groups[g2][g2Idx];
|
---|
| 89 | groups[g2][g2Idx] = h;
|
---|
| 90 | lle.SetGroups(groups);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public override IOperation Apply() {
|
---|
| 94 | var move = Swap2MoveParameter.ActualValue;
|
---|
| 95 | var lle = LLEParameter.ActualValue;
|
---|
| 96 | var moveQuality = MoveQualityParameter.ActualValue;
|
---|
| 97 | var quality = QualityParameter.ActualValue;
|
---|
| 98 |
|
---|
| 99 | Apply(lle, move);
|
---|
| 100 |
|
---|
| 101 | quality.Value = moveQuality.Value;
|
---|
| 102 |
|
---|
| 103 | return base.Apply();
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|