def __init__(self): """ This is the constructor for the War Class. This initialises: usr_data: this is the list containing all the user data. all are initialised with default values' index value default (0) < player name > Bill Zuckerberg (1) < health point > 5 (2) < attack point > 0 (3) < score > 0 (4) < active magic_cards > 1 SHIELD (5) < all magic cards > 1 MEDIPACK (6) < win counter > 0 (7) < rounds > 0 (8) < Computer Health Point> 7 (9) < Invalid Inputs> [] dir_path: the path where all the files are saved at - //home/user/Document/Lucky_Skills :return : None """ self.usr_data = ['Bill Zuckerberg', 5, 3, 100, 0, [0, 0, 1, 0, 0], 0, 0, 9, []] self.dir_path = '/' + path.expanduser('~') + '/Documents/Lucky_Skills/' self.mc = MagicCards.MagicCard() self.io = CustomIO.CustomIO() self.hp = attribute.Attribute(1) self.chp = attribute.Attribute(8) self.ap = attribute.Attribute(2) self.usr_input = -1 self.tra_input = 0
def test_factory(self): rf = RecordFactory() self.assertRaises(ValueError, rf.generateRecord, (rf, None)) self.assertRaises(ValueError, rf.generateRecord, (rf, "Hello")) self.assertTrue(rf.addAttribute("attr1", attribute.Attribute())) at = attribute.Attribute() at.setValue("hello") self.assertTrue(rf.addAttribute("attr2", at)) at = attribute.Attribute() at.setType(int) assert at.getType() is int self.assertTrue(rf.addAttribute("attr3", at)) rec = rf.generateRecord(None) self.assertIsNotNone(rec) at = rec.getRawAttribute("attr1") self.assertIsNotNone(at) self.assertEqual(at.getType(), str) self.assertIsNone(at.asStr()) at = rec.getRawAttribute("attr2") self.assertIsNotNone(at) self.assertIs(at.getType(), str) self.assertEqual(at.asStr(), "hello") at = rec.getRawAttribute("attr3") self.assertIsNotNone(at) self.assertIs(at.getType(), int) self.assertIsNone(at.asStr()) rec = rf.generateMatchRecord(None) self.assertIsNotNone(rec) at = rec.getRawAttribute("attr1") self.assertIsNotNone(at) self.assertIs(at.getType(), str) self.assertIsNone(at.asStr()) at = rec.getRawAttribute("attr2") self.assertIsNotNone(at) self.assertIs(at.getType(), str) self.assertEqual(at.asStr(), "hello") at = rec.getRawAttribute("attr3") self.assertIsNotNone(at) self.assertIs(at.getType(), str) self.assertRaises(ValueError, rf.setFactoryType, None) rf.setFactoryType(RecordTests.RecordTest) rec = rf.generateRecord(None) self.assertEqual(type(rec), RecordTests.RecordTest)
def _toFactory(*attrs, factoryType=None): """Converts the name and attributes to a factory. Each attribute must be in the form (name, type[, default value]), otherwise this will fail.""" if attrs is None or len(attrs) == 0: raise ValueError("attrs must be len > 0 and not None") fact = record.RecordFactory() if factoryType is not None: fact.setFactoryType(factoryType) for i in attrs: assert len(i) in [2, 3] attr = attribute.Attribute() name = None if len(i) == 3: (name, attrType, attrDefault) = i attr.setType(attrType) attr.setValue(attrDefault) elif len(i) == 2: (name, attrType) = i attr.setType(attrType) if name is None: raise ValueError("Can't assign None name to attribute") fact.addAttribute(name, attr) return fact
def generate_algorithm_info(self, output): """ generate selected algorithm and related training data set information. :return: """ obj = attr.Attribute() self.db_name, self.tb_name = obj.database_info() self.output_attr, self.attr_no, self.attr_list = obj.attribute_info() u_intent = cfg.user["Sensor_name"][0] self.algo["Sensor_name"] = [u_intent] self.algo["Domain"] = [self.db_name, self.tb_name] if (output == 1): self.algo["Algorithm_name"] = self.supervised_algorithm_selection( 1) self.algo["Output_attribute"] = self.output_attr else: self.algo[ "Algorithm_name"] = self.unsupervised_algorithm_selection(0) self.algo["Output_attribute"] = "none" self.algo["Number_of_training_attributes"] = self.attr_no self.algo["Training_attributes"] = self.attr_list print(self.algo, file=sys.stderr) print("Generating selected algorithm info file...", file=sys.stderr) with open('watch/selected_algo.json', 'w') as data_file: json.dump(self.algo, data_file)
def genAttrs(): result = {} for i in range(5): result["attr"+str(i)] = attribute.Attribute() at = result["attr3"] at.setType(int) at = result["attr4"] at.setType(float) return result
def unsupervised_algorithm_selection(self, output): """ determine appropriate algorithm for unsupervised learning problem. :return: selected algorithm name. """ self.output = output self.predict_value = "multiple" obj = attr.Attribute() self.dataset = obj.dataset_information() l = len(cfg.algo["Algorithm"]) for i in range(l): if (cfg.algo["Algorithm"][i]["Prediction_type"][0] == self.predict_value or cfg.algo["Algorithm"][i]["Prediction_type"][1] == self.predict_value): algo_l = len(cfg.algo["Algorithm"][i]["Training_data_type"]) data_l = len(obj.dataset["Attribute_characteristics"]) if (algo_l == 2 and data_l == 2): if (cfg.algo["Algorithm"][i]["Training_data_type"][0] == self.dataset["Attribute_characteristics"][0] and cfg.algo["Algorithm"][i]["Training_data_type"][1] == self.dataset["Attribute_characteristics"][1]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] elif (algo_l == 1 and data_l == 1): if (cfg.algo["Algorithm"][i]["Training_data_type"] == self.dataset["Attribute_characteristics"]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] elif (algo_l == 1 and data_l == 2): if (cfg.algo["Algorithm"][i]["Training_data_type"] == self.dataset["Attribute_characteristics"][0]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] elif (algo_l == 2 and data_l == 1): if (cfg.algo["Algorithm"][i]["Training_data_type"][0] == self.dataset["Attribute_characteristics"]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] return self.algo_name
def main(self): """ Call functions for selecting dataset and algorithm. :return: """ cached_stamp = 0 self.filename = 'watch/user_intention.json' stamp = os.stat(self.filename).st_mtime if stamp != cached_stamp: cached_stamp = stamp print("user intention file updated", file=sys.stderr) obj1 = attr.Attribute() self.learning_problem = obj1.determine_learning_problem() if self.learning_problem is 1: obj1.supervised_attribute_set() else: obj1.unsupervised_attribute_set() obj1.generate_attribute_set() obj2 = alg.Algorithm() obj2.generate_algorithm_info(output=self.learning_problem)
def __init__(self, *args, **kwargs): self.__subject = attribute.Attribute(kwargs.pop('subject', ''), self, self.subjectChangedEvent) self.__description = attribute.Attribute(kwargs.pop('description', ''), self, self.descriptionChangedEvent) self.__fgColor = attribute.Attribute(kwargs.pop('fgColor', None), self, self.foregroundColorChangedEvent) self.__bgColor = attribute.Attribute(kwargs.pop('bgColor', None), self, self.backgroundColorChangedEvent) self.__font = attribute.Attribute(kwargs.pop('font', None), self, self.fontChangedEvent) self.__icon = attribute.Attribute(kwargs.pop('icon', ''), self, self.iconChangedEvent) self.__selectedIcon = attribute.Attribute(kwargs.pop('selectedIcon', ''), self, self.selectedIconChangedEvent) self.__id = kwargs.pop('id', None) or '%s:%s'%(id(self), time.time()) # FIXME: Not a valid XML id # FIXME: When dropping support for python 2.4, use the uuid module super(Object, self).__init__(*args, **kwargs)
def add_attribute(self, str_name, value=None): return self.__attributes.append(attribute.Attribute(str_name, value))
def __getattr__(self, item): if item in rt.dcc.connections.list_attr(self): return at.Attribute('%s.%s' % (self, item)) else: return super(DagNode, self).__getattr__(item)
def connect_attr(self, attribute_source, attribute_destination, **kwargs): self.attr(attribute_source).connect( at.Attribute(attribute_destination), **kwargs)
def add_attr(self, attribute, **kwargs): rt.dcc.connections.add_attr(self, longName=attribute, **kwargs) return at.Attribute(self._resolve_attribute_name(attribute))
def supervised_algorithm_selection(self, output): """ determine appropriate algorithm for supervised learning problem. :return: selected algorithm name. """ obj = attr.Attribute() self.output = output self.x_type = obj.xtype() print(self.x_type, file=sys.stderr) self.unique_value_x = obj.unique_x() print(self.unique_value_x, file=sys.stderr) if (self.x_type == "categorical"): if (self.unique_value_x == 2): self.predict_value = "binary" else: self.predict_value = "multiple" else: self.predict_value = "single" self.dataset = obj.dataset_information() print(self.dataset, file=sys.stderr) l = len(cfg.algo["Algorithm"]) for i in range(l): if (cfg.algo["Algorithm"][i]["Output_dataset"] == self.output): if (cfg.algo["Algorithm"][i]["Output_data_type"] == self.x_type ): if (cfg.algo["Algorithm"][i]["Prediction_type"][0] == self.predict_value or cfg.algo["Algorithm"][i]["Prediction_type"][1] == self.predict_value): algo_l = len( cfg.algo["Algorithm"][i]["Training_data_type"]) data_l = len(self.dataset["Attribute_characteristics"]) if (algo_l == 2 and data_l == 2): if (cfg.algo["Algorithm"][i]["Training_data_type"] [0] == self. dataset["Attribute_characteristics"][0] and cfg.algo["Algorithm"][i] ["Training_data_type"][1] == self. dataset["Attribute_characteristics"][1]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] elif (algo_l == 1 and data_l == 1): if (cfg.algo["Algorithm"][i]["Training_data_type"] == self.dataset["Attribute_characteristics"]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] elif (algo_l == 1 and data_l == 2): if (cfg.algo["Algorithm"][i]["Training_data_type"] == self. dataset["Attribute_characteristics"][0]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] elif (algo_l == 2 and data_l == 1): if (cfg.algo["Algorithm"][i]["Training_data_type"] [0] == self.dataset["Attribute_characteristics"]): self.algo_name = cfg.algo["Algorithm"][i][ "Algorithm_name"] return self.algo_name
#!/usr/bin/env python import os import sys import time import signal import subprocess MAINPATH = os.path.dirname(os.path.abspath(__file__)) + "/../" sys.path.append(MAINPATH + 'lib/jmx') import attribute import reporter import jmxdaemon import crawl if __name__ == '__main__': replist = reporter.ReporterList() attr = attribute.Attribute() attr.load(MAINPATH + "etc/jmx.conf") replist.config(attr) attr.set("jmxcmd", MAINPATH + 'lib/jmx/jmxclient.jar') daemon = jmxdaemon.JMXDaemon(replist, attr) daemon.config(attr) while True: daemon.scan() time.sleep(10)
PutIn = lambda x, y: action.MoveItemAction('put', x, 'in', y) TakeFrom = lambda x, y: action.MoveItemAction('take', x, 'from', y) Search = lambda x: action.SingleAction('search', x ) # TODO: Create informative action Ask = lambda x: action.SingleAction('ask', x) Talk = lambda x: action.SingleAction('talk to', x) SayTo = lambda x, y: action.DoubleAction('say', x, 'to', y) Kiss = lambda x: action.SingleAction('kiss', x) Bribe = lambda x: action.SingleAction('bribe', x) BuyFrom = lambda x, y: action.MoveItemAction('buy', x, 'from', y) Attack = lambda x: action.SingleAction('attack', x) AttackWith = lambda x, y: action.DoubleAction('attack', x, 'with', y) Kill = lambda x: action.SingleAction('kill', x) KillWith = lambda x, y: action.DoubleAction('kill', x, 'with', y) # Global Entity Attributes Portable = attribute.Attribute('portable', [Take, Drop, GiveTo, PutIn, TakeFrom]) Edible = attribute.Attribute('edible', [Eat, Drink, Swallow, Consume]) Moveable = attribute.Attribute('moveable', [Move, Push, Pull, Drag, Lift]) Switchable = attribute.Attribute('switchable', [TurnOn, TurnOff]) Flammable = attribute.Attribute('flammable', [Light, Extinguish]) Openable = attribute.Attribute('openable', [Open, Close]) Lockable = attribute.Attribute('lockable', [Lock, Unlock, LockWith, UnlockWith]) # TODO: An Openable object may be a container. We should have logic to check for containment Container = attribute.Attribute('container', [PutIn, TakeFrom, Search]) Person = attribute.Attribute('person', [Ask, Talk, SayTo, Kiss, Bribe, GiveTo, BuyFrom]) Enemy = attribute.Attribute('enemy', [Attack, AttackWith, Kill, KillWith])
def attr(self, attribute): return at.Attribute(self._resolve_attribute_name(attribute))
def main(): print "1 : Breast Cancer dataset" print "2 : Car Evaluation dataset" print "3 : Student Performance dataset" print "4 : Balloon dataset" print "5 : Balance Scale dataset" user_input = input("Enter choice of dataset: ") #Reading the control file and associated data. if user_input == 0: ctrl_file = open("sample-dataset/control-file.txt", "r") data = sample_main.reading_data() elif user_input == 1: ctrl_file = open("breast-cancer-dataset/control-file.txt", "r") data = cancer_main.reading_data() elif user_input == 2: ctrl_file = open("car-evaluation-dataset/control-file.txt", "r") data = car_main.reading_data() elif user_input == 3: ctrl_file = open("student-performance-dataset/control-file.txt", "r") data = student_main.reading_data() elif user_input == 4: ctrl_file = open("balloon-dataset/control-file.txt", "r") data = balloon_main.reading_data() elif user_input == 5: ctrl_file = open("balance-scale-dataset/control-file.txt", "r") data = balance_main.reading_data() # list of all the attributes with their properties. attr_list = [] for line in ctrl_file: line = line.strip('\n') words = line.split(" ") if (line[0] != "#"): num = words[0] name = words[1] num_of_vals = words[2] obj = attribute.Attribute(num, name, num_of_vals) for x in words[3:]: obj.adding_values(x) attr_list.append(obj) #copying target attribute from list of attributes seperately and removing it from the total list target_attr = attribute.Attribute(attr_list[-1].num, attr_list[-1].name, attr_list[-1].num_of_vals) for y in attr_list[-1].vals: target_attr.adding_values(y) print "Target attribute details:", target_attr.printvals() del attr_list[-1] datasets = [[]] valid_data_len = len(data) * 1 / 10 # dividing data into 10 sets. j = 0 for i in range(1, 11): dat = [] for d in range(j, j + valid_data_len): dat.append(data[d]) j = j + 1 i = i + 1 datasets.append(dat) datasets.remove([]) #print "it should be 10: ", len(datasets) accuracy = [] errors = [] sizes = [] num = 1 for d in datasets: print "***********Iteration", num, "***********" num += 1 test_data = d t_data = [] for left_d in datasets: if left_d != d: t_data = t_data + left_d #print "test data: ", len(test_data) #dividing data into training and pruning datasets. train_data_len = len(t_data) * 6 / 10 prune_data = [] train_data = [] for d in range(0, train_data_len): train_data.append(t_data[d]) for d in range(train_data_len, len(t_data)): prune_data.append(t_data[d]) #print "train data: ", len(train_data) #print "prune data: ", len(prune_data) #calling ID3 algorithm. dec_tree = decision_tree.Node() dec_tree = ID3_algo.ID3_algorithm(train_data, attr_list, target_attr, 0) # printing the decision tree before pruning. #print "Before pruning:" printing_final_tree.print_tree(dec_tree, target_attr.getvals()[0]) #implementing pruning. #prune_tree = pruning.prune_tree(dec_tree, t_data, target_attr.getvals()[0]) # printing the decision tree after pruning. #print "After pruning:" #printing_final_tree.print_tree(prune_tree,target_attr.getvals()[0]) #implementing testing. mylist = testing.testing(dec_tree, test_data, attr_list, target_attr) print "Accuracy of tree: ", mylist[0] * 100 sz = find_size.size(dec_tree, 0) #print "Size of tree(after pruning): ", sz sizes.append(sz) accuracy.append(mylist[0]) errors.append(mylist[1]) #finding accuracy and confidence interval. print "***********Statistics***********" sum_acc = 0.0 for acc in accuracy: #print acc*100 sum_acc += (acc * 100) #print sum_acc mean_acc = sum_acc / len(accuracy) print "Mean accuracy: ", mean_acc print "Mean Error rate: ", 100 - mean_acc sum_err = 0.0 for err in errors: #print err sum_err += err mean_err = sum_err / len(accuracy) print "Mean Size of tree (before pruning):", max(sizes) print "Mean Size of tree (after pruning):", min(sizes) print "Confidence Interval: [", (mean_acc - 1.96 * mean_err), " , ", ( mean_acc + 1.96 * mean_err), "]"
def main(): print "1 : Breast Cancer dataset" print "2 : Car Evaluation dataset" print "3 : Student Performance dataset" print "4 : Balloon dataset" print "5 : Balance Scale dataset" user_input = input("Enter choice of dataset: ") #Reading the control file and associated data. if user_input == 0: ctrl_file = open("sample-dataset/control-file.txt","r") data = sample_main.reading_data() elif user_input == 1: ctrl_file = open("breast-cancer-dataset/control-file.txt","r") data = cancer_main.reading_data() elif user_input == 2: ctrl_file = open("car-evaluation-dataset/control-file.txt","r") data = car_main.reading_data() elif user_input == 3: ctrl_file = open("student-performance-dataset/control-file.txt","r") data = student_main.reading_data() elif user_input == 4: ctrl_file = open("balloon-dataset/control-file.txt","r") data = balloon_main.reading_data() elif user_input == 5: ctrl_file = open("balance-scale-dataset/control-file.txt","r") data = balance_main.reading_data() # list of all the attributes with their properties. attr_list = [] for line in ctrl_file: line = line.strip('\n') words = line.split(" ") if(line[0] != "#"): num = words[0] name = words[1] num_of_vals = words[2] obj = attribute.Attribute(num, name, num_of_vals) for x in words[3:]: obj.adding_values(x) attr_list.append(obj) #copying target attribute from list of attributes seperately and removing it from the total list target_attr = attribute.Attribute(attr_list[-1].num, attr_list[-1].name, attr_list[-1].num_of_vals) for y in attr_list[-1].vals: target_attr.adding_values(y) print "Target attribute details:", target_attr.printvals() del attr_list[-1] pat = create_pattern.pattern(data, attr_list, target_attr) # myNN = neural_network.NN(5, 10, 2) # myNN.train(f_pat) datasets = [[]] valid_data_len = len(pat)*1/10 # dividing data into 10 sets. j=0 for i in range(1,11): dat = [] for d in range(j,j+valid_data_len): dat.append(pat[d]) j=j+1 i=i+1 datasets.append(dat) datasets.remove([]) #print "it should be 10: ", len(datasets) accuracy = [] errors = [] # sizes = [] num = 1 for d in datasets: print "***********Iteration", num, "***********" num += 1 test_data = d train_data = [] for left_d in datasets: if left_d != d: train_data = train_data + left_d train_pat = [] test_pat = [] for p in train_data: train_pat.append(p) for p in test_data: test_pat.append(p) myNN = neural_network.NN(len(attr_list), 6, int(attribute.Attribute.getvals(target_attr)[2])) myNN.train(train_pat) tested_pat = myNN.test(test_pat) #frac = 1.0/int(attribute.Attribute.getvals(target_attr)[2]) acc = 0.0 for t in tested_pat: res = t[0] res_now = [] for r in res: if r < 0.5: res_now.append(0.0) else: res_now.append(1.0) cnt = 0 for i in range(0,len(res_now)): if res_now[i] == t[1][i]: cnt = cnt+1 if cnt == len(res_now): acc = acc+1 #print res_now , " compare to ", t[1] accu = acc/len(tested_pat) accuracy.append(accu) errors.append(1.0-accu) # # # # #finding accuracy and confidence interval. print "***********Statistics***********" sum_acc = 0.0 for acc in accuracy: print acc*100 sum_acc += (acc*100) #print sum_acc mean_acc = sum_acc/len(accuracy) print "Mean accuracy: ", mean_acc print "Mean Error rate: ", 100-mean_acc sum_err = 0.0 for err in errors: #print err sum_err += err mean_err = sum_err/len(accuracy) #print "Mean Size of tree (before pruning):", max(sizes) #print "Mean Size of tree (after pruning):", min(sizes) print "Confidence Interval on Accuracy: [",(mean_acc-1.96*mean_err)," , ",(mean_acc+1.96*mean_err) , "]"