示例#1
0
    def get_pyfanova_obj(self, improvement_over='DEFAULT', check_scenario_files = True, heap_size=8192):
        try:
            import pyfanova.fanova
            
            self.merged_dir = os.path.join(self.output_dir,"merged_run")
            
            # delete existing merged run folder
            if os.path.exists(self.merged_dir):
                import shutil
                shutil.rmtree(self.merged_dir)

            from pysmac.utils.state_merge import state_merge
            
            print(os.path.join(self.scenario_output_dir, 'state-run*'))
            print(glob.glob(os.path.join(self.scenario_output_dir, 'state-run*')))
            
            state_merge(glob.glob(os.path.join(self.scenario_output_dir, 'state-run*')),
                            self.merged_dir, check_scenario_files = check_scenario_files)
            

            return(pyfanova.fanova.Fanova(self.merged_dir, improvement_over=improvement_over,heap_size=heap_size))
                
        except ImportError:
            raise
            raise NotImplementedError('To use this feature, please install the pyfanova package.')
        except:
            print('Something went during the initialization of the FANOVA.')
            raise
示例#2
0
def smac_to_fanova(state_run_directory, destination_dir):
    '''
    Takes the state-run files, merges them and prepares the configuration space for fANOVA.
    
    outputs: fANOVA object
    
    state_run_directory: str
                        path to the directory of the pysmac_output/out/scenario file
    destination_dir: str
                    path to the directory in which the merged states should be stored
    '''

    state_run_list =[]
    files = glob(state_run_directory + "/*")
    for file in files:
        if file.startswith(state_run_directory + "/state-run"):
            state_run_list.append(file)
    state_merge.state_merge(state_run_list, destination_dir)
    merged_files = glob(destination_dir + '/*')

    for file in merged_files:
        if file.startswith(destination_dir + '/runs_and_results'):
            response_file = file
        if file.startswith(destination_dir + '/paramstrings'):
            paramstrings = file
    param_dict = output_reader.read_paramstrings_file(paramstrings)
    
    num_line = str(param_dict[0]).replace("'", "")
    num_line = str(num_line).replace("}", "")
    # messy way to get the parameter names wrt order
    f_params = []
    for line in str(num_line).split(" "):
        line = str(line).replace(",", "")
        line = line.replace('{',  '')
        if ':' in line:
            parameter = line.replace(':', '')
            f_params.append(parameter)
    
    # get configspace
    with open(destination_dir + '/param.pcs') as fh:
        cs = pcs_new.read(fh.readlines(), debug=True)

    X = []
    hps = cs.get_hyperparameters()


    for p in param_dict:
        c = CS.Configuration(cs, fix_types(p, cs), allow_inactive_with_values=True)
        X.append([])
        for hp in hps:
            if hasattr(hp, 'choices'):
                value = hp.choices.index(c[hp.name])
            else:
                value = c[hp.name]
            X[-1].append(value)
    
    X = np.array(X)
    Y = data_extractor(response_file, X.shape[0])

    return fanova.fANOVA(X = X, Y = Y, config_space= cs)
示例#3
0
文件: api.py 项目: automl/pysmac
def run_ISMAC(source_dir, function_file, working_dir, python_executable='python3',
              verbosity='ERROR', seed=None,
              exploration_time_budget=np.float('inf'),
              per_partition_time=60,
              init_rand_exploration_evaluations=20,
              init_default_evaluations=5,
              exploration_evaluations=20,
              insts_for_PEI=5,
              max_num_partitions=4,
              min_partition_size=4,
              tae_str='old', #  options 'aclib', 'old'
              save_rounds=False,
              regularize=False,
              modus='SMAC', # other choice 'ROAR'
              only_use_known_configs=False,
              budget_strat="equal" # other choice 'inc' for increasing
              ):
    state_run_directory = os.path.join(source_dir, 'out/scenario')
    merge_dir = os.path.join(working_dir, "scenario")
    state_run_list = glob.glob(state_run_directory + "/state-run*")
    state_merge.state_merge(state_run_list, merge_dir)
    
   
    # copy necessary files
    with open(merge_dir + '/scenario.txt', "r") as inputfile:
        with open(working_dir + '/piac_scenario.txt', "w") as outputfile:
            for line in inputfile:
                header, rest = line.split(" ", 1)
                if not header in ['output-dir', 'algo-exec', 'algo-exec-dir', 'run-obj']:
                    outputfile.write(line)
                if header == 'run-obj':
                    outputfile.write(" ".join([header, rest.lower()]))
            outputfile.write('algo-exec-dir = %s \n' %(working_dir))
            outputfile.write('algo-exec = %s %s' %(python_executable, function_file))
    
    scenario_file = working_dir + '/piac_scenario.txt'

    #shutil.copyfile(source_dir+'/features.dat', working_dir+'/features.dat')


    
    
    if seed is None:
        seed = np.random.seed()
    
    model = kraken(scen_file=scenario_file, per_partition_time=per_partition_time,
              working_dir=working_dir, verbosity=verbosity, seed=seed,
              exploration_time_budget=exploration_time_budget,
              init_rand_exploration_evaluations=init_rand_exploration_evaluations,
              init_default_evaluations=init_default_evaluations,
              exploration_evaluations=exploration_evaluations, insts_for_PEI=insts_for_PEI,
              max_num_partitions=max_num_partitions, tae_str=tae_str, save_rounds=save_rounds,
              min_partition_size=min_partition_size, regularize=regularize, modus=modus,
              only_known_configs=only_use_known_configs, budget_strat=budget_strat)
    return(model)
