Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614: worked on GQAP (added operators, assignment view)

File size: 3.4 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;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Operators {
32  [Item("DiscreteLocationCrossover", "Combines the assignment to locations from various parents.")]
33  [StorableClass]
34  public class DiscreteLocationCrossover : GQAPCrossover, ILocationAwareGQAPOperator {
35
36    public ILookupParameter<DoubleArray> CapacitiesParameter {
37      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
38    }
39
40    [StorableConstructor]
41    protected DiscreteLocationCrossover(bool deserializing) : base(deserializing) { }
42    protected DiscreteLocationCrossover(DiscreteLocationCrossover original, Cloner cloner)
43      : base(original, cloner) { }
44    public DiscreteLocationCrossover()
45      : base() {
46      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new DiscreteLocationCrossover(this, cloner);
51    }
52
53    public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray capacities) {
54     
55      var groupedLocations = parents
56              .Select(p => p.Select((value, index) => new { Equipment = index, Location = value })
57              .GroupBy(x => x.Location)
58              .ToDictionary(x => x.Key, y => y.AsEnumerable())).ToArray();
59
60      IntegerVector child = new IntegerVector(parents[0].Length);
61      HashSet<int> remainingEquipment = new HashSet<int>(Enumerable.Range(0, child.Length));
62      int locations = capacities.Length;
63      foreach (var i in Enumerable.Range(0, locations).Shuffle(random)) {
64        int parent = random.Next(parents.Length);
65        foreach (var item in groupedLocations[parent][i]) {
66          if (remainingEquipment.Contains(item.Equipment)) {
67            child[item.Equipment] = i;
68            remainingEquipment.Remove(item.Equipment);
69          }
70        }
71      }
72
73      foreach (var equipment in remainingEquipment) {
74        int parent = random.Next(parents.Length);
75        child[equipment] = parents[parent][equipment];
76      }
77
78      return child;
79    }
80
81    protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents) {
82      return Apply(random, parents, CapacitiesParameter.ActualValue);
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.