1 | using HeuristicLab.Collections;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Encodings.MoveVectorEncoding.Interfaces;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.PluginInfrastructure;
|
---|
10 | using System;
|
---|
11 | using System.Collections.Generic;
|
---|
12 | using System.Linq;
|
---|
13 | using HeuristicLab.Data;
|
---|
14 | using HeuristicLab.Data.MoveVectorData;
|
---|
15 |
|
---|
16 | namespace 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 | } |
---|