Exemplo n.º 1
0
def test_lhs_pdist():
    n_dim = 2
    n_samples = 20
    lhs = Lhs()

    h = lhs._lhs_normalized(n_dim, n_samples, 0)
    d_classic = spatial.distance.pdist(np.array(h), 'euclidean')
    lhs = Lhs(criterion="maximin", iterations=100)
    h = lhs.generate([
        (0., 1.),
    ] * n_dim, n_samples, random_state=0)
    d = spatial.distance.pdist(np.array(h), 'euclidean')
    assert np.min(d) > np.min(d_classic)
def main():
    args = getArgumentParser().parse_args()

    space = [
        skopt.space.Real(args.mzp_min,
                         args.mzp_max,
                         name='mzp',
                         prior='uniform'),
        skopt.space.Real(args.mdh_min,
                         args.mdh_max,
                         name='mdh',
                         prior='uniform'),
        skopt.space.Real(args.mdm_min,
                         args.mdm_max,
                         name='mdm',
                         prior='uniform'),
        skopt.space.Real(args.gx_min,
                         args.gx_max,
                         name='g',
                         prior='log-uniform')
    ]

    # sample data
    lhs = Lhs(lhs_type="centered", criterion=None)
    x = np.array(lhs.generate(space, args.n_samples, random_state=42))

    # set up dataframe for writing to file
    df = pd.DataFrame(data=x, columns=["mzp", "mdh", "mdm", "g"])
    df.index.rename('dsid', inplace=True)
    df.index = np.arange(args.dsid_start, args.dsid_start + len(df))

    # write data to file
    df.to_csv('./grid_hypercube.csv', header=False)
Exemplo n.º 3
0
def test_lhs_random_state(criterion):
    n_dim = 2
    n_samples = 20
    lhs = Lhs()

    h = lhs._lhs_normalized(n_dim, n_samples, 0)
    h2 = lhs._lhs_normalized(n_dim, n_samples, 0)
    assert_array_equal(h, h2)
    lhs = Lhs(criterion=criterion, iterations=100)
    h = lhs.generate([
        (0., 1.),
    ] * n_dim, n_samples, random_state=0)
    h2 = lhs.generate([
        (0., 1.),
    ] * n_dim, n_samples, random_state=0)
    assert_array_equal(h, h2)
Exemplo n.º 4
0
def lhs_sample(n_samples):
    """
    Takes random n_samples with the lhs method.
    Returns array x and y.
    """
    x = np.array([])
    y = np.array([])

    # Makes the space of points which van be chosen from
    space = Space([(-2., 1.), (-1.5, 1.5)])

    # Chooses which kind oh lhs will be used
    lhs = Lhs(lhs_type="classic", criterion=None)

    coordinates = 0
    # Generates n_samples withhi the chosen space
    coordinates = lhs.generate(space.dimensions, n_samples)

    # appends all x and y values to array
    for coordinate in coordinates:
        a = coordinate[0]
        x = np.append(x, a)
        b = coordinate[1]
        y = np.append(y, b)

    return x, y
Exemplo n.º 5
0
    def solve(self):
        """
        Wrapper function for scipy.optimize.differential_evolution
        """
        progress = []
        def cb(xk, convergence):
            progress.append(self.fun(xk))

        # initialize number of points = popsize
        space = Space([(0.,1.)]*len(self.bounds))
        lhs = Lhs()
        pop = np.asarray(lhs.generate(space.dimensions, self.popsize))
        
        min_b, max_b = np.asarray(self.bounds).T
        diff = max_b - min_b
        pop = min_b + pop * diff
        
        progress.append(np.min(np.apply_along_axis(self.fun, 1, pop)))
        
        result = scipy_de(self.fun, self.bounds, popsize=1, maxiter = self.maxiter, tol=0.0001, disp=self.disp, callback=cb, init=pop)
        self.x = result.x
        self.fx = result.fun
        f_calls = (np.arange(1,len(progress)+1)) * self.popsize
        self.converge_data = np.vstack((f_calls, np.asarray(progress)))
        self.solved = True
Exemplo n.º 6
0
 def draw_latin_hypercube_samples(self, num_samples: int) -> list:
     """ Draws an LHS-distributed sample from the search space """
     if self.searchspace_size < num_samples:
         raise ValueError("Can't sample more than the size of the search space")
     if self.sampling_crit is None:
         lhs = Lhs(lhs_type="centered", criterion=None)
     else:
         lhs = Lhs(lhs_type="classic", criterion=self.sampling_crit, iterations=self.sampling_iter)
     param_configs = lhs.generate(self.dimensions(), num_samples)
     indices = list()
     normalized_param_configs = list()
     for i in range(len(param_configs) - 1):
         try:
             param_config = self.normalize_param_config(param_configs[i])
             index = self.find_param_config_index(param_config)
             indices.append(index)
             normalized_param_configs.append(param_config)
         except ValueError:
             """ Due to search space restrictions, the search space may not be an exact cartesian product of the tunable parameter values.
             It is thus possible for LHS to generate a parameter combination that is not in the actual searchspace, which must be skipped. """
             continue
     return list(zip(normalized_param_configs, indices))
