def __init__(self, path, size, m, dim=2, M=255): self.path = path self.size = size self.dim = dim self.m = m self.M = M self.histogram = BAT.Histogram(path)
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 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 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
def selector(algo, func_details, popSize, Iter, completeData): function_name = func_details[0] lb = func_details[1] ub = func_details[2] DatasetSplitRatio = 0.34 #Training 66%, Testing 34% DataFile = "datasets/" + completeData data_set = numpy.loadtxt(open(DataFile, "rb"), delimiter=",", skiprows=0) numRowsData = numpy.shape(data_set)[ 0] # number of instances in the dataset numFeaturesData = numpy.shape( data_set)[1] - 1 #number of features in the dataset dataInput = data_set[0:numRowsData, 0:-1] dataTarget = data_set[0:numRowsData, -1] trainInput, testInput, trainOutput, testOutput = train_test_split( dataInput, dataTarget, test_size=DatasetSplitRatio, random_state=1) # # numRowsTrain=numpy.shape(trainInput)[0] # number of instances in the train dataset # numFeaturesTrain=numpy.shape(trainInput)[1]-1 #number of features in the train dataset # # numRowsTest=numpy.shape(testInput)[0] # number of instances in the test dataset # numFeaturesTest=numpy.shape(testInput)[1]-1 #number of features in the test dataset # dim = numFeaturesData if (algo == 0): x = pso.PSO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput) if (algo == 1): x = mvo.MVO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput) if (algo == 2): x = gwo.GWO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput) if (algo == 3): x = mfo.MFO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput) if (algo == 4): x = woa.WOA(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput) if (algo == 5): x = ffa.FFA(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput) if (algo == 6): x = bat.BAT(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize, Iter, trainInput, trainOutput) # 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) reducedfeatures = [] for index in range(0, dim): if (x.bestIndividual[index] == 1): reducedfeatures.append(index) reduced_data_train_global = trainInput[:, reducedfeatures] reduced_data_test_global = testInput[:, reducedfeatures] knn = KNeighborsClassifier(n_neighbors=5) knn.fit(reduced_data_train_global, trainOutput) # Compute the accuracy of the prediction target_pred_train = knn.predict(reduced_data_train_global) acc_train = float(accuracy_score(trainOutput, target_pred_train)) x.trainAcc = acc_train target_pred_test = knn.predict(reduced_data_test_global) acc_test = float(accuracy_score(testOutput, target_pred_test)) x.testAcc = acc_test #print('Test set accuracy: %.2f %%' % (acc * 100)) #x.testTP=testClassification_results[1] #x.testFN=testClassification_results[2] #x.testFP=testClassification_results[3] #x.testTN=testClassification_results[4] return x