Esempio n. 1
0
    def fitness_calc_pole(self,
                          net,
                          cart,
                          s_len,
                          src_models,
                          target_model,
                          sample_size,
                          mutation_strength,
                          samples_count,
                          solution_found=None):

        start = time()

        if not all(self.genes == self.genes[0]):
            termination_mask = self.genes > (1 / (len(src_models) + 1) *
                                             0.01) * 1.0
            genes = termination_mask * self.genes

            genes = genes / np.sum(genes)
            print('genes after normalization: ', genes)
        else:
            genes = self.genes
            print('first iteration!? not neutralized')

        # Initializing the weights of the mixture model with
        mixModel = MixtureModel(src_models, alpha=genes)
        mixModel.add_target_model(target_model)

        offsprings, mutation_strength, samples_count, fitness_mean, eval_num = \
            mixModel.sample_enhanced(sample_size, cart, mutation_strength,
                                    samples_count, net=net, s_len=s_len, mutation=False,
                                    solution_found=solution_found, problem_type='pole')

        self.fitness = fitness_mean

        self.fitness_calc_time = time() - start
        print('self.fitness_calc_time (m): ', self.fitness_calc_time / 60)
        # best_offspring = np.max(offsprings)
        return self.fitness, offsprings, mutation_strength, samples_count, eval_num
Esempio n. 2
0
    def fitness_calc_enhanced(self,
                              problem,
                              src_models,
                              target_model,
                              sample_size,
                              mutation_strength,
                              samples_count,
                              max_sampling_num=None,
                              mutation=True,
                              problem_type='knapsack'):
        start = time()

        if (not all(self.genes == self.genes[0])):
            termination_mask = self.genes > (1 / (len(src_models) + 1) *
                                             0.01) * 1.0  # BUG
            genes = termination_mask * self.genes

            genes = genes / np.sum(genes)

        else:
            genes = self.genes
            print('first iteration!? not neutralized')

        # Initializing the weights of the mixture model with
        mixModel = MixtureModel(src_models, alpha=genes)
        mixModel.add_target_model(target_model)

        offsprings, mutation_strength, samples_count, fitness_mean = \
            mixModel.sample_enhanced(sample_size, problem, mutation_strength,
                                    samples_count, max_sampling_num, mutation=mutation,
                                    problem_type=problem_type)

        self.fitness = fitness_mean

        self.fitness_calc_time = time() - start

        return self.fitness, offsprings, mutation_strength, samples_count