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
92eddbce
Commit
92eddbce
authored
4 years ago
by
Sepehr Madani
Browse files
Options
Downloads
Patches
Plain Diff
Refactored some functions
parent
ea9bbae2
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
+16
-15
16 additions, 15 deletions
algorithms/genetic_algorithm.py
with
16 additions
and
15 deletions
algorithms/genetic_algorithm.py
+
16
−
15
View file @
92eddbce
...
...
@@ -34,16 +34,14 @@ class GeneticAlgorithm(BaseAlgorithm):
self
.
mutation_factor
=
options
.
mutation_factor
self
.
check_parameters
()
self
.
chromosomes
=
[
Chromosome
(
self
.
N
,
self
.
bit_count
)
for
i
in
range
(
self
.
sample_size
)
]
self
.
update_fitness
()
self
.
sort_fitness
()
def
check_parameters
(
self
):
super
().
check_parameters
()
def
solve
(
self
):
self
.
intialize_sample
()
self
.
update_fitness
()
self
.
sort_fitness
()
for
generation
in
range
(
self
.
gen_to_repeat
):
for
ii
in
range
(
self
.
sample_size
//
2
,
self
.
sample_size
-
1
,
2
):
p1
,
p2
=
random
.
sample
(
range
(
self
.
sample_size
//
2
),
2
)
...
...
@@ -53,9 +51,7 @@ class GeneticAlgorithm(BaseAlgorithm):
self
.
sort_fitness
()
# print(["{:.2f}".format(x.fitness) for x in self.chromosomes[:15]])
return
[
cmath
.
exp
(
1j
*
self
.
get_angle
(
bits
))
for
bits
in
self
.
chromosomes
[
0
].
gene
]
return
self
.
make_weights
(
self
.
chromosomes
[
0
])
def
mutate_sample
(
self
):
for
chromosome
in
self
.
chromosomes
[
1
:]:
# for all except the best chromosome
...
...
@@ -70,9 +66,7 @@ class GeneticAlgorithm(BaseAlgorithm):
for
x
in
compute_pattern
(
N
=
self
.
N
,
k
=
self
.
k
,
weights
=
[
cmath
.
exp
(
1j
*
self
.
get_angle
(
bits
))
for
bits
in
chromosome
.
gene
],
weights
=
self
.
make_weights
(
chromosome
),
degrees
=
self
.
null_degrees
,
)
]
...
...
@@ -81,10 +75,12 @@ class GeneticAlgorithm(BaseAlgorithm):
def
sort_fitness
(
self
):
self
.
chromosomes
.
sort
(
key
=
lambda
x
:
x
.
fitness
,
reverse
=
True
)
def
get_angle
(
self
,
bits
):
return
(
(
bits
-
(
2
**
self
.
bit_count
-
1
)
/
2
)
*
2
*
pi
/
(
2
**
self
.
bit_resolution
)
)
def
make_weights
(
self
,
chromosome
):
weights
=
[]
for
bits
in
chromosome
.
gene
:
angle
=
(
bits
-
(
2
**
self
.
bit_count
-
1
)
/
2
)
*
2
*
pi
/
(
2
**
self
.
bit_resolution
)
weights
.
append
(
cmath
.
exp
(
1j
*
angle
))
return
weights
def
crossover
(
self
,
p1
,
p2
,
c1
,
c2
):
self
.
chromosomes
[
c1
]
=
deepcopy
(
self
.
chromosomes
[
p1
])
...
...
@@ -95,3 +91,8 @@ class GeneticAlgorithm(BaseAlgorithm):
self
.
chromosomes
[
c1
].
gene
[
i
],
self
.
chromosomes
[
c2
].
gene
[
i
],
)
def
intialize_sample
(
self
):
self
.
chromosomes
=
[
Chromosome
(
self
.
N
,
self
.
bit_count
)
for
i
in
range
(
self
.
sample_size
)
]
\ No newline at end of file
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