|
1 | 1 | #! usr/bin/env python3
|
2 | 2 |
|
| 3 | +from ..switch import decide |
| 4 | + |
3 | 5 | import random
|
| 6 | +import sys |
4 | 7 |
|
5 | 8 | """
|
6 | 9 | 2.2
|
|
12 | 15 | The example problem (solved by the code below) is an instance of a
|
13 | 16 | continuous function optimization that seeks min f(x) where
|
14 | 17 | f = \sum_{i=1}^{n} x_i^2, -5 <= x_i <= 5 and n = 2.
|
15 |
| -
|
16 |
| -@author Chad Estioco |
17 | 18 | """
|
18 | 19 |
|
19 | 20 | def objective_function(vector):
|
20 |
| - sum = 0 |
21 |
| - |
22 |
| - for val in vector: |
23 |
| - sum += val ** 2 |
24 |
| - |
25 |
| - return sum |
| 21 | + sum = 0 |
| 22 | + |
| 23 | + for val in vector: |
| 24 | + sum += val ** 2 |
| 25 | + |
| 26 | + return sum |
26 | 27 |
|
27 | 28 | def random_vector(minmax):
|
28 |
| - i = 0 |
29 |
| - limit = len(minmax) |
30 |
| - random_vector = [0 for i in range(limit)] |
31 |
| - |
32 |
| - for i in range(limit): |
33 |
| - spam = minmax[i][0] |
34 |
| - random_vector[i] = spam + ((minmax[i][1] - spam) * random.random()) |
35 |
| - |
36 |
| - return random_vector |
37 |
| - |
38 |
| -def search(search_space, max_iter): |
39 |
| - best = None |
40 |
| - |
41 |
| - for i in range(max_iter): |
42 |
| - candidate = {} |
43 |
| - candidate['vector'] = random_vector(search_space) |
44 |
| - candidate['cost'] = objective_function(candidate['vector']) |
45 |
| - |
46 |
| - if best is None or candidate['cost'] < best['cost']: |
47 |
| - best = candidate |
48 |
| - |
49 |
| - print("Iteration " + str(i) + ": best = " + str(best['cost'])) |
50 |
| - |
51 |
| - return best |
| 29 | + i = 0 |
| 30 | + limit = len(minmax) |
| 31 | + random_vector = [0 for i in range(limit)] |
| 32 | + |
| 33 | + for i in range(limit): |
| 34 | + spam = minmax[i][0] |
| 35 | + random_vector[i] = spam + ((minmax[i][1] - spam) * random.random()) |
| 36 | + |
| 37 | + return random_vector |
| 38 | + |
| 39 | +def search(search_space, max_iter, show_log=True): |
| 40 | + best = None |
| 41 | + |
| 42 | + for i in range(max_iter): |
| 43 | + candidate = {} |
| 44 | + candidate['vector'] = random_vector(search_space) |
| 45 | + candidate['cost'] = objective_function(candidate['vector']) |
| 46 | + |
| 47 | + if best is None or candidate['cost'] < best['cost']: |
| 48 | + best = candidate |
| 49 | + |
| 50 | + if show_log: |
| 51 | + print("Iteration " + str(i) + ": best = " + str(best['cost'])) |
| 52 | + |
| 53 | + return best |
52 | 54 |
|
53 | 55 | if __name__ == "__main__":
|
54 |
| - # problem configuration |
55 |
| - problem_size = 2 |
56 |
| - search_space = [[-5, 5] for i in range(problem_size)] |
57 |
| - |
58 |
| - # algorithm configuration |
59 |
| - max_iter = 100 |
60 |
| - |
61 |
| - # execute the algorithm |
62 |
| - best = search(search_space ,max_iter) |
63 |
| - print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector'])) |
| 56 | + # problem configuration |
| 57 | + problem_size = 2 |
| 58 | + search_space = [[-5, 5] for i in range(problem_size)] |
| 59 | + |
| 60 | + # algorithm configuration |
| 61 | + max_iter = 100 |
| 62 | + |
| 63 | + # execute the algorithm |
| 64 | + if decide(): |
| 65 | + best = search(search_space ,max_iter) |
| 66 | + print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector'])) |
| 67 | + else: |
| 68 | + best = search(search_space, max_iter, False) |
| 69 | + print(best["cost"]) |
0 commit comments