1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
32 | /// <summary>
|
---|
33 | /// An operator which creates a new random permutation of integer values.
|
---|
34 | /// </summary>
|
---|
35 | [Item("PreexistingPermutationCreator", "An operator which creates a new permutation of integer values from a set of given permutations.")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class PreexistingPermutationCreator : InstrumentedOperator, IPermutationCreator, IPreexistingSolutionCreator, IStochasticOperator {
|
---|
38 | public override bool CanChangeName {
|
---|
39 | get { return false; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ILookupParameter<IRandom> RandomParameter {
|
---|
43 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
44 | }
|
---|
45 | public IValueLookupParameter<IntValue> LengthParameter {
|
---|
46 | get { return (IValueLookupParameter<IntValue>)Parameters["Length"]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<Permutation> PermutationParameter {
|
---|
49 | get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
50 | }
|
---|
51 | public IValueParameter<PermutationType> PermutationTypeParameter {
|
---|
52 | get { return (IValueParameter<PermutationType>)Parameters["PermutationType"]; }
|
---|
53 | }
|
---|
54 | public IValueLookupParameter<ScopeList> PreexistingSolutionsParameter {
|
---|
55 | get { return (IValueLookupParameter<ScopeList>)Parameters["PreexistingSolutions"]; }
|
---|
56 | }
|
---|
57 | public IValueLookupParameter<BoolValue> SampleFromPreexistingParameter {
|
---|
58 | get { return (IValueLookupParameter<BoolValue>)Parameters["SampleFromPreexisting"]; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public PermutationTypes PermutationType {
|
---|
62 | get { return PermutationTypeParameter.Value.Value; }
|
---|
63 | set { PermutationTypeParameter.Value.Value = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | [StorableConstructor]
|
---|
67 | private PreexistingPermutationCreator(bool deserializing) : base(deserializing) { }
|
---|
68 | private PreexistingPermutationCreator(PreexistingPermutationCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
69 | public PreexistingPermutationCreator()
|
---|
70 | : base() {
|
---|
71 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used to sample from the existing permutations."));
|
---|
72 | Parameters.Add(new ValueLookupParameter<IntValue>("Length", "The length of the new random permutation."));
|
---|
73 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The new random permutation."));
|
---|
74 | Parameters.Add(new ValueParameter<PermutationType>("PermutationType", "The type of the permutation.", new PermutationType(PermutationTypes.RelativeUndirected)));
|
---|
75 | Parameters.Add(new ValueLookupParameter<ScopeList>("PreexistingSolutions", "The list of solutions that should be used to create a new solution.", new ScopeList()));
|
---|
76 | Parameters.Add(new ValueLookupParameter<BoolValue>("SampleFromPreexisting", "Whether the preexisting solutions should be used as a basis to sample from or whether they should be cloned.", new BoolValue(true)));
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
80 | return new PreexistingPermutationCreator(this, cloner);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override IOperation InstrumentedApply() {
|
---|
84 | var random = RandomParameter.ActualValue;
|
---|
85 | var preexisting = (Permutation)PreexistingSolutionsParameter.ActualValue.SampleRandom(random).Variables[PermutationParameter.ActualName].Value;
|
---|
86 | var sample = SampleFromPreexistingParameter.ActualValue.Value;
|
---|
87 | if (sample) {
|
---|
88 | var perm = new Permutation(PermutationTypeParameter.Value.Value, LengthParameter.Value.Value, random);
|
---|
89 | PermutationParameter.ActualValue = perm.PermutationType == PermutationTypes.Absolute ? PartiallyMatchedCrossover.Apply(random, preexisting, perm)
|
---|
90 | : OrderCrossover.Apply(random, preexisting, perm);
|
---|
91 | } else {
|
---|
92 | PermutationParameter.ActualValue = (Permutation)preexisting.Clone();
|
---|
93 | }
|
---|
94 | return base.InstrumentedApply();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|