-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example for constrained optimization
- Loading branch information
1 parent
6eb3943
commit 234bf68
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
examples/optimization_applications/constrained_optimization.py
This file contains 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,38 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
|
||
|
||
def convex_function(pos_new): | ||
score = -(pos_new["x1"] * pos_new["x1"] + pos_new["x2"] * pos_new["x2"]) | ||
return score | ||
|
||
|
||
search_space = { | ||
"x1": list(np.arange(-100, 101, 0.1)), | ||
"x2": list(np.arange(-100, 101, 0.1)), | ||
} | ||
|
||
|
||
def constraint_1(para): | ||
# reject parameters where x1 and x2 are higher than 2.5 at the same time | ||
return not (para["x1"] > 2.5 and para["x2"] > 2.5) | ||
|
||
|
||
# put one or more constraints inside a list | ||
constraints_list = [constraint_1] | ||
|
||
|
||
hyper = Hyperactive() | ||
# pass list of constraints | ||
hyper.add_search( | ||
convex_function, | ||
search_space, | ||
n_iter=50, | ||
constraints=constraints_list, | ||
) | ||
hyper.run() | ||
|
||
search_data = hyper.search_data(convex_function) | ||
|
||
print("\n search_data \n", search_data, "\n") |