Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Grégoire Grzeczkowicz
sho
Commits
1530f101
Commit
1530f101
authored
Nov 02, 2020
by
Grégoire Grzeczkowicz
Browse files
SoftMax for genetic Selection
parent
28784f40
Changes
1
Hide whitespace changes
Inline
Side-by-side
sho/algo.py
View file @
1530f101
...
...
@@ -68,15 +68,24 @@ def genetic(func, init, crossover, neighb, population_size, again):
best_val
=
P_val
[
np
.
argmax
(
P_val
)]
i
=
1
while
again
(
i
,
best_val
,
best_sol
):
# Crossover and mutation
new_sol
=
[]
for
j
in
range
(
5
*
population_size
):
e1
,
e2
=
np
.
random
.
randint
(
0
,
population_size
,
2
)
sol
=
neighb
(
crossover
(
P_sol
[
e1
],
P_sol
[
e2
]))
new_sol
.
append
(
sol
)
# Selection
new_val
=
[
func
(
sol
)
for
sol
in
new_sol
]
P_i
=
np
.
random
.
choice
(
range
(
len
(
new_sol
)),
size
=
population_size
,
replace
=
False
,
p
=
[
v
/
np
.
sum
(
new_val
)
for
v
in
new_val
])
# Probability folowing value order
order_val
=
np
.
argsort
(
new_val
)
proba
=
np
.
zeros
(
len
(
new_val
))
for
j
in
range
(
len
(
new_val
)):
proba
[
order_val
[
j
]]
=
j
proba
=
np
.
exp
(
proba
)
/
sum
(
np
.
exp
(
proba
))
P_i
=
np
.
random
.
choice
(
range
(
len
(
new_sol
)),
size
=
population_size
,
replace
=
False
,
p
=
proba
)
P_sol
=
[
new_sol
[
i
]
for
i
in
P_i
]
P_val
=
[
new_val
[
i
]
for
i
in
P_i
]
# Keeping best value
if
P_val
[
np
.
argmax
(
P_val
)]
>
best_val
:
best_sol
=
P_sol
[
np
.
argmax
(
P_val
)]
best_val
=
P_val
[
np
.
argmax
(
P_val
)]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment