Esempio n. 1
0
 public void AdvanceOneObservable()
 {
     if (this.current < this.observables.Count) // more to do
     {
         this.current += 1;
         if (this.current <= this.window)
         {
             var res = Etcetera.NBest(this.observables.GetRange(0, this.current), this.knowledgebase, this.depth, this.beam, false);
             var pre = "$" + this.current.ToString() + ":";
             this.solutions = new List <Solution>(); // blow away previous
             foreach (Solution s in res)
             {
                 this.solutions.Add(skolemize_with_prefix(s, pre));
             }
         }
         else
         {
             if (this.current > this.cached + this.window) // treat old solutions as contexts
             {
                 this.contexts = this.solutions;
                 this.cached  += this.window;
             }
             var current_window = this.observables.GetRange(this.cached, this.current - this.cached);
             var solution_list  = Incremental.ContextualEtcAbduction(this.observables, current_window, knowledgebase, contexts, depth, beam, current);
             if (solution_list.Count > this.beam)
             {
                 solution_list.RemoveRange(this.beam, solution_list.Count - this.beam);
             }
             this.solutions = solution_list;
         }
     }
 }
Esempio n. 2
0
        public static List <Solution> DoIncremental(List <Literal> obs, Knowledgebase kb, int maxdepth, int n, int w, int b, bool sk)
        {
            var iteration    = 1; // count for skolem constants
            var window_start = 0;
            var window_end   = Math.Min(w * iteration, obs.Count);
            var previous     = new List <Solution>();

            // first, interpret the first window as normal
            var res = Etcetera.NBest(obs.GetRange(window_start, window_end - window_start), kb, maxdepth, b, false);
            var pre = "$" + iteration.ToString() + ":";

            foreach (Solution s in res)
            {
                previous.Add(skolemize_with_prefix(s, pre));
            }
            // then iterate through remaining windows
            while (window_end < obs.Count) // some remain
            {
                iteration   += 1;          //advance
                window_start = window_end;
                window_end   = Math.Min(w * iteration, obs.Count);
                res          = ContextualEtcAbduction(obs.GetRange(0, window_end), obs.GetRange(window_start, window_end - window_start), kb, previous, maxdepth, b, iteration);
                previous     = res;
            }
            if (previous.Count > n)                          // too many results
            {
                previous.RemoveRange(n, previous.Count - n); // truncate excess
            }
            return(previous);
        }
Esempio n. 3
0
        public static void Main(string[] cli_args)
        {
            var args = new Cli(cli_args);

            if (args.Help_flag)
            {
                Console.WriteLine(HELP_MESSAGE);
                System.Environment.Exit(1);
            }


            // read buffers
            var buffer_i = "";
            var buffer_k = "";

            if (args.Input_path != null)
            {
                buffer_i = File.ReadAllText(args.Input_path);
            }
            else
            {
                if (Console.IsInputRedirected)
                {
                    using (StreamReader reader = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding))
                    {
                        buffer_i = reader.ReadToEnd();
                    }
                }
            }

            if (args.Knowledge_path != null)
            {
                buffer_k = File.ReadAllText(args.Knowledge_path);
            }

            // create knowledgebase
            var kb  = new Knowledgebase();
            var obs = kb.Add(buffer_i);

            obs.AddRange(kb.Add(buffer_k));

            // compose result
            var result = "";

            if (args.Parse_flag == true) // parse only
            {
                result = String.Join("\n", kb.Axioms.Select(dc => dc.Repr()));
            }

            else if (args.Forward_flag == true) // Forward chain
            {
                var entailments = Forward.Entailments(kb, obs);
                if (args.Graph_flag == true) // dot format graph
                {
                    result = Forward.Graph(obs, entailments, new List <Literal>());
                }
                else
                {
                    result = String.Join("\n", entailments.Select(e => e.Repr()));
                }
            }

            else // Abduction
            {
                var all_solutions = new List <List <Literal> >(); // void

                if (args.All_flag) // All flag
                {
                    all_solutions = Etcetera.DoEtcAbduction(obs, kb, args.Depth, true);
                }

                // else if incremental here

                else if (args.Incremental_flag)
                {
                    all_solutions = Incremental.DoIncremental(obs, kb, args.Depth, args.Nbest, args.Window, args.Beam, true);
                    //all_solutions = Realtime_Sidecar.incremental_alternative(obs, kb, args.Depth, args.Nbest, args.Window, args.Beam, true);
                }

                // else n-best
                else
                {
                    all_solutions = Etcetera.NBest(obs, kb, args.Depth, args.Nbest, true);
                }

                // Then decide what to output
                if (args.Graph_flag)
                {
                    var entailments = Forward.Entailments(kb, all_solutions[args.Solution - 1]);
                    result = Forward.Graph(all_solutions[args.Solution - 1], entailments, obs);
                }
                else if (all_solutions.Count == 0)
                {
                    result = "0 solutions.";
                }
                else
                {
                    var reslist = new List <String>();
                    if (args.EntailmentsFlag)
                    {
                        foreach (List <Literal> solution in all_solutions)
                        {
                            var entailed = Incremental.GetContext(solution, obs, kb);
                            if (entailed.Count == 0)
                            {
                                reslist.Add("none.");
                            }
                            else
                            {
                                reslist.Add($"({String.Join(" ",entailed.Select(e => e.Repr()))})");
                            }
                        }
                    }
                    else
                    {
                        foreach (List <Literal> solution in all_solutions)
                        {
                            var reprs = new List <String>();
                            foreach (Literal l in solution)
                            {
                                reprs.Add(l.Repr());
                            }
                            var reprstr = $"({String.Join(" ", reprs)})";
                            reslist.Add(reprstr);
                        }
                    }
                    result = $"{String.Join("\n",reslist)}\n{reslist.Count} solutions.";
                }
            }

            // output
            if (args.Output_path != null)
            {
                using (StreamWriter file = new StreamWriter(args.Output_path))
                {
                    file.WriteLine(result);
                }
            }
            else
            {
                Console.WriteLine(result);
            }
        }