示例#1
0
def test_equation_discoverer_hyperopt():
    B = -2.56; a = 0.4; ts = np.linspace(0.45, 0.87, 5)
    ys = (ts+B)*np.exp(a*ts); xs = np.exp(a*ts)
    data = np.hstack((ts.reshape(-1, 1), xs.reshape(-1, 1), ys.reshape(-1, 1)))

    np.random.seed(20)
    ED = EqDisco(data = data,
                 task = None,
                 task_type = "differential",
                 time_index = 0,
                 target_variable_index = -1,
                 variable_names=["t", "x", "y"],
                 sample_size = 2,
                 verbosity = 1,
                 estimation_settings={
                     # "optimizer": hyperopt_fit,
                     "optimizer": 'hyperopt',
                     "hyperopt_space_fn": hp.qnormal,
                     "hyperopt_space_args": (0.4, 0.5, 1/1000),
                     "hyperopt_max_evals": 100,
                 }
                 )
    ED.generate_models()
    ED.fit_models()

    def assert_line(models, i, expr, error, tol=1e-9, n=100):
        assert str(ED.models[i].get_full_expr())[:n] == expr[:n]
        assert abs(ED.models[i].get_error() - error) < tol
    assert_line(ED.models, 0, "y", 0.06518775248116751)
    assert_line(ED.models, 1, "0.401*x + y", 2.5584839808071686e-07, n=6)
    return
示例#2
0
def test_equation_discoverer():
    np.random.seed(0)
    def f(x):
        return 2.0 * (x[:,0] + 0.3)
	
    X = np.linspace(-1, 1, 20).reshape(-1,1)
    Y = f(X).reshape(-1,1)
    data = np.hstack((X,Y))
        
    ED = EqDisco(data = data,
                 task = None,
                 target_variable_index = -1,
                 sample_size = 2,
                 verbosity = 1)
    
    ED.generate_models()
    ED.fit_models()
    
    #print(ED.models[0].get_error())
    assert np.abs(ED.models[0].get_error() - 0.72842105) < 1e-6
    assert np.abs(ED.models[1].get_error() - 0.59163899) < 1e-6
示例#3
0
def test_equation_discoverer_ODE():
    B = -2.56; a = 0.4; ts = np.linspace(0.45, 0.87, 5)
    ys = (ts+B)*np.exp(a*ts); xs = np.exp(a*ts)
    data = np.hstack((ts.reshape(-1, 1), xs.reshape(-1, 1), ys.reshape(-1, 1)))

    np.random.seed(20)
    ED = EqDisco(data = data,
                 task = None,
                 task_type = "differential",
                 time_index = 0,
                 target_variable_index = -1,
                 variable_names=["t", "x", "y"],
                 sample_size = 2,
                 verbosity = 1)
    ED.generate_models()
    ED.fit_models()

    def assert_line(models, i, expr, error, tol=1e-9, n=100):
        assert str(models[i].get_full_expr())[:n] == expr[:n]
        assert abs(models[i].get_error() - error) < tol
    assert_line(ED.models, 0, "y", 0.06518775248116751)
    assert_line(ED.models, 1, "0.400266188520229*x + y", 2.5265653001321905e-09, n=6)
    return
示例#4
0
from ProGED.equation_discoverer import EqDisco
from ProGED.parameter_estimation import DE_fit, hyperopt_fit #, DE_fit_metamodel


np.random.seed(0)

ED = EqDisco(data = data,
             task = None,
             task_type = "differential",
             time_index = 0,
             target_variable_index = aquation[0][0],  # aquation = [123] -> target = 1 -> ([t,x,y,z]->x)
             variable_names=["t", "x", "y", "z"],
             generator = "grammar",
             generator_template_name = "polynomial",
             generator_settings={
                 # "variables": ["'x'", "'y'"],
                 "p_S": [0.4, 0.6],
                 "p_T": [0.4, 0.6],
                 "p_vars": [0.33, 0.33, 0.34],
                 "p_R": [1, 0],
                 "p_F": [],
                 "functions": [],
             },
             sample_size = sample_size,
             verbosity = 4)

