Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/16/16 17:10:05 (8 years ago)
Author:
abeham
Message:

#2701:

  • Reusing similiarty calculator in BinaryMemPR
  • Fixing distance calculation for linear linkage and LinearLinkageMemPR
  • Small changes to base algorithm
  • Added biased model trainer for permutation (rank and fitness)
  • Fixing best known quality calculation for GCP
File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified branches/MemPRAlgorithm/HeuristicLab.Problems.GraphColoring/3.3/GraphColoringProblem.cs

    r14487 r14496  
    5454      get { return fitnessFunctionParameter; }
    5555    }
     56    public FitnessFunction FitnessFunction {
     57      get { return fitnessFunctionParameter.Value.Value; }
     58      set { fitnessFunctionParameter.Value.Value = value; }
     59    }
    5660
    5761    [StorableConstructor]
     
    6670      Encoding = new LinearLinkageEncoding("lle");
    6771      Parameters.Add(adjacencyListParameter = new ValueParameter<IntMatrix>("Adjacency List", "The adjacency list that describes the (symmetric) edges in the graph with nodes from 0 to N-1."));
    68       Parameters.Add(fitnessFunctionParameter = new ValueParameter<EnumValue<FitnessFunction>>("Fitness Function", "The function to use for evaluating the quality of a solution.", new EnumValue<FitnessFunction>(FitnessFunction.Prioritized)));
     72      Parameters.Add(fitnessFunctionParameter = new ValueParameter<EnumValue<FitnessFunction>>("Fitness Function", "The function to use for evaluating the quality of a solution.", new EnumValue<FitnessFunction>(FitnessFunction.Penalized)));
    6973
    7074      var imat = new IntMatrix(defaultInstance.Length, 2);
     
    7579      Encoding.Length = defaultInstanceNodes;
    7680      AdjacencyListParameter.Value = imat;
    77       BestKnownQuality = 7;
     81      BestKnownQualityParameter.Value = null;
    7882
    7983      InitializeOperators();
     
    112116
    113117    public override double Evaluate(Individual individual, IRandom random) {
    114       var fitFun = FitnessFunctionParameter.Value.Value;
    115118      var adjList = adjacencyListParameter.Value;
    116       var lle = individual.LinearLinkage(Encoding.Name).ToEndLinks(); // LLE-e encoding uses the highest indexed member as group number
    117 
    118       switch (fitFun) {
     119      var llee = individual.LinearLinkage(Encoding.Name).ToEndLinks(); // LLE-e encoding uses the highest indexed member as group number
     120
     121      switch (FitnessFunction) {
    119122        case FitnessFunction.Prioritized: {
    120             var conflicts = 0;
    121             var colors = lle.Distinct().Count();
    122             for (var r = 0; r < adjList.Rows; r++) {
    123               if (lle[adjList[r, 0]] == lle[adjList[r, 1]]) conflicts++; // both nodes are adjacent and have the same color (are in the same group)
    124             }
     123            var colors = llee.Distinct().Count();
     124            var conflicts = CalculateConflicts(llee);
    125125            // number of conflicts is the integer part of the quality
    126126            // number of colors constitutes the fractional part
    127127            // the number of fractional digits is defined by the maximum number of possible colors (each node its own color)
    128             var mag = Math.Pow(10, -(int)Math.Ceiling(Math.Log10(lle.Length)));
     128            var mag = Math.Pow(10, -(int)Math.Ceiling(Math.Log10(llee.Length)));
    129129            // the value is e.g. 4.03 for 4 conflicts with 3 colors (and less than 100 nodes)
    130130            return conflicts + colors * mag;
     
    136136            // Operations Research 39(3), pp. 378–406.
    137137            // All local optima of this function correspond to legal colorings.
    138             var colors = lle.GroupBy(x => x).ToDictionary(x => x.Key, x => new EvaluationHelper() { ColorCount = x.Count() });
     138            // We need to calculate conflicts and nodes per color
     139            var colors = llee.GroupBy(x => x).ToDictionary(x => x.Key, x => new EvaluationHelper() { ColorCount = x.Count() });
    139140            for (var r = 0; r < adjList.Rows; r++) {
    140               var color1 = lle[adjList[r, 0]];
    141               var color2 = lle[adjList[r, 1]];
     141              var color1 = llee[adjList[r, 0]];
     142              var color2 = llee[adjList[r, 1]];
    142143              if (color1 == color2) colors[color1].ConflictCount++;
    143144            }
    144145            return 2 * colors.Sum(x => x.Value.ColorCount * x.Value.ConflictCount) - colors.Sum(x => x.Value.ColorCount * x.Value.ColorCount);
    145146          }
    146         default: throw new InvalidOperationException(string.Format("Unknown fitness function {0}.", fitFun));
     147        default: throw new InvalidOperationException(string.Format("Unknown fitness function {0}.", FitnessFunction));
    147148      }
    148149    }
     
    156157      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    157158      var best = Maximization ? orderedIndividuals.Last().Individual.LinearLinkage(Encoding.Name) : orderedIndividuals.First().Individual.LinearLinkage(Encoding.Name);
    158 
    159       var adjList = AdjacencyListParameter.Value;
     159       
    160160      var lle = best.ToEndLinks();
    161       var conflicts = 0;
    162161      var colors = lle.Distinct().Count();
    163       for (var r = 0; r < adjList.Rows; r++) {
    164         if (lle[adjList[r, 0]] == lle[adjList[r, 1]]) conflicts++; // both nodes are adjacent and have the same color (are in the same group)
    165       }
     162      var conflicts = CalculateConflicts(lle);
    166163
    167164      IResult res;
     
    176173    }
    177174
     175    private int CalculateConflicts(int[] llee) {
     176      var adjList = AdjacencyListParameter.Value;
     177      var conflicts = 0;
     178      for (var r = 0; r < adjList.Rows; r++) {
     179        if (llee[adjList[r, 0]] == llee[adjList[r, 1]]) conflicts++; // both nodes are adjacent and have the same color (are in the same group)
     180      }
     181      return conflicts;
     182    }
     183
    178184    public void Load(GCPData data) {
    179185      Encoding.Length = data.Nodes;
    180186      AdjacencyListParameter.Value = new IntMatrix(data.Adjacencies);
    181       if (FitnessFunctionParameter.Value.Value == FitnessFunction.Prioritized && data.BestKnownColors.HasValue) {
     187      if (data.BestKnownColors.HasValue && FitnessFunction == FitnessFunction.Prioritized) {
    182188        var mag = Math.Pow(10, -(int)Math.Ceiling(Math.Log10(data.Nodes)));
    183         // the value is e.g. 4.03 for 4 conflicts with 3 colors (and less than 100 nodes)
     189        // the value is e.g. 0.051 for 0 conflicts with 51 colors (and less than 1000 nodes)
    184190        BestKnownQuality = data.BestKnownColors.Value * mag;
     191      } else if (data.BestKnownColoring != null && FitnessFunction == FitnessFunction.Prioritized) {
     192        var mag = Math.Pow(10, -(int)Math.Ceiling(Math.Log10(data.Nodes)));
     193        var colors = data.BestKnownColoring.Distinct().Count();
     194        BestKnownQuality = colors * mag;
     195      } else if (data.BestKnownColoring != null && FitnessFunction == FitnessFunction.Penalized) {
     196        var colors = data.BestKnownColoring.GroupBy(x => x).Select(x => x.Count());
     197        BestKnownQuality = -colors.Sum(x => x * x);
    185198      } else BestKnownQualityParameter.Value = null;
    186199      Name = data.Name;
Note: See TracChangeset for help on using the changeset viewer.