Пример #1
0
def shutdown_metric(metric_server_addresses, authkey):
    """ This function tells all of the SimulationPotential instances running on the addresses in metric_server_addresses to shutdown. This is called when a SimulationClient instance shuts down.

    Args:
      metric_server_addresses: A list of tuples of the form (str, int) containing the hostnames and port numbers of the SimulationPotential instances.
      authkey (str): The password used in order to communicate with the SimulationPotential instances.

    Returns:
      float: The length of the curve with metric values in metric.

    """

    # For each SimulationPotential instance...
    for address in xrange(len(metric_server_addresses)):

        # Try making contact with the SimulationPotential instance...
        try:
            # Establish a connection with the SimulationPotential
            metric_server = Client(metric_server_addresses[address], authkey=authkey)

            # Send a status code indicating the SimulationPotential instance should stop running.
            metric_server.send({'status_code': comm_code('KILL')})

            # Close the connection.
            metric_server.close()

        # If contact with the SimulationPotential instance cannot be made then...
        except socket.error:
            # Make a note in the log which SimulationPotential couldn't be contacted.
            logging.warning('Failed to Make Connection to SimulationPotential at '
                          + str(metric_server_addresses[address]) + '.')
Пример #2
0
 def send(self, packet):
     address = ('localhost', self.port)
     conn = Client(address, authkey=self.pwd)
     self._printDebug("MultiBus,Client: send")
     conn.send(packet)
     conn.send('close')
     conn.close()
Пример #3
0
class RsClient:

	def __enter__(self):
		"""no init"""

	def __exit__(self, type=None, value=None, tb=None):

		#self.conn may not exist so expect that it may fail.
		try:
			self.conn.close()
		except:
			pass

	def start(self, host='localhost', port=8000, key='8457#$%^&3648'):

		address = (host, port)
		self.conn = Client(address)

	def send(self, data):
		try:
			self.conn.send(data)
		except:
			self.stop()

	def stop(self):
		self.conn.close()
def triggerEvent( type, scheduleMethod, *args ):
    """
    This function inserts an event into CHOTestMonkey
    """
    host = "localhost"
    port = 6000
    address = ( host, port )
    conn = Client( address )
    request = []
    request.append( 1 )
    request.append( type )
    request.append( scheduleMethod )
    for arg in args:
        request.append( arg )
    conn.send( request )
    response = conn.recv()
    while response == 11:
        conn.close()
        time.sleep( 1 )
        conn = Client( address )
        conn.send( request )
        response = conn.recv()
    if response == 10:
        print "Event inserted:", type, scheduleMethod, args
    elif response == 20:
        print "Unknown message to server"
    elif response == 21:
        print "Unknown event type to server"
    elif response == 22:
        print "Unknown schedule method to server"
    elif response == 23:
        print "Not enough argument"
    else:
        print "Unknown response from server:", response
    conn.close()
Пример #5
0
def communicate(message):
    address = ('localhost',6345)
    conn = Client(address)
    conn.send(message)
    reply = conn.recv()
    conn.close()
    return reply
Пример #6
0
class ThreadedExponentClient(ExponentClient):
    def __init__(self, ip, port, password):
        super(ThreadedExponentClient, self).__init__(ip, port, password)

        self.conn = Client((self.ip, self.port))

    def _auth(self, password):
        self.conn.send(password)
        auth_value = self.conn.recv()

        if auth_value == 'AUTH':
            print("Authorized")
            return True
        else:
            raise RuntimeError("Invalid Password")
            return False

    def __del__(self):
        print("Entering del")
        if self.__dict__.get('conn') != None:
            self.conn.close()

    def send(self, value):
        self.conn.send(value)
        return self.conn.recv()
Пример #7
0
class ConnectClient():
    address = None
    conn = None
    
    def __init__(self, ip = 'localhost', port = 7000):
        self.address = (ip, port)
        self.conn = Client(self.address)

    def __del__(self):
        self.conn.close()
    
    def register_module(self, process_file):
        """send and register a module in the server"""
        f = open(process_file, "rb")
        l = f.read()
        self.conn.send([0, l])
        f.close()

    def request_process(self, args):
        """request the server to do a process previously sent in a module"""
        self.conn.send([1, args[0]] + args[1:])
        answer = self.conn.recv()
        if isinstance(answer, Exception):
            raise answer
        return answer
Пример #8
0
    def send(self, msg):
        #conn = Client((self.address, self.port), authkey=str(self.key))
        conn = Client((self.address, self.port))

        conn.send(json.dumps(msg))

        conn.close()
Пример #9
0
def import_exec_globals(config, session):
    '''
    get exec_globals directory
    cycle over its files
        if the file name not in importregistry
            send its contents to server
            put filename in importregistry
    '''
    for module in get_plugins(config):
        dirname = os.path.join(os.path.dirname(module.__file__),
                               'exec_globals')
        for fname in sorted(os.listdir(dirname)):
            if fname.endswith('.py'):
                name = 'execs:' + fname[:-3]
                try:
                    session.query(ImportRegistry).filter(ImportRegistry.name==name).one()
                except NoResultFound:
                    path = os.path.join(dirname, fname)
                    with open(path, 'r') as f:
                        eg = f.read()
                    kb = Client((config('kb_host'), int(config('kb_port'))))
                    kb.send_bytes('compiler:exec_globals:' + eg)
                    kb.send_bytes('FINISH-TERMS')
                    for fact in iter(kb.recv_bytes, 'END'):
                        print(fact)
                    kb.close()
                    ir = ImportRegistry(name)
                    session.add(ir)
