|
| 1 | +from pythonosc import udp_client |
| 2 | +from pythonosc import dispatcher |
| 3 | +from pythonosc import osc_server |
| 4 | +import numpy as np |
| 5 | +import threading |
| 6 | + |
| 7 | +class OSCClass: |
| 8 | + def __init__(self, STATE_SIZE, ACTION_SIZE , TRANSITION_TIME, ip, port, TRAINING_LABEL): |
| 9 | + |
| 10 | + self.received = False |
| 11 | + self.resample_states = False |
| 12 | + self.resetstate = False |
| 13 | + self.new_speed = False |
| 14 | + self.super_like = False |
| 15 | + self.rnd_action = False |
| 16 | + self.previous = False |
| 17 | + self.next = False |
| 18 | + self.VSTsample_bool = False |
| 19 | + self.get_trajlist = False |
| 20 | + |
| 21 | + self.running = True |
| 22 | + self.paused = True |
| 23 | + self.save = False |
| 24 | + self.load = False |
| 25 | + self.resetmodel = False |
| 26 | + |
| 27 | + self.reward = 0 |
| 28 | + self.resample_factor = 0 |
| 29 | + self.superlike_value = 0 |
| 30 | + self.increment_reward_length = 0 |
| 31 | + self.state_idx = 0 |
| 32 | + self.row1_idx = 0 |
| 33 | + self.col1_idx = 0 |
| 34 | + self.row2_idx = 0 |
| 35 | + self.col2_sl_idx = 0 |
| 36 | + self.col2_sdl_idx = 0 |
| 37 | + self.col2_es_idx = 0 |
| 38 | + self.idx = 1 |
| 39 | + self.VSTstate = 0 |
| 40 | + self.save_modelname = None |
| 41 | + |
| 42 | + |
| 43 | + self.client = udp_client.SimpleUDPClient('localhost', 8000) |
| 44 | + self.initialise_client(STATE_SIZE, ACTION_SIZE , TRANSITION_TIME, 1) |
| 45 | + |
| 46 | + self.dispatch = dispatcher.Dispatcher() |
| 47 | + |
| 48 | + # Main controls |
| 49 | + self.dispatch.map("/reward", self.store_reward) |
| 50 | + |
| 51 | + # Workflow controls |
| 52 | + self.dispatch.map("/stop_program", self.stop_program) |
| 53 | + self.dispatch.map("/pause", self.pause_training) |
| 54 | + self.dispatch.map("/previous_state", self.previous_state) |
| 55 | + self.dispatch.map("/next_state", self.next_state) |
| 56 | + self.dispatch.map("/sample_vst", self.sample_vststate) |
| 57 | + self.dispatch.map("/save_model", self.save_model) |
| 58 | + self.dispatch.map("/load_model", self.load_model) |
| 59 | + self.dispatch.map("/reset_model", self.reset_model) |
| 60 | + |
| 61 | + # Agent controls |
| 62 | + self.dispatch.map("/resample", self.adjust_sampling) |
| 63 | + self.dispatch.map("/super_like", self.record_superlike) |
| 64 | + self.dispatch.map("/explore_state", self.reset_state) |
| 65 | + self.dispatch.map("/explore_action", self.random_action) |
| 66 | + self.dispatch.map("/new_speed", self.rescale_reward_length) |
| 67 | + |
| 68 | + self.server = osc_server.ThreadingOSCUDPServer((ip, port), self.dispatch) |
| 69 | + self.server_thread = threading.Thread(target=self.server.serve_forever) |
| 70 | + self.server_thread.start() |
| 71 | + |
| 72 | + # ------------------ CLIENT ------------------------------ |
| 73 | + def initialise_client(self, STATE_SIZE, ACTION_SIZE , TRANSITION_TIME, paused_bool): |
| 74 | + self.send_state(np.ones(STATE_SIZE)*0.5) |
| 75 | + self.send_agent_control(time = TRANSITION_TIME * 1000) |
| 76 | + self.send_agent_control(precision = 1.0/ACTION_SIZE) |
| 77 | + self.send_workflow_control(training = 0) |
| 78 | + self.send_workflow_control(paused = paused_bool) |
| 79 | + self.state_idx = 0 |
| 80 | + self.row1_idx = 0 |
| 81 | + self.col1_idx = 0 |
| 82 | + self.row2_idx = 0 |
| 83 | + self.col2_sl_idx = 0 |
| 84 | + self.col2_sdl_idx = 0 |
| 85 | + self.col2_es_idx = 0 |
| 86 | + |
| 87 | + def send_state(self, state): |
| 88 | + self.client.send_message('/params', state) |
| 89 | + |
| 90 | + def send_state_to_slider(self, state, label): |
| 91 | + state = str(state[0])[1:-1] |
| 92 | + |
| 93 | + self.col1_idx = self.state_idx % 278 |
| 94 | + content_slider1 = 'set ' + str(self.col1_idx) + ' ' + str(self.row1_idx) + ' ' + str(state) |
| 95 | + |
| 96 | + if label == 'Superlike': |
| 97 | + self.row2_idx = 0 |
| 98 | + format_slider1 = 'cell ' + str(self.col1_idx) + ' ' + str(self.row1_idx) + ' brgb 16 127 64' |
| 99 | + content_slider2 = 'set ' + str(self.col2_sl_idx) + ' 0 ' + state |
| 100 | + format_slider2 = 'cell ' + str(self.col2_sl_idx) + ' 0 brgb 16 127 64' |
| 101 | + self.col2_sl_idx += 1 |
| 102 | + |
| 103 | + elif label == 'Superdislike': |
| 104 | + self.row2_idx = 1 |
| 105 | + format_slider1 = 'cell ' + str(self.col1_idx) + ' ' + str(self.row1_idx) + ' brgb 127 0 2' |
| 106 | + content_slider2 = 'set ' + str(self.col2_sdl_idx) + ' 1 ' + state |
| 107 | + format_slider2 = 'cell ' + str(self.col2_sdl_idx) + ' 1 brgb 127 0 2' |
| 108 | + self.col2_sdl_idx += 1 |
| 109 | + |
| 110 | + elif label == 'Explore_state': |
| 111 | + self.row2_idx = 2 |
| 112 | + format_slider1 = 'cell ' + str(self.col1_idx) + ' ' + str(self.row1_idx) + ' brgb 253 204 101' |
| 113 | + content_slider2 = 'set ' + str(self.col2_es_idx) + ' 2 ' + state |
| 114 | + format_slider2 = 'cell ' + str(self.col2_es_idx) + ' 2 brgb 253 204 101' |
| 115 | + self.col2_es_idx += 1 |
| 116 | + |
| 117 | + elif label == 1: |
| 118 | + format_slider1 = 'cell ' + str(self.col1_idx) + ' ' + str(self.row1_idx) + ' brgb 150 255 151' |
| 119 | + content_slider2 = '' |
| 120 | + format_slider2 = '' |
| 121 | + |
| 122 | + elif label == -1: |
| 123 | + format_slider1 = 'cell ' + str(self.col1_idx) + ' ' + str(self.row1_idx) + ' brgb 252 121 132' |
| 124 | + content_slider2 = '' |
| 125 | + format_slider2 = '' |
| 126 | + |
| 127 | + else: |
| 128 | + format_slider1 = 'cell ' + str(self.col1_idx) + ' ' + str(self.row1_idx) + ' brgb 33 35 34' |
| 129 | + content_slider2 = '' |
| 130 | + format_slider2 = '' |
| 131 | + |
| 132 | + self.state_idx += 1 |
| 133 | + if self.state_idx % 278 == 0: |
| 134 | + self.row1_idx += 1 |
| 135 | + self.client.send_message('/toslider1', 'rows ' + str(self.row1_idx+1)) |
| 136 | + self.col1_idx += 1 |
| 137 | + |
| 138 | + self.client.send_message('/toslider1', content_slider1) |
| 139 | + self.client.send_message('/toslider1', format_slider1) |
| 140 | + if not content_slider2 == '': |
| 141 | + self.client.send_message('/toslider2', content_slider2) |
| 142 | + self.client.send_message('/toslider2', format_slider2) |
| 143 | + |
| 144 | + def send_agent_control(self, **kwargs): |
| 145 | + for key, value in kwargs.items(): |
| 146 | + if key == 'reward_in': |
| 147 | + self.client.send_message('/reward_in', value) |
| 148 | + elif key == 'time': |
| 149 | + self.client.send_message('/time', value) |
| 150 | + elif key == 'precision': |
| 151 | + self.client.send_message('/precision', value) |
| 152 | + elif key == 'superlike': |
| 153 | + self.client.send_message('/superlike', self.superlike_value) |
| 154 | + elif key == 'explore_state': |
| 155 | + self.client.send_message('/explore_state', value) |
| 156 | + elif key == 'explore_action': |
| 157 | + self.client.send_message('/explore_action', value) |
| 158 | + elif key == 'previous_s': |
| 159 | + self.client.send_message('/previous_s', value) |
| 160 | + elif key == 'next_s': |
| 161 | + self.client.send_message('/next_s', value) |
| 162 | + |
| 163 | + def send_workflow_control(self, **kwargs): |
| 164 | + for key, value in kwargs.items(): |
| 165 | + if key == 'init': |
| 166 | + self.client.send_message('/init', value) |
| 167 | + elif key == 'paused': |
| 168 | + value = (value + 1) % 2 |
| 169 | + self.client.send_message('/paused', value) |
| 170 | + elif key == 'rand': |
| 171 | + self.client.send_message('/rand', value) |
| 172 | + |
| 173 | + # ------------------ SERVER ------------------------------ |
| 174 | + # ------------------ Basic reward control ------------------------------ |
| 175 | + def store_reward(self,unused_addr, reward): |
| 176 | + self.reward = reward |
| 177 | + self.received = True |
| 178 | + |
| 179 | + # ------------------ Workflow controls ------------------------------ |
| 180 | + def pause_training(self, unused_addr, pause_bool): |
| 181 | + self.paused = pause_bool |
| 182 | + |
| 183 | + def save_model(self, unused_addr, *args): |
| 184 | + self.save_modelname = args[1] |
| 185 | + self.save = True |
| 186 | + |
| 187 | + def load_model(self, unused_addr, model_name): |
| 188 | + self.load_modelname = model_name |
| 189 | + self.load = True |
| 190 | + |
| 191 | + def reset_model(self, unused_addr, reset_bool): |
| 192 | + self.resetmodel = True |
| 193 | + |
| 194 | + def stop_program(self, unused_addr, running_bool): |
| 195 | + self.running = running_bool |
| 196 | + |
| 197 | + def end_thread(self): |
| 198 | + self.server.shutdown() |
| 199 | + |
| 200 | + # ------------------ Agent controls ------------------------------ |
| 201 | + def adjust_sampling(self,unused_addr, resample): |
| 202 | + self.resample_factor = pow(2,resample) |
| 203 | + self.resample_states = True |
| 204 | + |
| 205 | + def record_superlike(self,unused_addr, superlike_flag): |
| 206 | + self.superlike_value = superlike_flag |
| 207 | + self.super_like = True |
| 208 | + |
| 209 | + def reset_state(self,unused_addr, reset): |
| 210 | + self.resetstate = True |
| 211 | + |
| 212 | + def random_action(self,unused_addr, action): |
| 213 | + self.rnd_action = True |
| 214 | + |
| 215 | + def rescale_reward_length(self, unused_addr, new_reward_length): |
| 216 | + self.increment_reward_length = new_reward_length * -2 |
| 217 | + self.new_speed = True |
| 218 | + |
| 219 | + # ------------------ Workflow controls (paused) -------------------------- |
| 220 | + def previous_state(self, unused_addr, previous_bool): |
| 221 | + self.previous = True |
| 222 | + |
| 223 | + def next_state(self, unused_addr, next_bool): |
| 224 | + self.next = True |
| 225 | + |
| 226 | + def sample_vststate(self, unused_addr, *args): |
| 227 | + VSTsample = np.array(args) |
| 228 | + self.VSTstate = VSTsample |
| 229 | + self.VSTsample_bool = True |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | + |
0 commit comments