Ejemplo n.º 1
0
        /// <summary>
        /// Parse a probability list. 
        /// </summary>
        /// <param name="network">The network to parse for.</param>
        /// <param name="line">The line to parse.</param>
        /// <returns>The parsed list.</returns>
        public static IList<ParsedProbability> ParseProbabilityList(BayesianNetwork network, String line)
        {
            IList<ParsedProbability> result = new List<ParsedProbability>();

            StringBuilder prob = new StringBuilder();
            for (int i = 0; i < line.Length; i++)
            {
                char ch = line[i];
                if (ch == ')')
                {
                    prob.Append(ch);
                    ParseProbability parse = new ParseProbability(network);
                    ParsedProbability parsedProbability = parse.Parse(prob.ToString());
                    result.Add(parsedProbability);
                    prob.Length = 0;
                }
                else
                {
                    prob.Append(ch);
                }
            }
            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parse a probability list.
        /// </summary>
        /// <param name="network">The network to parse for.</param>
        /// <param name="line">The line to parse.</param>
        /// <returns>The parsed list.</returns>
        public static IList <ParsedProbability> ParseProbabilityList(BayesianNetwork network, String line)
        {
            IList <ParsedProbability> result = new List <ParsedProbability>();

            StringBuilder prob = new StringBuilder();

            for (int i = 0; i < line.Length; i++)
            {
                char ch = line[i];
                if (ch == ')')
                {
                    prob.Append(ch);
                    ParseProbability  parse             = new ParseProbability(network);
                    ParsedProbability parsedProbability = parse.Parse(prob.ToString());
                    result.Add(parsedProbability);
                    prob.Length = 0;
                }
                else
                {
                    prob.Append(ch);
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Perform a query.
        /// </summary>
        /// <param name="line">The query.</param>
        /// <returns>The probability.</returns>
        public double PerformQuery(String line)
        {
            if (this.Query == null)
            {
                throw new BayesianError("This Bayesian network does not have a query to define.");
            }

            ParseProbability parse = new ParseProbability(this);
            ParsedProbability parsedProbability = parse.Parse(line);

            // create a temp query
            IBayesianQuery q = this.Query.Clone();

            // first, mark all events as hidden
            q.Reset();

            // deal with evidence (input)
            foreach (ParsedEvent parsedEvent in parsedProbability.GivenEvents)
            {
                BayesianEvent e = this.RequireEvent(parsedEvent.Label);
                q.DefineEventType(e, EventType.Evidence);
                q.SetEventValue(e, parsedEvent.ResolveValue(e));
            }

            // deal with outcome (output)
            foreach (ParsedEvent parsedEvent in parsedProbability.BaseEvents)
            {
                BayesianEvent e = RequireEvent(parsedEvent.Label);
                q.DefineEventType(e, EventType.Outcome);
                q.SetEventValue(e, parsedEvent.ResolveValue(e));
            }

            q.LocateEventTypes();

            q.Execute();
            return q.Probability;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Define a relationship.
 /// </summary>
 /// <param name="line">The relationship to define.</param>
 public void DefineRelationship(String line)
 {
     ParseProbability parse = new ParseProbability(this);
     ParsedProbability parsedProbability = parse.Parse(line);
     parsedProbability.DefineRelationships(this);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Define the probability for an event.
 /// </summary>
 /// <param name="line">The event.</param>
 /// <param name="probability">The probability.</param>
 public void DefineProbability(String line, double probability)
 {
     ParseProbability parse = new ParseProbability(this);
     ParsedProbability parsedProbability = parse.Parse(line);
     parsedProbability.DefineTruthTable(this, probability);
 }