Changeset 17175 for branches/2457_ExpertSystem/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
- Timestamp:
- 07/26/19 15:13:36 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2457_ExpertSystem/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs
r16958 r17175 24 24 using System.Drawing; 25 25 using System.IO; 26 using System.IO.Compression; 26 27 using System.Linq; 27 28 using System.Threading; 28 29 using System.Threading.Tasks; 30 using HEAL.Attic; 29 31 using HeuristicLab.Algorithms.DataAnalysis; 30 32 using HeuristicLab.Analysis; … … 296 298 297 299 private static readonly HashSet<string> InterestingValueNames = new HashSet<string>() { 298 "QualityPerEvaluations", " Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization", "BestKnownQuality"300 "QualityPerEvaluations", "QualityPerClock", "Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization", "BestKnownQuality" 299 301 }; 300 302 … … 517 519 518 520 IItem bkParam; 519 if (!prob.Parameters.TryGetValue("BestKnownQuality", out bkParam) || !(bkParam is DoubleValue)) { 520 var list = kvp.Value.SelectMany(x => x.Value) 521 .Where(x => x.Results.ContainsKey("QualityPerEvaluations")) 522 .Select(x => ((IndexedDataTable<double>)x.Results["QualityPerEvaluations"]).Rows.First().Values.Last().Item2); 523 if (!list.Any()) continue; 524 bkParam = new DoubleValue(maximization ? list.Max() : list.Min()); 521 if (!prob.Parameters.TryGetValue("BestKnownQuality", out bkParam) || !(bkParam is DoubleValue) || double.IsNaN(((DoubleValue)bkParam).Value)) { 522 var best = double.NaN; 523 foreach (var x in kvp.Value.SelectMany(x => x.Value)) { 524 double? lastVal = null; 525 if (x.Results.TryGetValue("QualityPerEvaluations", out var item)) { 526 lastVal = ((IndexedDataTable<double>)item).Rows.FirstOrDefault()?.Values.LastOrDefault()?.Item2; 527 } 528 if (x.Results.TryGetValue("QualityPerClock", out item)) { 529 lastVal = ((IndexedDataTable<double>)item).Rows.FirstOrDefault()?.Values.LastOrDefault()?.Item2; 530 } 531 if (lastVal.HasValue && (double.IsNaN(best) 532 || maximization && best < lastVal.Value 533 || !maximization && best > lastVal.Value)) 534 best = lastVal.Value; 535 } 536 if (double.IsNaN(best)) continue; 537 bkParam = new DoubleValue(best); 525 538 prob.Parameters["BestKnownQuality"] = bkParam; 526 539 } … … 600 613 var total = hashSet.Count; 601 614 try { 602 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeuristicLab.OKB", "cache", "runs"); 603 Parallel.ForEach(Directory.EnumerateDirectories(path).Select((d, i) => new { Index = i, Directory = d }).GroupBy(x => x.Index / 100), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, 604 (folderGroup) => { 605 var localRunList = new List<Tuple<long, long, IRun>>(); 606 foreach (var runPath in folderGroup.Select(x => x.Directory)) { 607 long runId; 608 var runFolder = new DirectoryInfo(runPath).Name; 609 if (!long.TryParse(runFolder, out runId) || !hashSet.Contains(runId)) continue; 610 var runFilePath = Directory.EnumerateFiles(runPath).Single(); 611 var runFileName = Path.GetFileNameWithoutExtension(runFilePath); 612 long algId; 613 if (!long.TryParse(runFileName, out algId)) continue; 614 IRun run = null; 615 try { 616 using (var file = File.OpenRead(runFilePath)) 617 run = XmlParser.Deserialize<IRun>(file); 618 } catch { 619 File.Delete(runFilePath); 620 Directory.Delete(runPath); 621 } 622 if (run != null) localRunList.Add(Tuple.Create(algId, runId, run)); 623 } 624 lock (runList) { 625 foreach (var r in localRunList) { 626 hashSet.Remove(r.Item2); 627 algorithmId2RunMapping.Add(r.Item1, r.Item3); 628 runList.Add(r.Item3); 629 } 630 progress.Message = string.Format("Retrieved {0} of {1} from cache", runList.Count, total); 631 progress.ProgressValue = (double)runList.Count / total; 632 } 633 }); 615 var updateCount = 0; 616 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeuristicLab.OKB", "cache", "runsperalg"); 617 Parallel.ForEach(Directory.EnumerateFiles(path), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, 618 (algPath) => { 619 var serializer = new ProtoBufSerializer(); 620 var algId = long.Parse(Path.GetFileName(algPath)); 621 using (var stream = File.Open(algPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { 622 using (var archive = new ZipArchive(stream, ZipArchiveMode.Read)) { 623 foreach (var entry in archive.Entries) { 624 var runId = long.Parse(entry.Name); 625 var useEntry = false; 626 lock (hashSet) { 627 useEntry = hashSet.Remove(runId); 628 } 629 if (useEntry) { 630 //using (var df = new DeflateStream(entry.Open(), CompressionMode.Decompress)) { 631 var run = (Tuple<long, long, IRun>)serializer.Deserialize(entry.Open()); 632 if (run.Item1 != algId || run.Item2 != runId) { 633 lock (hashSet) hashSet.Add(runId); 634 continue; 635 } 636 lock (runList) { 637 algorithmId2RunMapping.Add(algId, run.Item3); 638 runList.Add(run.Item3); 639 updateCount++; 640 if (total < 100 || updateCount % (total / 100) == 0) { 641 progress.Message = string.Format("Retrieved {0} of {1} from cache", updateCount, total); 642 progress.ProgressValue = (double)runList.Count / total; 643 } 644 } 645 //} 646 } 647 } 648 } 649 } 650 }); 634 651 } catch { } 635 652 return hashSet.ToList(); … … 638 655 private void SaveToCache(IEnumerable<Tuple<long, long, IRun>> runs) { 639 656 try { 640 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeuristicLab.OKB", "cache", "runs ");657 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeuristicLab.OKB", "cache", "runsperalg"); 641 658 if (!Directory.Exists(path)) Directory.CreateDirectory(path); 642 foreach (var r in runs) { 643 var runPath = Path.Combine(path, r.Item2.ToString()); 644 if (!Directory.Exists(runPath)) Directory.CreateDirectory(runPath); 645 using (var file = File.Open(Path.Combine(runPath, r.Item1.ToString()), FileMode.Create, FileAccess.ReadWrite)) { 646 XmlGenerator.Serialize(r.Item3, file); 659 var serializer = new ProtoBufSerializer(); 660 foreach (var runsOfAlg in runs.GroupBy(x => x.Item1)) { 661 var runPath = Path.Combine(path, runsOfAlg.Key.ToString()); 662 using (var stream = File.Open(runPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { 663 using (var archive = new ZipArchive(stream, ZipArchiveMode.Update)) { 664 foreach (var run in runsOfAlg) { 665 var entry = archive.CreateEntry(run.Item2.ToString(), CompressionLevel.NoCompression); 666 using (var entrystream = entry.Open()) { 667 serializer.Serialize(run, entrystream, disposeStream: false); 668 } 669 } 670 } 647 671 } 648 672 } … … 764 788 var target = GetTarget(((DoubleValue)problemInstance.Parameters["BestKnownQuality"]).Value, MinimumTarget.Value, Maximization); 765 789 return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value) 766 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single())) 767 .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", target, Maximization).ExpectedRuntime); 790 .Select(x => { 791 IItem item = null; 792 if (x.Results.TryGetValue("QualityPerEvaluations", out item)) { 793 var idt = (IndexedDataTable<double>)item; 794 return Tuple.Create(x, idt.Rows.First().Values.AsEnumerable()); 795 } 796 if (x.Results.TryGetValue("QualityPerClock", out item)) { 797 var idt = (IndexedDataTable<double>)item; 798 return Tuple.Create(x, idt.Rows.First().Values.AsEnumerable()); 799 } 800 return null; 801 }) 802 .Where(x => x != null) 803 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x.Item1).Single())) 804 .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.Select(y => y.Item2), target, Maximization).ExpectedRuntime); 768 805 } 769 806 … … 772 809 var target = GetTarget(((DoubleValue)problemInstance.Parameters["BestKnownQuality"]).Value, MinimumTarget.Value, Maximization); 773 810 return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value) 774 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single())) 775 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", target, Maximization).ExpectedRuntime)); 811 .Select(x => { 812 IItem item = null; 813 if (x.Results.TryGetValue("QualityPerEvaluations", out item)) { 814 var idt = (IndexedDataTable<double>)item; 815 return Tuple.Create(x, idt.Rows.First().Values.AsEnumerable()); 816 } 817 if (x.Results.TryGetValue("QualityPerClock", out item)) { 818 var idt = (IndexedDataTable<double>)item; 819 return Tuple.Create(x, idt.Rows.First().Values.AsEnumerable()); 820 } 821 return null; 822 }) 823 .Where(x => x != null) 824 .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x.Item1).Single())) 825 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.Select(y => y.Item2), target, Maximization).ExpectedRuntime)); 776 826 } 777 827 … … 871 921 result[problemMap[pr.Key]] = new Dictionary<long, int>(); 872 922 873 var values = pr.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single()) 874 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerEvaluations", GetTarget(bkq, target, max), max).ExpectedRuntime)); 923 var values = pr.Select(x => { 924 IItem item = null; 925 if (x.Results.TryGetValue("QualityPerEvaluations", out item)) { 926 var idt = (IndexedDataTable<double>)item; 927 return Tuple.Create(x, idt.Rows.First().Values.AsEnumerable()); 928 } 929 if (x.Results.TryGetValue("QualityPerClock", out item)) { 930 var idt = (IndexedDataTable<double>)item; 931 return Tuple.Create(x, idt.Rows.First().Values.AsEnumerable()); 932 } 933 return null; 934 }) 935 .Where(x => x != null) 936 .GroupBy(x => algorithmId2RunMapping.GetBySecond(x.Item1).Single()) 937 .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.Select(y => y.Item2), GetTarget(bkq, target, max), max).ExpectedRuntime)); 875 938 var ranks = ClusteringHelper<long>.Cluster(nClasses, values, x => double.IsInfinity(x.Value)) 876 939 .GetByCluster().ToList();
Note: See TracChangeset
for help on using the changeset viewer.