/// <summary>
        /// Implements Tset.OptimalSubset(Tnum fcn).
        /// </summary>
        private static Tset OptimalSubsetCore(Tset theSet, Func <Tset, Tnum> fcn)
        {
            Tset result = new Tset();

            // For each time period in the Tset
            for (int i = 0; i < theSet.IntervalValues.Count; i++)
            {
                // Get some useful values
                Hval     thisSetVal = theSet.IntervalValues.Values[i];
                DateTime start      = theSet.IntervalValues.Keys[i];

                // Handle uncertainty of theSet
                if (!thisSetVal.IsKnown)
                {
                    result.AddState(start, thisSetVal);
                }
                else
                {
                    // Date parameters and set members of that time interval
                    List <Thing> mems = (List <Thing>)thisSetVal.Val;
                    DateTime     end  = Time.EndOf;
                    try { end = theSet.IntervalValues.Keys[i + 1]; } catch {}

                    // For each combination of set members, get the associated fcn val
                    List <Tuple <Tset, Tnum> > setFcnVals = new List <Tuple <Tset, Tnum> >();
                    Tnum maxVal = new Tnum(Decimal.MinValue);
                    foreach (Tset s in Combos(mems))
                    {
                        // Invoke the fcn for that subset
                        Tnum val = fcn(s);

                        // Save the result of the fcn and the associated Tset
                        setFcnVals.Add(Tuple.Create(s, val));

                        // Update the running maximum value
                        maxVal = Max(maxVal, val);
                    }

                    // Foreach changepoint in maxVal, find the associated Tset
                    for (int j = 0; j < maxVal.IntervalValues.Count; j++)
                    {
                        DateTime mDate = maxVal.IntervalValues.Keys[j];
                        if (mDate >= start && mDate < end)
                        {
                            // Get the associated Tset
                            Hval outSet = AssociatedTset(setFcnVals, maxVal, mDate);

                            // Add the change point
                            result.AddState(mDate, outSet);
                        }
                    }
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a Tset of all Things in the asserted facts.
        /// </summary>
        public static Tset AllKnownThings()
        {
            Tset result = new Tset();

            result.AddState(Time.DawnOf, new Hval(ThingBase));
            return(result);
        }
 /// <summary>
 /// Creates a Tset of all Things in the asserted facts.
 /// </summary>
 public static Tset AllKnownThings()
 {
     Tset result = new Tset();
     result.AddState(Time.DawnOf,new Hval(ThingBase));
     return result;
 }
        /// <summary>
        /// Implements Tset.OptimalSubset(Tnum fcn).
        /// </summary>
        private static Tset OptimalSubsetCore(Tset theSet, Func<Tset,Tnum> fcn)
        {
            Tset result = new Tset();

            // For each time period in the Tset
            for (int i=0; i < theSet.IntervalValues.Count; i++)
            {
                // Get some useful values
                Hval thisSetVal = theSet.IntervalValues.Values[i];
                DateTime start = theSet.IntervalValues.Keys[i];

                // Handle uncertainty of theSet
                if (!thisSetVal.IsKnown)
                {
                    result.AddState(start, thisSetVal);
                }
                else
                {
                    // Date parameters and set members of that time interval
                    List<Thing> mems = (List<Thing>)thisSetVal.Val;
                    DateTime end = Time.EndOf;
                    try {end = theSet.IntervalValues.Keys[i+1];} catch {}

                    // For each combination of set members, get the associated fcn val
                    List<Tuple<Tset,Tnum>> setFcnVals = new List<Tuple<Tset,Tnum>>();
                    Tnum maxVal = new Tnum(Decimal.MinValue);
                    foreach (Tset s in Combos(mems))
                    {
                        // Invoke the fcn for that subset
                        Tnum val = fcn(s);

                        // Save the result of the fcn and the associated Tset
                        setFcnVals.Add(Tuple.Create(s, val));

                        // Update the running maximum value
                        maxVal = Max(maxVal, val);
                    }

                    // Foreach changepoint in maxVal, find the associated Tset
                    for (int j=0; j < maxVal.IntervalValues.Count; j++)
                    {
                        DateTime mDate = maxVal.IntervalValues.Keys[j];
                        if (mDate >= start && mDate < end)
                        {
                            // Get the associated Tset
                            Hval outSet = AssociatedTset(setFcnVals, maxVal, mDate);

                            // Add the change point
                            result.AddState(mDate, outSet);
                        }
                    }
                }
            }

            return result;
        }
Beispiel #5
0
        /// <summary>
        /// Asserts a given fact (of the proper Tvar type)
        /// </summary>
        private static void AssertFact(Factoid f)
        {
            // Instantiate relevant Things
            Thing t1 = f.Arg1.ToString() != "" ? Facts.AddThing(f.Arg1.ToString()) : null;
            Thing t2 = f.Arg2.ToString() != "" ? Facts.AddThing(f.Arg2.ToString()) : null;
            Thing t3 = f.Arg3.ToString() != "" ? Facts.AddThing(f.Arg3.ToString()) : null;

            // Sometimes I have my doubts about static typing...
            if (f.FactType == "Tbool")
            {
                Tbool val = new Tbool();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tnum")
            {
                Tnum val = new Tnum();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tstr")
            {
                Tstr val = new Tstr();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tdate")
            {
                Tdate val = new Tdate();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tset")
            {
                Tset val = new Tset();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
        }