Free cookie consent management tool by TermsFeed Policy Generator

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

#1614:

  • Fixed some bugs
  • Added OSGA
  • Updated ES (added recombination)
  • Reusing operators among algorihtms (RelocateEquipmentManipluator)
  • Rewrote DiscreteLocationCrossover
Location:
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators
Files:
2 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  }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/RelocateEquipmentManipluator.cs

    r15504 r15564  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Core;
    27 using HeuristicLab.Data;
    2826using HeuristicLab.Encodings.IntegerVectorEncoding;
    2927using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4745    }
    4846
    49     public static void Apply(IRandom random, IntegerVector assignment, DoubleArray capacities, DoubleArray demands) {
    50       int locations = capacities.Length;
     47    public static void Apply(IRandom random, IntegerVector assignment, int locations, double stopProb) {
    5148      if (locations < 2) throw new InvalidOperationException("There are not enough locations for relocation operations.");
    5249
    53       Dictionary<int, double> usedCapacities = new Dictionary<int, double>();
    54       Dictionary<int, List<int>> groupedLocations = new Dictionary<int, List<int>>();
    55       for (int i = 0; i < locations; i++) {
    56         usedCapacities[i] = 0.0;
    57         groupedLocations.Add(i, new List<int>());
    58       }
    59       for (int i = 0; i < assignment.Length; i++) {
    60         usedCapacities[assignment[i]] += demands[i];
    61         groupedLocations[assignment[i]].Add(i);
    62       }
    63 
    64       var overbookedLocations = usedCapacities.Where(x => capacities[x.Key] < x.Value);
    65       var freeLocations = usedCapacities.Where(x => capacities[x.Key] > x.Value);
    66       if (!overbookedLocations.Any() || !freeLocations.Any()) { // perform a random relocation
    67         assignment[random.Next(assignment.Length)] = random.Next(locations);
    68         return;
    69       }
    70 
    71       var sourceLocation = overbookedLocations.SampleRandom(random);
    72       int equipmentToRelocate = groupedLocations[sourceLocation.Key].SampleRandom(random);
    73       var bestFreeLocations = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipmentToRelocate]);
    74 
    75       if (bestFreeLocations.Any()) { // a feasible solution will still be feasible, an infeasible solution might become feasible
    76         var selected = bestFreeLocations.SampleRandom(random);
    77         assignment[equipmentToRelocate] = sourceLocation.Key;
    78       } else { // the solution will become infeasible
    79         sourceLocation = freeLocations.SampleRandom(random);
    80         assignment[equipmentToRelocate] = sourceLocation.Key;
     50      var equipments = Enumerable.Range(0, assignment.Length).Shuffle(random);
     51      foreach (var equipment in equipments) {
     52        var oldLoc = assignment[equipment];
     53        var newLoc =random.Next(locations - 1);
     54        if (newLoc >= oldLoc) newLoc++; // don't reassign to current loc
     55        assignment[equipment] = newLoc;
     56        if (random.NextDouble() < stopProb) break;
    8157      }
    8258    }
    8359
    8460    protected override void Manipulate(IRandom random, IntegerVector vector, GQAPInstance problemInstance) {
    85       Apply(random, vector, problemInstance.Capacities, problemInstance.Demands);
     61      Apply(random, vector, problemInstance.Capacities.Length, 4.0 / problemInstance.Demands.Length);
    8662    }
    8763  }
Note: See TracChangeset for help on using the changeset viewer.