/// <summary> /// Compare two nodes. /// </summary> /// <param name="result">The result of previous comparisons.</param> /// <param name="node1">The first node to compare.</param> /// <param name="node2">The second node to compare.</param> /// <returns>The result.</returns> private double CompareNode(double result, ProgramNode node1, ProgramNode node2) { double newResult = result; if (node1.Template != node2.Template) { newResult++; } int node1Size = node1.ChildNodes.Count; int node2Size = node2.ChildNodes.Count; int childNodeCount = Math.Max(node1Size, node2Size); for (int i = 0; i < childNodeCount; i++) { if (i < node1Size && i < node2Size) { ProgramNode childNode1 = node1.GetChildNode(i); ProgramNode childNode2 = node2.GetChildNode(i); newResult = CompareNode(newResult, childNode1, childNode2); } else { newResult++; } } return newResult; }
/// <summary> /// Attempt to rewrite the specified node. /// </summary> /// <param name="node">The node to attempt to rewrite.</param> /// <returns>The rewritten node, the original node, if no rewrite occured.</returns> private ProgramNode RewriteNode(ProgramNode node) { // first try to rewrite the child node ProgramNode rewrite = TryNodeRewrite(node); if (rewrite != null) { return rewrite; } // if we could not rewrite the entire node, rewrite as many children as // we can for (int i = 0; i < node.ChildNodes.Count; i++) { var childNode = (ProgramNode) node.ChildNodes[i]; rewrite = RewriteNode(childNode); if (rewrite != null) { node.ChildNodes.RemoveAt(i); node.ChildNodes.Insert(i, rewrite); _rewritten = true; } } // we may have rewritten some children, but the parent was not // rewritten, so return null. return null; }
/// <summary> /// Returns true, if the specified constant value is a false const. Returns /// false in any other case. /// </summary> /// <param name="node">The node to check.</param> /// <returns>True if the value is a false const.</returns> private bool IsFalse(ProgramNode node) { if (node.Template == StandardExtensions.EXTENSION_CONST_SUPPORT) { ExpressionValue v = node.Evaluate(); if (v.IsBoolean) { if (!v.ToBooleanValue()) { return true; } } } return false; }
/// <summary> /// Attempt to rewrite the specified node. /// </summary> /// <param name="parent">The node to attempt to rewrite.</param> /// <returns>The rewritten node, or the original node, if no change was made.</returns> private ProgramNode InternalRewrite(ProgramNode parent) { ProgramNode rewrittenParent = parent; rewrittenParent = TryAnd(rewrittenParent); // try children for (int i = 0; i < rewrittenParent.ChildNodes.Count; i++) { var childNode = (ProgramNode) rewrittenParent.ChildNodes[i]; ProgramNode rewriteChild = InternalRewrite(childNode); if (childNode != rewriteChild) { rewrittenParent.ChildNodes.RemoveAt(i); rewrittenParent.ChildNodes.Insert(i, rewriteChild); _rewritten = true; } } return rewrittenParent; }
public void TestSemanticsFail() { UnitTestParsing testParsing = new UnitTestParsing(); string directoryFail = "../../../TestCases/Semantics/fail/"; DirectoryInfo directory = new DirectoryInfo(directoryFail); FileInfo[] files = directory.GetFiles(); foreach (var file in files) { var errors = new List <string>(); List <string> errorParsing = testParsing.ParsingFile(file.FullName); if (errorParsing.Any()) { Assert.IsTrue(errorParsing.Any()); continue; } var astBuilder = new ASTBuilder(); ProgramNode program = astBuilder.Visit(testParsing.tree) as ProgramNode; Scope.Clear(); var scope = new Scope(); program = new Tour1().CheckSemantic(program, scope, errors); if (errors.Any()) { Assert.IsTrue(errors.Any(), file.Name); } else { program = new Tour2().CheckSemantic(program, scope, errors); Assert.IsTrue(errors.Any(), file.Name); } } }
public void TestSemanticsSuccess() { UnitTestParsing testParsing = new UnitTestParsing(); string directorySuccess = "../../../TestCases/Semantics/success/"; DirectoryInfo directory = new DirectoryInfo(directorySuccess); FileInfo[] files = directory.GetFiles(); foreach (var file in files) { var errors = new List <string>(); List <string> errorParsing = testParsing.ParsingFile(file.FullName); foreach (var item in errorParsing) { Assert.Fail($"Parsing error in {file.Name}: {item}"); } var astBuilder = new ASTBuilder(); ProgramNode program = astBuilder.Visit(testParsing.tree) as ProgramNode; Scope.Clear(); var scope = new Scope(); program = new Tour1().CheckSemantic(program, scope, errors); foreach (var item in errors) { Assert.Fail(file.Name + ": " + item.ToString()); } program = new Tour2().CheckSemantic(program, scope, errors); foreach (var item in errors) { Assert.Fail(file.Name + ": " + item.ToString()); } } }
public void TestFunctionDeclaration() { var programSource = new TokenList() { { TokenType.KwFunction }, { TokenType.Identifier, "func" }, { TokenType.LParen }, { TokenType.Identifier, "par1" }, { TokenType.Colon }, { TokenType.Identifier, "int" }, { TokenType.RParen }, { TokenType.Colon }, { TokenType.Identifier, "bool" }, { TokenType.LineTerm }, { TokenType.KwBegin }, { TokenType.KwReturn }, { TokenType.IntLiteral, "123" }, { TokenType.KwEnd } }; Parser parser = new Parser(CreateMockScanner(programSource), new ErrorHandler()); ProgramNode program = parser.Parse(); var declr = new FunctionDeclarationStmt(0, 0); declr.Identifier = "func"; declr.ReturnType = new SimpleType(0, 0, ExprType.Bool); declr.AddParameter("par1", new SimpleType(0, 0, ExprType.Int), false); declr.ProcedureBlock = new BlockStmt(0, 0); var returnStmt = new ReturnStmt(0, 0); returnStmt.ReturnExpression = new IntLiteralExpr(0, 0, 123); declr.ProcedureBlock.Statements.Add(returnStmt); expected.Block.Statements.Add(declr); program.ShouldBeEquivalentTo(expected); }
public ProgramCompiler( AllTypes allTypes, ProgramNode programNode, string mainClassName, string mainMethodName ) { this.allTypes = allTypes; module = allTypes.Module; this.programNode = programNode; sourceFile = programNode.SourceFile; mainClass = new TypeDefinition( "", mainClassName, TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, module.TypeSystem.Object ); module.Types.Add(mainClass); MainMethod = new MethodDefinition( "Main", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Static, module.TypeSystem.Void ); mainClass.Methods.Add(MainMethod); }
private static void LoadAndTestSubstrings() { var grammar = LoadGrammar("ProseSample.Substrings.grammar"); if (grammar == null) { return; } ProgramNode p = ProgramNode.Parse(@"SubStr(v, PosPair(AbsPos(v, -4), AbsPos(v, -1)))", grammar, ASTSerializationFormat.HumanReadable); StringRegion data = RegionLearner.CreateStringRegion("Microsoft PROSE SDK"); State input = State.Create(grammar.InputSymbol, data); Console.WriteLine(p.Invoke(input)); StringRegion sdk = data.Slice(data.End - 3, data.End); Spec spec = ShouldConvert.Given(grammar).To(data, sdk); Learn(grammar, spec, new Substrings.RankingScore(grammar), new Substrings.WitnessFunctions(grammar)); TestTextTransformationBenchmark(grammar, "emails"); }
public void TestLearnSubstringOneExample() { Result <Grammar> grammar = CompileGrammar(); SynthesisEngine prose = ConfigureSynthesis(grammar.Value); State input = State.CreateForExecution(grammar.Value.InputSymbol, "Toby Miller"); var examples = new Dictionary <State, object> { { input, "Miller" } }; var spec = new ExampleSpec(examples); var scoreFeature = new RankingScore(grammar.Value); ProgramSet topPrograms = prose.LearnGrammarTopK(spec, scoreFeature, 1, null); ProgramNode topProgram = topPrograms.RealizedPrograms.First(); var output = topProgram.Invoke(input) as string; Assert.AreEqual("Miller", output); State input2 = State.CreateForExecution(grammar.Value.InputSymbol, "Courtney Lynch"); var output2 = topProgram.Invoke(input2) as string; Assert.AreEqual("Lynch", output2); }
public void ProgramWithTwoFunctions() { var tree = Wrap(P.Program, new ParseTree[] { NumberFunction(), NumberFunction(), Leaf(P.Eof, "") }); var expected = new ProgramNode(new LinkedList <FunctionDefinitionNode>(new FunctionDefinitionNode[] { new FunctionDefinitionNode( "MyFunc", new LinkedList <VariableDeclNode>(), NamedTypeNode.IntType(true), new AtomNode(NamedTypeNode.IntType(true), "42") ), new FunctionDefinitionNode( "MyFunc", new LinkedList <VariableDeclNode>(), NamedTypeNode.IntType(true), new AtomNode(NamedTypeNode.IntType(true), "42") ) }), new LinkedList <RecordTypeDeclarationNode>()); ConductTest(tree, expected); }
/// <summary> /// Try to rewrite the specified node. /// </summary> /// <param name="parentNode">The node to attempt rewrite.</param> /// <returns>The rewritten node, or original node, if no rewrite could happen.</returns> private ProgramNode TryNodeRewrite(ProgramNode parentNode) { ProgramNode result = null; if (parentNode.IsLeaf) { return(null); } if (parentNode.AllConstDescendants()) { ExpressionValue v = parentNode.Evaluate(); double ck = v.ToFloatValue(); // do not rewrite if it produces a div by 0 or other bad result. if (Double.IsNaN(ck) || Double.IsInfinity(ck)) { return(null); } result = parentNode.Owner.Context.Functions .FactorProgramNode("#const", parentNode.Owner, new ProgramNode[] { }); // is it an integer? if (Math.Abs(ck - ((int)ck)) < EncogFramework.DefaultDoubleEqual) { result.Data[0] = new ExpressionValue((int)ck); } else { result.Data[0] = v; } } return(result); }
/// <summary> /// Instrument the if-else block. /// </summary> /// <param name="node">The lca node.</param> private void InstrumentIfElse(ProgramNode node) { ProgramNode next = node.Successors.First(); // the block should have source location information for instrumentation to work if (next.Block.Cmds.Count > 1 && next.Block.Cmds[1] is AssertCmd) { AssertCmd assert = next.Block.Cmds[1] as AssertCmd; assert = DuplicateAssertCmd(assert); if (assert != null) { Block lca = null; foreach (Block block in program.Implementations.SelectMany(x => x.Blocks)) { if (block.TransferCmd is GotoCmd) { GotoCmd gotoCmd = block.TransferCmd as GotoCmd; if (gotoCmd.labelTargets.Contains(next.Block)) { lca = block; break; } } } int index = lca.Cmds.Count; lca.Cmds.Insert(index, assert); Implementation implementation = program.Implementations.First(x => x.Blocks.Contains(lca)); // insert a barrier at the end of the header block AddBarrier(implementation, lca, index + 1); } } }
public static ProgramNode Learn(Grammar grammar, Spec spec, Feature <double> scoreFeature, DomainLearningLogic witnessFunctions) { var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config { UseThreads = false, Strategies = new ISynthesisStrategy[] { new EnumerativeSynthesis(), new DeductiveSynthesis(witnessFunctions), }, LogListener = new LogListener(), }); ProgramSet consistentPrograms = engine.LearnGrammar(spec); engine.Configuration.LogListener.SaveLogToXML("learning.log.xml"); /*foreach (ProgramNode p in consistentPrograms.RealizedPrograms) * { * Console.WriteLine(p); * }*/ ProgramNode bestProgram = consistentPrograms.TopK(scoreFeature).FirstOrDefault(); if (bestProgram == null) { WriteColored(ConsoleColor.Red, "No program :("); return(null); } var score = bestProgram.GetFeatureValue(scoreFeature); WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}"); return(bestProgram); }
/// <summary> /// Try to rewrite x+-c. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryPlusNeg(ProgramNode parent) { if (parent.Name.Equals("+") && parent.ChildNodes.Count == 2) { ProgramNode child1 = parent.GetChildNode(0); ProgramNode child2 = parent.GetChildNode(1); if (child2.Name.Equals("-") && child2.ChildNodes.Count == 1) { parent = parent.Owner.Context.Functions.FactorProgramNode( "-", parent.Owner, new[] { child1, child2.GetChildNode(0) }); } else if (child2.Name.Equals("#const")) { ExpressionValue v = child2.Data[0]; if (v.IsFloat) { double v2 = v.ToFloatValue(); if (v2 < 0) { child2.Data[0] = new ExpressionValue(-v2); parent = parent.Owner.Context.Functions.FactorProgramNode("-", parent.Owner, new[] {child1, child2}); } } else if (v.IsInt) { long v2 = v.ToIntValue(); if (v2 < 0) { child2.Data[0] = new ExpressionValue(-v2); parent = parent.Owner.Context.Functions .FactorProgramNode("-", parent.Owner, new[] {child1, child2}); } } } } return parent; }
/// <summary> /// Try to rewrite x-0. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryMinusZero(ProgramNode parent) { if (parent.Template == StandardExtensions.EXTENSION_SUB) { ProgramNode child2 = parent.GetChildNode(1); if (IsConstValue(child2, 0)) { ProgramNode child1 = parent.GetChildNode(0); return child1; } } return parent; }
/// <summary> /// Determine if the specified node is constant. /// </summary> /// <param name="node">The node to check.</param> /// <param name="v">The constant to compare against.</param> /// <returns>True if the specified node matches the specified constant.</returns> private bool IsConstValue(ProgramNode node, double v) { if (node.Template == StandardExtensions.EXTENSION_CONST_SUPPORT) { if (Math.Abs(node.Data[0].ToFloatValue() - v) < EncogFramework.DefaultDoubleEqual) { return true; } } return false; }
/// <summary> /// This method is called reflexivly as we iterate downward. Once we reach /// the desired point (when current level drops to zero), the operation is /// performed. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="parentNode">The parent node.</param> /// <param name="types">The desired node.</param> /// <param name="holder">The level holder.</param> private void FindNode(EncogRandom rnd, ProgramNode parentNode, IList<EPLValueType> types, LevelHolder holder) { if (holder.CurrentLevel == 0) { holder.DecreaseLevel(); holder.Types = types; holder.NodeFound = parentNode; } else { holder.DecreaseLevel(); for (int i = 0; i < parentNode.Template.ChildNodeCount; i++) { ProgramNode childNode = parentNode.GetChildNode(i); IList<EPLValueType> childTypes = parentNode.Template .Params[i].DetermineArgumentTypes(types); FindNode(rnd, childNode, childTypes, holder); } } }
public void Visit(ProgramNode node) { node.Classes.ForEach(cclass => cclass.Accept(new Tour2(cclass.Scope, errors))); }
public BlockStatementNode(ProgramNode children) => Statements = children;
public static void TypeDefinition(SourceCodeBody src, ProgramNode pgm) { pgm.Structs.Each(x => src.Structs.Add(TypeBodyDefinition(src, x))); }
public ExpressionValue Evaluate(ProgramNode actual) { return _delEvaluate(actual); }
/// <summary> /// Create a terminal node. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="program">The program to generate for.</param> /// <param name="types">The types that we might generate.</param> /// <returns>The terminal program node.</returns> public ProgramNode CreateTerminalNode(EncogRandom rnd, EncogProgram program, IList<EPLValueType> types) { IProgramExtensionTemplate temp = GenerateRandomOpcode( rnd, Context.Functions.FindOpcodes(types, _context, true, false)); if (temp == null) { throw new EACompileError("No opcodes exist for the type: " + types); } var result = new ProgramNode(program, temp, new ProgramNode[] {}); temp.Randomize(rnd, types, result, MinConst, MaxConst); return result; }
/// <summary> /// Try to rewrite the specified node. /// </summary> /// <param name="parentNode">The node to attempt rewrite.</param> /// <returns>The rewritten node, or original node, if no rewrite could happen.</returns> private ProgramNode TryNodeRewrite(ProgramNode parentNode) { ProgramNode result = null; if (parentNode.IsLeaf) { return null; } if (parentNode.AllConstDescendants()) { ExpressionValue v = parentNode.Evaluate(); double ck = v.ToFloatValue(); // do not rewrite if it produces a div by 0 or other bad result. if (Double.IsNaN(ck) || Double.IsInfinity(ck)) { return null; } result = parentNode.Owner.Context.Functions .FactorProgramNode("#const", parentNode.Owner, new ProgramNode[] {}); // is it an integer? if (Math.Abs(ck - ((int) ck)) < EncogFramework.DefaultDoubleEqual) { result.Data[0] = new ExpressionValue((int) ck); } else { result.Data[0] = v; } } return result; }
public virtual T Visit(ProgramNode node) { Visit((TranslationUnit)node); return(traverse(node.section)); }
/// <summary> /// This method is called reflexivly as we iterate downward. Once we reach /// the desired point (when current level drops to zero), the operation is /// performed. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="result">The parent node.</param> /// <param name="parentNode"></param> /// <param name="types">The desired node</param> /// <param name="globalIndex">The level holder.</param> private void FindNode(EncogRandom rnd, EncogProgram result, ProgramNode parentNode, IList<EPLValueType> types, int[] globalIndex) { if (globalIndex[0] == 0) { globalIndex[0]--; ProgramNode newInsert = Generator.CreateNode(rnd, result, _maxDepth, types); result.ReplaceNode(parentNode, newInsert); } else { globalIndex[0]--; for (int i = 0; i < parentNode.Template.ChildNodeCount; i++) { ProgramNode childNode = parentNode.GetChildNode(i); IList<EPLValueType> childTypes = parentNode.Template.Params[i].DetermineArgumentTypes(types); FindNode(rnd, result, childNode, childTypes, globalIndex); } } }
/// <summary> /// Try to rewrite true and true, false and false. /// </summary> /// <param name="parent">The node to attempt to rewrite.</param> /// <returns>The rewritten node, or the original node if not rewritten.</returns> private ProgramNode TryAnd(ProgramNode parent) { if (parent.Template == StandardExtensions.EXTENSION_AND) { ProgramNode child1 = parent.GetChildNode(0); ProgramNode child2 = parent.GetChildNode(1); if (IsTrue(child1) && child2.Template != StandardExtensions.EXTENSION_CONST_SUPPORT) { _rewritten = true; return child2; } if (IsTrue(child2) && child1.Template != StandardExtensions.EXTENSION_CONST_SUPPORT) { _rewritten = true; return child1; } } return parent; }
/// <summary> /// Try to rewrite x+x, x-x, x*x, x/x. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryVarOpVar(ProgramNode parent) { if (parent.ChildNodes.Count == 2 && parent.Name.Length == 1 && "+-*/".IndexOf(parent.Name[0]) != -1) { ProgramNode child1 = parent.GetChildNode(0); ProgramNode child2 = parent.GetChildNode(1); if (child1.Name.Equals("#var") && child2.Name.Equals("#var")) { if (child1.Data[0].ToIntValue() == child2.Data[0] .ToIntValue()) { switch (parent.Name[0]) { case '-': parent = CreateNumericConst(parent.Owner, 0); break; case '+': parent = parent.Owner.Functions.FactorProgramNode("*", parent.Owner, new[] { CreateNumericConst( parent.Owner, 2), child1 }); break; case '*': parent = parent .Owner .Functions .FactorProgramNode( "^", parent.Owner, new[] { child1, CreateNumericConst( parent.Owner, 2) }); break; case '/': parent = CreateNumericConst(parent.Owner, 1); break; } } } } return parent; }
/// <inheritdoc /> public void Randomize(EncogRandom rnd, IList<EPLValueType> desiredType, ProgramNode actual, double minValue, double maxValue) { if (_delRandomize != null) { _delRandomize(rnd, desiredType, actual, minValue, maxValue); } }
/// <summary> /// Try to rewrite 0+x. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryZeroPlus(ProgramNode parent) { if (parent.Template == StandardExtensions.EXTENSION_ADD) { ProgramNode child1 = parent.GetChildNode(0); ProgramNode child2 = parent.GetChildNode(1); if (IsConstValue(child1, 0)) { _rewritten = true; return child2; } if (IsConstValue(child2, 0)) { _rewritten = true; return child1; } } return parent; }
public void RunProgram(ProgramNode program) { Compile(program); RunCompiled(); }
/// <summary> /// Create a random note according to the specified paramaters. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="program">The program to generate for.</param> /// <param name="depthRemaining">The depth remaining to generate.</param> /// <param name="types">The types to generate.</param> /// <param name="includeTerminal">Should we include terminal nodes.</param> /// <param name="includeFunction">Should we include function nodes.</param> /// <returns>The generated program node.</returns> public ProgramNode CreateRandomNode(EncogRandom rnd, EncogProgram program, int depthRemaining, IList<EPLValueType> types, bool includeTerminal, bool includeFunction) { // if we've hit the max depth, then create a terminal nodes, so it stops // here if (depthRemaining == 0) { return CreateTerminalNode(rnd, program, types); } // choose which opcode set we might create the node from IList<IProgramExtensionTemplate> opcodeSet = Context.Functions.FindOpcodes(types, Context, includeTerminal, includeFunction); // choose a random opcode IProgramExtensionTemplate temp = GenerateRandomOpcode(rnd, opcodeSet); if (temp == null) { throw new EACompileError( "Trying to generate a random opcode when no opcodes exist."); } // create the child nodes int childNodeCount = temp.ChildNodeCount; var children = new ProgramNode[childNodeCount]; if (EncogOpcodeRegistry.IsOperator(temp.NodeType) && children.Length >= 2) { // for an operator of size 2 or greater make sure all children are // the same time IList<EPLValueType> childTypes = temp.Params[0] .DetermineArgumentTypes(types); EPLValueType selectedType = childTypes[rnd .Next(childTypes.Count)]; childTypes.Clear(); childTypes.Add(selectedType); // now create the children of a common type for (int i = 0; i < children.Length; i++) { children[i] = CreateNode(rnd, program, depthRemaining - 1, childTypes); } } else { // otherwise, let the children have their own types for (int i = 0; i < children.Length; i++) { IList<EPLValueType> childTypes = temp.Params[i] .DetermineArgumentTypes(types); children[i] = CreateNode(rnd, program, depthRemaining - 1, childTypes); } } // now actually create the node var result = new ProgramNode(program, temp, children); temp.Randomize(rnd, types, result, MinConst, MaxConst); return result; }
/// <summary> /// Factor a new program node, based in a template object. /// </summary> /// <param name="temp">The opcode.</param> /// <param name="program">The program.</param> /// <param name="args">The arguments for this node.</param> /// <returns>The newly created ProgramNode.</returns> public ProgramNode FactorProgramNode(IProgramExtensionTemplate temp, EncogProgram program, ProgramNode[] args) { return new ProgramNode(program, temp, args); }
public virtual Value evaluate(Context cx, ProgramNode node) { output("<ProgramNode position=\"" + node.pos() + "\">"); indent_Renamed_Field++; if (node.pkgdefs != null) { // for (PackageDefinitionNode n : node.pkgdefs) for (int i = 0, size = node.pkgdefs.size(); i < size; i++) { PackageDefinitionNode n = (PackageDefinitionNode) node.pkgdefs.get_Renamed(i); n.evaluate(cx, this); } } if (node.statements != null) { node.statements.evaluate(cx, this); } if (node.fexprs != null) { // for (FunctionCommonNode n : node.fexprs) for (int i = 0, size = node.fexprs.size(); i < size; i++) { FunctionCommonNode n = (FunctionCommonNode) node.fexprs.get_Renamed(i); n.evaluate(cx, this); } } if (node.clsdefs != null) { // for (FunctionCommonNode n : node.clsdefs) for (int i = 0, size = node.clsdefs.size(); i < size; i++) { ClassDefinitionNode n = (ClassDefinitionNode) node.clsdefs.get_Renamed(i); n.evaluate(cx, this); } } indent_Renamed_Field--; output("</ProgramNode>"); return null; }
/// <summary> /// Carries out type checking on a program /// </summary> /// <param name="tree">The program to check</param> public void PerformTypeChecking(ProgramNode tree) { PerformTypeCheckingOnProgram(tree); }
private String RenderConst(ProgramNode node) { return(node.Data[0].ToStringValue()); }
/// <summary> /// Called for each node in the progrmam. If this is a const node, then /// mutate it according to the frequency and sigma specified. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="node">The node to mutate.</param> private void MutateNode(EncogRandom rnd, ProgramNode node) { if (node.Template == StandardExtensions.EXTENSION_CONST_SUPPORT) { if (rnd.NextDouble() < _frequency) { ExpressionValue v = node.Data[0]; if (v.IsFloat) { double adj = rnd.NextGaussian()*_sigma; node.Data[0] = new ExpressionValue(v.ToFloatValue() + adj); } } } foreach (ITreeNode n in node.ChildNodes) { var childNode = (ProgramNode) n; MutateNode(rnd, childNode); } }
/// <summary> /// Factor a new program node, based on an opcode name and arguments. /// </summary> /// <param name="name">The name of the opcode.</param> /// <param name="program">The program to factor for.</param> /// <param name="args">The arguements.</param> /// <returns>The newly created ProgramNode.</returns> public ProgramNode FactorProgramNode(String name, EncogProgram program, ProgramNode[] args) { String key = EncogOpcodeRegistry.CreateKey(name, args.Length); if (!_templateMap.ContainsKey(key)) { throw new EACompileError("Undefined function/operator: " + name + " with " + args.Length + " args."); } IProgramExtensionTemplate temp = _templateMap[key]; return new ProgramNode(program, temp, args); }
private String RenderVar(ProgramNode node) { int varIndex = (int)node.Data[0].ToIntValue(); return(this.holder.Variables.GetVariableName(varIndex)); }
/// <summary> /// Carries out type checking on a program node /// </summary> /// <param name="programNode">The node to perform type checking on</param> private void PerformTypeCheckingOnProgram(ProgramNode programNode) { PerformTypeChecking(programNode.Command); }
/// <summary> /// Serves as the default hash function. /// </summary> /// <returns> /// A hash code for the current object. /// </returns> public override int GetHashCode() => ProgramNode?.GetHashCode() ?? 0;
/// <summary> /// Try to rewrite --x. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryDoubleNegative(ProgramNode parent) { if (parent.Name.Equals("-")) { ProgramNode child = parent.GetChildNode(0); if (child.Name.Equals("-")) { ProgramNode grandChild = child.GetChildNode(0); _rewritten = true; return grandChild; } } return parent; }
/// <summary> /// Returns a string that represents the current object. /// </summary> /// <returns> /// A string that represents the current object. /// </returns> public override string ToString() => ProgramNode.ToString();
/// <summary> /// Try to rewrite x^1. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryOnePower(ProgramNode parent) { if (parent.Template == StandardExtensions.EXTENSION_POWER || parent.Template == StandardExtensions.EXTENSION_POWFN) { ProgramNode child = parent.GetChildNode(0); if (child.Template == StandardExtensions.EXTENSION_CONST_SUPPORT) { if (Math.Abs(child.Data[0].ToFloatValue() - 1) < EncogFramework.DefaultDoubleEqual) { _rewritten = true; return CreateNumericConst(parent.Owner, 1); } } } return parent; }
/// <summary> /// Executes the program on the <paramref name="input" /> to obtain the output. /// </summary> /// <param name="input">The input token.</param> /// <returns>The result output.</returns> public override IReadOnlyList <Node> Run(MergeConflict input) { State inputState = State.CreateForExecution(LanguageGrammar.Instance.Grammar.InputSymbol, input); return(ProgramNode.Invoke(inputState) as IReadOnlyList <Node>); }
/// <summary> /// Try to rewrite x^0. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryPowerZero(ProgramNode parent) { if (parent.Template == StandardExtensions.EXTENSION_POWER || parent.Template == StandardExtensions.EXTENSION_POWFN) { ProgramNode child0 = parent.GetChildNode(0); ProgramNode child1 = parent.GetChildNode(1); if (IsConstValue(child1, 0)) { return CreateNumericConst(parent.Owner, 1); } if (IsConstValue(child0, 0)) { return CreateNumericConst(parent.Owner, 0); } } return parent; }
/// <summary> /// Serializes a program to a string that can be loaded using <see cref="Loader.Load" />. /// </summary> /// <returns>A machine-readable string representation of this program.</returns> public string Serialize() => ProgramNode.PrintAST();
/// <summary> /// Try to rewrite 0*x. /// </summary> /// <param name="parent">The parent node to attempt to rewrite.</param> /// <returns>The rewritten node, if it was rewritten.</returns> private ProgramNode TryZeroMul(ProgramNode parent) { if (parent.Template == StandardExtensions.EXTENSION_MUL) { ProgramNode child1 = parent.GetChildNode(0); ProgramNode child2 = parent.GetChildNode(1); if (IsConstValue(child1, 0) || IsConstValue(child2, 0)) { _rewritten = true; return CreateNumericConst(parent.Owner, 0); } } return parent; }
public ExpressionValue Evaluate(ProgramNode actual) { return(_delEvaluate(actual)); }
/// <summary> /// Attempt to rewrite the specified node. /// </summary> /// <param name="parent">The parent node to start from.</param> /// <returns>The rewritten node, or the same node if no rewrite occurs.</returns> private ProgramNode InternalRewrite(ProgramNode parent) { ProgramNode rewrittenParent = parent; rewrittenParent = TryDoubleNegative(rewrittenParent); rewrittenParent = TryMinusMinus(rewrittenParent); rewrittenParent = TryPlusNeg(rewrittenParent); rewrittenParent = TryVarOpVar(rewrittenParent); rewrittenParent = TryPowerZero(rewrittenParent); rewrittenParent = TryOnePower(rewrittenParent); rewrittenParent = TryZeroPlus(rewrittenParent); rewrittenParent = TryZeroDiv(rewrittenParent); rewrittenParent = TryZeroMul(rewrittenParent); rewrittenParent = TryMinusZero(rewrittenParent); // try children for (int i = 0; i < rewrittenParent.ChildNodes.Count; i++) { var childNode = (ProgramNode) rewrittenParent.ChildNodes[i]; ProgramNode rewriteChild = InternalRewrite(childNode); if (childNode != rewriteChild) { rewrittenParent.ChildNodes.RemoveAt(i); rewrittenParent.ChildNodes.Insert(i, rewriteChild); _rewritten = true; } } return rewrittenParent; }
public int NumberOfEquals_Sort(ProgramNode p) { return(0); }
/// <summary> /// Generates code for a program node /// </summary> /// <param name="programNode">The node to generate code for</param> private void GenerateCodeForProgram(ProgramNode programNode) { Debugger.Write("Generating code for Program"); GenerateCodeFor(programNode.Command); code.AddInstruction(OpCode.HALT, 0, 0, 0); }
/// <summary> /// Carries out identification on a program node /// </summary> /// <param name="programNode">The node to perform identification on</param> private void PerformIdentificationOnProgram(ProgramNode programNode) { PerformIdentification(programNode.Command); }
public void Generate(ProgramNode programNode) { Visit(programNode); }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example (e.g., \"(Sumit Gulwani)\",\"Gulwani\"): "); try { string input = Console.ReadLine(); if (input != null) { int startFirstExample = input.IndexOf("\"", StringComparison.Ordinal) + 1; int endFirstExample = input.IndexOf("\"", startFirstExample + 1, StringComparison.Ordinal) + 1; int startSecondExample = input.IndexOf("\"", endFirstExample + 1, StringComparison.Ordinal) + 1; int endSecondExample = input.IndexOf("\"", startSecondExample + 1, StringComparison.Ordinal) + 1; if (startFirstExample >= endFirstExample || startSecondExample >= endSecondExample) { throw new Exception( "Invalid example format. Please try again. input and out should be between quotes"); } string inputExample = input.Substring(startFirstExample, endFirstExample - startFirstExample - 1); string outputExample = input.Substring(startSecondExample, endSecondExample - startSecondExample - 1); State inputState = State.CreateForExecution(Grammar.InputSymbol, inputExample); Examples.Add(inputState, outputExample); } } catch (Exception) { throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
internal Program(ProgramNode node) : base(node, node.GetFeatureValue(Learner.Instance.ScoreFeature)) { }
public void Visit(ProgramNode tree) { Console.WriteLine("generating target code"); }