{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OPTaaS Quick Start Tutorial\n", "\n", "### Note: To run this notebook, you need an API Key. You can get one here.\n", "\n", "More tutorials are [available here](./)" ] }, { "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 parameters" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": false }, "outputs": [], "source": [ "from mindfoundry.optaas.client.parameter import IntParameter, FloatParameter, CategoricalParameter, BoolParameter, \\\n", " ChoiceParameter, GroupParameter\n", "\n", "bool_param = BoolParameter('my_bool')\n", "cat_param = CategoricalParameter('my_cat', values=['a', 'b', 'c'], default='c')\n", "\n", "int_param = IntParameter('my_int', minimum=0, maximum=20)\n", "optional_int_param = IntParameter('my_optional_int', minimum=-10, maximum=10, optional=True)\n", "\n", "parameters = [\n", " bool_param,\n", " cat_param,\n", " ChoiceParameter('ints_or_floats', choices=[\n", " GroupParameter('ints', items=[int_param, optional_int_param]),\n", " GroupParameter('floats', items=[\n", " FloatParameter('float1', minimum=0, maximum=1),\n", " FloatParameter('float2', minimum=0.5, maximum=4.5)\n", " ])\n", " ]),\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define your scoring function\n", "\n", "The argument names in your scoring function must match the parameter names you defined above.\n", "\n", "Your function can return just a score, or a tuple of (score, variance)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def scoring_function(my_bool, my_cat, ints_or_floats):\n", " score = 5 if my_bool is True else -5\n", " score += 1 if my_cat == 'a' else 3\n", " if 'ints' in ints_or_floats:\n", " score += sum(ints_or_floats['ints'].values())\n", " else:\n", " score *= sum(ints_or_floats['floats'].values())\n", " return score" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create your Task\n", "\n", "You can use Goal.max or Goal.min as appropriate. You can also specify the minimum and maximum score values (if known)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from mindfoundry.optaas.client.client import Goal\n", "\n", "task = client.create_task(\n", " title='Quick Start Example Task',\n", " parameters=parameters,\n", " goal=Goal.max,\n", " min_known_score=-22,\n", " max_known_score=44\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run your Task\n", "\n", "We will run for a maximum of 50 iterations, but we will stop as soon as we reach our score threshold of 32.\n", "\n", "The score threshold is optional - you can omit it and simply run as many iterations as you need." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running task \"Quick Start Example Task\" for 50 iterations\n", "(or until score is 32 or better)\n", "\n", "Iteration: 0 Score: 8\n", "Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 10, 'my_optional_int': 0}}}\n", "\n", "Iteration: 1 Score: -6.0\n", "Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.5, 'float2': 2.5}}}\n", "\n", "Iteration: 2 Score: 8\n", "Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 10}}}\n", "\n", "Iteration: 3 Score: 16\n", "Configuration: {'my_bool': True, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 4, 'my_optional_int': 4}}}\n", "\n", "Iteration: 4 Score: -5.791952740746135\n", "Configuration: {'my_bool': False, 'my_cat': 'a', 'ints_or_floats': {'floats': {'float1': 0.10035829146613562, 'float2': 1.347629893720398}}}\n", "\n", "Iteration: 5 Score: 17.43489269882466\n", "Configuration: {'my_bool': True, 'my_cat': 'a', 'ints_or_floats': {'floats': {'float1': 0.40060581587316, 'float2': 2.5052096339309498}}}\n", "\n", "Iteration: 6 Score: -5.495464339398121\n", "Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.15954325368827416, 'float2': 2.5881889160107865}}}\n", "\n", "Iteration: 7 Score: 25.718165938735293\n", "Configuration: {'my_bool': True, 'my_cat': 'b', 'ints_or_floats': {'floats': {'float1': 0.09124239203659368, 'float2': 3.123528350305318}}}\n", "\n", "Iteration: 8 Score: -10.534040821572427\n", "Configuration: {'my_bool': False, 'my_cat': 'a', 'ints_or_floats': {'floats': {'float1': 0.1705407216529995, 'float2': 2.4629694837401073}}}\n", "\n", "Iteration: 9 Score: -8.543820348517656\n", "Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.09147941562344819, 'float2': 4.18043075863538}}}\n", "\n", "Iteration: 10 Score: 34.900277874822024\n", "Configuration: {'my_bool': True, 'my_cat': 'b', 'ints_or_floats': {'floats': {'float1': 0.043502624007697124, 'float2': 4.319032110345056}}}\n", "\n", "Task Completed\n", "\n", "Best Result: { 'configuration': { 'type': 'exploitation',\n", " 'values': { 'ints_or_floats': { 'floats': { 'float1': 0.043502624007697124,\n", " 'float2': 4.319032110345056}},\n", " 'my_bool': True,\n", " 'my_cat': 'b'}},\n", " 'score': 34.900277874822024,\n", " 'user_defined_data': None}\n" ] } ], "source": [ "best_result = task.run(scoring_function, max_iterations=50, score_threshold=32)\n", "print(\"Best Result:\", best_result)" ] } ], "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.5" }, "nav_menu": {}, "toc": { "navigate_menu": true, "number_sections": false, "sideBar": true, "threshold": 6.0, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }