def set_hyperparameters(self,mode='replicate'): if mode in ['pp_impact', 'we_impact']: self._preprocess_text = lambda x: pp_without_duplicate_quote(x) elif mode == 'pp_impact_20_split': self._preprocess_text = lambda x: pp_without_duplicate_quote(x) self.validation_split = 0.2 elif mode == 'pt_1': self.nb_epoch = 5 self._preprocess_text = lambda x: pp(x) elif mode == 'pt_2': self._preprocess_text = lambda x: pp(x) self.nb_epoch = 5 self.embedding_dim = 400 self.conv_filter_dim = 1000 elif mode == 'ru_ns': self._preprocess_text = lambda x: rus_mystem(x) self.nb_epoch = 5 self.conv_filter_dim = 300 self.optimizer = 'rmsprop' elif mode == 'ru_word': self.nb_epoch = 5 self.conv_filters = [3,5,8,12] self.optimizer = 'rmsprop' self.embedding_dim = 300 elif mode == 'ru_char': self.conv_filters = [2,3,5,7,9,11] self.conv_filter_dim = 100 self.embedding_dim = 100 self.optimizer = 'rmsprop' self.nb_epoch = 20 self.char_level = True elif mode == 'char': self._preprocess_text = lambda x: pp(x) self.conv_filters = [2,3,5,7,9,11] self.conv_filter_dim = 100 self.embedding_dim = 100 self.optimizer = 'rmsprop' self.nb_epoch = 20 self.char_level = True elif mode == 'word': self._preprocess_text = lambda x: pp(x) self.conv_filters = [2,3,5] self.conv_filter_dim = 100 self.embedding_dim = 200 self.optimizer = 'sgd' self.nb_epoch = 20 self.char_level = False else: self._preprocess_text = lambda x: pp_with_duplicate_quote(x)
def predict_vm(ecs_lines, input_lines): # Do your work from here# result = [] if ecs_lines is None: print('ecs information is none') return result if input_lines is None: print('input file information is none') return result #训练第一天的0点 预测第一天的0点 ecs_first_date_zero = ecs_lines[0].split( "\t")[2].strip().split()[0] + " " + "00:00:00" ecs_first_date_zero = time.strptime(ecs_first_date_zero, "%Y-%m-%d %H:%M:%S") input_first_date_zero = input_lines[-2].strip().split( )[0] + " " + "00:00:00" input_first_date_zero = time.strptime(input_first_date_zero, "%Y-%m-%d %H:%M:%S") kuadu_first_last_day = (time.mktime(input_first_date_zero) - time.mktime(ecs_first_date_zero)) / nd kuadu_first_last_day = int(kuadu_first_last_day) #print(u"时间跨度为"+str(kuadu_first_last_day)) # 行数为虚拟机的规格 列数是天数 ecs_xunlian = [] for i in range(30): t = [] for j in range(kuadu_first_last_day): t.append(0) ecs_xunlian.append(t) for ecs_line in ecs_lines: ecs_line = ecs_line.strip() ecs_now_name = ecs_line.split("\t")[1] ecs_now_no = int(ecs_now_name.replace("flavor", "")) ecs_now_date = ecs_line.split("\t")[2] ecs_now_date = time.strptime(ecs_now_date, "%Y-%m-%d %H:%M:%S") #print(ecs_now_name+" "+str(ecs_now_no)+" "+str(ecs_now_date)) ecs_kuadu_first_now_day = (time.mktime(ecs_now_date) - time.mktime(ecs_first_date_zero)) / nd ecs_kuadu_first_now_day = int(ecs_kuadu_first_now_day) ecs_xunlian[ecs_now_no][ecs_kuadu_first_now_day] = ecs_xunlian[ ecs_now_no][ecs_kuadu_first_now_day] + 1 ''' print(u"训练集中的数据") print(len(ecs_xunlian)) print(len(ecs_xunlian[0])) temp = pymatrix.matrix(ecs_xunlian) print(temp) ''' # 获得要预测的规格 input_num = int(input_lines[2].strip()) input_nos = [] for i in range(input_num): input_line = input_lines[i + 3] input_no = input_line.strip().split()[0].replace("flavor", "") input_no = int(input_no) input_nos.append(input_no) train_data = fluth_data.quZao2(ecs_xunlian, input_nos) ''' print(u"===去噪之后===") for t in train_data: print(t) print(len(train_data)) print(len(train_data[0])) print("==============") ''' #预测的第一天 预测的最后一天 input_first_date = input_lines[-2].strip() input_first_date = time.strptime(input_first_date, "%Y-%m-%d %H:%M:%S") input_last_date = input_lines[-1].strip() input_last_date = time.strptime(input_last_date, "%Y-%m-%d %H:%M:%S") kuadu_first_last_day = (time.mktime(input_last_date) - time.mktime(input_first_date)) / nd kuadu_first_last_day = int(kuadu_first_last_day) #print(u"时间跨度为:"+str(kuadu_first_last_day)) # 对数据进行预处理 train_data = preprocess.pp(train_data, kuadu_first_last_day) ''' print(u"===预处理之后之后===") for t in train_data: print(t) print(len(train_data)) print(len(train_data[0])) print("==============") ''' mm = pymatrix.matrix(train_data) mm = mm.transpose() #print(u'===GM算法的输入值===') print(mm) #print(mm.colAt(5)) #print(mm.colAt(7)) res = GM11.yuce(2, mm) #print(res) # 获得要预测的规格 yuce_flvorname_flavornum_s = {} input_num = int(input_lines[2].strip()) for i in range(input_num): input_line = input_lines[i + 3] input_line_name = input_line.split(" ")[0] yuce_flvorname_flavornum_s[input_line_name] = int(round(res[i])) print(yuce_flvorname_flavornum_s) ###################### # 开始装箱 ##################### # input文件中的物理机的规格 input_wuLiJi_cpu = int(input_lines[0].split(" ")[0]) input_wuLiJi_mem = int(input_lines[0].split(" ")[1]) #input文件中需要预测的虚拟机[("flavor1",12,1024),("flavor1",12,1024)] input_xuNiJis = [] i = 3 while (i < 3 + int(input_lines[2].strip())): input_now_flavor_name = input_lines[i].strip().split(" ")[0] input_now_flavor_CPU = int(input_lines[i].strip().split(" ")[1]) input_now_flavor_MEM = int(input_lines[i].strip().split(" ")[2]) input_now_flavor = [ input_now_flavor_name, input_now_flavor_CPU, input_now_flavor_MEM ] input_xuNiJis.append(input_now_flavor) i = i + 1 input_xuNiJis.reverse() cpus = [] mems = [] names = [] for input_xuNiJi in input_xuNiJis: input_xuNiJi_name = input_xuNiJi[0] input_xuNiJi_CPU = input_xuNiJi[1] input_xuNiJi_MEM = input_xuNiJi[2] / 1024 nums = yuce_flvorname_flavornum_s[input_xuNiJi_name] for i in range(nums): cpus.append(input_xuNiJi_CPU) mems.append(input_xuNiJi_MEM) names.append(input_xuNiJi_name) # 优化的维度 input_weidu = input_lines[3 + int(input_lines[2].strip()) + 1].strip() if (input_weidu == "CPU"): zx_wuLiJis, _ = memory_alloction.get_memory_allocation( input_wuLiJi_cpu, input_wuLiJi_mem, cpus, mems, names) elif (input_weidu == "MEM"): zx_wuLiJis, _ = memory_alloction.get_memory_allocation( input_wuLiJi_mem, input_wuLiJi_cpu, mems, cpus, names) else: print("出错了,别玩了") print(zx_wuLiJis) #开始输出结果 result = [] sum = 0 for input_xuNiJi in input_xuNiJis: input_xuNiJi_name = input_xuNiJi[0] input_xuNiJi_num = yuce_flvorname_flavornum_s.get(input_xuNiJi_name) sum = sum + input_xuNiJi_num result.append(input_xuNiJi_name + " " + str(input_xuNiJi_num)) result.insert(0, str(sum)) result.append("") result.append(len(zx_wuLiJis)) print(result) for i in range(0, len(zx_wuLiJis), 1): zx_wuLiJi = zx_wuLiJis[i] sss = str(i + 1) temp = {} for xuNiJi in zx_wuLiJi: if (temp.has_key(xuNiJi)): temp[xuNiJi] = temp[xuNiJi] + 1 else: temp[xuNiJi] = 1 print(temp) for name in temp: sss = sss + " " + name + " " + str(temp[name]) print(sss) result.append(sss.strip()) return result
cosClassList[2] += 1 cosNum = cosClassList.index(max(cosClassList)) + 1 if (cosNum == int(test_y[ii])): cosAccNum += 1 euAcc = float(euAccNum) / len(test_x) cosAcc = float(cosAccNum) / len(test_x) return euAcc, cosAcc if __name__ == "__main__": knn = KNN() with open("./data.txt", "r") as f: data = f.readlines() for i in range(len(data)): data[i] = data[i].replace("\n", "").split(",") with open("./target.txt", "r") as e: target = e.readlines() for i in range(len(target)): target[i] = target[i].replace("\n", "").split(",") train_x, test_x, train_y, test_y = pp().train_test_split(data, target, test_size=0.1) train_x = np.array(train_x, dtype=np.float32) train_y = np.array(train_y, dtype=np.int) test_x = np.array(test_x, dtype=np.float32) test_y = np.array(test_y, dtype=np.int) euAcc, cosAcc = knn.test(train_x, train_y, test_x, test_y, 20) print u"欧式距离结果", euAcc print u"余弦距离结果", cosAcc
def predict_vm(ecs_lines, input_lines): # Do your work from here# result = [] if ecs_lines is None: #print ('ecs information is none') return result if input_lines is None: #print ('input file information is none') return result #### 去掉所有的空行 ecs_lines_new = [] for ecs_line in ecs_lines: if(ecs_line.strip()!=""): ecs_lines_new.append(ecs_line) input_lines_new = [] for input_line in input_lines: if(input_line.strip()!=""): input_lines_new.append(input_line) # 获得要预测的规格 input_num = int(input_lines_new[1].strip()) #训练第一天的0点 预测第一天的0点 ecs_first_date_zero = ecs_lines_new[0].split("\t")[2].strip().split()[0]+" "+"00:00:00" ecs_first_date_zero = time.strptime(ecs_first_date_zero, "%Y-%m-%d %H:%M:%S") input_first_date_zero = input_lines_new[6+input_num].strip().split()[0]+" "+"00:00:00" input_first_date_zero = time.strptime(input_first_date_zero, "%Y-%m-%d %H:%M:%S") kuadu_first_last_day = (time.mktime(input_first_date_zero)-time.mktime(ecs_first_date_zero))/nd kuadu_first_last_day = int(kuadu_first_last_day) ##print(u"时间跨度为"+str(kuadu_first_last_day)) # 行数为虚拟机的规格 列数是天数 ecs_xunlian = [] for i in range(30): t = [] for j in range(kuadu_first_last_day): t.append(0) ecs_xunlian.append(t) for ecs_line in ecs_lines_new: ecs_line = ecs_line.strip() ecs_now_name = ecs_line.split("\t")[1] ecs_now_no = int(ecs_now_name.replace("flavor","")) ecs_now_date = ecs_line.split("\t")[2] ecs_now_date = time.strptime(ecs_now_date, "%Y-%m-%d %H:%M:%S") #print(ecs_now_name+" "+str(ecs_now_no)+" "+str(ecs_now_date)) ecs_kuadu_first_now_day = (time.mktime(ecs_now_date)-time.mktime(ecs_first_date_zero))/nd ecs_kuadu_first_now_day = int(ecs_kuadu_first_now_day) ecs_xunlian[ecs_now_no][ecs_kuadu_first_now_day] = ecs_xunlian[ecs_now_no][ecs_kuadu_first_now_day] + 1 ''' print(u"训练集中的数据") print(len(ecs_xunlian)) print(len(ecs_xunlian[0])) temp = pymatrix.matrix(ecs_xunlian) print(temp) ''' # 获得要预测的规格 input_num = int(input_lines_new[1].strip()) input_nos = [] for i in range(input_num): input_line = input_lines_new[i+2] input_no = input_line.strip().split()[0].replace("flavor","") input_no = int(input_no) input_nos.append(input_no) train_data = fluth_data.quZao2(ecs_xunlian,input_nos) # 增加一列0 train_data = preprocess.pp2(train_data) ''' print("======增加0之后=======") for t in train_data: print(t) print(len(train_data)) print(len(train_data[0])) ''' ###########获得预测跨度#################### #预测的第一天 预测的最后一天 input_first_date = input_lines_new[-2].strip() input_first_date = time.strptime(input_first_date, "%Y-%m-%d %H:%M:%S") input_last_date = input_lines_new[-1].strip() input_last_date = time.strptime(input_last_date, "%Y-%m-%d %H:%M:%S") kuadu_first_last_day_2 = (time.mktime(input_last_date)-time.mktime(input_first_date))/nd kuadu_first_last_day_2 = int(kuadu_first_last_day_2) #print(u"预测时间跨度为:"+str(kuadu_first_last_day_2)) ########################################### temp = math.ceil(kuadu_first_last_day_2/2.0) #print("temp="+str(temp)) #分片 train_data = preprocess.pp(train_data,temp) ''' print(u"========分片之后============") for t in train_data: print(t) print(len(train_data)) print(len(train_data[0])) ''' ##########开始预测############################ yuce_flvorname_flavornum_s = {} s1 = [0]*len(train_data[0]) s2 = [0]*len(train_data[0]) s3 = [0]*len(train_data[0]) alpha = [0.6]*30 for i in range(len(train_data)): #print(u"====规格"+str(i+1)+"=======") t = train_data[i] s1[0] = t[1] for j in range(1,len(s1),1): s1[j] = alpha[i]*train_data[i][j]+(1-alpha[i])*s1[j-1] s2[0] = s1[1] for j in range(1,len(s2),1): s2[j] = alpha[i]*s1[j]+(1-alpha[i])*s2[j-1] s3[0] = s2[1] for j in range(1,len(s3),1): s3[j] = alpha[i]*s2[j]+(1-alpha[i])*s3[j-1] #print("s1= "+str(s1)) #print("s2= "+str(s2)) #print("s3= "+str(s3)) a = 3.0 * s1[-1] - 3 * s2[-1] + s3[-1] b = alpha[i]/(2.0*(1-alpha[i])**2) * ((6-5 * alpha[i])*s1[-1] - 2*(5 - 4 * alpha[i])*s2[-1] + (4 - 3 * alpha[i]) * s3[-1]) c = alpha[i]**2/(2*((1-alpha[i])**2)) * (s1[-1] - 2*s2[-1] + s3[-1]) #print("a= "+str(a)) #print("b= "+str(b)) #print("c= "+str(c)) yuce1 = a + b + c yuce2 = (a + 2 * b + 4 * c) * ((temp-1)/temp) yuce = yuce1 + yuce2 yuce = round(yuce) if yuce < 0: yuce = 0 #print(u"预测值为 "+str(yuce)) name = "flavor"+str(i+1) yuce_flvorname_flavornum_s[name] = int(yuce) ###############获得虚拟机名和预测台数的对应#################### ###################################################### ###################获得names cpus mems这三个序列###################### cpus = [] mems = [] names = [] #input文件中需要预测的虚拟机[("flavor1",12,1024),("flavor1",12,1024)] input_xuNiJis = [] i = 2 while(i<2 + input_num): input_now_flavor_name = input_lines_new[i].strip().split(" ")[0] input_now_flavor_CPU = int(input_lines_new[i].strip().split(" ")[1]) input_now_flavor_MEM = int(input_lines_new[i].strip().split(" ")[2])/1024 input_now_flavor = [input_now_flavor_name,input_now_flavor_CPU,input_now_flavor_MEM] input_xuNiJis.append(input_now_flavor) nums = yuce_flvorname_flavornum_s[input_now_flavor_name] for j in range(nums): names.append(input_now_flavor_name) cpus.append(input_now_flavor_CPU) mems.append(input_now_flavor_MEM) i = i + 1 #print(names) #print(mems) #print(cpus) ################# 获得物理机的规格############### input_wuLiJi_cpu = int(input_lines_new[0].split(" ")[0]) input_wuLiJi_mem = int(input_lines_new[0].split(" ")[1]) ########################获得物理机的规格######### ##################### 优化的维度 装箱########################### input_weidu = input_lines_new[-3].strip() if(input_weidu=="CPU"): zx_wuLiJis,_ = memory_alloction.get_memory_allocation(input_wuLiJi_cpu,input_wuLiJi_mem,cpus,mems,names) elif(input_weidu=="MEM"): zx_wuLiJis,_ = memory_alloction.get_memory_allocation(input_wuLiJi_mem,input_wuLiJi_cpu,mems,cpus,names) else: pass #print("出错了,别玩了") #print(zx_wuLiJis) #开始输出结果 result=[] sum_t = 0 for input_xuNiJi in input_xuNiJis: input_xuNiJi_name = input_xuNiJi[0] input_xuNiJi_num = yuce_flvorname_flavornum_s.get(input_xuNiJi_name) sum_t = sum_t + input_xuNiJi_num result.append(input_xuNiJi_name+" "+str(input_xuNiJi_num)) result.insert(0,str(sum_t)) result.append("") result.append(len(zx_wuLiJis)) #print(result) for i in range(0,len(zx_wuLiJis),1): zx_wuLiJi = zx_wuLiJis[i] sss = str(i+1) temp = {} for xuNiJi in zx_wuLiJi : if(temp.has_key(xuNiJi)): temp[xuNiJi] = temp[xuNiJi] + 1 else: temp[xuNiJi] = 1 #print(temp) for name in temp: sss = sss+" "+name+" "+str(temp[name]) #print(sss) result.append(sss.strip()) return result