示例#4
0
    def get_pyfanova_obj(self,
                         improvement_over='DEFAULT',
                         check_scenario_files=True,
                         heap_size=8192):
        try:
            import pyfanova.fanova

            self.merged_dir = os.path.join(self.output_dir, "merged_run")

            # delete existing merged run folder
            if os.path.exists(self.merged_dir):
                import shutil
                shutil.rmtree(self.merged_dir)

            from pysmac.utils.state_merge import state_merge

            print(os.path.join(self.scenario_output_dir, 'state-run*'))
            print(
                glob.glob(os.path.join(self.scenario_output_dir,
                                       'state-run*')))

            state_merge(glob.glob(
                os.path.join(self.scenario_output_dir, 'state-run*')),
                        self.merged_dir,
                        check_scenario_files=check_scenario_files)

            return (pyfanova.fanova.Fanova(self.merged_dir,
                                           improvement_over=improvement_over,
                                           heap_size=heap_size))

        except ImportError:
            raise
            raise NotImplementedError(
                'To use this feature, please install the pyfanova package.')
        except:
            print('Something went during the initialization of the FANOVA.')
            raise
示例#5
0
def smac_to_fanova(state_run_directory, destination_dir):
    '''
    Takes the state-run files, merges them and prepares the configuration space for fANOVA.
    
    outputs: fANOVA object
    
    state_run_directory: str
                        path to the directory of the pysmac_output/out/scenario file
    destination_dir: str
                    path to the directory in which the merged states should be stored
    '''

    state_run_list = []
    files = glob(state_run_directory + "/*")
    for file in files:
        if file.startswith(state_run_directory + "/state-run"):
            state_run_list.append(file)
    state_merge.state_merge(state_run_list, destination_dir)
    merged_files = glob(destination_dir + '/*')

    for file in merged_files:
        if file.startswith(destination_dir + '/runs_and_results'):
            response_file = file
        if file.startswith(destination_dir + '/paramstrings'):
            paramstrings = file
    param_dict = output_reader.read_paramstrings_file(paramstrings)

    num_line = str(param_dict[0]).replace("'", "")
    num_line = str(num_line).replace("}", "")
    # messy way to get the parameter names wrt order
    f_params = []
    for line in str(num_line).split(" "):
        line = str(line).replace(",", "")
        line = line.replace('{', '')
        if ':' in line:
            parameter = line.replace(':', '')
            f_params.append(parameter)

    # get configspace
    with open(destination_dir + '/param.pcs') as fh:
        cs = pcs_new.read(fh.readlines(), debug=True)

    X = []
    hps = cs.get_hyperparameters()

    for p in param_dict:
        c = CS.Configuration(cs,
                             fix_types(p, cs),
                             allow_inactive_with_values=True)
        X.append([])
        for hp in hps:
            if hasattr(hp, 'choices'):
                value = hp.choices.index(c[hp.name])
            else:
                value = c[hp.name]
            X[-1].append(value)

    X = np.array(X)
    Y = data_extractor(response_file, X.shape[0])

    return fanova.fANOVA(X=X, Y=Y, config_space=cs)