Пример #10
0
 def send(self, msg):
     # TODO: Busy wait will do for initial startup but for dealing with server down in the middle of things
     # TODO: then busy wait is probably inappropriate.
     if self.port == 5555:
         self.redis.rpush('flowqueue', msg)
     else:
         while True: # keep going until we break out inside the loop
             try:
                 self.logger.debug('Attempting to connect to '+self.serverName+' server at '+str(self.address)+' port '+str(self.port))
                 conn = Client((self.address, self.port))
                 self.logger.debug('Connect to '+self.serverName+' successful.')
                 break
             except SocketError as serr:
                 if serr.errno == errno.ECONNREFUSED:
                     self.logger.debug('Connect to '+self.serverName+' failed because connection was refused (the server is down). Trying again.')
                 else:
                     # Not a recognized error. Treat as fatal.
                     self.logger.debug('Connect to '+self.serverName+' gave socket error '+str(serr.errno))
                     raise serr
             except:
                 self.logger.exception('Connect to '+self.serverName+' threw unknown exception')
                 raise
         self.logger.debug('PCTRLSEND ' + str(msg))
         conn.send(msg)
         self.logger.debug('TRYCONNCLOSE trying to close the connection in pctrl')
         conn.close()
Пример #11
0
class UIListener:
    def __init__(self, address, port, authkey):
        self.client = Client((address, port), authkey=authkey)

    def get_args(self, n):
        return [self.client.recv() if self.client.poll(0.5) else None for _ in range(n)]

    def listenloop(self):
        keepalive = True

        try:
            while keepalive:
                while self.client.poll():
                    data = self.client.recv()
                    print('{}: {}'.format('x64' if utils.is64bit() else 'x86', data))

                    if data == 'close':
                        keepalive = False
                    else:
                        func = _interface[data]
                        self.client.send(func(*self.get_args(func.__code__.co_argcount)))
                        print('{}: sent response to {}'.format('x64' if utils.is64bit() else 'x86', data))

                time.sleep(0.05)
        except EOFError or ConnectionError:
            pass

            self.client.close()
Пример #12
0
	def send(self, msg):

		try:
			connection = Client(self.address, authkey=self.auth)
			connection.send(msg)
			connection.close()
		except ConnectionRefusedError:
			print('Connection Refused')
Пример #13
0
 def _send(self, msg):
     conn = Client(self.address, authkey=self.authkey)
     conn.send(msg)
     result = conn.recv()
     conn.close()
     if result:
         self._locked = True
     return result
Пример #14
0
    def run(self):
        while not self._stop:
            address = ('localhost', 6000)
            conn = Client(address)
            msg = conn.recv()
            conn.close()

            [cb() for k,cb in self.cbs.items() if msg==k]
Пример #15
0
def run():
    global connection
    connection = Client(('localhost', 6000), authkey=b'bluesky')
    plugin.init()
    stack.init()
    bs.sim.doWork()
    connection.close()
    print('Node', nodeid, 'stopped.')
Пример #16
0
 def send(self, command, payload = None):
     address = ('localhost', self.port)
     #print address
     conn = Client(address)
     conn.send((command, payload))
     response, response_payload = conn.recv()
     conn.close()
     return response_payload
Пример #17
0
 def run(self):
     address = ("localhost", 6000)
     conn = Client(address, authkey="secret password")
     while True:
         command = conn.recv_bytes()
         if command == "quit":
             break
         self.animations[:] = parse_command(command, self.boxs, "receiver")
     conn.close()
Пример #18
0
 def _query(self, message):
     connection = Client(self.address, family='AF_INET')
     connection.send(message)
     response = connection.recv()
     connection.close()
     if response['type'] == 'result':
         return response['data']
     else:
         raise DatabaseException(response['data'])
Пример #19
0
 def send_update(self, id, route):
     # TODO: Explore what is better, persistent client sockets or
     # new socket for each BGP update
     "Send this BGP route to participant id's controller"
     logger.debug("Sending a route update to participant "+str(id))
     conn = Client(tuple(self.participants[id].eh_socket), authkey = None)
     conn.send(json.dumps({'bgp': route}))
     conn.recv()
     conn.close()
Пример #20
0
 def method(*args, **kwargs):
     connection = Client(address, authkey=authkey)
     connection.send([method_name, args, kwargs])
     result = connection.recv()
     connection.close()
     if result['type'] == 'result':
         return result['data']
     else:
         raise Exception(result['data'])
Пример #21
0
def update_blackboard(nearby_devices):
    conn = Client(address)
    user = "******"
    for device in nearby_devices:
        if device in user1_devices:
            user = "******"
            break
    conn.send('PUT:user,' + str(user))
    conn.send('close')
    conn.close()
