-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8f9be5
commit 189f1c6
Showing
2 changed files
with
65 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,59 @@ | ||
from decimal import Decimal | ||
import random | ||
|
||
|
||
class Individual: | ||
def __init__(self, n, f=1.0): | ||
def __init__(self, n, f=1.0, mutationrate=0.0): | ||
self._id = n | ||
self._fitness = f | ||
self._genotype = [] | ||
self._mutationrate = mutationrate | ||
|
||
def get_id(self): | ||
return self._id | ||
|
||
def get_fitness(self): | ||
return self._fitness | ||
|
||
def get_genotype(self): | ||
return self._genotype | ||
|
||
def acquire_mutation(self): | ||
r = random.random() | ||
next_genotype = self._genotype | ||
if self._mutationrate > r: | ||
next_genotype.append(random.random()) | ||
self._genotype = next_genotype | ||
|
||
def __repr__(self): | ||
return str(self._id) | ||
return Decimal(self._fitness) | ||
return str(self._fitness) | ||
return str(self._genotype) | ||
|
||
|
||
def main(): | ||
ind = Individual(42) | ||
print(ind.get_id()) | ||
print(ind.get_fitness()) | ||
|
||
ind2 = Individual(30, 0.8, 1.0) | ||
print(ind2.get_id()) | ||
print(ind2.get_fitness()) | ||
ind2.acquire_mutation() | ||
print(ind2.get_genotype()) | ||
ind2.acquire_mutation() | ||
print(ind2.get_genotype()) | ||
print(ind2.get_id()) | ||
print(ind2.get_fitness()) | ||
|
||
ind3 = Individual(0, 1.0, 0.8) | ||
ind4 = Individual(1, 1.0, 0.8) | ||
pop = [ind3, ind4] | ||
print([x.get_genotype() for x in pop]) | ||
[x.acquire_mutation() for x in pop] | ||
print([x.get_genotype() for x in pop]) | ||
[x.acquire_mutation() for x in pop] | ||
print([x.get_genotype() for x in pop]) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters