Exemple #1
0
        public Model FindModelFor(String logicalSentence, int numberOfFlips,
                                  double probabilityOfRandomWalk)
        {
            this.myModel = new Model();
            var s              = (Sentence) new PEParser().Parse(logicalSentence);
            var transformer    = new CNFTransformer();
            var clauseGatherer = new CNFClauseGatherer();
            var sc             = new SymbolCollector();

            var symbols = sc.GetSymbolsIn(s).ToList();
            var r       = new Random();

            foreach (var sym in symbols)
            {
                this.myModel = this.myModel.Extend(sym, Util.RandomBoolean());
            }

            IList <Sentence> clauses = clauseGatherer.GetClausesFrom(transformer.Transform(s)).ToList();

            //IList<Sentence> ClauseSet = new Converter<Sentence>().ListToSet(clauses);
            lastClauses     = clauses;
            lastClausesFlip = new List <Int64>(clauses.Count + 1);
            for (int i = 0; i < clauses.Count; i++)
            {
                lastClausesFlip.Add(0);
            }

            for (int i = 0; i < numberOfFlips; i++)
            {
                List <int> unsatList = new List <int>();
                int        numSat    = this.GetNumberOfClausesSatisfiedIn(new Converter <Sentence>().ListToSet(clauses), myModel, unsatList);
                if (numSat == clauses.Count)
                {
                    trials = i;
                    return(myModel);
                }
                int probe = FindAnRandomUnsatisfiedClause(clauses, myModel, unsatList);
                //Sentence clause = clauses[random.Next(clauses.Count)];
                Sentence clause = clauses[probe];
                lastClausesFlip[probe]++;

                IList <Symbol> symbolsInClause = sc.GetSymbolsIn(clause).ToList();

                if (random.NextDouble() >= probabilityOfRandomWalk)
                {
                    Symbol randomSymbol = symbolsInClause[random.Next(symbolsInClause.Count)];
                    myModel = myModel.Flip(randomSymbol);
                }
                else
                {
                    Symbol symbolToFlip = this.GetSymbolWhoseFlipMaximisesSatisfiedClauses(
                        new Converter <Sentence>().ListToSet(clauses),
                        symbolsInClause, myModel);
                    myModel = myModel.Flip(symbolToFlip);
                }
            }
            trials = numberOfFlips;
            return(null);
        }
Exemple #2
0
        //TODO: Update this method with better name
        public bool TtEntails(KnowledgeBase kb, String alpha)
        {
            Sentence        kbSentence    = kb.AsSentence();
            Sentence        querySentence = (Sentence) new PEParser().Parse(alpha);
            SymbolCollector collector     = new SymbolCollector();
            ISet <Symbol>   kbSymbols     = collector.GetSymbolsIn(kbSentence);
            ISet <Symbol>   querySymbols  = collector.GetSymbolsIn(querySentence);
            IList <Symbol>  symbolList    = kbSymbols.Union(querySymbols).ToList();

            return(ttCheckAll(kbSentence, querySentence, symbolList, new Model()));
        }