Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/GQAPSolver/Program.cs @ 16036

Last change on this file since 16036 was 15904, checked in by abeham, 7 years ago

#1614: fix loading of problem instance

File size: 6.9 KB
Line 
1using System;
2using System.Linq;
3using CommandLine;
4using HeuristicLab.Data;
5using HeuristicLab.Problems.GeneralizedQuadraticAssignment;
6using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.Evolutionary;
7using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.GRASP;
8using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSearch;
9using HeuristicLab.Problems.Instances.CordeauGQAP;
10
11namespace GQAPSolver {
12  public enum AlgorithmType { OSGA = 1, ILS = 2, ES = 3, GRASP = 4 }
13
14  class CLOptions {
15    [Value(0, HelpText = "Configuration ID, set by irace.")]
16    public int ConfigId { get; set; }
17    [Value(1, HelpText = "Instance ID, set by irace.")]
18    public int InstanceId { get; set; }
19    [Value(2, HelpText = "Seed, set by irace.")]
20    public long Seed { get; set; }
21
22    [Value(3, Default = "20-15-35", HelpText = "Name of the problem instance.", Required = true)]
23    public string ProblemInstance { get; set; }
24
25    [Option('a', "alg", Default = AlgorithmType.OSGA, HelpText = "Either: OSGA, ILS, ES, GRASP")]
26    public AlgorithmType Algorithm { get; set; }
27
28    [Option('t', "tries", Default = 1,
29      HelpText = "Number of repetitions to perform.")]
30    public int Tries { get; set; }
31    [Option('r', "maxrt", Default = 60,
32      HelpText = "Maximum runtime in seconds.")]
33    public int MaxRuntime { get; set; }
34    [Option('i', "maxiter", Default = 0,
35      HelpText = "Maximum number of iterations.")]
36    public int MaxIterations { get; set; }
37    [Option('e', "maxeval", Default = 0,
38      HelpText = "Maximum number of evaluations.")]
39    public int MaxEvaluations { get; set; }
40
41    [Option("popsize", Default = 500, HelpText = "Population size (OSGA)")]
42    public int PopulationSize { get; set; }
43    [Option("mutprob", Default = 0.05, HelpText = "Mutation probability (OSGA)")]
44    public double MutationProbability { get; set; }
45
46    [Option("pertstr", Default = 0.5, HelpText = "Perturbation strength (ILS)")]
47    public double PerturbationStrength { get; set; }
48
49    [Option("mu", Default = 10, HelpText = "Parent population size (ES)")]
50    public int Mu { get; set; }
51    [Option("lambda", Default = 10, HelpText = "Offspring population size (ES)")]
52    public int Lambda { get; set; }
53    [Option("evosel", Default = ESSelection.Plus, HelpText = "Selection strategy + or , (ES)")]
54    public ESSelection Selection { get; set; }
55    [Option("recomb", Default = 0, HelpText = "Use recombination 0 or 1 (ES)")]
56    public int Recombination { get; set; }
57
58    [Option("eliteset", Default = 10, HelpText = "Size of the elite set (GRASP)")]
59    public int EliteSetSize { get; set; }
60    [Option("mineliteset", Default = 2, HelpText = "Minimum size of the elite set (GRASP)")]
61    public int MinimumEliteSetSize { get; set; }
62    [Option("lsiters", Default = 100, HelpText = "Maximum local search iterations (GRASP)")]
63    public int MaximumLocalSearchIterations { get; set; }
64    [Option("candidatesize", Default = 0.5, HelpText = "Candidate Size Factor (GRASP)")]
65    public double CandidateSizeFactor { get; set; }
66    [Option("maxcandsize", Default = 10, HelpText = "Maximum candidate list size (GRASP)")]
67    public int MaximumCandidateListSize { get; set; }
68    [Option("onemoveprob", Default = 0.5, HelpText = "Probability for performing a 1-move (GRASP)")]
69    public double OneMoveProbability { get; set; }
70    [Option("mindiff", Default = 4, HelpText = "Minimum difference (GRASP)")]
71    public int MinimumDifference { get; set; }
72  }
73
74  class Program {
75    static void Main(string[] args) {
76      var parsed = CommandLine.Parser.Default.ParseArguments<CLOptions>(args)
77        .WithParsed(x => Run(x));
78    }
79
80    private static void Run(CLOptions opts) {
81      var provider = new CordeauGQAPInstanceProvider();
82      var descriptors = provider.GetDataDescriptors().ToList();
83      var avgBest = 0.0;
84      for (var t = 0; t < opts.Tries; t++) {
85        HeuristicLab.Optimization.IAlgorithm alg = null;
86        switch (opts.Algorithm) {
87          case AlgorithmType.OSGA:
88            alg = new OSGA() {
89              MaximumEvaluations = opts.MaxEvaluations,
90              MaximumIterations = opts.MaxIterations,
91              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
92              PopulationSize = opts.PopulationSize,
93              MutationProbability = opts.MutationProbability,
94              StopAtBestKnownQuality = true,
95              Seed = (int)opts.Seed,
96              SetSeedRandomly = false
97            };
98            break;
99          case AlgorithmType.ILS:
100            alg = new IteratedLS() {
101              MaximumEvaluations = opts.MaxEvaluations,
102              MaximumIterations = opts.MaxIterations,
103              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
104              PerturbationStrength = opts.PerturbationStrength,
105              StopAtBestKnownQuality = true,
106              Seed = (int)opts.Seed,
107              SetSeedRandomly = false
108            };
109            break;
110          case AlgorithmType.ES:
111            alg = new EvolutionStrategy() {
112              MaximumEvaluations = opts.MaxEvaluations,
113              MaximumIterations = opts.MaxIterations,
114              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
115              Mu = opts.Mu,
116              Lambda = opts.Lambda,
117              Selection = opts.Selection,
118              UseRecombination = opts.Recombination > 0,
119              StopAtBestKnownQuality = true,
120              Seed = (int)opts.Seed,
121              SetSeedRandomly = false
122            };
123            break;
124          case AlgorithmType.GRASP:
125            alg = new GRASP() {
126              MaximumEvaluations = opts.MaxEvaluations,
127              MaximumIterations = opts.MaxIterations,
128              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
129              EliteSetSize = opts.EliteSetSize,
130              MinimumEliteSetSize = opts.MinimumEliteSetSize,
131              MaximumLocalSearchIterations = opts.MaximumLocalSearchIterations,
132              CandidateSizeFactor = opts.CandidateSizeFactor,
133              MaximumCandidateListSize = opts.MaximumCandidateListSize,
134              OneMoveProbability = opts.OneMoveProbability,
135              MinimumDifference = opts.MinimumDifference,
136              StopAtBestKnownQuality = true,
137              Seed = (int)opts.Seed,
138              SetSeedRandomly = false
139            };
140            break;
141        }
142        if (alg == null) throw new InvalidOperationException("Unknown algorithm.");
143        var prob = alg.Problem as GQAP;
144        if (prob == null) alg.Problem = prob = new GQAP();
145        prob.Load(provider.LoadData(descriptors.Single(x => x.Name == opts.ProblemInstance)));
146        alg.Start();
147        avgBest += ((DoubleValue)alg.Results["BestQuality"].Value).Value;
148      }
149      Console.WriteLine(avgBest / opts.Tries);
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.