Exemplo n.º 1
0
        public Dict <DateTime, string, double[]> Run(Dict <DateTime, string, VertexEvidence> evidences)
        {
            var beliefs      = new Dict <DateTime, string, double[]>();
            var lastDateTime = evidences.Keys[0];

            foreach (var dateTime in evidences.Keys)
            {
                if (lastDateTime == dateTime)
                {
                    beliefs[dateTime] = this.Run(evidences[dateTime]);
                }
                else
                {
                    foreach (var dstNodeKey in this.Loops.Keys)
                    {
                        evidences[dateTime][dstNodeKey] = new VertexEvidence
                        {
                            Type  = VertexEvidenceType.Distribution,
                            Value = beliefs[lastDateTime][this.Loops[dstNodeKey]]
                        };
                    }

                    beliefs[dateTime] = this.Run(evidences[dateTime]);
                }

                lastDateTime = dateTime;
            }

            return(beliefs);
        }
Exemplo n.º 2
0
 public void SetEvidence(VertexEvidence anEvidence)
 {
     if (anEvidence != null)
     {
         this.Evidence       = anEvidence.Value;
         this.EvidenceString = anEvidence.ToString();
     }
     else
     {
         this.Evidence       = null;
         this.EvidenceString = null;
     }
 }
Exemplo n.º 3
0
        public static void SetEvidence(this ILineData lineData, string sectionId, int year, string vertexKey, VertexEvidence vertexEvidence)
        {
            var sectionEvidence = lineData.GetEvidence(sectionId);

            if (vertexEvidence.Type == VertexEvidenceType.Null)
            {
                sectionEvidence[year][vertexKey] = null;
            }
            else
            {
                sectionEvidence[year][vertexKey] = vertexEvidence;
            }

            lineData.SetEvidence(sectionId, sectionEvidence);
        }
Exemplo n.º 4
0
        public static VertexEvidence ParseEvidenceString(this IEnumerable <State> states, string anEvidenceString)
        {
            if (string.IsNullOrWhiteSpace(anEvidenceString))
            {
                return(new VertexEvidence
                {
                    Type = VertexEvidenceType.Null
                });
            }


            var stateList = states as IList <State> ?? states.ToList();

            // Check if string is the label of any of the states.
            if (stateList.Any(state => state.Key == anEvidenceString))
            {
                var evidencePar = Enumerable.Range(1, stateList.Count + 1).Select(i => (double)i).ToArray(); // to display labelled nodes on chart
                return(new VertexEvidence
                {
                    Value = stateList.Select(state => state.Key == anEvidenceString ? 1.0 : 0.0).Normalized().ToArray(),
                    Type = VertexEvidenceType.State,
                    StateKey = stateList.Where(state => state.Key == anEvidenceString).Select(state => state.Key).First(),
                    Params = evidencePar,
                });
            }

            double value;

            if (!anEvidenceString.Contains(",") && double.TryParse(anEvidenceString, out value) && stateList.GetSafeMin() <= value && value <= stateList.GetSafeMax())
            {
                return(new VertexEvidence
                {
                    Value = stateList.ParseEvidence(new DeltaDistribution(value)).Select(doubleVal => Math.Round(doubleVal, 2)).ToArray(),
                    Type = VertexEvidenceType.Number,
                    Params = new[]
                    {
                        value
                    },
                });
            }

            var evidenceParams = VertexEvidence.ParseEvidenceParams(anEvidenceString);
            var evidenceType   = VertexEvidenceType.Invalid;

            double[] evidence = null;

            // Check for functions
            if (anEvidenceString.ToLowerInvariant().Contains("tri") && evidenceParams.Count == 3)
            {
                evidenceParams.Sort();

                evidence     = stateList.ParseEvidence(new TriangularDistribution(evidenceParams[0], evidenceParams[1], evidenceParams[2])).Select(doubleVal => Math.Round(doubleVal, 2)).ToArray();
                evidenceType = VertexEvidenceType.Triangular;
            }

            else if (anEvidenceString.ToLowerInvariant().Contains("norm") && evidenceParams.Count == 2 && evidenceParams[1] > 0)
            {
                evidence     = stateList.ParseEvidence(new NormalDistribution(evidenceParams[0], evidenceParams[1])).Select(doubleVal => Math.Round(doubleVal, 2)).ToArray();
                evidenceType = VertexEvidenceType.Normal;
            }

            else if (anEvidenceString.Contains(":") && evidenceParams.Count == 2)
            {
                evidenceParams.Sort();

                evidence     = stateList.ParseEvidence(new UniformDistribution(evidenceParams[0], evidenceParams[1])).Select(doubleVal => Math.Round(doubleVal, 2)).ToArray();
                evidenceType = VertexEvidenceType.Range;
            }

            else if (evidenceParams.Count == stateList.Count)
            {
                evidence     = evidenceParams.Normalized().Select(doubleVal => Math.Round(doubleVal, 2)).ToArray();
                evidenceType = VertexEvidenceType.Distribution;
            }

            return(new VertexEvidence
            {
                Value = evidence,
                Type = evidenceType,
                Params = evidenceParams.ToArray(),
            });
        }