Пример #1
0
        public static ValueOutcome[] NewOutcomes(Node vNode, Node eNode, Node[] newParents) //generate new outcomes wih extended parents
        {
            Node[]     parents     = vNode.Parents.Except(new Node[] { eNode }).Concat(newParents).Distinct().ToArray();
            string[]   parentNames = new string[0];
            string[][] outcomes    = new string[0][];

            foreach (Node parent in parents)
            {
                Array.Resize(ref parentNames, parentNames.Length + 1);
                parentNames[parentNames.Length - 1] = parent.Name;
                Array.Resize(ref outcomes, outcomes.Length + 1);
                outcomes[outcomes.Length - 1] = parent.Definitions;
            }

            var conditionCombinations     = CartesianCompute.CartesianProduct(outcomes);
            HashSet <ValueOutcome> values = new HashSet <ValueOutcome>();

            if (conditionCombinations != null && conditionCombinations.Count() > 0)
            {
                foreach (var comb in conditionCombinations)
                {
                    string[]     combArr = comb.ToArray();
                    ValueOutcome value   = new ValueOutcome(combArr.ToArray(), parents, 0);
                    values.Add(value);
                }
            }
            return(values.ToArray());
        }
Пример #2
0
        public static Probability[] ChildProbabilities(Node parent, Node child) // find child probabilities after relation turn
        {
            Node[]     parents     = child.Parents.Where(x => x != parent).ToArray();
            string[]   parentNames = new string[0];
            string[][] outcomes    = new string[][] { child.Definitions };

            bool hasEventParent = false;

            foreach (Node par in parents)
            {
                if (child.Type == NodeType.Event)
                {
                    hasEventParent = true;
                }
                Array.Resize(ref parentNames, parentNames.Length + 1);
                parentNames[parentNames.Length - 1] = par.Name;
                Array.Resize(ref outcomes, outcomes.Length + 1);
                outcomes[outcomes.Length - 1] = par.Definitions;
            }

            var conditionCombinations   = CartesianCompute.CartesianProduct(outcomes);
            HashSet <Probability> probs = new HashSet <Probability>();

            foreach (var comb in conditionCombinations)
            {
                string[]    combArr = comb.ToArray();
                Probability prob    = null;
                if (hasEventParent)
                {
                    prob = new Probability(new string[] { combArr[0] }, new Node[] { child }, combArr.Skip(1).ToArray(), parents);
                }
                else
                {
                    prob = new Probability(combArr, new Node[] { child }.Concat(parents).ToArray(), new string[0], new Node[0]);
                }
                Console.WriteLine("Probability: " + prob.ToString());
                probs.Add(prob);
            }
            return(probs.ToArray());
        }