def createTable(self, solutions, CV, modelType, probs_RL=None):
        if CV:
            self.add_target_solutions(solutions, modelType)
            self.alpha = (1/self.nModels) * np.ones(self.nModels)
            nSol = solutions.shape[0]
            self.nSol = nSol
            self.probTable = np.ones([nSol, self.nModels])
 
            if probs_RL is None:
                for j in range(self.nModels-1):
                    self.probTable[:, j] = self.model_list[j].pdfEval(solutions) 
            else:
                for j in range(0, self.nModels-1):
                    self.probTable[:, j] = self.model_list[j].pdfEval(solutions) # Time complexity: O(pd)

            for i in range(nSol):  # Leave-one-out cross validation
                x = np.concatenate((solutions[:i, :], solutions[i+1:, :]))
                tModel = ProbabilisticModel(modelType=modelType)
                tModel.buildModel(x)
                self.probTable[i, -1] = tModel.pdfEval(solutions[[i], :])
        else:
            nSol = solutions.shape[0]
            self.probTable = np.ones([nSol, self.nModels])
            for j in range(self.nModels):
                self.probTable[:, j] = self.model_list[j].pdfEval(solutions)
            self.nSol = nSol
 def add_target_solutions(self, solutions, modelType):
     if not self.__target_model_added:
         self.nModels = self.nModels + 1
         self.model_list.append(ProbabilisticModel(modelType=modelType))
         self.model_list[-1].buildModel(solutions)
         self.__target_model_added = True
     else:
         raise Exception('Target model is already added.')
def create_unitation_optimum(global_optimum=True,
                             problem='onemin',
                             psize=100,
                             dims=120):
    pop = None
    if problem == 'onemin':
        pop = np.zeros((psize, dims))
    elif problem == 'onemax':
        pop = np.ones((psize, dims))
    elif problem == 'trap5':
        pop = np.ones((psize, dims))
        if not global_optimum:
            for i in range(psize):
                for j in range(0, dims, 5):
                    if np.random.rand() > 0.5:
                        pop[i, j:j + 5] = 0
    model = ProbabilisticModel('umd')
    model.buildModel(pop.mean(0))
    def _transfer_ea(self):
        prev_samples = None
        genes_differ = None

        target_model = ProbabilisticModel(modelType='umd')

        func_eval_num = 0

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

        target_model.buildModel(target_array)

        fitness, first_specie_offsprings, eval_num = self.second_specie. \
                                fitness_calc_pole(self.net, self.cart, self.s_len, self.src_models,
                                                target_model, self.sample_size, solution_found=self.solution_found,
                                                mutation_vec=None, prev_samples=None, efficient_version=False)

        func_eval_num += eval_num

        self.shared_queue.put(first_specie_offsprings)

        while True:
            offspring = deepcopy(self.second_specie)

            genes_differ = offspring.mutation(self.mutation_strength, 0, 1)

            target_model = ProbabilisticModel(modelType='umd')

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

            target_model.buildModel(target_array)

            fitness, first_specie_offsprings, eval_num = self.second_specie. \
                                    fitness_calc_pole(self.net, self.cart, self.s_len, self.src_models,
                                                    target_model, self.sample_size, mutation_vec=None, solution_found=self.solution_found,
                                                    prev_samples=None, efficient_version=False)

            func_eval_num += eval_num
            if self.solution_found.value:
                break
            self.shared_queue.put(first_specie_offsprings)

            self.second_specie, self.mutation_strength, is_off_selected = selection_adoption(
                self.second_specie, offspring, self.mutation_strength)

        # second_species_gen_num += 1
        # while True:
        self.return_list.append([func_eval_num])

        print('Transfer Process is finished')
  def _transfer_ea(self):
    prev_samples = None
    genes_differ = None

    target_model = ProbabilisticModel(modelType='umd')

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

    target_model.buildModel(target_array)

    _, sampled_offsprings, prev_samples = \
                        self.second_specie.fitness_calc(self.problem, self.src_models, target_model, self.sample_size,
                                          self.sub_sample_size, mutation_vec=genes_differ, prev_samples=deepcopy(prev_samples),
                                          efficient_version=True)

    self.shared_queue.put(sampled_offsprings)

    while True:
      offspring = deepcopy(self.second_specie)

      genes_differ = offspring.mutation(self.mutation_strength, 0, 1)

      target_model = ProbabilisticModel(modelType='umd')

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

      target_model.buildModel(target_array)

      _, sampled_offsprings, prev_samples_tmp = \
                    offspring.fitness_calc(self.problem, self.src_models, target_model, self.sample_size,
                                      self.sub_sample_size, mutation_vec=genes_differ, prev_samples=deepcopy(prev_samples),
                                      efficient_version=True)

      self.shared_queue.put(sampled_offsprings)
      
      self.second_specie, self.mutation_strength, is_off_selected = selection_adoption(self.second_specie, offspring, self.mutation_strength)

      if is_off_selected:
        prev_samples = prev_samples_tmp
