{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OPTaaS Batching\n", "\n", "### Note: To run this notebook, you need an API Key. You can get one here.\n", "\n", "OPTaaS can facilitate parallel computation, where you generate a batch of configurations, pass them to a number of workers to calculate the results, and then store the results to get the next batch of configurations." ] }, { "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": [ "## Create your Task" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "task <- client$create_task(\n", " title=\"Batching Example\",\n", " parameters=list(\n", " FloatParameter('x', minimum = 0, maximum = 1),\n", " FloatParameter('y', minimum = 0.1, maximum = 2)\n", " )\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set up your workers\n", "This is just a simple example of how you would pass a Configuration to a worker and get a Result back. Your process will of course likely be more complex!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "library(parallel)\n", "\n", "number_of_workers <- 4\n", "\n", "scoring_function <- function(configuration) {\n", " x <- configuration$values[[\"x\"]]\n", " y <- configuration$values[[\"y\"]]\n", " Sys.sleep(1) # Calculating score...\n", " score <- (x * y) - (x / y)\n", " Result$new(configuration=configuration, score=score)\n", "}\n", "\n", "spin_off_workers_and_get_results <- function(configurations) {\n", " mclapply(configurations, scoring_function)\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate the first batch of configurations" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"list(x = 0.5, y = 1.05)\" \"list(x = 0.25, y = 1.525)\" \n", "[3] \"list(x = 0.75, y = 0.575)\" \"list(x = 0.375, y = 0.8125)\"\n" ] } ], "source": [ "configurations <- task$generate_configurations(number_of_workers)\n", "print(paste(lapply(configurations, `[[`, \"values\")))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Record results to get the next batch of configurations\n", "\n", "The next batch will be the same size as the number of results you record." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Batch 1\"\n", "[[1]]\n", "[1] 0.04880952\n", "\n", "[[2]]\n", "[1] 0.2173156\n", "\n", "[[3]]\n", "[1] -0.8730978\n", "\n", "[[4]]\n", "[1] -0.156851\n", "\n", "[1] \"Batch 2\"\n", "[[1]]\n", "[1] 1.045734\n", "\n", "[[2]]\n", "[1] 0.06385012\n", "\n", "[[3]]\n", "[1] -1.640914\n", "\n", "[[4]]\n", "[1] -0.5423534\n", "\n", "[1] \"Batch 3\"\n", "[[1]]\n", "[1] 0.5648003\n", "\n", "[[2]]\n", "[1] 0.08435553\n", "\n", "[[3]]\n", "[1] 1.5\n", "\n", "[[4]]\n", "[1] 0.9496137\n", "\n" ] } ], "source": [ "for (batch in seq(1, 3)) {\n", " results <- spin_off_workers_and_get_results(configurations)\n", " print(paste(\"Batch\", batch))\n", " print(lapply(results, function(result) { result$score }))\n", " flush.console()\n", "\n", " configurations <- task$record_results(results)\n", "}" ] } ], "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 }