|
| 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()) |
0 commit comments