Example #6
0
def evolutionary_algorithm(sLen,
                           psize=100,
                           gen=100,
                           muc=10,
                           mum=10,
                           stop_condition=True,
                           create_model=True):

    src_model = None

    fitness_hist = np.zeros((gen, psize))
    fitness_time = np.zeros((gen))

    cart = PoledCart(sLen)

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

    init_func = lambda n: 12 * np.random.rand(n) - 6
    pop = get_pop_init(psize, n_vars, init_func, p_type='double_pole')
    start = time()

    for j in range(psize):
        pop[j].fitness_calc(net, cart, sLen)

    bestfitness = np.max(pop).fitness
    fitness = Chromosome.fitness_to_numpy(pop)
    fitness_hist[0, :] = fitness

    fitness_time[0] = time() - start
    counter = 0  # Generation Repetition without fitness improvement counter
    for i in range(1, gen):
        start = time()
        randlist = np.random.permutation(psize)
        offsprings = np.ndarray(psize, dtype=object)

        # Crossover & Mutation
        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):
            # print(pop[j].genes)
            cfitness[j] = offsprings[j].fitness_calc(net, cart, sLen)

        # 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

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

        fitness_time[i] = time() - start

    best_sol = pop[0]
    if create_model and fitness_hist[-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
    elif not create_model:
        print("Evolutionary algorithm didn't reach the criteria!")
        # src_models.append(model)

    return src_model, best_sol, fitness_hist, fitness_time
Example #7
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
Example #8
0
def transfer_cc_v2(problem, dims, reps, trans,
                   s1_psize=50, s2_psize=1, gen=100,
                   sample_size=50, sub_sample_size=50,
                   mutation_strength=1, injection_type='full', 
                   to_repititon_num=1, selection_version='v1', 
                   c=2, src_models=[], efficient_version=False, 
                   transfer_repeat_num=None):


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

  delta = trans['delta']

  init_func_s1 = lambda n: np.round(np.random.rand(n))
  
  if transfer_repeat_num is None:
    transfer_repeat_num = float('inf') # repeat in all iterations
    fitness_hist_s1 = np.ndarray([reps, int((gen/delta * (delta-1)) + gen%delta) + 1, s1_psize], dtype=object)
    fitness_hist_s2 = np.ndarray([reps, int(gen/delta), s2_psize], dtype=object)
    time_hist_s1 = np.zeros([reps, int((gen/delta * (delta-1)) + gen%delta) + 1, s1_psize], dtype=object)
    mutation_strength_hist = np.zeros([reps, int(gen/delta), s2_psize])
  else:
    fitness_hist_s1 = np.ndarray([reps, gen-transfer_repeat_num, s1_psize], dtype=object)
    fitness_hist_s2 = np.ndarray([reps, transfer_repeat_num, s2_psize], dtype=object)
    time_hist_s1 = np.zeros([reps, gen-transfer_repeat_num, s1_psize], dtype=object)
    mutation_strength_hist = np.zeros([reps, transfer_repeat_num, s2_psize])
  dims_s2 = len(src_models)+1

  best_chrom = None # Best Chromosome to inject to the first species from second species
  
  ms_value = mutation_strength
  for rep in range(reps):
      print('------------------------- Repetition {} ---------------------------'.format(rep))
      first_species = get_pop_init(s1_psize, dims, init_func_s1) # For choosing decision params
      # second_species = get_pop_init_s2(s2_psize, dims_s2) # For choosing alpha params
      start = time()
      second_specie = StrategyChromosome(dims_s2)
      for i in range(s1_psize):
        first_species[i].fitness_calc(problem)

      second_species_gen_num = 0 # used in selection version 2 for calculating the g
      second_species_gen_success_num = 0 # used in selection version 2 for calculating the g
      ea_counter = 0
      mutation_strength = ms_value
      bestfitness = np.max(first_species).fitness
      fitness = Chromosome.fitness_to_numpy(first_species)
      s2_fitness = None
      fitness_hist_s1[rep, 0, :] = first_species
      time_hist_s1[rep, 0] = time() - start
      print('Generation %d best fitness of first species = %f' % (0, bestfitness))
      start = time()
      prev_samples = None
      genes_differ = None
      for g in range(1, gen):
        # Initialize a container for the next generation representatives
        if trans['transfer'] and (g % delta == 0) and (g/delta <= transfer_repeat_num):
          ################# Add Evolution Strategy #####################
            for tg in range(to_repititon_num):
              if g/delta != 1 or tg != 0:
                print('Test Mutation: ')
                offspring = deepcopy(second_specie)
                second_species_gen_num += 1
                print ('Offspring genes before mutation: {}'.format(offspring.genes/np.sum(offspring.genes)*50))
                genes_differ = offspring.mutation(mutation_strength, 0, 1)
                print ('Offspring genes after mutation: {}'.format(offspring.genes/np.sum(offspring.genes)*50))
                print ('Genes Difference: {}'.format(genes_differ))
                print ('Genes Difference sum: {}'.format(np.sum(genes_differ)))
                print('Test Finished')
                # offsprings = total_crossover_s2(second_species)
                # for j in range(s2_psize): offsprings[j].mutation(1/dims_s2)
              else:
                offspring = second_specie

              target_model = ProbabilisticModel(modelType='umd')
              target_model.buildModel(Chromosome.genes_to_numpy(first_species))

              # print('start fitness o ina')
              # for i in range(s2_psize):

              if efficient_version:
                _, sampled_offsprings, prev_samples_tmp = offspring.fitness_calc(problem, src_models, target_model, sample_size,
                                                                sub_sample_size, mutation_vec=genes_differ, prev_samples=deepcopy(prev_samples),
                                                                efficient_version=True)
              else:
                _, sampled_offsprings = offspring.fitness_calc(problem, src_models, target_model,
                                                            sample_size, sub_sample_size)
                # if best_chrom < best_offspring: # Saving best Chromosome for future injection
                #   best_chrom = best_offspring
              # print('end fitness o ina')

                # print('hereee')
              is_off_selected = False
              if g/delta != 1 or tg != 0:
                if selection_version == 'v1':
                  second_specie, mutation_strength, is_off_selected = selection_adoption(second_specie, offspring, mutation_strength)
                elif selection_version == 'v2':
                  second_specie, mutation_strength, second_species_gen_success_num = selection_adoption_v2(second_specie, offspring, mutation_strength,
                                                                                                          second_species_gen_num, second_species_gen_success_num, c=c)
                else:
                  raise ValueError('selection_version value is wrong')

              if efficient_version and (is_off_selected or (g/delta == 1 and tg == 0)):
                prev_samples = prev_samples_tmp
              # Replacing the best chromosome found by sampling from second species with the worst chromosome of first species
              if injection_type == 'elite':
                  first_species[-1] == np.max(sampled_offsprings)
              elif injection_type == 'full':
                  first_species = total_selection_pop(np.concatenate((first_species, sampled_offsprings)), s1_psize)
                  

              fitness_hist_s2[rep, int(g/delta)-1, :] = second_specie
              mutation_strength_hist[rep, int(g/delta)-1, :]  = mutation_strength
              print('Generation %d: Best Fitness of Second Species: %s' % (g, second_specie.fitness))
              print('Best Alpha generation {}: best fitness of second species = {}'.format(g, second_specie.genes))
              print('generation {}: mutation strength = {}'.format(g, mutation_strength))
        else:
            # Crossover & Mutation
            offsprings = total_crossover(first_species)
            for j in range(s1_psize): offsprings[j].mutation(1/dims)
              
            # Fitness Calculation
            cfitness = np.zeros(s1_psize)
            for j in range(s1_psize): 
              cfitness[j] = offsprings[j].fitness_calc(problem)

            # Selection
            first_species, fitness = total_selection(np.concatenate((first_species, offsprings)),
                                      np.concatenate((fitness, cfitness)), s1_psize)

            bestfitness = fitness[0]
            fitness_hist_s1[rep, ea_counter, :] = first_species
            time_hist_s1[rep, ea_counter] = time() - start
            print('Generation %d best fitness of first species= %f' % (g, bestfitness))
            start = time()
            ea_counter += 1

  print('Finished')
  return fitness_hist_s1, fitness_hist_s2, mutation_strength_hist, time_hist_s1
Example #9
0
def transfer_cc(problem,
                src_models,
                n_vars,
                psize=100,
                sample_size=100,
                gen=100,
                muc=10,
                mum=10,
                initial_genes_value=0,
                initial_lr=.9,
                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,))

    genes_list = list()

    dims_s2 = len(src_models)+1
    init_func_es = lambda n: np.ones(n)*initial_genes_value

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

    pop = None
    target_avg_fitness = 0
    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')

        second_specie = StrategyChromosome(dims_s2, init_func=init_func_es)
        mutation_strength = np.zeros(dims_s2) -2*np.sqrt(2)
        samples_count = np.zeros(dims_s2)
        lr = initial_lr
        genes_hist = []

        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:
                target_array = Chromosome.genes_to_numpy(pop)
                shared_target_fitness = np.mean(fitness)

                second_specie_offspring = deepcopy(second_specie)

                if i // delta != 1:
                    mutation_strength[-1] = shared_target_fitness
                    second_specie_offspring. \
                      mutation_enhanced(mutation_strength, 0, 1, lr=lr)
                print('mutation_strength: ', mutation_strength)
                target_model = ProbabilisticModel(modelType='mvarnorm')

                target_model.buildModel(target_array)

                _, offsprings, mutation_strength, samples_count = second_specie_offspring . \
                                fitness_calc_enhanced(problem, src_models,
                                                target_model, sample_size, mutation_strength,
                                                samples_count, problem_type='arm')


                cfitness = Chromosome.fitness_to_numpy(offsprings)
                
                if second_specie.fitness <= second_specie_offspring.fitness:
                    second_specie = second_specie_offspring
                
                print(second_specie.genes)
                genes_hist.append(second_specie.genes)
                
                #################################################################
                # print('Probabilities: {}'.format(prob_rep[i,:]))
                # print('Genese: %s' % np.array(second_specie_offspring.genes))           
            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)
                

            # 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))

        genes_list.append(genes_hist)

    return fitness_hist, genes_list, fitness_time