Exemplo n.º 7
0
    def create_sampler(self):
        """
        Create the sampler

        Returns
        -------

        """

        if self.method == "lhs":
            self.lhs = Lhs(lhs_type=self.lhs_type,
                           criterion=self.criterion,
                           iterations=self.iterations)
        else:
            raise Exception("The specified sampling method ", self.method,
                            "is not supported.")
Exemplo n.º 8
0
def cook_initial_point_generator(generator, **kwargs):
    """Cook a default initial point generator.
    For the special generator called "random" the return value is None.
    Parameters
    ----------
    generator : "lhs", "sobol", "halton", "hammersly", "grid", "random" \
            or InitialPointGenerator instance"
        Should inherit from `skopt.sampler.InitialPointGenerator`.
    kwargs : dict
        Extra parameters provided to the generator at init time.
    """
    if generator is None:
        generator = "random"
    elif isinstance(generator, str):
        generator = generator.lower()
        if generator not in [
                "sobol", "halton", "hammersly", "lhs", "random", "grid",
                "maxpro", "maxpro-gd"
        ]:
            raise ValueError("Valid strings for the generator parameter "
                             " are: 'sobol', 'lhs', 'halton', 'hammersly',"
                             "'random', 'maxpro','maxpro-gd', or 'grid' not "
                             "%s." % generator)
    elif not isinstance(generator, InitialPointGenerator):
        raise ValueError("generator has to be an InitialPointGenerator."
                         "Got %s" % (str(type(generator))))

    if isinstance(generator, str):
        if generator == "sobol":
            generator = Sobol()
        elif generator == "halton":
            generator = Halton()
        elif generator == "hammersly":
            generator = Hammersly()
        elif generator == "lhs":
            generator = Lhs()
        elif generator == "grid":
            generator = Grid()
        elif generator == "random":
            return None
        elif generator == "maxpro":
            generator = MaxPro(use_gradient=False)
        elif generator == "maxpro-gd":
            generator = MaxPro(use_gradient=True)
    generator.set_params(**kwargs)
    return generator
Exemplo n.º 9
0
def test_lhs_criterion(lhs_type, criterion):
    lhs = Lhs(lhs_type=lhs_type, criterion=criterion, iterations=100)
    samples = lhs.generate([
        (0., 1.),
    ] * 2, 200)
    assert len(samples) == 200
    assert len(samples[0]) == 2
    samples = lhs.generate([("a", "b", "c")], 3)
    assert samples[0][0] in ["a", "b", "c"]

    samples = lhs.generate([("a", "b", "c"), (0, 1)], 1)
    assert samples[0][0] in ["a", "b", "c"]
    assert samples[0][1] in [0, 1]

    samples = lhs.generate([("a", "b", "c"), (0, 1)], 3)
    assert samples[0][0] in ["a", "b", "c"]
    assert samples[0][1] in [0, 1]
Exemplo n.º 10
0
 def __init__(self, bounds, method='latin'):
     self.dimensions = len(bounds)
     self.position = np.empty(self.dimensions)
     self.velocity = np.zeros(self.dimensions)
     self.pbest_position = np.empty(self.dimensions)
     self.pbest_value = None
     self.lowerbounds, self.upperbounds = np.asarray(bounds).T
     
     # initialize positions
     if method == 'random':
         position = np.random.rand(self.dimensions)
     elif method == 'latin':
         space = Space([(0.,1.)]*self.dimensions)
         lhs = Lhs()
         position = np.asarray(lhs.generate(space.dimensions,1))[0]
         
     min_b, max_b = np.asarray(bounds).T
     diff = max_b - min_b
     self.position = min_b + position * diff
Exemplo n.º 11
0
from skopt.sampler import Lhs


space = Space([
    (0.0395, 0.0790, 0.1185, 0.0000), 
    (0, 1e-2, 1e-4, 1e-8), 
    (0.08750, 0.13125, 0.17500, 0.21875, 0.26250, 0.00000)
])

n_samples = 100

rand = space.rvs(NSAMP)
len(rand)


lhs = Lhs(lhs_type="centered", criterion=None)
lhs.generate(space.dimensions, n_samples)

