def main(expt_dir): os.chdir(expt_dir) sys.path.append(expt_dir) options = parse_config_file(expt_dir, 'config.json') experiment_name = options["experiment-name"] # main_file = options['main_file'] main_file = 'OSY_no_noisy' if main_file[-3:] == '.py': main_file = main_file[:-3] module = __import__(main_file) input_space = InputSpace(options["variables"]) chooser_module = importlib.import_module('spearmint.choosers.' + options['chooser']) chooser = chooser_module.init(input_space, options) db = MongoDB(database_address=options['database']['address']) jobs = load_jobs(db, experiment_name) hypers = db.load(experiment_name, 'hypers') tasks = parse_tasks_from_jobs(jobs, experiment_name, options, input_space) if len(tasks) < 2: print 'Not a multi-objective problem!' return -1 if options['language'] != "PYTHON": print 'Only python programs supported!' return -1 objectives = dict() contraints = dict() for task in tasks: if tasks[task].type == 'objective': objectives[task] = tasks[task] else: contraints[task] = tasks[task] assert len(objectives) >= 2 and len(contraints) >= 1 def create_fun(task): def fun(params, gradient=False): if len(params.shape) > 1 and params.shape[1] > 1: values = np.zeros(params.shape[0]) params_orig = params for i in range(params_orig.shape[0]): param = params[i, :] param = param.flatten() param = input_space.from_unit(np.array([param])).flatten() values[i] = module.main( 0, paramify_no_types(input_space.paramify(param)))[task] else: return module.main( 0, paramify_no_types(input_space.paramify(params)))[task] return values return fun funs_o = [create_fun(task) for task in objectives] funs_c = [create_fun(task) for task in contraints] moop = MOOP_basis_functions(funs_o, input_space.num_dims, constraints=funs_c) grid = sobol_grid.generate(input_space.num_dims, grid_size=1000 * input_space.num_dims) # We only retain the feasible points moop.solve_using_grid(grid) reference = np.ones(len(objectives)) * 1e3 hyper_volume_solution = moop.get_hypervolume(reference.tolist()) result = moop.compute_pareto_front_and_set() front = result['frontier'] pareto_set = result['pareto_set'] with open('hypervolume_solution.txt', 'a') as f: print >> f, "%lf" % (hyper_volume_solution) # We iterate through each recommendation made i = 0 more_recommendations = True while more_recommendations: recommendation = db.load(experiment_name, 'recommendations', {'id': i + 1}) if recommendation == None: more_recommendations = False else: solution = input_space.to_unit( input_space.vectorify(recommendation['params'])) if len(solution.shape) == 1: solution = solution.reshape((1, len(solution))) # We compute the objective values associated to this recommendation values_solution = np.zeros((solution.shape[0], len(objectives))) for j in range(values_solution.shape[0]): for k in range(values_solution.shape[1]): values_solution[j, k] = funs_o[k](solution[j:(j + 1), :]) moop = MOOP_basis_functions(funs_o, input_space.num_dims) moop.set_population(solution) hyper_volume = moop.get_hypervolume(reference.tolist()) # We make sure that there are no infeasible points recommended # If there are infeasible recommendations we return 0 as the hypervolume all_feasible = True for k in range(len(funs_c)): all_feasible = all_feasible and not np.any( funs_c[k](solution) < 0) if not all_feasible: hyper_volume = 0.0 with open('hypervolumes.txt', 'a') as f: print >> f, "%lf" % (hyper_volume) with open('evaluations.txt', 'a') as f_handle: np.savetxt( f_handle, np.array([recommendation['num_complete_tasks'].values()]), delimiter=' ', newline='\n') i += 1
def main(expt_dir): os.chdir(expt_dir) sys.path.append(expt_dir) options = parse_config_file(expt_dir, 'config.json') experiment_name = options["experiment-name"] options['main_file'] = 'prog_no_noisy' main_file = options['main_file'] if main_file[-3:] == '.py': main_file = main_file[:-3] module = __import__(main_file) input_space = InputSpace(options["variables"]) chooser_module = importlib.import_module('spearmint.choosers.' + options['chooser']) chooser = chooser_module.init(input_space, options) db = MongoDB(database_address=options['database']['address']) jobs = load_jobs(db, experiment_name) hypers = db.load(experiment_name, 'hypers') tasks = parse_tasks_from_jobs(jobs, experiment_name, options, input_space) if len(tasks) < 2: print 'Not a multi-objective problem!' return -1 if options['language'] != "PYTHON": print 'Only python programs supported!' return -1 for task in tasks: if tasks[ task ].type != 'objective': print 'Not a multi-objective problem!' return -1 def create_fun(task): def fun(params, gradient = False): if len(params.shape) > 1 and params.shape[ 1 ] > 1: params = params.flatten() params = input_space.from_unit(np.array([ params ])).flatten() return module.main(0, paramify_no_types(input_space.paramify(params)))[ task ] return fun funs = [ create_fun(task) for task in tasks ] moop = MOOP_basis_functions(funs, input_space.num_dims) # moop.evolve(1, 8) grid = sobol_grid.generate(input_space.num_dims, grid_size = 1000 * input_space.num_dims) moop.solve_using_grid(grid) # reference = find_reference_point_using_direct(tasks, module, input_space) # reference = reference + np.abs(reference) * 0.1 reference = np.ones(len(tasks)) * 7 hyper_volume_solution = moop.get_hypervolume(reference.tolist()) result = moop.compute_pareto_front_and_set() front = result['frontier'] pareto_set = result['pareto_set'] # os.remove('hypervolume_solution.txt') with open('hypervolume_solution.txt', 'a') as f: print >> f, "%lf" % (hyper_volume_solution) # os.remove('hypervolumes.txt') # We iterate through each recommendation made i = 0 more_recommendations = True while more_recommendations: recommendation = db.load(experiment_name, 'recommendations', {'id' : i + 1}) if recommendation == None: more_recommendations = False else: solution = input_space.to_unit(input_space.vectorify(recommendation[ 'params' ])) if len(solution.shape) == 1: solution = solution.reshape((1, len(solution))) # We compute the objective values associated to this recommendation values_solution = np.zeros((solution.shape[ 0 ], len(tasks))) for j in range(values_solution.shape[ 0 ]): for k in range(values_solution.shape[ 1 ]): values_solution[ j, k ] = funs[ k ](solution[ j : (j + 1), : ]) moop = MOOP_basis_functions(funs, input_space.num_dims) moop.set_population(solution) hyper_volume = moop.get_hypervolume(reference.tolist()) with open('hypervolumes.txt', 'a') as f: print >> f, "%lf" % (hyper_volume) with open('mean_min_distance_to_frontier.txt', 'a') as f: print >> f, "%lf" % (average_min_distance(values_solution, front)) with open('mean_min_distance_from_frontier.txt', 'a') as f: print >> f, "%lf" % (average_min_distance(front, values_solution)) with open('mean_min_distance_to_pareto_set.txt', 'a') as f: print >> f, "%lf" % (average_min_distance(input_space.from_unit(solution), \ input_space.from_unit(pareto_set))) with open('mean_min_distance_from_pareto_set.txt', 'a') as f: print >> f, "%lf" % (average_min_distance(input_space.from_unit(pareto_set), \ input_space.from_unit(solution))) with open('evaluations.txt','a') as f_handle: np.savetxt(f_handle, np.array([recommendation['num_complete_tasks'].values()]), delimiter = ' ', newline = '\n') i += 1