Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/DiscreteLocationCrossover.cs @ 17357

Last change on this file since 17357 was 16712, checked in by gkronber, 6 years ago

#2936: adapted branch to new persistence (works with HL trunk r16711)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31using HEAL.Attic;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
34  [Item("DiscreteLocationCrossover", "Combines the assignment to locations from various parents.")]
35  [StorableType("E001CEB3-DAA4-4AF4-843B-2DD951F0EAA6")]
36  public class DiscreteLocationCrossover : GQAPCrossover {
37
38    [StorableConstructor]
39    protected DiscreteLocationCrossover(StorableConstructorFlag _) : base(_) { }
40    protected DiscreteLocationCrossover(DiscreteLocationCrossover original, Cloner cloner)
41      : base(original, cloner) { }
42    public DiscreteLocationCrossover()
43      : base() {
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new DiscreteLocationCrossover(this, cloner);
48    }
49
50    public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray demands, DoubleArray capacities) {
51      var locations = capacities.Length;
52      var cap = Math.Max(parents[0].Length / locations, 1);
53      var lookup = new List<int>[parents.Length][];
54      for (var p = 0; p < parents.Length; p++) {
55        lookup[p] = new List<int>[locations];
56        var assign = parents[p];
57        for (var e = 0; e < parents[p].Length; e++) {
58          var loc = assign[e];
59          if (lookup[p][loc] == null) lookup[p][loc] = new List<int>(cap);
60          lookup[p][loc].Add(e);
61        }
62      }
63
64      var slack = capacities.ToArray();
65      IntegerVector child = new IntegerVector(parents[0].Length);
66      var takenEquip = new bool[child.Length];
67      foreach (var loc in Enumerable.Range(0, locations).Shuffle(random)) {
68        int parent = random.Next(parents.Length);
69        if (lookup[parent][loc] == null) {
70          int tmp = parent;
71          do {
72            tmp = (tmp + 1) % parents.Length;
73          } while (tmp != parent && lookup[tmp][loc] == null);
74          if (parent == tmp) continue;
75          else parent = tmp;
76        }
77        foreach (var equip in lookup[parent][loc]) {
78          if (!takenEquip[equip]) {
79            child[equip] = loc;
80            takenEquip[equip] = true;
81            slack[loc] -= demands[equip];
82          }
83        }
84      }
85
86      var order = Enumerable.Range(0, takenEquip.Length)
87        .Where(x => !takenEquip[x])
88        .Shuffle(random); // avoid bias
89      foreach (var e in order) {
90        var assigned = false;
91        // try 1: find a parent where equipment can be assigned feasibly
92        var fallback = -1;
93        var count = 1;
94        foreach (var p in parents.Shuffle(random)) {
95          if (slack[p[e]] >= demands[e]) {
96            child[e] = p[e];
97            slack[child[e]] -= demands[e];
98            assigned = true;
99            break;
100          } else if (random.NextDouble() < 1.0 / count) {
101            fallback = p[e];
102          }
103          count++;
104        }
105        // try 2: find a random feasible location
106        if (!assigned) {
107          var possible = Enumerable.Range(0, locations).Where(x => slack[x] >= demands[e]).ToList();
108          if (possible.Count > 0) {
109            var loc = possible.SampleRandom(random);
110            child[e] = loc;
111            slack[loc] -= demands[e];
112          } else {
113            // otherwise: fallback
114            child[e] = fallback;
115            slack[child[e]] -= demands[e];
116          }
117        }
118      }
119
120      return child;
121    }
122
123    protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents,
124      GQAPInstance problemInstance) {
125      return Apply(random, parents, problemInstance.Demands, problemInstance.Capacities);
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.