def __init__(self, m, row_symbols=None, column_symbols=None): ''' m will be transformed into a matrix using 'pymatrix.matrix'. row_symbols and column_symbols can be of any sequence type. ''' self._matrix = (m if isinstance(m, pymatrix.Matrix) else pymatrix.matrix(m)) self.row_symbols = (row_symbols if row_symbols else range(self._matrix.numcols)) self.column_symbols = (column_symbols if column_symbols else range(self._matrix.numrows))
def quZao2(arr,input_nos): train_data=[] M = pymatrix.matrix(arr) M = M.transpose() for input_no in input_nos: temp = M.rowAt(input_no+1) #print(input_no,temp,type(temp)) temp = quZao(temp) #print(temp) train_data.append(temp) #print(M) return train_data
def quZao2(arr, input_nos): train_data = {} M = pymatrix.matrix(arr) for input_no in input_nos: temp = M.rowAt(input_no + 1) #print(input_no,temp,type(temp)) temp = fluth_data(temp) #print(temp) train_data[input_no] = temp #print(M) return train_data
def mlp(input): hiddenNeuron = 4 learningRate = 0.6 epoch = 3000 maxMSE = 10 ^ -6 #define W1 W1 = matrix([]) for i in range(0, input): temp = [] for j in range(0, hiddenNeuron): temp.append(1) W1.append(temp) return W1
def yuce(N, train_data): #train_data = pymatrix.matrix(train_data_list) #print(train_data) # size[0]是行数 size[1]是列数 seq_len, flavor_num = train_data.shape() #print(seq_len,flavor_num) res = [] for i in range(1, flavor_num + 1, 1): #print(u"===规格"+str(i)+"===") seq_fi = train_data.colAt(i) #print(u"原始序列"+str(seq_fi)) seq_fi = cumsum(seq_fi) #print(u"累加序列"+str(seq_fi)) if (seq_fi[-1] == 0): res.append(0) print(res) continue #print(seq_fi) z = [[0]] * (seq_len - 1) #print(u"白化之前:"+str(z)) for j in range(0, seq_len - 1, 1): z[j] = [0.5 * (float(seq_fi[j]) + float(seq_fi[j + 1]))] #print(u"白化背景之后: "+str(len(z))+str(z)) z = pymatrix.matrix(z) print("z=" + str(z)) z = z.numberSubMatrix(0) print(u"-z=" + str(z)) B = [[1]] * (seq_len - 1) B = pymatrix.matrix(B) B = z.heBing(B) #print(u"合并矩阵之后:"+ str(B)) u = B.transpose() * B #print(u"B的转置 "+str(B.transpose())) #print(u"B原来 "+str(B)) u = u.inverse() u = u * B.transpose() #print(u) #print(train_data) #print(u"子矩阵 "+str(train_data.subMatrix(2,seq_len,i,i))) u = u * (train_data.subMatrix(2, seq_len, i, i)) #print("u= "+str(u)) u1 = u[0][0] u2 = u[1][0] #print(u1,u2) left = train_data[0][i - 1] - u2 / u1 #print("left= "+str(left)) #print("u1= "+str(u1)) right = u2 / u1 forecast1 = [] for t in range(seq_len - 1, seq_len + N, 1): forecast1.append(t) for i in range(0, len(forecast1), 1): forecast1[i] = (-u1) * forecast1[i] for i in range(0, len(forecast1), 1): forecast1[i] = math.exp(forecast1[i]) for i in range(0, len(forecast1), 1): forecast1[i] = left * forecast1[i] for i in range(0, len(forecast1), 1): forecast1[i] = forecast1[i] + right #print(forecast1[-1]-forecast1[0]) print(forecast1) rrr = abs(round(forecast1[-1] - forecast1[0])) if (rrr > 40): rrr = 40 res.append(int(rrr)) print(res) #exchange=[] #for i in range(1,len(forecast1),1): # exchange.append(forecast1[i]-forecast1[i-1]) return res
# Pyboard hardware I2C i2c = machine.I2C(1) time.sleep(2) imu = BNO055(i2c) time.sleep(2) calibrated = False start_time = time.ticks_ms()/1000 initial_x = imu.lin_acc()[0] initial_y = imu.lin_acc()[1] initial_z = imu.lin_acc()[2] x = pym.matrix([[initial_x],[0],[0],\ [initial_y],[0],[0],\ [initial_z],[0],[0]]) KF = KalmanFilter(x) pos = Pos(start_time, initial_x, initial_y) iterations = 1000 i = 0 while i<iterations: time.sleep(.01) update_time = time.ticks_ms()/1000 acc_x = imu.lin_acc()[0] acc_y = imu.lin_acc()[1]
def test_auto_matrix(self): assert M(pymatrix.matrix('0')) == M('0')
import pymatrix arr = [[1, 2, 3], [2, 3, 4], [123, 12, 124], [12, 34, 22], [123132, 1234, 43]] mm = pymatrix.matrix(arr) for row in mm.rows(): print(row) print(mm) print("==================") print(mm.subMatrix(2, 3, 1, 3)) arr = [ 0, 0, 0, 0, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0 ] print(len(arr)) m1 = [[1.1, 2.5], [3, 4]] m1 = pymatrix.matrix(m1) mm = m1.inverse() print(mm) print(mm * m1) m2 = [[5, 6], [7, 8]] m2 = pymatrix.matrix(m2) m3 = m1 * m2 print(m3)
def yuce(N,train_data): #train_data = pymatrix.matrix(train_data_list) #print(train_data) # size[0]是行数 size[1]是列数 seq_len= len(train_data) #print(seq_len,flavor_num) # train_data是预测结果,数组的数组,第i个数组是规格i个预测结果 # train_data = [] old_train_data = train_data[:] print("oooooooooooooooooo",train_data) train_data = cumsum(train_data) print("SSSSSSSSSSSSS",train_data) res = 0 if(train_data[-1]==0): return 0 #print(train_data) z = [[0]]*(seq_len-1) #print(u"白化之前:"+str(z)) for j in range(0,seq_len-1,1): z[j] = [0.5*(train_data[j]+train_data[j+1])] #print(u"白化背景之后: "+str(len(z))+str(z)) z = pymatrix.matrix(z) z = z.numberSubMatrix(0) #print(u"-z="+str(z)) B = [[1]]*(seq_len-1) B = pymatrix.matrix(B) B = z.heBing(B) #print(u"合并矩阵之后:"+ str(B)) u = B.transpose()*B #print(u"B的转置 "+str(B.transpose())) u = u.inverse() u = u * B.transpose() #print(u) #print(train_data) #print(u"子矩阵 "+str(train_data.subMatrix(2,seq_len,i,i))) # u = u * (train_data.subMatrix(2,seq_len,i,i)) # u = u * train_data[1:seq_len] # print("u= "+str(u)) # u1 = u[0][0] # u2 = u[1][0] sub_matrix = old_train_data[1:seq_len] u1 = sum(list(map(lambda x:x[0] *x[1],zip(u[0],sub_matrix)))) u2 = sum(list(map(lambda x:x[0] *x[1],zip(u[1],sub_matrix)))) print(u1,u2) left = train_data[0]- u2/u1 #print("left= "+str(left)) #print("u1= "+str(u1)) right = u2/u1 forecast1 = [] # for t in range(seq_len-1,seq_len+N,1): for t in range(0,seq_len): forecast1.append(t) for i in range(0,len(forecast1),1): forecast1[i] = (-u1)*forecast1[i] for i in range(0,len(forecast1),1): forecast1[i] = math.exp(forecast1[i]) for i in range(0,len(forecast1),1): forecast1[i] = left * forecast1[i] for i in range(0,len(forecast1),1): forecast1[i] = forecast1[i] + right #print(forecast1[-1]-forecast1[0]) #print(forecast1) #exchange=[] #for i in range(1,len(forecast1),1): # exchange.append(forecast1[i]-forecast1[i-1]) return int(abs(round(forecast1[-1]-forecast1[0])))
def evaluate(self): ''' Evaluate the current state of the board for completion of the game. A game is complete if any of the three cases apply (i) Any row with only crosses (X) or circles (O) (ii) Any column with only crosses (X) or circles (O) (iii) Any diagonal with only crosses (X) or circles (O) Note that a cross or circle uniquely identifies a player ''' from pymatrix import matrix gameBoard = self.board mat = matrix(gameBoard.board) players = ['X','O'] def evaluateRows(): """ Evaluates each row in the matrix for a win """ for sign in players: for row in mat.rows(): win = True for element in row: if(element != sign): win = False if win is True: self.winner = self.getNextTurn() self.gameComplete = True return return def evaluateColumns(): """ Evaluates each column in the matrix for a win """ for sign in players: for col in mat.cols(): win = True for element in col: if(element != sign): win = False if win is True: self.winner = self.getNextTurn() self.gameComplete = True return return def evaluateDiagonals(): """ Evaluates each diagonal in the matrix for a win """ for sign in players: # Right Diagonal if(gameBoard.getElement(1) == gameBoard.getElement(5) == gameBoard.getElement(9) == sign): self.gameComplete = True self.winner = self.getNextTurn() return # Left Diagonal if(gameBoard.getElement(3) == gameBoard.getElement(5) == gameBoard.getElement(7) == sign): self.gameComplete = True self.winner = self.getNextTurn() return return evaluateRows() evaluateColumns() evaluateDiagonals() return
def test_colvec(): assert matrix([[0.3], [0.5], [0.2]]) == colvec([0.3, 0.5, 0.2])
def test_spike(): assert matrix('0 \n 0 \n 1 \n 0') == spike(4, 2)
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
import random from pymatrix import matrix import numpy as np def mlp(input): hiddenNeuron = 4 learningRate = 0.6 epoch = 3000 maxMSE = 10 ^ -6 #define W1 W1 = matrix([]) for i in range(0, input): temp = [] for j in range(0, hiddenNeuron): temp.append(1) W1.append(temp) return W1 m = matrix([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) c = matrix([[2, 4], [6, 1]]) m = matrix([]) print()