def _dev_int_context(self, ec):
        segment_choices = []
        for segment in self.segments:
            results = map(lambda x: reduce(operator.mul, \
                            map(lambda i: self.app_pool[x][i][ec[i]], xrange(self.feature_num)), 1), \
                            segment)
            score = reduce(operator.mul, results, 1)
            segment_choices.append((segment, score))

        app_choices = []
        for app in self.app_pool.keys():
            score = reduce(operator.mul, map(lambda i: self.app_pool[app][i][ec[i]], xrange(self.feature_num)), 1)
            app_choices.append((app, score))

        int_context = []
        segment = nonrandom.choice(segment_choices)
        # turning_point = random.choice(xrange(1, self.l)) if random.random() > 0.3 else -1
        for i in xrange(self.l):
            # if i == turning_point:
            if random.random() > 0.9:
                segment = nonrandom.choice(filter(lambda x: x!= segment, segment_choices))

            s = list(segment)
            for j in xrange(self.s):
                if random.random() < self.c:
                    s[j] = nonrandom.choice(app_choices) if random.random() > 0.7 else None
            s = filter(lambda x: x, s)
            if not s:
                s = list(segment)  
            int_context.extend(s)

        return int_context
예제 #2
0
    def _generate_segments(self, app_usage_distrs):
        # TODO: decide parameters
        session_len = 5
        I = 3
        l = 50

        apps = app_usage_distrs.keys()

        segments = []
        size = nonrandom.choice(
            map(lambda x: (x, I**x * math.exp(-I) / math.factorial(x)),
                xrange(2, 6)))
        segment = random.sample(apps, size)
        segments.append(segment)
        for i in xrange(l):
            prev_segment = segments[-1]
            # size = nonrandom.choice(map(lambda x: (x, I**x*math.exp(-I)/math.factorial(x)), xrange(2, 6)))
            size = 4
            # random.choice([2, 3])
            # TODO: decide how many items are preserved
            fragment_size = int(0.6 * len(prev_segment))
            if fragment_size > 0:
                # segment = [prev_segment[i] for i in \
                #     sorted(random.sample(xrange(len(prev_segment)), fragment_size))]
                segment = random.sample(prev_segment, fragment_size)
            if len(segment) < size:
                segment.extend(
                    random.sample(filter(lambda x: x not in segment, apps),
                                  size - len(segment)))
            # random.shuffle(segment)

            segment = random.sample(apps, size)
            segments.append(segment)

        return segments
    def _generate_segments(self, app_usage_distrs):
        # TODO: decide parameters
        session_len = 5
        I = 3
        l = 50

        apps = app_usage_distrs.keys()

        segments = []
        size = nonrandom.choice(map(lambda x: (x, I**x*math.exp(-I)/math.factorial(x)), xrange(2, 6)))
        segment = random.sample(apps, size)
        segments.append(segment)
        for i in xrange(l):
            prev_segment = segments[-1]
            # size = nonrandom.choice(map(lambda x: (x, I**x*math.exp(-I)/math.factorial(x)), xrange(2, 6)))
            size = 4
            # random.choice([2, 3])
            # TODO: decide how many items are preserved
            fragment_size = int(0.6*len(prev_segment))
            if fragment_size > 0:
                # segment = [prev_segment[i] for i in \
                #     sorted(random.sample(xrange(len(prev_segment)), fragment_size))]
                segment = random.sample(prev_segment, fragment_size)
            if len(segment) < size:
                segment.extend(random.sample(filter(lambda x: x not in segment, apps), size - len(segment)))
            # random.shuffle(segment)

            segment = random.sample(apps, size)
            segments.append(segment)

        return segments
