Mind Foundry OPTaaS - Python Client
Quick Start
from mindfoundry.optaas.client.client import OPTaaSClient
from mindfoundry.optaas.client.goal import Goal
from mindfoundry.optaas.client.expressions import Constraint
from mindfoundry.optaas.client.parameter import IntParameter, FloatParameter, CategoricalParameter, \
BoolParameter, ChoiceParameter, GroupParameter
# Connect to your OPTaaS server with your API Key
client = OPTaaSClient('<Your OPTaaS Server URL>', '<Your OPTaaS API Key>')
# Define your parameters, e.g.
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 constraints, e.g.
constraints = [
Constraint(int_param != 13),
Constraint(when=cat_param == 'a', then=optional_int_param.is_present()),
]
# Define your scoring function, e.g.
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 return just a score, or a tuple of (score, variance)
# Create your task
task = client.create_task(
title='My Task',
parameters=parameters,
constraints=constraints,
goal=Goal.max, # or Goal.min as appropriate
min_known_score=-22, max_known_score=44 # optional
)
# Run your task
best_result = task.run(
scoring_function,
max_iterations=50,
score_threshold=32 # optional (defaults to the max_known_score defined above since the goal is "max")
)
print("Best Result: ", best_result)
Output:
Running task "My Task" for 50 iterations
(or until score is 32 or better)
Iteration: 0 Score: -6.0
Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.5, 'float2': 2.5}}}
Iteration: 1 Score: 25
Configuration: {'my_bool': True, 'my_cat': 'b', 'ints_or_floats': {'ints': {'my_int': 17}}}
Iteration: 2 Score: 22
Configuration: {'my_bool': False, 'my_cat': 'a', 'ints_or_floats': {'ints': {'my_int': 17, 'my_optional_int': 9}}}
Iteration: 3 Score: 17
Configuration: {'my_bool': False, 'my_cat': 'b', 'ints_or_floats': {'ints': {'my_int': 19}}}
Iteration: 4 Score: 2
Configuration: {'my_bool': False, 'my_cat': 'b', 'ints_or_floats': {'ints': {'my_int': 4}}}
Iteration: 5 Score: -2.9602607504199505
Configuration: {'my_bool': False, 'my_cat': 'b', 'ints_or_floats': {'floats': {'float1': 0.9526990747067507, 'float2': 0.5274313005032245}}}
Iteration: 6 Score: 8
Configuration: {'my_bool': True, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 0}}}
Iteration: 7 Score: -4.15886768219851
Configuration: {'my_bool': False, 'my_cat': 'c', 'ints_or_floats': {'floats': {'float1': 0.6693825633262663, 'float2': 1.4100512777729888}}}
Iteration: 8 Score: 12.227207128196591
Configuration: {'my_bool': True, 'my_cat': 'b', 'ints_or_floats': {'floats': {'float1': 0.2038529480927045, 'float2': 1.3245479429318694}}}
Iteration: 9 Score: -1.8345703378305132
Configuration: {'my_bool': False, 'my_cat': 'b', 'ints_or_floats': {'floats': {'float1': 0.09876773702384911, 'float2': 0.8185174318914075}}}
Iteration: 10 Score: 7
Configuration: {'my_bool': False, 'my_cat': 'a', 'ints_or_floats': {'ints': {'my_int': 11, 'my_optional_int': 0}}}
Iteration: 11 Score: 13
Configuration: {'my_bool': False, 'my_cat': 'b', 'ints_or_floats': {'ints': {'my_int': 15}}}
Iteration: 12 Score: 26
Configuration: {'my_bool': True, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 18}}}
Iteration: 13 Score: 25
Configuration: {'my_bool': True, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 17}}}
Iteration: 14 Score: 35
Configuration: {'my_bool': True, 'my_cat': 'c', 'ints_or_floats': {'ints': {'my_int': 18, 'my_optional_int': 9}}}
Task Completed
Best Result: { 'configuration': { 'type': 'exploitation',
'values': { 'ints_or_floats': {'ints': {'my_int': 18, 'my_optional_int': 9}},
'my_bool': True,
'my_cat': 'c'}},
'score': 35.0,
'user_defined_data': None}
Process finished with exit code 0