Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614: worked on GQAP

File size: 3.5 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, ILocationAwareGQAPOperator {
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", "The capacities vector describes the available space at the locations."));
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        foreach (var item in groupedLocations[parent][i]) {
67          if (remainingEquipment.Contains(item.Equipment)) {
68            child[item.Equipment] = i;
69            remainingEquipment.Remove(item.Equipment);
70          }
71        }
72      }
73
74      foreach (var equipment in remainingEquipment) {
75        int parent = random.Next(parents.Length);
76        child[equipment] = parents[parent][equipment];
77      }
78
79      return child;
80    }
81
82    protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents) {
83      return Apply(random, parents, CapacitiesParameter.ActualValue);
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.