Example #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
Example #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
Example #12
0
def evolutionary_algorithm(problem,
                           dims,
                           psize=100,
                           gen=100,
                           src_models=[],
                           stop_condition=True,
                           create_model=True,
                           multi_proc=False,
                           workers=1):

    fitness_hist = np.zeros((gen, psize))
    fitness_time = np.zeros((gen))

    init_func = lambda n: np.round(np.random.rand(n))
    pop = get_pop_init(psize, dims, init_func)
    start = time()

    if multi_proc:
        with concurrent.futures.ProcessPoolExecutor(
                max_workers=workers) as executor:
            processes = []
            for j in range(psize):

                proc = executor.submit(pop[j].fitness_calc, problem)
                processes.append(proc)

            fitnesses = [proc.result() for proc in processes]

            for j in range(psize):
                pop[j].fitness = fitnesses[j]
    else:
        fitnesses = np.zeros(psize)
        for j in range(psize):
            fitnesses[j] = pop[j].fitness_calc(problem)

    bestfitness = np.max(fitnesses)
    fitnesses = fitnesses
    fitness_hist[0, :] = fitnesses

    fitness_time[0] = time() - start
    counter = 0  # Generation Repetition without fitness improvement counter
    for i in range(1, gen):
        start = time()

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

        # Fitness Calculation

        if multi_proc:
            with concurrent.futures.ProcessPoolExecutor(
                    max_workers=workers) as executor:
                processes = []
                for j in range(psize):

                    proc = executor.submit(offsprings[j].fitness_calc, problem)
                    processes.append(proc)

                cfitnesses = [proc.result() for proc in processes]

                for j in range(psize):
                    offsprings[j].fitness = cfitnesses[j]
        else:
            cfitnesses = np.zeros(psize)
            for j in range(psize):
                cfitnesses[j] = offsprings[j].fitness_calc(problem)

        # Selection
        pop, fitnesses = total_selection(
            np.concatenate((pop, offsprings)),
            np.concatenate((fitnesses, cfitnesses)), psize)

        fitness_hist[i, :] = fitnesses
        print('Generation %d best fitness = %f' % (i, bestfitness))

        if fitnesses[0] > bestfitness:
            bestfitness = fitnesses[0]
            counter = 0
        else:
            counter += 1
        fitness_time[i] = time() - start
        if counter == 20 and stop_condition:
            fitness_hist[i:, :] = fitnesses[0]
            break

    best_sol = pop[0]
    if create_model:
        model = ProbabilisticModel('umd')
        model.buildModel(Chromosome.genes_to_numpy(pop))
        src_models.append(model)

    return src_models, best_sol, fitness_hist, fitness_time
