Exemple #1
0
 def fitness_calc(
     self, problem, src_models, target_model, sample_size, sub_sample_size
 ):  # You can implement this in a more optmized way using vectorizatioin but it will hurt modularity
     start = time()
     normalized_alpha = self.genes / np.sum(self.genes)
     mixModel = MixtureModel(src_models, alpha=normalized_alpha)
     mixModel.add_target_model(target_model)
     # mixModel.createTable(Chromosome.genes_to_numpy(pop), True, 'umd')
     # mixModel.EMstacking()
     # mixModel.mutate()
     print('sample start')
     offsprings = mixModel.sample(sample_size)
     print('sample end')
     print('selecting start')
     idx = np.random.randint(sample_size, size=sub_sample_size)
     offsprings = offsprings[idx]  # Creating sub_samples of samples
     print('selecting end')
     offsprings = np.array(
         [Chromosome(offspring) for offspring in offsprings])
     sfitness = np.zeros(sub_sample_size)
     print('fitness_calc start')
     for i in range(sub_sample_size):
         sfitness[i] = offsprings[i].fitness_calc(problem)
     print('fitness_calc end')
     self.fitness = np.mean(sfitness)
     self.fitness_calc_time = time() - start
     # best_offspring = np.max(offsprings)
     return self.fitness, offsprings
Exemple #2
0
    def _transfer_ea(self):

        while True:

            mixModel = MixtureModel(self.src_models)

            self.list_lock.acquire()
            target_array = np.array(self.shared_array[:])
            self.list_lock.release()

            mixModel.createTable(target_array, True, 'umd')
            mixModel.EMstacking()
            mixModel.mutate()

            offsprings = mixModel.sample(self.sample_size)
            offsprings = np.array(
                [Chromosome(offspring) for offspring in offsprings])
            # alpha_rep = np.concatenate((alpha_rep, mixModel.alpha), axis=0)
            # print('Mixture coefficients: %s' % np.array(mixModel.alpha))

            # Fitness Calculation
            for j in range(self.sample_size):
                offsprings[j].fitness_calc(self.problem)

            self.shared_queue.put(offsprings)
Exemple #3
0
    def fitness_calc_pole(self,
                          net,
                          cart,
                          s_len,
                          src_models,
                          target_model,
                          sample_size,
                          solution_found=None,
                          mutation_vec=None,
                          prev_samples=None,
                          efficient_version=False):

        start = time()
        if not efficient_version or (mutation_vec is None):
            normalized_alpha = self.genes / np.sum(self.genes)
        else:
            normalized_alpha = np.clip(mutation_vec, 0, None)
        mixModel = MixtureModel(src_models, alpha=normalized_alpha)
        mixModel.add_target_model(target_model)

        if efficient_version:
            pass
        else:
            offsprings = mixModel.sample(sample_size)
            # idx = np.random.randint(sample_size, size=sub_sample_size)
            # offsprings = offsprings[idx] # Creating sub_samples of samples
            # print('selecting end')
            offsprings = np.array(
                [ChromosomePole(offspring) for offspring in offsprings])

            func_eval_num = 0
            sfitness = np.zeros(sample_size)
            # print('fitness_calc start')
            for i in range(sample_size):
                sfitness[i] = offsprings[i].fitness_calc(net, cart, s_len)
                if not solution_found.value:
                    func_eval_num += 1
                if sfitness[i] - 2000 > -0.0001:
                    solution_found.value = True
            # print('fitness_calc end')
            self.fitness = np.mean(sfitness)
            # print('sample end')
            # print('selecting start')

            self.fitness_calc_time = time() - start
            # best_offspring = np.max(offsprings)
            return self.fitness, offsprings, func_eval_num
