Changeset 13759 for branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
- Timestamp:
- 04/14/16 01:20:10 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
r13757 r13759 131 131 public KnowledgeCenter() { 132 132 maximumEvaluations = new IntValue(10000); 133 minimumTarget = new DoubleValue( 1.05);133 minimumTarget = new DoubleValue(0.05); 134 134 instanceRuns = new RunCollection(); 135 135 seededRuns = new RunCollection(); … … 140 140 problemCharacteristics = new CheckedItemList<StringValue>(); 141 141 problemInstanceProximity = new EnumValue<ProblemInstanceProximityType>(ProblemInstanceProximityType.FeatureSpace); 142 problemInstanceNeighborhoodFactor = new DoubleValue( 1);142 problemInstanceNeighborhoodFactor = new DoubleValue(5); 143 143 readonlyProblemCharacteristics = problemCharacteristics.AsReadOnly(); 144 144 problem = new SingleObjectiveOKBProblem(); … … 455 455 progress.ProgressValue = 0; 456 456 // FIXME: How to tell if refresh is necessary? 457 queryClient.Refresh();458 progress.ProgressValue = 0.5;459 progress.Status = "Downloading algorithm and problem instance information...";460 // FIXME: How to tell if refresh is necessary?461 adminClient.Refresh();457 var refreshTasks = new[] { 458 Task.Factory.StartNew(() => queryClient.Refresh()), 459 Task.Factory.StartNew(() => adminClient.Refresh()) 460 }; 461 Task.WaitAll(refreshTasks); 462 462 463 463 var probInstance = adminClient.Problems.SingleOrDefault(x => x.Id == Problem.ProblemId); … … 476 476 var characteristics = new HashSet<string>(); 477 477 var totalProblems = adminClient.Problems.Count(x => x.ProblemClassId == probClassId); 478 Parallel.ForEach(adminClient.Problems.Where(x => x.ProblemClassId == probClassId), (pInst) => {478 Parallel.ForEach(adminClient.Problems.Where(x => x.ProblemClassId == probClassId), new ParallelOptions { MaxDegreeOfParallelism = 3 }, (pInst) => { 479 479 var charas = new List<string>(); 480 480 IRun probRun = null; … … 512 512 progress.ProgressValue = 0; 513 513 p[0] = 0; 514 Parallel.ForEach(adminClient.Algorithms, (algInst) => {514 Parallel.ForEach(adminClient.Algorithms, new ParallelOptions { MaxDegreeOfParallelism = 3 }, (algInst) => { 515 515 IAlgorithm alg = null; 516 516 var data = Clients.OKB.Administration.AdministrationClient.GetAlgorithmData(algInst.Id); … … 544 544 var runIds = queryClient.GetRunIds(problemClassFilter).ToList(); 545 545 var batches = runIds.Select((v, i) => new { Idx = i, Val = v }).GroupBy(x => x.Idx / 500, x => x.Val); 546 Parallel.ForEach(batches.Select(x => x.ToList()), (batch) => {546 Parallel.ForEach(batches.Select(x => x.ToList()), new ParallelOptions { MaxDegreeOfParallelism = 3 }, (batch) => { 547 547 var okbRuns = queryClient.GetRunsWithValues(batch, true, interestingValues); 548 548 var hlRuns = okbRuns.AsParallel().Select(x => new { AlgorithmId = x.Algorithm.Id, Run = queryClient.ConvertToOptimizationRun(x) }).ToList(); … … 630 630 if (Problem == null) return; 631 631 var piDistances = GetProblemDistances(); 632 var maxDistance = piDistances.Max(); 633 // Weighted combination of algorithm performances using distance-based exponentially falling weights 634 // Scale distances by maxDistance 635 // Parameter to influence dampening factor 636 // Algorithm performances are given in expected run time 637 // Using convergence graphs up to maximum evaluations effort 638 // Care has to be taken if not every algorithm has been executed on every problem instance 632 var maxDist = piDistances.Max(x => x.Value); 639 633 var instances = new SortedList<double, IAlgorithm>(); 640 634 foreach (var relevantRuns in knowledgeBase.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single())) { 641 635 var algorithm = algorithmId2AlgorithmInstanceMapping.GetByFirst(relevantRuns.Key); 642 var avgQuality = 0.0; 643 var counter = 0; 636 Func<double, double> distFunc = (d) => Math.Exp(ProblemInstanceNeighborhoodFactor.Value * (-d / maxDist)); 637 var pis = relevantRuns.Select(x => ((StringValue)x.Parameters["Problem Name"]).Value).Distinct() 638 .Select(x => Tuple.Create(x, ProblemInstances.SingleOrDefault(y => ((StringValue)y.Parameters["Problem Name"]).Value == x))) 639 .Where(x => x.Item2 != null) 640 .Select(x => Tuple.Create(x.Item1, distFunc(piDistances[x.Item2]), ((DoubleValue)x.Item2.Parameters["BestKnownQuality"]).Value)) 641 .ToDictionary(x => x.Item1, x => Tuple.Create(x.Item2, x.Item3)); 642 var sumPis = pis.Sum(x => x.Value.Item1); 643 var avgERT = 0.0; 644 644 foreach (var problemRuns in relevantRuns.GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value)) { 645 var probInstance = ProblemInstances.SingleOrDefault(x => x.Name == problemRuns.Key);646 if ( probInstance == null) continue;647 var bkQuality = ((DoubleValue)probInstance.Parameters["BestKnownQuality"]).Value;645 Tuple<double, double> info; 646 if (!pis.TryGetValue(problemRuns.Key, out info)) continue; 647 var convGraph = new List<List<Tuple<double, double>>>(); 648 648 foreach (var run in problemRuns) { 649 var current = new List<Tuple<double, double>>(); 649 650 var performanceGraph = ((IndexedDataTable<double>)run.Results["QualityPerEvaluations"]); 650 try { 651 avgQuality += performanceGraph.Rows.First().Values.TakeWhile(x => x.Item1 < MaximumEvaluations.Value).Last().Item2 / bkQuality; 652 counter++; 653 } catch { 654 continue; 655 } 656 } 657 } 658 avgQuality /= counter; 659 instances.Add(avgQuality, algorithm); 651 current.AddRange(performanceGraph.Rows.First().Values.TakeWhile(v => v.Item1 < MaximumEvaluations.Value)); 652 if (current.Count > 0) { 653 current.Add(Tuple.Create((double)MaximumEvaluations.Value, current.Last().Item2)); 654 convGraph.Add(current); 655 } 656 } 657 var ert = ExpectedRuntimeHelper.CalculateErt(convGraph, (Maximization ? (1 - MinimumTarget.Value) : (1 + MinimumTarget.Value)) * info.Item2, Maximization).ExpectedRuntime; 658 if (double.IsNaN(ert)) { 659 ert = ExpectedRuntimeHelper.CalculateErt(problemRuns.ToList(), "QualityPerEvaluations", (Maximization ? (1 - MinimumTarget.Value) : (1 + MinimumTarget.Value)) * info.Item2, Maximization).ExpectedRuntime; 660 if (double.IsNaN(ert)) ert = int.MaxValue; 661 } 662 avgERT += info.Item1 * ert; 663 } 664 avgERT /= sumPis; 665 if (instances.ContainsKey(avgERT)) { 666 avgERT += new System.Random().NextDouble(); 667 } 668 instances.Add(avgERT, algorithm); 660 669 } 661 670 662 671 var instanceLadder = instances.Select(x => (IAlgorithm)x.Value.Clone()).ToList(); 663 if (Maximization) instanceLadder.Reverse();664 672 suggestedInstances.Replace(instanceLadder); 665 673 } 666 674 667 private DoubleMatrix GetProblemDistances() { 668 var matrix = new DoubleMatrix(ProblemInstances.Count, ProblemInstances.Count); 675 private Dictionary<IRun, double> GetProblemDistances() { 676 var result = new Dictionary<IRun, double>(); 677 var currentInstance = problemId2ProblemInstanceMapping.GetByFirst(Problem.ProblemId); 669 678 switch (ProblemInstanceProximity.Value) { 670 679 case ProblemInstanceProximityType.MDS: 671 680 case ProblemInstanceProximityType.PCA: 672 681 case ProblemInstanceProximityType.SOM: 673 int i = 0, j = 0; 674 foreach (var a in ProblemInstances) { 675 double xa, ya; 676 GetProjectionCoordinates(a, out xa, out ya); 677 j = 0; 678 foreach (var b in ProblemInstances) { 679 double xb, yb; 680 GetProjectionCoordinates(b, out xb, out yb); 681 matrix[i, j] = Math.Sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb)); 682 j++; 683 } 684 i++; 682 double xa, ya; 683 GetProjectionCoordinates(currentInstance, out xa, out ya); 684 foreach (var b in ProblemInstances) { 685 double xb, yb; 686 GetProjectionCoordinates(b, out xb, out yb); 687 var d = Math.Sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb)); 688 result[b] = d; 685 689 } 686 690 break; 687 691 case ProblemInstanceProximityType.FeatureSpace: 688 int k = 0, l = 0;689 692 var features = GetProblemCharacteristics(); 690 foreach (var a in ProblemInstances) { 691 l = 0; 692 var fa = features[a]; 693 foreach (var b in ProblemInstances) { 694 var sum = features[b].Select((t, f) => (fa[f] - t) * (fa[f] - t)).Sum(); 695 matrix[k, l] = Math.Sqrt(sum); 696 l++; 697 } 698 k++; 693 var cF = features[currentInstance]; 694 foreach (var b in ProblemInstances) { 695 var sum = features[b].Select((t, f) => (cF[f] - t) * (cF[f] - t)).Sum(); 696 result[b] = Math.Sqrt(sum); 699 697 } 700 698 break; 701 699 default: throw new InvalidOperationException(string.Format("Unkonwn proximity type {0}", ProblemInstanceProximity.Value)); 702 700 } 703 return matrix;701 return result; 704 702 } 705 703 … … 707 705 x = ((DoubleValue)problemInstance.Results["Projection." + ProblemInstanceProximity.Value + ".X"]).Value; 708 706 y = ((DoubleValue)problemInstance.Results["Projection." + ProblemInstanceProximity.Value + ".Y"]).Value; 707 if (ProblemInstanceProximity.Value == ProblemInstanceProximityType.SOM) { 708 x = Math.Floor(x); 709 y = Math.Floor(y); 710 } 709 711 } 710 712
Note: See TracChangeset
for help on using the changeset viewer.