def offline_cluster(self,datapoint,timestamp): """ offline clustering of a datapoint Args: datapoint (ndarray): single point from the dataset timestamp (int): timestamp of the datapoint """ if len(self.kernels)!=self.m: #0. Initialize self.kernels.append(Kernel(datapoint,timestamp,self.t,self.m)) return centers=[kernel.center for kernel in self.kernels] #TODO :faster computing with caching #1. Determine closest kernel closest_kernel_index,min_distance=min( ((i,np.linalg.norm(center-datapoint)) for i,center in enumerate(centers)), key=lambda t:t[1] ) closest_kernel=self.kernels[closest_kernel_index] closet_kernel_center=centers[closest_kernel_index] # 2. Check whether instance fits into closest_kernel if closest_kernel.n==1: # Special case: estimate radius by determining the distance to the # next closest cluster radius=min(( #distance between the 1st closest center and the 2nd np.linalg.norm(center-closet_kernel_center) for center in centers if not center is closet_kernel_center )) else: radius=closest_kernel.get_radius() if min_distance<radius: # Date fits, put into kernel and be happy closest_kernel.insert(datapoint,timestamp) print(f"{timestamp} fits",file=FILE) return # 3. Date does not fit, we need to free # some space to insert a new kernel threshold = timestamp - self.time_window # Kernels before this can be forgotten # 3.1 Try to forget old kernels oldest_kernel_index=next(( i for i,kernel in enumerate(self.kernels) if kernel.get_relevance_stamp() < threshold ),None) if oldest_kernel_index!=None: print(f"{timestamp} forgot old kernel",file=FILE) self.kernels[oldest_kernel_index]=Kernel(datapoint,timestamp,self.t,self.m) return # 3.2 Merge closest two kernels print(f"{timestamp} merge closest kernel",file=FILE) combination_indexes=itertools.combinations(range(len(centers)),2) closest_a,closest_b,dist=min( ((i,j,np.linalg.norm(centers[j]-centers[i])) for i,j in combination_indexes), key=lambda t:t[-1] ) self.kernels[closest_a].add(self.kernels[closest_b]) self.kernels[closest_b]=Kernel(datapoint,timestamp,self.t,self.m)
def __init__(self, aiml_path, drinks_path, app): super(DialogueManager, self).__init__() app.start() session = app.session self.__shutdown_requested = False signal.signal(signal.SIGINT, self.signal_handler) self.memory = session.service("ALMemory") self.kernel = Kernel() self.__learn(aiml_path) self.possible_drinks = slu_utils.lines_to_list(drinks_path)
def __init__(self, params): self.phase = params.get("phase") var_enum = params.get("var_enum") dof_handler = params.get("dof_handler") params.set("var_index", dof_handler.variable_index[var_enum][self.phase]) Kernel.__init__(self, params) if self.phase == 0: self.sign = 1.0 else: self.sign = -1.0 # create list of relevant variable indices self.aA1_index = self.dof_handler.aA1_index[0] self.arhoA1_index = self.dof_handler.arhoA_index[0] self.arhouA1_index = self.dof_handler.arhouA_index[0] self.arhoEA1_index = self.dof_handler.arhoEA_index[0] self.arhoA2_index = self.dof_handler.arhoA_index[1] self.arhouA2_index = self.dof_handler.arhouA_index[1] self.arhoEA2_index = self.dof_handler.arhoEA_index[1] self.var_indices = [self.aA1_index, self.arhoA1_index, self.arhouA1_index, self.arhoEA1_index, self.arhoA2_index, self.arhouA2_index, self.arhoEA2_index] # create variable names self.vf1 = "vf1" self.arhoA1 = "arhoA1" self.arhouA1 = "arhouA1" self.arhoEA1 = "arhoEA1" self.arhoA2 = "arhoA2" self.arhouA2 = "arhouA2" self.arhoEA2 = "arhoEA2" self.rho1 = "rho1" self.u1 = "u1" self.E1 = "E1" self.v1 = "v1" self.e1 = "e1" self.p1 = "p1" self.T1 = "T1" self.rho2 = "rho2" self.u2 = "u2" self.E2 = "E2" self.v2 = "v2" self.e2 = "e2" self.p2 = "p2" self.T2 = "T2"
class UserSpace: ''' Represents functions and operations available to a normal user. ''' def __init__(self): self.kernel = Kernel() self.kernel.memory.mmap("read_only.txt", Flags.READ_ONLY, Flags.MAP_PRIVATE) def write(self): ''' Write is a non-atomic operation meaning that it takes *multiple* steps to complete. For the sake of this exercise we'll assume that write always calls mem_write. This is not (entirely) true and only applies IF you try to write to /proc/self/mem. What is /proc/self/mem? In short, it's a pseudo file (doesn't really exist on disk) that the kernel generates on the fly, this file represents the memory of our current program. Why /proc/self/mem? Recall that mem_write is only called when writing to /proc/self/mem, if you try to write to something else another internal write function is called. The bug that allows for the dirty cow exploit to exist occurs in the call stack of mem_write, this is why we try to write to /proc/self/mem/. FUN FACT: ptrace was an alternative attack vector to /proc/self/mem https://man7.org/linux/man-pages/man2/ptrace.2.html https://github.com/nowsecure/dirtycow/blob/master/ptrace.c For more context take a look at http://lxr.linux.no/linux+v4.8/fs/proc/base.c#L933 which defines operations for mem_write. Also the manual page for proc: https://man7.org/linux/man-pages/man5/procfs.5.html ''' self.kernel.mem_write("MOOOOOOOOOOOOOOOOOOOOOOOO", 0)
def __str__ (self): if silent_mode: return '' filled = '' if self.fill_price: filled = " (filled @ {})".format(dollarize(self.fill_price)) # Until we make explicit market orders, we make a few assumptions that EXTREME prices on limit # orders are trying to represent a market order. This only affects printing - they still hit # the order book like limit orders, which is wrong. return "(Agent {} @ {}) : {} {} {} @ {}{}".format(self.agent_id, Kernel.fmtTime(self.time_placed), "BUY" if self.is_buy_order else "SELL", self.quantity, self.symbol, dollarize(self.limit_price) if abs(self.limit_price) < sys.maxsize else 'MKT', filled)
def __init__(self, params): self.phase = params.get("phase") self.var_enum = params.get("var_enum") dof_handler = params.get("dof_handler") params.set("var_index", dof_handler.variable_index[self.var_enum][self.phase]) Kernel.__init__(self, params) # create list of relevant variable indices self.arhoA_index = self.dof_handler.arhoA_index[self.phase] self.arhouA_index = self.dof_handler.arhouA_index[self.phase] self.arhoEA_index = self.dof_handler.arhoEA_index[self.phase] if self.dof_handler.model_type == ModelType.TwoPhase: self.aA1_index = self.dof_handler.aA1_index[0] self.var_indices = [ self.aA1_index, self.arhoA_index, self.arhouA_index, self.arhoEA_index ] else: self.aA1_index = float("NaN") self.var_indices = [ self.arhoA_index, self.arhouA_index, self.arhoEA_index ] # create variable names phase_str = str(self.phase + 1) self.vf = "vf" + phase_str self.arhoA = "arhoA" + phase_str self.arhouA = "arhouA" + phase_str self.arhoEA = "arhoEA" + phase_str self.rho = "rho" + phase_str self.u = "u" + phase_str self.E = "E" + phase_str self.v = "v" + phase_str self.e = "e" + phase_str self.p = "p" + phase_str self.grad_arhoA = "grad_" + self.arhoA self.grad_arhouA = "grad_" + self.arhouA self.grad_arhoEA = "grad_" + self.arhoEA
def Gs_model(self, delta, beta, N_ps, N_ns, N_pt, N_nt, q=0): X_ts = np.append(self.X_tar, self.X_src) K_t = Kernel(self.X_tar, self.theta) J_t = self.KFDA_J(K_t, N_pt, N_nt, q) log_pt = np.log(self.P_poster(self.X_tar, K_t, J_t, delta)) pt = self.P_poster(self.X_tar, K_t, J_t, N_pt, N_nt, q) K_ts = Kernel(X_ts, self.theta) J_ts = self.KFDA_J(K_ts, N_ps + N_pt, N_ns + N_nt, q) log_pts = np.log(self.P_poster(X_ts, K_ts, J_ts, delta)) pts = self.P_poster(X_ts, K_ts, J_ts, N_ps, N_ns, q) K_s = Kernel(self.X_src, self.theta) J_s = self.KFDA_J(K_s, N_ps, N_ns, q) log_ps = np.log(self.P_poster(self.X_src, K_s, J_s, delta)) Lmodl = -log_pt + beta * pt * log_pt + beta * (pts * log_ps - pts * log_pts) return Lmodl
def __str__(self): if silent_mode: return '' filled = '' if self.dollar: if self.fill_price: filled = " (filled @ {})".format(dollarize(self.fill_price)) else: if self.fill_price: filled = " (filled @ {})".format(self.fill_price) # Until we make explicit market orders, we make a few assumptions that EXTREME prices on limit # orders are trying to represent a market order. This only affects printing - they still hit # the order book like limit orders, which is wrong. return "(Order_ID: {} Agent {} @ {}) : {} {} {} @ {}{}".format( self.order_id, self.agent_id, Kernel.fmtTime(self.time_placed), "CREATE" if self.is_buy_order else "REDEEM", self.quantity, self.symbol, filled, self.fill_price)
class SysLib(object): def __init__(self): self.__kernel = Kernel() def createMem(self) -> list: return self.__kernel.allocMemory() def storeProcess(self, mem: list, processo) -> bool: return self.__kernel.allocProcess(mem, processo) def removeProcess(self, mem: list, pid: int) -> bool: return self.__kernel.freeProcess(mem, pid) def deleteMem(self, mem: list): return self.__kernel.freeMemory(mem) def scheduler(self, mem: list): return self.__kernel.escalonador(mem) def showProcesses(self, mem: list, code: int): return self.__kernel.monitor(mem, code)
# This will configure the kernel with a default computation delay # (time penalty) for each agent's wakeup and recvMsg. An agent # can change this at any time for itself. (nanoseconds) defaultComputationDelay = 1000000000 * 5 # five seconds # IMPORTANT NOTE CONCERNING AGENT IDS: the id passed to each agent must: # 1. be unique # 2. equal its index in the agents list # This is to avoid having to call an extra getAgentListIndexByID() # in the kernel every single time an agent must be referenced. ### Configure the Kernel. kernel = Kernel("Base Kernel", random_state=np.random.RandomState( seed=np.random.randint(low=0, high=2**32))) ### Obtain random state for whatever latency model will be used. latency_rstate = np.random.RandomState( seed=np.random.randint(low=0, high=2**32)) ### Obtain a seed for the train-test split shuffling. shuffle_seed = np.random.randint(low=0, high=2**32) ### Configure the agents. When conducting "agent of change" experiments, the ### new agents should be added at the END only. agent_count = 0 agents = [] agent_types = []
from Kernel import Kernel kernel = Kernel() kernel.learn("chat_app/std-startup.xml") kernel.respond("load aiml") kernel.respond("Hi") kernel.learn("std-startup.xml") # while True: # print kernel.respond(raw_input("> ")) # while True: # print kernel.respond("") def askquestion(stringinput): kernel.learn("std-startup.xml") return kernel.respond(stringinput)
from Kernel import Kernel from Classifier import Classifier from Detector import Detector input = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] patterns = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 1, 0]] classifier = Classifier(patterns) kernel = Kernel((3, 3), (1, 1), classifier) detector = Detector(kernel) print(detector(input))
import redis from time import sleep from Kernel import Kernel import threading # TTS service def tts(text): memory.set('ttsOverride',text) # Redis memory (short-term, HAL) memory = redis.StrictRedis(host='localhost', port=6379, db=0) # AIML memory k = Kernel() k.saveFilepath('/root/AutonomousVehicle/src/python/nlp/memory/') k.learn("*") class nlpEngine(): state = 'nlp' def __init__(self): threading.Thread(target=self.respond).start() def respond(self): while True: result = memory.get('stt_result') if result and (state=='nlp'): text = memory.get('stt_result') memory.set('stt_result','') # Processes meta-vars shiftArray(lastSaidUser,text)
tablefmt='orgtbl')) ## # kernels => Kernel('linear') || Kernel('gaussian') || Kernel('polynomial') # metrics => Metric('pearson') || Metric('spearman') || Metric('ig') # svm_C => Float || None # keys_limit => Number # show_plot => False || True # logs => False || True ## if __name__ == "__main__": keys_limit = 1000 svm_C = 1 show_plot = True create_table([Kernel('linear')], [Metric('pearson'), Metric('spearman'), Metric('ig')], keys_limit, svm_C) if show_plot: pearson_keys, m, f = build(Kernel('linear'), Metric('pearson'), keys_limit, svm_C, False) spearman_keys, m, f = build(Kernel('linear'), Metric('spearman'), keys_limit, svm_C, False) ig_keys, m, f = build(Kernel('linear'), Metric('ig'), keys_limit, svm_C, False) Plot().euler(pearson_keys, spearman_keys, ig_keys)
def main(): kernel = Kernel()
type="ValueAgent", symbol=symbol, starting_cash=starting_cash, sigma_n=sigma_n, r_bar=r_bar, kappa=kappa, lambda_a=lambda_a, random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64'))) for j in range(agent_count, agent_count + num_value)]) agent_count += num_value agent_types.extend(['ValueAgent']) ######################################################################################################################## ########################################### KERNEL AND OTHER CONFIG #################################################### kernel = Kernel("Market Replay Kernel", random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, dtype='uint64'))) kernelStartTime = mkt_open kernelStopTime = historical_date + pd.to_timedelta('16:01:00') defaultComputationDelay = 0 latency = np.zeros((agent_count, agent_count)) noise = [0.0] kernel.runner(agents=agents, startTime=kernelStartTime, stopTime=kernelStopTime, agentLatency=latency, latencyNoise=noise, defaultComputationDelay=defaultComputationDelay, defaultLatency=0,
for i in range(agent_count, agent_count + num_exchanges) ]) agent_types.extend(["ExchangeAgent" for i in range(num_exchanges)]) agent_count += num_exchanges ### Configure a simple message latency matrix for the agents. Each entry is the minimum # nanosecond delay on communication [from][to] agent ID. # Square numpy array with dimensions equal to total agent count. In the SRG config, # there should not be any communication delay. latency = np.zeros((len(agent_types), len(agent_types))) # Configure a simple latency noise model for the agents. # Index is ns extra delay, value is probability of this delay being applied. # In the SRG config, there is no latency (noisy or otherwise). noise = [1.0] # Create the data oracle for this experiment. All agents will use the same one. oracle = MeanRevertingOracle(mkt_open, mkt_close, symbols) # Start a basic kernel. kernel = Kernel("Base Kernel") kernel.runner(agents=agents, startTime=kernelStartTime, stopTime=kernelStopTime, agentLatency=latency, latencyNoise=noise, defaultComputationDelay=defaultComputationDelay, oracle=oracle, log_dir=log_dir)
import time from CommunicationBox import CommunicationBox from Kernel import Kernel from Plot.Plot import Plot from ModelChecker import ModelChecker if __name__ == "__main__": f = open('args', 'r') a = f.read() f.close() i, startIndex, num = [int(i) for i in a.split(',')] f = open('./results/main.csv', 'a+') time_begin = time.time() cb = CommunicationBox(startIndex) kernel = Kernel(cb, startIndex, num) time_end = time.time() #Plot.displayPlot(cb) filename = './results/' + str(i) + '.csv' cb.saveToFile(kernel, filename) print(time_end - time_begin) ModelChecker.check_model(startIndex, cb, 40, num, filename, f, startIndex) f.flush() del (kernel, cb) i += 1 f.close()
result.append(predictor.predict(point)) Z = np.array(result).reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=cm.Paired, levels=[-0.001, 0.001], extend='both', alpha=0.8) plt.scatter(flatten(X[:, 0]), flatten(X[:, 1]), c=flatten(y), cmap=cm.Paired) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.show() num_samples = 500 num_features = 2 grid_size = 20 samples = np.matrix( np.random.normal(size=num_samples * num_features).reshape( num_samples, num_features)) labels = 2 * (samples.sum(axis=1) > 0) - 1.0 model = SVM(1., Kernel.linear()) model.fit(samples, labels) plot(model, samples, labels, grid_size)
class DialogueManager(object): PATH = '' RANKED_EVENT = "VRanked" TABLET_ANSWER_EVENT = "TabletAnswer" DIALOGUE_REQUEST_EVENT = "DialogueVequest" PARSE_AIML_REQUEST_EVENT = "ParseAIMLRequest" cocktail_data = {} location = {} order_counter = 0 restaurant_order_counter = 0 def __init__(self, aiml_path, drinks_path, app): super(DialogueManager, self).__init__() app.start() session = app.session self.__shutdown_requested = False signal.signal(signal.SIGINT, self.signal_handler) self.memory = session.service("ALMemory") self.kernel = Kernel() self.__learn(aiml_path) self.possible_drinks = slu_utils.lines_to_list(drinks_path) #self.food = slu_utils.lines_to_list(drinks_path) def start(self): self.ranked_sub = self.memory.subscriber(DialogueManager.RANKED_EVENT) self.ranked_sub_id = self.ranked_sub.signal.connect( self.ranked_callback) self.dialogue_sub = self.memory.subscriber( DialogueManager.DIALOGUE_REQUEST_EVENT) self.dialogue_sub_id = self.dialogue_sub.signal.connect( self.request_callback) self.tablet_sub = self.memory.subscriber( DialogueManager.TABLET_ANSWER_EVENT) self.tablet_sub_id = self.tablet_sub.signal.connect( self.tablet_callback) self.aiml_sub = self.memory.subscriber( DialogueManager.PARSE_AIML_REQUEST_EVENT) self.aiml_sub_id = self.aiml_sub.signal.connect(self.parse_aiml) time.sleep(1) print "[" + self.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers( DialogueManager.RANKED_EVENT) print "[" + self.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers( DialogueManager.DIALOGUE_REQUEST_EVENT) print "[" + self.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers( DialogueManager.TABLET_ANSWER_EVENT) print "[" + self.__class__.__name__ + "] Subscribers:", self.memory.getSubscribers( DialogueManager.PARSE_AIML_REQUEST_EVENT) def quit(self): self.ranked_sub.signal.disconnect(self.ranked_sub_id) self.dialogue_sub.signal.disconnect(self.dialogue_sub_id) self.tablet_sub.signal.disconnect(self.tablet_sub_id) self.aiml_sub.signal.disconnect(self.aiml_sub_id) def ranked_callback(self, msg): transcriptions_dict = slu_utils.list_to_dict_w_probabilities(msg) best_transcription = slu_utils.pick_best(transcriptions_dict) print "[" + self.__class__.__name__ + "] User says: " + best_transcription reply = self.kernel.respond(best_transcription) self.do_something(reply) def request_callback(self, msg): splitted = msg.split('_') to_send = ' '.join(splitted) print 'to_send: ' + to_send if 'start' in splitted: print 'Found start' self.memory.raiseEvent("ASR_enable", "1") if 'end' in splitted: print 'Found end' self.memory.raiseEvent("ASR_enable", "0") if 'stop' in splitted: print 'Found stop' self.memory.raiseEvent("ASR_enable", "0") if 'SPRinfocollected' in splitted[1]: try: crowd_info = json.loads(self.memory.getData('Humans/Crowd')) total = crowd_info['total'] males = crowd_info['num_males'] females = crowd_info['num_females'] to_send = "say SPRinfocollected total " + total + " males " + males + " females " + females except: to_send = "say SPRinfocollected donotknow" if 'restaurantlistorders' in splitted[1]: for i in range(1, self.restaurant_order_counter): try: table = json.loads( self.memory.getData('/Restaurant/Table' + str(i))) order = table['Order'] if len(order) == 1: reply = order[0] + ' for table ' + str(i) self.memory.raiseEvent("Veply", reply) elif len(order) == 2: self.restaurant_order_counter += 1 reply = order[0] + ' and ' + order[ 1] + ' for table ' + str(i) self.memory.raiseEvent("Veply", reply) except: pass if 'order' == splitted[1] or 'confirmdrink' == splitted[ 1] or 'confirmnotavailable' == splitted[ 1] or 'unknownavailable' == splitted[1]: try: self.current_user_id = splitted[2] self.user_profile = json.loads( self.memory.getData("Humans/Profile" + self.current_user_id)) except: print 'Invalid User' #Need to define how to get back the names and drink name = self.user_profile['Name'] drink = self.user_profile['Drink'] to_send = 'say ' + splitted[ 1] + ' customer ' + name + ' drink ' + drink if 'unknownavailable' == splitted[0]: print 'Found unknownavailable' self.current_user_id = splitted[1] try: self.user_profile = json.loads( self.memory.getData("Humans/Profile" + self.current_user_id)) except: print 'Invalid User' to_send = splitted[0] + ' customer ' + self.user_profile[ 'Name'] + ' drink ' + self.user_profile[ 'Drink'] + ' ' + splitted[2] if 'callpersonunavailable' == splitted[1]: try: self.profile_1 = json.loads( self.memory.getData("Humans/Profile1")) self.profile_2 = json.loads( self.memory.getData("Humans/Profile2")) self.profile_3 = json.loads( self.memory.getData("Humans/Profile3")) if self.profile_1['DrinkAvailability'] == 'False': customer = self.profile_1['Name'] drink = self.profile_1['Drink'] self.profile_unavailable = self.profile_1 if self.profile_2['DrinkAvailability'] == 'False': customer = self.profile_2['Name'] drink = self.profile_2['Drink'] self.profile_unavailable = self.profile_2 if self.profile_3['DrinkAvailability'] == 'False': customer = self.profile_3['Name'] drink = self.profile_3['Drink'] self.profile_unavailable = self.profile_3 except: pass to_send = 'SAY CALLPERSONUNAVAILABLE CUSTOMER ' + customer + ' DRINK ' + drink if 'altdrink' == splitted[0]: print 'Found altdrink' try: self.current_user_id = splitted[1] self.user_profile = json.loads( self.memory.getData("Humans/Profile" + self.current_user_id)) except: print 'Invalid User' #Need to define how to get back the names and drink name = self.user_profile['Name'] to_send = splitted[0] + ' customer ' + name + ' ' + splitted[2] if 'providealternatives' == splitted[0]: print 'Found providealternatives' try: name = self.profile_unavailable['Name'] drinks = self.profile_unavailable['DrinkAlternatives'] alternatives = '' if len(alternatives) == 0: drinks = ['coke', 'green tea', 'aquarius'] for drink in drinks: alternatives = drink + ',' + alternatives except: name = 'John' alternatives = 'coke, green tea and aquarius' # Need to define how to get back the names and drink 'PROVIDEALTERNATIVES CUSTOMER * ALTERNATIVES * START' to_send = splitted[ 0] + ' customer ' + name + ' alternatives ' + alternatives + ' ' + splitted[ 2] if 'persondescription' == splitted[1]: print 'Found persondescription' try: local_profile = json.loads( self.memory.getData("Humans/" + splitted[2])) name = local_profile['Name'] gender = local_profile['Gender'] shirt = local_profile['ColorTshirt'] except: sentence = '' sentence = name + " is the " + gender + " with the " + shirt + " t-shirt!" self.memory.raiseEvent("Veply", sentence) if 'fivequestions' == splitted[0]: to_send = splitted[0] + ' ' + splitted[2] reply = self.kernel.respond(to_send) self.do_something(reply) def tablet_callback(self, msg): best_transcription = msg print "[" + self.__class__.__name__ + "] User says: " + best_transcription reply = self.kernel.respond(best_transcription) self.do_something(reply) def signal_handler(self, signal, frame): print "[" + self.__class__.__name__ + "] Caught Ctrl+C, stopping." self.__shutdown_requested = True print "[" + self.__class__.__name__ + "] Good-bye" def __learn(self, path): for root, directories, file_names in os.walk(path): for filename in file_names: if filename.endswith('.aiml'): self.kernel.learn(os.path.join(root, filename)) print "[" + self.__class__.__name__ + "] Number of categories: " + str( self.kernel.num_categories()) def do_something(self, message): splitted = message.split('|') for submessage in splitted: if '[SAY]' in submessage: reply = submessage.replace('[SAY]', '').strip() print "[" + self.__class__.__name__ + "] Robot says: " + reply self.memory.raiseEvent("Veply", reply) elif '[TAKEORDERDATA]' in submessage: data = submessage.replace('[TAKEORDERDATA]', '').replace(')', '').strip() customer, drink = data.split('(') try: self.person_id = self.memory.getData( "Actions/personhere/PersonID") except: self.person_id = 9999 cocktail_data = {} cocktail_data['PersonID'] = self.person_id cocktail_data['Name'] = customer cocktail_data['Drink'] = drink cocktail_data['DrinkAvailability'] = True self.memory.raiseEvent("DialogueVesponse", json.dumps(cocktail_data)) elif '[RESTAURANTORDERDATA]' in submessage: data = submessage.replace('[RESTAURANTORDERDATA]', '').replace(')', '').strip() food_alternatives = [] self.restaurant_order_data = {} for drink in self.possible_drinks: if drink in data: food_alternatives.append(drink) self.restaurant_order_data['Order'] = food_alternatives if len(food_alternatives) == 1: self.restaurant_order_counter += 1 reply = 'So, you want only ' + food_alternatives[0] self.memory.raiseEvent("Veply", reply) self.memory.insertData( "/Restaurant/Table" + str(self.restaurant_order_counter), json.dumps(self.restaurant_order_data)) self.memory.raiseEvent( "DialogueVesponse", json.dumps(self.restaurant_order_data)) elif len(food_alternatives) == 2: self.restaurant_order_counter += 1 reply = 'So, you want ' + food_alternatives[ 0] + ' and ' + food_alternatives[1] self.memory.raiseEvent("Veply", reply) self.memory.insertData( "/Restaurant/Table" + str(self.restaurant_order_counter), json.dumps(self.restaurant_order_data)) self.memory.raiseEvent( "DialogueVesponse", json.dumps(self.restaurant_order_data)) else: reply = 'Sorry, I did not understand your choice. ' self.memory.raiseEvent("Veply", reply) reply = self.kernel.respond(reply) self.do_something(reply) elif '[TAKEORDER]' in submessage: data = submessage.replace('[TAKEORDER]', '').replace(')', '').strip() set_condition(self.memory, "takeorder", "true") self.memory.raiseEvent("DialogueVesponse", 'takeorder') elif '[WANTSTOFOLLOW]' in submessage: data = submessage.replace('[WANTSTOFOLLOW]', '').replace(')', '').strip() set_condition(self.memory, "wantstofollow", "true") self.memory.raiseEvent("DialogueVesponse", 'wantstofollow') elif '[NOTFOLLOW]' in submessage: data = submessage.replace('[NOTFOLLOW]', '').replace(')', '').strip() set_condition(self.memory, "wantstofollow", "false") self.memory.raiseEvent("DialogueVesponse", 'wantstofollow') elif '[NOTTAKEORDER]' in submessage: data = submessage.replace('[NOTTAKEORDER]', '').replace(')', '').strip() set_condition(self.memory, "takeorder", "false") self.memory.raiseEvent("DialogueVesponse", 'nottakeorder') elif '[DRINKSALTERNATIVES]' in submessage: data = submessage.replace('[DRINKSALTERNATIVES]', '').replace(')', '').strip() alternatives = [] for drink in self.possible_drinks: if drink in data: alternatives.append(drink) self.user_profile['DrinkAlternatives'] = alternatives self.user_profile['DrinkAvailability'] = False reply = 'So' for available_drink in alternatives: reply = reply + ', ' + available_drink reply = reply + '. I am going to notify the alternatives.' self.memory.raiseEvent("Veply", reply) self.memory.raiseEvent("DialogueVesponse", json.dumps(self.user_profile)) self.memory.insertData("Humans/Profile" + self.current_user_id, json.dumps(self.user_profile)) elif '[DRINKAVAILABLE]' in submessage: self.user_profile['DrinkAvailability'] = True self.memory.raiseEvent("DialogueVesponse", json.dumps(self.user_profile)) self.memory.insertData("Humans/Profile" + self.current_user_id, json.dumps(self.user_profile)) elif '[LOOKFORDATA]' in submessage: data = submessage.replace('[LOOKFORDATA]', '').strip() self.location['location'] = data try: data = str(self.memory.getData('/location_mapping/' + data)).replace("+", " ") except Exception as e: reply = "The " + data + " is somewhere!" self.memory.insertData("helplocation", data) self.memory.raiseEvent("DialogueVesponse", json.dumps(self.location)) elif '[OPTIONS]' in submessage: data = submessage.replace('[OPTIONS]', '').strip() data = data.replace(' ', ' ') data = data.replace(' ', '+') self.memory.raiseEvent('AnswerOptions', 'speechbtn_' + data) elif '[STOPFOLLOWING]' in submessage: self.memory.raiseEvent("ASR_enable", "0") self.memory.raiseEvent("DialogueVesponse", '[STOPFOLLOWING]') set_condition(self.memory, "stopfollowing", "true") elif '[WHATSTHETIME]' in submessage: now = datetime.datetime.now() reply = "It's " + str(now.hour) + " " + str(now.minute) print "[" + self.__class__.__name__ + "] Robot says: " + reply self.memory.raiseEvent("DialogueVesponse", '[WHATSTHETIME]') self.memory.raiseEvent("Veply", reply) elif '[STOP]' in submessage: self.memory.raiseEvent("DialogueVesponse", "Action stopped") self.memory.raiseEvent("ASR_enable", "0") set_condition(self.memory, "stopfollowing", "false") elif '[STOPASR]' in submessage: set_condition(self.memory, "stopfollowing", "false") self.memory.raiseEvent("DialogueVesponse", "Action stopped") self.memory.raiseEvent("ASR_enable", "0") elif '[WHEREIS]' in submessage: data = submessage.replace('[WHEREIS]', '').strip().replace(" ", "+") try: location = str( self.memory.getData('/location_mapping/' + data)).replace("+", " ") reply = "The " + data.replace( "+", " ") + " is in the " + location except Exception as e: reply = "The " + data.replace("+", " ") + " is somewhere!" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[HOWMANY]' in submessage: data = submessage.replace('[HOWMANY]', '') splitted = data.split("#") reply = "There are multiple " + splitted[ 0] + " in the " + splitted[1] self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[CROWD]' in submessage: data = submessage.replace('[CROWD]', '') try: crowd_info = json.loads( self.memory.getData('Humans/Peoplesummary')) total = crowd_info['total'] num_males = crowd_info['num_males'] num_females = crowd_info['num_females'] if total == 0: reply = "I don't know how many people there are" else: reply = "I may be wrong, but I see " + total + " people: " + num_males + " are males and " + num_females + " are females." except Exception as e: reply = "I don't know how many people there are" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[CHILDREN]' in submessage: data = submessage.replace('[CHILDREN]', '') reply = "I don't know how many children there are" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[MALE]' in submessage: data = submessage.replace('[MALE]', '') try: crowd_info = json.loads( self.memory.getData('Humans/Peoplesummary')) total = crowd_info['total'] num_males = crowd_info['num_males'] num_females = crowd_info['num_females'] if total == 0: reply = "I don't know how many males there are" else: reply = "I may be wrong, but I see " + total + " people: " + num_males + " are males and " + num_females + " are females." except Exception as e: reply = "I don't know how many males there are" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[FEMALE]' in submessage: data = submessage.replace('[FEMALE]', '') try: crowd_info = json.loads( self.memory.getData('Humans/Peoplesummary')) total = crowd_info['total'] num_males = crowd_info['num_males'] num_females = crowd_info['num_females'] if total == 0: reply = "I don't know how many females there are" else: reply = "I may be wrong, but I see " + total + " people: " + num_males + " are males and " + num_females + " are females." except Exception as e: reply = "I don't know how many females there are" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[WAVING]' in submessage: data = submessage.replace('[WAVING]', '') reply = "I don't know how many people are waving arms" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[RISING]' in submessage: data = submessage.replace('[RISING]', '') reply = "I don't know how many people are rising arms" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[STANDING]' in submessage: data = submessage.replace('[STANDING]', '') try: crowd_info = json.loads( self.memory.getData('Humans/Peoplesummary')) total = crowd_info['total'] if total == 0: reply = "I don't know how many people are standing" else: reply = "I see " + total + " people that are standing." except Exception as e: reply = "I don't know how many people are standing" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[SITTING]' in submessage: data = submessage.replace('[SITTING]', '') reply = "I don't know how many people are sitting" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[LYING]' in submessage: data = submessage.replace('[LYING]', '') reply = "I don't know how many people are lying" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[HOWOLD]' in submessage: data = submessage.replace('[HOWOLD]', '') try: person_info = json.loads( self.memory.getData('Humans/Peoplesummary')) age = person_info['Age'] if total == 0: reply = "You are older than me, I guess..." else: reply = "I may be wrong, but you could be " + age + " years old. Don't you?" except Exception as e: reply = "You are older than me, I guess..." self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) elif '[GENDER]' in submessage: data = submessage.replace('[GENDER]', '') reply = "Man? Woman? I couldn't tell, sorry!" self.memory.raiseEvent("DialogueVesponse", submessage) self.memory.raiseEvent("Veply", reply) else: print submessage def parse_aiml(self, msg): splitted = msg.split('_') to_send = ' '.join(splitted) reply = self.kernel.respond(to_send) print "AIML response:", reply self.memory.raiseEvent("DialogueVesponse", reply)
print '% Training set:',PERCENTUAL_TRAINING_SET print 'RBF paramaters tested:',RBF_PARAMETER_LIST print 'Lambda KOMD tested:',LAM_PARAMETER_LIST for f in FILE_LIST: print '\n\n\nFILE: ',f sys.stdout.flush() ex = Import_Data(verb=VERBOSE,max_ex=100000) ex.weka_stair(f,binary='False') ex.resizing_data() Yorig = ex.Y[:] PERCENTUAL_TRAIN = PERCENTUAL_TRAINING_SET # % of examples in the training set for r in RBF_PARAMETER_LIST: for l in LAM_PARAMETER_LIST: k = Kernel(ex) RBF_PARAMETER = r # sigma parameter of the RBF #print 'RBF Kernel evaluation with parameter sigma %f ...'%RBF_PARAMETER, k.RBF(RBF_PARAMETER) #print 'Done!' ClassUPauc_list = [] ClassUPacc_list = [] ClassUPrecallp_list = [] ClassUPprecisionp_list = [] auc_list = [] acc_list = [] recallp_list = [] precisionp_list = [] for it in range(ITERATIONS): ex.Y = Yorig[:] idx_train, idx_test = ex.percentual_idx(PERCENTUAL_TRAIN)
def __init__(self, alpha=1.0): Kernel.__init__(self) GenericTests.check_type(alpha,'alpha',float) self.alpha = alpha
): # eventually make this a stopping criteria # Flush the agent population and start over for each simulation. agents = [] # The random state of each symbol needs to be set for each simulation, so the # stocks won't always do the same thing. Note that the entire experiment # should still be fully repeatable with the same initial seed, because the # list of random seeds for a symbol is fixed at the start, based on the initial # seed. for symbol in symbols: symbols[symbol]['random_state'] = get_rand_obj(symbol_seeds[symbol]) # Obtain a fresh simulation Kernel with the next appropriate random_state, seeded # from the list obtained before the first simulation. kernel = Kernel("Base Kernel", random_state=get_rand_obj(kernel_seeds)) # Configure an appropriate oracle for all traded stocks. # All agents requiring the same type of Oracle will use the same oracle instance. # The oracle does not require its own source of randomness, because each symbol # and agent has those, and the oracle will always use on of those sources, as appropriate. oracle = SparseMeanRevertingOracle(mkt_open, mkt_close, symbols) # Create the agents in the same order they were specified in the first configuration # section (outside the simulation loop). It is very important they be in the same # order. agent_id = 0 # Create the exchange. for i in range(num_exch):
def plot(predictor, X, y, grid_size): x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.linspace(x_min, x_max, grid_size), np.linspace(y_min, y_max, grid_size), indexing="ij") flatten = lambda m: np.array(m).reshape(-1) result = [] for (i, j) in itertools.product(range(grid_size), range(grid_size)): point = np.array([xx[i, j], yy[i, j]]).reshape(1, 2) result.append(predictor.predict(point)) Z = np.array(result).reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=cm.Paired, levels=[-0.001, 0.001], extend="both", alpha=0.8) plt.scatter(flatten(X[:, 0]), flatten(X[:, 1]), c=flatten(y), cmap=cm.Paired) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.show() num_samples = 500 num_features = 2 grid_size = 20 samples = np.matrix(np.random.normal(size=num_samples * num_features).reshape(num_samples, num_features)) labels = 2 * (samples.sum(axis=1) > 0) - 1.0 model = SVM(1.0, Kernel.linear()) print samples[0] model.fit(samples, labels) plot(model, samples, labels, grid_size)
def __init__(self, alpha=1.0): Kernel.__init__(self) GenericTests.check_type(alpha, 'alpha', float) self.alpha = alpha
ExperimentalAgent(id=2, name="Experimental_Agent", symbol=symbols[0], starting_cash=10000000, log_orders=log_orders, execution_timestamp=pd.Timestamp("2019-06-19 09:32:00"), quantity=1000, is_buy_order=True, limit_price=50000, random_state=random_state) ] agents.extend(experimental_agents) ####################################################################################################################### # 6) Kernel Parameters kernel = Kernel("Market Replay Kernel", random_state=random_state) kernelStartTime = date_pd + pd.to_timedelta('09:30:00') kernelStopTime = date_pd + pd.to_timedelta('09:35:00') defaultComputationDelay = 0 latency = np.zeros((3, 3)) noise = [0.0] oracle = RandomOrderBookOracle(symbol='AAPL', market_open_ts=mkt_open, market_close_ts=mkt_close, buy_price_range=[9000, 10500], sell_price_range=[9500, 11000], quantity_range=[5000, 50000], seed=seed)
def __str__(self): s=self.__class__.__name__+ "=[" s += "alpha="+ str(self.alpha) s += ", " + Kernel.__str__(self) s += "]" return s
kernelStopTime = midnight + pd.to_timedelta('17:00:00') # This will configure the kernel with a default computation delay # (time penalty) for each agent's wakeup and recvMsg. An agent # can change this at any time for itself. (nanoseconds) defaultComputationDelay = 1000000000 * 5 # five seconds # IMPORTANT NOTE CONCERNING AGENT IDS: the id passed to each agent must: # 1. be unique # 2. equal its index in the agents list # This is to avoid having to call an extra getAgentListIndexByID() # in the kernel every single time an agent must be referenced. ### Configure the Kernel. kernel = Kernel("Base Kernel", random_state=np.random.RandomState( seed=np.random.randint(low=0, high=2**32))) ### Configure the agents. When conducting "agent of change" experiments, the ### new agents should be added at the END only. agent_count = 0 agents = [] agent_types = [] ### How many client agents will there be? num_clients = 10 ### Configure a sum service agent. agents.extend([ SumServiceAgent(0,
from ContinousAllocation import ContinousAllocation from Fits import BestFit from Program import Program from Add import Add from Print import Print from Fifo import Fifo from Paging import Paging memory = Memory(32) mmu = ContinousAllocation(memory, BestFit()) mmu = Paging(memory, 4) hard_disk = HardDisk() cpu = Cpu(None, None, None) scheduling_policy = Fifo() h_kernel = Kernel(cpu, mmu, hard_disk, scheduling_policy) h_kernel = Kernel(cpu, mmu, hard_disk) Log().debug_mode = False shell = SheelH(h_kernel) h_kernel.start() pro = Program('program1') pro.add_instruction(Add(1,2)) pro.add_instruction(Add(1,2)) pro.add_instruction(Add(1,2)) pro.add_instruction(Add(1,2)) pro.add_instruction(Add(1,2)) pro.add_instruction(Add(1,2)) pro.add_instruction(Add(1,2))
def __str__(self): s = self.__class__.__name__ + "=[" s += "alpha=" + str(self.alpha) s += ", " + Kernel.__str__(self) s += "]" return s
def __str__(self): if silent_mode: return '' return "(Agent {} @ {}) : MKT Order {} {} {}".format( self.agent_id, Kernel.fmtTime(self.time_placed), "BUY" if self.is_buy_order else "SELL", self.quantity, self.symbol)
starting_cash=starting_cash, min_size=1, max_size=10, log_orders=log_orders, random_state=np.random.RandomState( seed=np.random.randint(low=0, high=2**32, dtype='uint64'))) for j in range(agent_count, agent_count + num_momentum_agents) ]) agent_count += num_momentum_agents agent_types.extend("MomentumAgent") ######################################################################################################################## ########################################### KERNEL AND OTHER CONFIG #################################################### kernel = Kernel("random_fund_diverse Kernel", random_state=np.random.RandomState( seed=np.random.randint(low=0, high=2**32, dtype='uint64'))) kernelStartTime = historical_date kernelStopTime = mkt_close + pd.to_timedelta('00:01:00') defaultComputationDelay = 50 # 50 nanoseconds # LATENCY latency_rstate = np.random.RandomState( seed=np.random.randint(low=0, high=2**32)) pairwise = (agent_count, agent_count) # All agents sit on line from Seattle to NYC nyc_to_seattle_meters = 3866660
def __init__(self): self.kernel = Kernel() self.kernel.memory.mmap("read_only.txt", Flags.READ_ONLY, Flags.MAP_PRIVATE)
def __init__(self): self.__kernel = Kernel()
# quantity=pov_quantity, # trade=trade, # log_orders=True, # needed for plots so conflicts with others # random_state=np.random.RandomState(seed=np.random.randint(low=0, high=2 ** 32, # dtype='uint64'))) # execution_agents = [pov_agent] # agents.extend(execution_agents) # agent_types.extend("ExecutionAgent") # agent_count += 1 ######################################################################################################################## ########################################### KERNEL AND OTHER CONFIG #################################################### kernel = Kernel("RMSC03 Kernel", random_state=np.random.RandomState( seed=np.random.randint(low=0, high=2**32, dtype='uint64'))) kernelStartTime = historical_date kernelStopTime = mkt_close + pd.to_timedelta('00:01:00') defaultComputationDelay = 0 # 50 nanoseconds #there was 50, doesn't work for MarketReplay as this is history that should be #executed at exact order book time. # LATENCY latency_rstate = np.random.RandomState( seed=np.random.randint(low=0, high=2**32)) pairwise = (agent_count, agent_count) # All agents sit on line from Seattle to NYC