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(): 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 evolve_population(initial_population, initial_fitness, initial_velocity, initial_local_best_matrix, initial_local_fitness, model, fileW, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY): print('gen #' + str(0) + '\t\t min fit: ' + str(initial_fitness.min()) + '\t\t avg fit: ' + str(np.average(initial_fitness))) alpha = 0.5 for i in range(1, numOfGens): velocity = update_velocity(initial_velocity, initial_population) population = create_new_population(numOfPop, numOfFea, initial_population, velocity, initial_local_best_matrix, alpha, i) x, fitness = FromFinessFileMLR.validate_model(model, fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) local_best_matrix, local_fitness = update_local_best_matrix( population, fitness, initial_local_best_matrix, initial_local_fitness) update_global_best_row(local_best_matrix, local_fitness) alpha = alpha - (0.17 / numOfGens) print('gen #' + str(i) + '\t\t min fit: ' + str(fitness.min()) + '\t\t avg fit: ' + str(np.average(fitness)))
# Final model requirements R2req_train = .6 R2req_validate = .5 R2req_test = .5 alpha = 0.5 beta = 0.004 TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData( ) TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData( TrainX, ValidateX, TestX) population = BPSO.Create_A_Population(numOfPop, numOfFea) """ Get fitness""" fitness = FromFinessFileMLR.validate_model(model, fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) """Initialize velocity""" initial_velocity = BPSO.create_initial_velocity(numOfPop, numOfFea) #print str(shape(initial_velocity)) """Initialize Local Best Matrix (Same as Initial Population)""" local_best_matrix = population """Create Global best row""" global_best_row_index = argmin(fitness) global_best_row_fitness = fitness[global_best_row_index] global_best_row = population[global_best_row_index] #global_best_row=population[argmin(fitness)] generations_to_run = 2000 print "Starting program"
def evaluate_population(model, fileW, population, trainX, trainY, validateX, validateY, testX, testY): fittingStatus, fitness = FromFinessFileMLR.validate_model( model, fileW, population, trainX, trainY, validateX, validateY, testX, testY) return fitness
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 """ Generate population with 385 features and 50 pop""" population = DifferentialEvolution.Create_A_Population(numOfPop, numOfFea) """ Get fitness for the population""" fittingStatus, fitness = FromFinessFileMLR.validate_model(model, fileW, population, \ TrainX, TrainY, ValidateX, ValidateY, TestX, TestY) """Create functions in the Differential Evolution class for the following: """ """grab best model-fitness (row) and move it to new pop in same index""" #DifferentialEvolution.create_DE_population(numOfPop, numOfFea,fitness,population) generations_to_run = 20000 fit = fitness iterations_since_best_fitness_has_changed = 0 for i in range(0, generations_to_run): print "Running Generation " + str(i) if iterations_since_best_fitness_has_changed == 500: print "Program stopped - the best fitness has not changed in 500 generations" break fitness = fit best_fitness_index = argmin(fitness) pop, fit = DifferentialEvolution.create_DE_population(