diff --git a/examples/SMCExample.py b/examples/SMCExample.py index cc0a5c26..47bf1178 100644 --- a/examples/SMCExample.py +++ b/examples/SMCExample.py @@ -49,7 +49,10 @@ def get_training_data(equ, minx, maxx, numx, noise_ratio): def make_fitness_function( - training_data, mcmc_steps, num_particles, num_multistarts, + training_data, + mcmc_steps, + num_particles, + num_multistarts, ): fitness = ExplicitRegression(training_data=training_data, metric="mse") @@ -81,7 +84,9 @@ def make_island( for comp in operators: component_generator.add_operator(comp) generator = AGraphGenerator( - stack_size, component_generator, use_simplification=True, + stack_size, + component_generator, + use_simplification=True, ) # variation @@ -111,9 +116,7 @@ def make_island( x.command_array, y.command_array ), ) - island = Island( - ea, generator, population_size=population_size, hall_of_fame=hof - ) + island = Island(ea, generator, population_size=population_size, hall_of_fame=hof) return island @@ -134,11 +137,11 @@ def make_island( NUM_MULTISTARTS = 8 # ISLAND PARAMS - POPULATION_SIZE = 100 + POPULATION_SIZE = 20 # AGRAPH PARAMS OPERATORS = ["+", "-", "*"] - STACK_SIZE = 64 + STACK_SIZE = 16 USE_SIMPLIFICATION = True # VARIATION PARAMS @@ -146,12 +149,15 @@ def make_island( MUTATION_PROB = 0.4 # EVOLUTION PARAMS - NUM_GENERATIONS = 10 + NUM_GENERATIONS = 1 np.random.seed(0) TRAINING_DATA = get_training_data(TRU_EQU, MINX, MAXX, NUMX, NOISE_RATIO) NML = make_fitness_function( - TRAINING_DATA, NUM_MCMC_STEPS, NUM_PARTICLES, NUM_MULTISTARTS, + TRAINING_DATA, + NUM_MCMC_STEPS, + NUM_PARTICLES, + NUM_MULTISTARTS, ) ISLAND = make_island( NML, @@ -162,7 +168,7 @@ def make_island( MUTATION_PROB, ) - for _ in range(2): + for _ in range(NUM_GENERATIONS): ISLAND.evolve(1) print("HOF") diff --git a/examples/Tutorial_1_One_Max.ipynb b/examples/Tutorial_1_One_Max.ipynb index 85ea2622..588c7244 100644 --- a/examples/Tutorial_1_One_Max.ipynb +++ b/examples/Tutorial_1_One_Max.ipynb @@ -292,7 +292,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/examples/Tutorial_2_Zero_Min.ipynb b/examples/Tutorial_2_Zero_Min.ipynb index 4f2ce44e..90dc2e9f 100644 --- a/examples/Tutorial_2_Zero_Min.ipynb +++ b/examples/Tutorial_2_Zero_Min.ipynb @@ -19,18 +19,9 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "[0.0, 1.0, 2.0, 3.0]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from bingo.chromosomes.multiple_floats import MultipleFloatChromosome\n", "chromosome = MultipleFloatChromosome([0., 1., 2., 3.])\n", @@ -69,18 +60,9 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.5488135039273248, 0.7151893663724195, 0.6027633760716439, 0.5448831829968969, 0.4236547993389047, 0.6458941130666561, 0.4375872112626925, 0.8917730007820798]\n", - "3\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Example of Generator\n", "chromosome = generator()\n", @@ -111,19 +93,9 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Mutation\n", - "before: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]\n", - "after: [0.2726562945801132, 0.0, 0.0, 0.0, 0.0, 0.0]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Example of Mutation\n", "before_mutation = MultipleFloatChromosome([0., 0., 0., 0., 0., 0.])\n", @@ -135,21 +107,9 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Crossover\n", - "parent 1: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]\n", - "parent 1: [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]\n", - "child 1: [0.0, 1.0, 1.0, 1.0, 1.0, 1.0]\n", - "child 1: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Example of Crossover\n", "parent_1 = MultipleFloatChromosome([0., 0., 0., 0., 0., 0.])\n", @@ -194,20 +154,9 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.449489742783178\n", - "[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]\n", - "2.0000000000000018\n", - "[8.510915796020789e-08, 1.0, 1.0, 3.5189156817659693e-09, 1.0, 1.0]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Example of fitness\n", "chromosome = MultipleFloatChromosome([1., 1., 1., 1., 1., 1.], \n", @@ -324,28 +273,9 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Island age: 0 with best fitness: 0.9071522257521843 \n", - "\n", - "indv 0 [0.5680445610939323, -3.439277129082302e-08, 0.07103605819788694, 2.2901250614652325e-08, -1.2672932417253898e-08, 0.832619845547938, 0.7781567509498505, 0.8700121482468192]\n", - "indv 1 [0.978618342232764, -9.166314277214794e-07, 0.46147936225293185, 2.3328352770924484e-07, 9.402448224646705e-08, 0.6399210213275238, 0.1433532874090464, 0.9446689170495839]\n", - "indv 2 [0.5218483217500717, 2.9783307631507725e-07, 0.26455561210462697, -5.105384643184367e-07, 8.673128822368415e-07, 0.5684339488686485, 0.018789800436355142, 0.6176354970758771]\n", - "indv 3 [0.6120957227224214, -7.930571874083672e-07, 0.9437480785146242, 1.3110174844462484e-06, 8.367856635100972e-07, 0.43703195379934145, 0.6976311959272649, 0.06022547162926983]\n", - "indv 4 [0.6667667154456677, -1.730686306694669e-07, 0.2103825610738409, 7.535056273877088e-08, 2.503398893827159e-07, 0.3637107709426226, 0.5701967704178796, 0.43860151346232035]\n", - "indv 5 [0.9883738380592262, 2.4652257687485484e-08, 0.2088767560948347, -1.3655811695386645e-07, 2.7529889539741097e-08, 0.2532916025397821, 0.4663107728563063, 0.24442559200160274]\n", - "indv 6 [0.15896958364551972, -1.8506301743184803e-08, 0.6563295894652734, -1.8713466175035825e-07, -1.2231475760966863e-07, 0.3687251706609641, 0.8209932298479351, 0.09710127579306127]\n", - "indv 7 [0.8379449074988039, 8.533685532283956e-07, 0.9764594650133958, 4.663005770349062e-07, 8.463966268040845e-07, 0.604845519745046, 0.7392635793983017, 0.039187792254320675]\n", - "indv 8 [0.2828069625764096, -1.8823710783817741e-07, 0.29614019752214493, 2.506683720185509e-07, 9.031653776819193e-08, 0.41426299451466997, 0.06414749634878436, 0.6924721193700198]\n", - "indv 9 [0.5666014542065752, -6.203918268318251e-08, 0.5232480534666997, -3.150666625847705e-08, -2.687448350836163e-09, 0.9292961975762141, 0.31856895245132366, 0.6674103799636817]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(\"Island age:\", island.generational_age,\n", " \" with best fitness:\", island.get_best_fitness(), \"\\n\")\n", @@ -365,18 +295,9 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Island age: 0 with best fitness: 0.9071522257521843\n", - "Island age: 10 with best fitness: 0.2322576696603759\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(\"Island age:\", island.generational_age,\n", " \" with best fitness:\", island.get_best_fitness())\n", @@ -396,29 +317,9 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Island age: 30 with best fitness: 0.042675552380132487 \n", - "\n", - "Best indv: [0.0017897903222204858, -2.5782987014685056e-08, 0.0004863056520357789, 1.487743605170051e-08, 1.3603063896249012e-08, 0.038004487981462276, 0.005510440235525937, 0.01852179446061397]\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\David\\Desktop\\GPSR Research\\bingoNASAFork\\bingo\\evolutionary_algorithms\\ea_diagnostics.py:46: RuntimeWarning: invalid value encountered in double_scalars\n", - " self._cross_mut_stats[1] / self._cross_mut_stats[0],\n", - "C:\\Users\\David\\Desktop\\GPSR Research\\bingoNASAFork\\bingo\\evolutionary_algorithms\\ea_diagnostics.py:47: RuntimeWarning: invalid value encountered in double_scalars\n", - " self._cross_mut_stats[2] / self._cross_mut_stats[0])\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "island.evolve_until_convergence(max_generations=1000,\n", " fitness_threshold=0.05)\n", @@ -438,22 +339,9 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "RANK FITNESS\n", - " 0 0.042675552380132487\n", - " 1 0.06866925870312078\n", - " 2 0.06997463293425148\n", - " 3 0.08101448932321696\n", - " 4 0.10213727596681292\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(\"RANK FITNESS\")\n", "for i, member in enumerate(hof):\n", @@ -484,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -506,7 +394,8 @@ "\n", "\n", " def animate(i):\n", - " ax.collections.clear()\n", + " for artist in ax.collections:\n", + " artist.remove()\n", " polygon = ax.fill_between(x, zero, y[i], color='b', alpha=0.3)\n", " points.set_ydata(y[i]) # update the data\n", " points.set_label('Generation :' + str(i))\n", @@ -534,23 +423,20 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": "", - "text/html": "\n\n\n\n\n\n
\n \n
\n \n
\n \n \n \n \n \n \n \n \n \n
\n
\n \n \n \n \n \n \n
\n
\n
\n\n\n\n" - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from IPython.display import HTML\n", "HTML(animate_data(best_indv_values).to_jshtml())" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -569,9 +455,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 2 -} \ No newline at end of file +} diff --git a/examples/Tutorial_3_Archipelagos_and_Logging.ipynb b/examples/Tutorial_3_Archipelagos_and_Logging.ipynb index 2c0feb30..3c08c598 100644 --- a/examples/Tutorial_3_Archipelagos_and_Logging.ipynb +++ b/examples/Tutorial_3_Archipelagos_and_Logging.ipynb @@ -454,7 +454,8 @@ "\n", "\n", " def animate(i):\n", - " ax.collections.clear()\n", + " for artist in ax.collections:\n", + " artist.remove()\n", " polygon = ax.fill_between(x, zero, y[i], color='b', alpha=0.3)\n", " points.set_ydata(y[i]) # update the data\n", " points.set_label('Generation :' + str(i))\n", @@ -513,13 +514,13 @@ "pycharm": { "stem_cell": { "cell_type": "raw", - "source": [], "metadata": { "collapsed": false - } + }, + "source": [] } } }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} diff --git a/examples/Tutorial_4_Symbolic_Regression.ipynb b/examples/Tutorial_4_Symbolic_Regression.ipynb index 8a50389c..a3c7fc26 100644 --- a/examples/Tutorial_4_Symbolic_Regression.ipynb +++ b/examples/Tutorial_4_Symbolic_Regression.ipynb @@ -352,7 +352,8 @@ "\n", "\n", " def animate(i):\n", - " ax.collections.clear()\n", + " for artist in ax.collections:\n", + " artist.remove()\n", " points.set_ydata(y[i].evaluate_equation_at(x)) # update the data\n", " points.set_label('Generation :' + str(g[i]))\n", " legend = ax.legend(loc='upper right')\n", @@ -408,4 +409,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} \ No newline at end of file +}