source: branches/StackingProblems/HeuristicLab.Encodings.MoveVectorEncoding/3.3/Manipulators/MultiMoveManipulator.cs @ 14278

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

Initial Commit for ticket #2605

Implemented:

  • Encoding
  • Moves
  • Views
  • Blocks World Problem
  • Blocks Relocation Problem
  • Stacking Problem
File size: 4.1 KB
Line 
1using HeuristicLab.Collections;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Encodings.MoveVectorEncoding.Interfaces;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.PluginInfrastructure;
10using System;
11using System.Collections.Generic;
12using System.Linq;
13using HeuristicLab.Data;
14using HeuristicLab.Data.MoveVectorData;
15
16namespace HeuristicLab.Encodings.MoveVectorEncoding.Manipulators
17{
18    [Item("Multi Move Manipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
19    [StorableClass]
20    public class MultiMoveManipulator : StochasticMultiBranch<IMoveVectorManipulator>, IMoveVectorManipulator, IBoundedMoveVectorOperator, IStochasticOperator
21    {
22        public override bool CanChangeName
23        {
24            get { return false; }
25        }
26        protected override bool CreateChildOperation
27        {
28            get { return true; }
29        }
30
31        public ILookupParameter<MoveVector> MoveVectorParameter
32        {
33            get { return (ILookupParameter<MoveVector>)Parameters["MoveVector"]; }
34        }
35
36        public IValueLookupParameter<IntMatrix> BoundsParameter
37        {
38            get { return (IValueLookupParameter<IntMatrix>)Parameters["Bounds"]; }
39        }
40
41        [StorableConstructor]
42        protected MultiMoveManipulator(bool deserializing) : base(deserializing) { }
43        protected MultiMoveManipulator(MultiMoveManipulator original, Cloner cloner) : base(original, cloner) { }
44        public MultiMoveManipulator()
45          : base()
46        {
47            Parameters.Add(new LookupParameter<MoveVector>("MoveVector", "The encoding vector that is to be manipulated."));
48            Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "The bounds matrix can contain one row for each dimension with three columns specifying minimum (inclusive), maximum (exclusive), and step size. If less rows are given the matrix is cycled."));
49            foreach (Type type in ApplicationManager.Manager.GetTypes(typeof(IMoveVectorManipulator), typeof(MoveVectorEncoding).Assembly))
50            {
51                if (!typeof(MultiOperator<IMoveVectorManipulator>).IsAssignableFrom(type))
52                    Operators.Add((IMoveVectorManipulator)Activator.CreateInstance(type), true);
53            }
54            SelectedOperatorParameter.ActualName = "SelectedManipulationOperator";
55        }
56
57        public override IDeepCloneable Clone(Cloner cloner)
58        {
59            return new MultiMoveManipulator(this, cloner);
60        }
61
62        protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IMoveVectorManipulator>> e)
63        {
64            base.Operators_ItemsReplaced(sender, e);
65            ParameterizeManipulators(e.Items);
66        }
67
68        protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IMoveVectorManipulator>> e)
69        {
70            base.Operators_ItemsAdded(sender, e);
71            ParameterizeManipulators(e.Items);
72        }
73
74        private void ParameterizeManipulators(IEnumerable<IndexedItem<IMoveVectorManipulator>> manipulators)
75        {
76            foreach (var m in manipulators.Select(x => x.Value))
77            {
78                m.MoveVectorParameter.ActualName = MoveVectorParameter.Name;
79                var stOp = m as IStochasticOperator;
80                if (stOp != null) stOp.RandomParameter.ActualName = RandomParameter.Name;
81
82                var bounded = m as IBoundedMoveVectorOperator;
83                if (bounded != null) bounded.BoundsParameter.ActualName = MoveVectorParameter.Name + "." + BoundsParameter.Name;
84            }
85        }
86
87        public override IOperation InstrumentedApply()
88        {
89            if (Operators.Count == 0) throw new InvalidOperationException(Name + ": Please add at least one move manipulator to choose from.");
90            return base.InstrumentedApply();
91        }
92    }
93}
Note: See TracBrowser for help on using the repository browser.