示例#6
0
def run_ISMAC(
        source_dir,
        function_file,
        working_dir,
        python_executable='python3',
        verbosity='ERROR',
        seed=None,
        exploration_time_budget=np.float('inf'),
        per_partition_time=60,
        init_rand_exploration_evaluations=20,
        init_default_evaluations=5,
        exploration_evaluations=20,
        insts_for_PEI=5,
        max_num_partitions=4,
        min_partition_size=4,
        tae_str='old',  #  options 'aclib', 'old'
        save_rounds=False,
        regularize=False,
        modus='SMAC',  # other choice 'ROAR'
        only_use_known_configs=False,
        budget_strat="equal"  # other choice 'inc' for increasing
):
    state_run_directory = os.path.join(source_dir, 'out/scenario')
    merge_dir = os.path.join(working_dir, "scenario")
    state_run_list = glob.glob(state_run_directory + "/state-run*")
    state_merge.state_merge(state_run_list, merge_dir)

    # copy necessary files
    with open(merge_dir + '/scenario.txt', "r") as inputfile:
        with open(working_dir + '/piac_scenario.txt', "w") as outputfile:
            for line in inputfile:
                header, rest = line.split(" ", 1)
                if not header in [
                        'output-dir', 'algo-exec', 'algo-exec-dir', 'run-obj'
                ]:
                    outputfile.write(line)
                if header == 'run-obj':
                    outputfile.write(" ".join([header, rest.lower()]))
            outputfile.write('algo-exec-dir = %s \n' % (working_dir))
            outputfile.write('algo-exec = %s %s' %
                             (python_executable, function_file))

    scenario_file = working_dir + '/piac_scenario.txt'

    #shutil.copyfile(source_dir+'/features.dat', working_dir+'/features.dat')

    if seed is None:
        seed = np.random.seed()

    model = kraken(
        scen_file=scenario_file,
        per_partition_time=per_partition_time,
        working_dir=working_dir,
        verbosity=verbosity,
        seed=seed,
        exploration_time_budget=exploration_time_budget,
        init_rand_exploration_evaluations=init_rand_exploration_evaluations,
        init_default_evaluations=init_default_evaluations,
        exploration_evaluations=exploration_evaluations,
        insts_for_PEI=insts_for_PEI,
        max_num_partitions=max_num_partitions,
        tae_str=tae_str,
        save_rounds=save_rounds,
        min_partition_size=min_partition_size,
        regularize=regularize,
        modus=modus,
        only_known_configs=only_use_known_configs,
        budget_strat=budget_strat)
    return (model)
示例#7
0
def smac_to_fanova(state_run_directory, destination_dir):
    '''
    Takes the state-run files, merges them and prepares the configuration space for fANOVA.
    
    outputs: fANOVA object
    
    state_run_directory: str
                        path to the directory of the pysmac_output/out/scenario file
    destination_dir: str
                    path to the directory in which the merged states should be stored
    '''
    state_run_list =[]
    files = glob(state_run_directory + "/*")
    for file in files:
        if file.startswith(state_run_directory + "/state-run"):
            state_run_list.append(file)
    state_merge.state_merge(state_run_list, destination_dir)
    merged_files = glob(destination_dir + '/*')
    for file in merged_files:
        if file.startswith(destination_dir + '/runs_and_results'):
            response_file = file
        if file.startswith(destination_dir + '/paramstrings'):
            paramstrings = file
    param_dict = output_reader.read_paramstrings_file(paramstrings)
    
    num_line = str(param_dict[0]).replace("'", "")
    num_line = str(num_line).replace("}", "")
    # messy way to get the parameter names wrt order
    f_params = []
    for line in str(num_line).split(" "):
        line = str(line).replace(",", "")
        line = line.replace('{',  '')
        if ':' in line:
            parameter = line.replace(':', '')
            f_params.append(parameter)

    # getting features
    all_nums = []
    for dict_row in param_dict:
        num_line = str(dict_row).replace("'", "")
        num_line = str(num_line).replace("}", "")
        nums = []
        for line in str(num_line).split(" "):
            line = str(line).replace(",", "")
            if line.isdigit():
                nums.append(np.int(line))
            elif line.replace(".", "", 1).isdigit():
                nums.append(np.float(line))
            elif '-' in line:
                new_line = line.replace("-","")
                if new_line.isdigit():
                    nums.append(np.int(line))
                elif new_line.replace(".", "", 1).isdigit():
                    nums.append(np.float(line))
        all_nums.append(nums)

    x = np.array(all_nums)
    length = len(x)
    Y = data_extractor(response_file, length)
    fh = open(destination_dir + '/param.pcs')
    orig_pcs = fh.readlines()
    cs = pcs_new.read(orig_pcs, debug=True)
    X = np.zeros((x.shape))


    for i in range(x.shape[1]):
        idx = cs.get_idx_by_hyperparameter_name(f_params[i])
        X[:, idx] = x[:, i]   
    # create an instance of fanova with data for the random forest and the configSpace
    return fanova.fANOVA(X = X, Y = Y, config_space= cs)