def __init__(self, args, connection): self.connectionComm = connection # process input file self.args = args self.xmlHandler = XmlHandler() self.xmlHandler.process_input(INPUT, args.input) self.numAvg = self.xmlHandler.get_header_item("averages") self.args.path += "/" if self.args.path[-1] != "/" else "" # ids self.cid = 0 # number of contexts/scenarii sent self.pid = 0 # number of parameter sets sent # create children self.netGenerator = NetGen(args.path, self.xmlHandler) self.netGenerator.process_input_file(args.input)
class PhaseSpaceExplorer(object): __metaclass__ = ABCMeta di_instructions = { COMMAND: None, SIZE: None, DATA: None, ID: None } str_cid = "Context{}" str_pid = "ParamSet{}" #------# # Init # #------# def __init__(self, args, connection): self.connectionComm = connection # process input file self.args = args self.xmlHandler = XmlHandler() self.xmlHandler.process_input(INPUT, args.input) self.numAvg = self.xmlHandler.get_header_item("averages") self.args.path += "/" if self.args.path[-1] != "/" else "" # ids self.cid = 0 # number of contexts/scenarii sent self.pid = 0 # number of parameter sets sent # create children self.netGenerator = NetGen(args.path, self.xmlHandler) self.netGenerator.process_input_file(args.input) @abstractmethod def init_parameters(self): ''' initialize the parameters ''' pass #------------------------# # sending/receiving data # #------------------------# def send_xml_context(self): ''' send the xml context to the server ''' bContinue = False # fill the dictionary di_context = self.di_instructions.copy() di_context[COMMAND] = C_S di_context[ID] = self.str_cid.format(self.cid) di_context[DATA] = self.xmlHandler.get_string_context() di_context[SIZE] = len(di_context[DATA]) # check context/scenario, then send bContinue = False if di_context[DATA] is None else True self.connectionComm.send(di_context) bReceived = self.connectionComm.recv() self.cid += 1 return bReceived*bContinue def send_next_matrices(self): ''' send the next pair of matrices to the server ''' self.reservoir, self.connect = self.netGenerator.next_pair() if self.reservoir is not None: # fill the dictionary for the reservoir di_reservoir = self.di_instructions.copy() di_reservoir[COMMAND] = C_M di_reservoir[ID] = self.reservoir.get_name() di_reservoir[DATA] = mat_to_string( self.reservoir.get_mat_adjacency(), self.reservoir.get_name() ) di_reservoir[SIZE] = len(di_reservoir[DATA]) # send reservoir self.connectionComm.send(di_reservoir) bPairReceived = self.connectionComm.recv() # fill the dictionary for the connectivity di_connect = self.di_instructions.copy() di_connect[COMMAND] = C_M di_connect[ID] = self.connect.get_name() di_connect[DATA] = mat_to_string( self.connect.as_csr(), self.connect.get_name() ) di_connect[SIZE] = len(di_connect[DATA]) # send connectivity self.connectionComm.send(di_connect) bPairReceived *= self.connectionComm.recv() return bPairReceived else: return False def send_parameters(self): ''' send the parameters to the server ''' strNameReservoir, strNameConnect = self.current_names() # fill the dictionary di_param = self.di_instructions.copy() di_param[COMMAND] = C_P di_param[ID] = self.str_pid.format(self.pid) xmlParamList = self.xmlHandler.gen_xml_param( strNameConnect, strNameReservoir, self.lstParameterSet ) di_param[DATA] = self.xmlHandler.to_string(xmlParamList) di_param[SIZE] = len(di_param[DATA]) di_param[MAXPROG] = float(len(xmlParamList)-1) # send self.connectionComm.send(di_param) bReceived = self.connectionComm.recv() self.pid += 1 return bReceived def get_results(self, lstParam): ''' launch the run and wait for the results, then get the scores into an array of reals ''' self.connectionComm.send({COMMAND: RUN}) strXmlResults = self.connectionComm.recv() results = self.xmlHandler.results_dic(strXmlResults, lstParam) return results #---------# # Running # #---------# @abstractmethod def run(self): pass #----------------# # Tool functions # #----------------# def make_dirs(self): pass def current_names(self): return self.reservoir.get_name(), self.connect.get_name() def save_networks(self, path): save_reservoir(self.reservoir, path) save_connect(self.connect, path, self.reservoir.get_name())