# if monet.isNotebook():
#     (USR, LND) = ('dsk', 'PAN')
# else:
#     (USR, LND) = (sys.argv[1], sys.argv[2])
# EXPS = aux.getExps(LND)
# (PT_ROT, _, _, _, _, _) = aux.selectPath(USR, EXPS[0], LND)
# expsToPlot = aux.EXPS_TO_PLOT
###############################################################################
# Generate keycards
###############################################################################
# kCats = [i[0] for i in aux.DATA_HEAD]
# scalers = [aux.DATA_SCA[i] for i in kCats]
# splitExps = [i.split('_')[1:] for i in expsToPlot]
#############################################################################
# Sobol'
# ------

sobol = Sobol()
x = sobol.generate(space.dimensions, n_samples)
plot_searchspace(x, "Sobol'")
pdist_data.append(pdist(x).flatten())
x_label.append("sobol'")

#############################################################################
# Classic Latin hypercube sampling
# --------------------------------

lhs = Lhs(lhs_type="classic", criterion=None)
x = lhs.generate(space.dimensions, n_samples)
plot_searchspace(x, 'classic LHS')
pdist_data.append(pdist(x).flatten())
x_label.append("lhs")

#############################################################################
# Centered Latin hypercube sampling
# ---------------------------------

lhs = Lhs(lhs_type="centered", criterion=None)
x = lhs.generate(space.dimensions, n_samples)
plot_searchspace(x, 'centered LHS')
pdist_data.append(pdist(x).flatten())
x_label.append("center")
Exemplo n.º 13
0
    new_file.close()

    print('Wrote file to ',fname)


omega_m_limits = [0.2,0.4]
h_limits = [0.6,0.8]
sigma_8_limits = [0.7,0.8]

f_esc_limits = [0.01,1.]
C_ion_limits = [0.,1.]
D_ion_limits = [0.,2.]

limits = np.array([omega_m_limits,h_limits,sigma_8_limits,f_esc_limits,C_ion_limits,D_ion_limits])

lhs = Lhs(lhs_type="classic", criterion=None)

np.random.seed(123456789)
omega_m, h, sigma_8, f_esc, C_ion, D_ion = np.array(lhs.generate(limits, n_samples= 1000)).T

file = open("simfast21.ini")
ini_file = file.readlines()
file.close()


for i in range(len(omega_m)):

    dirname = 'runs/run'+str(i)
    os.system('mkdir '+dirname)

    fname = dirname+'/simfast21.ini'
Exemplo n.º 14
0
def test_opt(env):
    """Wrapper for calling the optimizer."""

    # First day of historical data
    first_day = env.init_date
    run_params = env.bucky_params.opt_params
    if run_params.rolling:
        first_day -= datetime.timedelta(days=6)

    # Environment admin2 and admin1 values
    env_adm2 = xp.to_cpu(env.g_data.adm2_id)
    env_adm1 = xp.to_cpu(env.g_data.adm1_id)

    # Get historical case and death data
    hist = case_death_df(first_day, env_adm2)

    # Make sure environment end date is same as amount of available historical data
    days_of_hist_data = (
        hist.index.get_level_values(-1).max() - datetime.datetime(
            env.init_date.year, env.init_date.month, env.init_date.day)).days
    if days_of_hist_data != env.base_mc_instance.t_max:
        env.base_mc_instance.set_tmax(days_of_hist_data)

    # Get environment admin2 mask
    good_fips = hist.index.get_level_values("adm2").unique()
    fips_mask = xp.array(np.isin(env_adm2, good_fips))

    # Extract case and death data from data frame
    hist_daily_cases = xp.array(
        hist.cumulative_reported_cases.unstack().to_numpy())
    hist_daily_deaths = xp.array(hist.cumulative_deaths.unstack().to_numpy())

    # Sum case and death data to state
    hist_daily_cases = env.g_data.sum_adm1(hist_daily_cases, mask=fips_mask)
    hist_daily_deaths = env.g_data.sum_adm1(hist_daily_deaths, mask=fips_mask)

    # Hosp data
    hist = hosp_df(first_day, env_adm1)

    # Move hosp data to xp array where 0-index is admin1 id
    hist_daily_h_df = hist.previous_day_admission_adult_covid_confirmed.unstack(
    )
    hist_daily_h = xp.zeros(
        (hist_daily_h_df.index.max() + 1, len(hist_daily_h_df.columns)))
    hist_daily_h[hist_daily_h_df.index.to_numpy()] = hist_daily_h_df.to_numpy()

    # Collect case, death, hosp data
    hist_vals = [hist_daily_cases, hist_daily_deaths, hist_daily_h]
    # Get rid of negatives
    hist_vals = [xp.clip(vals, a_min=0.0, a_max=None) for vals in hist_vals]

    # Rolling mean
    if run_params.rolling:
        from ..util.rolling_mean import rolling_mean

        hist_vals = [rolling_mean(vals, axis=1) for vals in hist_vals]

    # Spline
    if run_params.spline:
        from ..util.spline_smooth import fit

        hist_vals = [fit(vals, df=run_params.dof) for vals in hist_vals]

    # Get rid of negatives
    hist_vals = [xp.clip(vals, a_min=0.0, a_max=None) for vals in hist_vals]

    from functools import partial

    from scipy.optimize import minimize
    from skopt import gp_minimize
    from skopt.sampler import Lhs
    from skopt.space import Real

    # Opt function params
    opt_params, keys = extract_values(env.bucky_params.base_params,
                                      env.bucky_params.opt_params.to_opt)

    # Opt function args
    args = (env, hist_vals, fips_mask, keys)

    # Global search initialization
    lhs = Lhs(criterion="maximin", iterations=10000)

    # Best objective value
    best_opt = np.inf
    best_params = opt_params

    # 2 Global searches
    for (lower, upper) in run_params.global_multipliers:
        dims = [Real(lower * p, upper * p) for p in best_params]
        res = gp_minimize(
            partial(opt_func, args=args),
            dimensions=dims,
            x0=best_params.tolist(),
            initial_point_generator=lhs,
            # callback=[checkpoint_saver],
            n_calls=run_params.global_calls,
            verbose=True,
        )
        if res.fun < best_opt:
            best_opt = res.fun
            best_params = np.array(res.x)

    # Local search
    result = minimize(
        opt_func,
        best_params,
        (args, ),
        options={
            "disp": True,
            "adaptive": True,
            "maxfev": run_params.local_calls
        },  # local_calls
        method="Nelder-Mead",
    )
    if result.fun < best_opt:
        best_opt = result.fun
        best_params = np.array(result.x)

    print("Best Opt:", best_opt)
    print("Best Params:", best_params)

    with open(BEST_OPT_FILE, "w") as f:
        best_params = [p.item() for p in best_params]
        new_params = rebuild_params(best_params, keys)
        yaml.safe_dump(new_params, f)

    with open(VALUES_FILE, "a") as f:
        f.write("{},{}\n".format(run_params.ID, best_opt))
