-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneticAlgorithm.py
More file actions
executable file
·88 lines (73 loc) · 2.89 KB
/
geneticAlgorithm.py
File metadata and controls
executable file
·88 lines (73 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python3.8
from typing import List, Tuple
import random
import sys
VectorStr = List[float] # VectorStr is the population
goal = ""
# Check if the solution is in the population
def checkSolutionInVectorStr(arrayStr: VectorStr) -> bool:
pass
# Swap two characters in a string
def swapTwoRandomCharacterInString(str: str) -> str:
pass
# Change one character randomly
def changeRandomlyOneCharacter(str: str) -> str:
pass
# Create mutation with a probability of 10%
# A mutation is a random modification of the string
# It can be a swap of two characters, a change of one character or a random character, a revert string etc. per example
def mutationOfTenPercent(str: str) -> str:
pass
# dictionnary is a dictionnary with the string (word) as key and the fitness as value
# Return a tuple with both words with highest fitness
def getTwoStringWithHighestFitness(dictionnary: dict) -> Tuple[str, str]:
pass
# Get all possibilities of crossing over two strings
# Generate population with 'all possibilities' of crossing over two strings
# For example: if the two strings are "abc" and "def"
# The possibilities are: 'ade' or 'fab' or 'cde' or 'bdf' etc.
def getAllPossibilitiesOfCrossingOverTwoStrings(str1: str, str2: str) -> VectorStr:
pass
# Create a string with random characters with a length of len
def createStringWithRandomChars(len: int) -> str:
pass
# Initial population
# Generate a population with a size of arrLength
def initPopulation(arrLength: int) -> VectorStr:
pass
# Calc the score of the string compared to the goal
# The score is the number of characters that are the same per example
def fitnessFunction(str: str) -> int:
pass
# Calculate fitness for each string
# Generate a dictionnary with the string/word as key and the fitness as value
def createDictionnaryWithFitness(arrayStr: VectorStr) -> dict:
pass
# Main function
# Return the final population
def GeneticAlgorithm() -> VectorStr:
pass
# Initial population
# Compute fitness for each string
# Until the population has converged to the goal
# Select two strings with the highest fitness
# Create a new population with all possibilities of crossing over two strings
# Mutate the new population
# Compute fitness for each string
# Display final population
# Check if string has only alphabetic characters
def hasOnlyAlphabeticCharactersInLowerCase(str: str) -> bool:
return str.isalpha() and str.islower()
if __name__ == "__main__":
if (len(sys.argv) == 2):
if (hasOnlyAlphabeticCharactersInLowerCase(sys.argv[1])):
goal = sys.argv[1]
print("The goal : " + str(goal))
GeneticAlgorithm()
exit(0)
else:
print("Please enter a string composed only with characters in lower case.")
exit(84)
else:
print("Please enter the goal as the first argument")
exit(84)