コード例 #1
0
from functions import genesis
from functions import ras_calc
from functions import fitness
from functions import mate
from agent import agent

#Global Variables
g = 0         #instantaenous generation count
target_ras = 0  #global minima for convergence criteria
max_gen = 50  #generation for exit criteria
size = 100    #population size
mut_prob = 0.1  #overall mutation probability
fittest = []

#generation 0
population = genesis(size)
population = ras_calc(population)


while g <= max_gen: #exit criteria
    #evaluation
    parents = fitness(population)
    fittest.append(parents[0])
    ras_fittest = parents[0].ras

    # convergence criteria
    if ras_fittest <= target_ras:
        break

    #generation update (crossover,mutation,etc)
    g += 1
コード例 #2
0
from functions import genesis
from functions import write_block, change_block
import os

from_who = 'ivan'
to_whom = 'petr'
amount = 1002


def create_dir(num):
    if not os.path.exists('{}'.format(num)):
        os.makedirs('{}'.format(num))
    return ('Done!')


#write_block(from_who,to_whom,amount)
genesis()
#create_dir(2)
コード例 #3
0
def GA_FineMut(ind_op, gen_op, mut_prob_op, n_runs, n_genes_op):
    config_perf_FineMut = {
        'Config #': [],
        'Mut_Prob': [],
        'Fit_Ave_Fit': [],
        'Ave_Ave_Fit': [],
        'Weak_Ave_Fit': [],
    }
    raw_data_FineMut = {}

    #Test config modelling
    mut_configs = np.linspace(mut_prob_op - 0.05, mut_prob_op, 11)
    counter = 0
    for m_config in mut_configs:
        counter += 1
        raw_data_FineMut[f"Config # {counter}"] = []
        #config status
        print(f"Config # {counter}")

        config_perf_FineMut['Config #'].append(counter)
        config_perf_FineMut['Mut_Prob'].append(m_config)

        #intermediary Data Storage Lists
        percent_improvement = []
        ave_cost_fittest_g = []
        ave_cost_fittest_run = []
        stdv_g1 = []
        stdv_gEnd = []
        ch_stdv = []

        run = 0
        while run < n_runs:
            #iterator
            run += 1
            #GA Parameters
            gen = 1
            target_cost = 0
            max_gen = gen_op
            size = ind_op
            mut_prob = m_config
            mut_type = 'n_gene'  #mutation type - all,gene,motor
            n_genes = n_genes_op
            fittest = []
            search_limit = 5000

            #Simulation Parameters
            dt = 5e-4
            tsteps = 100

            #Generation 0
            population = genesis(size)
            #population = response(population,dt,tsteps)
            #population = cost_calc(gen,population)
            population = rastrigin(population)

            cost_g1_s = []
            for agent in population:
                cost_g1_s.append(agent.ras)

            #storage for plotting
            cost_fittest_g = []
            gen_s = []

            #GA loop
            while gen <= max_gen:
                gen_s.append(gen)
                #parents = fitness(population)
                parents = fitness_ras(population)

                fittest.append(parents[0])

                for item in range(3):
                    random.shuffle(parents)

                #cost_fittest = parents[0].j_fluc
                cost_fittest = parents[0].ras
                #cost_fittest_g.append(parents[0].j_fluc)
                cost_fittest_g.append(cost_fittest)

                if cost_fittest <= target_cost:
                    break
                gen += 1
                #children = mate(parents, mut_prob, mut_type, gen, max_gen, search_limit,n_genes)
                children = mate_ras(parents, mut_prob, mut_type, gen, max_gen,
                                    search_limit, n_genes)

                if children == None:
                    break
                #children = response(children,dt,tsteps)
                #children = cost_calc(gen,children)
                children = rastrigin(children)
                parents.extend(children)
                population = parents

            # final stats:
            '''print(f'Defined Parameters:')
            print(f'    Target Objective Value: {target_cost}')
            print(f'    Maximum Generation Count: {max_gen}')
            print(f'    Population Size: {size}')
            print(f'    Mutation Probability:  {mut_prob*100} %')
            print('')

            if gen < max_gen:
                print(f'Premature search stop at generation {gen}')
            else:
                print(f'Search completed for defined generations')

            #minimization = fittest[0].j_fluc - fittest[-1].j_fluc
            minimization = fittest[0].ras - fittest[-1].ras

            #percent_improvement = (minimization/fittest[0].j_fluc)*100
            percent_improvement = abs((minimization/fittest[0].ras)*100)

            print(f'GA minimized: {minimization}')
            print(f'Percent Improvement: {percent_improvement} %')

            #print(f'Best objective value: {fittest[-1].j_fluc}')
            print(f'Best objective value: {fittest[-1].ras}')

            print(f'With genes: {fittest[-1].genes}')'''

            cost_fittest_run = []
            raw_data_FineMut[f"Config # {counter}"].append(fittest[-1].ras)
            cost_fittest_run.append(fittest[-1].ras)
            cost_gEnd_s = []
            for agent in population:
                cost_gEnd_s.append(agent.ras)

            #Intermediary Performance Data
            percent_improvement.append(
                abs(((fittest[0].ras - fittest[-1].ras) / fittest[0].ras) *
                    100))
            ave_cost_fittest_g.append(stat.mean(cost_fittest_g))
            ave_cost_fittest_run.append(stat.mean(cost_fittest_run))
            stdv_g1.append(stat.pstdev(cost_g1_s))
            stdv_gEnd.append(stat.pstdev(cost_gEnd_s))
            ch_stdv.append(
                abs((stat.pstdev(cost_gEnd_s) - stat.pstdev(cost_g1_s)) /
                    stat.pstdev(cost_g1_s)))

        #Final Performance Data
        #config_perf['Min_%Imp'].append(min(percent_improvement))
        #config_perf['Ave_%Imp'].append(stat.mean(percent_improvement))
        #config_perf['Max_%Imp'].append(max(percent_improvement))

        config_perf_FineMut['Fit_Ave_Fit'].append(min(ave_cost_fittest_run))
        config_perf_FineMut['Ave_Ave_Fit'].append(
            stat.mean(ave_cost_fittest_run))
        config_perf_FineMut['Weak_Ave_Fit'].append(max(ave_cost_fittest_run))

        #config_perf['Min_StDv_g1'].append(min(stdv_g1))
        #config_perf['Ave_StDv_g1'].append(stat.mean(stdv_g1))
        #config_perf['Max_StDv_g1'].append(max(stdv_g1))

        #config_perf['Min_StDv_gEnd'].append(min(stdv_gEnd))
        #config_perf['Ave_StDv_gEnd'].append(stat.mean(stdv_gEnd))
        #config_perf['Max_StDv_gEnd'].append(max(stdv_gEnd))

        #config_perf['Min_%Ch_StDv'].append(min(ch_stdv))
        #config_perf['Ave_%Ch_StDv'].append(stat.mean(ch_stdv))
        #config_perf['Max_%Ch_StDv'].append(max(ch_stdv))

    return config_perf_FineMut, raw_data_FineMut