Skip to content

Latest commit

 

History

History
97 lines (73 loc) · 3.79 KB

quick_start.md

File metadata and controls

97 lines (73 loc) · 3.79 KB

Quick Start

The easiest way to become familiar with the tool is to go through the following example of finding minima of the second order Griewank function which is a common test scenario for optimization algorithms. Code with respective output can be found in the examples folder.

1. Prepare your target function

  • declare your phyton script as a function with a single argument named parameter
  • delete (or comment out) all hard coded parameter definitions you want to control with phs tool
  • add exec(parameter['hyperpar'],globals(),globals())
  • return a meaningful parameter
import math as ma

def test_griewank(parameter):
    exec(parameter['hyperpar'],globals(),globals())
    z = (x*x+y*y)/4000 - ma.cos(x)*ma.cos(y/ma.sqrt(2)) + 1
    return z

2. Define your search strategy

  • the search strategy specifies how the parameters expected by the target function should be generated
  • here 20 initial random sets are generated followed by 10 bayesian optimization sets
  • adapt the parameter export_path
import phs.parameter_definition  # standalone import

pardef = phs.parameter_definition.ParameterDefinition()

pardef.set_data_types_and_order([('x', float), ('y', float)])

pardef.add_individual_parameter_set(
    number_of_sets=20,
    set={'x': {'type': 'random', 'bounds': [-5, 5], 'distribution': 'uniform', 'round_digits': 3},
         'y': {'type': 'random_from_list', 'lst': [1.2, 3.4, 5.4, 6.3]}},
    prevent_duplicate=True)

pardef.add_individual_parameter_set(
    number_of_sets=10,
    set={'x': {'type': 'bayesian', 'bounds': [-5, 5], 'round_digits': 3},
         'y': {'type': 'bayesian', 'bounds': [-5, 5], 'round_digits': 3}})

pardef.export_parameter_definitions(export_path='absolute/path/to/parent/folder/for/export')

3. Setup your experiment

  • define a phs experiment by customizing the arguments experiment_dir, target_module_root_dir, target_module_name, target_function_name and parameter_definitions_root_dir_in of the class instantiation:
import phs.experiment_definition  # standalone import
# Make sure that python can import 'phs'.
# One way is to run the 'install.sh' script provided within this project.

expdef = phs.experiment_definition.ExperimentDefinition(
    experiment_dir='/absolute/path/to/parent/folder/your/experiments/should/be/saved',
    target_module_root_dir='/absolute/path/to/root/dir/in/which/your/test_function/resides',
    target_module_name='file_name_with_test_function_definition_(without_extension)',
    target_function_name='name_of_function_inside_target_module',
    parameter_definitions_root_dir_in='absolute/path/to/parent/folder/of/parameterdefinitions')

4. Setup your computation

  • define a computation on a previously saved experiment by providing the experiment_dir. Here 'local_processes' is selected as the compute environment.
import phs.compute_definition  # standalone import

compdef = phs.compute_definition.ComputeDefinition(
    experiment_dir=experiment_dir,
    parallelization='local_processes',
    local_processes_num_workers=1)

compdef.start_execution()

5. Post process the results

  • select the directory of the experiment you want to post process
import phs.post_processing  # standalone import

post_pro = phs.post_processing.PostProcessing(
    experiment_dir='/absolute/path/to/experiment_dir/you/want/to/post_process')

post_pro.plot_3d(name='plot_xyr_post',
                 first='x',
                 second='y',
                 third='result',
                 contour=True,
                 animated=True,
                 animated_step_size=1,
                 animated_fps=1)