def __init__(self, num_experts, training_x, training_y, test_x, test_y, poly_degree=1, feat_type="polynomial"): self.poly_degree = poly_degree self.feat_type = feat_type self.norm_training_x, self.norm_test_x, self.x_mins, self.x_maxs = normalize_data(training_x, test_x) self.norm_training_y, self.norm_test_y, self.y_mins, self.y_maxs = normalize_data(training_y, test_y) training_x = self.transform_features(training_x) dimension_in = training_x.shape[1] dimension_out = training_y.shape[1] self.gateNet = GaussianGate(num_experts, dimension_in, dimension_out) self.training_iterations = 0 self.experts = list() self.numExperts = num_experts self.bestError = float("inf") for i in range(num_experts): location = np.ones( (dimension_in, 1) ) for j in range(dimension_in): if j == 0: continue maxx = max(training_x[:,j]) minx = min(training_x[:,j]) step = (maxx - minx) / num_experts location[j] = (minx + step/2) + step * i self.experts.append( Expert(dimension_in, dimension_out, location, i) )
def getExpert(): experts = [] with open("../data/process_time_matrix.csv") as f: reader = list(csv.reader(f)) for e in reader[1:]: expert = Expert() expert.id = int(e[0]) for index in range(1, len(e)): if int(e[index]) != 999999: expert.able[index] = int(e[index]) # 根据各个专家最擅长的任务进行排序 # expert.able = sorted(expert.able.items(), key=lambda d: d[1], reverse=False) experts.append(expert) return experts
def growNetwork(self, sortedExperts): transformed_training_x = self.transform_features(self.norm_training_x) for expert in sortedExperts: meanBackup = expert.mean().copy() alphaBackup = self.gateNet.alphas[expert.index].copy() sigmaBackup = self.gateNet.sigma[expert.index].copy() weightsBackup = expert.weights.copy() newExpert = Expert(self.norm_training_x.shape[1], self.norm_training_y.shape[1], expert.location, self.numExperts) newExpert.weights = expert.weights.copy() ###########Update experts according to Ramamurti page 5###################### ############################################################################# firstMean, secondMean = self.gateNet.find_best_means(expert, transformed_training_x, self.norm_training_y) expert.setMean(firstMean) newExpert.setMean(secondMean) newAlpha = np.array([alphaBackup / 2.0]) self.gateNet.alphas = np.vstack( (self.gateNet.alphas, newAlpha) ) self.gateNet.alphas[expert.index] /= 2.0 newSigma = np.array( [sigmaBackup] ) self.gateNet.sigma = np.vstack( (self.gateNet.sigma, newSigma) ) newMean, oldMean = self.gateNet.weighted_2_means(transformed_training_x, self.norm_training_y, newExpert, expert) newExpert.setMean(newMean) expert.setMean(oldMean) self.experts.append(newExpert) ############################################################################## #################### Test new network ######################################## for i in range(5): self.gateNet.train( transformed_training_x, self.norm_training_y, self.experts) error, prediction = self.testMixture(self.norm_test_x, self.norm_test_y) avg_error = sum(error) / len(error) if self.bestError - avg_error > 0.0001: print "Error ", avg_error print "Errors: ", error self._saveParameters(avg_error) self.numExperts += 1 print "Adding new expert!" return True ###### Revert to previous network######## expert.setMean(meanBackup) expert.weights = weightsBackup self.gateNet.alphas[expert.index] = alphaBackup self.gateNet.sigma[expert.index] = sigmaBackup self.experts.remove(newExpert) self.gateNet.sigma = np.delete(self.gateNet.sigma, self.numExperts, 0) self.gateNet.alphas = np.delete(self.gateNet.alphas, self.numExperts, 0) print "Reversing to previous network" return False
def readFileContentExp(self, inFile): """ Reads expert's file content after header, creates an object with the data that was read and puts all the objects into a list. Requires: File Object, with the opened file Ensures: Return of List of Expert Objects """ expertList = ExpertCollection() for line in inFile: name, local, domains, reputation, price, date, hour, total_renum = line.strip( ).split(', ') domains = tuple(domains.strip("(").strip(")").split("; ")) expertList.appendExpert( Expert(name, local, domains, reputation, price, date, hour, total_renum)) return expertList
def __init__(self, dataset): for items in range(1,len(dataset[0])): ex = Expert(dataset[0][items].lettersVector, dataset[1][items].lettersVector) ex.predict(dataset[0][items].lettersVector[3]) #print ex.lastResult ex.predict(dataset[1][items].lettersVector[3]) #print ex.lastResult ex.probability.append(1/float(len(dataset[0]))) # h=w=int(math.sqrt(len(dataset[0][items].lettersVector[3]))) # arr = dataset[0][items].lettersVector[3].reshape(h, w) # toimage(arr).show() self.experts.append(ex) for i in range(1, len(dataset[0])): j = 0 for e in self.experts: j += 1
#!/usr/bin/env python3 from Expert import Expert blackboard = {"inventory": [], "needs": []} print( "\nThe travelers have taken a wrong turn and have ended up lost in The Great Forest of Ashwood. At first they're focused on just finding their way." ) chef = Expert("Chef", blackboard, cooking=0.99) hunter = Expert("Elmer", blackboard, hunting=0.99) lumberjack = Expert("Paul", blackboard, chopping=0.99) survivalist = Expert("Bear", blackboard, survivalism=0.99) experts = [chef, hunter, lumberjack, survivalist] for expert in experts: print("{}'s insistence: {:1.3f}".format(expert.name, expert.getInsistence())) print( "\nIt has been nearly 8 hours since they've seen any sign of the designated trail, and they're beginning to get hungry." ) blackboard["needs"].append("food") lastExpert = None lastInsistence = None while "food" in blackboard["needs"] and "food" not in blackboard["inventory"]: maxInsistence = 0 bestExpert = None
def readExperts(fileName): """ Reads the experts file and converts it in a ExpertsCollection object. Ensures: ExpertsCollection object made up of Experts with the information taken from the experts file. """ fileIn = open(fileName, 'r') filetwo = open(fileName, 'r') lenfile = len(filetwo.readlines()) outputList = [] inExperts = ExpertsCollection() # header always has 7 lines for i in range(7): fileIn.readline() # eliminating all meaningless characters for i in range(lenfile - 7): outputList.append(fileIn.readline().replace("*", "").replace("\n", "").split(",")) # This cycle goes through all experts in the file, one at the time for i in outputList: # The first conditionals make sure the program # can understand the dates, even when the number # starts with 0 # Calculate month if i[5][6] == '0': month = int(i[5][7]) else: month = int(i[5][6:8]) # Calculate day if i[5][9] == '0': day = int(i[5][10]) else: day = int(i[5][9:11]) # Calculate hour if i[6][1] == '0': hour = int(i[6][2]) else: hour = int(i[6][1:3]) # Calculate minute if i[6][4] == '0': minute = int(i[6][5]) else: minute = int(i[6][4:6]) # A temporary Expert object is created with all the information expertTemp = Expert( i[0], i[1][1:], tuple(i[2][1:].replace(";", ",").replace("(", "").replace( ")", "").replace(" ", "").split(",")), int(i[3][1:]), int(i[4][1:]), DateTime(int(i[5][1:5]), month, day, hour, minute), float(i[7][1:])) # The Expert object is then added to the inExperts Collection inExperts.addExpert(expertTemp) fileIn.close() filetwo.close() return inExperts