Ejemplo n.º 1
0
def jmoo_evo(problem, algorithm, configurations, toStop = bstop):
    """
    ----------------------------------------------------------------------------
     Inputs:
      -@problem:    a MOP to optimize
      -@algorithm:  the MOEA used to optimize the problem
      -@toStop:     stopping criteria method
    ----------------------------------------------------------------------------
     Summary:
      - Evolve a population for a problem using some algorithm.
      - Return the best generation of that evolution
    ----------------------------------------------------------------------------
     Outputs:
      - A record (statBox) of the best generation of evolution
    ----------------------------------------------------------------------------
    """
    
    # # # # # # # # # # #
    # 1) Initialization #
    # # # # # # # # # # #
    stoppingCriteria = False                             # Just a flag for stopping criteria
    statBox          = jmoo_stats_box(problem,algorithm) # Record keeping device
    gen              = 0                                 # Just a number to track generations
    numeval = 0
    values_to_be_passed = {}
    
    # # # # # # # # # # # # # # # #
    # 2) Load Initial Population  #
    # # # # # # # # # # # # # # # #
    # Though this is not important I am sticking to NSGA3 paper
    # if algorithm.name == "NSGA3":
    #     print "-"*20 + "boom"
    #     jmoo_properties.PSI = jmoo_properties.max_generation[problem.name]
    #     jmoo_properties.MU = population_size[problem.name.split("_")[-1]]

    population = problem.loadInitialPopulation(configurations["Universal"]["Population_Size"])

    assert(len(population) == configurations["Universal"]["Population_Size"]), "The population loaded from the file must be equal to MU"
    from time import time
    old = time()

    # # # # # # # # # # # # # # # #
    # 3.1) Special Initialization #
    # # # # # # # # # # # # # # # #
    if algorithm.initializer is not None:
        # TODO:fix MOEAD
        population, numeval = algorithm.initializer(problem, population, configurations, values_to_be_passed)


    # Generate a folder to store the population
    foldername = "./RawData/PopulationArchives/" + algorithm.name + "_" + problem.name + "/"
    import os
    all_subdirs = [foldername + d for d in os.listdir(foldername) if os.path.isdir(foldername + d)]
    latest_subdir = sorted(all_subdirs, key=os.path.getmtime)[-1]


    # # # # # # # # # # # # # # #
    # 3) Collect Initial Stats  #
    # # # # # # # # # # # # # # #
    statBox.update(population, 0, numeval, initial=True)

    # # # # # # # # # # # # # # #
    # 4) Generational Evolution #
    # # # # # # # # # # # # # # #
    
    while gen < configurations["Universal"]["No_of_Generations"] and stoppingCriteria is False:
        gen+= 1
        print gen, " | ",
        import sys
        sys.stdout.flush()

        # # # # # # # # #
        # 4a) Selection #
        # # # # # # # # #

        problem.referencePoint = statBox.referencePoint
        selectees, evals = algorithm.selector(problem, population, configurations, values_to_be_passed)
        numNewEvals = evals
        old = time()

        # # # # # # # # # #
        # 4b) Adjustment  #
        # # # # # # # # # #
        selectees, evals = algorithm.adjustor(problem, selectees, configurations)
        numNewEvals += evals


        
        # # # # # # # # # # #
        # 4c) Recombination #
        # # # # # # # # # # #

        population, evals = algorithm.recombiner(problem, population, selectees, configurations)

        numNewEvals += evals
        assert(len(population) == configurations["Universal"]["Population_Size"]), \
            "Length of the population should be equal to MU"
        # # # # # # # # # # #
        # 4d) Collect Stats #
        # # # # # # # # # # #
        if algorithm.name == "GALE0" or algorithm.name == "GALE_no_mutation":
            statBox.update(selectees, gen, numNewEvals, population_size=Configurations["Universal"]["Population_Size"])
            store_values(latest_subdir, gen, selectees)
        elif algorithm.name == "GALE4":
            statBox.update(selectees, gen, len(selectees), population_size=Configurations["Universal"]["Population_Size"])
            store_values_g4(latest_subdir, gen, selectees)
        else:
            statBox.update(population, gen, numNewEvals)
            store_values(latest_subdir, gen, population)

        # from PerformanceMetrics.IGD.IGD_Calculation import IGD
        # resulting_pf = [[float(f) for f in individual.fitness.fitness] for individual in statBox.box[-1].population]
        # fitnesses = statBox.box[-1].fitnesses
        # median_fitness = []
        # for i in xrange(len(problem.objectives)):
        #     temp_fitness = [fit[i] for fit in fitnesses]
        #     median_fitness.append(median(temp_fitness))
        # print IGD(resulting_pf, readpf(problem)), median_fitness
            
        # # # # # # # # # # # # # # # # # #
        # 4e) Evaluate Stopping Criteria  #
        # # # # # # # # # # # # # # # # # #
        if algorithm.name != "GALE4":
            stoppingCriteria = toStop(statBox)
        # stoppingCriteria = False

        # assert(len(statBox.box[-1].population) == configurations["Universal"]["Population_Size"]), \
        #     "Length in the statBox should be equal to MU"

    return statBox
