from mindfoundry.optaas.client.client import OPTaaSClient
client = OPTaaSClient('https://optaas.mindfoundry.ai', '<Your OPTaaS API key>')
This can be a JSON, array, string, number or boolean:
from mindfoundry.optaas.client.parameter import FloatParameter
task = client.create_task(
title='My Task with User-defined Data',
parameters=[
FloatParameter('x', minimum=0, maximum=5),
FloatParameter('y', minimum=1, maximum=5),
],
user_defined_data={
'description': 'Lorem ipsum...',
'tags': ['abc', 'defg']
}
)
print(task.user_defined_data)
{'description': 'Lorem ipsum...', 'tags': ['abc', 'defg']}
If you've already tried some configurations, you can record the scores upfront, thus giving the optimizer a "warm start":
def scoring_function(x, y):
return (x * y) - (x / y)
warm_start_values = [
{'x': 0, 'y': 1},
{'x': 1, 'y': 2},
{'x': 0.5, 'y': 1.5},
]
for values in warm_start_values:
task.add_user_defined_configuration(values, scoring_function(**values))
task.get_results()
[{ 'configuration': {'type': 'user-defined', 'values': {'x': 0, 'y': 1}}, 'score': 0.0, 'user_defined_data': None}, { 'configuration': {'type': 'user-defined', 'values': {'x': 1, 'y': 2}}, 'score': 1.5, 'user_defined_data': None}, { 'configuration': {'type': 'user-defined', 'values': {'x': 0.5, 'y': 1.5}}, 'score': 0.4166666666666667, 'user_defined_data': None}]
This can be a JSON, array, string, number or boolean:
configuration = task.generate_configurations()[0]
score = scoring_function(**configuration.values)
task.record_result(configuration, score, user_defined_data={'Any data': ['you', 'like']})
task.get_results()[-1:]
[{ 'configuration': {'type': 'default', 'values': {'x': 2.5, 'y': 3.0}}, 'score': 6.666666666666667, 'user_defined_data': {'Any data': ['you', 'like']}}]
If you encountered an error while calculating the score for a configuration, you can report it:
error_configuration = task.generate_configurations()[0]
next_configuration = task.record_result(error_configuration,
error='Unexpected error: code 12345')
error_result = task.get_results()[-1]
print(f'Score: {error_result.score} {error_result.error}')
Score: None Unexpected error: code 12345
task.get_best_result_and_configuration()
{ 'configuration': {'type': 'default', 'values': {'x': 2.5, 'y': 3.0}}, 'score': 6.666666666666667, 'user_defined_data': {'Any data': ['you', 'like']}}
task.get_results(limit=3, best_first=True)
[{ 'configuration': {'type': 'default', 'values': {'x': 2.5, 'y': 3.0}}, 'score': 6.666666666666667, 'user_defined_data': {'Any data': ['you', 'like']}}, { 'configuration': {'type': 'user-defined', 'values': {'x': 1, 'y': 2}}, 'score': 1.5, 'user_defined_data': None}, { 'configuration': {'type': 'user-defined', 'values': {'x': 0.5, 'y': 1.5}}, 'score': 0.4166666666666667, 'user_defined_data': None}]
task.get_results(as_dataframe=True)
config.x | config.y | error | score | variance | |
---|---|---|---|---|---|
0 | 0.00 | 1.0 | None | 0.000000 | 0.0 |
1 | 1.00 | 2.0 | None | 1.500000 | 0.0 |
2 | 0.50 | 1.5 | None | 0.416667 | 0.0 |
3 | 2.50 | 3.0 | None | 6.666667 | 0.0 |
4 | 3.75 | 2.0 | Unexpected error: code 12345 | NaN | NaN |
Completing a task means that no further configurations can be generated and no further results can be recorded for it.
task.complete()
try:
task.generate_configurations()
except Exception as err:
print(err)
Status: 400 Message: Cannot add configurations to a completed task
However, you can resume a completed task if necessary:
task.resume()
task.generate_configurations()
[{'type': 'exploitation', 'values': {'x': 4.375, 'y': 4.5}}]
You will be notified with a UserWarning if a new OPTaaS Client version is available. It will look like this:
A new version of the OPTaaS client is available. Please run:
pip install mindfoundry-optaas-client==1.2.8
To stop these messages, use OPTaaSClient(url, api_key, disable_version_check=True)