diff --git a/examples/example_caching.py b/examples/example_caching.py index 4123bdd9..aafc4825 100644 --- a/examples/example_caching.py +++ b/examples/example_caching.py @@ -46,7 +46,7 @@ def inner_objective(ind): expr = ind.to_sympy() loss = [] for x0 in np.linspace(-2.0, 2.0, 100): - y = float(expr[0].subs({"x_0": x0}).evalf()) + y = float(expr.subs({"x_0": x0}).evalf()) loss.append((f_target(x0) - y) ** 2) time.sleep(0.25) # emulate long fitness evaluation diff --git a/examples/example_differential_evo_regression.py b/examples/example_differential_evo_regression.py index 18f1a958..06e388c6 100644 --- a/examples/example_differential_evo_regression.py +++ b/examples/example_differential_evo_regression.py @@ -176,7 +176,7 @@ def recording_callback(pop): ax_function.set_xlabel(r"$x$") -print(f"Final expression {pop.champion.to_sympy()[0]} with fitness {pop.champion.fitness}") +print(f"Final expression {pop.champion.to_sympy()} with fitness {pop.champion.fitness}") history_fitness = np.array(history["fitness_parents"]) ax_fitness.plot(np.max(history_fitness, axis=1), label="Champion") diff --git a/examples/example_evo_regression.py b/examples/example_evo_regression.py index 57744749..a070081f 100644 --- a/examples/example_evo_regression.py +++ b/examples/example_evo_regression.py @@ -84,7 +84,7 @@ def objective(individual, target_function, seed): "ignore", message="invalid value encountered in double_scalars" ) try: - y[i] = f_graph(x_i)[0] + y[i] = f_graph(x_i[0], x_i[1]) except ZeroDivisionError: individual.fitness = -np.inf return individual @@ -123,7 +123,7 @@ def evolution(f_target): Individual Individual with the highest fitness in the last generation """ - population_params = {"n_parents": 10, "seed": 8188211} + population_params = {"n_parents": 10, "seed": 818821} genome_params = { "n_inputs": 2, @@ -191,7 +191,7 @@ def recording_callback(pop): x_0_range = np.linspace(-5.0, 5.0, 20) x_1_range = np.ones_like(x_0_range) * 2.0 # fix x_1 such than 1d plot makes sense - y = [f_graph([x_0, x_1_range[0]]) for x_0 in x_0_range] + y = [f_graph(x_0, x_1_range[0]) for x_0 in x_0_range] y_target = target_function(np.hstack([x_0_range.reshape(-1, 1), x_1_range.reshape(-1, 1)])) ax_function.plot(x_0_range, y_target, lw=2, alpha=0.5, label="Target") diff --git a/examples/example_fec_caching.py b/examples/example_fec_caching.py index 03f6e168..c6d9795d 100644 --- a/examples/example_fec_caching.py +++ b/examples/example_fec_caching.py @@ -61,7 +61,7 @@ def inner_objective(ind): loss = [] for x_0 in np.linspace(-2.0, 2.0, 100): - y = f([x_0]) + y = f(x_0) loss.append((f_target(x_0) - y) ** 2) time.sleep(0.25) # emulate long fitness evaluation diff --git a/examples/example_hurdles.py b/examples/example_hurdles.py index 1db7b52b..c31013f7 100644 --- a/examples/example_hurdles.py +++ b/examples/example_hurdles.py @@ -44,7 +44,7 @@ def f_target(x): - return x[0] ** 2 + 1.0 + return x ** 2 + 1.0 # %% @@ -73,8 +73,8 @@ def objective_one(individual): # the callable returned from `to_func` accepts and returns # lists; accordingly we need to pack the argument and unpack # the return value - y = f([x])[0] - loss += (f_target([x]) - y) ** 2 + y = f(x) + loss += (f_target(x) - y) ** 2 individual.fitness = -loss / n_function_evaluations @@ -97,8 +97,8 @@ def objective_two(individual): # the callable returned from `to_func` accepts and returns # lists; accordingly we need to pack the argument and unpack # the return value - y = f([x])[0] - loss += (f_target([x]) - y) ** 2 + y = f(x) + loss += (f_target(x) - y) ** 2 individual.fitness = -loss / n_function_evaluations @@ -184,8 +184,8 @@ def recording_callback(pop): f = pop.champion.to_func() x = np.linspace(-5.0, 5.0, 20) -y = [f([x_i]) for x_i in x] -y_target = [f_target([x_i]) for x_i in x] +y = [f(x_i) for x_i in x] +y_target = [f_target(x_i) for x_i in x] ax_function.plot(x, y_target, lw=2, alpha=0.5, label="Target") ax_function.plot(x, y, "x", label="Champion") diff --git a/examples/example_local_search_evolution_strategies.py b/examples/example_local_search_evolution_strategies.py index 85702669..c2d4ba32 100644 --- a/examples/example_local_search_evolution_strategies.py +++ b/examples/example_local_search_evolution_strategies.py @@ -37,7 +37,7 @@ def f_target(x): - return np.e * x[:, 0] ** 2 + 1.0 + np.pi + return np.e * x ** 2 + 1.0 + np.pi # %% @@ -61,9 +61,9 @@ def inner_objective(ind, seed): f = ind.to_numpy() rng = np.random.RandomState(seed) batch_size = 500 - x = rng.uniform(-5, 5, size=(batch_size, 1)) + x = rng.uniform(-5, 5, size=batch_size) y = f(x) - return -np.mean((f_target(x) - y[:, 0]) ** 2) + return -np.mean((f_target(x) - y) ** 2) def objective(individual, seed): @@ -165,7 +165,7 @@ def recording_callback(pop): ax_function.set_xlabel(r"$x$") -print(f"Final expression {pop.champion.to_sympy()[0]} with fitness {pop.champion.fitness}") +print(f"Final expression {pop.champion.to_sympy()} with fitness {pop.champion.fitness}") history_fitness = np.array(history["fitness_parents"]) ax_fitness.plot(np.max(history_fitness, axis=1), label="Champion") diff --git a/examples/example_minimal.py b/examples/example_minimal.py index 70640e50..64f85dec 100644 --- a/examples/example_minimal.py +++ b/examples/example_minimal.py @@ -31,7 +31,7 @@ def f_target(x): - return x[0] ** 2 + 1.0 + return x ** 2 + 1.0 # %% @@ -56,8 +56,8 @@ def objective(individual): # the callable returned from `to_func` accepts and returns # lists; accordingly we need to pack the argument and unpack # the return value - y = f([x])[0] - loss += (f_target([x]) - y) ** 2 + y = f(x) + loss += (f_target(x) - y) ** 2 individual.fitness = -loss / n_function_evaluations @@ -128,8 +128,8 @@ def recording_callback(pop): f = pop.champion.to_func() x = np.linspace(-5.0, 5.0, 20) -y = [f([x_i]) for x_i in x] -y_target = [f_target([x_i]) for x_i in x] +y = [f(x_i) for x_i in x] +y_target = [f_target(x_i) for x_i in x] ax_function.plot(x, y_target, lw=2, alpha=0.5, label="Target") ax_function.plot(x, y, "x", label="Champion") diff --git a/examples/example_mountain_car.py b/examples/example_mountain_car.py index c9caab51..d3813fae 100644 --- a/examples/example_mountain_car.py +++ b/examples/example_mountain_car.py @@ -83,8 +83,8 @@ def inner_objective(f, seed, n_runs_per_individual, n_total_steps, *, render): if render: env.render() - continuous_action = f(observation) - observation, reward, done, _ = env.step(continuous_action) + continuous_action = f(*observation) + observation, reward, done, _ = env.step([continuous_action]) cum_reward_this_episode += reward if done: @@ -231,8 +231,8 @@ def evaluate_champion(ind): cum_reward_this_episode = 0 while len(cum_reward_all_episodes) < 100: - continuous_action = f(observation) - observation, reward, done, _ = env.step(continuous_action) + continuous_action = f(*observation) + observation, reward, done, _ = env.step([continuous_action]) cum_reward_this_episode += reward if done: @@ -299,7 +299,7 @@ def f(x): print("evolution ended") max_fitness = history["fitness_champion"][-1] - best_expr = history["expr_champion"][-1][0] + best_expr = history["expr_champion"][-1] best_expr_str = str(best_expr).replace("x_0", "x").replace("x_1", "dx/dt") print(f'solution with highest fitness: "{best_expr_str}" (fitness: {max_fitness:.05f})') diff --git a/examples/example_multi_genome.py b/examples/example_multi_genome.py index ce077ae1..a0295850 100644 --- a/examples/example_multi_genome.py +++ b/examples/example_multi_genome.py @@ -56,9 +56,9 @@ def objective(individual): # Note that f is now a list of functions because individual is an instance # of `InvidividualMultiGenome` f = individual.to_numpy() - x = np.random.uniform(-4, 4, (n_function_evaluations, 1)) - y = np.piecewise(x, [x[:, 0] < 0, x[:, 0] >= 0], f)[:, 0] - loss = np.sum((f_target(x[:, 0]) - y) ** 2) + x = np.random.uniform(-4, 4, n_function_evaluations) + y = np.piecewise(x, [x < 0, x >= 0], f) + loss = np.sum((f_target(x) - y) ** 2) individual.fitness = -loss / n_function_evaluations return individual @@ -131,8 +131,8 @@ def recording_callback(pop): f = pop.champion.to_numpy() x = np.linspace(-5.0, 5.0, 20)[:, np.newaxis] -y = np.piecewise(x, [x[:, 0] < 0, x[:, 0] >= 0], f)[:, 0] -y_target = f_target(x[:, 0]) +y = np.piecewise(x, [x < 0, x >= 0], f) +y_target = f_target(x) ax_function.plot(x, y_target, lw=2, alpha=0.5, label="Target") ax_function.plot(x, y, "x", label="Champion") diff --git a/examples/example_parametrized_nodes.py b/examples/example_parametrized_nodes.py index e5135f9c..ccbb489c 100644 --- a/examples/example_parametrized_nodes.py +++ b/examples/example_parametrized_nodes.py @@ -158,7 +158,7 @@ def recording_callback(pop): # After the evolutionary search has ended, we print the expression # with the highest fitness and plot the progression of the search. -print(f"Final expression {pop.champion.to_sympy()[0]} with fitness {pop.champion.fitness}") +print(f"Final expression {pop.champion.to_sympy()} with fitness {pop.champion.fitness}") print("Best performing expression per generation (for fitness increase > 0.5):") old_fitness = -np.inf diff --git a/examples/example_piecewise_target_function.py b/examples/example_piecewise_target_function.py index 2aee6360..d16980f0 100644 --- a/examples/example_piecewise_target_function.py +++ b/examples/example_piecewise_target_function.py @@ -14,7 +14,7 @@ Options: -h --help - --max-generations= Maximum number of generations [default: 5000] + --max-generations= Maximum number of generations [default: 2000] """ import functools @@ -61,7 +61,7 @@ def objective(individual, rng): n_function_evaluations = 1000 f = individual.to_numpy() - x = rng.uniform(-5, 5, size=(n_function_evaluations, 1)) + x = rng.uniform(-5, 5, size=n_function_evaluations) y = f(x) loss = np.mean((f_target(x) - y) ** 2) @@ -125,7 +125,7 @@ def recording_callback(pop): # %% # After the evolutionary search has ended, we print the expression # with the highest fitness and plot the search progression and target and evolved functions. -print(f"Final expression {pop.champion.to_sympy()[0]} with fitness {pop.champion.fitness}") +print(f"Final expression {pop.champion.to_sympy()} with fitness {pop.champion.fitness}") fig = plt.figure(1) plt.plot(history["fitness_champion"]) @@ -133,11 +133,10 @@ def recording_callback(pop): plt.xlabel("Generation") plt.ylabel("Loss (Fitness)") plt.legend(["Champion loss per generation"]) -plt.title({pop.champion.to_sympy()[0]}) +plt.title({pop.champion.to_sympy()}) fig.savefig("example_piecewise_fitness_history.pdf") x = np.arange(-5, 5, 0.01) -x.reshape(x.size, 1) champion_numpy = pop.champion.to_numpy() fig = plt.figure(2) @@ -148,7 +147,7 @@ def recording_callback(pop): plt.title("Target function") plt.legend(["target"]) plt.subplot(122) -plt.plot(x, champion_numpy(x.reshape(x.size, 1)), "r") +plt.plot(x, champion_numpy(x), "r") plt.xlabel("x") plt.ylabel("y") plt.title("Evolved function") diff --git a/examples/example_reorder.py b/examples/example_reorder.py index 47da116b..1f52eb74 100644 --- a/examples/example_reorder.py +++ b/examples/example_reorder.py @@ -36,7 +36,7 @@ def f_target(x): - return x[0] ** 2 + 1.0 + return x ** 2 + 1.0 # %% @@ -60,8 +60,8 @@ def objective(individual): # the callable returned from `to_func` accepts and returns # lists; accordingly we need to pack the argument and unpack # the return value - y = f([x])[0] - loss += (f_target([x]) - y) ** 2 + y = f(x) + loss += (f_target(x) - y) ** 2 individual.fitness = -loss / n_function_evaluations