|
| 1 | +#! usr/bin/env python3 |
| 2 | + |
| 3 | +import random |
| 4 | + |
| 5 | +""" |
| 6 | +2.3 |
| 7 | +
|
| 8 | +Adaptive Random Search was designed to address the limitations |
| 9 | +of the fixed step size in the Localized Random Search algorithm. |
| 10 | +The strategy for Adaptive Random Search is to continually approximate |
| 11 | +the optimal step size required to reach the global optimum in the search |
| 12 | +space. This is achieved by trialling and adopting smaller or larger step sizes |
| 13 | +only if they result in an improvement in the search performance. |
| 14 | +
|
| 15 | +The strategy of Adaptive Random Search is to trial a larger step in each |
| 16 | +iteration and adopt the larger step if it results in an improved result. Very |
| 17 | +large step sizes are trialled in the same manner although with a much lower |
| 18 | +frequency. This strategy of preferring large moves is intended to allow the |
| 19 | +technique to escape local optima. Smaller step sizes are adopted if no |
| 20 | +improvement is made for an extended period. |
| 21 | +
|
| 22 | +The example problem below is similar to the one solved by Random Search [2.2]. |
| 23 | +
|
| 24 | +@author Chad Estioco |
| 25 | +""" |
| 26 | + |
| 27 | +def objective_function(vector): |
| 28 | + """ |
| 29 | + Similar to the one in [2.2] |
| 30 | + """ |
| 31 | + sum = 0 |
| 32 | + |
| 33 | + for val in vector: |
| 34 | + sum += val ** 2 |
| 35 | + |
| 36 | + return sum |
| 37 | + |
| 38 | +def rand_in_bounds(minimum, maximum): |
| 39 | + return minimum + ((maximum - minimum) * random.random()) |
| 40 | + |
| 41 | +def random_vector(minmax): |
| 42 | + """ |
| 43 | + _Essentially_ similar to the one in [2.2] |
| 44 | + """ |
| 45 | + i = 0 |
| 46 | + limit = len(minmax) |
| 47 | + random_vector = [0 for i in range(limit)] |
| 48 | + |
| 49 | + for i in range(limit): |
| 50 | + random_vector[i] = rand_in_bounds(minmax[i][0], minmax[i][1]) |
| 51 | + |
| 52 | + return random_vector |
| 53 | + |
| 54 | +def take_step(minmax, current, step_size): |
| 55 | + limit = len(current) |
| 56 | + position = [0 for i in range(limit)] |
| 57 | + |
| 58 | + for i in range(limit): |
| 59 | + minimum = max(minmax[i][0], current[i] - step_size) |
| 60 | + maximum = min(minmax[i][1], current[i] + step_size) |
| 61 | + position[i] = rand_in_bounds(minimum, maximum) |
| 62 | + |
| 63 | + return position |
| 64 | + |
| 65 | +def large_step_size(iter_count, step_size, s_factor, l_factor, iter_mult): |
| 66 | + if iter_count > 0 and iter_count % iter_mult == 0: |
| 67 | + return step_size * l_factor |
| 68 | + else: |
| 69 | + return step_size * s_factor |
| 70 | + |
| 71 | +def take_steps(bounds, current, step_size, big_stepsize): |
| 72 | + step, big_step = {}, {} |
| 73 | + step["vector"] = take_step(bounds, current["vector"], step_size) |
| 74 | + step["cost"] = objective_function(step["vector"]) |
| 75 | + big_step["vector"] = take_step(bounds, current["vector"], big_stepsize) |
| 76 | + big_step["cost"] = objective_function(big_step["vector"]) |
| 77 | + return step, big_step |
| 78 | + |
| 79 | +def search(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr): |
| 80 | + step_size = (bounds[0][1] - bounds[0][0]) * init_factor |
| 81 | + current, count = {}, 0 |
| 82 | + current["vector"] = random_vector(bounds) |
| 83 | + current["cost"] = objective_function(current["vector"]) |
| 84 | + |
| 85 | + for i in range(max_iter): |
| 86 | + big_stepsize = large_step_size(i, step_size, s_factor, l_factor, iter_mult) |
| 87 | + step, big_step = take_steps(bounds, current, step_size, big_stepsize) |
| 88 | + |
| 89 | + if step["cost"] <= current["cost"] or big_step["cost"] <= current["cost"]: |
| 90 | + if big_step["cost"] <= step["cost"]: |
| 91 | + step_size, current = big_stepsize, big_step |
| 92 | + else: |
| 93 | + current = step |
| 94 | + |
| 95 | + count = 0 |
| 96 | + else: |
| 97 | + count += 1 |
| 98 | + |
| 99 | + if count >= max_no_impr: |
| 100 | + count, stepSize = 0, (step_size/s_factor) |
| 101 | + |
| 102 | + print("Iteration " + str(i) + ": best = " + str(current["cost"])) |
| 103 | + |
| 104 | + return current |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + # problem configuration |
| 108 | + problem_size = 2 |
| 109 | + bounds = [[-5, 5] for i in range(problem_size)] |
| 110 | + |
| 111 | + # algorithm configuration |
| 112 | + max_iter = 1000 |
| 113 | + init_factor = 0.05 |
| 114 | + s_factor = 1.3 |
| 115 | + l_factor = 3.0 |
| 116 | + iter_mult = 10 |
| 117 | + max_no_impr = 30 |
| 118 | + |
| 119 | + # execute the algorithm |
| 120 | + best = search(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr) |
| 121 | + print("Done. Best Solution: cost = " + str(best["cost"]) + ", v = " + str(best["vector"])) |
0 commit comments