diff --git a/plot_rastrigin_heatmaps.ipynb b/plot_rastrigin_heatmaps.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..88fc61ed837547f4816b48d1b7724652b2dfddb4 --- /dev/null +++ b/plot_rastrigin_heatmaps.ipynb @@ -0,0 +1,372 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0d7b66e2-e07e-41b6-a9da-b9d127d283b6", + "metadata": {}, + "source": [ + "# Plot Rastrigin Heatmaps SO PGPE\n", + "\n", + "In diesem Notebook wird der 2D Rastrigin als Image dargestellt.\n", + "Von dne einzelnen PGPGE Versionen wird dann der Verlauf der Baseline eingezeichnet. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "83250d4e-c586-4aba-8598-72dd369e2aae", + "metadata": {}, + "outputs": [], + "source": [ + "from matplotlib import cm\n", + "import matplotlib\n", + "import math\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "from benchmarks.functions import rastrigin\n", + "from black_box_optimizers.pgpe_spielplatz import SyS_PGPE, PGPE, RandomSearch, Learner, SyS_PGPE_MS\n", + "from black_box_optimizers.hillclimber import HillClimber, SmartHillClimber\n", + "from optimization.optimizer import OptimizeableParameterDict as OP\n", + "from scipy import stats\n", + "import pickle as cp" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "fb48615a-3d98-4332-8fdc-dd7e0cf7d8b2", + "metadata": {}, + "outputs": [], + "source": [ + "# parameters\n", + "\n", + "# shared parameters\n", + "dim = 2\n", + "opt_iter = 100\n", + "trainings = 20\n", + "prob = 'rastrigin'\n", + "\n", + "# standard pgpe parameters\n", + "mue_alpha_s = 0.1 \n", + "sigma_alpha_s = 0.05\n", + "\n", + "# norm pgpe parameters\n", + "mue_alpha_n = mue_alpha_s \n", + "sigma_alpha_n = sigma_alpha_s \n", + "\n", + "# new pgpge parameters\n", + "mue_alpha_ms = mue_alpha_s\n", + "sigma_alpha_ms = sigma_alpha_s\n", + "\n", + "\n", + "# combined version parameters\n", + "mue_alpha_c = 0.1\n", + "sigma_alpha_c = 0.05\n" + ] + }, + { + "cell_type": "markdown", + "id": "ed5441d0-806e-4a44-aa56-1fa18b226565", + "metadata": {}, + "source": [ + "## Hier werden die 2D PGPGE Versionen trainiert" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "636195be-c02a-4db3-8b6b-521a41386586", + "metadata": {}, + "outputs": [], + "source": [ + "# Creating the Optimizable Parameter Dict\n", + "def get_para_dict(dimension):\n", + " para_dict = OP()\n", + " for i in range(dimension):\n", + " para_dict.add_parameter(\n", + " key=f\"var_{i}\",\n", + " min=-10.0,\n", + " max=10.0,\n", + " type=\"f\", # f=float, i=int, b=1-float\n", + " scale=\"linear\", # linear, exp=exponential, ixp=inverse exponential\n", + " init=8.0, # if not provided uses center of search intervall\n", + " id=i, # if not provided uses internal counter\n", + " )\n", + " return para_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "09ee3682-d5e9-4f03-8d82-18411c97d4bd", + "metadata": {}, + "outputs": [], + "source": [ + "def get_algo(algo_kind, para_dict, mue_alpha, sigma_alpha):\n", + " seed = np.array([para_dict.get_init_parameters_array()])\n", + " para_dict = para_dict.parameter_dict\n", + "\n", + " if algo_kind == \"sys_pgpe\" or algo_kind == 'sys_pgpe_norm':\n", + " algorithm = SyS_PGPE(2,\n", + " paras=para_dict,\n", + " plot_paras = True,\n", + " seed=seed,\n", + " mue_alpha = mue_alpha, \n", + " sigma_alpha = sigma_alpha\n", + " )\n", + " elif algo_kind == \"sys_pgpe_ms\" or algo_kind == 'sys_pgpe_ms_norm':\n", + " algorithm = SyS_PGPE_MS(2,\n", + " paras=para_dict,\n", + " plot_paras = True,\n", + " seed=seed,\n", + " mue_alpha = mue_alpha, \n", + " sigma_alpha = sigma_alpha\n", + " )\n", + " return algorithm" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "84cc5da7-072c-432a-b984-503502d841f0", + "metadata": {}, + "outputs": [], + "source": [ + "def evaluate_problem(pop, problem):\n", + " if problem == 'rastrigin':\n", + " return - rastrigin(pop).reshape(-1,)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "7ef7c619-4b3d-4cf0-a46a-c7f8fc1ab9b8", + "metadata": {}, + "outputs": [], + "source": [ + "def create_sample(optimizer, dim, trainings, opt_iter, mue_alpha, sigma_alpha, problem='rastrigin'): \n", + " #print(\"dim: \", dim)\n", + " samples = []\n", + " for n in range(trainings):\n", + " sample = [] # samples of specific dimension\n", + " para_dict = get_para_dict(dim) #initialize the parameters for the current problem dimension\n", + " # get the algorithm object\n", + " algorithm = get_algo(algo_kind=optimizer, \n", + " para_dict=para_dict,\n", + " mue_alpha = mue_alpha,\n", + " sigma_alpha = sigma_alpha\n", + " )\n", + " for i in range(opt_iter):\n", + " sample.append(algorithm.mue[0].tolist())\n", + " population = algorithm.ask() # gibt mir das Sample\n", + " rewards = evaluate_problem(pop=population, \n", + " problem=problem\n", + " )\n", + " if \"norm\" in optimizer:\n", + " # tell with reward normalization \n", + " algorithm.tell_norm(pop=population, \n", + " rewards=rewards, \n", + " cfg=None,\n", + " logger=None\n", + " )\n", + " else: \n", + " algorithm.tell(pop=population, \n", + " rewards=rewards, \n", + " cfg=None,\n", + " logger=None\n", + " )\n", + " samples.append(sample)\n", + " return np.array(samples)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "36a89ca6-44cc-467e-bc86-ae726eb4d0fe", + "metadata": {}, + "outputs": [], + "source": [ + "original_samples = create_sample(\"sys_pgpe\", dim, trainings, opt_iter, mue_alpha_s, sigma_alpha_s)\n", + "#print(original_sample)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "e642cbd6-1149-41d2-a0a6-200611cec815", + "metadata": {}, + "outputs": [], + "source": [ + "norm_samples = create_sample(\"sys_pgpe_norm\", dim, trainings, opt_iter, mue_alpha_n, sigma_alpha_n)\n", + "#print(norm_sample)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "552bd5e7-834d-45a2-ad7f-c02b01734f16", + "metadata": {}, + "outputs": [], + "source": [ + "ms_samples = create_sample(\"sys_pgpe_ms\", dim, trainings, opt_iter, mue_alpha_ms, sigma_alpha_ms)\n", + "#print(ms_sample)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "34bd905a-d78e-4939-ab39-9caa9a3673c4", + "metadata": {}, + "outputs": [], + "source": [ + "comb_samples = create_sample(\"sys_pgpe_ms_norm\", dim, trainings, opt_iter, mue_alpha_c, sigma_alpha_c)\n", + "#print(comb_sample)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "1256975f-811d-4c34-a1de-664b5aa949c0", + "metadata": {}, + "outputs": [], + "source": [ + "all_samples = []" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "4c5a738b-d4d9-4f2b-8590-370ce81347af", + "metadata": {}, + "outputs": [], + "source": [ + "all_samples.extend(original_samples)\n", + "all_samples.extend(norm_samples)\n", + "all_samples.extend(ms_samples)" + ] + }, + { + "cell_type": "markdown", + "id": "ff8d24ec-ad02-4efd-8e4a-29862ba50653", + "metadata": {}, + "source": [ + "## Hier werden die PGPE Versionen in den Rastrigin geplottet" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "4164da95-38e2-427e-8101-7b3f216e3050", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/tmp/ipykernel_384943/1197428567.py:39: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n", + " plt.show()\n" + ] + } + ], + "source": [ + "import datetime\n", + "# Generate data for each heatmap\n", + "\n", + "X = np.linspace(-5.12, 5.12, 100) \n", + "Y = np.linspace(-5.12, 5.12, 100) \n", + "X, Y = np.meshgrid(X, Y) \n", + "Z = (X**2 - 10 * np.cos(2 * np.pi * X)) + (Y**2 - 10 * np.cos(2 * np.pi * Y)) + 20\n", + "\n", + "# Create a grid of subplots with 3 rows and 5 columns\n", + "fig, axs = plt.subplots(nrows=3, ncols=5, figsize=(12, 8), layout=\"constrained\")\n", + "\n", + "# Set titles for each row\n", + "#axs[0, 0].set_title(\"Standard SyS PGPE\", fontsize=14)\n", + "#axs[1, 0].set_title(\"Normalized SyS PGPE\", fontsize=14)\n", + "#axs[2, 0].set_title(\"MS PGPE\", fontsize=14)\n", + "# Set titles for each row of subplots\n", + "\n", + "\n", + "# Plot each heatmap in a separate subplot\n", + "for i, ax in enumerate(axs.flat):\n", + " print(i)\n", + " im = ax.imshow(Z, cmap='plasma', extent=[-10.0, 10.0, -10.0, 10.0])\n", + " samp = all_samples[i]\n", + " x_points, y_points = zip(*samp)\n", + " ax.plot(x_points, y_points, '.', color='white')\n", + "\n", + "\n", + "# Add colorbar to the last subplot\n", + "fig.colorbar(im, ax=axs, shrink=0.6)\n", + "\n", + "# Set the plot title\n", + "plt.suptitle('Rastrigin Function Heatmaps, 100 iterations, 2D', fontsize=14)\n", + "\n", + "# save the plots\n", + "timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')\n", + "plt.savefig(f'./sopgpe_plots/rastrigin_heatmaps/{prob}_{opt_iter}_iterations_{dim}D_{timestamp}.png')\n", + "\n", + "# Display the plot\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7d987ea5-dcc5-4fb5-adab-29f7815cb29c", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c1e33757-bd90-47cc-a774-77c310fbd4a6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}