Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/01/18 23:52:39 (6 years ago)
Author:
abeham
Message:

#1614:

  • Fixed some bugs
  • Added OSGA
  • Updated ES (added recombination)
  • Reusing operators among algorihtms (RelocateEquipmentManipluator)
  • Rewrote DiscreteLocationCrossover
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Crossovers/DiscreteLocationCrossover.cs

    r15504 r15564  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    4647    }
    4748
    48     public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray capacities) {
     49    public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray demands, DoubleArray capacities) {
     50      var locations = capacities.Length;
     51      var cap = Math.Max(parents[0].Length / locations, 1);
     52      var lookup = new List<int>[parents.Length][];
     53      for (var p = 0; p < parents.Length; p++) {
     54        lookup[p] = new List<int>[locations];
     55        var assign = parents[p];
     56        for (var e = 0; e < parents[p].Length; e++) {
     57          var loc = assign[e];
     58          if (lookup[p][loc] == null) lookup[p][loc] = new List<int>(cap);
     59          lookup[p][loc].Add(e);
     60        }
     61      }
    4962
    50       var groupedLocations = parents
    51               .Select(p => p.Select((value, index) => new { Equipment = index, Location = value })
    52               .GroupBy(x => x.Location)
    53               .ToDictionary(x => x.Key, y => y.AsEnumerable())).ToArray();
    54 
     63      var slack = capacities.ToArray();
    5564      IntegerVector child = new IntegerVector(parents[0].Length);
    56       HashSet<int> remainingEquipment = new HashSet<int>(Enumerable.Range(0, child.Length));
    57       int locations = capacities.Length;
    58       foreach (var i in Enumerable.Range(0, locations).Shuffle(random)) {
     65      var takenEquip = new bool[child.Length];
     66      foreach (var loc in Enumerable.Range(0, locations).Shuffle(random)) {
    5967        int parent = random.Next(parents.Length);
    60         if (!groupedLocations[parent].ContainsKey(i)) {
     68        if (lookup[parent][loc] == null) {
    6169          int tmp = parent;
    6270          do {
    6371            tmp = (tmp + 1) % parents.Length;
    64           } while (tmp != parent && !groupedLocations[tmp].ContainsKey(i));
     72          } while (tmp != parent && lookup[tmp][loc] == null);
    6573          if (parent == tmp) continue;
    6674          else parent = tmp;
    6775        }
    68         foreach (var item in groupedLocations[parent][i]) {
    69           if (remainingEquipment.Contains(item.Equipment)) {
    70             child[item.Equipment] = i;
    71             remainingEquipment.Remove(item.Equipment);
     76        foreach (var equip in lookup[parent][loc]) {
     77          if (!takenEquip[equip]) {
     78            child[equip] = loc;
     79            takenEquip[equip] = true;
     80            slack[loc] -= demands[equip];
    7281          }
    7382        }
    7483      }
    7584
    76       foreach (var equipment in remainingEquipment) {
    77         int parent = random.Next(parents.Length);
    78         child[equipment] = parents[parent][equipment];
     85      var order = Enumerable.Range(0, takenEquip.Length).Shuffle(random); // avoid bias
     86      foreach (var e in order) {
     87        if (takenEquip[e]) continue;
     88        var assigned = false;
     89        // try 1: find a parent where equipment can be assigned feasibly
     90        foreach (var p in Enumerable.Range(0, parents.Length).Shuffle(random)) {
     91          if (slack[parents[p][e]] >= demands[e]) {
     92            child[e] = parents[p][e];
     93            slack[child[e]] -= demands[e];
     94            assigned = true;
     95            break;
     96          }
     97        }
     98        // try 2: find a random feasible location
     99        if (!assigned) {
     100          var possible = Enumerable.Range(0, locations).Where(x => slack[x] >= demands[e]).ToList();
     101          if (possible.Count > 0) {
     102            var loc = possible.SampleRandom(random);
     103            child[e] = loc;
     104            slack[loc] -= demands[e];
     105            assigned = true;
     106          }
     107        }
     108        // try 3: insert in a random location (no feasible insert found)
     109        if (!assigned) {
     110          child[e] = random.Next(locations);
     111          slack[child[e]] -= demands[e];
     112        }
    79113      }
    80114
     
    84118    protected override IntegerVector Cross(IRandom random, ItemArray<IntegerVector> parents,
    85119      GQAPInstance problemInstance) {
    86       return Apply(random, parents, problemInstance.Capacities);
     120      return Apply(random, parents, problemInstance.Demands, problemInstance.Capacities);
    87121    }
    88122  }
Note: See TracChangeset for help on using the changeset viewer.