Exemple #1
0
 def fit(self):
     """ Fits the inverse normalised cdf and generates a lookup table of sort
     """
     # the for loop generates a lookup table correlating x and y based on the normalised cdf
     for idx, x_value in enumerate(self.x):
         res = sp_minimize(self.diff, 1.0, args=(x_value), method='Nelder-Mead', tol=1e-6)
         self.y[idx] = res.x[0]
def optimize_taper_params(x,
                          ERV,
                          wavelengths,
                          lambda_0,
                          init_taper_params,
                          SNAP_exp,
                          bounds=None,
                          max_iter=5):
    def _difference_on_taper(taper_params, *details):
        (absS, phaseS, ReD, ImD_exc, C) = taper_params
        x, ERV, wavelengths, lambda_0, x_exp, signal_exp = details
        SNAP = SNAP_model.SNAP(x, ERV, wavelengths, lambda_0)
        SNAP.set_taper_params(absS, phaseS, ReD, ImD_exc, C)
        x, wavelengths, num_data = SNAP.derive_transmission()
        t = _difference_between_exp_and_num(x_exp, signal_exp, x, num_data,
                                            wavelengths)
        print('taper opt. delta_t is {}'.format(t))
        return t

    if SNAP_exp.transmission_scale == 'log':
        SNAP_exp.convert_to_lin_transmission()
    x_exp, signal_exp = SNAP_exp.x, SNAP_exp.transmission
    options = {}
    options['maxiter'] = max_iter
    [absS, phaseS, ReD, ImD_exc,
     C] = init_taper_params  # use current taper parameters as initial guess
    res = sp_minimize(_difference_on_taper, [absS, phaseS, ReD, ImD_exc, C],
                      args=(x, ERV, wavelengths, lambda_0, x_exp, signal_exp),
                      bounds=bounds,
                      options=options)
    taper_params = res.x
    return res, taper_params
Exemple #3
0
def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
             hessp=None, bounds=None, constraints=(), tol=None,
             callback=None, options=None):
    """
    wrapper around scipy.optimize.minimize with extra functions
    """
    if method in SCIPY_METHODS:
        return sp_minimize(fun, x0, args, method, jac, hess, hessp, bounds,
                           constraints, tol, callback, options)
    if options is None:
        options = {}

    if method is None:
        res = to_result(x=x0, fun=fun(x0, *args), niter=1, nfev=1)
        if callback is not None:
            callback(x0)
        return res
    if method == "MIMIC":
        return _minimize_mimic(fun, x0, args, callback=callback, **options)
    elif method == "genetic":
        return _minimize_genetic(fun, x0, args, callback=callback, **options)
    elif method == "rhc":
        return _minimize_rhc(fun, x0, args, callback=callback,
                             # minimize_rhc mutates options
                             options=copy.deepcopy(options))
    elif method == "ab_test":
        return _minimize_ab_test(fun, x0, args, callback=callback, **options)
    elif method == "twiddle":
        return _minimize_twiddle(fun, x0, args, callback=callback, **options)
    elif method == "simbo_general":
        return _minimize_simbo_general(fun, x0, args, callback=callback, **options)
    else:
        raise ValueError("Unknown solver: %s" % method)
Exemple #4
0
def expected_minimum(res, n_random_starts=20, random_state=None):
    """
    Compute the minimum over the predictions of the last surrogate model.
    Uses `expected_minimum_random_sampling` with `n_random_starts`=100000,
    when the space contains any categorical values.

    .. note::
        The returned minimum may not necessarily be an accurate
        prediction of the minimum of the true objective function.

    Parameters
    ----------
    res : `OptimizeResult`, scipy object
        The optimization result returned by a `skopt` minimizer.

    n_random_starts : int, default=20
        The number of random starts for the minimization of the surrogate
        model.

    random_state : int, RandomState instance, or None (default)
        Set random state to something other than None for reproducible
        results.

    Returns
    -------
    x : list
        location of the minimum.
    fun : float
        the surrogate function value at the minimum.
    """
    if res.space.is_partly_categorical:
        return expected_minimum_random_sampling(res,
                                                n_random_starts=100000,
                                                random_state=random_state)

    def func(x):
        reg = res.models[-1]
        x = res.space.transform(x.reshape(1, -1))
        return reg.predict(x.reshape(1, -1))[0]

    xs = [res.x]
    if n_random_starts > 0:
        xs.extend(res.space.rvs(n_random_starts, random_state=random_state))

    best_x = None
    best_fun = np.inf

    for x0 in xs:
        r = sp_minimize(func, x0=x0, bounds=res.space.bounds)

        if r.fun < best_fun:
            best_x = r.x
            best_fun = r.fun

    return [v for v in best_x], best_fun
	def run_optimizer(self, loss_wrapper, sim_id, init_pos, bounds, max_iter, **kwargs):
		def local_callback(_):
			self.sim_info_dicts[sim_id]['task_id_index'] += 1
		res = sp_minimize(loss_wrapper, init_pos, method = self.method, bounds = bounds, options = {'maxiter': max_iter}, callback = local_callback)
		content = open('TIME_res_report', 'a')
		content.write('completed %s\n' % sim_id)
		for prop in dir(res):
			try:
				content.write('%s\t%s\n' % (prop, str(getattr(res, prop))))
			except:
				pass
		content.write('===============\n')
		content.close()
		self.SCIPY_OPTIMIZERS_FINISHED[sim_id] = True
