{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OPTaaS Quick Start\n", "\n", "### Note: To run this notebook, you need an API Key. You can get one here." ] }, { "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": [ "## Run your Task\n", "We will run the task for *at most* 25 iterations, but we will set `score_threshold=-11` because we deem that score \"good enough\" for us to stop the optimization early." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Running Dummy task for 25 iterations\"\n", "[1] \"(or until score is -11 or better)\"\n", "[1] \"Iteration: 1 Score: 8\"\n", "[1] \"Iteration: 2 Score: -6\"\n", "[1] \"Iteration: 3 Score: 8\"\n", "[1] \"Iteration: 4 Score: 1\"\n", "[1] \"Iteration: 5 Score: 11.0201365726532\"\n", "[1] \"Iteration: 6 Score: 22.7535696151694\"\n", "[1] \"Iteration: 7 Score: 22\"\n", "[1] \"Iteration: 8 Score: 15.6572248660807\"\n", "[1] \"Iteration: 9 Score: 33.4510122527765\"\n", "[1] \"Iteration: 10 Score: 11.6539745848745\"\n", "[1] \"Iteration: 11 Score: -7.15987342755936\"\n", "[1] \"Iteration: 12 Score: -8.86927219282027\"\n", "[1] \"Iteration: 13 Score: -9.15037076259624\"\n", "[1] \"Iteration: 14 Score: -11.0227581839093\"\n", "[1] \"Task Completed\"\n", "[1] \"Best Score: -11.0228\"\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.4750516\n", "\n", "$ints_or_floats$floats$float2\n", "[1] 2.280638\n", "\n", "\n", "\n" ] } ], "source": [ "best_result <- task$run(scoring_function=scoring_function, number_of_iterations=25,\n", " score_threshold=-11)\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 }