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