[4362] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4362] | 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;
|
---|
[8053] | 23 | using HeuristicLab.Common;
|
---|
[4362] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Optimization;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[4362] | 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
| 30 | [Item("RandomCreator", "Creates a randomly initialized VRP solution.")]
|
---|
[16565] | 31 | [StorableType("3B0B288F-71C6-438B-96DF-C21243E7E66D")]
|
---|
[7865] | 32 | public sealed class RandomCreator : AlbaCreator, IStochasticOperator {
|
---|
[4362] | 33 | #region IStochasticOperator Members
|
---|
| 34 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 35 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 36 | }
|
---|
| 37 | #endregion
|
---|
| 38 |
|
---|
| 39 | [StorableConstructor]
|
---|
[16565] | 40 | private RandomCreator(StorableConstructorFlag _) : base(_) { }
|
---|
[4362] | 41 |
|
---|
| 42 | public RandomCreator()
|
---|
| 43 | : base() {
|
---|
| 44 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[4752] | 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 |
|
---|
[7865] | 55 | private List<int> CreateSolution() {
|
---|
[4362] | 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++)
|
---|
[7543] | 64 | perm[i] = -(i - cities);
|
---|
[4362] | 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 | }
|
---|
[7865] | 87 |
|
---|
[10298] | 88 | public override IOperation InstrumentedApply() {
|
---|
[7865] | 89 | //choose default encoding here
|
---|
| 90 | VRPToursParameter.ActualValue = AlbaEncoding.ConvertFrom(CreateSolution(), ProblemInstance);
|
---|
| 91 |
|
---|
[10298] | 92 | return base.InstrumentedApply();
|
---|
[7865] | 93 | }
|
---|
[4362] | 94 | }
|
---|
| 95 | }
|
---|