Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17952


Ignore:
Timestamp:
04/21/21 11:58:34 (4 years ago)
Author:
abeham
Message:

#2521: working on graph coloring problem

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.GraphColoring/3.3/GraphColoringProblem.cs

    r17778 r17952  
    4747    [Storable] public IValueParameter<IntValue> BestKnownColorsParameter { get; private set; }
    4848    [Storable] public IResult<IntValue> BestSolutionColorsResult { get; private set; }
    49     [Storable] public IResult<IntValue> BestSolutionConflicts { get; private set; }
     49    [Storable] public IResult<IntValue> BestSolutionConflictsResult { get; private set; }
     50    [Storable] public IResult<IntMatrix> BestColoringResult { get; private set; }
    5051
    5152    public FitnessFunction FitnessFunction {
     
    6162      FitnessFunctionParameter = cloner.Clone(original.FitnessFunctionParameter);
    6263      BestKnownColorsParameter = cloner.Clone(original.BestKnownColorsParameter);
     64      BestSolutionColorsResult = cloner.Clone(original.BestSolutionColorsResult);
     65      BestSolutionConflictsResult = cloner.Clone(original.BestSolutionConflictsResult);
     66      BestColoringResult = cloner.Clone(original.BestColoringResult);
    6367      RegisterEventHandlers();
    6468    }
     
    6973      Parameters.Add(BestKnownColorsParameter = new OptionalValueParameter<IntValue>("BestKnownColors", "The least amount of colors in a valid coloring.") { ReadOnly = true });
    7074      Results.Add(BestSolutionColorsResult = new Result<IntValue>("Best Solution Colors", "The number of best solution found so far."));
     75      Results.Add(BestSolutionConflictsResult = new Result<IntValue>("Best Solution Conflicts", "The number of conflicts in the the best solution found so far."));
     76      Results.Add(BestColoringResult = new Result<IntMatrix>("Best Coloring", "The best coloring that is currently found."));
    7177
    7278      var imat = new IntMatrix(defaultInstance.Length, 2);
     
    165171      base.Analyze(solutionContexts, random);
    166172
    167       var lles = solutionContexts.Select(context => context.EncodedSolution).ToArray();
    168 
    169       //TODO: reimplement code below using results directly
    170 
    171       //var orderedIndividuals = lles.Zip(qualities, (i, q) => new { LLE = i, Quality = q }).OrderBy(z => z.Quality);
    172       //var best = Maximization ? orderedIndividuals.Last().LLE : orderedIndividuals.First().LLE;
    173 
    174       //var llee = best.ToEndLinks();
    175       //var colors = llee.Distinct().Count();
    176       //var conflicts = CalculateConflicts(llee);
    177 
    178       //IResult res;
    179       //int bestColors = int.MaxValue, bestConflicts = int.MaxValue;
    180       //var improvement = false;
    181       //if (!results.TryGetValue("Best Solution Conflicts", out res)) {
    182       //  bestConflicts = conflicts;
    183       //  res = new Result("Best Solution Conflicts", new IntValue(bestConflicts));
    184       //  results.Add(res);
    185       //} else {
    186       //  bestConflicts = ((IntValue)res.Value).Value;
    187       //  improvement = conflicts < bestConflicts;
    188       //  if (improvement) ((IntValue)res.Value).Value = bestConflicts = conflicts;
    189       //}
    190       //if (!results.TryGetValue("Best Solution Colors", out res)) {
    191       //  bestColors = colors;
    192       //  res = new Result("Best Solution Colors", new IntValue(bestColors));
    193       //  results.Add(res);
    194       //} else {
    195       //  bestColors = ((IntValue)res.Value).Value;
    196       //  improvement = improvement || conflicts == bestConflicts && colors < bestColors;
    197       //  if (improvement)
    198       //    ((IntValue)res.Value).Value = bestColors = colors;
    199       //}
    200       //if (!results.ContainsKey("Best Encoded Solution") || improvement)
    201       //  results.AddOrUpdateResult("Best Encoded Solution", (LinearLinkage)best.Clone());
    202 
    203       //if (!results.TryGetValue("Best Solution", out res) || !(res.Value is IntMatrix)) {
    204       //  var matrix = new IntMatrix(llee.Length, 2) { ColumnNames = new[] { "Node", "Color" } };
    205       //  UpdateMatrix(llee, matrix);
    206       //  res = new Result("Best Solution", matrix);
    207       //  results.AddOrUpdateResult("Best Solution", matrix);
    208       //} else {
    209       //  if (improvement) {
    210       //    UpdateMatrix(llee, (IntMatrix)res.Value);
    211       //  }
    212       //}
    213 
    214       //if (conflicts == 0) {
    215       //  if (BestKnownColorsParameter.Value == null || BestKnownColorsParameter.Value.Value > colors)
    216       //    BestKnownColorsParameter.Value = new IntValue(colors);
    217       //}
     173      var best = GetBest(solutionContexts);
     174      var llee = best.EncodedSolution.ToEndLinks();
     175      var colors = llee.Distinct().Count();
     176      var conflicts = CalculateConflicts(llee);
     177
     178      var improvement = !BestSolutionConflictsResult.HasValue
     179        || conflicts < BestSolutionConflictsResult.Value.Value
     180        || (conflicts == BestSolutionConflictsResult.Value.Value
     181          && colors < BestSolutionColorsResult.Value.Value);
     182
     183      if (improvement) {
     184        BestSolutionColorsResult.Value = new IntValue(colors);
     185        BestSolutionConflictsResult.Value = new IntValue(conflicts);
     186        var matrix = new IntMatrix(llee.Length, 2) { ColumnNames = new[] { "Node", "Color" } };
     187        UpdateMatrix(llee, matrix);
     188        BestColoringResult.Value = matrix;
     189      }
     190
     191      if (conflicts == 0) {
     192        if (BestKnownColorsParameter.Value == null || colors < BestKnownColorsParameter.Value.Value)
     193          BestKnownColorsParameter.Value = new IntValue(colors);
     194      }
    218195    }
    219196
Note: See TracChangeset for help on using the changeset viewer.