def get_options(): parser = optparse.OptionParser(usage="usage: %prog [options] directory") parser.add_option("--config", dest="config_file", help="Configuration file name.", type="string", default="config.json") parser.add_option("--seed", dest="seed", help="Random seed", type="int", default=1000) (commandline_kwargs, args) = parser.parse_args() # Read in the config file expt_dir = os.path.realpath(os.path.expanduser(args[0])) if not os.path.isdir(expt_dir): raise Exception("Cannot find directory %s" % expt_dir) expt_file = os.path.join(expt_dir, commandline_kwargs.config_file) try: with open(expt_file, 'r') as f: options = json.load(f, object_pairs_hook=OrderedDict) except: raise Exception( "config.json did not load properly. Perhaps a spurious comma?") options["config"] = commandline_kwargs.config_file options["seed"] = int(commandline_kwargs.seed) options["grid_seed"] = options["seed"] # Set sensible defaults for options options['chooser'] = options.get('chooser', 'default_chooser') if 'tasks' not in options: options['tasks'] = { 'main': { 'type': 'OBJECTIVE', 'likelihood': options.get('likelihood', 'GAUSSIAN') } } # Set DB address db_address = parse_db_address(options) if 'database' not in options: options['database'] = {'name': 'spearmint', 'address': db_address} else: options['database']['address'] = db_address if not os.path.exists(expt_dir): sys.stderr.write("Cannot find experiment directory '%s'. " "Aborting.\n" % (expt_dir)) sys.exit(-1) return options, expt_dir
def get_options(**kwargs): parser = optparse.OptionParser(usage="usage: %prog [options] directory") parser.add_option("--config", dest="config_file", help="Configuration file name.", type="string", default="config.json") (commandline_kwargs, args) = parser.parse_args() # Read in the config file ######### Edit # Check if 'expt_dir' is passed via kwargs if kwargs.has_key('expt_dir'): expt_dir = kwargs['expt_dir'] else: expt_dir = os.path.realpath(os.path.expanduser(args[0])) if not os.path.isdir(expt_dir): raise Exception("Cannot find directory %s" % expt_dir) expt_file = os.path.join(expt_dir, commandline_kwargs.config_file) try: with open(expt_file, 'r') as f: options = json.load(f, object_pairs_hook=OrderedDict) except: raise Exception("config.json did not load properly. Perhaps a spurious comma?") options["config"] = commandline_kwargs.config_file # Set sensible defaults for options options['chooser'] = options.get('chooser', 'default_chooser') if 'tasks' not in options: options['tasks'] = {'main' : {'type' : 'OBJECTIVE', 'likelihood' : options.get('likelihood', 'GAUSSIAN')}} # Set DB address db_address = parse_db_address(options) if 'database' not in options: options['database'] = {'name': 'spearmint', 'address': db_address} else: options['database']['address'] = db_address if not os.path.exists(expt_dir): sys.stderr.write("Cannot find experiment directory '%s'. " "Aborting.\n" % (expt_dir)) sys.exit(-1) return options, expt_dir
def drop_db(path): if not os.path.isdir(path): raise Exception("%s is not a valid directory" % path) with open(os.path.join(path, "config.json"), "r") as f: cfg = json.load(f) db_address = parse_db_address(cfg) print "Cleaning up experiment %s in database at %s" % (cfg["experiment-name"], db_address) client = pymongo.MongoClient(db_address) db = client.spearmint db[cfg["experiment-name"]]["jobs"].drop() db[cfg["experiment-name"]]["hypers"].drop() del client
def main(): parser = optparse.OptionParser(usage="usage: %prog [options] directory") parser.add_option("--config", dest="config_file", help="Configuration file name.", type="string", default="config.json") (commandline_kwargs, args) = parser.parse_args() # Read in the config file expt_dir = os.path.realpath(args[0]) if not os.path.isdir(expt_dir): raise Exception("Cannot find directory %s" % expt_dir) expt_file = os.path.join(expt_dir, commandline_kwargs.config_file) try: with open(expt_file, 'r') as f: options = json.load(f, object_pairs_hook=OrderedDict) except: raise Exception("config.json did not load properly. Perhaps a spurious comma?") options["config"] = commandline_kwargs.config_file resources = parse_resources_from_config(options) # Set sensible defaults for options options['chooser'] = options.get('chooser', 'default_chooser') if 'tasks' not in options: options['tasks'] = {'main' : {'type' : 'OBJECTIVE', 'likelihood' : options.get('likelihood', 'GAUSSIAN')}} experiment_name = options.get("experiment-name", 'unnamed-experiment') # Set DB address db_address = parse_db_address(options) if 'database' not in options: options['database'] = {'name': 'spearmint', 'address': db_address} else: options['database']['address'] = db_address if not os.path.exists(expt_dir): sys.stderr.write("Cannot find experiment directory '%s'. " "Aborting.\n" % (expt_dir)) sys.exit(-1) # Load up the chooser. chooser_module = importlib.import_module('spearmint.choosers.' + options['chooser']) chooser = chooser_module.init(options) # Connect to the database sys.stderr.write('Using database at %s.\n' % db_address) db_address = options['database']['address'] db = MongoDB(database_address=db_address) while True: for resource_name, resource in resources.iteritems(): jobs = load_jobs(db, experiment_name) # resource.printStatus(jobs) # If the resource is currently accepting more jobs # TODO: here cost will eventually also be considered: even if the # resource is not full, we might wait because of cost incurred # Note: I chose to fill up one resource and them move on to the next # You could also do it the other way, by changing "while" to "if" here while resource.acceptingJobs(jobs): # Load jobs from DB # (move out of one or both loops?) would need to pass into load_tasks jobs = load_jobs(db, experiment_name) # Remove any broken jobs from pending. remove_broken_jobs(db, jobs, experiment_name, resources) # Get a suggestion for the next job suggested_job = get_suggestion(chooser, resource.tasks, db, expt_dir, options, resource_name) # Submit the job to the appropriate resource process_id = resource.attemptDispatch(experiment_name, suggested_job, db_address, expt_dir) # Set the status of the job appropriately (successfully submitted or not) if process_id is None: suggested_job['status'] = 'broken' save_job(suggested_job, db, experiment_name) else: suggested_job['status'] = 'pending' suggested_job['proc_id'] = process_id save_job(suggested_job, db, experiment_name) jobs = load_jobs(db, experiment_name) # Print out the status of the resources # resource.printStatus(jobs) print_resources_status(resources.values(), jobs) # If no resources are accepting jobs, sleep # (they might be accepting if suggest takes a while and so some jobs already finished by the time this point is reached) if tired(db, experiment_name, resources): time.sleep(options.get('polling-time', 5))
def main(): parser = optparse.OptionParser(usage="usage: %prog [options] directory") parser.add_option("--config", dest="config_file", help="Configuration file name.", type="string", default="config.json") (commandline_kwargs, args) = parser.parse_args() # Read in the config file expt_dir = os.path.realpath(args[0]) if not os.path.isdir(expt_dir): raise Exception("Cannot find directory %s" % expt_dir) expt_file = os.path.join(expt_dir, commandline_kwargs.config_file) try: with open(expt_file, 'r') as f: options = json.load(f, object_pairs_hook=OrderedDict) except: raise Exception( "config.json did not load properly. Perhaps a spurious comma?") options["config"] = commandline_kwargs.config_file resources = parse_resources_from_config(options) # Set sensible defaults for options options['chooser'] = options.get('chooser', 'default_chooser') options['tasks'] = options.get( 'tasks', {'main': { 'type': 'OBJECTIVE', 'likelihood': 'GAUSSIAN' }}) experiment_name = options.get("experiment-name", 'unnamed-experiment') # Set DB address db_address = parse_db_address(options) if 'database' not in options: options['database'] = {'name': 'spearmint', 'address': db_address} else: options['database']['address'] = db_address if not os.path.exists(expt_dir): sys.stderr.write("Cannot find experiment directory '%s'. " "Aborting.\n" % (expt_dir)) sys.exit(-1) # Load up the chooser. chooser_module = importlib.import_module('spearmint.choosers.' + options['chooser']) chooser = chooser_module.init(options) # Connect to the database sys.stderr.write('Using database at %s.\n' % db_address) db_address = options['database']['address'] db = MongoDB(database_address=db_address) while True: for resource_name, resource in resources.iteritems(): jobs = load_jobs(db, experiment_name) # resource.printStatus(jobs) # If the resource is currently accepting more jobs # TODO: here cost will eventually also be considered: even if the # resource is not full, we might wait because of cost incurred # Note: I chose to fill up one resource and them move on to the next # You could also do it the other way, by changing "while" to "if" here while resource.acceptingJobs(jobs): # Load jobs from DB # (move out of one or both loops?) would need to pass into load_tasks jobs = load_jobs(db, experiment_name) # Remove any broken jobs from pending. remove_broken_jobs(db, jobs, experiment_name, resources) # Get a suggestion for the next job suggested_job = get_suggestion(chooser, resource.tasks, db, expt_dir, options, resource_name) # Submit the job to the appropriate resource process_id = resource.attemptDispatch(experiment_name, suggested_job, db_address, expt_dir) # Set the status of the job appropriately (successfully submitted or not) if process_id is None: suggested_job['status'] = 'broken' save_job(suggested_job, db, experiment_name) else: suggested_job['status'] = 'pending' suggested_job['proc_id'] = process_id save_job(suggested_job, db, experiment_name) jobs = load_jobs(db, experiment_name) # Print out the status of the resources # resource.printStatus(jobs) print_resources_status(resources.values(), jobs) # If no resources are accepting jobs, sleep # (they might be accepting if suggest takes a while and so some jobs already finished by the time this point is reached) if tired(db, experiment_name, resources): time.sleep(options.get('polling-time', 5))
def get_options(): parser = optparse.OptionParser(usage="usage: %prog [options] directory") parser.add_option("--config", dest="config_file", help="Configuration file name.", type="string", default="config.json") parser.add_option("--evals", dest="max_eval", help="Maximum number of evaluations", type="int", default=200) parser.add_option("--gridseed", dest="grid_seed", help="Shifting sobol grid", type="int", default=0) parser.add_option( "--parallel", action="store_true", dest="parallel_opt", help="Use multicore in acquisition function optimization") (commandline_kwargs, args) = parser.parse_args() # Read in the config file expt_dir = os.path.realpath(os.path.expanduser(args[0])) if not os.path.isdir(expt_dir): raise Exception("Cannot find directory %s" % expt_dir) expt_file = os.path.join(expt_dir, commandline_kwargs.config_file) try: with open(expt_file, 'r') as f: options = json.load(f, object_pairs_hook=OrderedDict) except: raise Exception( "config.json did not load properly. Perhaps a spurious comma?") options["config"] = commandline_kwargs.config_file options["max_eval"] = commandline_kwargs.max_eval options["grid_seed"] = commandline_kwargs.grid_seed # Set sensible defaults for options options['chooser'] = options.get('chooser', 'default_chooser') options['chooser-args'] = {'parallel-opt': commandline_kwargs.parallel_opt} if 'tasks' not in options: options['tasks'] = { 'main': { 'type': 'OBJECTIVE', 'likelihood': options.get('likelihood', 'GAUSSIAN') } } # Set DB address db_address = parse_db_address(options) if 'database' not in options: options['database'] = {'name': 'spearmint', 'address': db_address} else: options['database']['address'] = db_address if not os.path.exists(expt_dir): sys.stderr.write("Cannot find experiment directory '%s'. " "Aborting.\n" % (expt_dir)) sys.exit(-1) return options, expt_dir