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