Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17485 was 16076, checked in by abeham, 6 years ago

#1614: pending updates

File size: 13.0 KB
Line 
1using System;
2using System.IO;
3using System.Linq;
4using CommandLine;
5using HeuristicLab.Algorithms.ALPS;
6using HeuristicLab.Data;
7using HeuristicLab.Optimization;
8using HeuristicLab.Persistence.Default.Xml;
9using HeuristicLab.Problems.GeneralizedQuadraticAssignment;
10using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.CPLEX;
11using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.Evolutionary;
12using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.GRASP;
13using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LAHC;
14using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSearch;
15using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSolverNet;
16using HeuristicLab.Problems.Instances.CordeauGQAP;
17using HeuristicLab.Selection;
18
19namespace GQAPSolver {
20  public enum AlgorithmType { ALPS = 0, OSGA = 1, ES = 2, ILS = 3, MLS = 4, GRASP = 5, PLAHCS = 6, CPLEX_FY = 7, CPLEX_KB = 8, LOSOL_01 = 9, LOSOL_N = 10 }
21
22  class CLOptions {
23    [Value(0, HelpText = "Configuration ID, set by irace.")]
24    public int ConfigId { get; set; }
25    [Value(1, HelpText = "Instance ID, set by irace.")]
26    public int InstanceId { get; set; }
27    [Value(2, HelpText = "Seed, set by irace.")]
28    public long Seed { get; set; }
29
30    [Value(3, Default = "20-15-35", HelpText = "Name of the problem instance.", Required = true)]
31    public string ProblemInstance { get; set; }
32
33    [Option('a', "alg", Required = true, HelpText = "Algorithm: ALPS, OSGA, ES, ILS, MLS, GRASP, PLAHCS, CPLEX_FY, CPLEX_KB, LOSOL_01, LOSOL_N")]
34    public AlgorithmType Algorithm { get; set; }
35
36    [Option('t', "tries", Default = 1,
37      HelpText = "Number of repetitions to perform.")]
38    public int Tries { get; set; }
39    [Option('r', "maxrt", Default = 60,
40      HelpText = "Maximum runtime in seconds.")]
41    public int MaxRuntime { get; set; }
42    [Option('i', "maxiter", Default = 0,
43      HelpText = "Maximum number of iterations.")]
44    public int MaxIterations { get; set; }
45    [Option('e', "maxeval", Default = 0,
46      HelpText = "Maximum number of evaluations.")]
47    public int MaxEvaluations { get; set; }
48
49    [Option("agegap", Default = 20, HelpText = "Age gap (ALPS)")]
50    public int AgeGap { get; set; }
51    [Option("agescheme", Default = AgingScheme.Polynomial, HelpText = "Aging Scheme: Linear, Polynomial, Fibonacci, Exponentional (ALPS)")]
52    public AgingScheme AgingScheme { get; set; }
53    [Option("elites", Default = 1, HelpText = "Elite solutions (ALPS)")]
54    public int Elites { get; set; }
55    [Option("layers", Default = 10, HelpText = "Number of layers (ALPS)")]
56    public int NumberOfLayers { get; set; }
57    [Option("selpress", Default = 4, HelpText = "Selection pressure (ALPS)")]
58    public double SelectionPressure { get; set; }
59
60    [Option("popsize", Default = 500, HelpText = "Population size (OSGA, ALPS)")]
61    public int PopulationSize { get; set; }
62    [Option("mutprob", Default = 0.05, HelpText = "Mutation probability (OSGA, ALPS)")]
63    public double MutationProbability { get; set; }
64
65    [Option("mu", Default = 10, HelpText = "Parent population size (ES)")]
66    public int Mu { get; set; }
67    [Option("lambda", Default = 10, HelpText = "Offspring population size (ES)")]
68    public int Lambda { get; set; }
69    [Option("evosel", Default = ESSelection.Plus, HelpText = "Selection strategy + or , (ES, ALPS)")]
70    public ESSelection Selection { get; set; }
71    [Option("recomb", Default = 0, HelpText = "Use recombination 0 or 1 (ES)")]
72    public int Recombination { get; set; }
73
74    [Option("pertstr", Default = 0.5, HelpText = "Perturbation strength (ILS)")]
75    public double PerturbationStrength { get; set; }
76
77    [Option("eliteset", Default = 10, HelpText = "Size of the elite set (GRASP)")]
78    public int EliteSetSize { get; set; }
79    [Option("mineliteset", Default = 2, HelpText = "Minimum size of the elite set (GRASP)")]
80    public int MinimumEliteSetSize { get; set; }
81    [Option("lsiters", Default = 100, HelpText = "Maximum local search iterations (GRASP)")]
82    public int MaximumLocalSearchIterations { get; set; }
83    [Option("candidatesize", Default = 0.5, HelpText = "Candidate Size Factor (GRASP)")]
84    public double CandidateSizeFactor { get; set; }
85    [Option("maxcandsize", Default = 10, HelpText = "Maximum candidate list size (GRASP)")]
86    public int MaximumCandidateListSize { get; set; }
87    [Option("onemoveprob", Default = 0.5, HelpText = "Probability for performing a 1-move (GRASP)")]
88    public double OneMoveProbability { get; set; }
89    [Option("mindiff", Default = 4, HelpText = "Minimum difference (GRASP)")]
90    public int MinimumDifference { get; set; }
91  }
92
93  class Program {
94    static void Main(string[] args) {
95      var parsed = CommandLine.Parser.Default.ParseArguments<CLOptions>(args)
96        .WithParsed(x => Run(x));
97    }
98
99    private static void Run(CLOptions opts) {
100      var provider = new CordeauGQAPInstanceProvider();
101      var descriptors = provider.GetDataDescriptors().ToList();
102      var avgBest = 0.0;
103      var gqapInstance = provider.LoadData(descriptors.Single(x => x.Name == opts.ProblemInstance));
104
105      for (var t = 0; t < opts.Tries; t++) {
106        IAlgorithm alg = null;
107        switch (opts.Algorithm) {
108          case AlgorithmType.ALPS:
109            using (var stream = new MemoryStream(Properties.Resources.ALPS)) {
110              var alps = (AlpsGeneticAlgorithm)XmlParser.Deserialize(stream, CompressionType.Zip);
111              alps.Seed.Value = (int)opts.Seed;
112              alps.SetSeedRandomly.Value = true;
113              #region Termination
114              var execTimeTerm = alps.Terminators.Operators.OfType<ExecutionTimeTerminator>().SingleOrDefault();
115              if (execTimeTerm != null) {
116                alps.Terminators.Operators.SetItemCheckedState(execTimeTerm, opts.MaxRuntime > 0);
117                execTimeTerm.Threshold.Value = TimeSpan.FromSeconds(opts.MaxRuntime);
118              }
119              var compTerm = alps.Terminators.Operators.OfType<ComparisonTerminator<IntValue>>().SingleOrDefault(x => x.Name == "Generations");
120              if (compTerm != null) {
121                alps.Terminators.Operators.SetItemCheckedState(compTerm, opts.MaxIterations > 0);
122                compTerm.Threshold.Value = opts.MaxIterations;
123              }
124              compTerm = alps.Terminators.Operators.OfType<ComparisonTerminator<IntValue>>().SingleOrDefault(x => x.Name == "Evaluations");
125              if (compTerm != null) {
126                alps.Terminators.Operators.SetItemCheckedState(compTerm, opts.MaxEvaluations > 0);
127                compTerm.Threshold.Value = opts.MaxEvaluations;
128              }
129              var fitTerm = alps.Terminators.Operators.OfType<SingleObjectiveQualityTerminator>().SingleOrDefault();
130              if (fitTerm != null) {
131                alps.Terminators.Operators.SetItemCheckedState(fitTerm, gqapInstance.BestKnownQuality.HasValue);
132                fitTerm.Threshold.Value = gqapInstance.BestKnownQuality.GetValueOrDefault();
133              }
134              if (alps.Terminators.Operators.CheckedItems.Count() == 0) throw new InvalidOperationException("No termination criteria defined for ALPS");
135              #endregion
136              alps.AgeGap.Value = opts.AgeGap;
137              alps.AgingScheme.Value = opts.AgingScheme;
138              alps.Elites.Value = opts.Elites;
139              alps.NumberOfLayers.Value = opts.NumberOfLayers;
140              if (!(alps.Selector is GeneralizedRankSelector))
141                alps.Selector = alps.SelectorParameter.ValidValues.OfType<GeneralizedRankSelector>().Single();
142              var sel = alps.Selector as GeneralizedRankSelector;
143              sel.PressureParameter.Value.Value = opts.SelectionPressure;
144              alps.PopulationSize.Value = opts.PopulationSize;
145              alps.MutationProbability.Value = opts.MutationProbability;
146              alps.PlusSelection = opts.Selection == ESSelection.Plus;
147              alg = alps;
148            }
149            break;
150          case AlgorithmType.OSGA:
151            alg = new OSGA() {
152              MaximumEvaluations = opts.MaxEvaluations,
153              MaximumIterations = opts.MaxIterations,
154              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
155              PopulationSize = opts.PopulationSize,
156              MutationProbability = opts.MutationProbability,
157              StopAtBestKnownQuality = true,
158              Seed = (int)opts.Seed,
159              SetSeedRandomly = false
160            };
161            break;
162          case AlgorithmType.ES:
163            alg = new EvolutionStrategy() {
164              MaximumEvaluations = opts.MaxEvaluations,
165              MaximumIterations = opts.MaxIterations,
166              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
167              Mu = opts.Mu,
168              Lambda = opts.Lambda,
169              Selection = opts.Selection,
170              UseRecombination = opts.Recombination > 0,
171              StopAtBestKnownQuality = true,
172              Seed = (int)opts.Seed,
173              SetSeedRandomly = false
174            };
175            break;
176          case AlgorithmType.ILS:
177            alg = new IteratedLS() {
178              MaximumEvaluations = opts.MaxEvaluations,
179              MaximumIterations = opts.MaxIterations,
180              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
181              PerturbationStrength = opts.PerturbationStrength,
182              StopAtBestKnownQuality = true,
183              Seed = (int)opts.Seed,
184              SetSeedRandomly = false
185            };
186            break;
187          case AlgorithmType.MLS:
188            alg = new MultistartLS() {
189              MaximumEvaluations = opts.MaxEvaluations,
190              MaximumIterations = opts.MaxIterations,
191              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
192              StopAtBestKnownQuality = true,
193              Seed = (int)opts.Seed,
194              SetSeedRandomly = false
195            };
196            break;
197          case AlgorithmType.GRASP:
198            alg = new GRASP() {
199              MaximumEvaluations = opts.MaxEvaluations,
200              MaximumIterations = opts.MaxIterations,
201              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
202              EliteSetSize = opts.EliteSetSize,
203              MinimumEliteSetSize = opts.MinimumEliteSetSize,
204              MaximumLocalSearchIterations = opts.MaximumLocalSearchIterations,
205              CandidateSizeFactor = opts.CandidateSizeFactor,
206              MaximumCandidateListSize = opts.MaximumCandidateListSize,
207              OneMoveProbability = opts.OneMoveProbability,
208              MinimumDifference = opts.MinimumDifference,
209              StopAtBestKnownQuality = true,
210              Seed = (int)opts.Seed,
211              SetSeedRandomly = false
212            };
213            break;
214          case AlgorithmType.PLAHCS:
215            alg = new PLAHCS() {
216              MaximumEvaluations = opts.MaxEvaluations,
217              MaximumIterations = opts.MaxIterations,
218              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
219              MaximumExponent = 20,
220              StopAtBestKnownQuality = true,
221              Seed = (int)opts.Seed,
222              SetSeedRandomly = false
223            };
224            break;
225          case AlgorithmType.CPLEX_FY:
226            alg = new GQAP_FY() {
227              MaximumEvaluations = opts.MaxEvaluations,
228              MaximumIterations = opts.MaxIterations,
229              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
230              StopAtBestKnownQuality = true
231            };
232            break;
233          case AlgorithmType.CPLEX_KB:
234            alg = new GQAP_KB() {
235              MaximumEvaluations = opts.MaxEvaluations,
236              MaximumIterations = opts.MaxIterations,
237              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
238              StopAtBestKnownQuality = true
239            };
240            break;
241          case AlgorithmType.LOSOL_01:
242            alg = new GQAPBinarySolver() {
243              MaximumEvaluations = opts.MaxEvaluations,
244              MaximumIterations = opts.MaxIterations,
245              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
246              StopAtBestKnownQuality = true
247            };
248            break;
249          case AlgorithmType.LOSOL_N:
250            alg = new GQAPIntegerSolver() {
251              MaximumEvaluations = opts.MaxEvaluations,
252              MaximumIterations = opts.MaxIterations,
253              MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime),
254              StopAtBestKnownQuality = true
255            };
256            break;
257        }
258        if (alg == null) throw new InvalidOperationException("Unknown algorithm " + opts.Algorithm + ".");
259        var prob = alg.Problem as GQAP;
260        if (prob == null) alg.Problem = prob = new GQAP();
261        prob.Load(gqapInstance);
262        alg.Start();
263        avgBest += ((DoubleValue)alg.Results["BestQuality"].Value).Value;
264      }
265      Console.WriteLine(avgBest / opts.Tries);
266    }
267  }
268}
Note: See TracBrowser for help on using the repository browser.