Ejemplo n.º 2
0
def jmoo_evo(problem, algorithm, toStop = bstop):
    """
    ----------------------------------------------------------------------------
     Inputs:
      -@problem:    a MOP to optimize
      -@algorithm:  the MOEA used to optimize the problem
      -@toStop:     stopping criteria method
    ----------------------------------------------------------------------------
     Summary:
      - Evolve a population for a problem using some algorithm.
      - Return the best generation of that evolution
    ----------------------------------------------------------------------------
     Outputs:
      - A record (statBox) of the best generation of evolution
    ----------------------------------------------------------------------------
    """
    
    # # # # # # # # # # #
    # 1) Initialization #
    # # # # # # # # # # #
    stoppingCriteria = False                             # Just a flag for stopping criteria
    statBox          = jmoo_stats_box(problem,algorithm) # Record keeping device
    gen              = 0                                 # Just a number to track generations
    
    # # # # # # # # # # # # # # # #
    # 2) Load Initial Population  #
    # # # # # # # # # # # # # # # #
    population = problem.loadInitialPopulation(MU)
    
    # # # # # # # # # # # # # # #
    # 3) Collect Initial Stats  #
    # # # # # # # # # # # # # # #
    statBox.update(population, 0, 0, initial=True)
    
    # # # # # # # # # # # # # # #
    # 4) Generational Evolution #
    # # # # # # # # # # # # # # #
    
    while gen < PSI and stoppingCriteria == False:
        gen+= 1
        
        # # # # # # # # #
        # 4a) Selection #
        # # # # # # # # #
            
            
        problem.referencePoint = statBox.referencePoint
        selectees,evals = algorithm.selector(problem,population)
        numNewEvals = evals
        
        
        #raw_input("Press any Key")
        # # # # # # # # # #
        # 4b) Adjustment  #
        # # # # # # # # # #
        selectees,evals = algorithm.adjustor(problem, selectees)
        numNewEvals += evals
        
        # # # # # # # # # # #
        # 4c) Recombination #
        # # # # # # # # # # #
        population,evals = algorithm.recombiner(problem, population, selectees, MU)
        numNewEvals += evals        
        
        
        # # # # # # # # # # #
        # 4d) Collect Stats #
        # # # # # # # # # # #
        statBox.update(population, gen, numNewEvals)
        #for row in population: print row.valid
        
        
            
        # # # # # # # # # # # # # # # # # #
        # 4e) Evaluate Stopping Criteria  #
        # # # # # # # # # # # # # # # # # #
        stoppingCriteria = toStop(statBox)
        #stoppingCriteria = False
        
        
    


    #return the representative generation
    return statBox
