Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Zhu/Crossovers/ZhuMergeCrossover2.cs @ 8085

Last change on this file since 8085 was 8053, checked in by ascheibe, 12 years ago

#1722 fixed more licensing information and source formatting

File size: 3.1 KB
RevLine 
[4379]1#region License Information
2/* HeuristicLab
[8053]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4379]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
[8053]22using System.Collections.Generic;
23using HeuristicLab.Common;
[4379]24using HeuristicLab.Core;
[8053]25using HeuristicLab.Data;
[4379]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.VehicleRouting.Variants;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Zhu {
30  [Item("ZhuMergeCrossover2", "The Zhu Merge Crossover (Version 2).  It is implemented as described in Zhu, K.Q. (2000). A New Genetic Algorithm For VRPTW. Proceedings of the International Conference on Artificial Intelligence.")]
31  [StorableClass]
[8053]32  public sealed class ZhuMergeCrossover2 : ZhuCrossover {
[4379]33    [StorableConstructor]
34    private ZhuMergeCrossover2(bool deserializing) : base(deserializing) { }
35
36    public ZhuMergeCrossover2()
37      : base() {
38    }
39
[4752]40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new ZhuMergeCrossover2(this, cloner);
42    }
43
44    private ZhuMergeCrossover2(ZhuMergeCrossover2 original, Cloner cloner)
45      : base(original, cloner) {
46    }
47
[4379]48    protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) {
49      List<int> p1 = new List<int>(parent1);
50      List<int> p2 = new List<int>(parent2);
51
52      ZhuEncoding child = parent2.Clone() as ZhuEncoding;
53
[6771]54      if (parent1.Length != parent2.Length)
55        return child;
56
[4379]57      int breakPoint = random.Next(child.Length);
58      int i = breakPoint;
59
60      int parent1Index = i;
61      int parent2Index = i;
62
63      DoubleArray dueTime = null;
64      if (ProblemInstance is ITimeWindowedProblemInstance) {
65        dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime;
66      }
67
68      do {
69        if (i == breakPoint) {
70          child[i] = p1[parent1Index];
71        } else {
[8053]72          if (dueTime != null &&
[4379]73            (dueTime[p1[parent1Index] + 1] <
74            dueTime[p2[parent2Index] + 1])) {
75            child[i] = p1[parent1Index];
76          } else {
77            child[i] = p2[parent2Index];
78          }
79        }
80
81        p1.Remove(child[i]);
82        if (parent1Index >= p1.Count)
83          parent1Index = 0;
84
85        p2.Remove(child[i]);
86        if (parent2Index >= p2.Count)
87          parent2Index = 0;
88
89        i = (i + 1) % child.Length;
90      } while (i != breakPoint);
91
92      return child;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.