Пример #22
0
    def TryToConnect(self,child,host,port,n):
        address = (host,port)
#        address = ('jeremyfisher.math.udel.edu', 6000)
        #address = ('nutkin', 6000)
        conn = Client(address, authkey='secret password')
        conn.send(pickle.dumps(n,pickle.HIGHEST_PROTOCOL))
        vdata = pickle.loads(conn.recv())
        conn.send('close')
        conn.close()
        child.send(vdata)
Пример #23
0
def rebuild_handle(pickled_data):
    address, handle, inherited = pickled_data
    if inherited:
        return handle
    sub_debug('rebuilding handle %d', handle)
    conn = Client(address, authkey=current_process().authkey)
    conn.send((handle, os.getpid()))
    new_handle = recv_handle(conn)
    conn.close()
    return new_handle
Пример #24
0
 def compute_score(self, gts, res):
     conn = Client(self.address, authkey=self.authkey)
     try:
         score = self._handle_client(gts, res, conn)
     except Exception as ioe:
         print ioe, '103'
         score = (0.0, [0.0])
     finally:
         conn.close()
     return score
Пример #25
0
 def send(self, *args):
     """
     Send a command to the listener. Add the .calc_id as last argument.
     """
     if self.address:
         client = Client(self.address, authkey=self.authkey)
         try:
             client.send(args + (self.calc_id,))
         finally:
             client.close()
Пример #26
0
def run_backend(replace=False, exit=False, w2v_vector_file=W2V_VECTOR_FILE, temp_startup_server=False):
  repr_req_count = 0

  send_exit_command_after_init = False

  if not temp_startup_server:
    try:
      print('Connecting to backend.')
      conn = Client(BACKEND_ADDRESS, family=BACKEND_CONNECTION_FAMILY, authkey=BACKEND_PASSWORD)
      if not replace and not exit:
        conn.send({'command': 'CAPABILITIES'})
        msg = conn.recv()
        if msg['status'] == 'OK' and (msg['value'] == 'FULL' or msg['value'] == 'W2V_ONLY'):
          print('There is already an existing daemon with all neccessary capacities. Exiting.')
          sys.exit()
        else:
          print msg
          print('Found a running backend without required capabilities. Telling it to exit after init.')
          send_exit_command_after_init = True
      else: 
        print('Found a running backend. Telling it to exit after init.')
        send_exit_command_after_init = True
    except Exception, e:
      print(str(e))
      print(str(traceback.format_exc()))
      print('Did not manage to find a backend to kill.')

    if exit:
      print('Exiting.')
      sys.exit()

    if not send_exit_command_after_init:
      # The temp_startup_serer will take care of requests until startup (init()) is complete.
      p = Process(target=run_backend, kwargs={'temp_startup_server': True})
      p.start()
    
    init(w2v_vector_file=w2v_vector_file)
    
    # either temp_startup_server backend, or a previously running backend.
    print('Connecting to temp_startup_server (or previously running) backend.')
    conn = Client(BACKEND_ADDRESS, family=BACKEND_CONNECTION_FAMILY, authkey=BACKEND_PASSWORD)
    conn.send({'command': 'EXIT_NOW'})
    conn.close()
    conn = None
    if not send_exit_command_after_init:
      print('Waiting 1 sec for temp_startup_server backend to clean up.')
      time.sleep(1)
    else:
      print('Waiting 30 sec for previously running backend to clean up.')
      time.sleep(30)
      
    if not send_exit_command_after_init:
      p.terminate()
      print('temp_startup_server backend is now terminated.')
Пример #27
0
 def method(self, *args):
     args = list(args)
     args.insert(0, attribute)
     connection = Client(self.address, authkey=self.authkey)
     connection.send(dumps(args))
     result = loads(connection.recv())
     connection.close()
     if result[0] == 'RESULT':
         return result[1]
     else:
         raise MemoClientException(result[1])
Пример #28
0
def TryToConnect(child):
    address = ('jeremyfisher.math.udel.edu', 6000)
    #address = ('nutkin', 6000)
    conn = Client(address, authkey='secret password')
#    conn.send('hi')
    x = r_[0.1:0.5:10j]
    conn.send(pickle.dumps(x))
    conn.send('close')
    # can also send arbitrary objects:
    # conn.send(['a', 2.5, None, int, sum])
    conn.close()
    child.send('All done')
Пример #29
0
def send_message_to_server(configuration):
    address = configuration.get('address')
    authkey = configuration.get('authkey')
    message = configuration.get('message')
    try:
        client = Client(address, authkey=authkey)
        client.send(message)
        client.close()
    except socket_error as serr:
        if serr.errno != errno.ECONNREFUSED:
            raise serr
        print('client couldn\'t connect')
Пример #30
0
 def process(self, query, **kwargs):
     connection = Client(self.address, authkey=self.authkey)
     name = query.func_name
     kwargs["__query_name__"] = name
     tree = ast.parse(inspect.getsource(query))
     tree = _ast.Module(tree.body[0].body)
     connection.send([tree, kwargs])
     result = connection.recv()
     connection.close()
     if result["type"] == "result":
         return result["data"]
     else:
         raise Exception(result["data"])
