A Template
contains the specification of how a parse tree is allowed to grow. Using the template system, we can ensure that the parse tree grows as desired, and it doesn't need to spend computing resources on fixing incorrect genetic programs.
The lookup name has two components, domain name, and attribute name.
The domain name specifies the hierarchy relationship in the hierarchy tree. For example, the Greater
node has a domain name of Operator.BinaryOperator.BinaryRelationalOperator.Greater
. It means the Greater
object extends the BinaryRelationalOperator
abstract class. Similarly, BinaryRelationalOperator
extends BinaryOperator
, and BinaryOperator
extends Operator
. The domain name is required in every lookup name.
Attribute names are optional. When it exists, it must be the last section in a lookup name, and it must start with a lower-case letter. For example, in the lookup name Operator.BinaryOperator.BinaryRelationalOperator.Greater.lhs
, lhs
is the attribute name. It indicates it is the left-hand side of the Greater
node. The detail attribute name for different classes may differ. For example, an IfThenElse
object (Statement.IfThenElse
) has three attributes: ifNode
, thenNode
, and elseNode
.
The lookup is recursive. If you do not specify for at a given level, for example, Operator.BinaryOperator.BinaryRelationalOperator.Greater.lhs
, it will look up recursively until a valid entry is found, for example, Operator.BinaryOperator.BinaryRelationalOperator.lhs
.
The following code is part of the example of a GP-based trading bot. In the example,
- We can place more prototypes of a certain type to increase the probability of it being chosen.
Value
s only appear on the right-hand side.
% Instantiate an object
template = Template();
% The options for the root of the parse tree
template.set("Root", { ...
IfThenElse(), ...
});
template.set("Operator.BinaryOperator.BinaryRelationalOperator.lhs", { ...
Variable("RSI", 'bounded', 'double', 0, 100), ...
Variable("EMA5", 'none', 'double'), ...
Variable("EMA20", 'none', 'double'), ...
Variable("MACD", 'bounded', 'double', ...
min(trainingSet{:, 'MACD'}), max(trainingSet{:, 'MACD'})), ...
Variable("MACDSignal", 'none', 'double'), ...
Variable("Bid", 'none', 'double'), ...
Variable("Return5", 'bounded', 'double', ...
min(trainingSet{:, 'Return5'}), max(trainingSet{:, 'Return5'})), ...
Variable("Return20", 'bounded', 'double', ...
min(trainingSet{:, 'Return20'}), max(trainingSet{:, 'Return20'})), ...
});
template.set("Operator.BinaryOperator.BinaryRelationalOperator.rhs", { ...
Value(), Value(), Value(), Value(), Value(), Value(), ...
Variable("EMA5", 'none', 'double'), ...
Variable("EMA20", 'none', 'double'), ...
Variable("MACD", 'bounded', 'double', min(trainingSet{:, 'MACD'}), max(trainingSet{:, 'MACD'})), ...
Variable("MACDSignal", 'none', 'double'), ...
});
template.set("Operator.BinaryOperator.BinaryLogicalOperator.lhs", { ...
GreaterEqual(), LessEqual(), And(), Or()...
});
template.set("Operator.BinaryOperator.BinaryLogicalOperator.rhs", { ...
GreaterEqual(), LessEqual(), And(), Or() ...
});
template.set("Statement.IfThenElse.ifNode", { ...
GreaterEqual(), LessEqual(), And(), Or() ...
});
template.set("Statement.IfThenElse.thenNode", { ...
IfThenElse(), IfThenElse(), EnumeratedSignal({1, 2, 3})});
template.set("Statement.IfThenElse.elseNode", { ...
IfThenElse(), IfThenElse(), EnumeratedSignal({1, 2, 3})});
The constructor of a Template
object takes in no argument. Or, it can take in a containers.Map()
, which contains the key-value-pair specification of the template, as input.
The class contains the following methods:
Signature | Description |
---|---|
set(tpl, key, val) |
Set a key for the template. |
extend(tpl, key, val) |
Add a value to an entry of the template. |
Note: The table omits methods used internally in the system. |
Set a key for the template.
Argument | Description |
---|---|
key |
The lookup name of a position. |
val |
A cell of objects. |
Add a value to an entry of the template.
Argument | Description |
---|---|
key |
The lookup name of a position. |
val |
A cell of objects. |
This project, including the documentation, is licensed under the MIT license. Copyright (c) 2022 Bohui WU.