{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OPTaaS Warm Start\n", "\n", "### Note: To run this notebook, you need an API Key. You can get one here.\n", "\n", "You can \"warm-start\" an OPTaaS Task by providing some results that you've already calculated. This should help you get to the optimum faster." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Connect to OPTaaS using your API Key" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "library(optaas.client)\n", "\n", "client <- OPTaaSClient$new(\"https://optaas.mindfoundry.ai\", \"Your OPTaaS API Key\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define your parameters" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": false }, "outputs": [], "source": [ "parameters <- list(\n", " BoolParameter('my_bool'),\n", " CategoricalParameter('my_cat', values=list('a', 'b', 'c'), default='c'),\n", " ChoiceParameter('ints_or_floats', choices=list(\n", " GroupParameter('ints', items=list(\n", " IntParameter('my_int', minimum=0, maximum=20),\n", " IntParameter('my_optional_int', minimum=-10, maximum=10, optional=TRUE)\n", " )),\n", " GroupParameter('floats', items=list(\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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "scoring_function <- function(my_bool, my_cat, ints_or_floats) {\n", " score <- if (isTRUE(my_bool)) 5 else -5\n", " score <- if (my_cat == 'a') score + 1 else score + 3\n", " if (!is.null(ints_or_floats$ints)) {\n", " score <- score + do.call(sum, ints_or_floats$ints)\n", " } else {\n", " score <- score * do.call(sum, ints_or_floats$floats)\n", " }\n", " score\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create your Task" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "task <- client$create_task(\n", " title=\"Dummy task\",\n", " parameters=parameters,\n", " goal=\"min\", # optional (default is \"max\")\n", " min_known_score=-22, max_known_score=44 # optional\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create some warm-start configurations" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
$id
\n", "\t\t
'495ed671-91a3-48a2-a0cc-f03f018dff56'
\n", "\t
$`_links`
\n", "\t\t
\n", "\t
$self
\n", "\t\t
$href = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56'
\n", "\t
$results
\n", "\t\t
$href = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56/results'
\n", "
\n", "
\n", "\t
$values
\n", "\t\t
\n", "\t
$my_bool
\n", "\t\t
FALSE
\n", "\t
$my_cat
\n", "\t\t
'a'
\n", "\t
$ints_or_floats
\n", "\t\t
$floats =
\n", "\t
$float1
\n", "\t\t
1
\n", "\t
$float2
\n", "\t\t
3
\n", "
\n", "
\n", "
\n", "
\n", "\t
$type
\n", "\t\t
'user-defined'
\n", "
\n" ], "text/latex": [ "\\begin{description}\n", "\\item[\\$id] '495ed671-91a3-48a2-a0cc-f03f018dff56'\n", "\\item[\\$`\\_links`] \\begin{description}\n", "\\item[\\$self] \\textbf{\\$href} = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56'\n", "\\item[\\$results] \\textbf{\\$href} = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56/results'\n", "\\end{description}\n", "\n", "\\item[\\$values] \\begin{description}\n", "\\item[\\$my\\_bool] FALSE\n", "\\item[\\$my\\_cat] 'a'\n", "\\item[\\$ints\\_or\\_floats] \\textbf{\\$floats} = \\begin{description}\n", "\\item[\\$float1] 1\n", "\\item[\\$float2] 3\n", "\\end{description}\n", "\n", "\\end{description}\n", "\n", "\\item[\\$type] 'user-defined'\n", "\\end{description}\n" ], "text/markdown": [ "$id\n", ": '495ed671-91a3-48a2-a0cc-f03f018dff56'\n", "$`_links`\n", ": $self\n", ": **$href** = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56'\n", "$results\n", ": **$href** = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56/results'\n", "\n", "\n", "\n", "$values\n", ": $my_bool\n", ": FALSE\n", "$my_cat\n", ": 'a'\n", "$ints_or_floats\n", ": **$floats** = $float1\n", ": 1\n", "$float2\n", ": 3\n", "\n", "\n", "\n", "\n", "\n", "\n", "$type\n", ": 'user-defined'\n", "\n", "\n" ], "text/plain": [ "$id\n", "[1] \"495ed671-91a3-48a2-a0cc-f03f018dff56\"\n", "\n", "$`_links`\n", "$`_links`$self\n", "$`_links`$self$href\n", "[1] \"/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56\"\n", "\n", "\n", "$`_links`$results\n", "$`_links`$results$href\n", "[1] \"/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/495ed671-91a3-48a2-a0cc-f03f018dff56/results\"\n", "\n", "\n", "\n", "$values\n", "$values$my_bool\n", "[1] FALSE\n", "\n", "$values$my_cat\n", "[1] \"a\"\n", "\n", "$values$ints_or_floats\n", "$values$ints_or_floats$floats\n", "$values$ints_or_floats$floats$float1\n", "[1] 1\n", "\n", "$values$ints_or_floats$floats$float2\n", "[1] 3\n", "\n", "\n", "\n", "\n", "$type\n", "[1] \"user-defined\"\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\t
$id
\n", "\t\t
'b9f0c7f7-1884-45d3-862e-4fbe6a23707e'
\n", "\t
$`_links`
\n", "\t\t
\n", "\t
$self
\n", "\t\t
$href = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e'
\n", "\t
$results
\n", "\t\t
$href = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e/results'
\n", "
\n", "
\n", "\t
$values
\n", "\t\t
\n", "\t
$my_bool
\n", "\t\t
TRUE
\n", "\t
$my_cat
\n", "\t\t
'b'
\n", "\t
$ints_or_floats
\n", "\t\t
$ints = $my_int = 20
\n", "
\n", "
\n", "\t
$type
\n", "\t\t
'user-defined'
\n", "
\n" ], "text/latex": [ "\\begin{description}\n", "\\item[\\$id] 'b9f0c7f7-1884-45d3-862e-4fbe6a23707e'\n", "\\item[\\$`\\_links`] \\begin{description}\n", "\\item[\\$self] \\textbf{\\$href} = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e'\n", "\\item[\\$results] \\textbf{\\$href} = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e/results'\n", "\\end{description}\n", "\n", "\\item[\\$values] \\begin{description}\n", "\\item[\\$my\\_bool] TRUE\n", "\\item[\\$my\\_cat] 'b'\n", "\\item[\\$ints\\_or\\_floats] \\textbf{\\$ints} = \\textbf{\\$my\\_int} = 20\n", "\\end{description}\n", "\n", "\\item[\\$type] 'user-defined'\n", "\\end{description}\n" ], "text/markdown": [ "$id\n", ": 'b9f0c7f7-1884-45d3-862e-4fbe6a23707e'\n", "$`_links`\n", ": $self\n", ": **$href** = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e'\n", "$results\n", ": **$href** = '/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e/results'\n", "\n", "\n", "\n", "$values\n", ": $my_bool\n", ": TRUE\n", "$my_cat\n", ": 'b'\n", "$ints_or_floats\n", ": **$ints** = **$my_int** = 20\n", "\n", "\n", "\n", "$type\n", ": 'user-defined'\n", "\n", "\n" ], "text/plain": [ "$id\n", "[1] \"b9f0c7f7-1884-45d3-862e-4fbe6a23707e\"\n", "\n", "$`_links`\n", "$`_links`$self\n", "$`_links`$self$href\n", "[1] \"/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e\"\n", "\n", "\n", "$`_links`$results\n", "$`_links`$results$href\n", "[1] \"/api/v1/tasks/459cfd18-9c54-4ac3-aa7c-9cef8f229d0c/configurations/b9f0c7f7-1884-45d3-862e-4fbe6a23707e/results\"\n", "\n", "\n", "\n", "$values\n", "$values$my_bool\n", "[1] TRUE\n", "\n", "$values$my_cat\n", "[1] \"b\"\n", "\n", "$values$ints_or_floats\n", "$values$ints_or_floats$ints\n", "$values$ints_or_floats$ints$my_int\n", "[1] 20\n", "\n", "\n", "\n", "\n", "$type\n", "[1] \"user-defined\"\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "good_configuration <- list(my_bool=FALSE, my_cat='a', ints_or_floats=list(floats=list(float1=1, float2=3)))\n", "good_score <- do.call(scoring_function, good_configuration)\n", "\n", "bad_configuration <- list(my_bool=TRUE, my_cat='b', ints_or_floats=list(ints=list(my_int=20)))\n", "bad_score <- do.call(scoring_function, bad_configuration)\n", "\n", "task$add_user_defined_configuration(configuration=good_configuration, score=good_score)\n", "task$add_user_defined_configuration(configuration=bad_configuration, score=bad_score)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run your Task" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Iteration: 1 Score: 8\"\n", "[1] \"Iteration: 2 Score: 8\"\n", "[1] \"Iteration: 3 Score: -6\"\n", "[1] \"Iteration: 4 Score: 10\"\n", "[1] \"Iteration: 5 Score: 19\"\n", "[1] \"Iteration: 6 Score: 7\"\n", "[1] \"Iteration: 7 Score: -8.89617074090028\"\n", "[1] \"Iteration: 8 Score: -3.92257123192305\"\n", "[1] \"Iteration: 9 Score: 25.3918140543229\"\n", "[1] \"Iteration: 10 Score: -16.8599102274374\"\n", "[1] \"Best Score: -16.8599\"\n", "[1] \"with configuration:\"\n", "$my_bool\n", "[1] FALSE\n", "\n", "$my_cat\n", "[1] \"a\"\n", "\n", "$ints_or_floats\n", "$ints_or_floats$floats\n", "$ints_or_floats$floats$float1\n", "[1] 0.972055\n", "\n", "$ints_or_floats$floats$float2\n", "[1] 3.242923\n", "\n", "\n", "\n" ] } ], "source": [ "best_result <- task$run(scoring_function=scoring_function, number_of_iterations=10)\n", "\n", "print(paste(\"Best Score:\", best_result$score))\n", "print(\"with configuration:\")\n", "print(best_result$configuration$values)" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.4.1" }, "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 }