Пример #31
0
class ClientAgent(Agent):
    """
    An Agent that can be sent requests from a ServerAgent and send back desired moves. Instantiate the client like you
    would a normal agent, either by passing in the functions to the constructor or by subclassing. The ClientAgent
    can be used for local play as usual, or if the game has been set up with a ServerAgent, call play_remote_game
    to have the client receive game information from the server and send back desired moves.
    """
    def __init__(self, handle_negative_cash_balance, make_pre_roll_move,
                 make_out_of_turn_move, make_post_roll_move,
                 make_buy_property_decision, make_bid, type):
        super().__init__(handle_negative_cash_balance, make_pre_roll_move,
                         make_out_of_turn_move, make_post_roll_move,
                         make_buy_property_decision, make_bid, type)
        self.conn = None
        self.kg = KG_OpenIE_eva()
        self.interface = Interface_eva()
        self.kg_change = [[]]
        self.func_history = []
        self.last_func = None
        self.kg_change_bool = False
        self.kg_change_wait = 0
        self.novelty_card = None
        self.novelty_bank = None
        self.gameboard = None
        self.gameboard_ini = None  # gameboard used in the first game of tournament
        self.upper_path = os.path.abspath('..').replace(
            '/Evaluation/monopoly_simulator', '')
        self.upper_path_eva = self.upper_path + '/Evaluation/monopoly_simulator'
        self.game_num = 0
        self.seed = random.randint(0, 10000)
        self.retrain_signal = False

        #Read the config
        self.config_file = self.upper_path_eva + '/A2C_agent/config.ini'
        self.config_data = ConfigParser()
        self.config_data.read(self.config_file)
        self.hyperparams = self.params_read(self.config_data, 'server')
        self.rule_change_name = self.hyperparams['rule_change_name']
        self.log_file_name = self.hyperparams['log_file_name']

        # Save path
        self.folder_path = None

        #A2C model parameters
        self.state_num = 90
        self.device = torch.device('cpu')
        model_path = self.upper_path_eva + '/A2C_agent/weights/Original_cpu_2.pkl'
        self.model = torch.load(model_path)
        self.logger = None
        self.adj_path = self.upper_path_eva + '/A2C_agent/kg_matrix_no.npy'

    def define_path(self, path):
        # Define the folder place the saved weights
        self.folder_path = self.upper_path_eva + '/A2C_agent/weights/' + path

        if not os.path.exists(self.folder_path):
            os.makedirs(self.folder_path)
        else:
            shutil.rmtree(self.folder_path)
            os.makedirs(self.folder_path)

        # Create logging info for this file
        self.logger = log_file_create(self.folder_path + '/log_client.log')
        self.logger.debug('Define the folder path as ' + self.folder_path)

    def initialize_gameboard(self, gameboard):
        """
        Intialize the used gameboard
        :param gameboard:
        :return:
        """
        gameboard_keys = [
            'bank', 'jail_position', 'railroad_positions', 'utility_positions',
            'go_position', 'go_increment', 'location_objects',
            'location_sequence', 'color_assets', 'dies', 'chance_cards',
            'community_chest_cards', 'chance_card_objects',
            'community_chest_card_objects', 'players', 'type'
        ]
        ini_gameboard = dict()
        for key in gameboard_keys:
            ini_gameboard[key] = gameboard[key]
        ini_gameboard['current_die_total'] = 0
        ini_gameboard['die_sequence'] = []
        ini_gameboard['history'] = dict()
        ini_gameboard['history'] = dict()
        ini_gameboard['history']['function'] = list()
        ini_gameboard['history']['param'] = list()
        ini_gameboard['history']['return'] = list()
        ini_gameboard['picked_community_chest_cards'] = []
        ini_gameboard['picked_chance_cards'] = []
        for i, player in enumerate(['players']):
            ini_gameboard['players'][i].agent = None
        return ini_gameboard

    def kg_run(self, current_gameboard):
        """
        Run the knowledge graph and novelty detection here
        :param current_gameboard:
        :return:
        """
        self.logger.debug('Run the knowledge graph to learn the rule')
        self.interface.set_board(current_gameboard)
        self.interface.get_logging_info_once(
            current_gameboard, self.folder_path + self.log_file_name)
        self.interface.get_logging_info(current_gameboard,
                                        self.folder_path + self.log_file_name)

        #######Add kg here######
        self.kg.set_gameboard(current_gameboard)
        self.kg.build_kg_file(self.folder_path + self.log_file_name,
                              level='rel',
                              use_hash=False)

        # self.kg.save_file(self.kg.kg_rel, self.kg_rel_path)
        # print('kg_run!!!!!!!!')
        # if 'is rented-0-house at' in self.kg.kg_rel.keys():
        #     if 'Indiana-Avenue' in self.kg.kg_rel['is rented-0-house at'].keys():
        #         print(self.kg.kg_rel['is rented-0-house at']['Indiana-Avenue'])

        # print(self.kg.kg_change)

        ####no need for evaluation####
        # self.kg.dict_to_matrix()
        # self.kg.save_matrix()
        # self.kg.save_vector()
        ###############################

        if self.kg_change_wait > 0:
            self.kg_change_wait += 1
            self.logger.debug('Novelty Firstly Detected ' +
                              str(self.kg_change_wait) + ' times ago.')
            self.logger.debug('New Novelty Detected as ' + str(self.kg_change))

        # Visulalize the novelty change in the network
        if self.kg.kg_change != self.kg_change[-1]:
            self.kg_change_wait = 1
            self.kg_change.append(self.kg.kg_change[:])
            self.logger.debug('Novelty Detected as ' + str(self.kg_change))

        # Only when never retrained and detect novelty, will call retrain
        if self.kg_change_wait > 3 and self.kg_change_bool == False:
            self.retrain_signal = True if self.kg_change_bool == False else False

            # log
            if self.retrain_signal == True:
                self.logger.debug(
                    'Retrain signal is True now, it will retrain the NN before next game!'
                )
            else:
                self.logger.debug(
                    'Retrain happened before, no need to retrain again!')

            # ini_current_gameboard = self.initialize_gameboard(current_gameboard)
            # model_retrained_path = self.retrain_from_scratch(ini_current_gameboard)
            # self.model = torch.load(model_retrained_path)
            # self.state_num = len(self.interface.board_to_state(current_gameboard))  # reset the state_num size
            # self.kg_change_bool = True
            # self.kg_change_wait = 0

    def params_read(self, config_data, key_word):
        params = {}
        for key in config_data[key_word]:
            v = eval(config_data[key_word][key])
            params[key] = v
        return params

    def retrain_from_scratch(self, gameboard):
        """
        Retrain the model and save the model
        :param gameboard:
        :return:
        """
        start_time = datetime.datetime.now()

        self.logger.info('Retrain start!!! It will save to ' +
                         self.folder_path)

        # Retrain the network
        params = self.params_read(self.config_data, 'hyper')
        params['save_path'] = self.folder_path
        # set exp dict
        exp_dict = dict()
        exp_dict['novelty_num'] = (0, 0)
        exp_dict['novelty_inject_num'] = sys.maxsize
        exp_dict['exp_type'] = '0_0'

        trainer = MonopolyTrainer_GAT(params,
                                      gameboard=gameboard,
                                      kg_use=True,
                                      logger_use=False,
                                      config_file='config.ini',
                                      test_required=False,
                                      tf_use=False,
                                      retrain_type='gat_part',
                                      device_id='-1',
                                      adj_path=self.adj_path,
                                      exp_dict=exp_dict)

        trainer.train()
        self.logger.info('Retrain results => ' + str(trainer.test_result))

        win_rate = trainer.test_result['winning_rate']
        win_rate.reverse()
        opt = len(win_rate) - win_rate.index(max(win_rate))
        model_retrained_path = self.folder_path + '/cpu_v3_lr_' + str(
            params['learning_rate']) + '_#_' + str(opt) + '.pkl'

        end_time = datetime.datetime.now()
        time_retrain = (end_time - start_time).seconds / 60
        self.logger.info('Retrain use ' + str(time_retrain) + 'mins')
        self.logger.info('The ' + str(opt) +
                         'steps is the best, we will use ' +
                         model_retrained_path)
        self.kg_change_wait = 0

        return model_retrained_path, trainer.adj_path

    def play_remote_game(self,
                         address=('localhost', 6001),
                         authkey=b"password"):
        """
        Connects to a ServerAgent and begins the loop of waiting for requests and responding to them.
        @param address: Tuple, the address and port number. Defaults to localhost:6000
        @param authkey: Byte string, the password used to authenticate the client. Must be same as server's authkey.
            Defaults to "password"
        """

        self.conn = Client(address, authkey=authkey)
        while True:
            func_name, args = self.conn.recv()  # Receive the signal

            # # When the game starts, we clear all the history
            # if func_name == 'start_tournament':
            #     self.clear_weights()
            #     result = 1

            # When the tournament begins, we need to
            if func_name == "start_tournament":
                self.define_path(args)
                self.logger.info('Tournament starts!')
                result = 1

            # Before simulating each game, we have to make sure if we need retrain the network
            elif func_name == "startup":  # args = (current_gameboard, indicator)
                # Clear interface history and set the init for interface
                self.interface.clear_history(self.folder_path +
                                             self.log_file_name)
                self.interface.set_board(
                    args[0])  # args[0] is current_gameboard
                s = self.interface.board_to_state(args[0])
                self.game_num += 1  # number of games simulated
                self.logger.info(str(self.game_num) + ' th game starts!')

                # We need to have a baseline of the gameboard
                if self.game_num == 1:
                    self.gameboard_ini = args[0]

                # Detect card type and number change with comparing gameboard
                if self.novelty_card == None:
                    self.novelty_card = detect_card_nevelty(
                        args[0], self.gameboard_ini)
                if self.novelty_bank == None:
                    self.novelty_bank = detect_contingent(
                        args[0], self.gameboard_ini)

                if self.novelty_card:
                    self.kg_change.append(self.novelty_card)
                    self.novelty_card = []
                if self.novelty_bank:
                    self.kg_change.append(self.novelty_bank)
                    self.novelty_bank = []

                # if board size changed, before the game, we need to retrain the NN
                if self.state_num != len(s) or self.retrain_signal:
                    ini_current_gameboard = self.initialize_gameboard(args[0])
                    model_retrained_path, adj_path = self.retrain_from_scratch(
                        ini_current_gameboard)

                    self.model = torch.load(model_retrained_path)
                    self.state_num = len(self.interface.board_to_state(
                        args[0]))  # reset the state_num size
                    self.kg_change_bool = True
                    self.retrain_signal = False
                    self.kg_change_wait = 0
                    self.adj_path = adj_path

                result = getattr(self, func_name)(*args)

            # When each game ends, we run the KG, but we don not shutdown the connection
            elif func_name == 'shutdown':
                result = 1
                self.kg_run(self.gameboard)
                self.logger.info(str(self.game_num) + ' th game stops!')

            # When calling agent to make decision
            elif func_name == 'make_post_roll_move':  # args = (player, current_gameboard, allowable_moves, code)
                result = self.make_post_roll_move_agent(args)

            # Send we will close the connection now back to server
            elif func_name == "end_tournament":
                result = 1
                self.logger.info('Tournament Finished!')

            else:
                result = getattr(self, func_name)(*args)

            # Send the results back to server agent
            self.conn.send(result)

            # Close connection after each tournament
            if func_name == "end_tournament":

                # Write to history
                file = open(self.folder_path + self.rule_change_name, "a")
                file.write(str(self.seed) + str(self.kg_change) + ' \n')
                file.close()
                self.logger.info('Novelty of game saved to ' +
                                 self.folder_path + self.rule_change_name)

                self.conn.close()
                break

    def make_post_roll_move_agent(self, args):
        player, current_gameboard, allowable_moves, code = args
        self.gameboard = copy.deepcopy(current_gameboard)

        # self.interface.set_board(current_gameboard)
        # a.save_history(current_gameboard, save_path='/media/becky/Evaluation/GNOME-p3/monopoly_simulator/loc_history.pickle')

        # if player.assets == None:
        # a.save_bool(0)
        self.interface.get_logging_info_once(
            current_gameboard, self.folder_path + self.log_file_name)

        s = self.interface.board_to_state(current_gameboard)

        # if self.state_num != len(s):
        #     ini_current_gameboard = self.initialize_gameboard(args[1])
        #     model_retrained_path = self.retrain_from_scratch(ini_current_gameboard)
        #     self.model = torch.load(model_retrained_path)
        #     self.state_num = len(self.interface.board_to_state(args[1]))  # reset the state_num size
        #     self.kg_change_bool = True
        #     self.kg_change_wait = 0

        s = s.reshape(1, -1)
        # print(s.shape)
        s = torch.tensor(s, device=self.device).float()
        prob = self.model.actor(s)
        action = Categorical(prob).sample().cpu().numpy()
        action = action[0]
        # print(action)
        actions_vector = self.interface.action_num2vec(action)
        move_actions = self.interface.vector_to_actions(
            current_gameboard, player, actions_vector)
        #####################

        move_action = [i[0] for i in move_actions]
        space_action = [i[1] for i in move_actions]

        current_location = current_gameboard['location_sequence'][
            player.current_position]

        if action_choices.buy_property in move_action:
            if action_choices.buy_property in allowable_moves:
                params = dict()
                params['player'] = player
                params['asset'] = current_location
                params['current_gameboard'] = current_gameboard
                if code == -1:
                    return (action_choices.concluded_actions, dict())
                player.agent._agent_memory[
                    'previous_action'] = action_choices.buy_property
                return (action_choices.buy_property, params)

        # improve property
        if action_choices.improve_property in move_action:  # beef up full color sets to maximize rent potential.
            if action_choices.improve_property in allowable_moves:
                params = dict()
                params['player'] = player
                params['asset'] = space_action[0]
                params['current_gameboard'] = current_gameboard
                player.agent._agent_memory[
                    'previous_action'] = action_choices.improve_property
                return (action_choices.improve_property, params)

        # free-mortgage
        if action_choices.free_mortgage in move_action:
            if action_choices.free_mortgage in allowable_moves:
                # free mortgages till we can afford it. the second condition should not be necessary but just in case.
                params = dict()
                params['player'] = player
                params['asset'] = space_action[0]
                params['current_gameboard'] = current_gameboard
                player.agent._agent_memory[
                    'previous_action'] = action_choices.free_mortgage
                return (action_choices.free_mortgage, params)

        # mortgage
        if action_choices.mortgage_property in move_action:
            if action_choices.mortgage_property in allowable_moves:
                params = dict()
                params['player'] = player
                params['asset'] = space_action[0]
                params['current_gameboard'] = current_gameboard
                player.agent._agent_memory[
                    'previous_action'] = action_choices.mortgage_property
                return (action_choices.mortgage_property, params)
        return (action_choices.concluded_actions, dict())
