#!/usr/bin/env python
# coding: utf-8
# # OPTaaS Quick Start
#
# ### Note: To run this notebook, you need an API Key. You can get one here.
#
# More tutorials are [available here](./)
# ## Connect to OPTaaS using your API Key
# In[1]:
from mindfoundry.optaas.client.client import OPTaaSClient
client = OPTaaSClient('https://optaas.mindfoundry.ai', '')
# ## Define your parameters
# In[2]:
from mindfoundry.optaas.client.parameter import IntParameter, FloatParameter, CategoricalParameter, BoolParameter, \
ChoiceParameter, GroupParameter
bool_param = BoolParameter('my_bool')
cat_param = CategoricalParameter('my_cat', values=['a', 'b', 'c'], default='c')
int_param = IntParameter('my_int', minimum=0, maximum=20)
optional_int_param = IntParameter('my_optional_int', minimum=-10, maximum=10, optional=True)
parameters = [
bool_param,
cat_param,
ChoiceParameter('ints_or_floats', choices=[
GroupParameter('ints', items=[int_param, optional_int_param]),
GroupParameter('floats', items=[
FloatParameter('float1', minimum=0, maximum=1),
FloatParameter('float2', minimum=0.5, maximum=4.5)
])
]),
]
# ## Define your scoring function
#
# The argument names in your scoring function must match the parameter names you defined above.
#
# Your function can return just a score, or a tuple of (score, variance).
# In[3]:
def scoring_function(my_bool, my_cat, ints_or_floats):
score = 5 if my_bool is True else -5
score += 1 if my_cat == 'a' else 3
if 'ints' in ints_or_floats:
score += sum(ints_or_floats['ints'].values())
else:
score *= sum(ints_or_floats['floats'].values())
return score
# ## Create your Task
#
# You can use Goal.max or Goal.min as appropriate. You can also specify the minimum and maximum score values (if known).
# In[4]:
from mindfoundry.optaas.client.client import Goal
task = client.create_task(
title='Quick Start Example Task',
parameters=parameters,
goal=Goal.max,
min_known_score=-22,
max_known_score=44
)
# ## Run your Task
#
# We will run for a maximum of 50 iterations, but we will stop as soon as we reach our score threshold of 32.
#
# The score threshold is optional - you can omit it and simply run as many iterations as you need.
# In[5]:
best_result = task.run(scoring_function, max_iterations=50, score_threshold=32)
print("Best Result:", best_result)