Example #13
0
def continues_ea(fitness_func,
                 init_func,
                 dim,
                 psize=100,
                 gen=100,
                 muc=10,
                 mum=10,
                 stop_condition=True,
                 create_model=True):

    src_model = None

    fitness_hist = np.zeros((gen, psize))
    fitness_time = np.zeros((gen))

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

    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[0, :] = fitness

    fitness_time[0] = time() - start
    print('Generation %d best fitness = %f' % (0, bestfitness))
    counter = 0  # Generation Repetition without fitness improvement counter
    for i in range(1, gen):
        start = time()
        randlist = np.random.permutation(psize)
        offsprings = np.ndarray(psize, dtype=object)

        # Crossover & Mutation
        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()

        # 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

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

        fitness_time[i] = time() - start

    best_sol = pop[0]
    if create_model:
        model = ProbabilisticModel('mvarnorm')
        print('build model input shape: ',
              ChromosomePole.genes_to_numpy(pop).shape)
        model.buildModel(ChromosomePole.genes_to_numpy(pop))
        print('model mean: ', model.mean)
        print("Model built successfully!")
        src_model = model
    elif not create_model:
        print("Evolutionary algorithm didn't reach the criteria!")

    return src_model, best_sol, fitness_hist, fitness_time
def transfer_cc(sLen,
                src_models,
                psize=100,
                gen=100,
                muc=10,
                mum=10,
                reps=1,
                delta=2,
                initial_genes_value=1/14,
                initial_lr=0.9,
                build_model=True):

    s_len = sLen
    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,
    ))
    genes_list = list()

    dims_s2 = len(src_models) + 1
    init_func_es = lambda n: np.ones(n) * initial_genes_value

    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)

    shared_target_fitness = None
    target_array = None

    pop = None
    func_eval_nums = []
    for rep in range(reps):
        print('-------------------- rep: {} -------------------'.format(rep))
        genes_hist = []

        start = time()
        # Evolution Strategy Initialization
        second_specie = StrategyChromosome(dims_s2, init_func=init_func_es)
        second_specie.fitness = 0
        mutation_strength = np.zeros(dims_s2)
        samples_count = np.zeros(dims_s2)
        lr = initial_lr

        func_eval_num = 0
        solution_found = IsFound()
        solution_found.value = 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.value:
                func_eval_num += 1
            if pop[j].fitness - 2000 > -0.0001:
                solution_found.value = 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:
                target_array = Chromosome.genes_to_numpy(pop)
                shared_target_fitness = np.mean(fitness)


                second_specie_offspring = deepcopy(second_specie)

                if i // delta != 1:
                    mutation_strength[-1] = shared_target_fitness
                    second_specie_offspring. \
                      mutation_enhanced(mutation_strength, 0, 1, lr=lr)

                target_model = ProbabilisticModel(modelType='mvarnorm')

                target_model.buildModel(target_array)

                _, offsprings, mutation_strength, samples_count, eval_num = second_specie_offspring. \
                          fitness_calc_pole(net, cart, s_len, src_models,
                                          target_model, psize, mutation_strength,
                                          samples_count, solution_found=solution_found)

                func_eval_num += eval_num

                cfitness = Chromosome.fitness_to_numpy(offsprings)

                if second_specie.fitness <= second_specie_offspring.fitness:
                    second_specie = second_specie_offspring
                
                genes_hist.append(second_specie.genes)
                #################################################################
                # print('Probabilities: {}'.format(prob_rep[i,:]))
                # print('Genese: %s' % np.array(second_specie_offspring.genes))
            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.value:
                        func_eval_num += 1
                    if cfitness[j] - 2000 > -0.0001:
                        # func_eval_num = (i*psize + j+1)
                        solution_found.value = 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.value else None)
        genes_list.append(genes_hist)

    return fitness_hist, genes_list, fitness_time, func_eval_nums