Пример #32
0
class AgentDistributed(Player):
    """ AI agent which will play chess. This is a parallel friendly version
    of the Agent. The only difference is that this agent sends the data to
    a worker process which is responsible of making the predictions. Based on
    that, it builds a pool of connections to the worker and uses a parallelized
    version of the Monte Carlo Tree Search.

    Attributes:
        move_encodings: list. List of all possible uci movements.
        uci_dict: dict. Dictionary with mappings 'uci'-> int. It's used
        to predict the policy only over the legal movements.
        endpoint: (str, int). Tuple with the address, port of the PredictWorker
        num_threads: int. Number of threads to use during MTCS
        conn: Connection. Connection to the prediction worker that is in use
        pool_conns: list[Connection]. Pool of connections that will be used
        during MCTS.
    """
    def __init__(self, color, endpoint=None, num_threads=6):
        super().__init__(color)

        self.move_encodings = netencoder.get_uci_labels()
        self.uci_dict = {u: i for i, u in enumerate(self.move_encodings)}

        self.conn = None
        self.pool_conns = None
        self.address = endpoint
        self.num_threads = num_threads

    def best_move(
            self,
            game: 'Game',
            real_game=False,
            max_iters=900,  # noqa: E0602, F821
            ai_move=True,
            verbose=False) -> str:
        """ Finds and returns the best possible move (UCI encoded)

        Parameters:
            game: Game. Current game before the move of this agent is made.
            real_game: Whether to use MCTS or only the neural network (self
            play vs playing in a real environment).
            max_iters: if not playing a real game, the max number of iterations
            of the MCTS algorithm.
            verbose: Whether to print debug info
            ai_move: bool. Whether to return the next move of the AI

        Returns:
            str. UCI encoded movement.
        """
        best_move = '00000'  # Null move
        if real_game:
            policy = self.predict_policy(game)
            best_move = game.get_legal_moves()[np.argmax(policy)]
        else:
            if game.get_result() is None:
                current_tree = mctree.SelfPlayTree(game,
                                                   threads=self.num_threads)
                best_move = current_tree.search_move(self,
                                                     max_iters=max_iters,
                                                     verbose=verbose,
                                                     ai_move=ai_move)

        return best_move

    def predict_outcome(self, game: 'Game') -> float:  # noqa: E0602, F821
        """ Predicts the outcome of a game from the current position """
        response = self.__send_game(game)
        return response[1]

    def predict_policy(self,
                       game: 'Game',
                       mask_legal_moves=True) -> float:  # noqa: E0602, F821
        """ Predict the policy distribution over all possible moves. """
        response = self.__send_game(game)
        policy = response[0]

        if mask_legal_moves:
            legal_moves = game.get_legal_moves()
            policy = [policy[self.uci_dict[x]] for x in legal_moves]
        return policy

    def predict(self, game: 'Game'):  # noqa: E0602, F821
        """ Predicts from a game board and returns policy / value"""
        response = self.__send_game(game)
        return response

    def __send_game(self, game: 'Game'):  # noqa: E0602, F821
        """ Sends a game to the neural net. and blocks the caller thread until
        the prediction is done.

        Parameters:
            game: Game. Game to send. to worker.
        """
        self.conn.send(game)
        response = self.conn.recv()
        return response

    def get_copy(self):
        """ Returns an empty agent with the color of this one """
        copy = AgentDistributed(self.color, endpoint=self.address)
        return copy

    def connect(self):
        self.conn = Client(self.address)

    def disconnect(self):
        self.conn.close()
        self.conn = None
