-
Notifications
You must be signed in to change notification settings - Fork 61
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
Showing
11 changed files
with
117 additions
and
37 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
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
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
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
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
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
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from random import choice, randint, random | ||
from string import lowercase | ||
from library.EvolutionaryAlgorithm import EvolutionaryAlgorithm | ||
|
||
|
||
class Algorithm(EvolutionaryAlgorithm): | ||
""" | ||
Tries to get a randomly-generated string to match string "clout" | ||
""" | ||
def _initial_population(self): | ||
return list(''.join([choice(lowercase) for _ in range(5)]) for _ in range(50)) | ||
|
||
def _fitness(self, member): | ||
return float(sum(member[i] == "clout"[i] for i in range(5))) | ||
|
||
def _crossover(self, parent1, parent2): | ||
partition = randint(0, len(self.population[0]) - 1) | ||
return parent1[0:partition] + parent2[partition:] | ||
|
||
def _mutate(self, member): | ||
if self.mutation_rate >= random(): | ||
member = list(member) | ||
member[randint(0,4)] = choice(lowercase) | ||
member = ''.join(member) | ||
return member | ||
|
||
|
||
def test_algorithm(): | ||
algorithm = Algorithm(.5, .7, 500, max_fitness=None) | ||
algorithm.run() |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from random import choice | ||
from library.GeneticAlgorithm import GeneticAlgorithm | ||
|
||
|
||
class Algorithm(GeneticAlgorithm): | ||
""" | ||
Tries to get a randomly-generated string to match 000111 | ||
""" | ||
def _initial_population(self): | ||
return list(list([choice([0, 1]) for _ in range(6)]) for _ in range(50)) | ||
|
||
def _fitness(self, member): | ||
return float(sum(member[i] == [0,0,0,1,1,1][i] for i in range(6))) | ||
|
||
|
||
def test_algorithm(): | ||
algorithm = Algorithm(.5, .7, 500, max_fitness=None) | ||
algorithm.run() |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from random import uniform | ||
from library.HarmonySearch import HarmonySearch | ||
|
||
|
||
class Algorithm(HarmonySearch): | ||
""" | ||
Tries to get a randomly-generated list to match [.1, .2, .3, .2, .1] | ||
""" | ||
def _random_harmony(self): | ||
return list([uniform(0, 1) for _ in range(5)]) | ||
|
||
def _score(self, member): | ||
return 1./ sum(abs(member[i] - [.1, .2, .3, .2, .1][i]) for i in range(5)) | ||
|
||
|
||
def test_algorithm(): | ||
algorithm = Algorithm(50, .5, .3, .01, 2000, max_score=None) | ||
algorithm.run() |