コード例 #1
0
        /// <summary>
        /// Returns true when a condition holds for at least one member of
        /// a given set.
        /// </summary>
        public Tbool Exists(Func <Thing, Tbool> argumentFcn)
        {
            // Analyze set for existence of the condition
            Tset subset = this.Filter(x => argumentFcn(x));

            return(subset.Count > 0);
        }
コード例 #2
0
ファイル: Tset.cs プロジェクト: mkpace/hammurabi
 /// <summary>
 /// Constructs a Tset from an existing Tset.
 /// </summary>
 public Tset(Tset s)
 {
     for (int i = 0; i < s.TimeLine.Count; i++)
     {
         this.AddState(s.TimeLine.Keys[i], s.TimeLine.Values[i]);
     }
 }
コード例 #3
0
ファイル: Tset.cs プロジェクト: mpoulshock/hammurabi
 /// <summary>
 /// Constructs a Tset from an existing Tset. 
 /// </summary>
 public Tset(Tset s)
 {
     for (int i=0; i<s.TimeLine.Count; i++)
     {
         this.AddState(s.TimeLine.Keys[i], s.TimeLine.Values[i]);
     }
 }
コード例 #4
0
ファイル: Inputs - entities.cs プロジェクト: mkpace/hammurabi
        /// <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);
        }
コード例 #5
0
        /// <summary>
        /// Applies an aggregation function to a Tset and an argument function.
        /// </summary>
        private static T ApplyFcnToTset <T>(Tset theSet,
                                            Func <Thing, Tvar> argumentFcn,
                                            Func <List <Tuple <Thing, Hval> >, Hval> aggregationFcn) where T : Tvar
        {
            Dictionary <Thing, Tvar> fcnValues = new Dictionary <Thing, Tvar>();
            List <Tvar> listOfTvars            = new List <Tvar>();

            // Get the temporal value of each distinct entity in the set
            foreach (Thing le in DistinctEntities(theSet))
            {
                Tvar val = argumentFcn(le);
                fcnValues.Add(le, val);
                listOfTvars.Add(val);
            }

            // At each breakpoint, for each member of the set,
            // aggregate and analyze the values of the functions
            T result = (T)Util.ReturnProperTvar <T>();

            foreach (DateTime dt in AggregatedTimePoints(theSet, listOfTvars))
            {
                Hval membersOfSet = theSet.ObjectAsOf(dt);

                // If theSet is unknown...
                if (!membersOfSet.IsKnown)
                {
                    result.AddState(dt, membersOfSet);
                }
                else
                {
                    // Cube that gets sent to the aggregation function
                    List <Tuple <Thing, Hval> > thingValPairs = new List <Tuple <Thing, Hval> >();

                    // Values to check for uncertainty
                    List <Hval> values = new List <Hval>();

                    foreach (Thing le in (List <Thing>)membersOfSet.Val)
                    {
                        Tvar funcVal   = (Tvar)fcnValues[le];
                        Hval funcValAt = funcVal.ObjectAsOf(dt);
                        values.Add(funcValAt);
                        thingValPairs.Add(new Tuple <Thing, Hval>(le, funcValAt));
                    }

                    Hstate top = PrecedingState(values);
                    if (top != Hstate.Known)
                    {
                        result.AddState(dt, new Hval(null, top));
                    }
                    else
                    {
                        result.AddState(dt, aggregationFcn(thingValPairs));
                    }
                }
            }

            return(result.LeanTvar <T>());
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        /// <summary>
        /// Returns true when a condition holds for all members of
        /// a given set.
        /// </summary>
        public Tbool ForAll(Func <Thing, Tbool> argumentFcn)
        {
            // By convention, the universal quantification of an empty set is always true
            // http://en.wikipedia.org/wiki/Universal_quantification#The_empty_set
            if (this.Count == 0)
            {
                return(new Tbool(true));
            }

            // Analyze set for universality of the condition
            Tset subset = this.Filter(x => argumentFcn(x));

            return(this.Count == subset.Count);
        }
コード例 #8
0
        /// <summary>
        /// Returns a list of all legal entities that were ever members of the
        /// set.
        /// </summary>
        private static List <Thing> DistinctEntities(Tset theSet)
        {
            List <Thing> result = new List <Thing>();

            foreach (KeyValuePair <DateTime, Hval> de in theSet.TimeLine)
            {
                if (de.Value.IsKnown)
                {
                    foreach (Thing le in (List <Thing>)de.Value.Val)
                    {
                        if (!result.Contains(le))
                        {
                            result.Add(le);
                        }
                    }
                }
            }

            return(result);
        }
コード例 #9
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;
 }
コード例 #10
0
 /// <summary>
 /// Private method that aggregates all time points among a Tset and one
 /// or more Tvars
 /// </summary>
 private static List <DateTime> AggregatedTimePoints(Tset theSet, List <Tvar> listOfTvars)
 {
     listOfTvars.Add(theSet);
     return(TimePoints(listOfTvars));
 }
コード例 #11
0
        /// <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;
        }
コード例 #12
0
ファイル: Tset.cs プロジェクト: mpoulshock/hammurabi
 /// <summary>
 /// Returns true when one Tset is a subset of another. 
 /// </summary>
 public Tbool IsSubsetOf(Tset super)
 {
     return ApplyFcnToTimeline<Tbool>(x => CoreSubset(x), this, super);
 }
コード例 #13
0
ファイル: Tset.cs プロジェクト: mkpace/hammurabi
 /// <summary>
 /// Returns true when one Tset is a subset of another.
 /// </summary>
 public Tbool IsSubsetOf(Tset super)
 {
     return(ApplyFcnToTimeline <Tbool>(x => CoreSubset(x), this, super));
 }
コード例 #14
0
ファイル: Service.svc.cs プロジェクト: mpoulshock/hammurabi
        /// <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);
            }
        }