ED.generate_models()
ED.fit_models(
    estimation_settings={
        "timeout": 115,
        "max_ode_steps": 10**6,
示例#5
0
def oeis_eq_disco(seq_id: str, is_direct: bool, order: int):
    """Run eq. discovery of given OEIS sequence."""

    data = grid(order, np.array(list(csv[seq_id])), is_direct)
    variable_names_ = [f"an_{i}" for i in range(order, 0, -1)] + ["an"]
    variable_names = ["n"] + variable_names_ if is_direct else variable_names_
    variables = [f"'{an_i}'" for an_i in variable_names[:-1]]
    # print(variable_names)
    # print(variables)
    # print(data.shape, type(data), data)

    np.random.seed(random_seed)
    ED = EqDisco(
        data=data,
        task=None,
        target_variable_index=-1,
        # variable_names=["an_2", "an_1", "an"],
        variable_names=variable_names,
        # sample_size = 16,  # for recursive
        # sample_size = 10,  # for direct fib
        sample_size=sample_size,
        # sample_size = 50,  # for recursive
        # sample_size = 38,  # for recursive
        # sample_size = 100,  # for recursive
        verbosity=0,
        # verbosity = 3,
        generator="grammar",
        generator_template_name=grammar_template_name,
        # generator_settings={"variables": ["'an_2'", "'an_1'"],
        generator_settings={
            "variables": variables,
            "p_T": p_T,
            "p_R": p_R
        },
        estimation_settings={
            # "verbosity": 3,
            "verbosity": 1,
            # "verbosity": 0,
            "task_type": task_type,
            # "task_type": "algebraic",
            # "task_type": "integer-algebraic",
            # "task_type": "oeis_recursive_error",  # bad idea
            "lower_upper_bounds": lower_upper_bounds,
            # "lower_upper_bounds": (-1000, 1000),  # najde (1001 pa ne) rec fib
            #  "lower_upper_bounds": (-100, 100),  # ne najde DE
            #  "lower_upper_bounds": (-25, 25),  # DA ne najde
            #  "lower_upper_bounds": (-11, 11),  # shgo limit
            #  "lower_upper_bounds": (-14, 14),  # DA dela
            # "lower_upper_bounds": (-2, 2),  # for direct
            # "lower_upper_bounds": (-4, 4),  # for direct fib
            # "lower_upper_bounds": (-5, 5),
            # "lower_upper_bounds": (-8, 8),  # long enough for brute
            # "optimizer": 'differential_evolution',
            "optimizer": optimizer,
            # "optimizer": 'hyperopt',
            "timeout": timeout,
            # "timeout": 1,
            # "timeout": 13,
            # "timeout_privilege": 30,

            ## hyperopt:
            # "hyperopt_max_evals": 3250,
            # "hyperopt_max_evals": 550,  # finds if result=min(10**6, hyperopt...)
            # "hyperopt_max_evals": 50,
            # "hyperopt_max_evals": 750,
            # "hyperopt_max_evals": 2000,
            # "hyperopt_max_evals": 700,
            # "hyperopt_max_evals": 500,
            # "hyperopt_max_evals": 300,
            # "hyperopt_space_fn": hp.randint,
            # "hyperopt_space_fn": hp.choice,
            # "hyperopt_space_fn": hp.loguniform,  # Seems working, but loss 785963174750.8921 in 2000 evals.
            # "hyperopt_space_fn": hp.qnormal,
            # "hyperopt_space_fn": hp.normal,
            # "hyperopt_space_args": (lower_upper_bounds[0], lower_upper_bounds[1]),
            # "hyperopt_space_args": ([-2, -1, 0, 1],),
            # "hyperopt_space_args": (0, 5, 1),
            # "hyperopt_space_args": (0, 2),  # Seems working for hp.normal, but loss 1492702199922.7131 in 2000 evals.
            # "hyperopt_space_args": [lower_upper_bounds[0]],
            # "hyperopt_space_kwargs": {"high": lower_upper_bounds[1]},
            # "hyperopt_algo": tpe.suggest,  # Works, with 1000 evals and (-2, 2) finds recursive.
            # "hyperopt_algo": rand.suggest,
        })

    # for i in range(0, 100):
    #     np.random.seed(i)
    #     print("seed", i)
    #     print(ED)
    #     ED.generate_models()
    #     print(ED.models)
    #     ED.models = None

    # 1/0
    ED.generate_models()
    print(ED.models)
    # 1/0
    seq_start = time.perf_counter()
    ED.fit_models()
    seq_cpu_time = time.perf_counter() - seq_start
    print(f"\nParameter fitting for sequence {seq_id} "
          f"took {seq_cpu_time} secconds.")
    print("\nFinal score:")
    for m in ED.models:
        print(
            f"model: {str(m.get_full_expr()):<30}; error: {m.get_error():<15}")
    return
示例#6
0
#  131,137,139,149,151,157,163,167,173,179,181,191,
#  193,197,199,211,223,227,229,233,239,241,251,257,
#  263,269,271][:40+1]
# oeis = oeis_primes
fibs = np.array(oeis).reshape(-1, 1)
ts = np.array([i for i in range(40+1)]).reshape(-1, 1)
data = np.hstack((ts, fibs))

np.random.seed(0)
ED = EqDisco(data = data,
            task = None,
            target_variable_index = -1,
            variable_names=["n", "an"],
            sample_size = 10,
            verbosity = 0,
            generator = "grammar", 
            generator_template_name = "polynomial",
            generator_settings={"variables":["'n'"]},
            estimation_settings={"verbosity": 0, "task_type": "algebraic", "lower_upper_bounds": 
            # (-5, 5)} 
            # (-10,8)} # Last bound where it still finds.
            (0,1)}  # Returns wrong error on windows.
            )
ED.generate_models()
ED.fit_models()
# print(ED.models)
print("\n\nFinal score:")
for m in ED.models:
    print(f"model: {str(m.get_full_expr()):<30}; error: {m.get_error():<15}")
    
phi = (1+5**(1/2))/2
psi = (1-5**(1/2))/2
示例#7
0
p_R = [0.9, 0.1]
# # grammar = grammar_from_template("polynomial", {"variables": variables, "p_R": p_R, "p_T": p_T})
# # np.random.seed(0)
# # # print(grammar.generate_one())
# # # models = generate_models(grammar, symbols, strategy_settings = {"N":500})
# # # print(models)

np.random.seed(0)
# seed 0 , size 20 (16)
# seed3 size 15 an-1 + an-2 + c3
ED = EqDisco(data = data,
            task = None,
            target_variable_index = -1,
            variable_names=["an_2", "an_1", "an"],
            sample_size = 16,
            verbosity = 0,
            generator = "grammar", 
            generator_template_name = "polynomial",
            generator_settings={"variables": ["'an_2'", "'an_1'"], "p_T": p_T, "p_R": p_R}
            ,estimation_settings={"verbosity": 1, "task_type": "algebraic", "lower_upper_bounds": 
            (0, 2)} # meja, ko se najde priblizno: (-10,8)}# , "timeout": np.inf}
            )
# # print(data, data.shape)
ED.generate_models()
ED.fit_models()
# # try:
# #     print(12/0)

print(ED.models)
# # print(ED.get_results())
# # print(ED.get_stats())
# print("\nFinal score:")