Exemple #4
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
Exemple #5
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
Exemple #6
0
def transfer_bandit(problem,
                    src_models,
                    n_vars,
                    psize=100,
                    sample_size=100,
                    gen=100,
                    muc=10,
                    mum=10,
                    reps=1,
                    delta=2,
                    build_model=False):

    if not src_models:
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()

    init_func = lambda n: np.random.rand(n)
    alpha = list()
    prob = list()

    model_num = len(src_models)
    pop = None
    for rep in range(reps):
        print('------------------------ rep: {} ---------------------'.format(
            rep))
        start = time()
        alpha_rep = []

        pop = get_pop_init(psize, n_vars, init_func, p_type='arm')
        for i in range(psize):
            pop[i].fitness_calc(*problem)

        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)

        fitness_hist[rep, 0, :] = fitness

        prob_rep = np.zeros((gen, model_num))
        prob_rep[0, :] = (1 / model_num) * np.ones(
            model_num)  # Initial uniform probablity of src model selection
        cum_rew = np.zeros((model_num))  # Initial source rewards

        fitness_time[rep, 0] = time() - start
        print('Generation 0 best fitness = %f' % bestfitness)
        for i in range(1, gen):
            start = time()
            if i % delta == 0:

                cfitness = np.zeros(sample_size)

                # Selecting the the probability model
                idx = roulette_wheel_selection(
                    prob_rep[i - 1, :]
                )  # Selecting a model using roulette wheel selection technique

                sel_model = [src_models[idx]]

                # Applying EM algorithm and sampling from the mixture model
                mixModel = MixtureModel(sel_model)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'mvarnorm')

                mixModel.EMstacking()

                alpha_rep.append(mixModel.alpha)

                mixModel.mutate(version='bandit')

                offsprings_tmp = mixModel.sample(sample_size)

                # Calculating Fitness
                offsprings = np.array([
                    ChromosomeKA(offspring_tmp)
                    for offspring_tmp in offsprings_tmp
                ])

                for j in range(sample_size):
                    cfitness[j] = offsprings[j].fitness_calc(*problem)

                # Getting reward using importance sampling
                rew = mixModel.reward(model_num, offsprings_tmp, cfitness)

                # Updating probablities and rewards using exp3 algorithm
                prob_rep[i, :], cum_rew = EXP3(model_num, rew, idx, cum_rew,
                                               prob_rep[i - 1])

                #################################################################
                # print('Mixture coefficients: %s' % np.array(mixModel.alpha))
            else:
                # Crossover & Mutation
                randlist = np.random.permutation(psize)
                offsprings = np.ndarray(psize, dtype=object)
                for j in range(0, psize, 2):
                    offsprings[j] = ChromosomeKA(n_vars)
                    offsprings[j + 1] = ChromosomeKA(n_vars)
                    p1 = randlist[j]
                    p2 = randlist[j + 1]
                    offsprings[j].genes, offsprings[j +
                                                    1].genes = sbx_crossover(
                                                        pop[p1], pop[p2], muc,
                                                        n_vars)
                    offsprings[j].mutation(mum, n_vars)
                    offsprings[j + 1].mutation(mum, n_vars)

                # Fitness Calculation
                cfitness = np.zeros(psize)
                for j in range(psize):
                    cfitness[j] = offsprings[j].fitness_calc(*problem)

                prob_rep[i, :] = prob_rep[i - 1, :]

            if i % delta == 0:
                print('cfitness mean: ', np.mean(cfitness))

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start

            if fitness[0] > bestfitness:
                bestfitness = fitness[0]

            print('Generation %d best fitness = %f' % (i, bestfitness))

        alpha.append(alpha_rep)
        prob.append(prob_rep)

    return fitness_hist, alpha, prob, fitness_time
Exemple #7
0
def transfer_ea(problem,
                src_models,
                n_vars,
                psize=100,
                sample_size=100,
                gen=100,
                muc=10,
                mum=10,
                reps=1,
                delta=2,
                build_model=False):

    if not src_models:
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()

    init_func = lambda n: np.random.rand(n)

    pop = None
    for rep in range(reps):
        print('------------------------ rep: {} ---------------------'.format(
            rep))
        start = time()
        alpha_rep = []
        pop = get_pop_init(psize, n_vars, init_func, p_type='arm')

        cfitness = np.zeros(psize)
        for j in range(psize):
            cfitness[j] = pop[j].fitness_calc(*problem)

        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)
        fitness_hist[rep, 0, :] = fitness
        fitness_time[rep, 0] = time() - start
        print('Generation 0 best fitness = %f' % bestfitness)

        for i in range(1, gen):
            start = time()
            if i % delta == 0:

                mixModel = MixtureModel(src_models)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'mvarnorm')
                mixModel.EMstacking()

                alpha_rep = np.concatenate((alpha_rep, mixModel.alpha), axis=0)

                mixModel.mutate()
                offsprings = mixModel.sample(sample_size)
                offsprings = np.array(
                    [ChromosomeKA(offspring) for offspring in offsprings])
                print('Mixture coefficients: %s' % np.array(mixModel.alpha))
            else:
                # Crossover & Mutation
                randlist = np.random.permutation(psize)
                offsprings = np.ndarray(psize, dtype=object)
                for j in range(0, psize, 2):
                    offsprings[j] = ChromosomeKA(n_vars)
                    offsprings[j + 1] = ChromosomeKA(n_vars)
                    p1 = randlist[j]
                    p2 = randlist[j + 1]
                    offsprings[j].genes, offsprings[j +
                                                    1].genes = sbx_crossover(
                                                        pop[p1], pop[p2], muc,
                                                        n_vars)
                    offsprings[j].mutation(mum, n_vars)
                    offsprings[j + 1].mutation(mum, n_vars)

            # Fitness Calculation
            cfitness = np.zeros(psize)
            for j in range(psize):
                cfitness[j] = offsprings[j].fitness_calc(*problem)

            if i % delta == 0:
                print('cfitness mean: ', np.mean(cfitness))

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start

            if fitness[0] > bestfitness:
                bestfitness = fitness[0]

            print('Generation %d best fitness = %f' % (i, bestfitness))

        alpha.append(alpha_rep)

    return fitness_hist, alpha, fitness_time
