def testEvalIndividualCapacitySufficient(self): ''' test on individual that offers more than enough capacity to handle all requests ''' pop = toolBox.toolbox.population(n=2) fit = Fitness() ind1 = pop[0] ind2 = pop[1] for i, item in enumerate(ind1): ind1[i][1] = 120 self.assertGreater(fit.evalIndividualCapacity(ind2), fit.evalIndividualCapacity(ind1))
def testEvalIndividualCapacityZeroCapacity(self): ''' test worst case scenario - individual with zero capacity has the worst fitness ''' pop = toolBox.toolbox.population(n=2) fit = Fitness() ind1 = pop[0] ind2 = pop[1] for i, item in enumerate(ind1): ind1[i][1] = 0 self.assertGreater(fit.evalIndividualCapacity(ind1), fit.evalIndividualCapacity(ind2))
def testEvalIndividualCapacityNotZero(self): ''' test that no one's perfect ''' pop = toolBox.toolbox.population(n=2) fit = Fitness() self.assertGreater(fit.evalIndividualCapacity(pop[0]), 0)
def testEvalIndividualCapacity(self): ''' test to check that no individual is assigned a fitness value less than 0 ''' pop = toolBox.toolbox.population(n=2) fit = Fitness() self.assertFalse(fit.evalIndividualCapacity(pop[0]) < 0)
def main(): fitnessClass =Fitness() # Generate the population pop = toolBox.toolbox.population(n=POPULATION_SIZE) fitnessClass.evalIndividualCapacity(pop[0]) hof = tools.HallOfFame(1) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean) stats.register("std", numpy.std) stats.register("min", numpy.min) stats.register("max", numpy.max) pop, log = algorithms.eaSimple(pop, toolBox.toolbox, cxpb=CROSS_OVER_PROB, mutpb=MUTATION_PROB, ngen=NO_OF_GENERATION, stats=stats, halloffame=hof, verbose=True) ## Evaluate the entire population #fitnesses = list(map(toolBox.toolbox.evaluate, pop)) #for ind, fit in zip(pop, fitnesses): # ind.fitness.values = fit # Iterate trough a number of generations # for g in range(NGEN): # print("-- Generation %i --" % g) # # Select individuals based on their fitness # offspring = toolBox.toolbox.select(pop, len(pop)) # # Cloning those individuals into a new population # offspring = list(map(toolBox.toolbox.clone, offspring)) # # Calling the crossover function # crossover(offspring) # mutation(offspring) # invalidfitness(offspring) # The Best Individual found best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) generateTimeTable(best_ind)