{
"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": [
"library(optaas.client)\n",
"\n",
"client <- OPTaaSClient$new(\"https://optaas.mindfoundry.ai\", \"Your OPTaaS API Key\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define your task"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"task <- client$create_task(\n",
" title=\"Cyclical Example\",\n",
" parameters=list(FloatParameter('x', minimum=0, maximum=1, cyclical=TRUE))\n",
")\n",
"\n",
"scoring_function <- function(x) {\n",
" cos(x)\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": [
"[1] \"Running Cyclical Example for 10 iterations\"\n",
"[1] \"Iteration: 1 Score: 0.877582561890373\"\n",
"[1] \"Iteration: 2 Score: 0.731688868873821\"\n",
"[1] \"Iteration: 3 Score: 0.968912421710645\"\n",
"[1] \"Iteration: 4 Score: 0.930507621912314\"\n",
"[1] \"Iteration: 5 Score: 0.640996858163325\"\n",
"[1] \"Iteration: 6 Score: 0.810963119505218\"\n",
"[1] \"Iteration: 7 Score: 0.992197667229329\"\n",
"[1] \"Iteration: 8 Score: 0.982473313101255\"\n",
"[1] \"Iteration: 9 Score: 0.772834946152472\"\n",
"[1] \"Iteration: 10 Score: 0.591805075092477\"\n",
"[1] \"Task Completed\"\n",
"[1] \"Best Score: 0.9922\"\n",
"[1] \"with configuration:\"\n",
"$x\n",
"[1] 0.125\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.5.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
}