Exemple #8
0
def transfer_ea(sLen,
                src_models,
                psize=50,
                gen=100,
                muc=10,
                mum=10,
                reps=1,
                delta=2,
                build_model=True):

    if not src_models:
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    init_func = lambda n: 12 * np.random.rand(n) - 6

    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()

    cart = PoledCart(sLen)

    n_input = 6
    n_hidden = 10
    n_output = 1
    net = Net(n_input, n_hidden, n_output)
    n_vars = net.nVariables

    pop = None
    func_eval_nums = []
    for rep in range(reps):
        print('-------------------- rep: {} -------------------'.format(rep))
        start = time()
        alpha_rep = []
        func_eval_num = 0
        solution_found = False
        pop = get_pop_init(psize, n_vars, init_func, p_type='double_pole')
        for j in range(psize):
            pop[j].fitness_calc(net, cart, sLen)
            if not solution_found:
                func_eval_num += 1
            if pop[j].fitness - 2000 > -0.0001:
                solution_found = True
        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)
        fitness_hist[rep, 0, :] = fitness
        fitness_time[rep, 0] = time() - start
        print('Generation 0 best fitness = %f' % bestfitness)

        for i in range(1, gen):
            start = time()
            if i % delta == 0:
                mixModel = MixtureModel(src_models)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'mvarnorm')
                
                mixModel.EMstacking()
                alpha_rep = np.concatenate((alpha_rep, mixModel.alpha), axis=0)
                
                mixModel.mutate()
                offsprings = mixModel.sample(psize)
                
                offsprings = np.array(
                    [ChromosomePole(offspring) for offspring in offsprings])
                # print('Mixture coefficients: %s' % np.array(mixModel.alpha))
            else:
                # Crossover & Mutation
                randlist = np.random.permutation(psize)
                offsprings = np.ndarray(psize, dtype=object)
                for j in range(0, psize, 2):
                    offsprings[j] = ChromosomePole(n_vars)
                    offsprings[j + 1] = ChromosomePole(n_vars)
                    p1 = randlist[j]
                    p2 = randlist[j + 1]
                    offsprings[j].genes, offsprings[j +
                                                    1].genes = sbx_crossover(
                                                        pop[p1], pop[p2], muc,
                                                        n_vars)
                    offsprings[j].mutation(mum, n_vars)
                    offsprings[j + 1].mutation(mum, n_vars)

            # Fitness Calculation
            cfitness = np.zeros(psize)
            for j in range(psize):
                cfitness[j] = offsprings[j].fitness_calc(net, cart, sLen)
                if not solution_found:
                    func_eval_num += 1
                if cfitness[j] - 2000 > -0.0001:
                    solution_found = True

            if i % delta == 0:
                print('cfitness mean: ', np.mean(cfitness))

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start

            if fitness[0] > bestfitness:
                bestfitness = fitness[0]

            print('Generation %d best fitness = %f' % (i, bestfitness))
            if fitness[0] - 2000 > -0.0001 and build_model:
                print('Solution found!')
                fitness_hist[rep, i:, :] = fitness[0]
                break

        func_eval_nums.append(func_eval_num if solution_found else None)

        alpha.append(alpha_rep)

    model = None
    print('fitness_hist: ', fitness_hist[0, -1, 0])
    if build_model and fitness_hist[0, -1, 0] - 2000 > -0.0001:
        model = ProbabilisticModel('mvarnorm')
        print('build model input shape: ',
              Chromosome.genes_to_numpy(pop).shape)
        model.buildModel(Chromosome.genes_to_numpy(pop))
        print("Model built successfully!")
        # src_model = model
    else:
        print("Evolutionary algorithm didn't reach the criteria!")

    if build_model:
        return fitness_hist[0, ...], alpha, fitness_time[0, ...], model
    else:
        return fitness_hist, alpha, fitness_time, func_eval_nums
