{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Running PHYSBO interactively\n", "\n", "You can run PHYSBO interactively in the following way:\n", "\n", "1. Get the next parameter to run from PHYSBO\n", "2. Get the evaluation values outside of PHYSBO\n", "3. Register the evaluation values into PHYSBO\n", "\n", "For example, it is suitable for the following cases.\n", "\n", "- You want to perform an experiment manually and give the evaluation values to PHYSBO.\n", "- You want to control the execution flexibly, such as running the simulator in a separate process.\n", "\n", "## Preparation of search candidate data\n", "\n", "As the previous tutorials, save the dataset file [s5-210.csv](https://raw.githubusercontent.com/issp-center-dev/PHYSBO/master/examples/grain_bound/data/s5-210.csv) into the subdirectory `data`, and load dataset from this file as the following:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:17:28.657314Z", "start_time": "2020-12-04T06:17:27.967614Z" } }, "outputs": [], "source": [ "import physbo\n", "\n", "import numpy as np\n", "\n", "\n", "def load_data():\n", " A = np.asarray(np.loadtxt('data/s5-210.csv',skiprows=1, delimiter=',') )\n", " X = A[:,0:3]\n", " t = -A[:,3]\n", " return X, t\n", "\n", "X, t = load_data()\n", "X = physbo.misc.centering(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Definition of simulator" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:17:28.663215Z", "start_time": "2020-12-04T06:17:28.658656Z" } }, "outputs": [], "source": [ "class simulator:\n", " def __init__( self ):\n", " _, self.t = load_data()\n", " \n", " def __call__( self, action ):\n", " return self.t[action]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Executing optimization" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:17:28.674407Z", "start_time": "2020-12-04T06:17:28.669875Z" } }, "outputs": [], "source": [ "# Set policy\n", "policy = physbo.search.discrete.policy(test_X=X)\n", "\n", "# Set seed\n", "policy.set_seed( 0 )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In each search step, the following processes are performed.\n", "\n", "1. Running random_search or bayes_search with `max_num_probes=1, simulator=None` to get action IDs (parameters). \n", "2. Getting the evaluation value (array of actions) by `t = simulator(actions)`. \n", "3. Registering the evaluation value for the action ID (parameter) with `policy.write(actions, t)`. \n", "4. Showing the history with `physbo.search.utility.show_search_results`.\n", "\n", "In the following, we will perform two random sampling (1st, and 2nd steps) and two Bayesian optimization proposals (3rd, and 4th steps)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:17:47.726433Z", "start_time": "2020-12-04T06:17:28.677429Z" }, "scrolled": false }, "outputs": [], "source": [ "simulator = simulator()\n", "\n", "''' 1st step (random sampling) '''\n", "actions = policy.random_search(max_num_probes=1, simulator=None)\n", "t = simulator(actions)\n", "policy.write(actions, t)\n", "physbo.search.utility.show_search_results(policy.history, 10)\n", "\n", "''' 2nd step (random sampling) '''\n", "actions = policy.random_search(max_num_probes=1, simulator=None)\n", "t = simulator(actions)\n", "policy.write(actions, t)\n", "physbo.search.utility.show_search_results(policy.history, 10)\n", "\n", "''' 3rd step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t) \n", "physbo.search.utility.show_search_results(policy.history, 10) \n", "\n", "''' 4-th step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t)\n", "physbo.search.utility.show_search_results(policy.history, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Suspend and restart\n", "\n", "You can suspend and restart the optimization process by saving the following predictor, training, and history to an external file.\n", "\n", "- predictor: Prediction model of the objective function\n", "- training: Data used to train the predictor (`physbo.variable` object)\n", "- history: History of optimization runs (`physbo.search.discrete.results.history` object)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:17:48.722691Z", "start_time": "2020-12-04T06:17:47.728006Z" } }, "outputs": [], "source": [ "policy.save(file_history='history.npz', file_training='training.npz', file_predictor='predictor.dump')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-04T06:18:05.746742Z", "start_time": "2020-12-04T06:17:48.724101Z" }, "scrolled": true }, "outputs": [], "source": [ "# delete policy \n", "del policy\n", "\n", "# load policy\n", "policy = physbo.search.discrete.policy(test_X=X)\n", "policy.load(file_history='history.npz', file_training='training.npz', file_predictor='predictor.dump')\n", "\n", "''' 5-th step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t) \n", "physbo.search.utility.show_search_results(policy.history, 10) \n", "\n", "# It is also possible to specify predictor and training separately.\n", "''' 6-th step (bayesian optimization) '''\n", "actions = policy.bayes_search(max_num_probes=1, \n", " predictor=policy.predictor, training=policy.training,\n", " simulator=None, score='EI', interval=0, num_rand_basis = 5000)\n", "t = simulator(actions) \n", "policy.write(actions, t) \n", "physbo.search.utility.show_search_results(policy.history, 10) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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": 1 }