Beispiel #1
0
    def run(self,mod):
        frontier = []
        for _ in range(self.candidates):
            frontier.append(mod.generate())
        generations = 0

        lives = 5
        baseline = frontier[:]
        prev_era = self.fitting(mod,baseline)
        for i in range(self.generations):
            to_select_sample = self.select(mod,frontier) # binary denomination score of all pairs
            sorted_sample = OrderedDict(sorted(to_select_sample.items(), key=lambda t: t[1],reverse=True)) # sorted by the scores
            selected_sample = Utility.take(40, sorted_sample.iteritems()) # select 40% of the population
            selection_pool = []
            for key, value in selected_sample:
                selection_pool.append(frontier[key])
            new_frontier = self.get_new_frontier(mod,selection_pool) # perform crossover
            mutated_frontier = self.do_mutation(mod,new_frontier) # mutated some candidates
            cur_era = self.fitting(mod,mutated_frontier) #average
            generations += 1
            ret_val = Utility.better(mod,prev_era,cur_era)
            if ret_val > 0:
                prev_era = cur_era[:]
            lives += ret_val
            if lives is 0:
                return prev_era

            #print "Generations : ", generations
        #print "Best Solution : ", sum(cur_era)

        return cur_era
Beispiel #2
0
    def run(self, mod):
        frontier = []
        for _ in range(self.candidates):
            frontier.append(mod.generate())
        generations = 0
        lives = 5
        baseline = frontier[:]
        prev_era = self.fitting(mod, baseline)
        #print "Optimizer"
        for i in range(self.generations):
            #print "Start generation"
            to_select_sample = self.select(
                mod, frontier)  # binary denomination score of all pairs
            sorted_sample = OrderedDict(
                sorted(to_select_sample.items(),
                       key=lambda t: t[1],
                       reverse=True))  # sorted by the scores
            slice = int(0.5 * self.candidates)
            selected_sample = Utility.take(
                slice,
                sorted_sample.iteritems())  # select 50% of the population
            selection_pool = []
            for key, value in selected_sample:
                selection_pool.append(frontier[key])
            new_frontier = self.get_new_frontier(
                mod, selection_pool)  # perform crossover
            mutated_frontier = self.do_mutation(
                mod, new_frontier)  # mutated some candidates
            cur_era = self.fitting(mod, mutated_frontier)  #average
            generations += 1
            if Utility.better(mod, prev_era, cur_era):
                prev_era = cur_era[:]
                lives += 0
            else:
                lives += -1
            if lives is 0:
                return prev_era

            #print "Generations : ", generations
        #print "Best Solution : ", sum(cur_era)

        return cur_era