def transfer_ea(problem,
                dims,
                reps,
                trans,
                psize=50,
                gen=100,
                src_models=[],
                time_limits=None,
                sample_size=None):

    if time_limits is not None:
        assert len(
            time_limits
        ) == reps, "time_limits length does not match the repetition numbers"
    else:
        time_limits = [float('inf')] * reps

    if sample_size is None:
        sample_size = psize

    if trans['transfer'] and (not src_models):
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    init_func = lambda n: np.round(np.random.rand(n))
    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()

    time_passed = 0

    for rep in range(reps):
        print('-------------------- rep: {} -------------------'.format(rep))
        alpha_rep = []
        start = time()
        pop = get_pop_init(psize, dims, init_func)
        for i in range(psize):
            pop[i].fitness_calc(problem)

        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)
        fitness_hist[rep, 0, :] = fitness
        fitness_time[rep, 0] = time() - start
        time_passed = fitness_time[rep, 0]
        print('Generation 0 best fitness = %f' % bestfitness)
        for i in range(1, gen):
            start = time()
            if trans['transfer'] and i % trans['delta'] == 0:
                mixModel = MixtureModel(src_models)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'umd')
                mixModel.EMstacking()
                alpha_rep.append(mixModel.alpha)
                mixModel.mutate()
                offsprings = mixModel.sample(sample_size)

                offsprings = np.array(
                    [Chromosome(offspring) for offspring in offsprings])
                print('Mixture coefficients: %s' % np.array(mixModel.alpha))

            else:
                # Crossover & Mutation
                offsprings = total_crossover(pop)
                for j in range(psize):
                    offsprings[j].mutation(1 / dims)

            # Fitness Calculation
            cfitness = np.zeros(psize)
            for j in range(psize):
                cfitness[j] = offsprings[j].fitness_calc(problem)

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            bestfitness = fitness[0]
            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start
            print('Generation %d best fitness = %f' % (i, bestfitness))
            time_passed += fitness_time[rep, i]
            if time_limits[rep] < time_passed:
                break
        alpha.append(alpha_rep)

    return fitness_hist, alpha, fitness_time
Exemple #10
0
def transfer_ea(problem,
                dims,
                delta,
                psize=100,
                gen=100,
                create_model=True,
                stop_condition=True,
                src_models=[]):
    # load probabilistic models

    if src_models is None or len(src_models) == 0:
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    init_func = lambda n: np.round(np.random.rand(n))
    fitness_hist = np.zeros([gen, psize])
    fitness_time = np.zeros((gen))

    alpha_rep = []
    counter = 0

    pop = get_pop_init(psize, dims, init_func)
    start = time()
    for i in range(psize):
        pop[i].fitness_calc(problem)

    bestfitness = np.max(pop).fitness
    fitness = Chromosome.fitness_to_numpy(pop)
    fitness_hist[0, :] = fitness
    fitness_time[0] = time() - start
    print('Generation 0 best fitness = %f' % bestfitness)
    for i in range(1, gen):

        start = time()
        if i % delta == 0:
            mixModel = MixtureModel(src_models)
            mixModel.createTable(Chromosome.genes_to_numpy(pop), True, 'umd')
            mixModel.EMstacking()
            mixModel.mutate()
            offsprings = mixModel.sample(psize)

            offsprings = np.array(
                [Chromosome(offspring) for offspring in offsprings])
            alpha_rep.append(mixModel.alpha)
            print('Mixture coefficients: %s' % np.array(mixModel.alpha))

        else:
            # Crossover & Mutation
            offsprings = total_crossover(pop)
            for j in range(psize):
                offsprings[j].mutation(1 / dims)

        # Fitness Calculation
        cfitness = np.zeros(psize)
        for j in range(psize):
            cfitness[j] = offsprings[j].fitness_calc(problem)

        # Selection
        pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                       np.concatenate((fitness, cfitness)),
                                       psize)

        fitness_hist[i, :] = fitness

        if fitness[0] > bestfitness:
            bestfitness = fitness[0]
            counter = 0
        else:
            counter += 1

        fitness_time[i] = time() - start

        if counter == 20 and stop_condition:
            fitness_hist[i:, :] = fitness[0]
            break

        print('Generation %d best fitness = %f' % (i, bestfitness))

    best_sol = pop[0]
    src_model = None
    if create_model:
        src_model = ProbabilisticModel('umd')
        print('build model input shape: ',
              Chromosome.genes_to_numpy(pop).shape)
        src_model.buildModel(Chromosome.genes_to_numpy(pop))
        print('probOne_noisy: ', src_model.probOne_noisy)
        print('probZero_noisy: ', src_model.probZero_noisy)

    return src_model, best_sol, fitness_hist, fitness_time
