|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import sympy as sp |
| 4 | +from autora.experiment_runner.synthetic.abstract.equation import equation_experiment |
| 5 | +from autora.experimentalist.random import random_pool |
| 6 | +from autora.state import StandardState, estimator_on_state, on_state |
| 7 | +from autora.theorist.bms import BMSRegressor |
| 8 | +from autora.variable import ValueType, Variable, VariableCollection |
| 9 | + |
| 10 | +#################################################################################### |
| 11 | +## Define initial data |
| 12 | +#################################################################################### |
| 13 | + |
| 14 | +#### Define variable data #### |
| 15 | +iv = Variable(name="x", value_range=(0, 2 * np.pi), allowed_values=np.linspace(0, 2 * np.pi, 30)) |
| 16 | +dv = Variable(name="y", type=ValueType.REAL) |
| 17 | +variables = VariableCollection(independent_variables=[iv], dependent_variables=[dv]) |
| 18 | + |
| 19 | +#### Define seed condition data #### |
| 20 | +conditions = random_pool(variables, num_samples=10, random_state=0) |
| 21 | + |
| 22 | +#################################################################################### |
| 23 | +## Define experimentalist |
| 24 | +#################################################################################### |
| 25 | + |
| 26 | +experimentalist = on_state(random_pool, output=["conditions"]) |
| 27 | + |
| 28 | +#################################################################################### |
| 29 | +## Define experiment runner |
| 30 | +#################################################################################### |
| 31 | + |
| 32 | +sin_experiment = equation_experiment( |
| 33 | + sp.simplify("sin(x)"), variables.independent_variables, variables.dependent_variables[0] |
| 34 | +) |
| 35 | +sin_runner = sin_experiment.experiment_runner |
| 36 | + |
| 37 | +experiment_runner = on_state(sin_runner, output=["experiment_data"]) |
| 38 | + |
| 39 | +#################################################################################### |
| 40 | +## Define theorist |
| 41 | +#################################################################################### |
| 42 | + |
| 43 | +theorist = estimator_on_state(BMSRegressor(epochs=100)) |
| 44 | + |
| 45 | +#################################################################################### |
| 46 | +## Define state |
| 47 | +#################################################################################### |
| 48 | + |
| 49 | +s = StandardState( |
| 50 | + variables=variables, conditions=conditions, experiment_data=pd.DataFrame(columns=["x", "y"]) |
| 51 | +) |
| 52 | + |
| 53 | +#################################################################################### |
| 54 | +## Cycle through the state |
| 55 | +#################################################################################### |
| 56 | + |
| 57 | +print("Pre-Defined State:") |
| 58 | +print(f"Number of datapoints collected: {len(s['experiment_data'])}") |
| 59 | +print(f"Derived models: {s['models']}") |
| 60 | +print("\n") |
| 61 | + |
| 62 | +for i in range(5): |
| 63 | + s = experimentalist(s, num_samples=10, random_state=42) |
| 64 | + s = experiment_runner(s, added_noise=1.0, random_state=42) |
| 65 | + s = theorist(s) |
| 66 | + print(f"\nCycle {i+1} Results:") |
| 67 | + print(f"Number of datapoints collected: {len(s['experiment_data'])}") |
| 68 | + print(f"Derived models: {s['models']}") |
| 69 | + print("\n") |
0 commit comments