constrained_optimization   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A convex_function() 0 3 1
A constraint_1() 0 3 1
1
import numpy as np
2
3
from hyperactive import Hyperactive
4
5
6
def convex_function(pos_new):
7
    score = -(pos_new["x1"] * pos_new["x1"] + pos_new["x2"] * pos_new["x2"])
8
    return score
9
10
11
search_space = {
12
    "x1": list(np.arange(-100, 101, 0.1)),
13
    "x2": list(np.arange(-100, 101, 0.1)),
14
}
15
16
17
def constraint_1(para):
18
    # reject parameters where x1 and x2 are higher than 2.5 at the same time
19
    return not (para["x1"] > 2.5 and para["x2"] > 2.5)
20
21
22
# put one or more constraints inside a list
23
constraints_list = [constraint_1]
24
25
26
hyper = Hyperactive()
27
# pass list of constraints
28
hyper.add_search(
29
    convex_function,
30
    search_space,
31
    n_iter=50,
32
    constraints=constraints_list,
33
)
34
hyper.run()
35
36
search_data = hyper.search_data(convex_function)
37
38
print("\n search_data \n", search_data, "\n")
39