Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/RandomCreator.cs @ 4179

Last change on this file since 4179 was 4179, checked in by svonolfe, 12 years ago

Refactored VRP based on the code review (#1039)

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