Source code for Circuit.FullySimCircuit
1from Circuit.Circuit import Circuit
2from Logger import Logger
3
[docs]
4class FullySimCircuit(Circuit):
5 """
6 A concrete class, the fully simulated circuit that stores its own bitstream in memory
7 """
8
9 def __init__(self, index: int, filename: str, sine_funcs, rand, logger: Logger,
10 mutation_prob: float):
11 Circuit.__init__(self, index, filename, logger)
12
13 self.__src_sine_funcs = sine_funcs
14 self.__simulation_bitstream = [0] * 100
15 self._rand = rand
16 self.__mutation_prob = mutation_prob
17 self.randomize_bitstream()
18
[docs]
19 def mutate(self):
20 """
21 Mutate the simulation mode circuit
22 """
23 for i in range(0, len(self.__simulation_bitstream)):
24 if self.__mutation_prob >= self._rand.uniform(0,1):
25 # Mutate this bit
26 self.__simulation_bitstream[i] = 1 - self.__simulation_bitstream[i]
27
[docs]
28 def randomize_bitstream(self):
29 """
30 Fully randomize the simulation mode circuit
31 """
32 for i in range(0, len(self.__simulation_bitstream)):
33 self.__simulation_bitstream[i] = self._rand.integers(0, 2)
34
[docs]
35 def crossover(self, parent, crossover_point: int):
36 """
37 Simulated crossover, pulls first n bits from parent and remaining from self
38
39 Parameters
40 ----------
41 parent : Circuit
42 The other circuit the crossover is performed with
43 crossover_point : int
44 The index in the editable bitstream the crossover occours at
45 """
46 for i in range(0, crossover_point):
47 self.__simulation_bitstream[i] = parent.__simulation_bitstream[i]
48 # Remaining bits left unchanged
49
[docs]
50 def copy_from(self, other):
51 for i in range(0, len(other.__simulation_bitstream)):
52 self.__simulation_bitstream[i] = other.__simulation_bitstream[i]
53
54 def upload(self):
55 # Doesn't need to do anything, runs locally
56 pass
57
[docs]
58 def get_bitstream(self) -> list[int]:
59 return self.__simulation_bitstream
60
61 def inject_bitstream(self, bitstream: list[int]):
62 self.__simulation_bitstream = bitstream
63
64 def get_file_attribute(self, attribute: str) -> str | None:
65 return None