Ejemplo n.º 3
0
def jmoo_evo(problem, algorithm, toStop=bstop):
    """
    ----------------------------------------------------------------------------
     Inputs:
      -@problem:    a MOP to optimize
      -@algorithm:  the MOEA used to optimize the problem
      -@toStop:     stopping criteria method
    ----------------------------------------------------------------------------
     Summary:
      - Evolve a population for a problem using some algorithm.
      - Return the best generation of that evolution
    ----------------------------------------------------------------------------
     Outputs:
      - A record (statBox) of the best generation of evolution
    ----------------------------------------------------------------------------
    """

    # # # # # # # # # # #
    # 1) Initialization #
    # # # # # # # # # # #
    stoppingCriteria = False  # Just a flag for stopping criteria
    statBox = jmoo_stats_box(problem, algorithm)  # Record keeping device
    gen = 0  # Just a number to track generations

    # # # # # # # # # # # # # # # #
    # 2) Load Initial Population  #
    # # # # # # # # # # # # # # # #
    population = problem.loadInitialPopulation(MU)

    # # # # # # # # # # # # # # #
    # 3) Collect Initial Stats  #
    # # # # # # # # # # # # # # #
    statBox.update(population, 0, 0, initial=True)

    # # # # # # # # # # # # # # #
    # 4) Generational Evolution #
    # # # # # # # # # # # # # # #

    while gen < PSI and stoppingCriteria == False:
        gen += 1

        # # # # # # # # #
        # 4a) Selection #
        # # # # # # # # #

        problem.referencePoint = statBox.referencePoint
        selectees, evals = algorithm.selector(problem, population)
        numNewEvals = evals

        #raw_input("Press any Key")
        # # # # # # # # # #
        # 4b) Adjustment  #
        # # # # # # # # # #
        selectees, evals = algorithm.adjustor(problem, selectees)
        numNewEvals += evals

        # # # # # # # # # # #
        # 4c) Recombination #
        # # # # # # # # # # #
        population, evals = algorithm.recombiner(problem, population,
                                                 selectees, MU)
        numNewEvals += evals

        # # # # # # # # # # #
        # 4d) Collect Stats #
        # # # # # # # # # # #
        statBox.update(population, gen, numNewEvals)
        #for row in population: print row.valid

        # # # # # # # # # # # # # # # # # #
        # 4e) Evaluate Stopping Criteria  #
        # # # # # # # # # # # # # # # # # #
        stoppingCriteria = toStop(statBox)
        #stoppingCriteria = False

    #return the representative generation
    return statBox