def evolutionary_algorithm(problem,
                           dims,
                           psize=100,
                           gen=100,
                           src_models=[],
                           stop_condition=True,
                           create_model=True):

    fitness_hist = np.zeros((gen, psize))
    fitness_time = np.zeros((gen))

    init_func = lambda n: np.round(np.random.rand(n))
    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
    counter = 0  # Generation Repetition without fitness improvement counter
    for i in range(1, gen):
        start = time()

        # 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
        print('Generation %d best fitness = %f' % (i, bestfitness))

        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
    best_sol = pop[0]
    if create_model:
        model = ProbabilisticModel('umd')
        print('build model input shape: ',
              Chromosome.genes_to_numpy(pop).shape)
        model.buildModel(Chromosome.genes_to_numpy(pop))
        src_models.append(model)

    return src_models, best_sol, fitness_hist, fitness_time
def evolutionary_algorithm(sLen,
                           psize=100,
                           gen=100,
                           muc=10,
                           mum=10,
                           stop_condition=True,
                           create_model=True):

    src_model = None

    fitness_hist = np.zeros((gen, psize))
    fitness_time = np.zeros((gen))

    cart = PoledCart(sLen)

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

    init_func = lambda n: 12 * np.random.rand(n) - 6
    pop = get_pop_init(psize, n_vars, init_func, p_type='double_pole')
    start = time()

    for j in range(psize):
        pop[j].fitness_calc(net, cart, sLen)

    bestfitness = np.max(pop).fitness
    fitness = Chromosome.fitness_to_numpy(pop)
    fitness_hist[0, :] = fitness

    fitness_time[0] = time() - start
    counter = 0  # Generation Repetition without fitness improvement counter
    for i in range(1, gen):
        start = time()
        randlist = np.random.permutation(psize)
        offsprings = np.ndarray(psize, dtype=object)

        # Crossover & Mutation
        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):
            # print(pop[j].genes)
            cfitness[j] = offsprings[j].fitness_calc(net, cart, sLen)

        # 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

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

        fitness_time[i] = time() - start

    best_sol = pop[0]
    if create_model and fitness_hist[-1, 0] - 2000 > -0.0001:
        model = ProbabilisticModel('mvarnorm')
        model.buildModel(Chromosome.genes_to_numpy(pop))
        src_model = model
    elif not create_model:
        # src_models.append(model)

    return src_model, best_sol, fitness_hist, 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


