Skip to content

Instantly share code, notes, and snippets.

@pfhayes
Created July 28, 2015 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfhayes/488c686cb3445774f38b to your computer and use it in GitHub Desktop.
Save pfhayes/488c686cb3445774f38b to your computer and use it in GitHub Desktop.
An example of using SigOpt + Rescale
import collections
import json
import requests
import sigopt.interface
import time
sigopt_user_token = 'put a user token here'
sigopt_client_token = 'put a client token here'
sigopt_client_id = 'put your client id here'
rescale_api_key = 'put a rescale api key here'
runs = 16
def run_rescale():
files = upload_rescale_files()
conn = sigopt.interface.Connection(
user_token=sigopt_user_token,
client_token=sigopt_client_token,
)
experiment = create_experiment(conn)
experiment_id = experiment.id
for i in xrange(runs):
suggestion = conn.experiments(experiment.id).suggest().suggestion
x_velocity = suggestion.assignments['x_velocity']
y_velocity = suggestion.assignments['y_velocity']
metric = compute_metric(x_velocity, y_velocity, files)
print "Run #{0}\t{1}\t{2}\t{3}".format(i + 1, x_velocity, y_velocity, metric)
conn.experiments(experiment.id).report(data={
'assignments': {
'x_velocity': x_velocity,
'y_velocity': y_velocity,
},
'value': metric,
})
def upload_rescale_files():
Files = collections.namedtuple('Files', ['input_file', 'post_process_script', 'template_file'])
input_file = rescale_post_request('https://platform.rescale.com/api/v2/files/contents/', files={
'file': open('airfoil2d.zip', 'r'),
})
post_process_script = rescale_post_request('https://platform.rescale.com/api/v2/files/contents/', files={
'file': open('extract.py', 'r'),
})
template_file = rescale_post_request('https://platform.rescale.com/api/v2/files/contents/', files={
'file': open('u_1.inp_template', 'r'),
})
return Files(input_file, post_process_script, template_file)
def compute_metric(x_velocity, y_velocity, files):
data = {
'name': 'SigOpt trial',
'paramFile': None,
'caseFile': None,
'jobvariables': [
{
'name': 'x_velocity',
'variableType': 'Param',
'displayOrder': 0,
'valueGeneratorType': 'FixedRange',
'valueGeneratorSettings': {
'minvalue': x_velocity,
'maxvalue': x_velocity,
'increment': 1,
},
},
{
'name': 'y_velocity',
'variableType': 'Param',
'displayOrder': 1,
'valueGeneratorType': 'FixedRange',
'valueGeneratorSettings': {
'minvalue': y_velocity,
'maxvalue': y_velocity,
'increment': 1,
},
},
],
"jobanalyses": [
{
"analysis": {
"code": "openfoam",
"name": "OpenFOAM",
"version": "2.3.1-openmpi",
"versionName": "2.3.1",
},
"command": "./airFoil2D/Allrun",
"hardware": {
"coreType": "standard-plus",
"coresPerSlot": 1,
},
"inputFiles": [files.input_file],
"postProcessScript": files.post_process_script,
"postProcessScriptCommand": "python extract.py airFoil2D/log.simpleFoam Cd Cl",
"templateTasks": [
{
"processedFilename": "airFoil2D/0/U",
"templateFile": files.template_file,
}
],
}
],
}
job = rescale_post_request('https://platform.rescale.com/api/v2/jobs/', data=data)
job_id = job['id']
rescale_post_request(
'https://platform.rescale.com/api/v2/jobs/{job_id}/submit/'.format(job_id=job['id']),
)
# Poll until job completes
while True:
time.sleep(10)
jobs = rescale_get_request('https://platform.rescale.com/api/v2/jobs/')['results']
status = [j for j in jobs if j['id'] == job_id][0]['jobStatus']['content']
if status == 'Completed':
break
runs = rescale_get_request('https://platform.rescale.com/api/v2/jobs/{job_id}/runs/'.format(job_id=job_id))['results']
assert len(runs) == 1
run = runs[0]
variables = run['variables']
cd_variable = [v for v in variables if v['name'] == 'Cd'][0]
return cd_variable['value']
def rescale_post_request(path, data=None, files=None):
headers = {'Authorization': 'Token ' + rescale_api_key}
if data:
headers['Content-Type'] = 'application/json'
response = requests.post(path, headers=headers, data=json.dumps(data) if data else None, files=files)
response.raise_for_status()
return response.json() if response.text else None
def rescale_get_request(path):
headers = {'Authorization': 'Token ' + rescale_api_key}
response = requests.get(path, headers=headers)
response.raise_for_status()
return response.json() if response.text else None
def create_experiment(conn):
return conn.experiments.create(
client_id=sigopt_client_id,
data={
'name': 'SigOpt + Rescale',
'parameters': [
{
'name': 'x_velocity',
'type': 'double',
'bounds': {
'min': 20,
'max': 30,
},
},
{
'name': 'y_velocity',
'type': 'double',
'bounds': {
'min': 3,
'max': 10,
},
},
],
},
).experiment
if __name__ == "__main__":
run_rescale()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment