def main(): # BPSO parameters num_pop = 50 num_feat = 385 num_gens = 1000 # initialize objects model = mlr.MLR() fdf = FromDataFileMLR.FromDataFileMLR() fff = FromFinessFileMLR.FromFinessFileMR(fdf) bpso = BinaryParticleSwarmOptimization(model, fff, num_pop, num_feat, num_gens) # load in data from files trainX, trainY, validateX, validateY, testX, testY = fdf.getAllOfTheData() trainX, validateX, testX = fdf.rescaleTheData(trainX, validateX, testX) # BPSO algorithm bpso.create_initial_population() bpso.evaluate_population(trainX, trainY, validateX, validateY, testX, testY) bpso.create_initial_velocity() initial_local_best_matrix, initial_local_fitness = bpso.create_initial_local_best_matrix( ) bpso.create_initial_global_best_row() bpso.evolve_population(initial_local_best_matrix, initial_local_fitness, trainX, trainY, validateX, validateY, testX, testY)
def main(): fileW = createAnOutputFile() model = mlr.MLR() numOfPop = 50 # should be 50 population numOfFea = 396 # should be 396 descriptors unfit = 1000 # Final model requirements R2req_train = .6 R2req_validate = .5 R2req_test = .5 TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR_DE_BPSO.getAllOfTheData() TrainX, ValidateX, TestX = FromDataFileMLR_DE_BPSO.rescaleTheData(TrainX, ValidateX, TestX) velocity = createInitVelMat(numOfPop, numOfFea) unfit = 1000 fittingStatus = unfit 59 print ("********** time is = ", time.strftime("%H:%M:%S", time.localtime())) while (fittingStatus == unfit): population = createInitPopMat(numOfPop, numOfFea) fittingStatus, fitness = FromFinessFileMLR_DE_BPSO.validate_model(model,fileW, population, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) globalBestRow = InitializeGlobalBestRow(population[0]) globalBestFitness = fitness[0] globalBestRow, globalBestFitness = findGlobalBest(population,fitness, globalBestRow,globalBestFitness) localBestMatrix = CreateInitialLocalBestMatrix(population) localBestFitness = CreateInitialLocalBestFitness(fitness) parentPop = getParentPopulation(population) print ("Starting the Loop - time is = ", time.strftime("%H:%M:%S", time.localtime())) IterateNtimes(model, fileW, fitness, velocity, population, parentPop, localBestFitness,localBestMatrix, globalBestRow, globalBestFitness, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def main(): np.random.seed() # initialize objects fileW = createAnOutputFile() model = mlr.MLR() # load in data from files TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData( TrainX, ValidateX, TestX) # DE_BPSO algorithm velocity = create_initial_velocity() population = create_initial_population(velocity, Lambda=0.01) x, fitness = FromFinessFileMLR.validate_model(model,fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) local_best_matrix, local_fitness = create_initial_local_best_matrix( population, fitness) create_initial_global_best_row(local_best_matrix, local_fitness) evolve_population(population, fitness, velocity, local_best_matrix, local_fitness, \ model, fileW, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def main(): fileW = FromFinessFileMLR.createAnOutputFile() model = mlr.MLR() #Number of descriptor should be 396 and number of population should be 50 or more numOfPop = 50 numOfFea = 396 unfit = 1000 # Final model requirements R2req_train = .6 R2req_validate = .5 R2req_test = .5 TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData( TrainX, ValidateX, TestX) unfit = 1000 fittingStatus = unfit """Create a population based on the number of features selected, in this case 10, from the pool of features""" population = DifferentialEvolution.Create_A_Population(numOfPop, numOfFea) fittingStatus, fitness = FromFinessFileMLR.validate_model(model,fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def main(): GA = GeneticAlgorithm() # create an output file. Name the object to be FileW fileW = GA.createAnOutputFile() # create an object of Multiple Linear Regression model. # The class is located in mlr file model = mlr.MLR() #Number of descriptor should be 385 and number of population should be 50 or more numOfPop = 50 numOfFea = 385 # we continue exhancing the model; however if after 1000 iteration no # enhancement is done, we can quit unfit = 1000 # Final model requirements: The following is used to evaluate each model. # The minimum values for R^2 of training should be 0.6, R^2 of Validation # should be 0.5 and R^2 of test should be 0.5 R2req_train = .6 R2req_validate = .5 R2req_test = .5 # getAllOfTheData is in FromDataFileMLR file. The following places the data # (training data, validation data, and test data) into associated matrices TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = \ GA.DataFile.getAllOfTheData() TrainX, ValidateX, TestX = GA.DataFile.rescaleTheData(TrainX, ValidateX, TestX) fittingStatus = unfit population = GA.Create_A_Population(numOfPop, numOfFea) fittingStatus, fitness = GA.FitnessFile.validate_model(model,fileW, population, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) globalBest = ndarray(shape=(1, numOfFea)) localBest = ndarray(shape=(numOfPop, numOfFea)) localFitness = ndarray(shape=(1, numOfPop)) velocity = GA.createInitialVelocity(numOfPop, numOfFea) localBest, localFitness = GA.createInitialLocalBestMatrix(numOfPop, numOfFea, population, fitness) globalBest = GA.findGlobalBestMatrix(globalBest, localBest) GA.PerformOneMillionIteration(numOfPop, numOfFea, population, fitness, model, fileW, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY, velocity, localBest, localFitness, globalBest)
def append_to_file(new_vector, fileW): #print new_vector model = mlr.MLR() TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData( TrainX, ValidateX, TestX) FromFinessFileMLR.validate_model_and_append(model,fileW, new_vector, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def cal_fitness_DE(new_vector): #print new_vector model = mlr.MLR() TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData( TrainX, ValidateX, TestX) fitness = FromFinessFileMLR.validate_single_model(model, new_vector, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) return fitness
def main(): # Number of descriptor should be 385 and number of population should be 50 or more numOfPop = 50 numOfFea = 385 # create an object of Multiple Linear Regression model. # The class is located in mlr file model = mlr.MLR() # Creates new populations and updates VelocityMatrix, LocalBestMatrix, and GlobalBestRow dataminer = DE_BPSO(numOfPop, numOfFea) # create an output file. Name the object to be FileW fileW = dataminer.createAnOutputFile() # we continue exhancing the model; however if after 1000 iteration no # enhancement is done, we can quit unfit = 1000 # Final model requirements: The following is used to evaluate each model. The minimum # values for R^2 of training should be 0.6, R^2 of Validation should be 0.5 and R^2 of # test should be 0.5 R2req_train = .6 R2req_validate = .5 R2req_test = .5 # getAllOfTheData is in FromDataFileMLR file. The following places the data # (training data, validation data, and test data) into associated matrices TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = dataminer.filedata.getAllOfTheData( ) TrainX, ValidateX, TestX = dataminer.filedata.rescaleTheData( TrainX, ValidateX, TestX) fittingStatus = unfit # Creating initial Velocity Matrix dataminer.CreateInitialVelocity(numOfPop, numOfFea) # Creating initial population based on initial VelocityMatrix population = dataminer.InitializePopulation(numOfPop, numOfFea) # Determining fitness of initial population fittingStatus, fitness = dataminer.analyzer.validate_model( model, fileW, population, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) # initializing LocalBestMatrix as the initial population dataminer.LocalBestM = population.copy() # initializing LocalBestMatrix's fitness values dataminer.LocalBestM_Fit = fitness.copy() # finding the GlobalBestRow of the initial population dataminer.FindGlobalBestRow() dataminer.PerformOneMillionIteration(numOfPop, numOfFea, population, fitness, model, fileW, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def main(): # mkl.set_num_threads(96) numOfPop = 1000 numOfFea = 385 numOfGen = 2000 model = mlr.MLR() # New BPSO model BPSO = BinaryParticleSwarmO(numOfPop, numOfFea, model) # Perform initialiation and firs iteration BPSO.initialize() BPSO.performIterations(numOfGen)
def main(): fileOut, fileW = createAnOutputFile() model = mlr.MLR() #fileW.writerow("heeeeeeeeeeeeeeeeellllllllllllllllloooooooo") numOfPop = 50 # should be 50 population unfit = 1000 # Final model requirements R2req_train = .6 R2req_validate = .5 R2req_test = .5 TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR_BPSO.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR_BPSO.rescaleTheData( TrainX, ValidateX, TestX) numOfFea = TrainX.shape[ 1] # should be 385 descriptors (new data, 396 for old) print TrainX.shape unfit = 1000 fittingStatus = unfit velocity = createInitVelMat(numOfPop, numOfFea) print "time is = ", time.strftime("%H:%M:%S", time.localtime()) while (fittingStatus == unfit): population = createInitPopMat(numOfPop, numOfFea) fittingStatus, fitness = FromFitnessFileMLR_BPSO.validate_model( 0, model, fileW, population, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) globalBestRow, globalBestFitness = InitializeGlobalBestRowAndFitness( fitness, population) globalBestRow, globalBestFitness = findGlobalBest(population, fitness, globalBestRow, globalBestFitness) localBestMatrix = CreateInitialLocalBestMatrix(population) localBestFitness = CreateInitialLocalBestFitness(fitness) print "Starting the Loop - time is = ", time.strftime( "%H:%M:%S", time.localtime()) IterateNtimes(model, fileW, fitness, velocity, population, localBestFitness, localBestMatrix, globalBestRow, globalBestFitness, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) fileOut.close()
def main(): # Number of descriptor should be 385 and number of population should be 50 or more numOfPop = 50 numOfFea = 385 # create an object of Multiple Linear Regression model. # The class is located in mlr file model = mlr.MLR() filedata = FromDataFileMLR.DataFromFile() fitnessdata = FromFinessFileMLR.FitnessResults() analyzer = Fitness(numOfPop, numOfFea) # create an output file. Name the object to be FileW fileW = analyzer.createAnOutputFile() # we continue exhancing the model; however if after 1000 iteration no # enhancement is done, we can quit unfit = 1000 # Final model requirements: The following is used to evaluate each model. The minimum # values for R^2 of training should be 0.6, R^2 of Validation should be 0.5 and R^2 of # test should be 0.5 R2req_train = .6 R2req_validate = .5 R2req_test = .5 # getAllOfTheData is in FromDataFileMLR file. The following places the data # (training data, validation data, and test data) into associated matrices TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = filedata.getAllOfTheData( ) TrainX, ValidateX, TestX = filedata.rescaleTheData(TrainX, ValidateX, TestX) fittingStatus = unfit population = analyzer.createInitialPopulation(numOfPop, numOfFea) fittingStatus, fitness = fitnessdata.validate_model(model,fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) analyzer.CreateInitialVelocity(numOfPop, numOfFea) copyto(analyzer.LocalBestM, population) #initializing LocalBestMatrix as the initial population copyto(analyzer.LocalBestM_Fit, fitness) analyzer.FindGlobalBestRow() analyzer.PerformOneMillionIteration(numOfPop, numOfFea, population, fitness, model, fileW, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def main(): # GA parameters num_pop = 50 num_feat = 385 num_gens = 1000 # initialize objects model = mlr.MLR() fdf = FromDataFileMLR.FromDataFileMLR() fff = FromFinessFileMLR.FromFinessFileMR(fdf) GA = GeneticAlgorithm(model, fff, num_pop, num_feat, num_gens) # load in data from files trainX, trainY, validateX, validateY, testX, testY = fdf.getAllOfTheData() trainX, validateX, testX = fdf.rescaleTheData(trainX, validateX, testX) # genetic algorithm GA.create_initial_population() GA.evaluate_population(trainX, trainY, validateX, validateY, testX, testY) GA.evolve_population(trainX, trainY, validateX, validateY, testX, testY)
def main(): # DE parameters num_pop = 50 num_feat = 385 num_gens = 100 # initialize objects model = mlr.MLR() FDF = FromDataFileMLR.FromDataFileMLR() FFF = FromFinessFileMLR.FromFinessFileMR(FDF) DE = DifferentialEvolution(model, FFF, num_pop, num_feat, num_gens) # load in data from files trainX, trainY, validateX, validateY, testX, testY = FDF.getAllOfTheData() trainX, validateX, testX = FDF.rescaleTheData(trainX, validateX, testX) # differential evolution algorithm DE.create_initial_population() DE.evaluate_population(trainX, trainY, validateX, validateY, testX, testY) DE.evolve_population(trainX, trainY, validateX, validateY, testX, testY)
def main(): random.seed() # create an output file. Name the object to be FileW fileW = GA.createAnOutputFile() # create an object of Multiple Linear Regression model. # The class is located in mlr file model = mlr.MLR() #Number of descriptor should be 385 and number of population should be 50 or more numOfPop = 50 numOfFea = 385 # we continue exhancing the model; however if after 1000 iteration no # enhancement is done, we can quit unfit = 1000 # Final model requirements: The following is used to evaluate each model. The minimum # values for R^2 of training should be 0.6, R^2 of Validation should be 0.5 and R^2 of # test should be 0.5 R2req_train = .6 R2req_validate = .5 R2req_test = .5 # getAllOfTheData is in FromDataFileMLR file. The following places the data # (training data, validation data, and test data) into associated matrices TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.DataMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.DataMLR.rescaleTheData( TrainX, ValidateX, TestX) fittingStatus = unfit population = GA.Create_A_Population(numOfPop, numOfFea) fittingStatus, fitness = FromFinessFileMLR.FitnessMLR.validate_model(model,fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) GA.PerformOneMillionIteration(numOfPop, numOfFea, population, fitness, model, fileW, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def main(): np.random.seed() # initialize objects fileW = createAnOutputFile() model = mlr.MLR() # load in data from files TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData( TrainX, ValidateX, TestX) # BPSO algorithm init_population = create_initial_population() init_fitness = evaluate_population(model, fileW, init_population, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) init_velocity = create_initial_velocity() init_local_best_matrix, init_local_fitness = create_initial_local_best_matrix( init_population, init_fitness) create_initial_global_best_row(init_local_best_matrix, init_local_fitness) evolve_population(init_population, init_fitness, init_velocity, init_local_best_matrix, init_local_fitness, \ model, fileW, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
def main(): model = mlr.MLR() # create an object of MLR model # initialize variables fileW = BPSO.createAnOutputFile() numOfPop = 5000 numOfFea = 385 numOfGen = 1 """ population = BPSO.CreateInitialPopulation(numOfPop, numOfFea) velocity = BPSO.CreateInitialVelocity(numOfPop, numOfFea) populationFitness = BPSO.CreateInitialPopulation(numOfPop, numOfFea) localBestMatrix = population TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.DataMLR.getAllOfTheData() TrainX, ValidateX, TestX = FromDataFileMLR.DataMLR.rescaleTheData(TrainX, ValidateX, TestX) fittingStatus, fitness = FromFinessFileMLR.FitnessMLR.validate_model(True, model,fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) localfitness = fitness globalBestRow = population[np.argmin(fitness)] newPop = BPSO.CreateANewPopulation(numOfPop, numOfFea, localBestMatrix, population, globalBestRow, \ localfitness, velocity, numOfGen, model, fileW) fittingStatus, newFitness = FromFinessFileMLR.FitnessMLR.validate_model(False, model,fileW, newPop, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) #BPSO.CreateInitialPopulation(numOfPop, numOfFea) # step 1 #BPSO.getAValidrow(numOfFea, eps=0.015) # step 2 #BPSO.CreateInitialVelocity(numOfPop, numOfFea) # step 4 #BPSO.CreateInitialLocalBestMatrix(localBestMatrix, population, localfitness, fitness) # step 5 #BPSO.CreateInitialGlobalBestRow(globalBestRow, population, fitness) # step 6 BPSO.CreateANewPopulation(numOfPop, numOfFea, localBestMatrix, population, globalBestRow, localfitness,\ velocity, numOfGen, model, fileW) # step 7 BPSO.UpdateNewLocalBest(numOfPop, numOfFea, localfitness, localBestMatrix, population, globalBestRow,\ newFitness, newPop, velocity, numOfGen, model, fileW) # step 8 & 9 BPSO.UpdateVelocityMatrix(numOfPop, numOfFea, localBestMatrix, population, globalBestRow, velocity) # step 10 BPSO.iterate() # training matrix (step 3) """ population = BPSO.CreateInitialPopulation(numOfPop, numOfFea) velocity = BPSO.CreateInitialVelocity(numOfPop, numOfFea) TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.DataMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.DataMLR.rescaleTheData( TrainX, ValidateX, TestX) fittingStatus, fitness = FromFinessFileMLR.FitnessMLR.validate_model(True, model,fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) localBestMatrix = np.zeros((numOfPop, numOfFea)) localFitness = np.zeros((numOfPop, 1)) globalBestRow = np.zeros(numOfFea) localBestMatrix, localFitness = BPSO.CreateInitialLocalBestMatrix( localBestMatrix, population, localFitness, fitness) globalBestRow, globalBestFitness = BPSO.CreateInitialGlobalBestRow( globalBestRow, population, fitness) # step 6 for i in range(2000): BPSO.UpdateVelocityMatrix(numOfPop, numOfFea, localBestMatrix, population, globalBestRow, velocity) population = BPSO.CreateANewPopulation(numOfPop, numOfFea, localBestMatrix, population, globalBestRow, localFitness,\ velocity, numOfGen, model, fileW) # step 7 fittingStatus, fitness = FromFinessFileMLR.FitnessMLR.validate_model(True, model,fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) localBestMatrix, localFitness, BPSO.UpdateNewLocalBest( numOfPop, numOfFea, localFitness, localBestMatrix, population, globalBestRow, fitness) globalBestRow, globalBestFitness = BPSO.UpdateNewGlobalBest( numOfPop, localFitness, localBestMatrix, globalBestRow, globalBestFitness) print "Global Best: " + str(globalBestFitness)
from numpy import * # provides complex math and array functions from sklearn import svm # provides Support Vector Regression import csv import math import sys # Local files created by me import mlr import FromDataFileMLR import FromFinessFileMLR import BPSO """Create the output file""" fileW = FromFinessFileMLR.createAnOutputFile() #fileW = 0 model = mlr.MLR() start = time.time() #Number of descriptor should be 396 and number of population should be 50 or more """Number of population""" numOfPop = 50 """Number of total features""" numOfFea = 396 # Final model requirements R2req_train = .6 R2req_validate = .5 R2req_test = .5 alpha = 0.5 beta = 0.004