- Timestamp:
- 04/11/16 17:34:50 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
r13750 r13751 53 53 } 54 54 55 private IntValue maximumEvaluations;55 private readonly IntValue maximumEvaluations; 56 56 public IntValue MaximumEvaluations { 57 57 get { return maximumEvaluations; } 58 58 } 59 59 60 private RunCollection instanceRuns;60 private readonly RunCollection instanceRuns; 61 61 public RunCollection InstanceRuns { 62 62 get { return instanceRuns; } 63 63 } 64 64 65 private RunCollection seededRuns;65 private readonly RunCollection seededRuns; 66 66 public RunCollection SeededRuns { 67 67 get { return seededRuns; } 68 68 } 69 69 70 private RunCollection knowledgeBase;70 private readonly RunCollection knowledgeBase; 71 71 public RunCollection KnowledgeBase { 72 72 get { return knowledgeBase; } 73 73 } 74 74 75 private SingleObjectiveOKBProblem problem;75 private readonly SingleObjectiveOKBProblem problem; 76 76 public SingleObjectiveOKBProblem Problem { 77 77 get { return problem; } 78 78 } 79 79 80 private ItemList<IAlgorithm> suggestedInstances;81 private ReadOnlyItemList<IAlgorithm> readOnlySuggestedInstances;80 private readonly ItemList<IAlgorithm> suggestedInstances; 81 private readonly ReadOnlyItemList<IAlgorithm> readOnlySuggestedInstances; 82 82 public ReadOnlyItemList<IAlgorithm> SuggestedInstances { 83 83 get { return readOnlySuggestedInstances; } 84 84 } 85 85 86 private RunCollection problemInstances;86 private readonly RunCollection problemInstances; 87 87 public RunCollection ProblemInstances { 88 88 get { return problemInstances; } 89 89 } 90 90 91 private CheckedItemList<IScope> solutionSeedingPool; 91 private readonly CheckedItemList<StringValue> problemCharacteristics; 92 public CheckedItemList<StringValue> ProblemCharacteristics { 93 get { return problemCharacteristics; } 94 } 95 96 private readonly CheckedItemList<IScope> solutionSeedingPool; 92 97 public CheckedItemList<IScope> SolutionSeedingPool { 93 98 get { return solutionSeedingPool; } 94 99 } 95 100 96 private EnumValue<SeedingStrategyTypes> seedingStrategy;101 private readonly EnumValue<SeedingStrategyTypes> seedingStrategy; 97 102 public EnumValue<SeedingStrategyTypes> SeedingStrategy { 98 103 get { return seedingStrategy; } … … 101 106 private BidirectionalLookup<long, IRun> algorithmId2RunMapping; 102 107 private BidirectionalDictionary<long, IAlgorithm> algorithmId2AlgorithmInstanceMapping; 103 104 private Run currentInstance; 105 108 private BidirectionalDictionary<long, IRun> problemId2ProblemInstanceMapping; 109 106 110 private bool Maximization { 107 111 get { return Problem != null && Problem.ProblemId >= 0 && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; } … … 116 120 readOnlySuggestedInstances = suggestedInstances.AsReadOnly(); 117 121 problemInstances = new RunCollection(); 122 problemCharacteristics = new CheckedItemList<StringValue>(); 118 123 problem = new SingleObjectiveOKBProblem(); 119 124 algorithmId2RunMapping = new BidirectionalLookup<long, IRun>(); 120 125 algorithmId2AlgorithmInstanceMapping = new BidirectionalDictionary<long, IAlgorithm>(); 126 problemId2ProblemInstanceMapping = new BidirectionalDictionary<long, IRun>(); 121 127 solutionSeedingPool = new CheckedItemList<IScope>(); 122 128 seedingStrategy = new EnumValue<SeedingStrategyTypes>(SeedingStrategyTypes.NoSeeding); … … 162 168 } 163 169 164 private void UpdateInstanceProjection() { 165 currentInstance = null; 166 var filter = new HashSet<string>(); 167 168 if (Problem.ProblemId != -1) { 169 currentInstance = new Run { Name = Problem.Name }; 170 Problem.Problem.CollectParameterValues(currentInstance.Parameters); 171 foreach (var c in RunCreationClient.Instance.GetCharacteristicValues(Problem.ProblemId)) { 172 var key = "Characteristic." + c.Name; 173 currentInstance.Results.Add(key, RunCreationClient.Instance.ConvertToItem(c)); 174 filter.Add(key); 175 } 176 } 177 // TODO: There is a problem with missing values that has to be solved 178 // The common set of characteristics among all problems may be too small to be useful 179 // It has to be decided somehow which problem instances to include in order to obtain the set of features that is expressive and available 180 var allCharacteristics = ProblemInstances.Select(x => new HashSet<string>(x.Results.Where(y => y.Key.StartsWith("Characteristic.", StringComparison.Ordinal)).Select(y => y.Key))).ToList(); 181 if (allCharacteristics.Count == 0) return; 182 183 var commonCharacteristics = filter.Count > 0 ? filter : allCharacteristics[0]; 184 for (var i = 0; i < allCharacteristics.Count; i++) 185 commonCharacteristics.IntersectWith(allCharacteristics[i]); 186 187 if (commonCharacteristics.Count == 0) return; 188 189 var instances = new Dictionary<string, double[]>(); 190 foreach (var i in ProblemInstances) 191 instances[i.Name] = i.Results.Where(x => commonCharacteristics.Contains(x.Key)).OrderBy(x => x.Key).Select(x => (double)((dynamic)x.Value).Value).ToArray(); 192 if (currentInstance != null) 193 instances[currentInstance.Name] = currentInstance.Results.Where(x => commonCharacteristics.Contains(x.Key)).OrderBy(x => x.Key).Select(x => (double)((dynamic)x.Value).Value).ToArray(); 170 public bool IsCurrentInstance(IRun run) { 171 if (!problemId2ProblemInstanceMapping.ContainsSecond(run)) return false; 172 return problemId2ProblemInstanceMapping.GetBySecond(run) == Problem.ProblemId; 173 } 174 175 public void UpdateInstanceProjection() { 176 if (ProblemCharacteristics.Count == 0) return; 177 178 var instances = new Dictionary<IRun, double[]>(); 179 foreach (var run in ProblemInstances) { 180 var f = 0; 181 instances[run] = new double[ProblemCharacteristics.CheckedItems.Count()]; 182 foreach (var c in ProblemCharacteristics.CheckedItems.Select(x => x.Value.Value)) { 183 IItem item; 184 if (run.Results.TryGetValue(c, out item)) { 185 instances[run][f] = (double)((dynamic)item).Value; 186 } else instances[run][f] = 0; // TODO: handle missing values 187 f++; 188 } 189 } 194 190 195 191 var allValues = instances.SelectMany(x => x.Value).ToList(); … … 205 201 } 206 202 207 var key2Idx = new BidirectionalDictionary< string, int>();203 var key2Idx = new BidirectionalDictionary<IRun, int>(); 208 204 foreach (var kvp in instances.Select((k, i) => new { Index = i, Key = k.Key })) 209 205 key2Idx.Add(kvp.Key, kvp.Index); … … 222 218 #endregion 223 219 #region PCA 224 var ds = new double[instances.Count, commonCharacteristics.Count];220 var ds = new double[instances.Count, ProblemCharacteristics.CheckedItems.Count()]; 225 221 foreach (var instance in instances) { 226 222 var arr = instance.Value; … … 235 231 #endregion 236 232 #region SOM 237 var features = new DoubleMatrix( commonCharacteristics.Count, instances.Count);233 var features = new DoubleMatrix(ProblemCharacteristics.CheckedItems.Count(), instances.Count); 238 234 foreach (var instance in instances) { 239 235 var arr = instance.Value; … … 241 237 features[feature, key2Idx.GetByFirst(instance.Key)] = arr[feature]; 242 238 } 243 var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 20, learningRadius: 20, jittering: true);239 var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 20, learningRadius: 20, iterations: 200, jittering: true); 244 240 #endregion 245 241 … … 249 245 double x = 0, y = 0; 250 246 for (var feature = 0; feature < ds.GetLength(1); feature++) { 251 x += ds[key2Idx.GetByFirst(instance .Name), feature] * v[feature, 0];252 y += ds[key2Idx.GetByFirst(instance .Name), feature] * v[feature, 1];247 x += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 0]; 248 y += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 1]; 253 249 } 254 250 … … 262 258 263 259 if (instance.Results.TryGetValue("Projection.MDS.X", out item)) { 264 ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance .Name), 0];265 } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance .Name), 0]));260 ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 0]; 261 } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance), 0])); 266 262 if (instance.Results.TryGetValue("Projection.MDS.Y", out item)) { 267 ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance .Name), 1];268 } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance .Name), 1]));263 ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 1]; 264 } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance), 1])); 269 265 270 266 if (instance.Results.TryGetValue("Projection.SOM.X", out item)) { 271 ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance .Name), 0];272 } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance .Name), 0]));267 ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 0]; 268 } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 0])); 273 269 if (instance.Results.TryGetValue("Projection.SOM.Y", out item)) { 274 ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance .Name), 1];275 } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance .Name), 1]));270 ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 1]; 271 } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 1])); 276 272 } 277 273 } finally { ProblemInstances.UpdateOfRunsInProgress = false; } 278 279 if (currentInstance != null) {280 double x = 0, y = 0;281 for (var feature = 0; feature < ds.GetLength(1); feature++) {282 x += ds[key2Idx.GetByFirst(currentInstance.Name), feature] * v[feature, 0];283 y += ds[key2Idx.GetByFirst(currentInstance.Name), feature] * v[feature, 1];284 }285 286 IItem item;287 if (currentInstance.Results.TryGetValue("Projection.PCA.X", out item)) {288 ((DoubleValue)item).Value = x;289 } else currentInstance.Results.Add("Projection.PCA.X", new DoubleValue(x));290 if (currentInstance.Results.TryGetValue("Projection.PCA.Y", out item)) {291 ((DoubleValue)item).Value = y;292 } else currentInstance.Results.Add("Projection.PCA.Y", new DoubleValue(y));293 294 if (currentInstance.Results.TryGetValue("Projection.MDS.X", out item)) {295 ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(currentInstance.Name), 0];296 } else currentInstance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(currentInstance.Name), 0]));297 if (currentInstance.Results.TryGetValue("Projection.MDS.Y", out item)) {298 ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(currentInstance.Name), 1];299 } else currentInstance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(currentInstance.Name), 1]));300 }301 }302 303 public Tuple<double, double> ProjectCurrentInstance(string projection) {304 if (currentInstance == null) return null;305 var xKey = "Projection." + projection + ".X";306 var yKey = "Projection." + projection + ".Y";307 if (!currentInstance.Results.ContainsKey(xKey) || !currentInstance.Results.ContainsKey(yKey))308 return null;309 var x = ((DoubleValue)currentInstance.Results[xKey]).Value;310 var y = ((DoubleValue)currentInstance.Results[yKey]).Value;311 return Tuple.Create(x, y);312 274 } 313 275 … … 455 417 ProblemInstances.UpdateOfRunsInProgress = true; 456 418 ProblemInstances.Clear(); 419 var characteristics = new HashSet<string>(); 457 420 var totalProblems = adminClient.Problems.Count(x => x.ProblemClassId == probClassId); 458 421 foreach (var problInst in adminClient.Problems.Where(x => x.ProblemClassId == probClassId)) { … … 470 433 foreach (var v in RunCreationClient.Instance.GetCharacteristicValues(problInst.Id)) { 471 434 probRun.Results.Add("Characteristic." + v.Name, RunCreationClient.Instance.ConvertToItem(v)); 435 characteristics.Add(v.Name); 472 436 } 473 437 ProblemInstances.Add(probRun); 438 problemId2ProblemInstanceMapping.Add(problInst.Id, probRun); 474 439 } catch { } 475 440 stream.Close(); … … 561 526 var result = ExpectedRuntimeHelper.CalculateErt(kvp.Value, "QualityPerEvaluations", bkQuality * target, maximization); 562 527 var resultName = kvp.Key + "@" + ((target - 1) * 100) + "%"; 528 characteristics.Add(resultName); 563 529 IItem item; 564 530 if (instance.Results.TryGetValue(resultName, out item)) { … … 568 534 } 569 535 } 536 ProblemCharacteristics.Replace(characteristics.Select(x => new StringValue(x))); 570 537 try { 571 538 KnowledgeBase.UpdateOfRunsInProgress = true;
Note: See TracChangeset
for help on using the changeset viewer.