/// <summary> /// Construct a string expression. /// </summary> /// <param name="theValue">The value to construct.</param> public ExpressionValue(String theValue) { _stringValue = theValue; _expressionType = EPLValueType.StringType; _boolValue = false; _floatValue = 0; _intValue = 0; _enumType = -1; }
/// <summary> /// Define a variable. /// </summary> /// <param name="theName">The name of the variable.</param> /// <param name="theVariableType">The type of variable.</param> /// <param name="theEnumType">The enum type, not used if not an enum type.</param> /// <param name="theEnumValueCount"> /// The number of values for the enum, not used if not an enum /// type. /// </param> public void DefineVariable(String theName, EPLValueType theVariableType, int theEnumType, int theEnumValueCount) { var mapping = new VariableMapping(theName, theVariableType, theEnumType, theEnumValueCount); DefineVariable(mapping); }
/// <summary> /// Construct a variable mapping. /// </summary> /// <param name="theName">The name of the variable.</param> /// <param name="theVariableType">The type of the variable.</param> /// <param name="theEnumType">The enum type.</param> /// <param name="theEnumValueCount">The number of values for an enum.</param> public VariableMapping(String theName, EPLValueType theVariableType, int theEnumType, int theEnumValueCount) { _name = theName; _variableType = theVariableType; _enumType = theEnumType; _enumValueCount = theEnumValueCount; }
/// <summary> /// Construct a value of the specified type. /// </summary> /// <param name="theType">The value to construct.</param> public ExpressionValue(EPLValueType theType) { _expressionType = theType; _intValue = 0; _boolValue = false; _floatValue = 0; _stringValue = null; _enumType = -1; }
/// <summary> /// Construct a boolean expression. /// </summary> /// <param name="theValue">The value to construct.</param> public ExpressionValue(double theValue) { _floatValue = theValue; _expressionType = EPLValueType.FloatingType; _boolValue = false; _stringValue = null; _intValue = 0; _enumType = -1; }
/// <summary> /// Construct an integer expression. /// </summary> /// <param name="theValue">The value to construct.</param> public ExpressionValue(long theValue) { _intValue = theValue; _expressionType = EPLValueType.IntType; _boolValue = false; _floatValue = 0; _stringValue = null; _enumType = -1; }
/// <summary> /// Construct a boolean expression. /// </summary> /// <param name="theValue">The value to construct.</param> public ExpressionValue(bool theValue) { _boolValue = theValue; _expressionType = EPLValueType.BooleanType; _floatValue = 0; _stringValue = null; _intValue = 0; _enumType = -1; }
/// <inheritdoc /> public bool IsPossibleReturnType(EncogProgramContext context, EPLValueType rtn) { if (_delIsPossibleReturnType == null) { return(_returnValue.PossibleTypes.Contains(rtn)); } if (!_returnValue.PossibleTypes.Contains(rtn)) { return(false); } return(_delIsPossibleReturnType(context, rtn)); }
/// <summary> /// Construct a expression based on an expression. /// </summary> /// <param name="other">The value to construct.</param> public ExpressionValue(ExpressionValue other) { switch (_expressionType = other._expressionType) { case EPLValueType.BooleanType: _boolValue = other._boolValue; _floatValue = 0; _stringValue = null; _intValue = 0; _enumType = -1; break; case EPLValueType.FloatingType: _floatValue = other._floatValue; _boolValue = false; _stringValue = null; _intValue = 0; _enumType = -1; break; case EPLValueType.IntType: _intValue = other._intValue; _boolValue = false; _floatValue = 0; _stringValue = null; _enumType = -1; break; case EPLValueType.StringType: _stringValue = other._stringValue; _boolValue = false; _floatValue = 0; _intValue = 0; _enumType = -1; break; case EPLValueType.EnumType: _intValue = other._intValue; _boolValue = false; _floatValue = 0; _stringValue = null; _enumType = other._enumType; break; default: throw new EARuntimeError("Unsupported type."); } }
private String RenderNode(ProgramNode node) { StringBuilder result = new StringBuilder(); for (int i = 0; i < node.ChildNodes.Count; i++) { ProgramNode childNode = node.GetChildNode(i); result.Append(RenderNode(childNode)); } result.Append('['); result.Append(node.Name); result.Append(':'); result.Append(node.Template.ChildNodeCount); for (int i = 0; i < node.Template.DataSize; i++) { result.Append(':'); EPLValueType t = node.Data[i].ExprType; if (t == EPLValueType.BooleanType) { result.Append(node.Data[i].ToBooleanValue() ? 't' : 'f'); } else if (t == EPLValueType.FloatingType) { result.Append(CSVFormat.EgFormat.Format(node.Data[i].ToFloatValue(), EncogFramework.DefaultPrecision)); } else if (t == EPLValueType.IntType) { result.Append(node.Data[i].ToIntValue()); } else if (t == EPLValueType.EnumType) { result.Append(node.Data[i].EnumType); result.Append("#"); result.Append(node.Data[i].ToIntValue()); } else if (t == EPLValueType.StringType) { result.Append("\""); result.Append(node.Data[i].ToStringValue()); result.Append("\""); } } result.Append(']'); return(result.ToString().Trim()); }
/// <summary> /// Construct a variable mapping for a non-enum type. /// </summary> /// <param name="theName">The variable name.</param> /// <param name="theVariableType">The variable type.</param> public VariableMapping(String theName, EPLValueType theVariableType) : this(theName, theVariableType, 0, 0) { }
/// <summary> /// Define the specified variable as the specified type. Don't use this for /// enums. /// </summary> /// <param name="theName">The name of the variable.</param> /// <param name="theVariableType">The variable type.</param> public void DefineVariable(String theName, EPLValueType theVariableType) { DefineVariable(theName, theVariableType, 0, 0); }
/// <inheritdoc /> public bool IsPossibleReturnType(EncogProgramContext context, EPLValueType rtn) { if (_delIsPossibleReturnType == null) { return _returnValue.PossibleTypes.Contains(rtn); } if (!_returnValue.PossibleTypes.Contains(rtn)) { return false; } return _delIsPossibleReturnType(context, rtn); }
/// <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> /// Add a type using a type enum. /// </summary> /// <param name="theType">The type to add.</param> public void AddType(EPLValueType theType) { _possibleTypes.Add(theType); }