1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.DecisionList.Mutation {
|
---|
29 | [Item("UniformOnePointMutation", "Description missing")]
|
---|
30 | [StorableClass]
|
---|
31 | public class UniformOnePointManipulator : DecisionListManipulator {
|
---|
32 |
|
---|
33 | [StorableConstructor]
|
---|
34 | protected UniformOnePointManipulator(bool deserializing) : base(deserializing) { }
|
---|
35 | protected UniformOnePointManipulator(UniformOnePointManipulator original, Cloner cloner)
|
---|
36 | : base(original, cloner) {
|
---|
37 | }
|
---|
38 | public UniformOnePointManipulator()
|
---|
39 | : base() {
|
---|
40 | }
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new UniformOnePointManipulator(this, cloner);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static void Apply(IRandom random, DecisionList child, double actionMutationProbability) {
|
---|
46 | int rulePos = random.Next(0, child.Rules.Count);
|
---|
47 | Rule mutateRule = child.Rules[rulePos];
|
---|
48 |
|
---|
49 | var possiblePosition = new List<int>() { mutateRule.Action.CurrentPosition };
|
---|
50 | if (child.DefaultAction != null) { possiblePosition.Add(child.DefaultAction.CurrentPosition); }
|
---|
51 | var remainingPositions = mutateRule.Action.GetPossibleActionPositions().Except(possiblePosition);
|
---|
52 |
|
---|
53 | if (remainingPositions.Count() > 0 && random.NextDouble() < actionMutationProbability) {
|
---|
54 | int pos = random.Next(0, remainingPositions.Count());
|
---|
55 | mutateRule.Action.SetToPosition(remainingPositions.ElementAt(pos));
|
---|
56 | } else {
|
---|
57 | int attributePos = random.Next(0, mutateRule.Variables.Count);
|
---|
58 | string variableName = mutateRule.Variables.Keys.ToList()[attributePos];
|
---|
59 | mutateRule.Variables[variableName].Mutate(random);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected override void Manipulate(IRandom random, DecisionList child, double actionMutationProbability) {
|
---|
64 | Apply(random, child, actionMutationProbability);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|