{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# simulatorクラスの定義\n", "\n", "\n", "PHYSBOではSimulator関数を使用して目的関数を定義します。\n", "ここでは、一次元の関数の最小値を求める例題をもとに、simulatorクラスの定義方法について紹介します。\n", "\n", "## 探索空間の定義\n", "\n", "その他のチュートリアルと同じように、最初に関数を探索する空間を定義します。\n", "以下の例では、探索空間``X``を ``x_min = -2.0``から``x_max = 2.0``まで``window_num=10001``分割で刻んだグリッドで定義しています。\n", "なお、``X``は ``window_num`` x ``d`` のndarray形式にする必要があります(``d``は次元数、この場合は2次元)。そのため、reshapeを行って変形しています。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:21:37.552947Z", "start_time": "2020-12-04T06:21:37.242727Z" } }, "outputs": [], "source": [ "#In\n", "import numpy as np\n", "import scipy\n", "import physbo\n", "import itertools\n", "\n", "#In\n", "#Create candidate\n", "window_num=10001\n", "x_max = 2.0\n", "x_min = -2.0\n", "\n", "X = np.linspace(x_min,x_max,window_num).reshape(window_num, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## simulatorクラスの定義\n", "\n", "目的関数を定義するためのsimulatorクラスをここで定義します。\n", "\n", "今回はf(x) = 3 x^4 + 4 x ^3 + 1.0 が最小となるxを探索するという問題設定にしています(答えはx=-1.0)。\n", "\n", "simulatorクラスでは、``__call__``関数を定義します(初期変数などがある場合は``__init__``を定義します)。\n", "actionは探索空間の中から取り出すグリッドのindex番号を示しており、複数の候補を一度に計算できるように一般的にndarrayの形式を取っています。\n", "今回は一つの候補のみを毎回計算するため、``action_idx=action[0]``として``X``から候補点を一つ選んでいます。\n", "PHYSBOでは目的関数値が最大となるものを求める仕様になっているため、候補点でのf(x)の値に-1をかけたものを返しています。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:21:37.558852Z", "start_time": "2020-12-04T06:21:37.554788Z" } }, "outputs": [], "source": [ "# Declare the class for calling the simulator.\n", "class simulator:\n", "\n", " def __call__(self, action ):\n", " action_idx = action[0]\n", " x = X[action_idx][0]\n", " fx = 3.0*x**4 + 4.0*x**3 + 1.0\n", " fx_list.append(fx)\n", " x_list.append(X[action_idx][0])\n", "\n", " print (\"*********************\")\n", " print (\"Present optimum interactions\")\n", "\n", " print (\"x_opt=\", x_list[np.argmin(np.array(fx_list))])\n", "\n", " return -fx, x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ランダムサーチ\n", "\n", "ベイズ最適化を行う前に、探索空間からランダムに候補点を選び目的関数値を評価することで、事前にトレーニングデータを用意します(飛ばすことも可能です)。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:21:37.717038Z", "start_time": "2020-12-04T06:21:37.563144Z" } }, "outputs": [], "source": [ "fx_list=[]\n", "x_list = []\n", "#In\n", "# Design of policy\n", "# Declaring the policy by\n", "policy = physbo.search.discrete.policy(test_X=X)\n", "# test_X is the set of candidates which is represented by numpy.array.\n", "# Each row vector represents the feature vector of the corresponding candidate\n", "\n", "# set the seed parameter\n", "policy.set_seed( 1 )\n", "\n", "\n", "# If you want to perform the initial random search before starting the Bayesian optimization,\n", "# the random sampling is performed by\n", "\n", "res = policy.random_search(max_num_probes=50, simulator=simulator())\n", "# Input:\n", "# max_num_probes: number of random search\n", "# simulator = simulator\n", "# output: combo.search.discreate.results (class)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ベイズ最適化\n", "\n", "定義したsimulatorに対してベイズ最適化を実施します。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:24:30.554566Z", "start_time": "2020-12-04T06:21:37.718406Z" } }, "outputs": [], "source": [ "# single query Bayesian search\n", "# The single query version of COMBO is performed by\n", "res = policy.bayes_search(max_num_probes= 150, simulator=simulator(), score='TS',\n", " interval=20, num_rand_basis=5000)\n", "\n", "# Input\n", "# max_num_probes: number of searching by Bayesian optimization\n", "# simulator: the class of simulator which is defined above\n", "# score: the type of aquision funciton. TS, EI and PI are available\n", "# interval: the timing for learning the hyper parameter.\n", "# In this case, the hyper parameter is learned at each 20 steps\n", "# If you set the negative value to interval, the hyper parameter learning is not performed\n", "# If you set zero to interval, the hyper parameter learning is performed only at the first step\n", "# num_rand_basis: the number of basis function. If you choose 0, ordinary Gaussian process runs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 結果表示\n", "\n", "得られた結果のうち、一番よいスコアを持つものは``export_all_sequence_best_fx()``を用いることで取り出すことができます。\n", "また、これまでの全ての履歴をみたい場合には、``chosen_actions``を呼び出すことで表示することができます。以下、結果を表示するサンプルスクリプトを記載します。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:24:30.932451Z", "start_time": "2020-12-04T06:24:30.555830Z" } }, "outputs": [], "source": [ "#In\n", "best_fx, best_action = res.export_all_sequence_best_fx()\n", "\n", "import matplotlib.pyplot as plt\n", "# The result of searching is summarized in the class combo.search.discrete.results.history()\n", "# res.fx: observed negative energy at each step\n", "# res.chosen_actions: history of choosed actions\n", "# fbest, best_action= res.export_all_sequence_best_fx(): current best fx and current best action\n", "# that has been observed until each step\n", "# res.total_num_search: total number of search\n", "plt.plot(res.fx[0:res.total_num_search])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:24:31.066401Z", "start_time": "2020-12-04T06:24:30.934178Z" } }, "outputs": [], "source": [ "plt.plot(best_fx)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:24:31.200565Z", "start_time": "2020-12-04T06:24:31.068515Z" } }, "outputs": [], "source": [ "plt.plot(best_action)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "最後に、一番よいスコアを持つ候補は以下のようにして表示することができます。正しい解 x=-1に行き着いていることがわかります。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:24:31.204953Z", "start_time": "2020-12-04T06:24:31.201964Z" } }, "outputs": [], "source": [ "print(X[int(best_action[-1])])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }