Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nulling-python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
smadani2
nulling-python
Commits
9c8e1be3
Commit
9c8e1be3
authored
4 years ago
by
Sepehr Madani
Browse files
Options
Downloads
Patches
Plain Diff
Fix syntax errors
parent
8e007456
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
algorithms/genetic_algorithm.py
+18
-17
18 additions, 17 deletions
algorithms/genetic_algorithm.py
with
18 additions
and
17 deletions
algorithms/genetic_algorithm.py
+
18
−
17
View file @
9c8e1be3
...
...
@@ -44,16 +44,16 @@ class GeneticAlgorithm(BaseAlgorithm):
self
.
bit_resolution
=
options
.
bit_resolution
self
.
mutation_factor
=
options
.
mutation_factor
self
.
overwrite_mutations
=
options
.
overwrite_mutations
self
.
stop_criterion
=
options
.
stop_criterion
# time, target, iter
self
.
gen_to_repeat
=
options
.
gen_to_repeat
self
.
time_limit
=
options
.
time_limit
self
.
stop_after_score
=
options
.
stop_after_score
self
.
generations
=
0
self
.
chromosomes
=
[]
self
.
buckets
=
None
if
options
.
use_buckets
:
self
.
bucket_count
=
options
.
bucket_count
...
...
@@ -67,15 +67,15 @@ class GeneticAlgorithm(BaseAlgorithm):
if
self
.
buckets
is
not
None
:
assert
len
(
self
.
null_degrees
)
==
1
assert
self
.
bucket_count
&
1
==
0
assert
self
.
stop_criterion
is
in
[
"
time
"
,
"
target
"
,
"
iter
"
]
assert
self
.
stop_criterion
in
[
"
time
"
,
"
target
"
,
"
iter
"
]
def
solve
(
self
):
self
.
initialize_sample
()
self
.
organize_sample
()
solve_function
=
getattr
(
self
,
"
solve_
"
+
self
.
stop_criterion
)
solve_function
()
return
(
self
.
make_weights
(
self
.
chromosomes
[
0
]),
self
.
chromosomes
[
0
].
get_score
(),
generations
)
return
(
self
.
make_weights
(
self
.
chromosomes
[
0
]),
self
.
chromosomes
[
0
].
get_score
(),
self
.
generations
)
def
solve_time
(
self
):
start_time
=
time_ns
()
while
(
time_ns
()
-
start_time
)
//
10
**
6
<=
self
.
time_limit
:
...
...
@@ -83,13 +83,13 @@ class GeneticAlgorithm(BaseAlgorithm):
def
solve_target
(
self
):
start_time
=
time_ns
()
while
(
time_ns
()
-
start_time
)
//
10
**
6
<=
10
*
1000
while
(
time_ns
()
-
start_time
)
//
10
**
6
<=
10
*
1000
:
self
.
step
()
def
solve_iter
(
self
):
for
generation
in
range
(
self
.
gen_to_repeat
):
self
.
step
()
def
step
(
self
):
self
.
create_children
()
self
.
mutate_sample
()
...
...
@@ -98,7 +98,7 @@ class GeneticAlgorithm(BaseAlgorithm):
def
create_children
(
self
):
"""
Using the better half of the population, creates children overwriting the bottom half by doing crossovers.
If use_buckets is True, uses AM-GM–based crossover. Otherwise, it uses the basic merger crossover.
"""
for
child
in
range
(
self
.
sample_size
//
2
,
self
.
sample_size
-
1
,
2
):
if
self
.
buckets
is
None
:
p1
,
p2
=
sample
(
range
(
self
.
sample_size
//
2
),
2
)
...
...
@@ -115,13 +115,13 @@ class GeneticAlgorithm(BaseAlgorithm):
key
=
lambda
x
:
abs
(
x
.
pattern
+
p1
.
pattern
)
)
if
len
(
self
.
buckets
[
bucket_opp
])
>
0
else
choice
(
self
.
chromosomes
)
self
.
crossover_bucket
(
p1
,
p2
,
child
,
child
+
1
)
self
.
generations
+=
1
def
organize_sample
(
self
):
"""
Reorganizes the sample by updating pattern for all chromosomes and sorting them by their scores.
Optionally, if use_buckets is True, allocates each chromosome to its respective bucket.
"""
# Update pattern
for
chromosome
in
self
.
chromosomes
:
if
chromosome
.
needs_update
:
...
...
@@ -149,7 +149,7 @@ class GeneticAlgorithm(BaseAlgorithm):
def
mutate_sample
(
self
):
"""
Mutates the sample excluding the best chromosome.
Overwrites the previous chromosomes if overwrite_mutations is True.
"""
if
self
.
overwrite_mutations
:
# For all except the best chromosome
for
chromosome
in
self
.
chromosomes
[
1
:]:
...
...
@@ -171,16 +171,16 @@ class GeneticAlgorithm(BaseAlgorithm):
def
make_weights
(
self
,
chromosome
):
"""
Returns e^{iθ} value for a chromosome
'
s θs
"""
weights
=
[]
for
bits
in
chromosome
.
gene
:
angle
=
(
bits
-
(
2
**
self
.
bit_count
-
1
)
/
2
)
*
2
*
pi
/
(
2
**
self
.
bit_resolution
)
weights
.
append
(
exp
(
1j
*
angle
))
weights
.
append
(
complex
(
cos
(
angle
),
sin
(
angle
))
)
return
weights
def
crossover
(
self
,
p1
,
p2
,
c1
,
c2
):
"""
Merges two parents
'
genes to create two children
"""
self
.
chromosomes
[
c1
]
=
deepcopy
(
self
.
chromosomes
[
p1
])
self
.
chromosomes
[
c2
]
=
deepcopy
(
self
.
chromosomes
[
p2
])
for
i
in
range
(
self
.
N
):
...
...
@@ -192,7 +192,7 @@ class GeneticAlgorithm(BaseAlgorithm):
def
crossover_bucket
(
self
,
p1
,
p2
,
c1
,
c2
):
"""
Creates two children from parents
'
genes using the AM-GM approximation
"""
for
ii
in
range
(
self
.
N
):
g1
=
p1
.
gene
[
ii
]
g2
=
p2
.
gene
[
ii
]
...
...
@@ -201,7 +201,8 @@ class GeneticAlgorithm(BaseAlgorithm):
def
initialize_sample
(
self
):
"""
Destroys all chromosomes and creates a new random population
"""
self
.
generations
=
0
self
.
chromosomes
.
clear
()
if
self
.
overwrite_mutations
:
self
.
chromosomes
=
[
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment