示例#1
0
 private IEnumerable <Weighted <T1> > GetEnumerator <T1>(Uncertain <T1> fromsource)
 {
     while (true)
     {
         this.generation++;
         fromsource.Accept(this);
         yield return((Weighted <T1>) this.sample);
     }
 }
示例#2
0
        private IEnumerable <Weighted <T1> > GetEnumerator <T1>(Uncertain <T1> uncertain)
        {
            /// TODO: I am still building a caching mechanism!
            var regenFrom = 0;

            RandomPrimitive sampled   = null;
            var             initstack = this.stack;
            var             sampler   = new SingleSampler();

            do
            {
                this.stack = initstack;

                // Run the program.
                uncertain.Accept(this);

                //var samples = (from e in this.trace select e.Sample).ToArray();
                //int count;
                //if (!this.cache.TryGetValue(samples, out count))
                //{
                //    count = 0;
                //}
                //else
                //{
                //    count = count;
                //}
                //this.cache[samples] = count + 1;

                // TODO: should we return 0 mass samples?
                //       0 IS a reasonable weight if all
                //       one wants to do is know about a
                //       posterior - need to compute the score
                //       from the Weighted<T1> object rather
                //       than the trace.
                var returnval = (Weighted <T1>) this.sample;

                //if (returnval.Probability > 0)
                //    yield return returnval;
                yield return(returnval);

                var roll       = Extensions.NextRandom();
                var acceptance = Accept(trace, oldTrace, regenFrom);
                if (this.generation > 1 && !(Math.Log(roll) < acceptance))
                {
                    // rollback proposal
                    trace.Clear();
                    trace.AddRange(oldTrace);
                }

                Swap(ref trace, ref oldTrace);
                if (oldTrace.Count > 0)
                {
                    // A programmer induced dependence implies the trace can have
                    // more than one copy of any given Random Primitive
                    // only sample from the set of distinct RandomPrimitives
                    // to avoid oversampling any particular RandomPrimitive.
                    //var dict = new Dictionary<RandomPrimitive, IList<int>>();
                    //var pos = 0;
                    //foreach(var e in oldTrace)
                    //{
                    //    IList<int> lst;
                    //    if (! dict.TryGetValue(e.Erp, out lst))
                    //    {
                    //        lst = new List<int>();
                    //        dict[e.Erp] = lst;
                    //    }
                    //    lst.Add(pos++);
                    //}
                    //regenFrom = (int)Math.Floor(Extensions.NextRandom() * dict.Keys.Count);
                    //sampled = dict.Keys.ElementAt(regenFrom);
                    var distinct = oldTrace.Select(e => e.Erp).Distinct().ToList();
                    regenFrom = (int)Math.Floor(Extensions.NextRandom() * distinct.Count);
                    sampled   = distinct[regenFrom];
                    // force regeneration of this random primitive
                    // note we reset this to false in the
                    // Visit(RandomPrimitive) method
                    sampled.ForceRegen = true;


                    sampled.Accept(sampler);
                    //var key = (from e in this.trace select e.Sample).ToArray();
                }
                trace.Clear();

                this.generation++;
            } while (true);
        }