Ejemplo n.º 4
0
def jmoo_evo(problem, algorithm, toStop=bstop):
    """
    ----------------------------------------------------------------------------
     Inputs:
      -@problem:    a MOP to optimize
      -@algorithm:  the MOEA used to optimize the problem
      -@toStop:     stopping criteria method
    ----------------------------------------------------------------------------
     Summary:
      - Evolve a population for a problem using some algorithm.
      - Return the best generation of that evolution
    ----------------------------------------------------------------------------
     Outputs:
      - A record (statBox) of the best generation of evolution
    ----------------------------------------------------------------------------
    """

    # # # # # # # # # # #
    # 1) Initialization #
    # # # # # # # # # # #
    stoppingCriteria = False  # Just a flag for stopping criteria
    statBox = jmoo_stats_box(problem, algorithm)  # Record keeping device
    gen = 0  # Just a number to track generations

    # # # # # # # # # # # # # # # #
    # 2) Load Initial Population  #
    # # # # # # # # # # # # # # # #
    population = problem.loadInitialPopulation(MU)
    #Vivek:
    #print "Length of population: ",len(population)

    # # # # # # # # # # # # # # #
    # 3) Collect Initial Stats  #
    # # # # # # # # # # # # # # #
    statBox.update(population, 0, 0, initial=True)

    # # # # # # # # # # # # # # #
    # 4) Generational Evolution #
    # # # # # # # # # # # # # # #

    while gen < PSI and stoppingCriteria == False:
        gen += 1
        #print "Generation: ",gen
        # # # # # # # # #
        # 4a) Selection #
        # # # # # # # # #
        if algorithm.name == "GALE2_1" and gen == 1:
            # use the initial population to build classes
            for pop in population:
                pop.fitness = problem.evaluate(pop.decisionValues)
            high = -1e6
            constraints = []
            for x in xrange(len(problem.decisions)):
                for e, d in enumerate(
                        sdiv2(population,
                              cohen=0.3,
                              num1=lambda y: y.decisionValues[x],
                              num2=lambda y: min(y.fitness))):
                    temp = sorted([y.decisionValues[x] for y in d[-1]])
                    mean = sum([min(y.fitness) for y in d[-1]]) / len(d[-1])
                    if mean > high:
                        const1 = temp[0]
                        const2 = temp[-1]
                        high = mean
                problem.decisions[x].low = const1
                problem.decisions[x].up = const2
            # read the data from population
            # run sdiv get constraints and input it in the problem. There is a problem with upper limit

        problem.referencePoint = statBox.referencePoint
        selectees, evals = algorithm.selector(problem, population)
        numNewEvals = evals

        #raw_input("Press any Key")
        # # # # # # # # # #
        # 4b) Adjustment  #
        # # # # # # # # # #
        selectees, evals = algorithm.adjustor(problem, selectees)
        numNewEvals += evals

        # # # # # # # # # # #
        # 4c) Recombination #
        # # # # # # # # # # #
        population, evals = algorithm.recombiner(problem, population,
                                                 selectees, MU)
        numNewEvals += evals

        # # # # # # # # # # #
        # 4d) Collect Stats #
        # # # # # # # # # # #
        statBox.update(population, gen, numNewEvals)
        #for row in population: print row.valid

        # # # # # # # # # # # # # # # # # #
        # 4e) Evaluate Stopping Criteria  #
        # # # # # # # # # # # # # # # # # #
        stoppingCriteria = toStop(statBox)
        #print ">>>>>>>> Stopping Criteria: ",stoppingCriteria
        #stoppingCriteria = False

    #return the representative generation
    return statBox
