source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Moves/ThreeOpt/AlbaTranslocationMoveGenerator.cs @ 3991

Last change on this file since 3991 was 3991, checked in by svonolfe, 10 years ago

Fixed small issues in the VRP implementation (#1039)

File size: 5.0 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 System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Parameters;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
34  [Item("AlbaTranslocationMoveGenerator", "An operator which generates translocation moves for the alba representation.")]
35  [StorableClass]
36  public sealed class AlbaTranslocationMoveGenerator : AlbaMoveOperator, IPermutationTranslocationMoveOperator, IMultiMoveGenerator {
37    public IValueLookupParameter<TranslocationMoveGenerator> TranslocationMoveGeneratorParameter {
38      get { return (IValueLookupParameter<TranslocationMoveGenerator>)Parameters["TranslocationMoveGenerator"]; }
39    }
40
41    protected override IPermutationMoveOperator PermutationMoveOperatorParameter {
42      get { return TranslocationMoveGeneratorParameter.Value; }
43      set {
44        TranslocationMoveGeneratorParameter.Value = value as TranslocationMoveGenerator;
45        if (TranslocationMoveGeneratorParameter.Value is IMultiMoveGenerator) {
46          ((IMultiMoveGenerator)TranslocationMoveGeneratorParameter.Value).SampleSizeParameter.ActualName = SampleSizeParameter.Name;
47        }
48      }
49    }
50
51    public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
52      get {
53        if (TranslocationMoveGeneratorParameter.Value != null)
54          return TranslocationMoveGeneratorParameter.Value.TranslocationMoveParameter;
55        else
56          return null;
57      }
58    }
59
60    public ILookupParameter<Permutation> PermutationParameter {
61      get {
62        if (TranslocationMoveGeneratorParameter.Value != null)
63          return TranslocationMoveGeneratorParameter.Value.PermutationParameter;
64        else
65          return null;
66      }
67    }
68
69    public IValueLookupParameter<IntValue> SampleSizeParameter {
70      get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
71    }
72
73    public AlbaTranslocationMoveGenerator(): base() {
74      Parameters.Add(new ValueLookupParameter<TranslocationMoveGenerator>("TranslocationMoveGenerator", "The move generator.",
75        new StochasticTranslocationMultiMoveGenerator()));
76      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
77
78      ((IMultiMoveGenerator)TranslocationMoveGeneratorParameter.Value).SampleSizeParameter.ActualName = SampleSizeParameter.Name;
79    }
80
81    public override IOperation Apply() {
82      IOperation successor = base.Apply();
83
84      Permutation permutation = VRPSolutionParameter.ActualValue as Permutation;
85      string moveName = TranslocationMoveGeneratorParameter.ActualValue.TranslocationMoveParameter.Name;
86      int cities = VRPSolutionParameter.ActualValue.Cities;
87
88      List<Scope> toBeDeleted = new List<Scope>();
89      foreach (Scope scope in this.ExecutionContext.Scope.SubScopes) {
90        if (scope.Variables.ContainsKey(moveName)) {
91          TranslocationMove move = scope.Variables[moveName].Value as TranslocationMove;
92
93          if (move != null) {
94            bool criteria1 = true;
95            if(move.Index1 - 1 >= 0 &&
96              move.Index3 -1 >= 0)
97              criteria1 = (permutation[move.Index1] >= cities &&
98                permutation[move.Index1 - 1] >= cities &&
99                permutation[move.Index3 - 1] >= cities);
100
101            int index3 = move.Index3 + (move.Index2 - move.Index1) + 1;
102            bool criteria2 = true;
103            if (move.Index2 + 1 < permutation.Length &&
104              index3 < permutation.Length)
105              criteria2 = (permutation[move.Index2] >= cities &&
106                permutation[move.Index2 + 1] >= cities &&
107                permutation[index3] >= cities);
108
109            if(criteria1 && criteria2)
110              toBeDeleted.Add(scope);
111          }
112        }
113      }
114
115      foreach (Scope scope in toBeDeleted) {
116        this.ExecutionContext.Scope.SubScopes.Remove(scope);
117      }
118
119      return successor;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.