- Timestamp:
- 09/15/20 13:53:11 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.GraphColoring/3.3/GraphColoringProblem.cs
r17695 r17745 170 170 } 171 171 172 public override void Analyze(LinearLinkage[] lles, double[] qualities, ResultCollection results, IRandom random) { 173 var orderedIndividuals = lles.Zip(qualities, (i, q) => new { LLE = i, Quality = q }).OrderBy(z => z.Quality); 174 var best = Maximization ? orderedIndividuals.Last().LLE : orderedIndividuals.First().LLE; 175 176 var llee = best.ToEndLinks(); 177 var colors = llee.Distinct().Count(); 178 var conflicts = CalculateConflicts(llee); 179 180 IResult res; 181 int bestColors = int.MaxValue, bestConflicts = int.MaxValue; 182 var improvement = false; 183 if (!results.TryGetValue("Best Solution Conflicts", out res)) { 184 bestConflicts = conflicts; 185 res = new Result("Best Solution Conflicts", new IntValue(bestConflicts)); 186 results.Add(res); 187 } else { 188 bestConflicts = ((IntValue)res.Value).Value; 189 improvement = conflicts < bestConflicts; 190 if (improvement) ((IntValue)res.Value).Value = bestConflicts = conflicts; 191 } 192 if (!results.TryGetValue("Best Solution Colors", out res)) { 193 bestColors = colors; 194 res = new Result("Best Solution Colors", new IntValue(bestColors)); 195 results.Add(res); 196 } else { 197 bestColors = ((IntValue)res.Value).Value; 198 improvement = improvement || conflicts == bestConflicts && colors < bestColors; 199 if (improvement) 200 ((IntValue)res.Value).Value = bestColors = colors; 201 } 202 if (!results.ContainsKey("Best Encoded Solution") || improvement) 203 results.AddOrUpdateResult("Best Encoded Solution", (LinearLinkage)best.Clone()); 204 205 if (!results.TryGetValue("Best Solution", out res) || !(res.Value is IntMatrix)) { 206 var matrix = new IntMatrix(llee.Length, 2) { ColumnNames = new[] { "Node", "Color" } }; 207 UpdateMatrix(llee, matrix); 208 res = new Result("Best Solution", matrix); 209 results.AddOrUpdateResult("Best Solution", matrix); 210 } else { 211 if (improvement) { 212 UpdateMatrix(llee, (IntMatrix)res.Value); 213 } 214 } 215 216 if (conflicts == 0) { 217 if (BestKnownColorsParameter.Value == null || BestKnownColorsParameter.Value.Value > colors) 218 BestKnownColorsParameter.Value = new IntValue(colors); 219 } 172 public override void Analyze(ISingleObjectiveSolutionContext<LinearLinkage>[] solutionContexts, IRandom random) { 173 base.Analyze(solutionContexts, random); 174 175 var lles = solutionContexts.Select(context => context.EncodedSolution).ToArray(); 176 177 //TODO: reimplement code below using results directly 178 179 //var orderedIndividuals = lles.Zip(qualities, (i, q) => new { LLE = i, Quality = q }).OrderBy(z => z.Quality); 180 //var best = Maximization ? orderedIndividuals.Last().LLE : orderedIndividuals.First().LLE; 181 182 //var llee = best.ToEndLinks(); 183 //var colors = llee.Distinct().Count(); 184 //var conflicts = CalculateConflicts(llee); 185 186 //IResult res; 187 //int bestColors = int.MaxValue, bestConflicts = int.MaxValue; 188 //var improvement = false; 189 //if (!results.TryGetValue("Best Solution Conflicts", out res)) { 190 // bestConflicts = conflicts; 191 // res = new Result("Best Solution Conflicts", new IntValue(bestConflicts)); 192 // results.Add(res); 193 //} else { 194 // bestConflicts = ((IntValue)res.Value).Value; 195 // improvement = conflicts < bestConflicts; 196 // if (improvement) ((IntValue)res.Value).Value = bestConflicts = conflicts; 197 //} 198 //if (!results.TryGetValue("Best Solution Colors", out res)) { 199 // bestColors = colors; 200 // res = new Result("Best Solution Colors", new IntValue(bestColors)); 201 // results.Add(res); 202 //} else { 203 // bestColors = ((IntValue)res.Value).Value; 204 // improvement = improvement || conflicts == bestConflicts && colors < bestColors; 205 // if (improvement) 206 // ((IntValue)res.Value).Value = bestColors = colors; 207 //} 208 //if (!results.ContainsKey("Best Encoded Solution") || improvement) 209 // results.AddOrUpdateResult("Best Encoded Solution", (LinearLinkage)best.Clone()); 210 211 //if (!results.TryGetValue("Best Solution", out res) || !(res.Value is IntMatrix)) { 212 // var matrix = new IntMatrix(llee.Length, 2) { ColumnNames = new[] { "Node", "Color" } }; 213 // UpdateMatrix(llee, matrix); 214 // res = new Result("Best Solution", matrix); 215 // results.AddOrUpdateResult("Best Solution", matrix); 216 //} else { 217 // if (improvement) { 218 // UpdateMatrix(llee, (IntMatrix)res.Value); 219 // } 220 //} 221 222 //if (conflicts == 0) { 223 // if (BestKnownColorsParameter.Value == null || BestKnownColorsParameter.Value.Value > colors) 224 // BestKnownColorsParameter.Value = new IntValue(colors); 225 //} 220 226 } 221 227
Note: See TracChangeset
for help on using the changeset viewer.