Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/20/17 20:41:33 (8 years ago)
Author:
abeham
Message:

#2457: working on identification of problem instances

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/ProblemInstanceIdentifier/Program.cs

    r14678 r14690  
    11using System;
    22using System.Collections.Generic;
     3using System.Globalization;
    34using System.Linq;
    45using System.Threading.Tasks;
     6using HeuristicLab.Algorithms.MemPR.Permutation;
    57using HeuristicLab.PluginInfrastructure;
    68using HeuristicLab.Problems.Instances;
     
    1012namespace ProblemInstanceIdentifier {
    1113  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    };
    1219    static void Main(string[] args) {
    1320      var sync = new object();
     
    1825        if (provider is TaillardQAPInstanceProvider) continue;
    1926        Parallel.ForEach(provider.GetDataDescriptors(), desc => {
    20           if (desc.Name == "esc16f") return;
     27          if (!selectedInstances.Contains(desc.Name)) return;
     28          //if (desc.Name == "esc16f") return;
    2129          var qapData = provider.LoadData(desc);
    22           if (qapData.Dimension < 15 || qapData.Dimension > 150) return;
     30          //if (qapData.Dimension < 15 || qapData.Dimension > 150) return;
    2331          var qap = new QuadraticAssignmentProblem();
    2432          qap.Load(qapData);
     
    2937      }
    3038
    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;
    37106            var kb = new List<InstanceDescriptor>();
    38107            var exp = new List<InstanceDescriptor>();
    39108            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]));
    42111            }
     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);
    43172            int exactCount = 0, clsCount = 0, missedCount = 0;
    44173            int exactRank = 0, clsRank = 0;
     
    54183            }
    55184            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);
    57187            }
    58188          }
Note: See TracChangeset for help on using the changeset viewer.