Exemple #6
0
def expected_minimum(res, n_random_starts=20, random_state=None):
    """
    Compute the minimum over the predictions of the last surrogate model.

    Note that the returned minimum may not necessarily be an accurate
    prediction of the minimum of the true objective function.

    Parameters
    ----------
    * `res`  [`OptimizeResult`, scipy object]:
        The optimization result returned by a `skopt` minimizer.

    * `n_random_starts` [int, default=20]:
        The number of random starts for the minimization of the surrogate
        model.

    * `random_state` [int, RandomState instance, or None (default)]:
        Set random state to something other than None for reproducible
        results.

    Returns
    -------
    * `x` [list]: location of the minimum.

    * `fun` [float]: the surrogate function value at the minimum.
    """
    def func(x):
        reg = res.models[-1]
        x = res.space.transform(x.reshape(1, -1))

        return reg.predict(x.reshape(1, -1))[0]

    xs = [res.x]

    if n_random_starts > 0:
        xs.extend(res.space.rvs(n_random_starts, random_state=random_state))

    best_x = None
    best_fun = np.inf

    for x0 in xs:
        r = sp_minimize(func, x0=x0, bounds=res.space.bounds)

        if r.fun < best_fun:
            best_x = r.x
            best_fun = r.fun

    return [v for v in best_x], best_fun
Exemple #7
0
def minimize(input_file,
             variables_names,
             x0,
             target_filepath,
             target_variable,
             number=0,
             overwrite=True):
    if isinstance(variables_names, Iterable):
        if len(variables_names) != len(x0):
            raise ValueError('variables and x0 should be of the same length')
    result = sp_minimize(minimization_function,
                         x0,
                         args=(input_file, variables_names, target_filepath,
                               target_variable, number, overwrite),
                         tol=1e-5)
    return result
    def minimize(self):
        """Minimise self.score_function,
        given the initial values and the constraints
        using scipy.optimize.minimize.
        
        Returns:
            res: the scipy.optimize.OptimizeResult (a dict).
        """
        if self.debug_level>0:
            print '-'*90
            print '::: initial values:', self.initial_values
            print '::: constraints:', self.constraints
        res = sp_minimize(self, self.initial_values, constraints=self.constraints, )

        if self.debug_level>0:
            print res
        return res
Exemple #9
0
def expected_minimum(res, n_random_starts=20, random_state=None):
    """
    Compute the minimum over the predictions of the last surrogate model.

    Note that the returned minimum may not necessarily be an accurate
    prediction of the minimum of the true objective function.

    Parameters
    ----------
    * `res`  [`OptimizeResult`, scipy object]:
        The optimization result returned by a `skopt` minimizer.

    * `n_random_starts` [int, default=20]:
        The number of random starts for the minimization of the surrogate
        model.

    * `random_state` [int, RandomState instance, or None (default)]:
        Set random state to something other than None for reproducible
        results.

    Returns
    -------
    * `x` [list]: location of the minimum.

    * `fun` [float]: the surrogate function value at the minimum.
    """
    def func(x):
        reg = res.models[-1]
        x = res.space.transform(x.reshape(1, -1))
        return reg.predict(x.reshape(1, -1))[0]

    xs = [res.x]
    if n_random_starts > 0:
        xs.extend(res.space.rvs(n_random_starts, random_state=random_state))

    best_x = None
    best_fun = np.inf

    for x0 in xs:
        r = sp_minimize(func, x0=x0, bounds=res.space.bounds)

        if r.fun < best_fun:
            best_x = r.x
            best_fun = r.fun

    return [v for v in best_x], best_fun
def optimize_ERV_shape_by_modes(ERV_f,
                                initial_ERV_params,
                                x_0_ERV,
                                x,
                                lambda_0,
                                taper_params,
                                SNAP_exp,
                                bounds=None,
                                max_iter=20):
    def _difference_for_ERV_shape(ERV_params, *details):
        ERV_f, x_0_ERV, x, wavelengths, lambda_0, taper_params, SNAP_exp = details
        exp_modes = SNAP_exp.find_modes()
        ERV_array = ERV_f(x, x_0_ERV, ERV_params)
        SNAP = SNAP_model.SNAP(x, ERV_array, wavelengths, lambda_0)
        SNAP.set_taper_params(*taper_params)
        x_num, wavelengths, num_data = SNAP.derive_transmission()
        num_modes = SNAP.find_modes()
        N_num = len(num_modes)
        N_exp = len(exp_modes)
        if N_num > N_exp:
            exp_modes = np.sort(
                np.append(exp_modes, lambda_0 * np.ones((N_num - N_exp, 1))))
        elif N_exp > N_num:
            num_modes = np.sort(
                np.append(num_modes, lambda_0 * np.ones((N_exp - N_num, 1))))
        t = np.sum(abs(num_modes - exp_modes))
        print('mode positions difference is {}'.format(t))
        return t

    if SNAP_exp.transmission_scale == 'log':
        SNAP_exp.convert_to_lin_transmission()
    wavelengths = SNAP_exp.wavelengths
    options = {}
    options['maxiter'] = max_iter
    [absS, phaseS, ReD, ImD_exc,
     C] = taper_params  # use current taper parameters as initial guess
    res = sp_minimize(_difference_for_ERV_shape,
                      initial_ERV_params,
                      args=(ERV_f, x_0_ERV, x, wavelengths, lambda_0,
                            taper_params, SNAP_exp),
                      bounds=bounds,
                      options=options,
                      method='Nelder-Mead')
    ERV_params = res.x
    return res, ERV_params
Exemple #11
0
def sp_sample_quantile_pN_av(vn_perm, rho, quantile, key, T):
    d = jnp.shape(vn_perm)[2]

    #unif rv
    y0_init = ndtri_(quantile)

    ##for scipy optimize
    def fun_sp(y0):
        return np.array(calc_pN_av_err2(y0, vn_perm, rho, quantile, key, T),
                        dtype='float64')

    def grad_sp(y0):
        return np.array(grad_pN_av_err2(y0, vn_perm, rho, quantile, key, T),
                        dtype='float64')

    opt = sp_minimize(fun_sp, y0_init, method="SLSQP", jac=grad_sp)
    y_samp = opt.x
    err2 = opt.fun
    n_iter = opt.nit

    return y_samp, err2, n_iter
def optimize_ERV_shape_by_whole_transmission(ERV_f,
                                             initial_ERV_params,
                                             x_0_ERV,
                                             x,
                                             lambda_0,
                                             taper_params,
                                             SNAP_exp,
                                             bounds=None,
                                             max_iter=20):
    def _difference_for_ERV_shape(ERV_params, *details):
        ERV_f, x_0_ERV, x, wavelengths, lambda_0, taper_params, SNAP_exp = details
        ERV_array = ERV_f(x, x_0_ERV, ERV_params)
        SNAP = SNAP_model.SNAP(x, ERV_array, wavelengths, lambda_0)
        SNAP.set_taper_params(*taper_params)
        x_num, wavelengths, num_data = SNAP.derive_transmission()
        t = _difference_between_exp_and_num(SNAP_exp.x, SNAP_exp.transmission,
                                            x_num, num_data, wavelengths)
        print('ERV opt, delta_t is {}'.format(t))
        return t

    if SNAP_exp.transmission_scale == 'log':
        SNAP_exp.convert_to_lin_transmission()
    wavelengths = SNAP_exp.wavelengths
    options = {}
    options['maxiter'] = max_iter
    [absS, phaseS, ReD, ImD_exc,
     C] = taper_params  # use current taper parameters as initial guess
    res = sp_minimize(_difference_for_ERV_shape,
                      initial_ERV_params,
                      args=(ERV_f, x_0_ERV, x, wavelengths, lambda_0,
                            taper_params, SNAP_exp),
                      bounds=bounds,
                      options=options,
                      method='Nelder-Mead')
    ERV_params = res.x
    return res, ERV_params
 def find_minimum(self) -> np.mat:
     res = sp_minimize(self.g, self.theta, method="Nelder-Mead", tol=1e-6)
     return np.mat(res.x)