More tutorials are available here
from mindfoundry.optaas.client.client import OPTaaSClient
client = OPTaaSClient('https://optaas.mindfoundry.ai', '<Your OPTaaS API key>')
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)
])
]),
]
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).
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
You can use Goal.max or Goal.min as appropriate. You can also specify the minimum and maximum score values (if known).
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
)
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.
best_result = task.run(scoring_function, max_iterations=50, score_threshold=32)
print("Best Result:", best_result)
Running task "Quick Start Example Task" for 50 iterations (or until score is 32 or better) Iteration: 0 Score: 8 Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 10, 'my_optional_int': 0}}} Iteration: 1 Score: -6.0 Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.5, 'float2': 2.5}}} Iteration: 2 Score: 8 Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 10}}} Iteration: 3 Score: 16 Configuration: {'my_bool': True, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 4, 'my_optional_int': 4}}} Iteration: 4 Score: -5.791952740746135 Configuration: {'my_bool': False, 'my_cat': 'a', 'ints_or_floats': {'floats': {'float1': 0.10035829146613562, 'float2': 1.347629893720398}}} Iteration: 5 Score: 17.43489269882466 Configuration: {'my_bool': True, 'my_cat': 'a', 'ints_or_floats': {'floats': {'float1': 0.40060581587316, 'float2': 2.5052096339309498}}} Iteration: 6 Score: -5.495464339398121 Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.15954325368827416, 'float2': 2.5881889160107865}}} Iteration: 7 Score: 25.718165938735293 Configuration: {'my_bool': True, 'my_cat': 'b', 'ints_or_floats': {'floats': {'float1': 0.09124239203659368, 'float2': 3.123528350305318}}} Iteration: 8 Score: -10.534040821572427 Configuration: {'my_bool': False, 'my_cat': 'a', 'ints_or_floats': {'floats': {'float1': 0.1705407216529995, 'float2': 2.4629694837401073}}} Iteration: 9 Score: -8.543820348517656 Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.09147941562344819, 'float2': 4.18043075863538}}} Iteration: 10 Score: 34.900277874822024 Configuration: {'my_bool': True, 'my_cat': 'b', 'ints_or_floats': {'floats': {'float1': 0.043502624007697124, 'float2': 4.319032110345056}}} Task Completed Best Result: { 'configuration': { 'type': 'exploitation', 'values': { 'ints_or_floats': { 'floats': { 'float1': 0.043502624007697124, 'float2': 4.319032110345056}}, 'my_bool': True, 'my_cat': 'b'}}, 'score': 34.900277874822024, 'user_defined_data': None}