Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/DiscreteLocationCrossover.cs @ 7419

Last change on this file since 7419 was 7419, checked in by abeham, 12 years ago

#1614

  • reworked parameterization (one interface for every parameter resp. parameter group)
  • unified parameter descriptions
File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
33  [Item("DiscreteLocationCrossover", "Combines the assignment to locations from various parents.")]
34  [StorableClass]
35  public class DiscreteLocationCrossover : GQAPCrossover, ICapacitiesAwareGQAPOperator {
36
37    public ILookupParameter<DoubleArray> CapacitiesParameter {
38      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
39    }
40
41    [StorableConstructor]
42    protected DiscreteLocationCrossover(bool deserializing) : base(deserializing) { }
43    protected DiscreteLocationCrossover(DiscreteLocationCrossover original, Cloner cloner)
44      : base(original, cloner) { }
45    public DiscreteLocationCrossover()
46      : base() {
47      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new DiscreteLocationCrossover(this, cloner);
52    }
53
54    public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray capacities) {
55
56      var groupedLocations = parents
57              .Select(p => p.Select((value, index) => new { Equipment = index, Location = value })
58              .GroupBy(x => x.Location)
59              .ToDictionary(x => x.Key, y => y.AsEnumerable())).ToArray();
60
61      IntegerVector child = new IntegerVector(parents[0].Length);
62      HashSet<int> remainingEquipment = new HashSet<int>(Enumerable.Range(0, child.Length));
63      int locations = capacities.Length;
64      foreach (var i in Enumerable.Range(0, locations).Shuffle(random)) {
65        int parent = random.Next(parents.Length);
66        if (!groupedLocations[parent].ContainsKey(i)) {
67          int tmp = parent;
68          do {
69            tmp = (tmp + 1) % parents.Length;
70          } while (tmp != parent && !groupedLocations[tmp].ContainsKey(i));
71          if (parent == tmp) continue;
72          else parent = tmp;
73        }
74        foreach (var item in groupedLocations[parent][i]) {
75          if (remainingEquipment.Contains(item.Equipment)) {
76            child[item.Equipment] = i;
77            remainingEquipment.Remove(item.Equipment);
78          }
79        }
80      }
81
82      foreach (var equipment in remainingEquipment) {
83        int parent = random.Next(parents.Length);
84        child[equipment] = parents[parent][equipment];
85      }
86
87      return child;
88    }
89
90    protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents) {
91      return Apply(random, parents, CapacitiesParameter.ActualValue);
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.