Ejemplo n.º 5
0
def jmoo_evo(problem, algorithm, configurations, toStop=bstop):
    """
    ----------------------------------------------------------------------------
     Inputs:
      -@problem:    a MOP to optimize
      -@algorithm:  the MOEA used to optimize the problem
      -@toStop:     stopping criteria method
    ----------------------------------------------------------------------------
     Summary:
      - Evolve a population for a problem using some algorithm.
      - Return the best generation of that evolution
    ----------------------------------------------------------------------------
     Outputs:
      - A record (statBox) of the best generation of evolution
    ----------------------------------------------------------------------------
    """

    # # # # # # # # # # #
    # 1) Initialization #
    # # # # # # # # # # #
    stoppingCriteria = False  # Just a flag for stopping criteria
    statBox = jmoo_stats_box(problem, algorithm)  # Record keeping device
    gen = 0  # Just a number to track generations
    numeval = 0
    values_to_be_passed = {}

    # # # # # # # # # # # # # # # #
    # 2) Load Initial Population  #
    # # # # # # # # # # # # # # # #
    # Though this is not important I am sticking to NSGA3 paper
    # if algorithm.name == "NSGA3":
    #     print "-"*20 + "boom"
    #     jmoo_properties.PSI = jmoo_properties.max_generation[problem.name]
    #     jmoo_properties.MU = population_size[problem.name.split("_")[-1]]

    population = problem.loadInitialPopulation(
        configurations["Universal"]["Population_Size"])

    assert (len(population) == configurations["Universal"]["Population_Size"]
            ), "The population loaded from the file must be equal to MU"
    from time import time
    old = time()

    # # # # # # # # # # # # # # # #
    # 3.1) Special Initialization #
    # # # # # # # # # # # # # # # #
    if algorithm.initializer is not None:
        # TODO:fix MOEAD
        population, numeval = algorithm.initializer(problem, population,
                                                    configurations,
                                                    values_to_be_passed)

    # Generate a folder to store the population
    foldername = "./RawData/PopulationArchives/" + algorithm.name + "_" + problem.name + "/"
    import os
    all_subdirs = [
        foldername + d for d in os.listdir(foldername)
        if os.path.isdir(foldername + d)
    ]
    latest_subdir = sorted(all_subdirs, key=os.path.getmtime)[-1]

    # # # # # # # # # # # # # # #
    # 3) Collect Initial Stats  #
    # # # # # # # # # # # # # # #
    statBox.update(population, 0, numeval, initial=True)

    # # # # # # # # # # # # # # #
    # 4) Generational Evolution #
    # # # # # # # # # # # # # # #

    while gen < configurations["Universal"][
            "No_of_Generations"] and stoppingCriteria is False:
        gen += 1
        print gen, " | ",
        import sys
        sys.stdout.flush()

        # # # # # # # # #
        # 4a) Selection #
        # # # # # # # # #

        problem.referencePoint = statBox.referencePoint
        selectees, evals = algorithm.selector(problem, population,
                                              configurations,
                                              values_to_be_passed)
        numNewEvals = evals
        old = time()

        # # # # # # # # # #
        # 4b) Adjustment  #
        # # # # # # # # # #
        selectees, evals = algorithm.adjustor(problem, selectees,
                                              configurations)
        numNewEvals += evals

        # # # # # # # # # # #
        # 4c) Recombination #
        # # # # # # # # # # #

        population, evals = algorithm.recombiner(problem, population,
                                                 selectees, configurations)

        numNewEvals += evals
        assert(len(population) == configurations["Universal"]["Population_Size"]), \
            "Length of the population should be equal to MU"
        # # # # # # # # # # #
        # 4d) Collect Stats #
        # # # # # # # # # # #
        if algorithm.name == "GALE0" or algorithm.name == "GALE_no_mutation":
            statBox.update(
                selectees,
                gen,
                numNewEvals,
                population_size=Configurations["Universal"]["Population_Size"])
            store_values(latest_subdir, gen, selectees)
        elif algorithm.name == "GALE4":
            statBox.update(
                selectees,
                gen,
                len(selectees),
                population_size=Configurations["Universal"]["Population_Size"])
            store_values_g4(latest_subdir, gen, selectees)
        else:
            statBox.update(population, gen, numNewEvals)
            store_values(latest_subdir, gen, population)

        # from PerformanceMetrics.IGD.IGD_Calculation import IGD
        # resulting_pf = [[float(f) for f in individual.fitness.fitness] for individual in statBox.box[-1].population]
        # fitnesses = statBox.box[-1].fitnesses
        # median_fitness = []
        # for i in xrange(len(problem.objectives)):
        #     temp_fitness = [fit[i] for fit in fitnesses]
        #     median_fitness.append(median(temp_fitness))
        # print IGD(resulting_pf, readpf(problem)), median_fitness

        # # # # # # # # # # # # # # # # # #
        # 4e) Evaluate Stopping Criteria  #
        # # # # # # # # # # # # # # # # # #
        if algorithm.name != "GALE4":
            stoppingCriteria = toStop(statBox)
        # stoppingCriteria = False

        # assert(len(statBox.box[-1].population) == configurations["Universal"]["Population_Size"]), \
        #     "Length in the statBox should be equal to MU"

    return statBox