Пример #33
0
 def run(self):
     '''
         This function overloads the Thread class function,
         and is called when you run start() to start a new thread.
     '''
     try:
         #Setup dictionaries to convert between foqus and ALAMO
         #variable names.  Also setup a list of input and output
         #variable names for ALAMO.  I'm going to keep the dicts
         #for now in case using the FOQUS variables names in ALAMO
         #doesn't work out for some reason.
         uq_file = self.options[
             "FOQUS Model (for UQ)"].value
         self.xList = self.input
         self.zList = self.output
         self.xi = {}
         self.zi = {}
         cn = self.graph.input.compoundNames(sort=True)
         for v in self.xList:
             self.xi[v] = cn.index(v)
         cn = self.graph.output.compoundNames(sort=False)
         for v in self.zList:
             self.zi[v] = cn.index(v)
         #Get options and show some information about settings
         adaptive = self.options['SAMPLER'].value
         alamoDir = self.alamoDir
         alamoDirFull = os.path.abspath(alamoDir)
         self.setupWorkingDir()
         adpexe = os.path.join(alamoDirFull, 'foqusALAMOClient.py')
         if os.path.isfile(adpexe):
             adpexe = win32api.GetShortPathName(adpexe)
             self.writeAlamoInputFile(adaptiveExe=adpexe)
         else:
             self.writeAlamoInputFile(adaptiveExe=adpexe)
         if self.checkNumVars():
             return
         alamoExe = self.dat.foqusSettings.alamo_path
         alamoInput = self.options["Input File"].value
         alamoOutput = alamoInput.rsplit('.', 1)[0] + '.lst'
         self.msgQueue.put("------------------------------------")
         self.msgQueue.put("Starting ALAMO\n")
         self.msgQueue.put("Exec File Path:    " + alamoExe)
         self.msgQueue.put("Sub-directory:     " + alamoDir)
         self.msgQueue.put("Input File Name:   " + alamoInput)
         self.msgQueue.put("Output File Name:  " + alamoOutput)
         self.msgQueue.put("SAMPLER:           " + str(adaptive))
         self.msgQueue.put("UQ Driver File:    " + uq_file)
         self.msgQueue.put("------------------------------------")
         # If using adaptive sampleing open a network socket to listen
         if adaptive:
             listener = listen.foqusListener(self.dat)
             listener.setInputs(self.input)
             listener.setOutputs(self.output)
             listener.failValue = self.options["PRESET"].value
             address = listener.address
             listener.start()
         #Run Alamo
         process = subprocess.Popen([
             alamoExe,
             alamoInput],
             cwd=alamoDir,
             stdout=subprocess.PIPE,
             stderr=subprocess.STDOUT,
             stdin = None,
             creationflags=win32process.CREATE_NO_WINDOW)
         line = process.stdout.readline()
         while process.poll() == None or line != '':
             if line == '': time.sleep(0.2)
             if line != '':
                 self.msgQueue.put(line.rstrip())
             line = process.stdout.readline()
             if self.stop.isSet():
                 self.msgQueue.put("**terminated by user**")
                 process.kill()
                 break
         if adaptive:
             # stop the listener
             conn = Client(address)
             conn.send(['quit'])
             conn.close()
         alamoOutput = os.path.join(alamoDir, alamoOutput)
         if self.options['FUNFORM'].value != "Fortran":
             self.msgQueue.put("MUST USE FORTRAN FORM to make UQ and flowsheet plugins")
             self.msgQueue.put("Skipping plugin creation")
             return
         res = SurrogateParser.parseAlamo(alamoOutput)
         self.result = res
         #self.msgQueue.put(str(res))
         self.writePlugin()
         SurrogateParser.writeAlamoDriver(
             self.result,
             uq_file,
             ii=self.ii,
             oi=self.oi,
             inputNames=self.xList2,
             outputNames=self.zList2)
         self.driverFile = uq_file
     except Exception:
         self.ex = sys.exc_info()
         logging.getLogger("foqus." + __name__).error(
                 "Exception in ALAMO Thread",
                 exc_info=sys.exc_info())
