- Timestamp:
- 02/20/17 20:41:33 (8 years ago)
- Location:
- branches/PerformanceComparison/ProblemInstanceIdentifier
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/ProblemInstanceIdentifier/InstanceDescriptor.cs
r14678 r14690 1 using System.Linq; 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 2 4 using HeuristicLab.Analysis.FitnessLandscape; 5 using HeuristicLab.Common; 3 6 using HeuristicLab.Data; 7 using HeuristicLab.Encodings.PermutationEncoding; 4 8 using HeuristicLab.Problems.QuadraticAssignment; 5 9 … … 9 13 public string Cls { get; private set; } 10 14 public int Dimension { get; set; } 11 public double Sharpness { get; set; } 12 public double SharpnessSdev { get; set; } 13 public double Bumpiness { get; set; } 14 public double BumpinessSdev { get; set; } 15 public double Flatness { get; set; } 16 public double FlatnessSdev { get; set; } 17 public double Steadiness { get; set; } 18 public double SteadinessSdev { get; set; } 15 16 public string[] FeatureNames { get; set; } 17 public double[] FeatureValues { get; set; } 19 18 20 19 private InstanceDescriptor() { } 21 20 22 public static InstanceDescriptor FromPathRelinkingAnalysis(QuadraticAssignmentProblem qap, int paths, int? seed) { 23 var walk = new QAPDirectedWalk { 24 Problem = qap, 25 BestImprovement = true, 26 Paths = paths, 27 Seed = seed 21 public InstanceDescriptor(string name, string cls, int dimension, string[] names, double[] values) { 22 Name = name; 23 Cls = cls; 24 Dimension = dimension; 25 FeatureNames = names; 26 FeatureValues = values; 27 } 28 29 public InstanceDescriptor(InstanceDescriptor other) { 30 Name = other.Name; 31 Cls = other.Cls; 32 Dimension = other.Dimension; 33 FeatureNames = (string[])other.FeatureNames.Clone(); 34 FeatureValues = (double[]) other.FeatureValues.Clone(); 35 } 36 37 public static InstanceDescriptor FromProblemOnly(QuadraticAssignmentProblem qap) { 38 return new InstanceDescriptor() { 39 Name = qap.Name, 40 Cls = GetClass(qap.Name), 41 Dimension = qap.Weights.Rows, 42 FeatureNames = new string[0], 43 FeatureValues = new double[0] 28 44 }; 29 var features = walk.Calculate().ToDictionary(x => x.Name, x => x.Value); 45 } 46 47 public static InstanceDescriptor FromPaths(QuadraticAssignmentProblem qap, List<List<Tuple<Permutation, double>>> trajectories) { 48 var features = QAPDirectedWalk.Calculate(trajectories).ToDictionary(x => x.Name, x => x.Value); 30 49 31 50 return new InstanceDescriptor() { … … 33 52 Cls = GetClass(qap.Name), 34 53 Dimension = qap.Weights.Rows, 35 Sharpness = ((DoubleValue)features["Swap2.Sharpness"]).Value, 36 Bumpiness = ((DoubleValue)features["Swap2.Bumpiness"]).Value, 37 Flatness = ((DoubleValue)features["Swap2.Flatness"]).Value, 38 Steadiness = ((DoubleValue)features["Swap2.Steadiness"]).Value, 54 FeatureNames = features.Keys.ToArray(), 55 FeatureValues = features.Values.Select(x => ((DoubleValue)x).Value).ToArray() 39 56 }; 40 57 } 41 58 42 p rivatestatic string GetClass(string name) {59 public static string GetClass(string name) { 43 60 var cls = name.Substring(0, 3); 44 61 var subCls = name.Last(); … … 58 75 59 76 public double CalculateSimilarity(InstanceDescriptor other) { 60 var sa = (Sharpness - other.Sharpness); 61 var bu = (Bumpiness - other.Bumpiness); 62 var fl = (Flatness - other.Flatness); 63 var se = (Steadiness - other.Steadiness); 64 return sa * sa + bu * bu + fl * fl + se * se; 77 return FeatureValues.Select((v, i) => (v - other.FeatureValues[i]) * (v - other.FeatureValues[i])).Sum(); 65 78 } 66 79 … … 69 82 } 70 83 } 84 85 public class InstancesStandardizer { 86 private double[] featureMeans; 87 private double[] featureStdevs; 88 89 private InstancesStandardizer() { } 90 91 public static InstancesStandardizer Create(IList<InstanceDescriptor> instances) { 92 var standardizer = new InstancesStandardizer(); 93 var featureLength = instances.First().FeatureValues.Length; 94 standardizer.featureMeans = 95 Enumerable.Range(0, featureLength) 96 .Select(x => instances.Select(y => y.FeatureValues[x]).Average()).ToArray(); 97 standardizer.featureStdevs = 98 Enumerable.Range(0, featureLength) 99 .Select(x => instances.Select(y => y.FeatureValues[x]).StandardDeviation()).ToArray(); 100 101 return standardizer; 102 } 103 104 public void Apply(IList<InstanceDescriptor> instances) { 105 for (var i = 0; i < instances.Count; i++) { 106 var inst = instances[i]; 107 for (var x = 0; x < featureMeans.Length; x++) 108 inst.FeatureValues[x] = (inst.FeatureValues[x] - featureMeans[x]) / featureStdevs[x]; 109 } 110 } 111 } 71 112 } -
branches/PerformanceComparison/ProblemInstanceIdentifier/ProblemInstanceIdentifier.csproj
r14678 r14690 23 23 <ErrorReport>prompt</ErrorReport> 24 24 <WarningLevel>4</WarningLevel> 25 <Prefer32Bit>false</Prefer32Bit> 25 26 </PropertyGroup> 26 27 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> … … 32 33 <ErrorReport>prompt</ErrorReport> 33 34 <WarningLevel>4</WarningLevel> 35 <Prefer32Bit>false</Prefer32Bit> 34 36 </PropertyGroup> 35 37 <ItemGroup> … … 67 69 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath> 68 70 </Reference> 71 <Reference Include="HeuristicLab.Selection-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 72 <SpecificVersion>False</SpecificVersion> 73 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Selection-3.3.dll</HintPath> 74 </Reference> 75 <Reference Include="HeuristicLab.SequentialEngine-3.3"> 76 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.SequentialEngine-3.3.dll</HintPath> 77 </Reference> 69 78 <Reference Include="System" /> 70 79 <Reference Include="System.Core" /> … … 78 87 <ItemGroup> 79 88 <Compile Include="InstanceDescriptor.cs" /> 89 <Compile Include="InstanceExplorer.cs" /> 80 90 <Compile Include="Program.cs" /> 81 91 <Compile Include="Properties\AssemblyInfo.cs" /> … … 85 95 </ItemGroup> 86 96 <ItemGroup> 97 <ProjectReference Include="..\HeuristicLab.Algorithms.MemPR\3.3\HeuristicLab.Algorithms.MemPR-3.3.csproj"> 98 <Project>{9d274421-6332-4fbc-aae4-467ace27c368}</Project> 99 <Name>HeuristicLab.Algorithms.MemPR-3.3</Name> 100 </ProjectReference> 87 101 <ProjectReference Include="..\HeuristicLab.Analysis.FitnessLandscape\3.3\HeuristicLab.Analysis.FitnessLandscape-3.3.csproj"> 88 102 <Project>{5fbdcd4a-3c2a-4ec6-83ce-34b29f43621a}</Project> -
branches/PerformanceComparison/ProblemInstanceIdentifier/Program.cs
r14678 r14690 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Globalization; 3 4 using System.Linq; 4 5 using System.Threading.Tasks; 6 using HeuristicLab.Algorithms.MemPR.Permutation; 5 7 using HeuristicLab.PluginInfrastructure; 6 8 using HeuristicLab.Problems.Instances; … … 10 12 namespace ProblemInstanceIdentifier { 11 13 class Program { 14 private static HashSet<string> selectedInstances = new HashSet<string>() { 15 "bur26a", "chr25a", "dre24", "els19", "esc32a", "had20", "kra32", "lipa30a", "lipa30b", 16 "nug30", "rou20", "scr20", "sko42", "ste36a", "tai25a", "tai25b", "tho30", "wil50", 17 "RAND-S-6x6-36-25-AFFY-00_rand_rand_bl", "RAND-S-6x6-36-25-AFFY-00_rand_rand_ci" 18 }; 12 19 static void Main(string[] args) { 13 20 var sync = new object(); … … 18 25 if (provider is TaillardQAPInstanceProvider) continue; 19 26 Parallel.ForEach(provider.GetDataDescriptors(), desc => { 20 if (desc.Name == "esc16f") return; 27 if (!selectedInstances.Contains(desc.Name)) return; 28 //if (desc.Name == "esc16f") return; 21 29 var qapData = provider.LoadData(desc); 22 if (qapData.Dimension < 15 || qapData.Dimension > 150) return;30 //if (qapData.Dimension < 15 || qapData.Dimension > 150) return; 23 31 var qap = new QuadraticAssignmentProblem(); 24 32 qap.Load(qapData); … … 29 37 } 30 38 31 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6:F2}\t{7:F2}", "Repetition", "KB-Paths", "Exp-Paths", "Cls-Hits", "Exact-Hits", "No-Hits", "Cls-Rank", "Exact-Rank"); 32 var paths = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50 }; 33 for (var r = 0; r < 20; r++) { 34 Parallel.ForEach(paths, kbPaths => { 35 foreach (var expPaths in paths) { 36 if (expPaths > kbPaths) continue; 39 /*var classes = instances.Select(InstanceDescriptor.FromProblemOnly).GroupBy(x => x.Cls); 40 foreach (var cls in classes.OrderBy(x => x.Key)) { 41 Console.WriteLine("{0};{1}", cls.Key, cls.Count()); 42 }*/ 43 44 //var kb = GenerateKnowledgeBase(instances, 200, localOptima: false); 45 46 47 //var paths = new[] { 1, 2, 3, 5, 10, 20, 30, 50, 100, 200 }; 48 //var kbExplorers = paths.Select(x => new PathRelinkingExplorer() { Paths = x }).ToArray(); 49 //var expExplorers = paths.Select(x => new PathRelinkingExplorer() { Paths = x }).ToArray(); 50 //ExploreSelfIdentification(instances, kbExplorers, expExplorers); 51 52 var iterations = new[] { 100, 1000, 10000, 100000 }; 53 var kbExplorers = iterations.Select(x => new RandomWalkExplorer() { Iterations = x }).ToArray(); 54 var expExplorers = iterations.Select(x => new RandomWalkExplorer() { Iterations = x }).ToArray(); 55 ExploreSelfIdentification(instances, kbExplorers, expExplorers); 56 57 //ExploreMemPrIdentification(instances); 58 } 59 60 private static List<InstanceDescriptor> GenerateKnowledgeBase(List<QuadraticAssignmentProblem> instances, InstanceExplorer explorer) { 61 var headerPrinted = false; 62 var sync = new object(); 63 var kb = new List<InstanceDescriptor>(); 64 Parallel.ForEach(instances.OrderBy(x => x.Weights.Rows), qap => { 65 var instance = explorer.Explore(qap); 66 lock (sync) { 67 if (!headerPrinted) { 68 headerPrinted = true; 69 Console.WriteLine(string.Join(";", 70 new [] { "Name", "Cls", "Dimension" } 71 .Concat(instance.FeatureNames))); 72 } 73 PrintInstanceLine(instance); 74 kb.Add(instance); 75 } 76 }); 77 var standardizer = InstancesStandardizer.Create(kb); 78 var normalizedKb = kb.Select(x => new InstanceDescriptor(x)).ToList(); 79 standardizer.Apply(normalizedKb); 80 Console.WriteLine(); 81 foreach (var instance in kb) { 82 PrintInstanceLine(instance); 83 } 84 //return normalizedKb; 85 return kb; 86 } 87 88 private static void PrintInstanceLine(InstanceDescriptor instance) { 89 Console.WriteLine(string.Join(";", 90 new[] {instance.Name, instance.Cls, instance.Dimension.ToString(CultureInfo.CurrentCulture)} 91 .Concat(instance.FeatureValues.Select(x => x.ToString(CultureInfo.CurrentCulture))))); 92 } 93 94 private static void ExploreSelfIdentification(List<QuadraticAssignmentProblem> instances, InstanceExplorer[] kbExplorer, InstanceExplorer[] expExporer) { 95 var sync = new object(); 96 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", "Repetition", "KB-Effort", "Exp-Effort", 97 "Exact-Hits", "No-Hits", "Exact-Rank"); 98 var kbSeeds = Enumerable.Range(0, 20).ToList(); 99 var expSeeds = Enumerable.Range(20, 20).ToList(); 100 for (var r = 0; r < 10; r++) { 101 var rlokal = r; 102 Parallel.ForEach(kbExplorer, kbPaths => { 103 //foreach (var kbPaths in kbExplorer) { 104 foreach (var expPaths in expExporer) { 105 //if (expPaths > kbPaths) continue; 37 106 var kb = new List<InstanceDescriptor>(); 38 107 var exp = new List<InstanceDescriptor>(); 39 108 foreach (var qap in instances) { 40 kb.Add( InstanceDescriptor.FromPathRelinkingAnalysis(qap, kbPaths, null));41 exp.Add( InstanceDescriptor.FromPathRelinkingAnalysis(qap, expPaths, null));109 kb.Add(kbPaths.Explore(qap, kbSeeds[rlokal])); 110 exp.Add(expPaths.Explore(qap, expSeeds[rlokal])); 42 111 } 112 var standardizer = InstancesStandardizer.Create(kb); 113 standardizer.Apply(kb); 114 standardizer.Apply(exp); 115 int exactCount = 0, clsCount = 0, missedCount = 0; 116 int exactRank = 0, clsRank = 0; 117 foreach (var e in exp) { 118 var ordered = kb.OrderBy(x => x.CalculateSimilarity(e)).ToList(); 119 var bestMatch = ordered.First(); 120 if (bestMatch.Cls == e.Cls) { 121 clsCount++; 122 if (bestMatch.Name == e.Name) exactCount++; 123 } 124 else missedCount++; 125 clsRank += ordered.FindIndex((id) => id.Cls == e.Cls) + 1; 126 exactRank += ordered.FindIndex((id) => id.Name == e.Name) + 1; 127 } 128 lock (sync) { 129 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5:F2}", rlokal, kbPaths.Effort, expPaths.Effort, exactCount, 130 missedCount, exactRank / (double) exp.Count); 131 } 132 } 133 }); 134 //} 135 } 136 } 137 138 private static void ExploreMemPrIdentification(List<QuadraticAssignmentProblem> instances) { 139 var sync = new object(); 140 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", "Repetition", "KB-Runtime", "Exp-Runtime", 141 "Exact-Hits", "No-Hits", "Average-Rank"); 142 var paths = new[] { 10 }; 143 var repetitions = 10; 144 var kbSeeds = Enumerable.Range(0, repetitions).ToList(); 145 var expSeeds = Enumerable.Range(repetitions, repetitions).ToList(); 146 for (var r = 0; r < repetitions; r++) { 147 var rlokal = r; 148 Parallel.ForEach(paths, kbPaths => { 149 var memPr = new PermutationMemPR(); 150 foreach (var expPaths in paths) { 151 //if (expPaths > kbPaths) continue; 152 var kb = new List<InstanceDescriptor>(); 153 var exp = new List<InstanceDescriptor>(); 154 foreach (var qap in instances.Select(x => (QuadraticAssignmentProblem)x.Clone())) { 155 memPr.Problem = qap; 156 memPr.Prepare(true); 157 memPr.Seed = kbSeeds[rlokal]; 158 memPr.MaximumExecutionTime = TimeSpan.FromSeconds(kbPaths); 159 memPr.StartSync(); 160 if (memPr.Context.RelinkedPaths.IsEmpty) continue; 161 kb.Add(InstanceDescriptor.FromPaths(qap, memPr.Context.RelinkedPaths.Paths.ToList())); 162 memPr.Prepare(true); 163 memPr.Seed = expSeeds[rlokal]; 164 memPr.MaximumExecutionTime = TimeSpan.FromSeconds(expPaths); 165 memPr.StartSync(); 166 if (memPr.Context.RelinkedPaths.IsEmpty) continue; 167 exp.Add(InstanceDescriptor.FromPaths(qap, memPr.Context.RelinkedPaths.Paths.ToList())); 168 } 169 var standardizer = InstancesStandardizer.Create(kb); 170 standardizer.Apply(kb); 171 standardizer.Apply(exp); 43 172 int exactCount = 0, clsCount = 0, missedCount = 0; 44 173 int exactRank = 0, clsRank = 0; … … 54 183 } 55 184 lock (sync) { 56 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6:F2}\t{7:F2}", r, kbPaths, expPaths, clsCount, exactCount, missedCount, clsRank / (double)exp.Count, exactRank / (double)exp.Count); 185 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5:F2}", rlokal, kbPaths, expPaths, exactCount, 186 missedCount, exactRank / (double)exp.Count); 57 187 } 58 188 }
Note: See TracChangeset
for help on using the changeset viewer.