Exemplo n.º 1
0
def main():
    """
    The main function of the program that turns user input into a schedule and
    uses a genetic algorithm to find an optimal schedule.
    """
    # Container for user input.
    info = {}

    # Get the desired term and courses.
    if DEBUG:
        info["term"] = "FA16"
        info["courses"] = ["CSE 12", "CSE 15L", "DOC 1"]
    elif handleInput(info):
        return

    print("Finding schedule data...")

    # Get the schedule data for the given courses and term.
    schedule = Schedule()
    schedule.term = info["term"]
    schedule.courses = info["courses"]

    try:
        scheduleData = schedule.retrieve()
    except ClassParserError: 
        print("The Schedule of Classes data could not be loaded at this " \
              "or you have provided an invalid class.")

        return
    
    # Make sure all of the desired classes were found.
    for course in info["courses"]:
        if course not in scheduleData:
            print("'" + course + "' was not found in the Schedule of Classes!")

            return

    # Initiate the population.
    algorithm = Algorithm(scheduleData)
    algorithm.initiate(CAPACITY, CROSSOVER, MUTATE, ELITISM)

    # Run the algorithm through the desired number of generations.
    generation = 0
    highest = 0


    while generation < GENERATIONS:
        algorithm.evolve()
        generation += 1

        print("Generating... "
              + str(int((generation / GENERATIONS) * 100)) + "%", end="\r")

    print("\nDone!")

    algorithm.printFittest()
Exemplo n.º 2
0
execfile("problems/"+problemName.lower()+".py")

# TODO optimizar esto para carga dinamica
if problemName == 'Griewank':
    problem = Griewank(problemSize)
if problemName == 'Rastrigin':
    problem = Rastrigin(problemSize)
if problemName == 'TSP':
    problem = TSP(dataset,optimun)


logger = Logger(logger_level)
genome = ArrayGenome(problem.size, problem.lower, problem.upper)
techniqueSet = []

# Technique's definition 
if 'RealUCUM' in techs:
    techniqueSet.append(techniques.Genetic('RealUCUM','RealEncoding',genome,problem.initialize,problem.objective,logger,crossovers.uniform,mutators.uniform,0.9,0.01))
if 'RealBCUM' in techs:
    techniqueSet.append(techniques.Genetic('RealBCUM','RealEncoding',genome,problem.initialize,problem.objective,logger,crossovers.blend,mutators.uniform,0.9,0.01))
if 'RealUCGM' in techs:
    techniqueSet.append(techniques.Genetic('RealUCGM','RealEncoding',genome,problem.initialize,problem.objective,logger,crossovers.uniform,mutators.gaussian,0.9,0.01))
if 'RealBCGM' in techs:
    techniqueSet.append(techniques.Genetic('RealBCGM','RealEncoding',genome,problem.initialize,problem.objective,logger,crossovers.blend,mutators.gaussian,0.9,0.01))

# Build algorithm and evolve
algorithm = Algorithm(Terminator(fitnessEvals), selectors.uniform, problem, populationSize, techniqueSet, logger)
algorithm.evolve()

logger.printResult(algorithm, problem)