def transfer_ea(sLen,
                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.')

    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):
        alpha_rep = []
        func_eval_num = 0
        solution_found = False
        pop = get_pop_init(psize, n_vars, init_func, p_type='double_pole')
        start = time()
        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()
                mixModel.mutate()
                offsprings = mixModel.sample(psize)
                offsprings = np.array(
                    [ChromosomePole(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] = 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))
            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)
        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 get_args():
    pass

def check_args(args):
    if args.sample_size < args.sub_sample_size:
        raise ValueError('sub_sample_size has greater value than sample_size')


def main_source(s_poles_length,
                target_pole_len,
                reps=50,
                gen=100,
                src_save_dir='models/pole_models/src_model'):
    
    # src_models = np.ndarray(len(s_poles_length), dtype=object)
    src_models = []
    src_model = None
    if os.path.isfile(src_save_dir + '_{}.pkl'.format(s_poles_length[0])):
        src_model = Tools.load_from_file(src_save_dir +
                                         '_{}'.format(s_poles_length[0]))
        print(
            '---------------------- source model loaded (length: {}) ---------------------'
            .format(s_poles_length[0]))
    else:
        src_model, _, _, _ = evolutionary_algorithm(s_poles_length[0],
                                                    psize=100,
                                                    gen=gen,
                                                    muc=10,
                                                    mum=10,
                                                    stop_condition=True,
                                                    create_model=True)
        Tools.save_to_file(src_save_dir + '_{}'.format(s_poles_length[0]),
                           src_model)
        print(
            '---------------------- source model created (length: {}) ---------------------'
            .format(s_poles_length[0]))
    src_models.append(src_model)
    for i, s_len in enumerate(s_poles_length[1:]):
        if os.path.isfile(src_save_dir + '_{}.pkl'.format(s_len)):
            src_model = Tools.load_from_file(src_save_dir +
                                             '_{}'.format(s_len))
            src_models.append(src_model)
            print(
                '---------------------- source model loaded (length: {}) ---------------------'
                .format(s_len))
        else:
            while (True):
                print('-------------- S_Length: {} ------------'.format(s_len))
                _, _, _, src_model = transfer_ea(s_len,
                                                 src_models,
                                                 psize=100,
                                                 gen=gen,
                                                 muc=10,
                                                 mum=10,
                                                 reps=1,
                                                 build_model=True)

                if src_model is not None:
                    Tools.save_to_file(src_save_dir + '_{}'.format(s_len),
                                       src_model)
                    src_models.append(src_model)
                    print(
                        '---------------------- source model created (length: {}) ---------------------'
                        .format(s_len))
                    break

    fitness_hist, alpha, fitness_time, func_eval_nums, prob =  \
          transfer_bandit(target_pole_len, src_models, psize=50, gen=100,
                  delta=2, muc=10, mum=10, reps=reps, build_model=False)

    Tools.save_to_file(
        'single_bandit_pole_outcome',
        [fitness_hist, alpha, fitness_time, func_eval_nums, prob])
    solved_indices = np.array(func_eval_nums) != None
    print('Function Evaluations: {}'.format(np.mean(np.array(func_eval_nums)[solved_indices])))
    print('Solutions found: {}/{}'.format(np.sum(solved_indices), reps))


