-
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.
Merge branch 'master' into feature/add-constr-opt
- Loading branch information
Showing
15 changed files
with
311 additions
and
14 deletions.
There are no files selected for viewing
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
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,24 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import DirectAlgorithm | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.1)), | ||
"y": list(np.arange(-10, 10, 0.1)), | ||
} | ||
|
||
opt = DirectAlgorithm() | ||
|
||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=1500, optimizer=opt) | ||
hyper.run() |
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,29 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import DownhillSimplexOptimizer | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.1)), | ||
"y": list(np.arange(-10, 10, 0.1)), | ||
} | ||
|
||
opt = DownhillSimplexOptimizer( | ||
alpha=1.2, | ||
gamma=1.1, | ||
beta=0.8, | ||
sigma=1, | ||
) | ||
|
||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=1500, optimizer=opt) | ||
hyper.run() |
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,26 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import GridSearchOptimizer | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.1)), | ||
"y": list(np.arange(-10, 10, 0.1)), | ||
} | ||
|
||
opt = GridSearchOptimizer( | ||
step_size=3, | ||
) | ||
|
||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=1500, optimizer=opt) | ||
hyper.run() |
25 changes: 25 additions & 0 deletions
25
examples/optimization_techniques/lipschitz_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,25 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import LipschitzOptimizer | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.1)), | ||
"y": list(np.arange(-10, 10, 0.1)), | ||
} | ||
|
||
opt = LipschitzOptimizer( | ||
sampling={"random": 100000}, | ||
) | ||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=100, optimizer=opt) | ||
hyper.run() |
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,28 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import PatternSearch | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.1)), | ||
"y": list(np.arange(-10, 10, 0.1)), | ||
} | ||
|
||
opt = PatternSearch( | ||
n_positions=2, | ||
pattern_size=0.5, | ||
reduction=0.99, | ||
) | ||
|
||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=1500, optimizer=opt) | ||
hyper.run() |
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,25 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import PowellsMethod | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.1)), | ||
"y": list(np.arange(-10, 10, 0.1)), | ||
} | ||
|
||
opt = PowellsMethod( | ||
iters_p_dim=20, | ||
) | ||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=1500, optimizer=opt) | ||
hyper.run() |
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,26 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import SpiralOptimization | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-25, 10, 0.1)), | ||
"y": list(np.arange(-10, 15, 0.1)), | ||
} | ||
|
||
opt = SpiralOptimization( | ||
population=15, | ||
decay_rate=0.99, | ||
) | ||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=1500, optimizer=opt) | ||
hyper.run() |
28 changes: 28 additions & 0 deletions
28
examples/optimization_techniques/stochastic_hill_climbing.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,28 @@ | ||
import numpy as np | ||
|
||
from hyperactive import Hyperactive | ||
from hyperactive.optimizers import StochasticHillClimbingOptimizer | ||
|
||
|
||
def sphere_function(para): | ||
x = para["x"] | ||
y = para["y"] | ||
|
||
return -(x * x + y * y) | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.1)), | ||
"y": list(np.arange(-10, 10, 0.1)), | ||
} | ||
|
||
opt = StochasticHillClimbingOptimizer( | ||
epsilon=0.01, | ||
n_neighbours=5, | ||
distribution="laplace", | ||
p_accept=0.05, | ||
) | ||
|
||
hyper = Hyperactive() | ||
hyper.add_search(sphere_function, search_space, n_iter=1500, optimizer=opt) | ||
hyper.run() |
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
Empty file.
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,26 @@ | ||
import numpy as np | ||
from hyperactive import Hyperactive | ||
|
||
|
||
def ackley_function(para): | ||
x, y = para["x"], para["y"] | ||
|
||
loss = ( | ||
-20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y))) | ||
- np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) | ||
+ np.exp(1) | ||
+ 20 | ||
) | ||
|
||
return -loss | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.01)), | ||
"y": list(np.arange(-10, 10, 0.01)), | ||
} | ||
|
||
|
||
hyper = Hyperactive(verbosity=False) | ||
hyper.add_search(ackley_function, search_space, n_iter=30) | ||
hyper.run() |
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,23 @@ | ||
import os | ||
import subprocess | ||
|
||
here = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
verbose_file = os.path.join(here, "verbose.py") | ||
non_verbose_file = os.path.join(here, "non_verbose.py") | ||
|
||
|
||
def test_empty_output(): | ||
output_verbose = subprocess.run(["python", verbose_file], stdout=subprocess.PIPE) | ||
output_non_verbose = subprocess.run( | ||
["python", non_verbose_file], stdout=subprocess.PIPE | ||
) | ||
|
||
verbose_str = output_verbose.stdout.decode() | ||
non_verbose_str = output_non_verbose.stdout.decode() | ||
|
||
print("\n verbose_str \n", verbose_str, "\n") | ||
print("\n non_verbose_str \n", non_verbose_str, "\n") | ||
|
||
assert verbose_str | ||
assert not non_verbose_str |
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,26 @@ | ||
import numpy as np | ||
from hyperactive import Hyperactive | ||
|
||
|
||
def ackley_function(para): | ||
x, y = para["x"], para["y"] | ||
|
||
loss = ( | ||
-20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y))) | ||
- np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) | ||
+ np.exp(1) | ||
+ 20 | ||
) | ||
|
||
return -loss | ||
|
||
|
||
search_space = { | ||
"x": list(np.arange(-10, 10, 0.01)), | ||
"y": list(np.arange(-10, 10, 0.01)), | ||
} | ||
|
||
|
||
hyper = Hyperactive() | ||
hyper.add_search(ackley_function, search_space, n_iter=30) | ||
hyper.run() |
Oops, something went wrong.