Passed
Push — master ( 87b46e...a9e71c )
by Simon
01:42
created

constrained_optimization.convex_function()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import numpy as np
2
from gradient_free_optimizers import RandomSearchOptimizer
3
4
5
def convex_function(pos_new):
6
    score = -(pos_new["x1"] * pos_new["x1"] + pos_new["x2"] * pos_new["x2"])
7
    return score
8
9
10
search_space = {
11
    "x1": np.arange(-100, 101, 0.1),
12
    "x2": np.arange(-100, 101, 0.1),
13
}
14
15
16
def constraint_1(para):
17
    # only values in 'x1' higher than -5 are valid
18
    return para["x1"] > -5
19
20
21
# put one or more constraints inside a list
22
constraints_list = [constraint_1]
23
24
25
# pass list of constraints to the optimizer
26
opt = RandomSearchOptimizer(search_space, constraints=constraints_list)
27
opt.search(convex_function, n_iter=50)
28
29
search_data = opt.search_data
30
31
# the search-data does not contain any samples where x1 is equal or below -5
32
print("\n search_data \n", search_data, "\n")
33