Changeset 16767
- Timestamp:
- 04/05/19 09:37:39 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.GraphColoring/3.3/GraphColoringProblem.cs
r16723 r16767 22 22 using System; 23 23 using System.Linq; 24 using HEAL.Attic; 24 25 using HeuristicLab.Analysis; 25 26 using HeuristicLab.Common; … … 30 31 using HeuristicLab.Optimization.Operators; 31 32 using HeuristicLab.Parameters; 32 using HEAL.Attic;33 33 using HeuristicLab.Problems.Instances; 34 34 … … 38 38 [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 135)] 39 39 [StorableType("007BD5F0-196C-4045-AC5D-BF287927C3DC")] 40 public sealed class GraphColoringProblem : SingleObjectiveBasicProblem<LinearLinkageEncoding>, IProblemInstanceConsumer<GCPData>, IProblemInstanceExporter<GCPData> { 40 public sealed class GraphColoringProblem : SingleObjectiveProblem<LinearLinkageEncoding, LinearLinkage>, 41 IProblemInstanceConsumer<GCPData>, IProblemInstanceExporter<GCPData> { 41 42 42 43 public override bool Maximization { … … 74 75 } 75 76 public GraphColoringProblem() { 76 Encoding = new LinearLinkageEncoding("lle");77 77 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.")); 78 78 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))); … … 130 130 } 131 131 132 public override double Evaluate( Individual individual, IRandom random) {132 public override double Evaluate(LinearLinkage lle, IRandom random) { 133 133 var adjList = adjacencyListParameter.Value; 134 var llee = individual.LinearLinkage(Encoding.Name).ToEndLinks(); // LLE-e encoding uses the highest indexed member as group number134 var llee = lle.ToEndLinks(); // LLE-e encoding uses the highest indexed member as group number 135 135 136 136 switch (FitnessFunction) { … … 169 169 } 170 170 171 public override void Analyze( Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {172 var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual= i, Quality = q }).OrderBy(z => z.Quality);173 var best = Maximization ? orderedIndividuals.Last(). Individual.LinearLinkage(Encoding.Name) : orderedIndividuals.First().Individual.LinearLinkage(Encoding.Name);171 public override void Analyze(LinearLinkage[] lles, double[] qualities, ResultCollection results, IRandom random) { 172 var orderedIndividuals = lles.Zip(qualities, (i, q) => new { LLE = i, Quality = q }).OrderBy(z => z.Quality); 173 var best = Maximization ? orderedIndividuals.Last().LLE : orderedIndividuals.First().LLE; 174 174 175 var lle = best.ToEndLinks();176 var colors = lle .Distinct().Count();177 var conflicts = CalculateConflicts(lle );175 var llee = best.ToEndLinks(); 176 var colors = llee.Distinct().Count(); 177 var conflicts = CalculateConflicts(llee); 178 178 179 179 IResult res; … … 199 199 ((IntValue)res.Value).Value = bestColors = colors; 200 200 } 201 if (!results.TryGetValue("Best Solution", out res)) { 202 res = new Result("Best Solution", (LinearLinkage)best.Clone()); 203 results.Add(res); 201 if (!results.ContainsKey("Best Encoded Solution") || improvement) 202 results.AddOrUpdateResult("Best Encoded Solution", (LinearLinkage)best.Clone()); 203 204 if (!results.TryGetValue("Best Solution", out res) || !(res.Value is IntMatrix)) { 205 var matrix = new IntMatrix(llee.Length, 2) { ColumnNames = new[] { "Node", "Color" } }; 206 UpdateMatrix(llee, matrix); 207 res = new Result("Best Solution", matrix); 208 results.AddOrUpdateResult("Best Solution", matrix); 204 209 } else { 205 if (improvement) 206 res.Value = (LinearLinkage)best.Clone(); 210 if (improvement) { 211 UpdateMatrix(llee, (IntMatrix)res.Value); 212 } 207 213 } 208 214 … … 210 216 if (BestKnownColorsParameter.Value == null || BestKnownColorsParameter.Value.Value > colors) 211 217 BestKnownColorsParameter.Value = new IntValue(colors); 218 } 219 } 220 221 private static void UpdateMatrix(int[] llee, IntMatrix matrix) { 222 var assign = llee.Select((v, i) => new { Node = i, Color = v }).OrderBy(x => x.Color); 223 var color = 0; 224 var prev = -1; 225 foreach (var a in assign) { 226 if (prev >= 0 && prev != a.Color) 227 color++; 228 matrix[a.Node, 0] = a.Node; 229 matrix[a.Node, 1] = color; 230 prev = a.Color; 212 231 } 213 232 }
Note: See TracChangeset
for help on using the changeset viewer.