{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OPTaaS Cyclical Parameters\n", "\n", "### Note: To run this notebook, you need an API Key. You can get one here.\n", "\n", "A new flag on `FloatParameter` now allows you to specify that the parameter is **cyclical** (aka *circular* or *periodic*). OPTaaS will select values from a period starting from the `minimum` (inclusive) and ending at the `maximum` (exclusive). Values near the minimum and maximum will be considered to be close, as if they were on a circle.\n", "\n", "**Note:** If you use any Cyclical parameters in your task, all your parameters must be Floats, Constants or Groups (other types are not currently supported), and none of them can be `optional`.\n", "\n", "As a simple example, let's optimize `cos(x)` for x in the range `[0, 2π)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Connect to OPTaaS using your API Key" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from mindfoundry.optaas.client.client import OPTaaSClient\n", "\n", "client = OPTaaSClient('https://optaas.mindfoundry.ai', '')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define your task" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from math import cos, pi\n", "\n", "from mindfoundry.optaas.client.parameter import FloatParameter\n", "\n", "def scoring_function(x):\n", " return cos(x)\n", "\n", "x = FloatParameter(\"x\", minimum=0, maximum=2 * pi, cyclical=True)\n", "\n", "task = client.create_task(\n", " title='Cyclical Example',\n", " parameters=[x],\n", " initial_configurations=1\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run your Task" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running task \"Cyclical Example\" for 10 iterations\n", "(no score threshold set)\n", "\n", "Iteration: 0 Score: -1.0\n", "Configuration: {'x': 3.141592653589793}\n", "\n", "Iteration: 1 Score: -0.5181244988793857\n", "Configuration: {'x': 2.115453031608477}\n", "\n", "Iteration: 2 Score: -0.41413695380966903\n", "Configuration: {'x': 1.997790746187629}\n", "\n", "Iteration: 3 Score: 0.4477296432117673\n", "Configuration: {'x': 1.1065716756360366}\n", "\n", "Iteration: 4 Score: 0.9964383191318358\n", "Configuration: {'x': 6.198760226305232}\n", "\n", "Iteration: 5 Score: 0.5181244988794008\n", "Configuration: {'x': 5.257045685198288}\n", "\n", "Iteration: 6 Score: 0.9962804147395031\n", "Configuration: {'x': 0.08627738331880817}\n", "\n", "Iteration: 7 Score: 0.9971137826607362\n", "Configuration: {'x': 0.07599482592806261}\n", "\n", "Iteration: 8 Score: 0.4141369532186448\n", "Configuration: {'x': 5.139383399128098}\n", "\n", "Iteration: 9 Score: -0.4491755260657931\n", "Configuration: {'x': 4.246546660377198}\n", "\n", "Task Completed\n", "\n" ] }, { "data": { "text/plain": [ "{ 'configuration': {'type': 'exploitation', 'values': {'x': 0.07599482592806261}},\n", " 'score': 0.9971137826607362,\n", " 'user_defined_data': None}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "task.run(scoring_function, max_iterations=10)" ] } ], "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.6.4" }, "nav_menu": {}, "toc": { "navigate_menu": true, "number_sections": false, "sideBar": true, "threshold": 6, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }