Beispiel #1
0
        public void TestFindActiveIdentifiers()
        {
            // Build up a tree with OR node as root, child a (OR node) for value 0, child b (value node) for value 1.
            // Create the nodes.
            IParameterTreeNode a = new OrNode <int>("a", new CategoricalDomain <int>(new List <int> {
                2, 5
            }));
            IParameterTreeNode b = new ValueNode <int>("b", new IntegerDomain());
            var rootDecision     = new OrNode <int>("or", new CategoricalDomain <int>(new List <int> {
                0, 1
            }));

            // Create connections.
            rootDecision.AddChild(0, a);
            rootDecision.AddChild(1, b);
            var parameterTree = new ParameterTree(rootDecision);

            // Set value for roort node s.t. only node a should be active.
            var values = new Dictionary <string, IAllele>(3)
            {
                { "a", new Allele <int>(2) },
                { "b", new Allele <int>(7) },
                { "or", new Allele <int>(0) },
            };

            var activeIdentifiers         = parameterTree.FindActiveIdentifiers(values.ToImmutableDictionary()).ToList();
            var expectedActiveIdentifiers = new List <string> {
                "or", "a"
            };

            Assert.True(
                TestUtils.SetsAreEquivalent(activeIdentifiers, expectedActiveIdentifiers),
                $"Active identifiers should be {TestUtils.PrintList(expectedActiveIdentifiers)}, but are {TestUtils.PrintList(activeIdentifiers)}.");
        }
Beispiel #2
0
        public void GetFilteredGenesHandlesOrNodesCorrectly()
        {
            // Build up a tree with OR node as root, child a (OR node) for value 0, child b (value node) for value 1.
            // Create the nodes.
            IParameterTreeNode a = new OrNode <int>("a", new CategoricalDomain <int>(new List <int> {
                2, 5
            }));
            IParameterTreeNode b            = new ValueNode <int>("b", new IntegerDomain());
            OrNode <int>       rootDecision = new OrNode <int>("or", new CategoricalDomain <int>(new List <int> {
                0, 1
            }));

            // Create connections.
            rootDecision.AddChild(0, a);
            rootDecision.AddChild(1, b);

            var parameterTree = new ParameterTree(rootDecision);

            this._genome.SetGene("a", new Allele <int>(2));
            this._genome.SetGene("b", new Allele <int>(7));
            this._genome.SetGene("or", new Allele <int>(0));

            var filteredGenes         = this._genome.GetFilteredGenes(parameterTree);
            var expectedFilteredGenes = new List <string> {
                "or", "a"
            };

            Assert.True(
                TestUtils.SetsAreEquivalent(filteredGenes.Keys, expectedFilteredGenes),
                $"Filtered genes should be {TestUtils.PrintList(expectedFilteredGenes)}, but are {TestUtils.PrintList(filteredGenes.Keys)}.");
        }
