Welcome to the Badger hands-on sessions! Please read through the prerequisites section before the hands-on session to get ready for it :)
A video for the instruction part of the first hands-on session can be found here.
Please do these before the hands-on session, since the setup could take tens of minutes or even longer, based on your network condition. Thank you!
Run the following command in terminal:
git clone https://github.com/SLAC-ML/Badger-Handson.git
Then cd to the root of the cloned repo:
cd Badger-Handson
Badger and its plugins are python-based packages/scripts, so a proper python environment is needed. The easiest way to play with Badger w/o messing w/ your system is through docker. If you feel confident and would like to install Badger directly on your system, conda would be a good choice. Please choose one of the two paths below to get Badger installed on your computer.
-
Install docker if you haven't done so already. You can follow the tutorial showed up when you launch docker for the first time to get a feeling of what docker is about
-
Pull the pre-configured Badger docker image into your computer:
docker pull slacml/badger-handsonThe docker image is around 2.5GB so it would take some time to get downloaded. When the pulling is done, run
docker imagesto make sure the image is listed there -
Install XQuartz if you are using a mac. In order to expose the GUI from within the Badger docker images, we need to forward the GUI to an X window system, which is provided by XQuartz
After the installation, for the mac users, you'll need to check the Allow connections from network clients option under the Security tab of the XQuartz preference, as shown below:
If you choose to go with the docker option, a set of bash scripts are provided to make life easier:
-
Run Badger in docker:
./run.sh -
Use Badger in docker again after quit the container:
./resume.sh -
Run Badger in docker and delete the container when quit:
./run_once.shNote that Badger that launched by
run_once.shcannot be resumed after quit! -
Remove the Badger docker container and reset other system settings:
./clean_up.sh
-
Install conda if you haven't done so already
-
List all conda environments on your computer:
conda env listand make sure you don't have an environment named
badger. Then create the badger conda environment with the given yaml env file:conda env create -f environment.ymlIt would take some time (~30min) since there are some bulky packages to be resolved/installed in the predefined conda env
These slides contain some cheat sheets and solutions to the following tasks, so please give it a look when you get stuck!
Tell Badger where to find:
- Plugins
- Database (routines)
- Logbook
- Archived runs
This task should be done w/ Badger CLI.
- Investigate
sillyalgo to see the hyperparameters - Investigate
sillyenv to see the available variables/observations - Use
sillyalgorithm to optimizesillyenvironment with the routine config given intasks/01/config.yaml - Modify
tasks/01/config.yamlto limit the viable range ofq1to[0.4, 0.6], run and save the routine ashelloworld - Rerun the
helloworldroutine you just saved
This task should be done w/ Badger GUI.
-
Launch the Badger GUI
-
Load the
helloworldroutine -
Add
l1as a constraint, set the rule:l1 < 0.5And make it a hard constraint, run the optimization, keep going until alert being triggered 3 times
This task should be done w/ Badger GUI.
- Load the
helloworldroutine - Set all available
sillyenvironment variables (q1,q2,q3,q4) to be optimization variables - Try to also add
q5,q7, andq9as optimization variables, run the optimization
This task should be done w/ Badger GUI.
-
Create a new routine with
simplexalgorithm andinj_surrogateenvironment -
Set optimization variables to be:
SOL1:solenoid_field_scaleCQ01:b1_gradientSQ01:b1_gradient
And optimization objective to be:
- Minimize
norm_emit
We'll refer to this routine config lately as
routine config X -
Use the default params of
simplexalgo, run the routine, save the optimal to logbook -
Set the
start_from_currenthyperparameter ofsimplexalgo tofalse, run the routine -
Tune the
x0hyperparameter (the initial solution) to make the routine work, save the optimal to logbook, reset the environment -
Close and relaunch Badger GUI, figure out a way to add a hard constraint:
sigma_x < 0.15to
routine config X, WITHOUT configuring everything from the beginning (note that we didn't save the routine) -
Switch the algorithm to
basic_bo, run the optimization again, save the optimal to logbook, reset the environment -
Delete all the failed runs in this task
-
Read code of the
_get_obsfunction inplugins/environments/inj_surrogate/__init__.py -
Add a Gaussian noise ~
N(0, 0.01)to thenorm_emitobservation. To generate a Gaussian noise ~N(mu, sigma^2), in Python one could:import numpy as np noise = sigma * np.random.randn() + mu
- Based on
sillyalgorithm plugin, create aconstalgorithm plugin that repeatly evaluates the initial solution for a given times - Use your
constalgorithm to optimizeroutine config X - Change the objective in
routine config Xtonorm_emit_x, use yourconstalgorithm to optimize the modified problem
