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

Location:
branches/PerformanceComparison/ProblemInstanceIdentifier
Files:
1 added
3 edited

Legend:

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

    r14678 r14690  
    1 using System.Linq;
     1using System;
     2using System.Collections.Generic;
     3using System.Linq;
    24using HeuristicLab.Analysis.FitnessLandscape;
     5using HeuristicLab.Common;
    36using HeuristicLab.Data;
     7using HeuristicLab.Encodings.PermutationEncoding;
    48using HeuristicLab.Problems.QuadraticAssignment;
    59
     
    913    public string Cls { get; private set; }
    1014    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; }
    1918   
    2019    private InstanceDescriptor() { }
    2120
    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]
    2844      };
    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);
    3049     
    3150      return new InstanceDescriptor() {
     
    3352        Cls = GetClass(qap.Name),
    3453        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()
    3956      };
    4057    }
    4158
    42     private static string GetClass(string name) {
     59    public static string GetClass(string name) {
    4360      var cls = name.Substring(0, 3);
    4461      var subCls = name.Last();
     
    5875
    5976    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();
    6578    }
    6679
     
    6982    }
    7083  }
     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  }
    71112}
  • branches/PerformanceComparison/ProblemInstanceIdentifier/ProblemInstanceIdentifier.csproj

    r14678 r14690  
    2323    <ErrorReport>prompt</ErrorReport>
    2424    <WarningLevel>4</WarningLevel>
     25    <Prefer32Bit>false</Prefer32Bit>
    2526  </PropertyGroup>
    2627  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     
    3233    <ErrorReport>prompt</ErrorReport>
    3334    <WarningLevel>4</WarningLevel>
     35    <Prefer32Bit>false</Prefer32Bit>
    3436  </PropertyGroup>
    3537  <ItemGroup>
     
    6769      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
    6870    </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>
    6978    <Reference Include="System" />
    7079    <Reference Include="System.Core" />
     
    7887  <ItemGroup>
    7988    <Compile Include="InstanceDescriptor.cs" />
     89    <Compile Include="InstanceExplorer.cs" />
    8090    <Compile Include="Program.cs" />
    8191    <Compile Include="Properties\AssemblyInfo.cs" />
     
    8595  </ItemGroup>
    8696  <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>
    87101    <ProjectReference Include="..\HeuristicLab.Analysis.FitnessLandscape\3.3\HeuristicLab.Analysis.FitnessLandscape-3.3.csproj">
    88102      <Project>{5fbdcd4a-3c2a-4ec6-83ce-34b29f43621a}</Project>
  • 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.