using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using HeuristicLab.Grammars; using Attribute = HeuristicLab.Grammars.Attribute; namespace CodeGenerator { // code generator for problem class public class ProblemCodeGen { private const string usings = @" using System.Collections.Generic; using System.Linq; using System; using System.Text.RegularExpressions; "; private const string problemTemplate = @" namespace ?PROBLEMNAME? { public class Tree { public int altIdx; public List subtrees; protected Tree() { // leave subtrees uninitialized } public Tree(int state, int altIdx, int numSubTrees) { subtrees = new List(); this.altIdx = altIdx; } } ?TERMINALNODECLASSDEFINITIONS? // generic interface for communication from problem interpretation to solver public interface ISolverState { void Reset(); int PeekNextAlternative(); // in alternative nodes returns the index of the alternative that should be followed void Follow(int idx); // next: derive the NT symbol with index=idx; void Unwind(); // finished with deriving the NT symbol } public sealed class ?IDENT?Problem { public ?IDENT?Problem() { Initialize(); } private void Initialize() { // the following is the source code from the INIT section of the problem definition #region INIT section ?INITSOURCE? #endregion } private ISolverState _state; public double Evaluate(ISolverState _state) { this._state = _state; #region objective function (MINIMIZE / MAXIMIZE section) ?FITNESSFUNCTION? #endregion } // additional code from the problem definition (CODE section) #region additional code ?ADDITIONALCODE? #endregion #region generated source for interpretation ?INTERPRETERSOURCE? #endregion #region generated code for the constraints for terminals ?CONSTRAINTSSOURCE? #endregion } }"; /// /// Generates the source code for a brute force searcher that can be compiled with a C# compiler /// /// An abstract syntax tree for a GPDL file public void Generate(GPDefNode ast) { var problemSourceCode = new SourceBuilder(); problemSourceCode.AppendLine(usings); GenerateProblemSource(ast, problemSourceCode); GenerateSolvers(ast, problemSourceCode); problemSourceCode .Replace("?PROBLEMNAME?", ast.Name) .Replace("?IDENT?", ast.Name); // write the source file to disk using (var stream = new StreamWriter(ast.Name + ".cs")) { stream.WriteLine(problemSourceCode.ToString()); } } private void GenerateProblemSource(GPDefNode ast, SourceBuilder problemSourceCode) { var grammar = CreateGrammarFromAst(ast); problemSourceCode .AppendLine(problemTemplate) .Replace("?FITNESSFUNCTION?", ast.FitnessFunctionNode.SrcCode) .Replace("?INITSOURCE?", ast.InitCodeNode.SrcCode) .Replace("?ADDITIONALCODE?", ast.ClassCodeNode.SrcCode) .Replace("?INTERPRETERSOURCE?", GenerateInterpreterSource(grammar)) .Replace("?CONSTRAINTSSOURCE?", GenerateConstraintMethods(ast.Terminals)) .Replace("?TERMINALNODECLASSDEFINITIONS?", GenerateTerminalNodeClassDefinitions(ast.Terminals.OfType())) ; } private void GenerateSolvers(GPDefNode ast, SourceBuilder solverSourceCode) { var grammar = CreateGrammarFromAst(ast); var randomSearchCodeGen = new RandomSearchCodeGen(); randomSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode); //var bruteForceSearchCodeGen = new BruteForceCodeGen(); //bruteForceSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode); } #region create grammar instance from AST // should be refactored so that we can directly query the AST private AttributedGrammar CreateGrammarFromAst(GPDefNode ast) { var nonTerminals = ast.NonTerminals .Select(t => new Symbol(t.Ident, GetSymbolAttributes(t.FormalParameters))) .ToArray(); var terminals = ast.Terminals .Select(t => new Symbol(t.Ident, GetSymbolAttributes(t.FormalParameters))) .ToArray(); string startSymbolName = ast.Rules.First().NtSymbol; // create startSymbol var startSymbol = nonTerminals.Single(s => s.Name == startSymbolName); var g = new AttributedGrammar(startSymbol, nonTerminals, terminals); // add all production rules foreach (var rule in ast.Rules) { var ntSymbol = nonTerminals.Single(s => s.Name == rule.NtSymbol); foreach (var alt in GetAlternatives(rule.Alternatives, nonTerminals.Concat(terminals))) { g.AddProductionRule(ntSymbol, alt); } // local initialization code if (!string.IsNullOrEmpty(rule.LocalCode)) g.AddLocalDefinitions(ntSymbol, rule.LocalCode); } return g; } private IEnumerable GetSymbolAttributes(string formalParameters) { return (from fieldDef in Util.ExtractParameters(formalParameters) select new Attribute(fieldDef.Identifier, fieldDef.Type, AttributeType.Parse(fieldDef.RefOrOut))) .ToList(); } private IEnumerable GetAlternatives(AlternativesNode altNode, IEnumerable allSymbols) { foreach (var alt in altNode.Alternatives) { yield return GetSequence(alt.Sequence, allSymbols); } } private Sequence GetSequence(IEnumerable sequence, IEnumerable allSymbols) { Debug.Assert(sequence.All(s => s is CallSymbolNode || s is RuleActionNode)); var l = new List(); foreach (var node in sequence) { var callSymbolNode = node as CallSymbolNode; var actionNode = node as RuleActionNode; if (callSymbolNode != null) { Debug.Assert(allSymbols.Any(s => s.Name == callSymbolNode.Ident)); // create a new symbol with actual parameters l.Add(new Symbol(callSymbolNode.Ident, GetSymbolAttributes(callSymbolNode.ActualParameter))); } else if (actionNode != null) { l.Add(new SemanticSymbol("SEM", actionNode.SrcCode)); } } return new Sequence(l); } #endregion #region helper methods for terminal symbols // produces helper methods for the attributes of all terminal nodes private string GenerateConstraintMethods(IEnumerable symbols) { var sb = new SourceBuilder(); var terminals = symbols.OfType(); foreach (var t in terminals) { GenerateConstraintMethods(t, sb); } return sb.ToString(); } // generates helper methods for the attributes of a given terminal node private void GenerateConstraintMethods(TerminalNode t, SourceBuilder sb) { foreach (var c in t.Constraints) { var fieldType = t.FieldDefinitions.First(d => d.Identifier == c.Ident).Type; if (c.Type == ConstraintNodeType.Range) { sb.AppendFormat("public {0} GetMax{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.RangeMaxExpression).AppendLine(); sb.AppendFormat("public {0} GetMin{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.RangeMinExpression).AppendLine(); //sb.AppendFormat("public {0} Get{1}_{2}(ISolverState _state) {{ _state. }}", fieldType, t.Ident, c.Ident, ) } else if (c.Type == ConstraintNodeType.Set) { sb.AppendFormat("public IEnumerable<{0}> GetAllowed{1}_{2}() {{ return {3}; }}", fieldType, t.Ident, c.Ident, c.SetExpression).AppendLine(); } } } #endregion private string GenerateTerminalNodeClassDefinitions(IEnumerable terminals) { var sb = new SourceBuilder(); foreach (var terminal in terminals) { GenerateTerminalNodeClassDefinitions(terminal, sb); } return sb.ToString(); } private void GenerateTerminalNodeClassDefinitions(TerminalNode terminal, SourceBuilder sb) { sb.AppendFormat("public class {0}Tree : Tree {{", terminal.Ident).BeginBlock(); foreach (var att in terminal.FieldDefinitions) { sb.AppendFormat("{0} {1};", att.Type, att.Identifier).AppendLine(); } sb.AppendFormat(" public {0}Tree(Random random, ?IDENT?Problem problem) : base() {{", terminal.Ident).BeginBlock(); foreach (var constr in terminal.Constraints) { if (constr.Type == ConstraintNodeType.Set) { sb.AppendLine("{").BeginBlock(); sb.AppendFormat(" var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); sb.AppendFormat("{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine(); sb.AppendLine("}").EndBlock(); } else { sb.AppendLine("{").BeginBlock(); sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min ", constr.Ident).AppendLine(); sb.AppendLine("}").EndBlock(); } } sb.AppendLine("}").EndBlock(); sb.AppendLine("}").EndBlock(); } private string GenerateInterpreterSource(AttributedGrammar grammar) { var sb = new SourceBuilder(); GenerateInterpreterStart(grammar, sb); // generate methods for all nonterminals and terminals using the grammar instance foreach (var s in grammar.NonTerminalSymbols) { GenerateInterpreterMethod(grammar, s, sb); } foreach (var s in grammar.TerminalSymbols) { GenerateTerminalInterpreterMethod(s, sb); } return sb.ToString(); } private void GenerateInterpreterStart(AttributedGrammar grammar, SourceBuilder sb) { var s = grammar.StartSymbol; // create the method which can be called from the fitness function if (!s.Attributes.Any()) sb.AppendFormat("private void {0}() {{", s.Name).BeginBlock(); else sb.AppendFormat("private void {0}({1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); // get formal parameters of start symbol var attr = s.Attributes; // actual parameter are the same as formalparameter only without type identifier string actualParameter; if (attr.Any()) actualParameter = attr.Skip(1).Aggregate(attr.First().AttributeType + " " + attr.First().Name, (str, a) => str + ", " + a.AttributeType + " " + a.Name); else actualParameter = string.Empty; sb.AppendLine("_state.Reset();"); sb.AppendFormat("{0}(_state, {1});", s.Name, actualParameter).AppendLine(); sb.AppendLine("}").EndBlock(); } private void GenerateInterpreterMethod(AttributedGrammar g, ISymbol s, SourceBuilder sb) { if (!s.Attributes.Any()) sb.AppendFormat("private void {0}(ISolverState _state) {{", s.Name).BeginBlock(); else sb.AppendFormat("private void {0}(ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); // generate local definitions sb.AppendLine(g.GetLocalDefinitions(s)); var altsWithSemActions = g.GetAlternativesWithSemanticActions(s).ToArray(); if (altsWithSemActions.Length > 1) { GenerateSwitchStatement(altsWithSemActions, sb); } else { int i = 0; foreach (var altSymb in altsWithSemActions.Single()) { GenerateSourceForAction(i, altSymb, sb); if (!(altSymb is SemanticSymbol)) i++; } } sb.Append("}").EndBlock(); } private void GenerateSwitchStatement(IEnumerable alts, SourceBuilder sb) { sb.Append("switch(_state.PeekNextAlternative()) {").BeginBlock(); // generate a case for each alternative int i = 0; foreach (var alt in alts) { sb.AppendFormat("case {0}: {{ ", i).BeginBlock(); // this only works for alternatives with a single non-terminal symbol (ignoring semantic symbols)! // a way to handle this is through grammar transformation (the examplary grammars all have the correct from) Debug.Assert(alt.Count(symb => !(symb is SemanticSymbol)) == 1); foreach (var altSymb in alt) { GenerateSourceForAction(0, altSymb, sb); // index is always 0 because of the assertion above } i++; sb.AppendLine("break;").Append("}").EndBlock(); } sb.AppendLine("default: throw new System.InvalidOperationException();").Append("}").EndBlock(); } // helper for generating calls to other symbol methods private void GenerateSourceForAction(int idx, ISymbol s, SourceBuilder sb) { var action = s as SemanticSymbol; if (action != null) sb.Append(action.Code + ";"); else if (!s.Attributes.Any()) sb.AppendFormat("_state.Follow({0}); {1}(_state); _state.Unwind();", idx, s.Name); else sb.AppendFormat("_state.Follow({0}); {1}(_state, {2}); _state.Unwind();", idx, s.Name, s.GetAttributeString()); sb.AppendLine(); } private void GenerateTerminalInterpreterMethod(ISymbol s, SourceBuilder sb) { // if the terminal symbol has attributes then we must samples values for these attributes if (!s.Attributes.Any()) sb.AppendFormat("private void {0}(ISolverState _state) {{", s.Name).BeginBlock(); else sb.AppendFormat("private void {0}(ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); // each field must match a formal parameter, assign a value for each parameter int i = 0; foreach (var element in s.Attributes) { sb.AppendFormat("_state.Follow({0});", i++).AppendLine(); sb.AppendFormat("{0} = Get{1}_{0}(_state);", element.Name, s.Name).AppendLine(); sb.AppendFormat("_state.Unwind();").AppendLine(); } sb.Append("}").EndBlock(); } } }