Exemple #11
0
def transfer_continues_ea(fitness_func,
                          init_func,
                          dim,
                          src_models,
                          psize=100,
                          gen=100,
                          muc=10,
                          mum=10,
                          reps=1,
                          delta=2,
                          build_model=True):

    if not src_models:
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    class ChromosomeEA(ChromosomePole):
        def fitness_calc(self):
            if self.fitness != float('-inf'):
                return self.fitness
            self.fitness = fitness_func(self.genes)
            return self.fitness

    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()

    pop = None
    for rep in range(reps):
        alpha_rep = []
        pop = get_pop_init(psize, dim, init_func, p_type=ChromosomeEA)
        start = time()
        for j in range(psize):
            pop[j].fitness_calc()

        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)
        fitness_hist[rep, 0, :] = fitness
        fitness_time[rep, 0] = time() - start
        print('Generation 0 best fitness = %f' % bestfitness)

        for i in range(1, gen):
            start = time()
            if i % delta == 0:
                mixModel = MixtureModel(src_models)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'mvarnorm')
                mixModel.EMstacking()
                mixModel.mutate()
                offsprings = mixModel.sample(psize)
                offsprings = np.array(
                    [ChromosomeEA(offspring) for offspring in offsprings])
                alpha_rep = np.concatenate((alpha_rep, mixModel.alpha), axis=0)


#                 print('Mixture coefficients: %s' % np.array(mixModel.alpha))
            else:
                # Crossover & Mutation
                randlist = np.random.permutation(psize)
                offsprings = np.ndarray(psize, dtype=object)
                for j in range(0, psize, 2):
                    offsprings[j] = ChromosomeEA(dim)
                    offsprings[j + 1] = ChromosomeEA(dim)
                    p1 = randlist[j]
                    p2 = randlist[j + 1]
                    offsprings[j].genes, offsprings[j +
                                                    1].genes = sbx_crossover(
                                                        pop[p1], pop[p2], muc,
                                                        dim)
                    offsprings[j].mutation(mum, dim)
                    offsprings[j + 1].mutation(mum, dim)

            # Fitness Calculation
            cfitness = np.zeros(psize)
            for j in range(psize):
                cfitness[j] = offsprings[j].fitness_calc()

            if i % delta == 0:
                print('cfitness mean: ', np.mean(cfitness))
                print('cfitness max: ', np.max(cfitness))
                print('cfitness min: ', np.min(cfitness))

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start

            if fitness[0] > bestfitness:
                bestfitness = fitness[0]

            print('Generation %d best fitness = %f' % (i, bestfitness))
            print('Generation %d mean fitness = %f' % (i, np.mean(fitness)))

        print()

        alpha.append(alpha_rep)

    model = None
    print('fitness_hist: ', fitness_hist[0, -1, 0])
    if build_model:
        model = ProbabilisticModel('mvarnorm')
        print('build model input shape: ',
              Chromosome.genes_to_numpy(pop).shape)
        model.buildModel(Chromosome.genes_to_numpy(pop))
        print('model mean: ', model.mean)
        print("Model built successfully!")
    else:
        print("Evolutionary algorithm didn't reach the criteria!")

    if build_model:
        return fitness_hist[0, ...], alpha, fitness_time[
            0, ...], model, np.max(pop).genes
    else:
        return fitness_hist, alpha, fitness_time
