Example #1
0
def _random_config(search_space, random_state):
    chosen_config = {}
    for key, val in search_space.items():
        if val['_type'] == 'choice':
            choices = val['_value']
            index = random_state.randint(len(choices))
            if all([isinstance(c, (int, float)) for c in choices]):
                chosen_config[key] = choices[index]
            else:
                raise ValueError('Choices with type other than int and float is not supported.')
        elif val['_type'] == 'uniform':
            chosen_config[key] = random_state.uniform(val['_value'][0], val['_value'][1])
        elif val['_type'] == 'randint':
            chosen_config[key] = random_state.randint(
                val['_value'][0], val['_value'][1])
        elif val['_type'] == 'quniform':
            chosen_config[key] = parameter_expressions.quniform(
                val['_value'][0], val['_value'][1], val['_value'][2], random_state)
        elif val['_type'] == 'loguniform':
            chosen_config[key] = parameter_expressions.loguniform(
                val['_value'][0], val['_value'][1], random_state)
        elif val['_type'] == 'qloguniform':
            chosen_config[key] = parameter_expressions.qloguniform(
                val['_value'][0], val['_value'][1], val['_value'][2], random_state)
        else:
            raise ValueError('Unknown key %s and value %s' % (key, val))
    return chosen_config
Example #2
0
    def random_sample(self):
        """
        Creates a random point within the bounds of the space.

        """
        params = np.empty(self.dim)
        for col, _bound in enumerate(self._bounds):
            if _bound['_type'] == 'choice':
                params[col] = parameter_expressions.choice(
                    _bound['_value'], self.random_state)
            elif _bound['_type'] == 'randint':
                params[col] = self.random_state.randint(_bound['_value'][0],
                                                        _bound['_value'][1],
                                                        size=1)
            elif _bound['_type'] == 'uniform':
                params[col] = parameter_expressions.uniform(
                    _bound['_value'][0], _bound['_value'][1],
                    self.random_state)
            elif _bound['_type'] == 'quniform':
                params[col] = parameter_expressions.quniform(
                    _bound['_value'][0], _bound['_value'][1],
                    _bound['_value'][2], self.random_state)
            elif _bound['_type'] == 'loguniform':
                params[col] = parameter_expressions.loguniform(
                    _bound['_value'][0], _bound['_value'][1],
                    self.random_state)
            elif _bound['_type'] == 'qloguniform':
                params[col] = parameter_expressions.qloguniform(
                    _bound['_value'][0], _bound['_value'][1],
                    _bound['_value'][2], self.random_state)

        return params