if __name__ == '__main__':
    src_poles_length = [
        0.1, 0.2, 0.3, 0.4, 0.5, 0.55, 0.6, 0.65, 0.675, 0.7, 0.725, 0.75,
        0.775
    ]
    target_pole_len = 0.825
    main_source(src_poles_length, target_pole_len, reps=50, gen=100)
Example #17
0
def transfer_cc_v1(problem, dims, reps, trans,
                   s1_psize=50, s2_psize=20, gen=100,
                   sample_size=50, sub_sample_size=50,
                   injection_type='elite', src_models=[]):

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

  delta = trans['delta']

  init_func_s1 = lambda n: np.round(np.random.rand(n))
  
  fitness_hist_s1 = np.ndarray([reps, int((gen/delta * (delta-1)) + gen%delta) + 1, s1_psize], dtype=object)
  fitness_hist_s2 = np.ndarray([reps, int(gen/delta), s2_psize], dtype=object)
  time_hist_s1 = np.zeros([reps, int((gen/delta * (delta-1)) + gen%delta) + 1], dtype=object)
  dims_s2 = len(src_models)+1

  best_chrom = None # Best Chromosome to inject to the first species from second species
  # Init with random a representative for each species
  # representatives = [np.random.choice(species[i]) for i in range(len(species))]
  for rep in range(reps):
      print('------------------------- Repetition {} ---------------------------'.format(rep))
      first_species = get_pop_init(s1_psize, dims, init_func_s1) # For choosing decision params
      second_species = get_pop_init_s2(s2_psize, dims_s2) # For choosing alpha params
      start = time()
      
      for i in range(s1_psize):
        first_species[i].fitness_calc(problem)

      bestfitness = np.max(first_species).fitness
      fitness = Chromosome.fitness_to_numpy(first_species)
      s2_fitness = None
      fitness_hist_s1[rep, 0, :] = first_species
      time_hist_s1[rep, 0] = time() - start
      print('Generation %d best fitness of first species = %f' % (0, bestfitness))
      start = time()
      for g in range(1, gen):
        # Initialize a container for the next generation representatives
        if trans['transfer'] and g % delta == 0:
            if g/delta != 1:
              offsprings = total_crossover_s2(second_species)
              for j in range(s2_psize): offsprings[j].mutation(1/dims_s2)
            else:
              offsprings = second_species

            target_model = ProbabilisticModel(modelType='umd')
            target_model.buildModel(Chromosome.genes_to_numpy(first_species))

            s2_cfitness = np.zeros(s2_psize)
            best_chrom = Chromosome(dims)
            sampled_offsprings = np.ndarray(s2_psize*sub_sample_size, dtype=object)

            for i in range(s2_psize):
              s2_cfitness[i], offsprings = offsprings[i].fitness_calc(problem, src_models,
                                                                      target_model, sample_size,
                                                                      sub_sample_size)
              sampled_offsprings[:i*sub_sample_size] = offsprings

            # Injecting elite chromosomes to first species
            if injection_type == 'elite':
              first_species[-1] == np.max(sampled_offsprings)
            elif injection_type == 'full':
              first_species = total_selection_pop(np.concatenate((first_species, sampled_offsprings)), s1_psize)

            # Selecting elite chromosome from second species
            if g/delta != 1:
              second_species, s2_fitness = total_selection(np.concatenate((second_species, offsprings)),
                                np.concatenate((s2_fitness, s2_cfitness)), s2_psize)
            else:
              second_species, s2_fitness = total_selection(offsprings, s2_cfitness, s2_psize)
            # Replacing the best chromosome found by sampling from second species with the worst chromosome of first species
            # first_species[-1] = best_chrom 

            best_fitness_s2 = s2_fitness[0]
            fitness_hist_s2[rep, int(g/delta)-1, :] = second_species
            print('Generation %d: Best Fitness of Second Species: %s' % (g, best_fitness_s2))
            print('Best Alpha generation {}: best fitness of second species = {}'.format(g, second_species[0].genes))
        else:
            # Crossover & Mutation
            offsprings = total_crossover(first_species)
            for j in range(s1_psize): offsprings[j].mutation(1/dims)
              
            # Fitness Calculation
            cfitness = np.zeros(s1_psize)
            for j in range(s1_psize): 
              cfitness[j] = offsprings[j].fitness_calc(problem)

            # Selection
            first_species, fitness = total_selection(np.concatenate((first_species, offsprings)),
                                      np.concatenate((fitness, cfitness)), s1_psize)

            bestfitness = fitness[0]
            fitness_hist_s1[rep, int(np.ceil(g/delta*(delta-1))), :] = first_species
            time_hist_s1[rep, int(np.ceil(g/delta*(delta-1)))] = time() - start
            print('Generation %d best fitness of first species= %f' % (g, bestfitness))
            start = time()
  print('Finished')
  return fitness_hist_s1, fitness_hist_s2, time_hist_s1
def transfer_cc(problem,
                dims,
                reps,
                delta,
                psize=50,
                gen=100,
                src_models=[],
                time_limits=None,
                sample_size=None,
                initial_genes_value=0.2,
                initial_lr=0.9,
                max_sampling_num=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 max_sampling_num is None:
        max_sampling_num = sample_size

    if 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,
    ))
    genes_list = list()

    dims_s2 = len(src_models) + 1
    init_func_es = lambda n: np.ones(n) * initial_genes_value

    shared_target_fitness = None
    target_array = None

    time_passed = 0

    for rep in range(reps):
        print('-------------------- rep: {} -------------------'.format(rep))

        start = time()
        genes_hist = []

        # Evolution Strategy Initialization
        second_specie = StrategyChromosome(dims_s2, init_func=init_func_es)
        second_specie.fitness = 0
        mutation_strength = np.zeros(dims_s2)
        samples_count = np.zeros(dims_s2)
        lr = initial_lr

        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()
            cfitness = np.zeros(psize)
            if i % delta == 0:
                target_array = Chromosome.genes_to_numpy(pop)
                shared_target_fitness = np.mean(fitness)

                second_specie_offspring = deepcopy(second_specie)

                if i // delta != 1:
                    mutation_strength[-1] = shared_target_fitness
                    second_specie_offspring. \
                      mutation_enhanced(mutation_strength, 0, 1, lr=lr)

                target_model = ProbabilisticModel(modelType='umd')

                target_model.buildModel(target_array)

                _, offsprings, mutation_strength, samples_count = second_specie_offspring. \
                            fitness_calc_enhanced(problem, src_models, target_model,
                                                sample_size, mutation_strength,
                                                samples_count, max_sampling_num)

                cfitness = Chromosome.fitness_to_numpy(offsprings)

                if second_specie.fitness <= second_specie_offspring.fitness:
                    second_specie = second_specie_offspring

                genes_hist.append(second_specie.genes)
                #################################################################
                # print('Probabilities: {}'.format(prob_rep[i,:]))
                # print('Genese: %s' % np.array(second_specie_offspring.genes))

            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

        genes_list.append(genes_hist)

    return fitness_hist, genes_list, fitness_time