예제 #1
0
    def save_population(self, seed):
        save_dir = self.save_dir
        mkdir_p(save_dir)
        filename = "gen_%d.gapy" % self.current_gen
        #population_data = []
        for f, p in zip(self.fitness, self.population):
            data = copy.deepcopy(argos_util.get_parameters(p))
            #data2= copy.deepcopy(argos_util.get_controller_params(p)) #qilu 07/25
            if "PrintFinalScore" in data:
                del data["PrintFinalScore"]
            data["fitness"] = f
            data["seed"] = seed
            self.population_data.append(data)
            #population_data2.append(data2)
            #print data
        data_keys = argos_util.PARAMETER_LIMITS.keys()
        data_keys.append("fitness")
        data_keys.append("seed")
        data_keys.sort()

        #data_keys2 = argos_util.controller_params_LIMITS.keys()
        #data_keys2.sort()
        with open(os.path.join(save_dir, filename), 'w') as csvfile:
            writer = csv.DictWriter(csvfile,
                                    fieldnames=data_keys,
                                    extrasaction='ignore')
            writer.writeheader()
            writer.writerows(self.population_data)  #qilu 07/27
예제 #2
0
    def run_generation(self):
        logging.info("Starting generation: " + str(self.current_gen))
        self.fitness = np.zeros(pop_size)  #reset it
        seeds = [np.random.randint(2**32) for _ in range(self.tests_per_gen)]
        logging.info("Seeds for generation: " + str(seeds))
        for i, p in enumerate(self.population):
            print "Gen: " + str(
                self.current_gen) + '; Population: ' + str(i + 1)
            for test_id in xrange(self.tests_per_gen):
                seed = seeds[test_id]
                logging.info("pop %d at test %d with seed %d", i, test_id,
                             seed)
                if self.not_evolved_idx[
                        i] == -1 or self.not_evolved_count[i] > 3:
                    self.not_evolved_count[i] = 0
                    self.fitness[i] += self.test_fitness(p, seed)
                else:  #qilu 03/27/2016 avoid recompute
                    self.fitness[i] += self.prev_fitness[
                        self.not_evolved_idx[i]]
                    logging.info("partial fitness = %d",
                                 self.prev_fitness[self.not_evolved_idx[i]])
        # use average fitness as fitness
        for i in xrange(len(self.fitness)):
            logging.info("pop %d total fitness = %g", i, self.fitness[i])
            self.fitness[i] /= self.tests_per_gen
            logging.info("pop %d avg fitness = %g", i, self.fitness[i])

        # sort fitness and population
        #fitpop = sorted(zip(self.fitness, self.population), reverse=True)
        #self.fitness, self.population = map(list, zip(*fitpop))
        fitpop = sorted(zip(self.fitness, self.population,
                            self.not_evolved_count),
                        reverse=True)  #qilu 04/02 add not_evolved_count
        self.fitness, self.population, self.not_evolved_count = map(
            list, zip(*fitpop))

        self.save_population(seed)

        self.prev_population = copy.deepcopy(self.population)
        self.prev_fitness = copy.deepcopy(self.fitness)  #qilu 03/27
        self.prev_not_evolved_count = copy.deepcopy(
            self.not_evolved_count)  #qilu 04/02

        self.not_evolved_idx = []  #qilu 03/27/2016
        self.not_evolved_count = []  #qilu 04/02/2016
        self.population = []
        self.check_termination()  #qilu 01/21/2016 add this function
        self.population_data = []  # qilu 01/21/2016 reset it
        # Add elites
        for i in xrange(self.elites):
            # reverse order from sort
            self.population.append(self.prev_population[i])
            self.not_evolved_idx.append(i)
            self.not_evolved_count.append(self.prev_not_evolved_count[i] + 1)

        # Now do crossover and mutation until population is full

        num_newOffSpring = self.pop_size - self.elites
        #pdb.set_trace()
        count = 0
        for i in xrange(num_newOffSpring):
            if count == num_newOffSpring: break
            p1c = np.random.choice(len(self.prev_population), 2)
            p2c = np.random.choice(len(self.prev_population), 2)
            if p1c[0] <= p1c[1]:
                parent1 = self.prev_population[p1c[0]]
                idx1 = p1c[0]
            else:
                parent1 = self.prev_population[p1c[1]]
                idx1 = p1c[1]

            if p2c[0] <= p2c[1]:
                parent2 = self.prev_population[p2c[0]]
                idx2 = p2c[0]
            else:
                parent2 = self.prev_population[p2c[1]]
                idx2 = p2c[1]
            #if parent1 != parent2 and np.random.uniform()<0.5: #qilu 11/26/2015
            #pdb.set_trace()
            if parent1 != parent2:  #qilu 03/26/2016
                children = argos_util.uniform_crossover(
                    xml_file, parent1, parent2, 0.5,
                    self.system)  # qilu 03/07/2016 add the crossover rate p
            else:
                children = [copy.deepcopy(parent1), copy.deepcopy(parent2)]
            for child in children:
                argos_util.mutate_parameters(child, self.mut_rate)
                self.population.append(child)
                if argos_util.get_parameters(
                        parent1) == argos_util.get_parameters(child):
                    #pdb.set_trace()
                    self.not_evolved_idx.append(idx1)
                    self.not_evolved_count.append(
                        self.prev_not_evolved_count[idx1] + 1)
                elif argos_util.get_parameters(
                        parent2) == argos_util.get_parameters(child):
                    #pdb.set_trace()
                    self.not_evolved_idx.append(idx2)
                    self.not_evolved_count.append(
                        self.prev_not_evolved_count[idx2] + 1)
                else:
                    self.not_evolved_idx.append(-1)
                    self.not_evolved_count.append(0)
            count += 2
            while count > num_newOffSpring:
                del self.population[-1]
                del self.not_evolved_idx[-1]
                del self.not_evolved_count[-1]
                count -= 1
        self.current_gen += 1