예제 #1
0
def process_input(input=INPUT_FILE):
    with open(input, 'r') as file_pointer:
        lines = file_pointer.read().splitlines()
    global group_count, pot_count
    group_count, pot_count = int(lines[0]), int(lines[1])
    create_countries_and_pots(lines[2:2 + pot_count])
    create_regions(lines[2 + pot_count:])
    #working cnf
    #speed - cnf no issue
    #----------------------------
    create_cnf(regions, True)
    create_cnf(pots)
    xor_cnf_combinations()
    symbols = create_symbols(cnf)
    model = dpll_int_repr(cnf, symbols, {})
    #print model
    #print 'model', mod[0]
    #----------------------------

    # model = dpll_satisfy({})
    if not model:
        #print model
        process_output(['No'])
    else:
        group_countries = ['None' for x in range(group_count)]
        for key in model:
            if key > 0 and model[key]:
                country, group_index = decode_sym(key)
                if group_countries[group_index - 1] != 'None':
                    group_countries[group_index - 1] += ',' + country
                else:
                    group_countries[group_index - 1] = country
        #print 'gc',group_countries
        #print group_countries
        group_countries = ['Yes'] + group_countries
        process_output(group_countries)
예제 #2
0
파일: ask.py 프로젝트: tovrstra/sympy
def ask(expr, key, assumptions=True):
    """
    Method for inferring properties about objects.

    **Syntax**

        * ask(expression, key)

        * ask(expression, key, assumptions)

            where expression is any SymPy expression

    **Examples**
        >>> from sympy import ask, Q, Assume, pi
        >>> from sympy.abc import x, y
        >>> ask(pi, Q.rational)
        False
        >>> ask(x*y, Q.even, Assume(x, Q.even) & Assume(y, Q.integer))
        True
        >>> ask(x*y, Q.prime, Assume(x, Q.integer) &  Assume(y, Q.integer))
        False

    **Remarks**
        Relations in assumptions are not implemented (yet), so the following
        will not give a meaningful result.
        >> ask(x, positive=True, Assume(x>0))
        It is however a work in progress and should be available before
        the official release

    """
    expr = sympify(expr)
    assumptions = And(assumptions, And(*global_assumptions))

    # direct resolution method, no logic
    resolutors = []
    for handler in handlers_dict[key]:
        resolutors.append( get_class(handler) )
    res, _res = None, None
    mro = inspect.getmro(type(expr))
    for handler in resolutors:
        for subclass in mro:
            if hasattr(handler, subclass.__name__):
                res = getattr(handler, subclass.__name__)(expr, assumptions)
                if _res is None: _res = res
                elif res is None:
                    # since first resolutor was conclusive, we keep that value
                    res = _res
                else:
                    # only check consistency if both resolutors have concluded
                    if _res != res: raise ValueError, 'incompatible resolutors'
                break
    if res is not None:
        return res

    if assumptions is True: return

    # use logic inference
    if not expr.is_Atom: return
    clauses = copy.deepcopy(known_facts_compiled)

    assumptions = conjuncts(to_cnf(assumptions))
    # add assumptions to the knowledge base
    for assump in assumptions:
        conj = eliminate_assume(assump, symbol=expr)
        if conj:
            out = []
            for sym in conjuncts(to_cnf(conj)):
                lit, pos = literal_symbol(sym), type(sym) is not Not
                if pos:
                    out.extend([known_facts_keys.index(str(l))+1 for l in disjuncts(lit)])
                else:
                    out.extend([-(known_facts_keys.index(str(l))+1) for l in disjuncts(lit)])
            clauses.append(out)

    n = len(known_facts_keys)
    clauses.append([known_facts_keys.index(key)+1])
    if not dpll_int_repr(clauses, range(1, n+1), {}):
        return False
    clauses[-1][0] = -clauses[-1][0]
    if not dpll_int_repr(clauses, range(1, n+1), {}):
        # if the negation is satisfiable, it is entailed
        return True
    del clauses
