-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_randomized_code_with_light.py
165 lines (130 loc) · 5.79 KB
/
puzzle_randomized_code_with_light.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from ortools.linear_solver import pywraplp
import paho.mqtt.client as mqtt
import json
import time
import sys
import os
import random
mqttBroker = "earth.informatik.uni-freiburg.de"
main_topic = "ubilab/colorcode/"
loop = True
clearConsole = lambda: os.system('cls' if os.name in ('nt', 'dos') else 'clear')
def print_usage_and_exit():
print('usage:\t\t', sys.argv[0], '--config\tSEED_CONFIG_FILE')
print(' or \t\t', sys.argv[0], ' -c\tSEED_CONFIG_FILE')
exit(1)
## CREATING EASY PUZZLE:
# each button controls a different number of lights, one controls one, the next two, then 3 and so on ...
def create_random_buttons_configuration():
button_indices = random.sample(range(nodecount), nodecount)
lights_affected = random.sample(range(nodecount), nodecount)
for button_index in range(nodecount):
config['buttons'][button_indices[button_index]]['actions'] = \
[ { "change_light_with_index" : lights_affected[light_index] ,
"by_this_many_steps" : random.randrange(1,7)
} for light_index in range(button_index+1) ]
config['code'][button_index] = random.randrange(1,7)
def check_if_config_is_infeasible():
solver.Clear()
effects_on_lights = [[] for _ in range(num_lights)]
for button_num in range(num_buttons):
for action in config['buttons'][button_num]['actions']:
effects_on_lights[action['change_light_with_index']].append((button_num, action['by_this_many_steps']))
button_presses = [ solver.IntVar(0, infinity, 'button_presses[%i]' % button_num)
for button_num in range(num_buttons) ]
role_overs = [ solver.IntVar(0, infinity, 'role_overs[%i]' % light_num)
for light_num in range(num_lights) ]
for light_num in range(num_lights):
constraint_expr = [ button_effect * button_presses[button_num]
for (button_num, button_effect) in effects_on_lights[light_num] ]
constraint_expr.append( - len(config['lights'][light_num]['colors']) * role_overs[light_num] )
solver.Add( sum(constraint_expr) == config['code'][light_num] )
solver.Minimize(solver.Sum( button_presses ))
status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
return False
return True
### MAIN ###
if len(sys.argv) < 3 or \
sys.argv[1] != '-c' and sys.argv[1] != '--config':
print_usage_and_exit()
if not os.path.isfile(sys.argv[2]):
print(sys.argv[2], ' is not a file!\nprovide existing CONFIG_FILE')
print_usage_and_exit()
with open(sys.argv[2], 'r') as json_file:
data = json_file.read()
config = json.loads(data)
num_lights = len(config['lights'])
num_buttons = len(config['buttons'])
if num_lights != num_buttons:
print('Different number of lights and buttons currently not supported!')
exit()
clearConsole()
nodecount = num_lights
solver = pywraplp.Solver.CreateSolver('SCIP')
infinity = solver.infinity()
infeasible = True
while infeasible:
print('Creating and checking random configuration ...')
create_random_buttons_configuration()
infeasible = check_if_config_is_infeasible()
print('Feasible config found with this solution:')
print([int(button.solution_value()) for button in solver.variables()[:num_buttons]])
time.sleep(1)
clearConsole()
lights_cli = mqtt.Client("lights_publisher")
lights_cli.connect(mqttBroker, keepalive=3600)
for digit in config['code']:
lights_cli.publish(main_topic + 'code', digit, retain=False)
lights_cli.publish(main_topic + 'code', 'D', retain=False)
def button_init_state(index):
return config['buttons'][index]['initial_state'] == "active"
def init_button_client(index):
def on_message(client, userdata, message):
global loop
for action in config['buttons'][index]['actions']:
light_index = action['change_light_with_index']
light = config['lights'][light_index]
next_state = ( lights_state[light_index] + action['by_this_many_steps'] ) % len(light['colors'])
lights_state[light_index] = next_state
lights_cli.publish(main_topic + light['light_id'], light['colors'][next_state],retain=False)
clearConsole()
print("Target \t\t{}".format(config['code']))
print("Current State \t{}".format(lights_state))
if config['code'] == lights_state:
print("CONGRATS, You are out of here!!!")
time.sleep(.5)
for light in config['lights']:
lights_cli.publish(main_topic + light['light_id'], "C",retain=False)
lights_cli.publish(main_topic + 'code', "C" , retain=True)
loop = False
client = mqtt.Client("buttons_listener_" + str(index))
client.on_message = on_message
client.connect(mqttBroker)
client.subscribe(main_topic + config['buttons'][index]['button_id'])
client.loop_start()
return client
print("ARE YOU READY TO BE TRAPPED ???")
time.sleep(1)
print("Initializing Buttons....")
time.sleep(1)
buttons_active = [button_init_state(index) for index in range(num_buttons)]
buttons_cli = [init_button_client(index) for index in range(num_buttons)]
print("Buttons Initialized....")
time.sleep(1)
print("Initializing Lights....")
lights_state = [0] * num_lights
for light in config['lights']:
lights_cli.publish(main_topic + light['light_id'], light['colors'][0],retain=False)
time.sleep(1)
print("Lights Initialized ...")
time.sleep(1)
print("\nHERE WE GO")
time.sleep(1)
clearConsole()
print("Target \t\t{}".format(config['code']))
print("Current State \t{}".format(lights_state))
while loop:
time.sleep(.5)
for client in buttons_cli:
client.loop_stop()