def on_enter(self): #self.ids["textbox"].font_name=utf8_font_path self.ids["textbox"].focus = True self.ids["textbox"].text_validate_unfocus = False self.ids["textbox"].bind(on_text_validate=self.send_message) self.ids["textbox"].bind(on_key_pressed=self.key_pressed_event) # remove existing widgets when the screen is entered # note : could be improved later self.ids["msg_view"].clear() # indicates if the user is typing or not self.isTyping = False # current typing status of all clients self.current_typing_msg = "" self.typing_widget = None self.top_date = None self.bottom_date = None # current content self.content = [] self.network_interface = NetworkInterface(client=self) self.network_interface.send(self.chat_address, "ENTER", "")
def on_enter(self): self.target_game = TurnBasedGameList.get_game_from_name( self.target_address) # create the grid of buttons self.create_grid() # update the button display before starting the game # needed for some games when the start position is not empty self.update_display() self.ids["state_label"].text = "Waiting opponent" # initial user name and set clock self.user_name = "" self.opp_user_name = "" self.init_user_frame() self.set_timer() # game state self.play_turn = False self.player_id = 0 self.network_interface = NetworkInterface(client=self) self.network_interface.send(self.target_address, "ENTER", "") # prepare timers self.cb = Clock.create_trigger(self.on_countdown, 1, interval=True) self.cb.cancel() self.opp_cb = Clock.create_trigger(self.on_opp_countdown, 1, interval=True) self.opp_cb.cancel()
def on_enter(self): #self.ids["textbox"].font_name=utf8_font_path self.ids["textbox"].text_validate_unfocus = False self.ids["screen_manager"].transition = NoTransition() self.current_content = "" self.network_interface = NetworkInterface(client = self) self.read_address(self.target_address)
def measure_performance(): network_interface = NetworkInterface( network=network, loss_class=torch.nn.MSELoss, batch_size=batch_size ) train_set_performance = network_interface.performance_on_set(train_set) dev_set_performance = network_interface.performance_on_set(dev_set) return train_set_performance, dev_set_performance
def train_one_epoch(): network_interface = NetworkInterface( network=network, loss_class=torch.nn.MSELoss, batch_size=batch_size ) epoch_loss = network_interface.train_one_epoch( train_set, shuffle=shuffle, learning_rate=learning_rate ) return epoch_loss
def create_new_node(self): """ Creates a unique disjointed node and adds to the network Returns ------- node: Node The newly created node """ node = Node(len(self.nodes), NetworkInterface(self)) self.nodes.append(node) return node
def __init__(self, node, oldNode, files_dir): self.node = node self.finger_table = [ Finger(client_node_id=self.node.id, row_idx=i) for i in range(m) ] self.successor = None self.predecessor = None self.networkInferface = NetworkInterface(node=node, chord=self) self.files_dir = files_dir self.join(n_=oldNode) # Start stabilization thread threading.Thread(target=self._stabilize_thread, args=()).start() threading.Thread(target=self._fix_fingers_thread, args=()).start() self.user_input() return
class TurnBasedGameClient(Screen): # kivy string property indicating the target network address target_address = StringProperty() # allotted time allotted_time = NumericProperty(0) opp_allotted_time = NumericProperty(0) # called by Kivy when the screen is entered (displayed) def on_enter(self): self.target_game = TurnBasedGameList.get_game_from_name( self.target_address) # create the grid of buttons self.create_grid() # update the button display before starting the game # needed for some games when the start position is not empty self.update_display() self.ids["state_label"].text = "Waiting opponent" # initial user name and set clock self.user_name = "" self.opp_user_name = "" self.init_user_frame() self.set_timer() # game state self.play_turn = False self.player_id = 0 self.network_interface = NetworkInterface(client=self) self.network_interface.send(self.target_address, "ENTER", "") # prepare timers self.cb = Clock.create_trigger(self.on_countdown, 1, interval=True) self.cb.cancel() self.opp_cb = Clock.create_trigger(self.on_opp_countdown, 1, interval=True) self.opp_cb.cancel() def on_leave(self): # cancel the Clock ticks self.cb.cancel() self.opp_cb.cancel() def create_grid(self): # remove all existing buttons (current client on_enter() can be called multiple times) self.ids["board_grid"].clear_widgets() # initialize the grid rows and cols rows = self.target_game.rows() cols = self.target_game.cols() cell_width, cell_height = self.target_game.cell_size() self.ids["board_grid"].rows = rows self.ids["board_grid"].cols = cols # list of buttons used to access buttons from an index self.button_list = [] for i in range(rows): for j in range(cols): # buttons are identified with coords value button = Button(text='', width=cell_width, height=cell_height, size_hint=(None, None)) button.coords = (i, j) button.bind(on_press=self.cell_clicked) self.ids["board_grid"].add_widget(button) self.button_list.append(button) def set_timer(self): # setting timer(for timeout) if 'allotted_time' not in dir(self.target_game): # use 1 min by default self.is_allotted_time = True limit_time = 1 self.allotted_time = limit_time * 60.0 self.opp_allotted_time = limit_time * 60.0 else: limit_time = self.target_game.allotted_time() if limit_time == -1: self.is_allotted_time = False pass else: self.is_allotted_time = True # Note: the unit of allotted time is minute. self.allotted_time = limit_time * 60.0 self.opp_allotted_time = limit_time * 60.0 def receive_message(self, message): if message.command == "SET_PLAYER_ID": self.player_id = int(message.content) elif message.command == "REQUEST_MOVE": opp_time = message.content # user timer counts on client. # the server send the time where the move is received # opponent timer freezes if self.is_allotted_time: self.opp_cb.cancel() self.cb() if opp_time is not "": opp_time = float(opp_time) self.opp_allotted_time = opp_time self.ids["user_timer_label"].text = self.format_time( self.allotted_time) self.ids["opp_timer_label"].text = self.format_time( self.opp_allotted_time) self.ids["state_label"].text = "Your turn" self.play_turn = True self.draw_active_frame() elif message.command == "WAIT_OPP_MOVE": time = message.content # opponent timer counts on clienct server. # user timer freezes if self.is_allotted_time: self.cb.cancel() self.opp_cb() if time is not "": time = float(time) self.allotted_time = time self.ids["opp_timer_label"].text = self.format_time( self.opp_allotted_time) self.ids["user_timer_label"].text = self.format_time( self.allotted_time) self.ids["state_label"].text = "Opponent turn" self.draw_inactive_frame() elif message.command == "PLAYER1_MOVE": move = message.content self.target_game.play(move, player=1) self.update_display() elif message.command == "PLAYER2_MOVE": move = message.content self.target_game.play(move, player=2) self.update_display() elif message.command == "SET_USER_NAMES": self.user_name, self.opp_user_name = message.content user_text = ("O", "x") if self.player_id == 1 else ("x", "O") self.ids["user_label"].text = self.user_name + " : " + user_text[0] self.ids[ "opp_user_label"].text = self.opp_user_name + " : " + user_text[ 1] elif message.command == "GAME_FINISHED": self.ids["state_label"].text = "Game finished" winner = message.content if winner == 0: win_result = "Draw" elif winner == self.player_id: win_result = "You Win" else: win_result = "You Lose" popup = Popup(title='Game finished', content=Label(text=win_result), size_hint=(None, None), size=(200, 200)) popup.open() def cell_clicked(self, button): if self.play_turn: move = button.coords if not self.target_game.is_valid_play(move, player=self.player_id): return self.play_turn = False time = str(self.allotted_time) if self.is_allotted_time else "" self.network_interface.send(self.target_address, "MOVE", [time, move]) def update_display(self): # update all the buttons (at least for now and for usual board games, this is not too heavy) rows = self.target_game.rows() for i in range(rows): for j in range(self.target_game.cols()): self.button_list[i * rows + j].text = self.target_game.get_label(i, j) def format_time(self, remaining_time): return str(int(remaining_time // 60)).zfill(2) + " : " + str( int(remaining_time % 60)).zfill(2) def on_countdown(self, *args): if not self.is_allotted_time: pass if self.allotted_time > 0: self.allotted_time -= 1.0 else: self.cb.cancel() self.network_interface.send(self.target_address, "TIMEOUT", "") self.allotted_time = 0 self.ids["user_timer_label"].text = self.format_time( self.allotted_time) def on_opp_countdown(self, *args): if not self.is_allotted_time: pass if self.opp_allotted_time > 0: self.opp_allotted_time -= 1.0 else: self.opp_cb.cancel() self.opp_allotted_time = 0 self.ids["opp_timer_label"].text = self.format_time( self.opp_allotted_time) def draw_active_frame(self): self.ids["user_frame"].canvas.before.children[0] = Color(1, 1, 1, 1) self.ids["opp_user_frame"].canvas.before.children[0] = Color( 0, 135 / 255, 173 / 255, 1) self.ids["user_label"].color = (0 / 255, 95 / 255, 133 / 255, 1) self.ids["user_timer_label"].color = (0 / 255, 95 / 255, 133 / 255, 1) self.ids["opp_user_label"].color = (69 / 255, 173 / 255, 213 / 255, 1) self.ids["opp_timer_label"].color = (69 / 255, 173 / 255, 213 / 255, 1) def init_user_frame(self): self.ids["user_label"].text = " " self.ids["user_timer_label"].text = " " self.ids["opp_user_label"].text = " " self.ids["opp_timer_label"].text = " " def draw_inactive_frame(self): self.ids["user_frame"].canvas.before.children[0] = Color( 0, 135 / 255, 173 / 255, 1) self.ids["opp_user_frame"].canvas.before.children[0] = Color( 1, 1, 1, 1) self.ids["user_label"].color = (69 / 255, 173 / 255, 213 / 255, 1) self.ids["user_timer_label"].color = (69 / 255, 173 / 255, 213 / 255, 1) self.ids["opp_user_label"].color = (0 / 255, 95 / 255, 133 / 255, 1) self.ids["opp_timer_label"].color = (0 / 255, 95 / 255, 133 / 255, 1)
def __init__(self, agent): self.interface = NetworkInterface(MessageScanner()) self.results = open('results.txt', 'a') self.run(agent) self.results.close() agent.save_model('./saved/')
class ChatClient(Screen): # kivy string property indicating the network address of the chat chat_address = StringProperty() # called by Kivy when the screen is entered (displayed) def on_enter(self): #self.ids["textbox"].font_name=utf8_font_path self.ids["textbox"].focus = True self.ids["textbox"].text_validate_unfocus = False self.ids["textbox"].bind(on_text_validate=self.send_message) self.ids["textbox"].bind(on_key_pressed=self.key_pressed_event) # remove existing widgets when the screen is entered # note : could be improved later self.ids["msg_view"].clear() # indicates if the user is typing or not self.isTyping = False # current typing status of all clients self.current_typing_msg = "" self.typing_widget = None self.top_date = None self.bottom_date = None # current content self.content = [] self.network_interface = NetworkInterface(client=self) self.network_interface.send(self.chat_address, "ENTER", "") def send_message(self, *args): msg = self.ids["textbox"].text if msg and self.network_interface: self.isTyping = False self.network_interface.send(self.chat_address, "APPEND", msg) self.ids["textbox"].text = "" def receive_message(self, message): if message.command == "IS_TYPING": self.add_typing_message(message.content) return if message.command == "INIT_CONTENT": self.content = message.content self.item_add_last = len(self.content) self.init_displayed_content() elif message.command == "APPEND": item = message.content self.content.append(item) text_color_str = "000000" self.print_message(item[2], text_color_str, msg_time=item[0], username=item[1]) elif message.command == "NOTIFICATION_NEW_CLIENT": item = message.content # red for notifications text_color_str = "ff0000" self.print_message("A new guest is here \^_^/ : " + item[1], text_color_str, msg_time=item[0]) elif message.command == "NOTIFICATION_CLIENT_LIST": # we receive a list [time, otheruser1, otheruser2, ...] # red for notifications text_color_str = "ff0000" text = "" if len(message.content) > 1: text += "Currently connected guests: " for i, item in enumerate(message.content[1:]): if i > 0: text += ", " text += item else: text += "No other guest currently connected." self.print_message(text, text_color_str, msg_time=message.content[0]) elif message.command == "NOTIFICATION_CLIENT_LEFT": # red for notifications text_color_str = "ff0000" self.print_message("Chat left by " + message.content[1], text_color_str, msg_time=message.content[0]) # Init the displayed content # For a more reactive initialization, messages are added in small batches # It allows Kivy to refresh the screen before all messages are added # Batches of messages are added from the end of the messages (so that the last messages are immediately visible) # In the future, we could do some more advanced processing with kivy RecycleView # dt argument not used here. Set to the delta-time by Kivy. def init_displayed_content(self, dt=0): # stop the initialization when all messages have been added if self.item_add_last == 0: return for _ in range(20): self.item_add_last -= 1 item = self.content[self.item_add_last] text_color_str = "000000" self.print_message(item[2], text_color_str, msg_time=item[0], username=item[1], insert_pos='top') # after all message have been added, insert the first date manually if self.item_add_last == 0: if self.top_date is not None: self.insert_date_label(date=self.top_date[5:10], insert_pos='top') break # schedule initialization of the next batch of messages Clock.schedule_once(self.init_displayed_content) def insert_date_label(self, date, insert_pos='bottom'): day_label = FitTextRoundedLabel() day_label.set_text(date, text_color="000000") day_label.bcolor = [0.8, 1, 0.8, 1] self.ids["msg_view"].add(day_label, insert_pos, halign='center') def print_message(self, msg, text_color_str, msg_time=None, username=None, isTyping=False, insert_pos='bottom'): self.remove_typing_message() # Insert a date label when the date between two messages is different if msg_time != None: msg_local_time = DateUtil.convert_utc_to_local(msg_time) # case of first inserted message, initialize both top and last date if self.top_date is None or self.bottom_date is None: self.top_date = msg_local_time self.bottom_date = msg_local_time if insert_pos == 'bottom': # compare the date with bottom msg date if msg_local_time[:10] != self.bottom_date[:10]: self.insert_date_label(date=msg_local_time[5:10], insert_pos=insert_pos) self.bottom_date = msg_local_time elif insert_pos == 'top': # compare the date with top msg date if msg_local_time[:10] != self.top_date[:10]: self.insert_date_label(date=self.top_date[5:10], insert_pos=insert_pos) self.top_date = msg_local_time # main message label label = TitledLabel() label.size_hint_x = 0.8 label.set_text(msg, text_color=text_color_str) if username == MainUser.username: # for message from the user itself, blue background and label on the right label.bcolor = [0.8, 0.93, 1, 1] halign = 'right' else: halign = 'left' # minor label with time and user name if msg_time is not None: title_txt = "" if username == MainUser.username: # don't display username and aligned on right label.title_to_right() elif username is not None: title_txt = username + " " title_txt += "[size=12]" + DateUtil.convert_utc_to_local_HM( msg_time) + " [/size]" label.set_title_text(title_txt) self.ids["msg_view"].add(label, insert_pos, halign) if isTyping: self.typing_widget = label #============= typing status =========================== # Typing status is done by storing the current state of typing status # When the status changes, we remove the current status from the label, and display the new one (if any) def add_typing_message(self, msg): text_color_str = "0000ff" if self.current_typing_msg != "": self.current_typing_msg += "\n" self.current_typing_msg += msg + " is typing..." self.print_message(self.current_typing_msg, text_color_str, isTyping=True) def remove_typing_message(self): if not self.typing_widget is None: self.ids["msg_view"].remove(self.typing_widget) self.typing_widget = None self.current_typing_msg = "" # called when a key is pressed in the input def key_pressed_event(self, *args): # if the user was not already typing, send a TYPING message to the server if not self.isTyping: self.isTyping = True self.network_interface.send(self.chat_address, "IS_TYPING", "")
Config.set('input', 'mouse', 'mouse,multitouch_on_demand') # start the kivy-compatible twisted reactor from kivy.support import install_twisted_reactor install_twisted_reactor() from kivy.app import App from apps.desktop_client import DesktopClient # currently needed to set localhost from network_interface import NetworkInterface # Kivy does not include fonts supporting japanese # A font file must be provided manually # NOTO font downloaded from here: https://www.google.com/get/noto/help/cjk/ utf8_font_path = "NotoSansCJK-Regular.ttc" # Main App with a screen manager class MainClient(App): def build(self): return DesktopClient() if __name__ == '__main__': # note: command-line arguments are parsed at top of file, before kivy import if custom_args.use_localhost: NetworkInterface.set_server_to_localhost() MainClient().run()
def main(): interface = NetworkInterface(server=False) # Initiate connection and receive public keys from server interface.send('Connection...') msg = interface.blocking_receive() print("\nUPDATE: Received server's public keys") keys = msg.split('!VERIFICATION_KEY:') server_public_key_text = keys[0][11:] server_public_key = crypto_tools.hex_to_key(server_public_key_text) server_verification_key_text = keys[1] server_verification_key = crypto_tools.hex_to_key( server_verification_key_text) print("> PUBLIC:" + pretty(server_public_key_text, width=75)) print("> VERIF.:" + pretty(server_verification_key_text, width=75) + "\n") # Generate two new pairs of RSA keys my_public_key, my_private_key, my_verification_key, my_signing_key = crypto_tools.generate_key_pairs( ) # Share public keys with server print("UPDATE: Broadcasting public and verification keys") public_key_text = my_public_key.exportKey(format='DER').hex() verification_key_text = my_verification_key.exportKey(format='DER').hex() interface.send('PUBLIC_KEY:' + public_key_text + '!VERIFICATION_KEY:' + verification_key_text) print("> PUBLIC:" + pretty(public_key_text, width=75)) print("> VERIF.:" + pretty(verification_key_text, width=75) + "\n") # Confirm server ready for asymmetric encryption msg = interface.blocking_receive() if msg != "ASYM": print("ERROR: Server not ready for asymmetric encryption\n") interface.close_connection() sys.exit() # Generate a new AES session key aes_key = crypto_tools.generate_aes_key() print("UPDATE: Secret AES key generated") print("> KEY:", aes_key + "\n") ciphertext, signature = crypto_asymmetric.encrypt(aes_key, server_public_key, my_signing_key) # Send AES session key via RSA channel print('UPDATE: Transmitting AES key via RSA') interface.send("CIPHERTEXT:" + ciphertext + "!SIGNATURE:" + signature) print("> CIPHERTEXT:", pretty(ciphertext, width=300) + "\n") # Get ciphertext (AES) print("UPDATE: Receiving message from server") ciphertext = interface.blocking_receive() print("> CIPHERTEXT:", pretty(ciphertext, width=300) + "\n") plaintext = crypto_symmetric.decrypt(ciphertext, bytes.fromhex(aes_key)) if plaintext is None: print( "ERROR: Server's message was not verified! There may be interference\n" ) interface.close_connection() sys.exit() else: print("UPDATE: Secret message decrypted and checksum verified") print("> MESSAGE:", pretty(plaintext, width=300) + "\n") # Create verification of received message by hashing h = crypto_tools.hash_as_hex(plaintext) # Send hash of message back to server so they can confirm correct. print("UPDATE: Confirmation integrity with server") secret_message = h print("> HASH: " + pretty(h, width=75)) my_ciphertext = crypto_symmetric.encrypt(secret_message, bytes.fromhex(aes_key)) print("> CIPHERTEXT: " + pretty(my_ciphertext, width=300)) interface.send(my_ciphertext) # Await confirmation that message matches msg = interface.blocking_receive() print("> CONFIRMATION:", msg + "\n") if msg == "VERIFIED": print("UPDATE: Server verified message integrity\n") else: print("UPDATE: Message verification failed. There may be a threat\n") # Close connection interface.close_connection() print("UPDATE: Connection closed") print("-------------------------\n\n")
def main(): dispatcher = EventDispatcher() ninterface = NetworkInterface(dispatcher, -1, -1, "", 8888, "") login_app = LoginApp(dispatcher, ninterface, 1, 1) login_app.run()
def main(): interface = NetworkInterface(server=True) msg = interface.blocking_receive() print(msg) # Generate two new pairs of RSA keys my_public_key, my_private_key, my_verification_key, my_signing_key = crypto_tools.generate_key_pairs( ) # Broadcast public key and verification key print("\nUPDATE: Broadcasting public and verification keys") public_key_text = crypto_tools.key_to_hex(my_public_key) verification_key_text = crypto_tools.key_to_hex(my_verification_key) interface.send('PUBLIC_KEY:' + public_key_text + '!VERIFICATION_KEY:' + verification_key_text) print("> PUBLIC: " + pretty(public_key_text, width=75)) print("> VERIF.: " + pretty(verification_key_text, width=75) + "\n") # Receive public keys from client msg = interface.blocking_receive() keys = msg.split('!VERIFICATION_KEY:') print("UPDATE: Received client's public and verification keys") client_public_key_text = keys[0][11:] client_public_key = crypto_tools.hex_to_key(client_public_key_text) client_verification_key_text = keys[1] client_verification_key = crypto_tools.hex_to_key( client_verification_key_text) print("> PUBLIC: " + pretty(client_public_key_text, width=75)) print("> VERIF.: " + pretty(client_verification_key_text, width=75) + "\n") # Confirm ready for asymmetric encryption print("UPDATE: Asking client for session AES key\n") interface.send('ASYM') msg = interface.blocking_receive() keys = msg.split('!SIGNATURE:') client_ciphertext = keys[0][11:] client_signature = keys[1] print("UPDATE: Received RSA ciphertext and signature") print("> CIPHERTEXT: " + pretty(client_ciphertext, width=300)) print("> SIGNATURE: " + pretty(client_signature, width=75) + "\n") # Decyrpt ciphertext and verify message with signature print("UPDATE: Decrypting message and verifying signature") plaintext = crypto_asymmetric.decrypt(client_ciphertext, client_signature, my_private_key, client_verification_key) if plaintext is None: print("ERROR: Signature incorrect. There may be a threat\n") interface.close_connection() sys.exit() else: print("> MESSAGE (sess. key):", pretty(plaintext, width=300) + " (verified)\n") aes_key = plaintext # Send a secret message to client using received AES key print("UPDATE: Sending secret message to client using AES session key") secret_message = "Bush did 9/11 and Harambe was an inside job." my_ciphertext = crypto_symmetric.encrypt(secret_message, bytes.fromhex(aes_key)) print("> MESSAGE: " + pretty(secret_message, width=300)) print("> CIPHERTEXT: " + pretty(my_ciphertext, width=300) + "\n") interface.send(my_ciphertext) # Receive hash of message from client through same AES scheme print("UPDATE: Receiving hash of message for integrity check") ciphertext = interface.blocking_receive() print("> CIPHERTEXT:", pretty(ciphertext, width=300) + "\n") plaintext = crypto_symmetric.decrypt(ciphertext, bytes.fromhex(aes_key)) print("UPDATE: AES message decrypted") print("> MESSAGE (hash):", pretty(plaintext, width=300) + "\n") # Check that received hash matches hash of sent message h = crypto_tools.hash_as_hex(secret_message) if h == plaintext: print( "UPDATE: Verified. Client received the secret message successfully\n" ) interface.send("VERIFIED") else: print("UPDATE: Verification failed. There may be a security risk\n") interface.send("FAILED") # Close the connection interface.close_connection() print("UPDATE: Connection closed") print("-------------------------\n\n")
class RLAgent: """ A controller for the robot arm """ def __init__(self, agent): self.interface = NetworkInterface(MessageScanner()) self.results = open('results.txt', 'a') self.run(agent) self.results.close() agent.save_model('./saved/') def run(self, agent): """ RL feedback loop """ max_av_r = -10 min_tick = 300 sum_r = 0 iter_count = 0 n = 0 while(iter_count < 2000): n += 1 state = self.get_state() # get state of arm action = agent.act(state) # choose action = joint 0,1,2,3,4 reward = self.execute(action) # execute action and observe reward agent.observe(reward=reward[0], terminal=reward[1]) # add experience sum_r += reward[0] if reward[1]: iter_count += 1 av = sum_r / n # average reward received for that iteration if av > max_av_r: max_av_r = av pathlib.Path('./saved/reward/' + str(iter_count)).mkdir(parents=True, exist_ok=True) agent.save_model('./saved/reward/' + str(iter_count) + '/' + str(iter_count), False) if n < min_tick: min_tick = n pathlib.Path('./saved/actions/' + str(iter_count)).mkdir(parents=True, exist_ok=True) agent.save_model('./saved/actions/' + str(iter_count) + '/' + str(iter_count), False) print(iter_count, ': , ', n, ', ', av) # print the average reward for that iteration self.results.write(str(n) + ',' + str(sum_r) + '\n') self.results.flush() sum_r = 0 n = 0 def get_state(self): """ Gets the current state of the arm """ msg_recv = self.interface.blocking_receive() # wait for message if msg_recv.id != STATE: print('ERROR: Expected message.id=', STATE, ' but got ', msg_recv.id) return None state = [] for i in range(len(msg_recv.data)): state.append(float(msg_recv.data[i])) return state def execute(self, action): """ Send the action to the arm and receive the reward """ if action == 0: joint = 0 degrees = 1 elif action == 1: joint = 0 degrees = -1 elif action == 2: joint = 1 degrees = 1 elif action == 3: joint = 1 degrees = -1 elif action == 4: joint = 2 degrees = 1 elif action == 5: joint = 2 degrees = -1 elif action == 6: joint = 3 degrees = 1 elif action == 7: joint = 3 degrees = -1 else: joint = 0 degrees = 1 msg_send = self.rotate_joint(joint, degrees) self.interface.send(msg_send) # send the action msg_recv = self.interface.blocking_receive() # wait for message if msg_recv.id != REWARD: print('ERROR: Expected message.id=', REWARD, ' but got ', msg_recv.id) return None reward = (float(msg_recv.data[0]), bool(int(msg_recv.data[1]))) return reward def rotate_joint(self, joint, degrees): """ Return a message corresponding to a rotation """ msg = Message(ACTION) msg.add_data(str(joint)) msg.add_data(str(degrees) + '\n') return msg
from network_interface import NetworkInterface from message_scanner import MessageScanner if __name__ == '__main__': """ This script simulates the behaviour of the unity simulation -- use for easier network debugging """ scanner = MessageScanner() interface = NetworkInterface(scanner, server=True) state = get_state() interface.send(state) # receive the chosen action from the agent action = interface.receive() while (action == null): action = server.receive() # Execute the action, receive reward and send it to agent TCPMessage reward = Execute(action); server.SendMessage (reward); while(True): msg_recv = interface.receive() while (msg_recv is None): msg_recv = interface.receive() print('received: ', scanner.msg_to_ascii(msg_recv)) text = '1$received message' print('sent: ', text) msg_send = scanner.ascii_to_msg(text) interface.send(msg_send)
def setUp(self): self.network = Continuer() self.interface = NetworkInterface(network=self.network)
class TestNetworkInterface(unittest.TestCase): def setUp(self): self.network = Continuer() self.interface = NetworkInterface(network=self.network) def test_performance_on_set(self): dataset = SequencesDataset(set_size=10) performance = self.interface.performance_on_set(dataset) self.assertIsInstance(performance, float) def test_train_one_epoch(self): dataset = SequencesDataset(set_size=10) mean_loss = self.interface.train_one_epoch(dataset, weight_decay=0, learning_rate=0.02, shuffle=False) self.assertIsInstance(mean_loss, float) def test_performance_on_samples(self): dataset = SequencesDataset(set_size=10) performance = self.interface.performance_on_samples(dataset) sample = performance[0] # raw_sample = dataset[0] self.assertIsInstance(sample.x, torch.FloatTensor) self.assertEqual(len(sample.x.shape), 2) self.assertGreater(sample.x.shape[0], 8) self.assertEqual(sample.x.shape[1], 1) self.assertIsInstance(sample.loss, float) self.assertIsInstance(sample.actual_y, float) self.assertIsInstance(sample.predicted_y, float) def test___preprocessed_batch__(self): batch_size = 5 batch = SequencesDataset(set_size=batch_size)[:batch_size] x, y, normalization_factors, normalization_offsets = self.interface.__preprocessed_batch__( batch) self.assertIsInstance(x, torch.nn.utils.rnn.PackedSequence) x_unpacked, x_lengths = torch.nn.utils.rnn.pad_packed_sequence( x, batch_first=True) self.assertEqual(x_unpacked.shape[0], batch_size) self.assertGreater(x_unpacked.shape[1], 8) self.assertEqual(x_unpacked.shape[2], 1) self.assertIsInstance(y, torch.FloatTensor) # y_unpacked, y_lengths = torch.nn.utils.rnn.pad_packed_sequence(y, batch_first=True) self.assertEqual(y.shape, (batch_size, 1)) self.assertFalse(y.requires_grad) self.assertIsInstance(normalization_factors, torch.FloatTensor) self.assertEqual(normalization_factors.shape, (batch_size, )) self.assertIsInstance(normalization_offsets, torch.FloatTensor) self.assertEqual(normalization_offsets.shape, (batch_size, )) # def test_plugin_known_values(self): # interface = NetworkInterface() # y = [ # torch.tensor([[5], [8], [1]]), # torch.tensor([[9], [1]]) # ] # x = [ # torch.tensor([[4, 1], [9, 0], [2, 0]]), # torch.tensor([[3, 1], [2, 1]]) # ] # # y = torch.nn.utils.rnn.pack_sequence(y) # x = torch.nn.utils.rnn.pack_sequence(x) # # y = interface.__plugin_known_values__(y, x) # # y_unpacked, y_lengths = torch.nn.utils.rnn.pad_packed_sequence(y, batch_first=True) # tt.assert_equal(y_lengths, torch.tensor([3, 2])) # tt.assert_equal(y_unpacked, torch.tensor([ # [[4], [8], [1]], # [[3], [2], [0]] # ])) def test_normalize(self): batch = [(torch.FloatTensor([[1.0], [0.75], [0.5], [0.5]]), torch.FloatTensor([[2]])), (torch.FloatTensor([[1.5], [2], [1.5], [0], [-2]]), torch.FloatTensor([[6]]))] expected_offsets = torch.FloatTensor([-0.5, 2]) expected_factors = torch.FloatTensor([2, 0.25]) expected_normalized_batch = [ (torch.FloatTensor([[1], [0.5], [0], [0]]), torch.FloatTensor([[3]])), (torch.FloatTensor([[3.5 / 4], [1], [3.5 / 4], [0.5], [0]]), torch.FloatTensor([[2]])) ] normalized_batch, factors, offsets = self.interface.__normalize_batch__( batch) tt.assert_equal(offsets, expected_offsets) tt.assert_equal(factors, expected_factors) for i, normalized_element in enumerate(normalized_batch): normalized_x, normalized_y = normalized_element expected_normalized_x, expected_normalized_y = expected_normalized_batch[ i] tt.assert_equal(normalized_x, expected_normalized_x) tt.assert_equal(normalized_y, expected_normalized_y)
class WikiClient(Screen): # kivy string property indicating the target network address target_address = StringProperty() is_edit = BooleanProperty(False) # called by Kivy when the screen is entered (displayed) def on_enter(self): #self.ids["textbox"].font_name=utf8_font_path self.ids["textbox"].text_validate_unfocus = False self.ids["screen_manager"].transition = NoTransition() self.current_content = "" self.network_interface = NetworkInterface(client = self) self.read_address(self.target_address) def receive_message(self, message): if message.command == "READ_RESULT": self.current_content = message.content self.update_text(self.current_content) elif message.command == "READ_LOG_RESULT": result_str = "" for item in message.content[::-1]: idx, timestamp, username, comment = item result_str += DateUtil.convert_utc_to_local(timestamp) + " " + username + " " + comment + "\n" self.update_text(result_str) elif message.command == "WRITE_DONE": # read the address again after writing self.read_address(self.target_address) elif message.command == "NOT_EXISTING": self.current_content = "" self.update_text(self.target_address + " not existing yet.") def read_address(self, read_target_address): self.network_interface.send(read_target_address, "READ", "") def read_address_changelog(self, read_target_address): self.network_interface.send(read_target_address, "READ_LOG", "") def update_text(self, msg): self.ids["label"].set_wiki_text(msg, text_color= "000000") def start_edit(self): self.ids["textbox"].focus = True self.ids["textbox"].text = self.current_content def save_edit(self): page_content = self.ids["textbox"].text # TODO : real comment from the user in the interface change_comment = "" self.network_interface.send(self.target_address, "WRITE", [page_content, change_comment]) # remove the current content of the label to show that we are waiting the server response self.ids["label"].text = "" def cancel_edit(self): #TODO : confirmation popup pass def show_history(self): self.read_address_changelog(self.target_address)