using System; using System.Linq; using CommandLine; using HeuristicLab.Data; using HeuristicLab.Problems.GeneralizedQuadraticAssignment; using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.Evolutionary; using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.GRASP; using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSearch; using HeuristicLab.Problems.Instances.CordeauGQAP; namespace GQAPSolver { public enum AlgorithmType { OSGA = 1, ILS = 2, ES = 3, GRASP = 4 } class CLOptions { [Value(0, HelpText = "Configuration ID, set by irace.")] public int ConfigId { get; set; } [Value(1, HelpText = "Instance ID, set by irace.")] public int InstanceId { get; set; } [Value(2, HelpText = "Seed, set by irace.")] public long Seed { get; set; } [Value(3, Default = "20-15-35", HelpText = "Name of the problem instance.", Required = true)] public string ProblemInstance { get; set; } [Option('a', "alg", Default = AlgorithmType.OSGA, HelpText = "Either: OSGA, ILS, ES, GRASP")] public AlgorithmType Algorithm { get; set; } [Option('t', "tries", Default = 1, HelpText = "Number of repetitions to perform.")] public int Tries { get; set; } [Option('r', "maxrt", Default = 60, HelpText = "Maximum runtime in seconds.")] public int MaxRuntime { get; set; } [Option('i', "maxiter", Default = 0, HelpText = "Maximum number of iterations.")] public int MaxIterations { get; set; } [Option('e', "maxeval", Default = 0, HelpText = "Maximum number of evaluations.")] public int MaxEvaluations { get; set; } [Option("popsize", Default = 500, HelpText = "Population size (OSGA)")] public int PopulationSize { get; set; } [Option("mutprob", Default = 0.05, HelpText = "Mutation probability (OSGA)")] public double MutationProbability { get; set; } [Option("pertstr", Default = 0.5, HelpText = "Perturbation strength (ILS)")] public double PerturbationStrength { get; set; } [Option("mu", Default = 10, HelpText = "Parent population size (ES)")] public int Mu { get; set; } [Option("lambda", Default = 10, HelpText = "Offspring population size (ES)")] public int Lambda { get; set; } [Option("evosel", Default = ESSelection.Plus, HelpText = "Selection strategy + or , (ES)")] public ESSelection Selection { get; set; } [Option("recomb", Default = 0, HelpText = "Use recombination 0 or 1 (ES)")] public int Recombination { get; set; } [Option("eliteset", Default = 10, HelpText = "Size of the elite set (GRASP)")] public int EliteSetSize { get; set; } [Option("mineliteset", Default = 2, HelpText = "Minimum size of the elite set (GRASP)")] public int MinimumEliteSetSize { get; set; } [Option("lsiters", Default = 100, HelpText = "Maximum local search iterations (GRASP)")] public int MaximumLocalSearchIterations { get; set; } [Option("candidatesize", Default = 0.5, HelpText = "Candidate Size Factor (GRASP)")] public double CandidateSizeFactor { get; set; } [Option("maxcandsize", Default = 10, HelpText = "Maximum candidate list size (GRASP)")] public int MaximumCandidateListSize { get; set; } [Option("onemoveprob", Default = 0.5, HelpText = "Probability for performing a 1-move (GRASP)")] public double OneMoveProbability { get; set; } [Option("mindiff", Default = 4, HelpText = "Minimum difference (GRASP)")] public int MinimumDifference { get; set; } } class Program { static void Main(string[] args) { var parsed = CommandLine.Parser.Default.ParseArguments(args) .WithParsed(x => Run(x)); } private static void Run(CLOptions opts) { var provider = new CordeauGQAPInstanceProvider(); var descriptors = provider.GetDataDescriptors().ToList(); var avgBest = 0.0; for (var t = 0; t < opts.Tries; t++) { HeuristicLab.Optimization.IAlgorithm alg = null; switch (opts.Algorithm) { case AlgorithmType.OSGA: alg = new OSGA() { MaximumEvaluations = opts.MaxEvaluations, MaximumIterations = opts.MaxIterations, MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime), PopulationSize = opts.PopulationSize, MutationProbability = opts.MutationProbability, StopAtBestKnownQuality = true, Seed = (int)opts.Seed, SetSeedRandomly = false }; break; case AlgorithmType.ILS: alg = new IteratedLS() { MaximumEvaluations = opts.MaxEvaluations, MaximumIterations = opts.MaxIterations, MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime), PerturbationStrength = opts.PerturbationStrength, StopAtBestKnownQuality = true, Seed = (int)opts.Seed, SetSeedRandomly = false }; break; case AlgorithmType.ES: alg = new EvolutionStrategy() { MaximumEvaluations = opts.MaxEvaluations, MaximumIterations = opts.MaxIterations, MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime), Mu = opts.Mu, Lambda = opts.Lambda, Selection = opts.Selection, UseRecombination = opts.Recombination > 0, StopAtBestKnownQuality = true, Seed = (int)opts.Seed, SetSeedRandomly = false }; break; case AlgorithmType.GRASP: alg = new GRASP() { MaximumEvaluations = opts.MaxEvaluations, MaximumIterations = opts.MaxIterations, MaximumRuntime = TimeSpan.FromSeconds(opts.MaxRuntime), EliteSetSize = opts.EliteSetSize, MinimumEliteSetSize = opts.MinimumEliteSetSize, MaximumLocalSearchIterations = opts.MaximumLocalSearchIterations, CandidateSizeFactor = opts.CandidateSizeFactor, MaximumCandidateListSize = opts.MaximumCandidateListSize, OneMoveProbability = opts.OneMoveProbability, MinimumDifference = opts.MinimumDifference, StopAtBestKnownQuality = true, Seed = (int)opts.Seed, SetSeedRandomly = false }; break; } if (alg == null) throw new InvalidOperationException("Unknown algorithm."); var prob = alg.Problem as GQAP; if (prob == null) alg.Problem = prob = new GQAP(); prob.Load(provider.LoadData(descriptors.Single(x => x.Name == opts.ProblemInstance))); alg.Start(); avgBest += ((DoubleValue)alg.Results["BestQuality"].Value).Value; } Console.WriteLine(avgBest / opts.Tries); } } }