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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.PackingEncoding.MultiComponentVector {
|
---|
31 | [Item("StochasticMultiComponentVectorMoveGenerator", "Generates all possible multi component moves from a given multiComponentVector.")]
|
---|
32 | [StorableClass]
|
---|
33 | public class StochasticMultiComponentVectorMoveGenerator : MultiComponentVectorMoveGenerator, IMultiMoveGenerator, IStochasticOperator {
|
---|
34 | public ILookupParameter<IRandom> RandomParameter {
|
---|
35 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
36 | }
|
---|
37 | public IValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
38 | get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public IntValue SampleSize {
|
---|
42 | get { return SampleSizeParameter.Value; }
|
---|
43 | set { SampleSizeParameter.Value = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected StochasticMultiComponentVectorMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
48 | protected StochasticMultiComponentVectorMoveGenerator(StochasticMultiComponentVectorMoveGenerator original, Cloner cloner) : base(original, cloner) { }
|
---|
49 | public StochasticMultiComponentVectorMoveGenerator()
|
---|
50 | : base() {
|
---|
51 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
52 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new StochasticMultiComponentVectorMoveGenerator(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static MultiComponentVectorMove[] Apply(MultiComponentVectorEncoding multiComponentVector, IRandom random, int sampleSize) {
|
---|
60 | MultiComponentVectorMove[] moves = new MultiComponentVectorMove[sampleSize];
|
---|
61 | var rm = new List<MultiComponentVectorMove> (GenerateRotationMoves(multiComponentVector));
|
---|
62 | var gm = new List<MultiComponentVectorMove>(GenerateSingleGroupingMoves(multiComponentVector));
|
---|
63 | var sm = new List<MultiComponentVectorMove>(GenerateSwapPositionMoves(multiComponentVector));
|
---|
64 | var cpm = new List<MultiComponentVectorMove>(GenerateChangePositionMoves(multiComponentVector));
|
---|
65 |
|
---|
66 | for (int i = 0; i < sampleSize; i++) {
|
---|
67 | int typeDecision = random.Next(4);
|
---|
68 | switch (typeDecision) {
|
---|
69 | case 0: moves[i] = rm[random.Next(rm.Count)];
|
---|
70 | break;
|
---|
71 | case 1: moves[i] = gm[random.Next(gm.Count)];
|
---|
72 | break;
|
---|
73 | case 2: moves[i] = sm[random.Next(sm.Count)];
|
---|
74 | break;
|
---|
75 | case 3: moves[i] = cpm[random.Next(cpm.Count)];
|
---|
76 | break;
|
---|
77 | default:
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | return moves;
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override MultiComponentVectorMove[] GenerateMoves(MultiComponentVectorEncoding multiComponentVector) {
|
---|
85 | IRandom random = RandomParameter.ActualValue;
|
---|
86 | return Apply(multiComponentVector, random, SampleSizeParameter.ActualValue.Value);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|