Exemple #12
0
    def fitness_calc(
        self,
        problem,
        src_models,
        target_model,
        sample_size,
        sub_sample_size,
        mutation_vec=None,
        prev_samples=None,
        efficient_version=False
    ):  # You can implement this in a more optmized way using vectorizatioin but it will hurt modularity
        start = time()
        if not efficient_version or (mutation_vec is None):
            normalized_alpha = self.genes / np.sum(self.genes)
        else:
            normalized_alpha = np.clip(mutation_vec, 0, None)
        mixModel = MixtureModel(src_models, alpha=normalized_alpha)
        mixModel.add_target_model(target_model)
        # mixModel.createTable(Chromosome.genes_to_numpy(pop), True, 'umd')
        # mixModel.EMstacking()
        # mixModel.mutate()
        # print('sample start')
        if efficient_version:
            offsprings = mixModel.sample_dic(sample_size)
            flat_offsprings = np.array([])
            is_prev = (prev_samples is not None) and (mutation_vec is not None)
            if is_prev:
                # removing samples
                removing_samples = np.clip(
                    np.ceil(mutation_vec * sample_size).astype(int), None, 0)
                # print('Removing Samples: {}'.format(removing_samples))
                for i in range(len(removing_samples)):
                    if removing_samples[i] != 0:
                        r_num = len(prev_samples[i]) + removing_samples[i]
                        if r_num != 0:
                            prev_samples[i] = np.random.choice(prev_samples[i],
                                                               r_num,
                                                               replace=False)
                        else:
                            prev_samples[i] = None

                # adding sapmles
                for i in range(len(offsprings)):
                    if offsprings[i] is not None:
                        if prev_samples[i] is None:
                            prev_samples[i] = offsprings[i]
                        else:
                            offspring_add = [
                                Chromosome(offspring)
                                for offspring in offsprings[i]
                            ]
                            flat_offsprings = np.append(
                                flat_offsprings, offspring_add)
                            prev_samples[i] = np.append(prev_samples[i],
                                                        offspring_add,
                                                        axis=0)
                offsprings = prev_samples
            self.fitness = 0
            count = 0
            # fitness calc
            for i in range(len(offsprings)):
                if offsprings[i] is not None:
                    if not is_prev:
                        offspring_add = np.array([
                            Chromosome(offspring)
                            for offspring in offsprings[i]
                        ])
                        flat_offsprings = np.append(flat_offsprings,
                                                    offspring_add)
                        offsprings[i] = offspring_add
                    for j in range(len(offsprings[i])):
                        self.fitness += offsprings[i][j].fitness_calc(problem)
                        count += 1
            # print('number of all samples: {}'.format(count))
            self.fitness /= count

            return self.fitness, flat_offsprings, offsprings
        else:
            offsprings = mixModel.sample(sample_size)
            idx = np.random.randint(sample_size, size=sub_sample_size)
            offsprings = offsprings[idx]  # Creating sub_samples of samples
            # print('selecting end')
            offsprings = np.array(
                [Chromosome(offspring) for offspring in offsprings])
            sfitness = np.zeros(sub_sample_size)
            # print('fitness_calc start')
            for i in range(sub_sample_size):
                sfitness[i] = offsprings[i].fitness_calc(problem)
            # print('fitness_calc end')
            self.fitness = np.mean(sfitness)
            # print('sample end')
            # print('selecting start')

            self.fitness_calc_time = time() - start
            # best_offspring = np.max(offsprings)
            return self.fitness, offsprings
def transfer_ea(problem, dims, reps, trans, psize=50, gen=100, src_models=[]):
    # load probabilistic models

    if trans['transfer'] and (not src_models):
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    init_func = lambda n: np.round(np.random.rand(n))
    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()

    for rep in range(reps):
        alpha_rep = []

        pop = get_pop_init(psize, dims, init_func)
        start = time()
        for i in range(psize):
            pop[i].fitness_calc(problem)

        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)
        fitness_hist[rep, 0, :] = fitness
        fitness_time[rep, 0] = time() - start
        print('Generation 0 best fitness = %f' % bestfitness)
        for i in range(1, gen):
            start = time()
            if trans['transfer'] and i % trans['delta'] == 0:
                mixModel = MixtureModel(src_models)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'umd')
                mixModel.EMstacking()
                mixModel.mutate()
                offsprings = mixModel.sample(psize)
                offsprings = np.array(
                    [Chromosome(offspring) for offspring in offsprings])
                alpha_rep = np.concatenate((alpha_rep, mixModel.alpha), axis=0)
                print('Mixture coefficients: %s' % np.array(mixModel.alpha))
            else:
                # Crossover & Mutation
                offsprings = total_crossover(pop)
                for j in range(psize):
                    offsprings[j].mutation(1 / dims)

            # Fitness Calculation
            cfitness = np.zeros(psize)
            for j in range(psize):
                cfitness[j] = offsprings[j].fitness_calc(problem)

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            bestfitness = fitness[0]
            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start
            print('Generation %d best fitness = %f' % (i, bestfitness))

        alpha.append(alpha_rep)

    return fitness_hist, alpha, fitness_time
