def ImplementBooleanFn(variables,Ones,dont_care ):
    ''' An interactive function which takes in templates/maxterms and
    prints the Boolean Function and implementable form.
    Don't Care Conditions can also be provided (optional)
    Eg:
    Enter the list of variables
    A,B,C
    Do you want to enter templates (m) or maxterms(M)?
    m
    Enter list of templates
    1,4,7
    Enter list of Don't Care terms
    Enter X if there are none
    2,5
    The logical expression is (((NOT B) AND C) OR (A AND C) OR (A AND (NOT B)))
    Can be implemented as OR(AND(NOT(B), C), AND(A, C), AND(A, NOT(B)))
    '''
    for i in range(len(Ones)):
        if Ones[i] >= pow(2, len(variables)):
            raise Exception("Error: Invalid Minterm")
    qm = QM(variables)
    if len(dont_care) != 0:
        LogicalExpression = qm.get_function(qm.solve(Ones, dont_care)[1])
    else:
        LogicalExpression = qm.get_function(qm.solve(Ones)[1])
    GateForm = convertExpression(LogicalExpression)

    # print("The logical expression is " + LogicalExpression)
    # print("Can be implemented as " + GateForm)
    return [LogicalExpression, GateForm]
Beispiel #2
0
  class InternalOptimizer:
    def __init__(self, state_word_len, variables):
      """ Initialize the internal helper class. """

      self.state_word_len = state_word_len
      self.variables = variables
      variable_names = map(lambda i:'S%d'%i, xrange(self.state_word_len))
      variable_names += self.variables
      self.qm = QM(variable_names)

    def solve(self, f_on, f_off = None):
      """
Returns a function that satisfies the conditions given.

f_on: a list of lambda expressions; if one of the lambda expressions
evaluates to True then the requested function should evaluate to True
f_off: a list of lambda expressions; if one of them evaluates to True
then the requested function whould evaluate to False

returns: a tuple a,b; a is the complexity of the function and b is the
function
"""

      self.state_env = self.State()
      self.variables_env = self.Variables(self.variables)

      c = self.state_word_len
      d = len(self.variables)
      ones = []
      dc = set(i for i in xrange(1<<(d+c)))
      for variables_word in xrange(1<<d):
        self.variables_env.word = variables_word
        for state,state_word in self.state_map.iteritems():
          self.state_env.state = state
          on = self.evaluate(f_on)
          if f_off == None:
            off = not on
          else:
            off = self.evaluate(f_off)
          assert not (on and off)
          if on:
            ones.append(variables_word<<c|state_word)
            dc.remove(variables_word<<c|state_word)
          elif off:
            dc.remove(variables_word<<c|state_word)

      dc = list(dc)
      return self.qm.solve(ones,dc)

    def evaluate(self, f_array):
      """
Evaluates a list of lambda expressions in the state and variables
environment. The lambda expressions are terms of an OR expression.

f_array: a list of lambda expressions

returns: the logical OR after evaluate the lambda expression in the setup
environment
"""

      for f in f_array:
        if f(self.state_env,self.variables_env):
          return True
      return False

    """
This class provides access to the state word from the lambda expressions.
"""

    class State:
      def __getitem__(self, item):
        return self.state == item

    """
This class provides access to the input variables from the lambda
expressions.
"""

    class Variables:
      def __init__(self, variables):
        self.variables = {}
        for i in xrange(len(variables)):
          self.variables[variables[i]] = 1<<i

      def __getitem__(self, item):
        return bool(self.word & self.variables[item])

    def get_function(self, minterms):
      """ Retrieve a human readable form of the given function. """

      return self.qm.get_function(minterms)
Beispiel #3
0
    class InternalOptimizer:

        def __init__(self, state_word_len, variables):
            """ Initialize the internal helper class. """

            self.state_word_len = state_word_len
            self.variables = variables
            variable_names = map(
                lambda i: 'S%d' %
                i, xrange(
                    self.state_word_len))
            variable_names += self.variables
            self.qm = QM(variable_names)

        def solve(self, f_on, f_off=None):
            """
            Returns a function that satisfies the conditions given.

            f_on: a list of lambda expressions; if one of the lambda
            expressions evaluates to True then the requested function
            should evaluate to True
            f_off: a list of lambda expressions; if one of them evaluates
            to True then the requested function whould evaluate to False

            returns: a tuple a,b; a is the complexity of the function and
            b is the function
            """
            self.state_env = self.State()
            self.variables_env = self.Variables(self.variables)

            c = self.state_word_len
            d = len(self.variables)
            ones = []
            dc = set(i for i in xrange(1 << (d + c)))
            for variables_word in xrange(1 << d):
                self.variables_env.word = variables_word
                for state, state_word in self.state_map.iteritems():
                    self.state_env.state = state
                    on = self.evaluate(f_on)
                    if f_off is None:
                        off = not on
                    else:
                        off = self.evaluate(f_off)
                    assert not (on and off)
                    if on:
                        ones.append(variables_word << c | state_word)
                        dc.remove(variables_word << c | state_word)
                    elif off:
                        dc.remove(variables_word << c | state_word)

            dc = list(dc)
            return self.qm.solve(ones, dc)

        def evaluate(self, f_array):
            """
            Evaluates a list of lambda expressions in the state and variables
            environment. The lambda expressions are terms of an OR expression.

            f_array: a list of lambda expressions

            returns: the logical OR after evaluate the lambda expression in the
            setup environment
            """
            for f in f_array:
                if f(self.state_env, self.variables_env):
                    return True
            return False

        class State:

            """
            This class provides access to the state word from the lambda
            expressions.
            """

            def __getitem__(self, item):
                return self.state == item

        class Variables:

            """
            This class provides access to the input variables from the
            lambda expressions.
            """

            def __init__(self, variables):
                self.variables = {}
                for i in xrange(len(variables)):
                    self.variables[variables[i]] = 1 << i

            def __getitem__(self, item):
                return bool(self.word & self.variables[item])

        def get_function(self, minterms):
            """ Retrieve a human readable form of the given function. """

            return self.qm.get_function(minterms)