Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
genetic-algorithms
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Lennart Eichhorn
genetic-algorithms
Commits
f4a0ba5a
Commit
f4a0ba5a
authored
5 years ago
by
Lennart Eichhorn
Browse files
Options
Downloads
Patches
Plain Diff
Refactored the FitnessProportional selector.
parent
c6fcb4a7
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/MainClasses/GeneticAlgorithm.java
+1
-1
1 addition, 1 deletion
src/main/java/MainClasses/GeneticAlgorithm.java
src/main/java/Selectors/FitnessProportional.java
+18
-15
18 additions, 15 deletions
src/main/java/Selectors/FitnessProportional.java
with
19 additions
and
16 deletions
src/main/java/MainClasses/GeneticAlgorithm.java
+
1
−
1
View file @
f4a0ba5a
...
@@ -122,7 +122,7 @@ public class GeneticAlgorithm {
...
@@ -122,7 +122,7 @@ public class GeneticAlgorithm {
}
}
if
(
config
.
getSelectionMethod
().
equals
(
SelectionMethods
.
Proportional
))
{
if
(
config
.
getSelectionMethod
().
equals
(
SelectionMethods
.
Proportional
))
{
this
.
selector
=
new
FitnessProportional
(
this
.
rand
,
this
.
isHydrophobic
);
this
.
selector
=
new
FitnessProportional
(
this
.
config
,
this
.
rand
);
}
else
if
(
config
.
getSelectionMethod
().
equals
(
SelectionMethods
.
Tournament
))
{
}
else
if
(
config
.
getSelectionMethod
().
equals
(
SelectionMethods
.
Tournament
))
{
this
.
selector
=
new
Tournament
(
this
.
rand
,
this
.
isHydrophobic
,
config
.
getK
());
this
.
selector
=
new
Tournament
(
this
.
rand
,
this
.
isHydrophobic
,
config
.
getK
());
}
else
if
(
config
.
getSelectionMethod
().
equals
(
SelectionMethods
.
OnlyBest
))
{
}
else
if
(
config
.
getSelectionMethod
().
equals
(
SelectionMethods
.
OnlyBest
))
{
...
...
This diff is collapsed.
Click to expand it.
src/main/java/Selectors/FitnessProportional.java
+
18
−
15
View file @
f4a0ba5a
...
@@ -3,37 +3,40 @@ package Selectors;
...
@@ -3,37 +3,40 @@ package Selectors;
import
Interfaces.Selector
;
import
Interfaces.Selector
;
import
MainClasses.Candidate
;
import
MainClasses.Candidate
;
import
MainClasses.Config
;
import
java.util.Random
;
import
java.util.Random
;
public
class
FitnessProportional
implements
Selector
{
public
class
FitnessProportional
implements
Selector
{
Random
rand
;
Random
rand
;
int
[]
isHydrophobic
;
Config
config
;
public
FitnessProportional
(
Random
rand
,
int
[]
isHydrophobic
)
{
public
FitnessProportional
(
Config
config
,
Random
rand
)
{
this
.
rand
=
rand
;
this
.
rand
=
rand
;
this
.
isHydrophobic
=
isHydrophobic
;
this
.
config
=
config
;
}
}
@Override
@Override
public
Candidate
[]
selectNewPopulation
(
Candidate
[]
population
,
double
[]
fitness
,
double
totalFitness
)
{
public
Candidate
[]
selectNewPopulation
(
Candidate
[]
generation
)
{
int
populationSize
=
popul
ation
.
length
;
int
populationSize
=
gener
ation
.
length
;
// Pick set for next generation
double
totalFitness
=
0.0
;
double
[]
proportionalFitness
=
new
double
[
populationSize
];
for
(
Candidate
candidate:
generation
)
{
for
(
int
i
=
0
;
i
<
populationSize
;
i
++)
{
totalFitness
+=
candidate
.
getFitness
();
proportionalFitness
[
i
]
=
fitness
[
i
]
/
totalFitness
;
}
}
Candidate
[]
newPopulation
=
new
Candidate
[
populationSize
];
Candidate
[]
newPopulation
=
new
Candidate
[
populationSize
];
for
(
int
i
=
0
;
i
<
populationSize
;
i
++)
{
for
(
int
i
=
0
;
i
<
populationSize
;
i
++)
{
double
picked
=
rand
.
nextDouble
();
//Select a number between 0 and the maximum fitness
int
j
=
-
1
;
double
requiredFitness
=
rand
.
nextDouble
()
*
totalFitness
;
while
(
picked
>
0
)
{
double
currentFitness
=
0.0
;
j
++;
for
(
Candidate
candidate
:
generation
)
{
picked
-=
proportionalFitness
[
j
];
currentFitness
+=
candidate
.
getFitness
();
if
(
currentFitness
>=
requiredFitness
)
{
newPopulation
[
i
]
=
candidate
;
break
;
}
}
}
newPopulation
[
i
]
=
new
Candidate
(
population
[
j
].
getFolding
());
}
}
return
newPopulation
;
return
newPopulation
;
...
...
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