Example #1
0
def discover_Equation(tokens, evolutionary_operator, basic_terms, **kwargs): 
    t1 = datetime.datetime.now()
    
    iter_number = set_argument('iter_number', kwargs, 100)
    pop_size = set_argument('pop_size', kwargs, 8)
    eq_len = set_argument('eq_len', kwargs, 6)
    max_factors = set_argument('max_factors', kwargs, 2)
    test_output = set_argument('test_output', kwargs, True)

    population = Population(evolutionary_operator, tokens, pop_size = pop_size, basic_terms = basic_terms, 
                            eq_len = eq_len, max_factors_in_terms = max_factors)

    best_fitnesses = population.Initiate_Evolution(iter_number = iter_number,
                                                   log_file = None, test_indicators = test_output)
#    filename = 'Fitness_alpha_' + str(alpha) + '.npy'
#    save_fitness(filename, best_fitnesses)
    
    print('Achieved best fitness:', best_fitnesses[-1])#, 'with alpha =', alpha)
    
    population.Calculate_True_Weights()
                    
    t2 = datetime.datetime.now()

    eq_text_form = population.text_form()
    print(eq_text_form)
Example #2
0
def Discover_Equtaion(tokens, token_params, basic_terms, **kwargs):
    '''
    
    
    
    '''
    t1 = datetime.datetime.now()

    assert 'evaluator' in kwargs.keys() and 'eval_params' in kwargs.keys()

    alpha = Set_argument('alpha', kwargs, 1)
    a_proc = Set_argument('a_proc', kwargs, 0.2)
    r_crossover = Set_argument('r_crossover', kwargs, 0.3)
    r_param_mutation = Set_argument('r_param_mutation', kwargs, 0.7)
    r_mutation = Set_argument('r_mutation', kwargs, 0.3)
    mut_chance = Set_argument('mut_chance', kwargs, 0.6)
    iter_number = Set_argument('iter_number', kwargs, 100)
    pop_size = Set_argument('pop_size', kwargs, 8)
    eq_len = Set_argument('eq_len', kwargs, 6)
    max_factors = Set_argument('max_factors', kwargs, 2)
    test_output = Set_argument('test_output', kwargs, False)

    population = Population(kwargs['evaluator'],
                            kwargs['eval_params'],
                            tokens,
                            token_params,
                            pop_size=pop_size,
                            basic_terms=basic_terms,
                            a_proc=a_proc,
                            r_crossover=r_crossover,
                            r_param_mutation=r_param_mutation,
                            r_mutation=r_mutation,
                            mut_chance=mut_chance,
                            alpha=alpha,
                            eq_len=eq_len,
                            max_factors_in_terms=max_factors)

    best_fitnesses = population.Initiate_Evolution(iter_number=iter_number,
                                                   estimator_type='Lasso',
                                                   log_file=None,
                                                   test_indicators=test_output)

    print('Achieved best fitness:', best_fitnesses[-1], 'with alpha =', alpha)

    population.Calculate_True_Weights(kwargs['evaluator'],
                                      kwargs['eval_params'])

    t2 = datetime.datetime.now()
    res = ((t1, t2), (population.target_term, population.zipped_list),
           best_fitnesses)
    print('Discovered equation:')

    print('- {', end='')
    for key, value in population.target_term.items():
        if value['power'] != 0 and key != '1':
            print(' ', key, ':', value, end='')
    print('} + ', end='')

    for term_idx in range(len(population.zipped_list)):
        print(population.zipped_list[term_idx][1], '* {', end='')
        for key, value in population.zipped_list[term_idx][0].items():
            if value['power'] != 0 and key != '1':
                print(' ', key, ':', value, end='')
        if term_idx != len(population.zipped_list) - 1:
            print('} + ', end='')
        else:
            print('} = 0')