예제 #4
0
    def _fill_segment(self, int_context, initiator, segment_len, candidates,
                      test):
        segment = []

        if len(int_context) > 0:
            predecessor = int_context[-1]
        else:
            predecessor = ''

        choices = list(candidates)
        initiator = nonrandom.choice(choices)
        if initiator == predecessor and predecessor != '' and initiator == test:
            choices = filter(lambda x: x[0] != predecessor, choices)
            initiator = nonrandom.choice(choices)
        """        
        choices = map(lambda x: (x, self.launching_contrib[x][initiator]), self.launching_contrib)
        choices = filter(lambda x: x[0] in map(lambda y: y[0], candidates), choices)
        if sum(v for k, v in choices) == 0:
            choices = list(candidates)
        """

        # """
        p = initiator
        while len(segment) < segment_len - 1:
            # choices = list(candidates)
            choices = map(lambda x: (x, self.launching_contrib[x][p]),
                          self.launching_contrib)
            # if sum(v for k, v in choices) == 0:
            #     app = random.choice(self.launching_contrib.keys())
            app = nonrandom.choice(choices)
            while app in segment or app == initiator:
                choices = filter(lambda x: x[0] not in [initiator] + segment,
                                 choices)
                # choices = map(lambda x: (x, self.launching_contrib[x][p]), self.launching_contrib)
                app = nonrandom.choice(choices)
            segment.append(app)
            # p = app
        # """

        random.shuffle(segment)
        segment.insert(0, initiator)
        # random.shuffle(segment)
        return segment
    def _fill_segment(self, int_context, initiator, segment_len, candidates, test):
        segment = []

        if len(int_context) > 0:
            predecessor = int_context[-1]
        else:
            predecessor = ''

        choices = list(candidates)
        initiator = nonrandom.choice(choices)
        if initiator == predecessor and predecessor != '' and initiator == test:
            choices = filter(lambda x: x[0] != predecessor, choices)
            initiator = nonrandom.choice(choices)


        """        
        choices = map(lambda x: (x, self.launching_contrib[x][initiator]), self.launching_contrib)
        choices = filter(lambda x: x[0] in map(lambda y: y[0], candidates), choices)
        if sum(v for k, v in choices) == 0:
            choices = list(candidates)
        """

        # """
        p = initiator
        while len(segment) < segment_len - 1:
            # choices = list(candidates)
            choices = map(lambda x: (x, self.launching_contrib[x][p]), self.launching_contrib)
            # if sum(v for k, v in choices) == 0:
            #     app = random.choice(self.launching_contrib.keys())
            app = nonrandom.choice(choices)
            while app in segment or app == initiator:
                choices = filter(lambda x: x[0] not in [initiator] + segment, choices)
                # choices = map(lambda x: (x, self.launching_contrib[x][p]), self.launching_contrib)
                app = nonrandom.choice(choices)
            segment.append(app)
            # p = app
        # """

        random.shuffle(segment)
        segment.insert(0, initiator)
        # random.shuffle(segment)
        return segment
    def _develop_int_context(self, ec, session_len, app_usage_distrs):
        choices = []
        for segment in self.segments:
            results = map(lambda x: reduce(operator.mul, \
                            map(lambda i: app_usage_distrs[x][i][ec[i]], xrange(self.feature_num)), 1), \
                            segment)
            score = reduce(operator.mul, results, 1)
            choices.append((segment, score))

        # for app in app_usage_distrs:
        #     score = reduce(operator.mul, map(lambda i: app_usage_distrs[app][i][ec[i]], xrange(self.feature_num)), 1)
        #     choices.append(([app], score))            
        
        # int_context = []
        # while len(int_context) < session_len:
        #     size = nonrandom.choice(map(lambda x: (x, 3**x*math.exp(-3)/math.factorial(x)), \
        #             xrange(2, session_len - len(int_context) + 1)))
        #     if len(int_context) > 0:
        #         int_context.extend(nonrandom.choice(filter(lambda x: len(x[0]) == size and x[0][0] != int_context[-1], choices)))
        #     else:
        #         int_context.extend(nonrandom.choice(filter(lambda x: len(x[0]) == size, choices)))
        # return int_context


        c = 0.3
        int_context = []
        segment = nonrandom.choice(filter(lambda x: len(x[0]) == 3, choices))
        for i in xrange(3):
            s = filter(lambda x: random.random() > c, segment)
            int_context.extend(s)
        if not int_context:
            int_context = segment
        return int_context


        # return nonrandom.choice(choices)+nonrandom.choice(choices)+nonrandom.choice(choices)
        return nonrandom.choice(filter(lambda x: len(x[0]) == 4, choices))
            # nonrandom.choice(filter(lambda x: len(x[0]) == 4, choices))
        return random.choice(self.segments)+random.choice(self.segments)+random.choice(self.segments)    
        
        
        """
예제 #7
0
    def _develop_int_context(self, ec, session_len, app_usage_distrs):
        choices = []
        for segment in self.segments:
            results = map(lambda x: reduce(operator.mul, \
                            map(lambda i: app_usage_distrs[x][i][ec[i]], xrange(self.feature_num)), 1), \
                            segment)
            score = reduce(operator.mul, results, 1)
            choices.append((segment, score))

        # for app in app_usage_distrs:
        #     score = reduce(operator.mul, map(lambda i: app_usage_distrs[app][i][ec[i]], xrange(self.feature_num)), 1)
        #     choices.append(([app], score))

        # int_context = []
        # while len(int_context) < session_len:
        #     size = nonrandom.choice(map(lambda x: (x, 3**x*math.exp(-3)/math.factorial(x)), \
        #             xrange(2, session_len - len(int_context) + 1)))
        #     if len(int_context) > 0:
        #         int_context.extend(nonrandom.choice(filter(lambda x: len(x[0]) == size and x[0][0] != int_context[-1], choices)))
        #     else:
        #         int_context.extend(nonrandom.choice(filter(lambda x: len(x[0]) == size, choices)))
        # return int_context

        c = 0.3
        int_context = []
        segment = nonrandom.choice(filter(lambda x: len(x[0]) == 3, choices))
        for i in xrange(3):
            s = filter(lambda x: random.random() > c, segment)
            int_context.extend(s)
        if not int_context:
            int_context = segment
        return int_context

        # return nonrandom.choice(choices)+nonrandom.choice(choices)+nonrandom.choice(choices)
        return nonrandom.choice(filter(lambda x: len(x[0]) == 4, choices))
        # nonrandom.choice(filter(lambda x: len(x[0]) == 4, choices))
        return random.choice(self.segments) + random.choice(
            self.segments) + random.choice(self.segments)
        """
예제 #8
0
    def _dev_int_context(self, ec):
        segment_choices = []
        for segment in self.segments:
            results = map(lambda x: reduce(operator.mul, \
                            map(lambda i: self.app_pool[x][i][ec[i]], xrange(self.feature_num)), 1), \
                            segment)
            score = reduce(operator.mul, results, 1)
            segment_choices.append((segment, score))

        app_choices = []
        for app in self.app_pool.keys():
            score = reduce(
                operator.mul,
                map(lambda i: self.app_pool[app][i][ec[i]],
                    xrange(self.feature_num)), 1)
            app_choices.append((app, score))

        int_context = []
        segment = nonrandom.choice(segment_choices)
        # turning_point = random.choice(xrange(1, self.l)) if random.random() > 0.3 else -1
        for i in xrange(self.l):
            # if i == turning_point:
            if random.random() > 0.9:
                segment = nonrandom.choice(
                    filter(lambda x: x != segment, segment_choices))

            s = list(segment)
            for j in xrange(self.s):
                if random.random() < self.c:
                    s[j] = nonrandom.choice(
                        app_choices) if random.random() > 0.7 else None
            s = filter(lambda x: x, s)
            if not s:
                s = list(segment)
            int_context.extend(s)

        return int_context