Example #1
0
        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)
Example #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 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)