Exemple #1
0
        public static List <Solution> DoEtcAbduction(List <Literal> obs, Knowledgebase kb, int maxdepth, bool sk)
        {
            var solutions = Abduction.DoAbduction(obs, kb, maxdepth, sk);

            solutions.Sort((x, y) => JointLnProbability(y).CompareTo(JointLnProbability(x))); // reverse
            return(solutions);
        }
Exemple #2
0
        public static List <Solution> ContextualEtcAbduction(List <Literal> obs, List <Literal> window, Knowledgebase kb, List <Solution> previous, int maxdepth, int beam, int iteration)
        {
            var ln_pr_to_beat = double.NegativeInfinity;
            var n_best        = new List <Solution>();
            var n_best_ln_pr  = new List <Double>();

            foreach (Solution previous_solution in previous)
            {
                var previous_solution_jlpr = JointLnProbability(previous_solution);
                var context       = GetContext(previous_solution, obs, kb);
                var list_of_lists = new List <List <Solution> >();
                foreach (Literal c in window)
                {
                    var remaining = new List <Literal>()
                    {
                        c
                    };
                    list_of_lists.Add(ContextualAndOrLeaflists(remaining, kb, maxdepth, context, new List <Literal>(), new List <Literal>()));
                }
                var combiner = new Combiner(list_of_lists);
                foreach (Solution s in combiner)
                {
                    if (BestCaseLnProbability(s) > ln_pr_to_beat) // maybe!
                    {
                        foreach (Solution solution in Abduction.Crunch(s))
                        {
                            var jlpr = JointLnProbability(solution) + previous_solution_jlpr; //add ln
                            if (jlpr > ln_pr_to_beat)
                            {
                                solution.AddRange(previous_solution); // important!
                                var insert_at = n_best_ln_pr.Count;
                                for (int i = 0; i < n_best_ln_pr.Count; i++)
                                {
                                    if (n_best_ln_pr[i] > jlpr)
                                    {
                                        insert_at = i;
                                        break;
                                    }
                                }
                                n_best.Insert(insert_at, solution);
                                n_best_ln_pr.Insert(insert_at, jlpr);
                                if (n_best.Count > beam)
                                {
                                    n_best.RemoveAt(0);
                                    n_best_ln_pr.RemoveAt(0);
                                    ln_pr_to_beat = n_best_ln_pr[0]; // second worst [0] is now lowest
                                }
                            }
                        }
                    }
                }
            }

            n_best.Reverse(); // 0 is now highest;
            var pre = "$" + iteration.ToString() + ":";
            var res = new List <Solution>();

            foreach (Solution c in n_best)
            {
                res.Add(skolemize_with_prefix(c, pre));
            }
            return(res);
        }
Exemple #3
0
        public static List <Solution> NBest(List <Literal> obs, Knowledgebase kb, int maxdepth, int n, bool sk)
        {
            var ln_pr_to_beat = double.NegativeInfinity;
            var n_best        = new List <Solution>();
            var n_best_ln_pr  = new List <Double>();
            var list_of_lists = new List <List <Solution> >();

            foreach (Literal c in obs)
            {
                var remaining = new List <Literal>()
                {
                    c
                };
                list_of_lists.Add(Abduction.AndOrLeaflists(remaining, kb, maxdepth, new List <Literal>(), new List <Literal>()));
            }
            var combiner = new Combiner(list_of_lists);

            foreach (Solution s in combiner)
            {
                if (BestCaseLnProbability(s) > ln_pr_to_beat)
                {
                    foreach (Solution solution in Abduction.Crunch(s))
                    {
                        var jpr = JointLnProbability(solution);
                        if (jpr > ln_pr_to_beat)
                        {
                            var insert_at = n_best_ln_pr.Count;
                            for (int i = 0; i < n_best_ln_pr.Count; i++)
                            {
                                if (n_best_ln_pr[i] > jpr)
                                {
                                    insert_at = i;
                                    break;
                                }
                            }
                            n_best.Insert(insert_at, solution);
                            n_best_ln_pr.Insert(insert_at, jpr);
                            if (n_best.Count > n)
                            {
                                n_best.RemoveAt(0);
                                n_best_ln_pr.RemoveAt(0);
                                //ln_pr_to_beat = jpr; // shouldn't this be the last item instead?
                                ln_pr_to_beat = n_best_ln_pr[0]; // second worst [0] is now lowest
                            }
                        }
                    }
                }
            }
            n_best.Reverse(); // 0 is now highest;
            if (sk)           // skolemize here
            {
                var res = new List <Solution>();
                foreach (Solution c in n_best)
                {
                    res.Add(skolemize(c));
                }
                return(res);
            }
            else
            {
                return(n_best);
            }
        }