source: branches/SpectralKernelForGaussianProcesses/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Creators/RandomCreator.cs @ 10479

Last change on this file since 10479 was 10479, checked in by gkronber, 6 years ago

#2124 merged all changes from trunk to prepare for trunk-reintegration

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
30  [Item("RandomCreator", "Creates a randomly initialized VRP solution.")]
31  [StorableClass]
32  public sealed class RandomCreator : AlbaCreator, IStochasticOperator {
33    #region IStochasticOperator Members
34    public ILookupParameter<IRandom> RandomParameter {
35      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
36    }
37    #endregion
38
39    [StorableConstructor]
40    private RandomCreator(bool deserializing) : base(deserializing) { }
41
42    public RandomCreator()
43      : base() {
44      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new RandomCreator(this, cloner);
49    }
50
51    private RandomCreator(RandomCreator original, Cloner cloner)
52      : base(original, cloner) {
53    }
54
55    private List<int> CreateSolution() {
56      int cities = ProblemInstance.Cities.Value;
57      int vehicles = ProblemInstance.Vehicles.Value;
58
59      int[] perm = new int[cities + vehicles];
60      int[] sortingArray = new int[perm.Length - 2];
61      for (int i = 0; i < cities; i++)
62        perm[i] = i + 1;
63      for (int i = cities; i < cities + vehicles; i++)
64        perm[i] = -(i - cities);
65      for (int i = 0; i < perm.Length - 2; i++) {
66        sortingArray[i] = RandomParameter.ActualValue.Next(perm.Length * 2);
67      }
68      // shuffle perm from 1...n-1 by sorting sortingArray (perm[0] and perm[n] must stay 0)
69      bool done;
70      do {
71        done = true;
72        for (int i = 0; i < perm.Length - 3; i++) {
73          if (sortingArray[i] > sortingArray[i + 1]) {
74            int h = sortingArray[i];
75            sortingArray[i] = sortingArray[i + 1];
76            sortingArray[i + 1] = h;
77            h = perm[i + 1];
78            perm[i + 1] = perm[i + 2];
79            perm[i + 2] = h;
80            done = false;
81          }
82        }
83      } while (!done);
84
85      return new List<int>(perm);
86    }
87
88    public override IOperation InstrumentedApply() {
89      //choose default encoding here
90      VRPToursParameter.ActualValue = AlbaEncoding.ConvertFrom(CreateSolution(), ProblemInstance);
91
92      return base.InstrumentedApply();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.