Skip to content

Commit 1a0d008

Browse files
committed
Initial commit
0 parents  commit 1a0d008

File tree

16 files changed

+395
-0
lines changed

16 files changed

+395
-0
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal",
13+
"justMyCode": true
14+
}
15+
]
16+
}

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TODO
2+
----
3+
- Implement cross-over, mutation and elitism
4+
- Implement environment
5+
- Implement objective function class
6+
- Implement fitness checker

mendel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '0.1.0'
157 Bytes
Binary file not shown.
3.49 KB
Binary file not shown.

mendel/app.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""Chromosome class."""
2+
3+
import struct
4+
5+
from typing import Union, List
6+
from config.config import Config
7+
8+
9+
class Gene:
10+
"""
11+
Gene class.
12+
13+
Genes represent a variable in the
14+
objective function.
15+
"""
16+
def __init__(self, var: Union[float, int]):
17+
"""__init__ Initialise Gene class.
18+
19+
Parameters
20+
----------
21+
var : Union[float, int]
22+
Float or int values representing the phenotype.
23+
"""
24+
self.var: float = float(var)
25+
self.hex_string: str = self.__hex_convert()
26+
self.bin_string: str = self.__bin_convert()
27+
self.length: int = len(self.bin_string)
28+
29+
def __hex_convert(self) -> str:
30+
"""Convert float to hex."""
31+
return float.hex(self.var)
32+
33+
def __bin_convert(self):
34+
"""
35+
Convert float to binary.
36+
Source: https://stackoverflow.com/questions/53538504/float-to-binary-and-binary-to-float-in-python
37+
"""
38+
return format(struct.unpack('!I', struct.pack('!f', self.var))[0], f'0{Config.BIT_LENGTH}b')
39+
40+
def bin_to_float(self):
41+
"""
42+
Convert binary to float.
43+
Source: https://stackoverflow.com/questions/53538504/float-to-binary-and-binary-to-float-in-python
44+
"""
45+
return struct.unpack('!f', struct.pack('!I', int(self.bin_string, 2)))[0]
46+
47+
48+
class Chromosome:
49+
"""
50+
Chromosome class.
51+
52+
Chromosomes represents a collection of
53+
variables encoded as genes.
54+
"""
55+
def __init__(self) -> None:
56+
self.genes: List[Gene] = []
57+
self.chromosome_string: str = ''
58+
self.length: int = len(self.chromosome_string)
59+
60+
def add_gene(self, gene: Gene) -> None:
61+
self.genes.append(gene)
62+
63+
def generate_chromosome_string(self) -> None:
64+
for gene in self.genes:
65+
self.chromosome_string += gene.bin_string
66+
self.__determine_chromosome_length()
67+
68+
def __determine_chromosome_length(self) -> None:
69+
self.length = len(self.chromosome_string)
70+
71+
72+
class Environment:
73+
"""
74+
Environment class.
75+
76+
Chromosomes interact with each other
77+
in Environment class.
78+
"""
79+
def __init__(self) -> None:
80+
self.population: List[Chromosome] = []
81+
self.max_population: int = 100
82+
self.mutation: float = 0.1
83+
self.elitism: bool = True
84+
self.elite_preservation: float = 0.1
85+
86+
87+
88+
if __name__ == '__main__':
89+
x_2 = Gene(1)
90+
print(x_2.bin_string)
91+
print(x_2.bin_to_float())
92+
x_2 = Gene(-1)
93+
print(x_2.bin_string)
94+
print(x_2.bin_to_float())

mendel/config/__init__.py

Whitespace-only changes.
143 Bytes
Binary file not shown.
378 Bytes
Binary file not shown.

mendel/config/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Config:
2+
BIT_LENGTH: int = 16
3+
HEX_LENGTH: int = 16

0 commit comments

Comments
 (0)