ai_utils/knowledge/model_checking.py
def model_check(knowledge, query):
    """Checks if knowledge base entails query."""

    def check_all(knowledge, query, symbols, model):
        """Checks if knowledge base entails query, given a particular model."""

        # If model has an assignment for each symbol
        if not symbols:
            # If knowledge base is true in model, then query must also be true
            if knowledge.evaluate(model):
                return query.evaluate(model)
            return True
        else:
            # Choose one of the remaining unused symbols
            remaining = symbols.copy()
            p = remaining.pop()

            # Create a model where the symbol is true
            model_true = model.copy()
            model_true[p] = True

            # Create a model where the symbol is false
            model_false = model.copy()
            model_false[p] = False

            # Ensure entailment holds in both models
            return (check_all(knowledge, query, remaining, model_true) and
                    check_all(knowledge, query, remaining, model_false))

    # Get all symbols in both knowledge and query
    symbols = set.union(knowledge.symbols(), query.symbols())

    # Check that knowledge entails query
    return check_all(knowledge, query, symbols, dict())
Checks if knowledge base entails query.
Checks if knowledge base entails query.
Checks if knowledge base entails query .
def model_check(knowledge, query):
    def check_all(knowledge, query, symbols, model):
        if not symbols:
            if knowledge.evaluate(model):
                return query.evaluate(model)
            return True
        else:
            remaining = symbols.copy()
            p = remaining.pop()
            model_true = model.copy()
            model_true[p] = True
            model_false = model.copy()
            model_false[p] = False
            return (check_all(knowledge, query, remaining, model_true) and
                    check_all(knowledge, query, remaining, model_false))
    symbols = set.union(knowledge.symbols(), query.symbols())
    return check_all(knowledge, query, symbols, dict())
def model_check ( knowledge , query ) : def check_all ( knowledge , query , symbols , model ) : """Checks if knowledge base entails query, given a particular model.""" if not symbols : if knowledge . evaluate ( model ) : return query . evaluate ( model ) return True else : remaining = symbols . copy ( ) p = remaining . pop ( ) model_true = model . copy ( ) model_true [ p ] = True model_false = model . copy ( ) model_false [ p ] = False return ( check_all ( knowledge , query , remaining , model_true ) and check_all ( knowledge , query , remaining , model_false ) ) symbols = set . union ( knowledge . symbols ( ) , query . symbols ( ) ) return check_all ( knowledge , query , symbols , dict ( ) )
Checks if knowledge base entails query.
Checks if knowledge base entails query .
"""Checks if knowledge base entails query.""" """Checks if knowledge base entails query, given a particular model.""" # If model has an assignment for each symbol # If knowledge base is true in model, then query must also be true # Choose one of the remaining unused symbols # Create a model where the symbol is true # Create a model where the symbol is false # Ensure entailment holds in both models # Get all symbols in both knowledge and query # Check that knowledge entails query
knowledge
query