Пример #34
0
class RemoteAgent:
    def __init__(self,
                 address,
                 socket_family,
                 auth_key,
                 connection_retries=100):
        self._log = logging.getLogger(self.__class__.__name__)
        auth_key_conn = str.encode(auth_key) if auth_key else None

        self._conn = None
        self._tp_exec = futures.ThreadPoolExecutor()

        for i in range(connection_retries):
            # Waiting on agent to open it's socket.
            try:
                self._conn = Client(address,
                                    family=socket_family,
                                    authkey=auth_key_conn)
                break
            except Exception:
                self._log.debug(
                    f"RemoteAgent retrying connection to agent in: attempt {i}"
                )
                time.sleep(0.1)

        if self._conn is None:
            raise RemoteAgentException("Failed to connect to remote agent")

    def __del__(self):
        self.terminate()

    def _act(self, obs, timeout):
        # Send observation
        self._conn.send({"type": "obs", "payload": obs})
        # Receive action
        if self._conn.poll(timeout):
            try:
                return self._conn.recv()
            except ConnectionResetError as e:
                self.terminate()
                raise e
        else:
            return None

    def act(self, obs, timeout=None):
        # Run task asynchronously and return a Future
        return self._tp_exec.submit(self._act, obs, timeout)

    def start(self, agent_spec: AgentSpec):
        # Send the AgentSpec to the agent runner
        self._conn.send(
            # We use cloudpickle only for the agent_spec to allow for serialization of lambdas
            {
                "type": "agent_spec",
                "payload": cloudpickle.dumps(agent_spec)
            })

    def terminate(self):
        if self._conn:
            self._conn.close()

        # Shutdown thread pool executor
        self._tp_exec.shutdown()
Пример #35
0
        conn = cl.accept()
        print '.............connection accepted from', cl.last_accepted
        m = conn.recv()
        print '.............message received from server', m


if __name__ == '__main__':

    print 'trying to connect'
    conn = Client(address=('127.0.0.1', 6000),
                  authkey='secret password server')
    conn.send(local_listener)

    cl = Process(target=client_listener, args=())
    cl.start()

    connected = True
    while connected:
        value = raw_input("'C', stay connected. 'Q' quit connection")
        if value == 'Q':
            connected = False
        else:
            print "continue connected"
            conn.send("connected")

    print "last message"
    conn.send("quit")
    conn.close()
    cl.terminate()
    print "end client"
Пример #36
0
def send_signal(s):
    conn = Client(signal_address)
    conn.send(s)
    conn.close()
    return