- Timestamp:
- 12/16/16 17:10:05 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified branches/MemPRAlgorithm/HeuristicLab.Problems.GraphColoring/3.3/GraphColoringProblem.cs ¶
r14487 r14496 54 54 get { return fitnessFunctionParameter; } 55 55 } 56 public FitnessFunction FitnessFunction { 57 get { return fitnessFunctionParameter.Value.Value; } 58 set { fitnessFunctionParameter.Value.Value = value; } 59 } 56 60 57 61 [StorableConstructor] … … 66 70 Encoding = new LinearLinkageEncoding("lle"); 67 71 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.P rioritized)));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))); 69 73 70 74 var imat = new IntMatrix(defaultInstance.Length, 2); … … 75 79 Encoding.Length = defaultInstanceNodes; 76 80 AdjacencyListParameter.Value = imat; 77 BestKnownQuality = 7;81 BestKnownQualityParameter.Value = null; 78 82 79 83 InitializeOperators(); … … 112 116 113 117 public override double Evaluate(Individual individual, IRandom random) { 114 var fitFun = FitnessFunctionParameter.Value.Value;115 118 var adjList = adjacencyListParameter.Value; 116 var lle = individual.LinearLinkage(Encoding.Name).ToEndLinks(); // LLE-e encoding uses the highest indexed member as group number117 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) { 119 122 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); 125 125 // number of conflicts is the integer part of the quality 126 126 // number of colors constitutes the fractional part 127 127 // 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))); 129 129 // the value is e.g. 4.03 for 4 conflicts with 3 colors (and less than 100 nodes) 130 130 return conflicts + colors * mag; … … 136 136 // Operations Research 39(3), pp. 378–406. 137 137 // 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() }); 139 140 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]]; 142 143 if (color1 == color2) colors[color1].ConflictCount++; 143 144 } 144 145 return 2 * colors.Sum(x => x.Value.ColorCount * x.Value.ConflictCount) - colors.Sum(x => x.Value.ColorCount * x.Value.ColorCount); 145 146 } 146 default: throw new InvalidOperationException(string.Format("Unknown fitness function {0}.", fitFun));147 default: throw new InvalidOperationException(string.Format("Unknown fitness function {0}.", FitnessFunction)); 147 148 } 148 149 } … … 156 157 var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality); 157 158 var best = Maximization ? orderedIndividuals.Last().Individual.LinearLinkage(Encoding.Name) : orderedIndividuals.First().Individual.LinearLinkage(Encoding.Name); 158 159 var adjList = AdjacencyListParameter.Value; 159 160 160 var lle = best.ToEndLinks(); 161 var conflicts = 0;162 161 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); 166 163 167 164 IResult res; … … 176 173 } 177 174 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 178 184 public void Load(GCPData data) { 179 185 Encoding.Length = data.Nodes; 180 186 AdjacencyListParameter.Value = new IntMatrix(data.Adjacencies); 181 if ( FitnessFunctionParameter.Value.Value == FitnessFunction.Prioritized && data.BestKnownColors.HasValue) {187 if (data.BestKnownColors.HasValue && FitnessFunction == FitnessFunction.Prioritized) { 182 188 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) 184 190 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); 185 198 } else BestKnownQualityParameter.Value = null; 186 199 Name = data.Name;
Note: See TracChangeset
for help on using the changeset viewer.