Skip to content

Commit a7e81eb

Browse files
committed
Put random initialization back to main code
1 parent 977454e commit a7e81eb

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

virtual_modi/virtual_bundle.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import time
33
import threading as th
4+
from random import randint
45

56
from importlib.util import find_spec
67

@@ -63,10 +64,69 @@ def __init__(
6364

6465
vnetwork.attach_module('r', vbutton)
6566
vbutton.attach_module('b', vled)
66-
else:
67-
raise ValueError(
68-
'VirtualMODI currently supports default initialization only'
69-
)
67+
# User explicitly specified to request randomized mode
68+
elif isinstance(modules[0], str) and modules[0] == 'random':
69+
# Create random number of input module random number of times
70+
nb_times = randint(2, 10)
71+
for i in range(nb_times):
72+
if not (i % 2):
73+
random_module = input_modules[randint(
74+
0, len(input_modules) - 1
75+
)]
76+
else:
77+
random_module = output_modules[randint(
78+
0, len(output_modules) - 1
79+
)]
80+
random_module_instance = self.create_new_module(random_module)
81+
82+
# At random location, (1) Pick a module, (2) Pick a direction
83+
avms = self.attached_virtual_modules
84+
avms_l = len(avms)
85+
j = randint(0, avms_l - 1)
86+
rand_parent_module = avms[j]
87+
rand_parent_module.attach_module(None, random_module_instance)
88+
89+
# Create every module exactly once, total 12 modules including network
90+
elif isinstance(modules[0], str) and modules[0] == 'all':
91+
all_modules = input_modules + output_modules
92+
for module_name in all_modules:
93+
module_instance = self.create_new_module(module_name)
94+
avms = self.attached_virtual_modules
95+
avms_l = len(avms)
96+
j = randint(0, avms_l - 1)
97+
rand_parent_module = avms[j]
98+
rand_parent_module.attach_module(None, module_instance)
99+
100+
# Given modules only e.g. modules = ["button", "dial", "led"]
101+
elif isinstance(modules[0], str) and ',' not in modules[0]:
102+
103+
if len(modules) > 12:
104+
raise ValueError("Virtual mode supports up to 12 modules!!")
105+
106+
# Create instances of the specified modules
107+
for module_name in modules:
108+
self.create_new_module(module_name.lower())
109+
110+
# Unfortunately, the topology of the virtual modules are random
111+
for i, module in enumerate(self.attached_virtual_modules):
112+
# Initiate a module to be attached to the current module
113+
if i == (len(self.attached_virtual_modules) - 1):
114+
break
115+
next_module = self.attached_virtual_modules[i + 1]
116+
117+
# Attach if current module has nothing on its random direction
118+
module.attach_module(None, next_module)
119+
120+
# Given both module and directions e.g. modules=["button, r", "led, t"]
121+
elif isinstance(modules[0], str) and ',' in modules[0]:
122+
if len(modules) > 12:
123+
raise ValueError("Virtual mode supports up to 12 modules!!")
124+
prev_module = vnetwork
125+
for s in modules:
126+
direction, module = s.replace(' ', '').split(',')
127+
module_instance = self.create_new_module(module)
128+
prev_module.attach_module(direction, module_instance)
129+
prev_module = module_instance
70130

71131
def open(self):
72132
self.conn.open()

0 commit comments

Comments
 (0)