def closeEvent(self, event): reply = QMessageBox.question(self, '退出', "是否要退出该账号?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: with codecs.open(self.id+'/'+'friendinfo.json', 'w', 'utf-8') as f: for i in range(self.friends_list.count()): item = self.friends_list.item(i) id = re.findall(r'\d{10}', item.text())[0] friend = {} friend['id'] = id json.dump(friend, f, ensure_ascii=False) f.write('\r\n') with codecs.open(self.id+'/'+'all_msg_info.json', 'w', 'utf-8') as f: for i in range(len(self.all_msg_info)): json.dump(self.all_msg_info[i], f, ensure_ascii=False) f.write('\r\n') CS.logout(self.id) print('将要关线程') self.receiver.end() self.friends_list.end() time.sleep(0.3) event.accept() else: event.ignore()
def selector(algo, func_details, popSize, Iter): function_name = func_details[0] lb = func_details[1] ub = func_details[2] dim = func_details[3] if (algo == 0): x = pso.PSO(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 1): x = mvo.MVO(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 2): x = gwo.GWO(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 3): x = mfo.MFO(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 4): x = cs.CS(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 5): x = bat.BAT(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 6): x = woa.WOA(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 7): x = ffa.FFA(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) if (algo == 8): x = ssa.SSA(getattr(benchmarks, function_name), lb, ub, dim, popSize, Iter) return x
def optimize(func, dim, optimization_algorithm, number_of_runs=51): global problem problem['func_num'] = func problem['dimension'] = dim if optimization_algorithm['pso']: print("Now dealing with PSO %sD %sF" % (dim, func)) stats_of_pso, best_pso = PSO.PSO(number_of_runs, problem, test_flags) print("PSO %sD %sF: %s" % (dim, func, best_pso)) if optimization_algorithm['bison']: print("Now dealing with Bison %sD %sF" % (dim, func)) improvements, best_bison = BasicBison.bison_algorithm( number_of_runs, problem, test_flags) print("Bison %sD %sF: %s" % (dim, func, best_bison)) if optimization_algorithm['cs']: print("Now dealing with CS %sD %sF" % (dim, func)) stats_of_cs, best_cs = CS.CS(number_of_runs, problem, test_flags) print("CS %sD %sF: %s" % (dim, func, best_cs)) if optimization_algorithm['bat']: print("Now dealing with BAT %sD %sF" % (dim, func)) stats_of_bat, best_bat = BAT.BAT(number_of_runs, problem, test_flags) print("BAT %sD %sF: %s" % (dim, func, best_bat)) if optimization_algorithm['ffa']: print("Now dealing with FFA %sD %sF" % (dim, func)) stats_of_ffa, best_ffa = FFA.FFA(number_of_runs, problem, test_flags) print("FFA %sD %sF: %s" % (dim, func, best_ffa)) print("Yay!~")
def login(self): id = self.idEdit.text() if CS.login(id): self.login_signal.emit(id) self.close() else: QMessageBox.warning(self, 'WARNING!', 'Your id or passport is wrong!')
def update_friend_state(self): for i in range(self.count()): item = self.item(i) front = '' if item.text()[0] == '*': front = '*' id = re.findall(r'\d{10}', item.text())[0] answer = CS.query_state(id) if answer == 'n': item.setText(front + id + '(不在线)') else: item.setText(front + id + '(在线)')
def add_member(self): name = self.nameEdit.text() answer = CS.query_state(name) if answer == 'Incorrect No.' or answer == 'Please send the correct message.': QMessageBox.warning(self, 'Warning', '该用户不存在') return else: for i in range(len(self.member)): if self.member[i] == name: QMessageBox.warning(self, 'Warning', '已添加该用户') return self.member.append(name) QMessageBox.information(self, 'OK', '成功添加') self.nameEdit.setText('')
def add_friend(self): dg = add_friend_Dialog.add_friend_Dialog() answer = dg.exec() if answer > 0: item = QListWidgetItem() name = dg.nameEdit.text() answer = CS.query_state(name) if answer == 'Incorrect No.' or answer == 'Please send the correct message.': QMessageBox.warning(self, 'Warning', '该用户不存在') return iconpath = dg.get_iconpath() font = QFont() font.setPointSize(16) item.setText(name) item.setIcon(QIcon(iconpath)) item.setFont(font) item.setTextAlignment(Qt.AlignCenter) self.addItem(item)
def selector(algo, func_details, popSize, Iter): function_name = func_details[0] lb = func_details[1] ub = func_details[2] dim = 30 if (algo == 'PSO'): x = pso.PSO(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'SSA'): x = ssa.SSA(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'GOA'): x = goa.GOA(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'IGOA'): x = igoa.IGOA(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'MVO'): x = mvo.MVO(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'GWO'): x = gwo.GWO(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'MFO'): x = mfo.MFO(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'CS'): x = cs.CS(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'BAT'): x = bat.BAT(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'WOA'): x = woa.WOA(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) if (algo == 'FFA'): x = ffa.FFA(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter) return x
def run(self): global lst, IsActivePeers try: if lst.count(False) > 0: block = random.sample( [i for i, e in enumerate(lst) if e == False], lst.count(False))[0] # use CS function from CS.py to download each block lst[block] = CS(self.IP, self.PORT, self.filename + ':' + str(block)) IsActivePeers[self.i] = 'inactive' # except ConnectionResetError: # IsActivePeers[self.i] = 'fail' # except ConnectionRefusedError: # IsActivePeers[self.i] = 'fail' # handle error from connection except Exception as e: print(e) lst[block] = False IsActivePeers[self.i] = 'fail' print("Peers status:" + str(IsActivePeers))
def TrackBall(): # Ball is left IROut = IR.value(0) IRVal = IR.value(3) if 0 < IROut < 4: print('Ball is on the left: 0<IROut<4') motors.left(1000,0.125) #move left 1cm if colour.CheckColourNew() == 1: #check colour motors.right(1000,0.5) elif colour.CheckColourNew() == 3: pass elif 4 < IROut < 6: print('Ball is on the in front: 4<IROut<6') motors.forward(1000,0.5) #move forward 1cm motors.backwards(1000,0.5) #move backwards 1cm if colour.CheckColourNew() == 1: #check colour motors.backwards(1000,0.5) elif colour.CheckColourNew() == 3: pass elif 9 >= IROut > 6: print('Ball is on the right: 9>=IROut>6') motors.right(1000,0.125) #move right 1cm if colour.CheckColourNew() == 1: #check colour motors.left(1000,0.5) elif colour.CheckColourNew() == 3: pass elif IROut == 0: print('Ball not visible: IROut=0') #if US.USFCheck == 3: motors.left(1000,0.25) motors.right(1000,0.5) motors.left(1000,0.25) #elif US.USFCheck == 1: #motors.left() #elif US.USFCheck == 2: #motors.right() else: print("IR Error")
def selector(algo, func_details, popSize, Iter, trainDataset, testDataset, actv): function_name = func_details[0] lb = func_details[1] ub = func_details[2] Dataset_train = trainDataset Dataset_test = testDataset numRowsTrain = numpy.shape(Dataset_train)[ 0] # number of instances in the train dataset numInputsTrain = numpy.shape( Dataset_train)[1] - 1 #number of features in the train dataset numRowsTest = numpy.shape(Dataset_test)[ 0] # number of instances in the test dataset numInputsTest = numpy.shape( Dataset_test)[1] - 1 #number of features in the test dataset trainInput = Dataset_train[0:numRowsTrain, 0:-1] trainOutput = Dataset_train[0:numRowsTrain, -1] testInput = Dataset_test[0:numRowsTest, 0:-1] testOutput = Dataset_test[0:numRowsTest, -1] #number of hidden neurons HiddenNeurons = numInputsTrain * 2 + 1 net = nl.net.newff([[0, 1]] * numInputsTrain, [HiddenNeurons, 1]) if (actv == 1): net = nl.net.newff( [[0, 1]] * numInputsTrain, [HiddenNeurons, 1], [nl.trans.LogSig(), nl.trans.LogSig()]) if (actv == 2): net = nl.net.newff( [[0, 1]] * numInputsTrain, [HiddenNeurons, 1], [nl.trans.SatLinPrm(1, 0, 1), nl.trans.SatLinPrm(1, 0, 1)]) dim = (numInputsTrain * HiddenNeurons) + (2 * HiddenNeurons) + 1 if (algo == 12): x = adam.adamse(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 0): x = pso.PSO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 1): x = mvo.MVO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 2): x = gwo.GWO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 3): x = cs.CS(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 4): x = bat.BAT(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 5): x = de.DE(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 6): x = ga.GA(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 7): x = fa.FFA(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 8): x = bbo.BBO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 9): printAcc = [] printAcc2 = [] x = solution() timerStart = time.time() x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S") if (function_name == "costNN"): net.trainf.defaults['trainf'] = nl.error.MSE() elif (function_name == "costNN4"): net.trainf.defaults['trainf'] = nl.error.CEE() else: return x net.trainf = nl.train.train_gd newOutput = [[x] for x in trainOutput] newOutput = numpy.asarray(newOutput) e = net.train(trainInput, newOutput, epochs=Iter * popSize) timerEnd = time.time() x.optimizer = "BP" x.objfname = function_name x.popnum = 0 x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S") x.executionTime = timerEnd - timerStart x.convergence = e pred = net.sim(trainInput).reshape(len(trainOutput)) pred = numpy.round(pred).astype(int) trainOutput = trainOutput.astype(int) pred = numpy.clip(pred, 0, 1) ConfMatrix = confusion_matrix(trainOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc.append(accuracy_score(trainOutput, pred, normalize=True)) classification_results = numpy.concatenate((printAcc, ConfMatrix1D)) x.trainAcc = classification_results[0] x.trainTP = classification_results[1] x.trainFN = classification_results[2] x.trainFP = classification_results[3] x.trainTN = classification_results[4] pred = net.sim(testInput).reshape(len(testOutput)) pred = numpy.round(pred).astype(int) testOutput = testOutput.astype(int) pred = numpy.clip(pred, 0, 1) ConfMatrix = confusion_matrix(testOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc2.append(accuracy_score(testOutput, pred, normalize=True)) classification_results2 = numpy.concatenate((printAcc2, ConfMatrix1D)) x.testAcc = classification_results2[0] x.testTP = classification_results2[1] x.testFN = classification_results2[2] x.testFP = classification_results2[3] x.testTN = classification_results2[4] return x if (algo == 10): printAcc = [] printAcc2 = [] x = solution() timerStart = time.time() x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S") if (function_name == "costNN"): net.trainf.defaults['trainf'] = nl.error.MSE() elif (function_name == "costNN4"): net.trainf.defaults['trainf'] = nl.error.CEE() else: return x net.trainf = nl.train.train_gdx newOutput = [[x] for x in trainOutput] newOutput = numpy.asarray(newOutput) e = net.train(trainInput, newOutput, epochs=Iter * popSize) timerEnd = time.time() x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S") x.optimizer = "BPMA" x.objfname = function_name x.popnum = 0 x.executionTime = timerEnd - timerStart x.convergence = e pred = net.sim(trainInput).reshape(len(trainOutput)) pred = numpy.round(pred).astype(int) trainOutput = trainOutput.astype(int) pred = numpy.clip(pred, 0, 1) ConfMatrix = confusion_matrix(trainOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc.append(accuracy_score(trainOutput, pred, normalize=True)) classification_results = numpy.concatenate((printAcc, ConfMatrix1D)) x.trainAcc = classification_results[0] x.trainTP = classification_results[1] x.trainFN = classification_results[2] x.trainFP = classification_results[3] x.trainTN = classification_results[4] pred = net.sim(testInput).reshape(len(testOutput)) pred = numpy.round(pred).astype(int) testOutput = testOutput.astype(int) pred = numpy.clip(pred, 0, 1) ConfMatrix = confusion_matrix(testOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc2.append(accuracy_score(testOutput, pred, normalize=True)) classification_results2 = numpy.concatenate((printAcc2, ConfMatrix1D)) x.testAcc = classification_results2[0] x.testTP = classification_results2[1] x.testFN = classification_results2[2] x.testFP = classification_results2[3] x.testTN = classification_results2[4] return x if (algo == 11): x = solution() printAcc = [] printAcc2 = [] if (function_name == "costNN4"): if (actv == 0): timerStart = time.time() x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S") clf = MLPClassifier(hidden_layer_sizes=HiddenNeurons, activation='tanh', max_iter=Iter * popSize, learning_rate_init=0.01, n_iter_no_change=7500).fit( trainInput, trainOutput) timerEnd = time.time() x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S") x.optimizer = "Adam" x.objfname = function_name x.popnum = 0 x.executionTime = timerEnd - timerStart x.convergence = clf.loss_curve_ pred = clf.predict(trainInput) ConfMatrix = confusion_matrix(trainOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc.append( accuracy_score(trainOutput, pred, normalize=True)) classification_results = numpy.concatenate( (printAcc, ConfMatrix1D)) x.trainAcc = classification_results[0] x.trainTP = classification_results[1] x.trainFN = classification_results[2] x.trainFP = classification_results[3] x.trainTN = classification_results[4] pred = clf.predict(testInput) ConfMatrix = confusion_matrix(testOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc2.append( accuracy_score(testOutput, pred, normalize=True)) classification_results2 = numpy.concatenate( (printAcc2, ConfMatrix1D)) x.testAcc = classification_results2[0] x.testTP = classification_results2[1] x.testFN = classification_results2[2] x.testFP = classification_results2[3] x.testTN = classification_results2[4] return x elif (actv == 1): timerStart = time.time() x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S") clf = MLPClassifier(hidden_layer_sizes=HiddenNeurons, activation='logistic', max_iter=Iter * popSize, learning_rate_init=0.01, n_iter_no_change=7500).fit( trainInput, trainOutput) timerEnd = time.time() x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S") x.optimizer = "Adam" x.objfname = function_name x.popnum = 0 x.executionTime = timerEnd - timerStart x.convergence = clf.loss_curve_ pred = clf.predict(trainInput) ConfMatrix = confusion_matrix(trainOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc.append( accuracy_score(trainOutput, pred, normalize=True)) classification_results = numpy.concatenate( (printAcc, ConfMatrix1D)) x.trainAcc = classification_results[0] x.trainTP = classification_results[1] x.trainFN = classification_results[2] x.trainFP = classification_results[3] x.trainTN = classification_results[4] pred = clf.predict(testInput) ConfMatrix = confusion_matrix(testOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc2.append( accuracy_score(testOutput, pred, normalize=True)) classification_results2 = numpy.concatenate( (printAcc2, ConfMatrix1D)) x.testAcc = classification_results2[0] x.testTP = classification_results2[1] x.testFN = classification_results2[2] x.testFP = classification_results2[3] x.testTN = classification_results2[4] return x elif (actv == 2): timerStart = time.time() x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S") clf = MLPClassifier(hidden_layer_sizes=HiddenNeurons, activation='relu', max_iter=Iter * popSize, learning_rate_init=0.01, n_iter_no_change=7500).fit( trainInput, trainOutput) timerEnd = time.time() x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S") x.optimizer = "Adam" x.objfname = function_name x.popnum = 0 x.executionTime = timerEnd - timerStart x.convergence = clf.loss_curve_ pred = clf.predict(trainInput) ConfMatrix = confusion_matrix(trainOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc.append( accuracy_score(trainOutput, pred, normalize=True)) classification_results = numpy.concatenate( (printAcc, ConfMatrix1D)) x.trainAcc = classification_results[0] x.trainTP = classification_results[1] x.trainFN = classification_results[2] x.trainFP = classification_results[3] x.trainTN = classification_results[4] pred = clf.predict(testInput) ConfMatrix = confusion_matrix(testOutput, pred) ConfMatrix1D = ConfMatrix.flatten() printAcc2.append( accuracy_score(testOutput, pred, normalize=True)) classification_results2 = numpy.concatenate( (printAcc2, ConfMatrix1D)) x.testAcc = classification_results2[0] x.testTP = classification_results2[1] x.testFN = classification_results2[2] x.testFP = classification_results2[3] x.testTN = classification_results2[4] return x else: return x # Evaluate MLP classification model based on the training set trainClassification_results = evalNet.evaluateNetClassifier( x, trainInput, trainOutput, net) x.trainAcc = trainClassification_results[0] x.trainTP = trainClassification_results[1] x.trainFN = trainClassification_results[2] x.trainFP = trainClassification_results[3] x.trainTN = trainClassification_results[4] # Evaluate MLP classification model based on the testing set testClassification_results = evalNet.evaluateNetClassifier( x, testInput, testOutput, net) x.testAcc = testClassification_results[0] x.testTP = testClassification_results[1] x.testFN = testClassification_results[2] x.testFP = testClassification_results[3] x.testTN = testClassification_results[4] return x
def selector(algo, func_details, popSize, Iter, trainDataset, testDataset): function_name = func_details[0] lb = func_details[1] ub = func_details[2] DatasetSplitRatio = 2 / 3 dataTrain = "datasets/" + trainDataset dataTest = "datasets/" + testDataset Dataset_train = numpy.loadtxt(open(dataTrain, "rb"), delimiter=",", skiprows=0) Dataset_test = numpy.loadtxt(open(dataTest, "rb"), delimiter=",", skiprows=0) numRowsTrain = numpy.shape(Dataset_train)[ 0] # number of instances in the train dataset numInputsTrain = numpy.shape( Dataset_train)[1] - 1 #number of features in the train dataset numRowsTest = numpy.shape(Dataset_test)[ 0] # number of instances in the test dataset numInputsTest = numpy.shape( Dataset_test)[1] - 1 #number of features in the test dataset trainInput = Dataset_train[0:numRowsTrain, 0:-1] trainOutput = Dataset_train[0:numRowsTrain, -1] testInput = Dataset_test[0:numRowsTest, 0:-1] testOutput = Dataset_test[0:numRowsTest, -1] #number of hidden neurons HiddenNeurons = numInputsTrain * 2 + 1 net = nl.net.newff([[0, 1]] * numInputsTrain, [HiddenNeurons, 1]) dim = (numInputsTrain * HiddenNeurons) + (2 * HiddenNeurons) + 1 if (algo == 0): x = pso.PSO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 1): x = mvo.MVO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 2): x = gwo.GWO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 3): x = mfo.MFO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 4): x = cs.CS(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) if (algo == 5): x = bat.BAT(getattr(costNN, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput, net) # Evaluate MLP classification model based on the training set trainClassification_results = evalNet.evaluateNetClassifier( x, trainInput, trainOutput, net) x.trainAcc = trainClassification_results[0] x.trainTP = trainClassification_results[1] x.trainFN = trainClassification_results[2] x.trainFP = trainClassification_results[3] x.trainTN = trainClassification_results[4] # Evaluate MLP classification model based on the testing set testClassification_results = evalNet.evaluateNetClassifier( x, testInput, testOutput, net) x.testAcc = testClassification_results[0] x.testTP = testClassification_results[1] x.testFN = testClassification_results[2] x.testFP = testClassification_results[3] x.testTN = testClassification_results[4] return x