|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "2a871b92", |
6 | 5 | "metadata": {}, |
7 | 6 | "source": [ |
8 | 7 | "# OneMax function\n", |
|
25 | 24 | }, |
26 | 25 | { |
27 | 26 | "cell_type": "markdown", |
28 | | - "id": "5314f4cb", |
29 | 27 | "metadata": {}, |
30 | 28 | "source": [ |
31 | 29 | "### First we import python libraries and set up the directory of our code." |
|
34 | 32 | { |
35 | 33 | "cell_type": "code", |
36 | 34 | "execution_count": 1, |
37 | | - "id": "e3e288b8", |
38 | 35 | "metadata": {}, |
39 | 36 | "outputs": [], |
40 | 37 | "source": [ |
|
48 | 45 | }, |
49 | 46 | { |
50 | 47 | "cell_type": "markdown", |
51 | | - "id": "a0ca56c0", |
52 | 48 | "metadata": {}, |
53 | 49 | "source": [ |
54 | 50 | "### Here we import all our custom GA code." |
|
57 | 53 | { |
58 | 54 | "cell_type": "code", |
59 | 55 | "execution_count": 2, |
60 | | - "id": "73015625", |
61 | 56 | "metadata": {}, |
62 | 57 | "outputs": [], |
63 | 58 | "source": [ |
|
78 | 73 | }, |
79 | 74 | { |
80 | 75 | "cell_type": "markdown", |
81 | | - "id": "fd95b659", |
82 | 76 | "metadata": {}, |
83 | 77 | "source": [ |
84 | 78 | "### Define the OneMax fitness function." |
|
87 | 81 | { |
88 | 82 | "cell_type": "code", |
89 | 83 | "execution_count": 3, |
90 | | - "id": "5a2d2bc4", |
91 | 84 | "metadata": {}, |
92 | 85 | "outputs": [], |
93 | 86 | "source": [ |
94 | 87 | "# OneMax function.\n", |
95 | 88 | "def fun_OneMax(individual: Chromosome, f_min: bool = False):\n", |
96 | 89 | " \n", |
97 | 90 | " # Compute the function value.\n", |
98 | | - " f_val = fsum([xi.value for xi in individual])\n", |
| 91 | + " f_val = fsum(individual.values())\n", |
99 | 92 | "\n", |
100 | 93 | " # Condition for termination.\n", |
101 | 94 | " solution_found = f_val == len(individual)\n", |
|
110 | 103 | }, |
111 | 104 | { |
112 | 105 | "cell_type": "markdown", |
113 | | - "id": "bdadd046", |
114 | 106 | "metadata": {}, |
115 | 107 | "source": [ |
116 | 108 | "Here we set the GA parameters, such as number of genes, number of chromosomes, etc." |
|
119 | 111 | { |
120 | 112 | "cell_type": "code", |
121 | 113 | "execution_count": 4, |
122 | | - "id": "091152b6", |
123 | 114 | "metadata": {}, |
124 | 115 | "outputs": [], |
125 | 116 | "source": [ |
|
153 | 144 | }, |
154 | 145 | { |
155 | 146 | "cell_type": "markdown", |
156 | | - "id": "d0712daf", |
157 | 147 | "metadata": {}, |
158 | 148 | "source": [ |
159 | 149 | "### Optimization process.\n", |
|
164 | 154 | { |
165 | 155 | "cell_type": "code", |
166 | 156 | "execution_count": 5, |
167 | | - "id": "b3502a18", |
168 | 157 | "metadata": {}, |
169 | 158 | "outputs": [ |
170 | 159 | { |
171 | 160 | "name": "stdout", |
172 | 161 | "output_type": "stream", |
173 | 162 | "text": [ |
174 | | - "Initial Avg. Fitness = 24.9700.\n", |
175 | | - "StandardGA finished in 33 iterations.\n", |
176 | | - "Final Avg. Fitness = 46.3100.\n", |
177 | | - "Elapsed time: 2.655 seconds.\n" |
| 163 | + "Initial Avg. Fitness = 25.5900\n", |
| 164 | + "StandardGA finished in 16 iterations.\n", |
| 165 | + "Final Avg. Fitness = 45.0500\n", |
| 166 | + "Elapsed time: 2.212 seconds.\n" |
178 | 167 | ] |
179 | 168 | } |
180 | 169 | ], |
|
185 | 174 | { |
186 | 175 | "cell_type": "code", |
187 | 176 | "execution_count": 6, |
188 | | - "id": "2523fc1f", |
189 | 177 | "metadata": {}, |
190 | 178 | "outputs": [ |
191 | 179 | { |
192 | 180 | "name": "stdout", |
193 | 181 | "output_type": "stream", |
194 | 182 | "text": [ |
195 | | - "Optimum Found: 50.00000\n", |
| 183 | + "Optimum Found: 50.000000\n", |
196 | 184 | "\n", |
197 | 185 | "x1 = 1.000000\n", |
198 | 186 | "x2 = 1.000000\n", |
|
255 | 243 | "optimal_fit, _ = fun_OneMax(optimal_solution)\n", |
256 | 244 | "\n", |
257 | 245 | "# Display the (final) optimal value.\n", |
258 | | - "print(f\"Optimum Found: {optimal_fit:.5f}\\n\")\n", |
| 246 | + "print(f\"Optimum Found: {optimal_fit:.6f}\\n\")\n", |
259 | 247 | "\n", |
260 | 248 | "# Display each gene value separately.\n", |
261 | | - "for i, xi in enumerate(optimal_solution.genome, start=1):\n", |
262 | | - " print(f\"x{i} = {xi.value:>10.6f}\")\n", |
| 249 | + "for i, xi in enumerate(optimal_solution.values(), start=1):\n", |
| 250 | + " print(f\"x{i} = {xi:>10.6f}\")\n", |
263 | 251 | "# _end_for_\n", |
264 | 252 | "\n", |
265 | 253 | "# True maximum: f(1.0, 1.0, ..., 1.0) = M" |
266 | 254 | ] |
267 | 255 | }, |
268 | 256 | { |
269 | 257 | "cell_type": "markdown", |
270 | | - "id": "e3193c7f", |
271 | 258 | "metadata": {}, |
272 | 259 | "source": [ |
273 | 260 | "### End of file" |
|
276 | 263 | { |
277 | 264 | "cell_type": "code", |
278 | 265 | "execution_count": null, |
279 | | - "id": "d7b1f97e", |
280 | 266 | "metadata": {}, |
281 | 267 | "outputs": [], |
282 | 268 | "source": [] |
283 | 269 | } |
284 | 270 | ], |
285 | 271 | "metadata": { |
286 | 272 | "kernelspec": { |
287 | | - "display_name": "venv_tf", |
| 273 | + "display_name": "SOMap", |
288 | 274 | "language": "python", |
289 | | - "name": "venv_tf" |
| 275 | + "name": "somap" |
290 | 276 | }, |
291 | 277 | "language_info": { |
292 | 278 | "codemirror_mode": { |
|
0 commit comments