def transfer_bandit(problem,
                    dims,
                    reps,
                    trans,
                    psize=50,
                    gen=100,
                    src_models=[],
                    time_limits=None,
                    sample_size=None):

    if time_limits is not None:
        assert len(
            time_limits
        ) == reps, "time_limits length does not match the repetition numbers"
    else:
        time_limits = [float('inf')] * reps

    if sample_size is None:
        sample_size = psize

    if trans['transfer'] and (not src_models):
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    init_func = lambda n: np.round(np.random.rand(n))
    model_num = len(src_models)
    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()
    prob = list()
    avg_runtime = 0
    time_passed = 0
    for rep in range(reps):
        print('------------------------ rep: {} ---------------------'.format(
            rep))
        start = time()
        alpha_rep = []

        pop = get_pop_init(psize, dims, init_func)
        for i in range(psize):
            pop[i].fitness_calc(problem)

        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)

        fitness_hist[rep, 0, :] = fitness

        prob_rep = np.zeros((gen, model_num))
        prob_rep[0, :] = (1 / model_num) * np.ones(
            model_num)  # Initial uniform probablity of src model selection
        cum_rew = np.zeros((model_num))  # Initial source rewards

        fitness_time[rep, 0] = time() - start
        time_passed = fitness_time[rep, 0]
        print('Generation 0 best fitness = %f' % bestfitness)
        for i in range(1, gen):
            start = time()
            cfitness = np.zeros(psize)
            if trans['transfer'] and i % trans['delta'] == 0:
                # Selecting the the probability model
                idx = roulette_wheel_selection(
                    prob_rep[i - 1, :]
                )  # Selecting a model using roulette wheel selection technique

                sel_model = [src_models[idx]]

                # Applying EM algorithm and sampling from the mixture model
                mixModel = MixtureModel(sel_model)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'umd')

                mixModel.EMstacking()
                alpha_rep.append(mixModel.alpha)

                mixModel.mutate(version='bandit')
                offsprings_tmp = mixModel.sample(sample_size)

                # Calculating Fitness
                offsprings = np.array([
                    Chromosome(offspring_tmp)
                    for offspring_tmp in offsprings_tmp
                ])

                for j in range(sample_size):
                    cfitness[j] = offsprings[j].fitness_calc(problem)

                # Getting reward using importance sampling
                rew = mixModel.reward(model_num, offsprings_tmp, cfitness)

                # Updating probablities and rewards using exp3 algorithm
                prob_rep[i, :], cum_rew = EXP3(model_num, rew, idx, cum_rew,
                                               prob_rep[i - 1])

                #################################################################
                # print('Probabilities: {}'.format(prob_rep[i,:]))
                print('Mixture coefficients: %s' % np.array(mixModel.alpha))
            else:
                # Crossover & Mutation
                offsprings = total_crossover(pop)
                for j in range(psize):
                    offsprings[j].mutation(1 / dims)

                # Fitness Calculation
                for j in range(psize):
                    cfitness[j] = offsprings[j].fitness_calc(problem)

                prob_rep[i, :] = prob_rep[i - 1, :]
                # print('prob_rep[i,:] ', prob_rep[i,:])

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            bestfitness = fitness[0]
            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start
            time_passed += fitness_time[rep, i]
            print('Generation %d best fitness = %f' % (i, bestfitness))
            if time_limits[rep] < time_passed:
                break

        alpha.append(alpha_rep)
        prob.append(prob_rep)
    return fitness_hist, alpha, prob, fitness_time