Ejemplo n.º 6
0
def jmoo_evo(problem, algorithm, repeat=-1, toStop=bstop):
    """
    ----------------------------------------------------------------------------
     Inputs:
      -@problem:    a MOP to optimize
      -@algorithm:  the MOEA used to optimize the problem
      -@toStop:     stopping criteria method
    ----------------------------------------------------------------------------
     Summary:
      - Evolve a population for a problem using some algorithm.
      - Return the best generation of that evolution
    ----------------------------------------------------------------------------
     Outputs:
      - A record (statBox) of the best generation of evolution
    ----------------------------------------------------------------------------
    """

    # # # # # # # # # # #
    # 1) Initialization #
    # # # # # # # # # # #
    stoppingCriteria = False  # Just a flag for stopping criteria
    statBox = jmoo_stats_box(problem, algorithm)  # Record keeping device
    gen = 0  # Just a number to track generations
    numeval = 0

    # # # # # # # # # # # # # # # #
    # 2) Load Initial Population  #
    # # # # # # # # # # # # # # # #
    # Though this is not important I am sticking to NSGA3 paper
    # if algorithm.name == "NSGA3":
    #     print "-"*20 + "boom"
    #     jmoo_properties.PSI = jmoo_properties.max_generation[problem.name]
    #     jmoo_properties.MU = population_size[problem.name.split("_")[-1]]

    population = problem.loadInitialPopulation(jmoo_properties.MU)
    print "Length of population: ", len(population)

    # # # # # # # # # # # # # # #
    # 3) Collect Initial Stats  #
    # # # # # # # # # # # # # # #
    statBox.update(population, 0, numeval, initial=True)

    # # # # # # # # # # # # # # # #
    # 3.1) Special Initialization #
    # # # # # # # # # # # # # # # #
    if algorithm.initializer is not None:
        # TODO:fix MOEAD
        population, numeval = algorithm.initializer(problem, population)

    # # # # # # # # # # # # # # #
    # 4) Generational Evolution #
    # # # # # # # # # # # # # # #

    while gen < jmoo_properties.PSI and stoppingCriteria is False:
        gen += 1
        # # # # # # # # #
        # 4a) Selection #
        # # # # # # # # #

        # from copy import deepcopy
        # new_population = deepcopy(population)

        problem.referencePoint = statBox.referencePoint
        selectees, evals = algorithm.selector(problem, population)
        numNewEvals = evals

        # raw_input("Press any Key")
        # # # # # # # # # #
        # 4b) Adjustment  #
        # # # # # # # # # #
        selectees, evals = algorithm.adjustor(problem, selectees)
        numNewEvals += evals

        # # # # # # # # # # #
        # 4c) Recombination #
        # # # # # # # # # # #

        population, evals = algorithm.recombiner(problem, population, selectees, MU)
        numNewEvals += evals

        print "Length of pop: jmoea: ", len(population)
        # # # # # # # # # # #
        # 4d) Collect Stats #
        # # # # # # # # # # #
        statBox.update(population, gen, numNewEvals)
        # for row in population: print row.valid
        # print statBox.bests

        # # # # # # # # # # # # # # # # # #
        # 4e) Evaluate Stopping Criteria  #
        # # # # # # # # # # # # # # # # # #
        stoppingCriteria = toStop(statBox)
        # stoppingCriteria = False

        fignum = len([name for name in os.listdir("data/finalpopulation")]) + 1
        filename = (
            "data/finalpopulation/"
            + problem.name
            + "_"
            + algorithm.name
            + "_"
            + str(repeat)
            + "_"
            + str(fignum)
            + ".txt"
        )
        filedesc = open(filename, "w")
        print "hold population: ", len(statBox.box[-1].population)
        for pop in statBox.box[-1].population:
            if problem.validate(pop.decisionValues) is False:
                continue
            filedesc.write(
                ",".join([str(int(round(p, 0))) for p in pop.decisionValues])
                + " : "
                + ",".join([str(p) for p in pop.fitness.fitness])
                + "\n"
            )
        filedesc.close()
    return statBox