Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StackingProblems/HeuristicLab.Encodings.MoveVectorEncoding/3.3/ShakingOperators/MoveVectorShakingOperator.cs @ 14278

Last change on this file since 14278 was 14278, checked in by mzehetho, 8 years ago

Initial Commit for ticket #2605

Implemented:

  • Encoding
  • Moves
  • Views
  • Blocks World Problem
  • Blocks Relocation Problem
  • Stacking Problem
File size: 4.4 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Collections;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Optimization;
8using HeuristicLab.Optimization.Operators;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using HeuristicLab.PluginInfrastructure;
12using HeuristicLab.Encodings.MoveVectorEncoding.Interfaces;
13using HeuristicLab.Data.MoveVectorData;
14
15namespace HeuristicLab.Encodings.IntegerVectorEncoding
16{
17    /// <summary>
18    /// A shaking operator for VNS.
19    /// </summary>
20    [Item("MoveVectorShakingOperator", "A shaking operator for VNS which uses available manipulation operators to perform the shaking.")]
21    [StorableClass]
22    public class MoveVectorShakingOperator : ShakingOperator<IMoveVectorManipulator>, IMoveVectorMultiNeighborhoodShakingOperator, IStochasticOperator, IBoundedMoveVectorOperator
23    {
24
25        public ILookupParameter<MoveVector> MoveVectorParameter
26        {
27            get { return (ILookupParameter<MoveVector>)Parameters["MoveVector"]; }
28        }
29
30        public ILookupParameter<IRandom> RandomParameter
31        {
32            get { return (LookupParameter<IRandom>)Parameters["Random"]; }
33        }
34
35        public IValueLookupParameter<IntMatrix> BoundsParameter
36        {
37            get { return (IValueLookupParameter<IntMatrix>)Parameters["Bounds"]; }
38        }
39
40        [StorableConstructor]
41        protected MoveVectorShakingOperator(bool deserializing) : base(deserializing) { }
42        protected MoveVectorShakingOperator(MoveVectorShakingOperator original, Cloner cloner) : base(original, cloner) { }
43        public override IDeepCloneable Clone(Cloner cloner)
44        {
45            return new MoveVectorShakingOperator(this, cloner);
46        }
47        public MoveVectorShakingOperator()
48          : base()
49        {
50            Parameters.Add(new LookupParameter<MoveVector>("MoveVector", "The move vector to shake."));
51            Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator that will be used for stochastic shaking operators."));
52            Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "A 2 column matrix specifying the lower and upper bound for each dimension. If there are less rows than dimension the bounds vector is cycled."));
53
54            foreach (IMoveVectorManipulator shaker in ApplicationManager.Manager.GetInstances<IMoveVectorManipulator>().OrderBy(x => x.Name))
55                if (!(shaker is ISelfAdaptiveManipulator)) Operators.Add(shaker);
56        }
57
58        [StorableHook(HookType.AfterDeserialization)]
59        private void AfterDeserialization()
60        {
61            #region Backwards compatible code, remove with 3.4
62            if (!Parameters.ContainsKey("Bounds"))
63                Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "A 2 column matrix specifying the lower and upper bound for each dimension. If there are less rows than dimension the bounds vector is cycled."));
64            #endregion
65        }
66
67        #region Wiring of some parameters
68        protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IMoveVectorManipulator>> e)
69        {
70            base.Operators_ItemsAdded(sender, e);
71            ParameterizeOperators(e.Items);
72        }
73
74        protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IMoveVectorManipulator>> e)
75        {
76            base.Operators_ItemsReplaced(sender, e);
77            ParameterizeOperators(e.Items);
78        }
79
80        private void ParameterizeOperators(IEnumerable<IndexedItem<IMoveVectorManipulator>> items)
81        {
82            if (items.Any())
83            {
84                foreach (IStochasticOperator op in items.Select(x => x.Value).OfType<IStochasticOperator>())
85                    op.RandomParameter.ActualName = RandomParameter.Name;
86                foreach (IMoveVectorManipulator op in items.Select(x => x.Value).OfType<IMoveVectorManipulator>())
87                    op.MoveVectorParameter.ActualName = MoveVectorParameter.Name;
88                foreach (IBoundedMoveVectorOperator op in items.Select(x => x.Value).OfType<IBoundedMoveVectorOperator>())
89                    op.BoundsParameter.ActualName = "MoveVector." + BoundsParameter.Name;
90            }
91        }
92        #endregion
93    }
94}
Note: See TracBrowser for help on using the repository browser.