Beispiel #3
0
        /// <summary>
        /// Builds the following parameter tree:
        /// - AND node as root
        /// - 1st child of AND node: OR node with string either a or b (default: "a")
        /// - 2nd child of AND node: value node with integer between 1 and 5 (default: 1)
        /// - OR node, a branch: value node with integer (default: 42).
        /// - OR node, b branch: value node with double between 0.1 and 0.8 (default: 0.2).
        /// </summary>
        /// <param name="includeDefaultValues">Indicates whether to add default values to the domains.</param>
        /// <returns>The build parameter tree.</returns>
        private static ParameterTree BuildParameterTree(bool includeDefaultValues = false)
        {
            // Create all parameter tree nodes.
            var             rootNode       = new AndNode();
            OrNode <string> decideAOrBNode = new OrNode <string>(
                DecisionParameter,
                new CategoricalDomain <string>(new List <string> {
                "a", "b"
            }, includeDefaultValues ? new Allele <string>("a") : (Allele <string>?)null));

            IParameterNode integerParamNode = new ValueNode <int>(
                GenomeBuilderTest.DiscreteParameter,
                new IntegerDomain(defaultValue: includeDefaultValues ? new Allele <int>(42) : (Allele <int>?)null));
            IParameterNode continuousParamNode = new ValueNode <double>(
                GenomeBuilderTest.ContinuousParameter,
                new ContinuousDomain(0.1, 0.8, includeDefaultValues ? new Allele <double>(0.2) : (Allele <double>?)null));

            IParameterNode smallIntegerParamNode = new ValueNode <int>(
                GenomeBuilderTest.SmallValueParameter,
                new IntegerDomain(1, 5, includeDefaultValues ? new Allele <int>(1) : (Allele <int>?)null));

            // Connect them.
            decideAOrBNode.AddChild("a", integerParamNode);
            decideAOrBNode.AddChild("b", continuousParamNode);
            rootNode.AddChild(decideAOrBNode);
            rootNode.AddChild(smallIntegerParamNode);

            // Return tree.
            return(new ParameterTree(rootNode));
        }
        /// <summary>
        /// Creates a parameter tree which root is an OR node "decision". If that is true, a value node "a" is
        /// evaluated, otherwise a value node "b".
        /// </summary>
        /// <returns>The created parameter tree.</returns>
        private static ParameterTree CreateParameterTree()
        {
            var decisionNode =
                new OrNode <bool>("decision", new CategoricalDomain <bool>(new List <bool> {
                true, false
            }));

            decisionNode.AddChild(true, new ValueNode <double>("a", new ContinuousDomain()));
            decisionNode.AddChild(false, new ValueNode <double>("b", new ContinuousDomain()));
            return(new ParameterTree(decisionNode));
        }
        /// <summary>
        /// Converts this node to an <see cref="OrNode{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of values the represented parameter can take.</typeparam>
        /// <returns>The converted <see cref="OrNode{T}"/>.</returns>
        /// <exception cref="XmlException">Thrown if the object was read from XML in such a way that it
        /// does not represent a valid <see cref="IParameterTreeNode"/> object.</exception>
        protected override IParameterTreeNode ConvertToParameterTreeNode<T>()
        {
            // Cast domain to correct type.
            CategoricalDomain<T> categoricalDomain =
                this.domain.ConvertToParameterTreeDomain() as CategoricalDomain<T>;
            if (categoricalDomain == null)
            {
                throw new XmlException(
                    $"Domain of OR node '{this.id}' was not of type {typeof(CategoricalDomain<T>)} as expected.");
            }

            // Create OR node.
            var node = new OrNode<T>(this.id, categoricalDomain);

            // Add children:
            foreach (var choice in this.choice)
            {
                // Check activator is of correct type.
                if (!(choice.Item is T))
                {
                    throw new XmlException(
                        $"OR node '{this.id}' had a choice of type {choice.Item.GetType()} instead of {typeof(T)}.");
                }

                // Add child.
                node.AddChild((T)choice.Item, choice.child.ConvertToParameterTreeNode());
            }

            return node;
        }
Beispiel #6
0
        /// <summary>
        /// Builds the following parameter tree:
        /// - AND node as root
        /// - 1st child of AND node: OR node with string either a or b
        /// - 2nd child of AND node: value node with integer between 1 and 5
        /// - OR node, a branch: value node with integer
        /// - OR node, b branch: value node with double between 0.1 and 0.8.
        /// </summary>
        /// <returns>The build parameter tree.</returns>
        private static ParameterTree BuildParameterTree()
        {
            // Create all parameter tree nodes.
            var             rootNode       = new AndNode();
            OrNode <string> decideAOrBNode = new OrNode <string>(
                DecisionParameter,
                new CategoricalDomain <string>(new List <string> {
                "a", "b"
            }));
            IParameterNode smallIntegerParamNode = new ValueNode <int>(GenomeBuilderTest.SmallValueParameter, new IntegerDomain(1, 5));
            IParameterNode integerParamNode      = new ValueNode <int>(GenomeBuilderTest.DiscreteParameter, new IntegerDomain());
            IParameterNode continuousParamNode   = new ValueNode <double>(
                GenomeBuilderTest.ContinuousParameter,
                new ContinuousDomain(0.1, 0.8));

            // Connect them.
            decideAOrBNode.AddChild("a", integerParamNode);
            decideAOrBNode.AddChild("b", continuousParamNode);
            rootNode.AddChild(decideAOrBNode);
            rootNode.AddChild(smallIntegerParamNode);

            // Return tree.
            return(new ParameterTree(rootNode));
        }
Beispiel #7
0
        public void GetChildWorksIfChildExists()
        {
            var decisionNode = new OrNode <string>(
                "test",
                new CategoricalDomain <string>(new List <string> {
                "a"
            }));
            var childNode = new ValueNode <int>("value", new IntegerDomain());

            decisionNode.AddChild("a", childNode);

            IParameterTreeNode returnedChildNode;

            Assert.True(decisionNode.TryGetChild("a", out returnedChildNode), "Child should be found.");
            Assert.Equal(childNode, returnedChildNode);
        }