Exemplo n.º 15
0
 def _generate(self):
     lhs = Lhs(criterion=self.criterion, iterations=self.iterations)
     X = lhs.generate(self.search_dims, self.size, random_state=self.rng)
     return X
Exemplo n.º 16
0
def test_lhs_centered():
    lhs = Lhs(lhs_type="centered")
    samples = lhs.generate([
        (0., 1.),
    ] * 3, 3)
    assert_almost_equal(np.sum(samples), 4.5)
Exemplo n.º 17
0
    def solve(self):
        """
        Solves the optimization problem through simulated annealing algorithm
        """
        
        # provide an initial state and temperature
        time = 0
        current_temp = self.initial_temp

        space = Space([(0.,1.)]*len(self.bounds))
        lhs = Lhs()
        current_state = np.asarray(lhs.generate(space.dimensions, self.popsize))
        min_b, max_b = np.asarray(self.bounds).T
        diff = max_b - min_b
        current_state = min_b + current_state * diff
    
        # evaluate current state
        energy = np.apply_along_axis(self.fun, 1, current_state)
        best_energy = np.min(energy)
        best_state = current_state[np.argmin(energy)]
        evals = self.popsize
        
        # variables for storing progress data
        progress = []
        
        for i in range(self.maxiter):
            for j in range(len(current_state)):
                # generate a new state, randomly chosen neighbour of state
                if self.get_neighbor == 'cauchy':
                    neighbor = SimulatedAnnealing.get_neighbor_cauchy(current_state[j], diff, min_b, max_b, current_temp, self.qv)
                else:
                    neighbor = SimulatedAnnealing.get_neighbor_normal(current_state[j], diff, self.initial_temp, current_temp)
                
                # evaluate new neighbor
                energy_neighbor = self.fun(neighbor)
                delta = energy_neighbor - energy[j]
                evals += 1
           
                if delta < 0:
                    current_state[j] = neighbor
                    energy[j] = energy_neighbor
                    if energy[j] < best_energy:
                        best_energy = energy[j]
                        best_state = current_state[j]
                else:
                    if np.random.rand() < np.exp(-delta/current_temp):
                        current_state[j] = neighbor
                        energy[j] = energy_neighbor
            
            progress.append(best_energy)
            
            time += 1
            current_temp = self.temp_func(self.initial_temp, current_temp, time, self.qv)
            
            if self.disp:
                print(f"simulated annealing step {i}: f(x)= {best_energy}")
                
            if evals > self.max_eval:
                break
        
        f_calls = np.arange(1, i+2) * self.popsize
        self.converge_data = np.vstack((f_calls, np.asarray(progress)))
        self.solved = True
        self.x = best_state
        self.fx = best_energy