Tests ILS acceptance behaviour over a fixed trajectory.
(ok_small)
| 128 | |
| 129 | |
| 130 | def test_ils_acceptance_behaviour(ok_small): |
| 131 | """ |
| 132 | Tests ILS acceptance behaviour over a fixed trajectory. |
| 133 | """ |
| 134 | sols = [ |
| 135 | Solution(ok_small, [[1, 3], [2, 4]]), # 22065 (infeas) |
| 136 | Solution(ok_small, [[1, 2], [3, 4]]), # 9725 |
| 137 | Solution(ok_small, [[1, 2], [4, 3]]), # 9868 |
| 138 | Solution(ok_small, [[1, 4], [2, 3]]), # 9240 |
| 139 | Solution(ok_small, [[1, 2], [3, 4]]), # 9725 |
| 140 | ] |
| 141 | |
| 142 | ils = IteratedLocalSearch( |
| 143 | ok_small, |
| 144 | PenaltyManager(initial_penalties=([20], 6, 6)), |
| 145 | RandomNumberGenerator(42), |
| 146 | lambda *_, **kws: sols.pop(0), # returns from sols one at a time |
| 147 | sols[0], |
| 148 | IteratedLocalSearchParams(history_length=2, exhaustive_on_best=False), |
| 149 | ) |
| 150 | |
| 151 | res = ils.run(stop=MaxIterations(len(sols))) |
| 152 | data = res.stats.data |
| 153 | |
| 154 | # First solution is also the initial solution, so nothing should have |
| 155 | # changed. |
| 156 | assert_equal(data[0].current_cost, 22_065) |
| 157 | assert_equal(data[0].candidate_cost, 22_065) |
| 158 | assert_equal(data[0].best_cost, 22_065) |
| 159 | |
| 160 | # The second iteration has a solution that is *much* better. This new |
| 161 | # solution should become both the current and best. |
| 162 | assert_equal(data[1].current_cost, 9_725) |
| 163 | assert_equal(data[1].candidate_cost, 9_725) |
| 164 | assert_equal(data[1].best_cost, 9_725) |
| 165 | |
| 166 | # We now get a candidate solution that is a little worse than the previous |
| 167 | # iteration, but better than the solution from two iterations ago. It is |
| 168 | # thus accepted. |
| 169 | assert_equal(data[2].current_cost, 9_868) |
| 170 | assert_equal(data[2].candidate_cost, 9_868) |
| 171 | assert_equal(data[2].best_cost, 9_725) |
| 172 | |
| 173 | # We now find a new best solution that should also be accepted. |
| 174 | assert_equal(data[3].current_cost, 9_240) |
| 175 | assert_equal(data[3].candidate_cost, 9_240) |
| 176 | assert_equal(data[3].best_cost, 9_240) |
| 177 | |
| 178 | # In the last iteration we again find a solution we found earlier. This |
| 179 | # solution improves over the solution from two iterations ago, and should |
| 180 | # thus be accepted. |
| 181 | assert_equal(data[4].current_cost, 9_725) |
| 182 | assert_equal(data[4].candidate_cost, 9_725) |
| 183 | assert_equal(data[4].best_cost, 9_240) |
| 184 | |
| 185 | |
| 186 | def test_restart(ok_small): |
nothing calls this directly
no test coverage detected