예제 #3
0
파일: ask.py 프로젝트: goriccardo/sympy
def ask(expr, key, assumptions=True):
    """
    Method for inferring properties about objects.

    **Syntax**

        * ask(expression, key)

        * ask(expression, key, assumptions)

            where expression is any SymPy expression

    **Examples**
        >>> from sympy import ask, Q, Assume, pi
        >>> from sympy.abc import x, y
        >>> ask(pi, Q.rational)
        False
        >>> ask(x*y, Q.even, Assume(x, Q.even) & Assume(y, Q.integer))
        True
        >>> ask(x*y, Q.prime, Assume(x, Q.integer) &  Assume(y, Q.integer))
        False

    **Remarks**
        Relations in assumptions are not implemented (yet), so the following
        will not give a meaningful result.
        >> ask(x, positive=True, Assume(x>0))
        It is however a work in progress and should be available before
        the official release

    """
    expr = sympify(expr)
    if type(key) is not Predicate:
        key = getattr(Q, str(key))
    assumptions = And(assumptions, And(*global_assumptions))

    # direct resolution method, no logic
    res = eval_predicate(key, expr, assumptions)
    if res is not None:
        return res

    # use logic inference
    if assumptions is True:
        return

    if not expr.is_Atom:
        return
    clauses = copy.deepcopy(known_facts_compiled)

    assumptions = conjuncts(to_cnf(assumptions))
    # add assumptions to the knowledge base
    for assump in assumptions:
        conj = eliminate_assume(assump, symbol=expr)
        if conj:
            for clause in conjuncts(to_cnf(conj)):
                out = set()
                for atom in disjuncts(clause):
                    lit, pos = literal_symbol(atom), type(atom) is not Not
                    if pos:
                        out.add(known_facts_keys.index(lit)+1)
                    else:
                        out.add(-(known_facts_keys.index(lit)+1))
                clauses.append(out)

    n = len(known_facts_keys)
    clauses.append(set([known_facts_keys.index(key)+1]))
    if not dpll_int_repr(clauses, set(range(1, n+1)), {}):
        return False
    clauses[-1] = set([-(known_facts_keys.index(key)+1)])
    if not dpll_int_repr(clauses, set(range(1, n+1)), {}):
        # if the negation is satisfiable, it is entailed
        return True
    del clauses
예제 #4
0
파일: ask.py 프로젝트: unix0000/sympy-polys
def ask(expr, key, assumptions=True):
    """
    Method for inferring properties about objects.

    **Syntax**

        * ask(expression, key)

        * ask(expression, key, assumptions)

            where expression is any SymPy expression

    **Examples**
        >>> from sympy import ask, Q, Assume, pi
        >>> from sympy.abc import x, y
        >>> ask(pi, Q.rational)
        False
        >>> ask(x*y, Q.even, Assume(x, Q.even) & Assume(y, Q.integer))
        True
        >>> ask(x*y, Q.prime, Assume(x, Q.integer) &  Assume(y, Q.integer))
        False

    **Remarks**
        Relations in assumptions are not implemented (yet), so the following
        will not give a meaningful result.
        >> ask(x, positive=True, Assume(x>0))
        It is however a work in progress and should be available before
        the official release

    """
    expr = sympify(expr)
    assumptions = And(assumptions, And(*global_assumptions))

    # direct resolution method, no logic
    resolutors = []
    for handler in handlers_dict[key]:
        resolutors.append(get_class(handler))
    res, _res = None, None
    mro = inspect.getmro(type(expr))
    for handler in resolutors:
        for subclass in mro:
            if hasattr(handler, subclass.__name__):
                res = getattr(handler, subclass.__name__)(expr, assumptions)
                if _res is None:
                    _res = res
                elif res is None:
                    # since first resolutor was conclusive, we keep that value
                    res = _res
                else:
                    # only check consistency if both resolutors have concluded
                    if _res != res:
                        raise ValueError('incompatible resolutors')
                break
    if res is not None:
        return res

    if assumptions is True:
        return

    # use logic inference
    if not expr.is_Atom:
        return
    clauses = copy.deepcopy(known_facts_compiled)

    assumptions = conjuncts(to_cnf(assumptions))
    # add assumptions to the knowledge base
    for assump in assumptions:
        conj = eliminate_assume(assump, symbol=expr)
        if conj:
            out = set()
            for sym in conjuncts(to_cnf(conj)):
                lit, pos = literal_symbol(sym), type(sym) is not Not
                if pos:
                    out.update([
                        known_facts_keys.index(str(l)) + 1
                        for l in disjuncts(lit)
                    ])
                else:
                    out.update([
                        -(known_facts_keys.index(str(l)) + 1)
                        for l in disjuncts(lit)
                    ])
            clauses.append(out)

    n = len(known_facts_keys)
    clauses.append(set([known_facts_keys.index(key) + 1]))
    if not dpll_int_repr(clauses, set(range(1, n + 1)), {}):
        return False
    clauses[-1] = set([-(known_facts_keys.index(key) + 1)])
    if not dpll_int_repr(clauses, set(range(1, n + 1)), {}):
        # if the negation is satisfiable, it is entailed
        return True
    del clauses