- class Sampler(sampling_plan, **kwargs)[source]#
Generate samples based on a sampling plan. Initiate the class by passing a
sampling_plan) object. The class can be configured to create samples based on the defined cases in the
The class can be created with optional keyword arguments which are passed to
Configuration and sampling:
set_param()to configure the class. Use
data_dirto choose the save location for the samples.
Set the sample generating function with
set_sample_function(). This function is executed for each of the samples in the
sample_data()to generate all samples defined in the
sampling_plan. A new file is written for each sample.
Or: Create an individual sample result with
sample_idx(), where an index (
int) referring to the
sampling_plandetermines the sampled case.
By default, the
Samplerwill only create samples that do not already exist in the chosen
sp = do_mpc.sampling.SamplingPlanner() # Plan with two variables alpha and beta: sp.set_sampling_var('alpha', np.random.randn) sp.set_sampling_var('beta', lambda: np.random.randint(0,5)) plan = sp.gen_sampling_plan(n_samples=10) sampler = do_mpc.sampling.Sampler(plan) # Sampler computes the product of two variables alpha and beta # that were created in the SamplingPlanner: def sample_function(alpha, beta): return alpha*beta sampler.set_sample_function(sample_function) sampler.sample_data()
Sample data after having configured the
Sampler. No user input is required and the method will iterate through all the items defined in the
Depending on your
set_sample_function()) and the total number of samples, executing this method may take some time.
sampler.set_param(overwrite = False)(default) data will only be sampled for instances that do not yet exist.
- sample_idx(self, idx)#
Sample case based on the index of the sample.
int) – Index of the
sampling_planfor which the sample should be created.
assertion – Index must be between 0 and
assertion – sample_function must be set prior to sampling data.
- Return type
- set_param(self, **kwargs)#
Parameters must be passed as pairs of valid keywords and respective argument. For example:
sampler.set_param(overwrite = True)
overwrite (bool) – Should previously created results be overwritten. Default is
sample_name (str) – Naming scheme for samples.
save_format (str) – Choose either
print_progress (bool) – Print progress-bar to terminal. Default is
- Return type
- set_sample_function(self, sample_function)#
Set sample generating function. The sampling function produces a sample result for each sample definition in the
sampling_planand is called in the method
It is important that the sample function only uses keyword arguments with the same name as previously defined in the
sp = do_mpc.sampling.SamplingPlanner() sp.set_sampling_var('alpha', np.random.randn) sp.set_sampling_var('beta', lambda: np.random.randint(0,5)) sampler = do_mpc.sampling.Sampler(plan) def sample_function(alpha, beta): return alpha*beta sampler.set_sample_function(sample_function)
BuiltinMethodType]]) – Function to create each sample of the sampling plan.
- Return type
Set the save directory for the results. If the directory does not exist yet, it is created. If the directory is nested all (non-existing) parent folders are also created.
sampler = do_mpc.sampling.Sampler() sampler.data_dir = './samples/experiment_1/'
This will set the directory to the indicated path. If the path does not exist, all folders are created.