1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Data;
|
---|
4 | using HeuristicLab.Data.MoveVectorData;
|
---|
5 | using HeuristicLab.Data.MoveVectorData.Interfaces;
|
---|
6 | using HeuristicLab.Data.MoveVectorData.Moves;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.PluginInfrastructure;
|
---|
9 | using System;
|
---|
10 | using System.Collections.Generic;
|
---|
11 | using System.Linq;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Encodings.MoveVectorEncoding.Manipulators
|
---|
14 | {
|
---|
15 | [Item("SingleMoveChangeManipulator", " Uniformly distributed change of a single position of a move.")]
|
---|
16 | [StorableClass]
|
---|
17 | public class SingleMoveChangeManipulator : BoundedMoveVectorManipulator
|
---|
18 | {
|
---|
19 | [StorableConstructor]
|
---|
20 | protected SingleMoveChangeManipulator(bool deserializing) : base(deserializing) { }
|
---|
21 | protected SingleMoveChangeManipulator(SingleMoveChangeManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
22 |
|
---|
23 | public SingleMoveChangeManipulator() : base() { }
|
---|
24 |
|
---|
25 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
26 | {
|
---|
27 | return new SingleMoveChangeManipulator(this, cloner);
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static void Apply(IRandom random, MoveVector moves, IntMatrix bounds)
|
---|
31 | {
|
---|
32 | Manipulate(random, moves, bounds, random.Next(moves.Length));
|
---|
33 | }
|
---|
34 |
|
---|
35 | public static void Manipulate(IRandom random, MoveVector moves, IntMatrix bounds, int index)
|
---|
36 | {
|
---|
37 | if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("SingleMoveChangeManipulator: Invalid bounds specified", "bounds");
|
---|
38 | int min = bounds[index % bounds.Rows, 0], max = bounds[index % bounds.Rows, 1], step = 1;
|
---|
39 | if (bounds.Columns > 2) step = bounds[index % bounds.Rows, 2];
|
---|
40 |
|
---|
41 | Type type = moves.MoveTypes[random.Next(0, moves.MoveTypes.Count)];
|
---|
42 | IMove move = (IMove)Activator.CreateInstance(type);
|
---|
43 | move.Randomize(random, min, max, step);
|
---|
44 | moves[index] = move;
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected override void ManipulateBounded(IRandom random, MoveVector moves, IntMatrix bounds)
|
---|
48 | {
|
---|
49 | if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("SingleMoveChangeManipulator: Parameter " + BoundsParameter.ActualName + " could not be found.");
|
---|
50 | Apply(random, moves, bounds);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|