-
Notifications
You must be signed in to change notification settings - Fork 47
Mpi evaluationmanager dev #761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nychiang
wants to merge
9
commits into
develop
Choose a base branch
from
mpi-evaluationmanager-dev
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f7e98f7
create branch
nychiang e0eec20
update
nychiang ccd6930
add example
nychiang 3ce0a96
fix
nychiang 49743d8
Update reference to point to EvaluationManagerCI.py
nychiang 80c54df
Fix EvaluationManager bugs and add comprehensive testing
nychiang ed6d8ff
Address PR #761 review comments: remove xfoil references and cleanup
nychiang 42c3a01
Remove additional xfoil references from README.md
nychiang b1b5a6e
Fix Spack build by removing --no-cache flag
nychiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| """ | ||
| Code description: | ||
| for a 2D example LpNormProblem (MPI version for scaling tests) | ||
| 1) randomly sample training points | ||
| 2) define a Kriging-based Gaussian-process (smt backend) | ||
| trained on said data | ||
| 3) determine the minimizer via BOAlgorithm | ||
|
|
||
| This version uses MPIPoolExecutor for multi-node parallel execution. | ||
|
|
||
| Usage: | ||
| mpiexec -n 16 python -m mpi4py.futures BODriverEX_mpi.py | ||
|
|
||
| Authors: Tucker Hartland <hartland1@llnl.gov> | ||
| Nai-Yuan Chiang <chiang7@llnl.gov> | ||
| """ | ||
|
|
||
| import sys | ||
| import os | ||
| import numpy as np | ||
| import warnings | ||
| warnings.filterwarnings("ignore") | ||
| from hiopbbpy.surrogate_modeling import smtKRG | ||
| from hiopbbpy.opt import BOAlgorithm | ||
| from hiopbbpy.problems import BraninProblem, LpNormProblem | ||
| from hiopbbpy.utils import MPIEvaluator | ||
| from mpi4py.futures import MPIPoolExecutor | ||
|
|
||
| ### parameters | ||
| n_samples = 64 # number of the initial samples to train GP | ||
| theta = 1.e-2 # hyperparameter for GP kernel | ||
| nx = 2 # dimension of the problem | ||
| xlimits = np.array([[-5, 5], [-5, 5]]) # bounds on optimization variable | ||
|
|
||
| prob_type_l = ["LpNorm"] # ["LpNorm", "Branin"] | ||
| acq_type_l = ["LCB"] # ["LCB", "EI"] | ||
|
|
||
| def con_eq(x): | ||
| return x[0] + x[1] - 4 | ||
|
|
||
| def con_jac_eq(x): | ||
| return np.array([1.0, 1.0]) | ||
|
|
||
| def con_ineq(x): | ||
| return x[0] - x[1] | ||
|
|
||
| def con_jac_ineq(x): | ||
| return np.array([1.0, -1.0]) | ||
|
|
||
| # 'SLSQP' requires constraints defined in a list of dict. | ||
| # IPOPT can support this format, too | ||
| user_constraint_list = [{'type': 'eq', 'fun': con_eq, 'jac': con_jac_eq}, | ||
| {'type': 'ineq', 'fun': con_ineq, 'jac': con_jac_ineq}] | ||
|
|
||
| def cons_vec(x): | ||
| x1, x2 = x | ||
| return np.array([ | ||
| (x1 - 2)**2 + (x2 - 2.5)**2 - 2, | ||
| x1 + x2 - 5, | ||
| -x1 | ||
| ]) | ||
|
|
||
| # Jacobian of constraints | ||
| def cons_jac_vec(x): | ||
| x1, x2 = x | ||
| return np.array([ | ||
| [2 * (x1 - 2), 2 * (x2 - 2.5)], | ||
| [1, 1], | ||
| [-1, 0] | ||
| ]) | ||
|
|
||
| cl = -np.inf * np.ones(3) | ||
| cu = np.zeros(3) | ||
|
|
||
| # 'trust-constr' and IPOPT support vector-valued constraints | ||
| user_constraint_dict = {'cons': cons_vec, 'jac': cons_jac_vec, 'cl': cl, 'cu': cu} | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| do_profiling = True | ||
|
|
||
| for prob_type in prob_type_l: | ||
| print() | ||
| print(f"========================================") | ||
| print(f"Testing BO with {prob_type} problem") | ||
| print(f"========================================") | ||
|
|
||
| # ----- Create MPI evaluators | ||
| # MPIPoolExecutor() will use all available MPI processes when | ||
| # launched with: mpiexec -n N python -m mpi4py.futures script.py | ||
| obj_evaluator = MPIEvaluator( | ||
| executor=MPIPoolExecutor(), | ||
| profiling=do_profiling, | ||
| task_name="MPI_OBJ_EVAL" | ||
| ) | ||
| opt_evaluator = MPIEvaluator( | ||
| function_mode=False, | ||
| executor=MPIPoolExecutor(), | ||
| profiling=do_profiling, | ||
| task_name="MPI_OPT_EVAL" | ||
| ) | ||
|
|
||
| if prob_type == "LpNorm": | ||
| problem = LpNormProblem(nx, xlimits) | ||
| else: | ||
| problem = BraninProblem() | ||
| problem.set_constraints(user_constraint_list) # for solver 'trust-constr' and IPOPT, use user_constraint_dict; for solver 'SLSQP' and IPOPT, user_constraint_list | ||
|
|
||
| for acq_type in acq_type_l: | ||
| print(f"\nAcquisition type: {acq_type}") | ||
|
|
||
| ### initial training set | ||
| x_train = problem.sample(n_samples) | ||
| y_train = obj_evaluator.run(problem.evaluate, x_train) | ||
|
|
||
| ### Define the GP surrogate model | ||
| gp_model = smtKRG(theta, xlimits, nx) | ||
| gp_model.train(x_train, y_train) | ||
|
|
||
| opt_solver = 'SLSQP' #"SLSQP" "IPOPT" "trust-constr" | ||
| if opt_solver == "SLSQP" or opt_solver == "trust-constr": | ||
| solver_options = {"maxiter": 100} #for scipy solvers | ||
| elif opt_solver == "IPOPT": | ||
| solver_options = {"max_iter": 100, "print_level": 1} | ||
|
|
||
| options = { | ||
| 'acquisition_type': acq_type, | ||
| 'log_level': 'info', | ||
| 'bo_maxiter': 20, | ||
| 'opt_solver': opt_solver, | ||
| 'batch_size': 2, | ||
| 'n_start': 64, | ||
| 'solver_options': solver_options, | ||
| 'obj_evaluator': obj_evaluator, | ||
| 'opt_evaluator': opt_evaluator | ||
| } | ||
|
|
||
| # Instantiate and run Bayesian Optimization | ||
| print(f"Starting Bayesian Optimization...") | ||
| bo = BOAlgorithm(problem, gp_model, x_train, y_train, options = options) #EI or LCB | ||
| bo.optimize() | ||
|
|
||
| print(f"\nOptimization complete!") | ||
| print(f"Optimal solution: {bo.x_opt}") | ||
| print(f"Optimal value: {bo.y_opt}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
prob_type_lcontains one element do we want to keep this loop over elements ofprob_type_l?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
umm... let's keep it since we can easily switch prob_type_l to a list with more than one elements