tests.test_constr_opt   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 93
dl 0
loc 149
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
B test_constr_opt_2() 0 72 2
A test_constr_opt_1() 0 32 1
A test_constr_opt_0() 0 33 1
1
"""Test module for constraint optimization functionality."""
2
3
import numpy as np
4
5
from hyperactive import Hyperactive
6
7
8
def test_constr_opt_0():
9
    """Test constraint optimization with single constraint."""
10
11
    def objective_function(para):
12
        score = -para["x1"] * para["x1"]
13
        return score
14
15
    search_space = {
16
        "x1": list(np.arange(-15, 15, 1)),
17
    }
18
19
    def constraint_1(para):
20
        print(" para", para)
21
22
        return para["x1"] > -5
23
24
    constraints_list = [constraint_1]
25
26
    hyper = Hyperactive()
27
    hyper.add_search(
28
        objective_function,
29
        search_space,
30
        n_iter=50,
31
        constraints=constraints_list,
32
    )
33
    hyper.run()
34
35
    search_data = hyper.search_data(objective_function)
36
    x0_values = search_data["x1"].values
37
38
    print("\n search_data \n", search_data, "\n")
39
40
    assert np.all(x0_values > -5)
41
42
43
def test_constr_opt_1():
44
    """Test constraint optimization with single constraint on 2D space."""
45
46
    def objective_function(para):
47
        score = -(para["x1"] * para["x1"] + para["x2"] * para["x2"])
48
        return score
49
50
    search_space = {
51
        "x1": list(np.arange(-10, 10, 1)),
52
        "x2": list(np.arange(-10, 10, 1)),
53
    }
54
55
    def constraint_1(para):
56
        return para["x1"] > -5
57
58
    constraints_list = [constraint_1]
59
60
    hyper = Hyperactive()
61
    hyper.add_search(
62
        objective_function,
63
        search_space,
64
        n_iter=50,
65
        constraints=constraints_list,
66
    )
67
    hyper.run()
68
69
    search_data = hyper.search_data(objective_function)
70
    x0_values = search_data["x1"].values
71
72
    print("\n search_data \n", search_data, "\n")
73
74
    assert np.all(x0_values > -5)
75
76
77
def test_constr_opt_2():
78
    """Test constraint optimization with multiple constraints."""
79
    n_iter = 50
80
81
    def objective_function(para):
82
        score = -para["x1"] * para["x1"]
83
        return score
84
85
    search_space = {
86
        "x1": list(np.arange(-10, 10, 0.1)),
87
    }
88
89
    def constraint_1(para):
90
        return para["x1"] > -5
91
92
    def constraint_2(para):
93
        return para["x1"] < 5
94
95
    constraints_list = [constraint_1, constraint_2]
96
97
    hyper = Hyperactive()
98
    hyper.add_search(
99
        objective_function,
100
        search_space,
101
        n_iter=50,
102
        constraints=constraints_list,
103
    )
104
    hyper.run()
105
106
    search_data = hyper.search_data(objective_function)
107
    x0_values = search_data["x1"].values
108
109
    print("\n search_data \n", search_data, "\n")
110
111
    assert np.all(x0_values > -5)
112
    assert np.all(x0_values < 5)
113
114
    n_new_positions = 0
115
    n_new_scores = 0
116
117
    n_current_positions = 0
118
    n_current_scores = 0
119
120
    n_best_positions = 0
121
    n_best_scores = 0
122
123
    for hyper_optimizer in hyper.opt_pros.values():
124
        optimizer = hyper_optimizer.gfo_optimizer
125
126
        n_new_positions = n_new_positions + len(optimizer.pos_new_list)
127
        n_new_scores = n_new_scores + len(optimizer.score_new_list)
128
129
        n_current_positions = n_current_positions + len(optimizer.pos_current_list)
130
        n_current_scores = n_current_scores + len(optimizer.score_current_list)
131
132
        n_best_positions = n_best_positions + len(optimizer.pos_best_list)
133
        n_best_scores = n_best_scores + len(optimizer.score_best_list)
134
135
        print("\n  optimizer", optimizer)
136
        print("  n_new_positions", optimizer.pos_new_list)
137
        print("  n_new_scores", optimizer.score_new_list)
138
139
    assert n_new_positions == n_iter
140
    assert n_new_scores == n_iter
141
142
    assert n_current_positions == n_current_scores
143
    assert n_current_positions <= n_new_positions
144
145
    assert n_best_positions == n_best_scores
146
    assert n_best_positions <= n_new_positions
147
148
    assert n_new_positions == n_new_scores
149