def transfer_bandit(sLen,
                    src_models,
                    psize=50,
                    gen=100,
                    muc=10,
                    mum=10,
                    reps=1,
                    delta=2,
                    build_model=True):

    if not src_models:
        raise ValueError(
            'No probabilistic models stored for transfer optimization.')

    init_func = lambda n: 12 * np.random.rand(n) - 6

    fitness_hist = np.zeros([reps, gen, psize])
    fitness_time = np.zeros((
        reps,
        gen,
    ))
    alpha = list()
    prob = list()
    cart = PoledCart(sLen)

    n_input = 6
    n_hidden = 10
    n_output = 1
    net = Net(n_input, n_hidden, n_output)
    n_vars = net.nVariables

    model_num = len(src_models)

    pop = None
    func_eval_nums = []
    for rep in range(reps): 
        print('-------------------- rep: {} -------------------'.format(rep))
        start = time()
        alpha_rep = []
        prob_rep = np.zeros((gen, model_num))
        prob_rep[0, :] = (1 / model_num) * np.ones(
            model_num)  # Initial uniform probablity of src model selection
        cum_rew = np.zeros((model_num))  # Initial source rewards

        func_eval_num = 0
        solution_found = False

        pop = get_pop_init(psize, n_vars, init_func, p_type='double_pole')
        for j in range(psize):
            pop[j].fitness_calc(net, cart, sLen)
            if not solution_found:
                func_eval_num += 1
            if pop[j].fitness - 2000 > -0.0001:
                solution_found = True

        bestfitness = np.max(pop).fitness
        fitness = Chromosome.fitness_to_numpy(pop)
        fitness_hist[rep, 0, :] = fitness
        fitness_time[rep, 0] = time() - start
        print('Generation 0 best fitness = %f' % bestfitness)

        for i in range(1, gen):
            start = time()
            cfitness = np.zeros(psize)
            if i % delta == 0:
                idx = roulette_wheel_selection(
                    prob_rep[i - 1]
                )  # Selecting a model using roulette wheel selection technique

                sel_model = [src_models[idx]]

                mixModel = MixtureModel(sel_model)
                mixModel.createTable(Chromosome.genes_to_numpy(pop), True,
                                     'mvarnorm')
                mixModel.EMstacking()

                alpha_rep = np.concatenate((alpha_rep, mixModel.alpha), axis=0)
                
                mixModel.mutate()
                offsprings_tmp = mixModel.sample(psize)

                
                # Calculating Fitness
                offsprings = np.array([
                    ChromosomePole(offspring_tmp)
                    for offspring_tmp in offsprings_tmp
                ])
                for j in range(psize):
                    cfitness[j] = offsprings[j].fitness_calc(net, cart, sLen)
                    if not solution_found:
                        func_eval_num += 1
                    if cfitness[j] - 2000 > -0.0001:
                        solution_found = True
                
                rew = mixModel.reward(model_num, offsprings_tmp, cfitness)
                # Updating probablities and rewards using exp3 algorithm
                prob_rep[i, :], cum_rew = EXP3(model_num, rew, idx, cum_rew,
                                               prob_rep[i - 1])

                #################################################################
                # print('Probabilities: {}'.format(prob_rep[i,:]))
                # print('Mixture coefficients: %s' % np.array(mixModel.alpha))
            else:
                # Crossover & Mutation
                randlist = np.random.permutation(psize)
                offsprings = np.ndarray(psize, dtype=object)
                for j in range(0, psize, 2):
                    offsprings[j] = ChromosomePole(n_vars)
                    offsprings[j + 1] = ChromosomePole(n_vars)
                    p1 = randlist[j]
                    p2 = randlist[j + 1]
                    offsprings[j].genes, offsprings[j +
                                                    1].genes = sbx_crossover(
                                                        pop[p1], pop[p2], muc,
                                                        n_vars)
                    offsprings[j].mutation(mum, n_vars)
                    offsprings[j + 1].mutation(mum, n_vars)

                # Fitness Calculation
                cfitness = np.zeros(psize)
                for j in range(psize):
                    cfitness[j] = offsprings[j].fitness_calc(net, cart, sLen)
                    if not solution_found:
                        func_eval_num += 1
                    if cfitness[j] - 2000 > -0.0001:
                        solution_found = True

                prob_rep[i, :] = prob_rep[i - 1, :]

            if i % delta == 0:
                print('cfitness mean: ', np.mean(cfitness))

            # Selection
            pop, fitness = total_selection(np.concatenate((pop, offsprings)),
                                           np.concatenate((fitness, cfitness)),
                                           psize)

            fitness_hist[rep, i, :] = fitness
            fitness_time[rep, i] = time() - start

            if fitness[0] > bestfitness:
                bestfitness = fitness[0]

            print('Generation %d best fitness = %f' % (i, bestfitness))
            print(fitness[0])
            if fitness[0] - 2000 > -0.0001 and build_model:
                print('Solution found!')
                fitness_hist[rep, i:, :] = fitness[0]
                break

        func_eval_nums.append(func_eval_num if solution_found else None)
        alpha.append(alpha_rep)
        prob.append(prob_rep)

    return fitness_hist, alpha, fitness_time, func_eval_nums, prob