using System; using HeuristicLab.Problems.GrammaticalOptimization; namespace HeuristicLab.Algorithms.GrammaticalOptimization { public class RandomSearch : SolverBase { private readonly int maxLen; private readonly System.Random random; private readonly IProblem problem; public RandomSearch(IProblem problem, System.Random random, int maxLen) { this.maxLen = maxLen; this.random = random; this.problem = problem; } public override void Run(int maxIterations) { for (int i = 0; i < maxIterations; i++) { var sentence = CreateSentence(problem.Grammar).ToString(); var quality = problem.Evaluate(sentence) / problem.BestKnownQuality(maxLen); OnSolutionEvaluated(sentence, quality); } } private Sequence CreateSentence(IGrammar grammar) { var sentence = new Sequence(grammar.SentenceSymbol); return grammar.CompleteSentenceRandomly(random, sentence, maxLen); } } }