{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\nDistributed Optimization\n========================\n\nThere is no complicated setup but just sharing the same study name among nodes/processes.\n\nFirst, create a shared study using ``optuna create-study`` command (or using :func:`optuna.create_study` in a Python script).\n\n.. code-block:: bash\n\n    $ optuna create-study --study-name \"distributed-example\" --storage \"sqlite:///example.db\"\n    [I 2020-07-21 13:43:39,642] A new study created with name: distributed-example\n\n\nThen, write an optimization script. Let's assume that ``foo.py`` contains the following code.\n\n.. code-block:: python\n\n    import optuna\n\n    def objective(trial):\n        x = trial.suggest_uniform('x', -10, 10)\n        return (x - 2) ** 2\n\n    if __name__ == '__main__':\n        study = optuna.load_study(study_name='distributed-example', storage='sqlite:///example.db')\n        study.optimize(objective, n_trials=100)\n\nFinally, run the shared study from multiple processes.\nFor example, run ``Process 1`` in a terminal, and do ``Process 2`` in another one.\nThey get parameter suggestions based on shared trials' history.\n\nProcess 1:\n\n.. code-block:: bash\n\n    $ python foo.py\n    [I 2020-07-21 13:45:02,973] Trial 0 finished with value: 45.35553104173011 and parameters: {'x': 8.73465151598285}. Best is trial 0 with value: 45.35553104173011.\n    [I 2020-07-21 13:45:04,013] Trial 2 finished with value: 4.6002397305938905 and parameters: {'x': 4.144816945707463}. Best is trial 1 with value: 0.028194513284051464.\n    ...\n\nProcess 2 (the same command as process 1):\n\n.. code-block:: bash\n\n    $ python foo.py\n    [I 2020-07-21 13:45:03,748] Trial 1 finished with value: 0.028194513284051464 and parameters: {'x': 1.8320877810162361}. Best is trial 1 with value: 0.028194513284051464.\n    [I 2020-07-21 13:45:05,783] Trial 3 finished with value: 24.45966755098074 and parameters: {'x': 6.945671597566982}. Best is trial 1 with value: 0.028194513284051464.\n    ...\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>We do not recommend SQLite for large scale distributed optimizations because it may cause serious performance issues. Please consider to use another database engine like PostgreSQL or MySQL.</p></div>\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>Please avoid putting the SQLite database on NFS when running distributed optimizations. See also: https://www.sqlite.org/faq.html#q5</p></div>\n"
      ]
    }
  ],
  "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.7.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}