コード例 #1
0
    def test_run(self):
        '''
        Make sure GA engine can run correctly.
        '''
        indv_template = GAIndividual(ranges=[(0, 10)],
                                     encoding='binary',
                                     eps=0.001)
        population = GAPopulation(indv_template=indv_template, size=50).init()

        # Create genetic operators.
        selection = RouletteWheelSelection()
        crossover = UniformCrossover(pc=0.8, pe=0.5)
        mutation = FlipBitMutation(pm=0.1)

        # Create genetic algorithm engine.
        engine = GAEngine(population=population,
                          selection=selection,
                          crossover=crossover,
                          mutation=mutation)

        @engine.fitness_register
        def fitness(indv):
            x, = indv.variants
            return x + 10 * sin(5 * x) + 7 * cos(4 * x)

        engine.run(50)
コード例 #2
0

def get_w(alphas, dataset, labels):
    ''' 通过已知数据点和拉格朗日乘子获得分割超平面参数w
    '''
    alphas, dataset, labels = np.array(alphas), np.array(dataset), np.array(
        labels)
    yx = labels.reshape(1, -1).T * np.array([1, 1]) * dataset
    w = np.dot(yx.T, alphas)

    return w.tolist()


# Population definition.
indv_template = GAIndividual(ranges=[(-2, 2), (-2, 2), (-5, 5)],
                             encoding='binary',
                             eps=[0.001, 0.001, 0.005])
population = GAPopulation(indv_template=indv_template, size=600).init()

# Genetic operators.
selection = RouletteWheelSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitBigMutation(pm=0.1, pbm=0.55, alpha=0.6)

engine = GAEngine(population=population,
                  selection=selection,
                  crossover=crossover,
                  mutation=mutation,
                  analysis=[ConsoleOutput, FitnessStore])

# 加载数据
コード例 #3
0
ファイル: HYSYS_GA.py プロジェクト: WheatZhang/ThermoFitting
str_PC = float(sys.argv[6])
str_PM = float(sys.argv[7])
str_ng = int(sys.argv[8])

#str_THF_LB = 0.99
#str_THF_HB = 0.999
#str_TOL_LB = 0.90
#str_TOL_HB = 0.99
#str_P = 20
#str_PC = 0.8
#str_PM = 0.1
#str_ng = 2

# Define population.
indv_template = GAIndividual(ranges=[(str_THF_LB, str_THF_HB),
                                     (str_TOL_LB, str_TOL_HB)],
                             encoding='binary',
                             eps=0.001)
population = GAPopulation(indv_template=indv_template, size=str_P).init()

# Create genetic operators.
selection = RouletteWheelSelection()
crossover = UniformCrossover(pc=str_PC, pe=0.5)
mutation = FlipBitMutation(pm=str_PM)

# Create genetic algorithm engine.
# Here we pass all built-in analysis to engine constructor.
engine = GAEngine(population=population,
                  selection=selection,
                  crossover=crossover,
                  mutation=mutation,
                  analysis=[ConsoleOutputAnalysis, FitnessStoreAnalysis])
コード例 #4
0
ファイル: ex01.py プロジェクト: qiaokangqi/gaft
from gaft import GAEngine
from gaft.components import GAIndividual
from gaft.components import GAPopulation
from gaft.operators import TournamentSelection
from gaft.operators import UniformCrossover
from gaft.operators import FlipBitMutation

# Analysis plugin base class.
from gaft.plugin_interfaces.analysis import OnTheFlyAnalysis

# Built-in best fitness analysis.
from gaft.analysis.fitness_store import FitnessStore

# Define population.
indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001)
population = GAPopulation(indv_template=indv_template, size=50).init()

# Create genetic operators.
selection = TournamentSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitMutation(pm=0.1)

# Create genetic algorithm engine.
engine = GAEngine(population=population, selection=selection,
                  crossover=crossover, mutation=mutation,
                  analysis=[FitnessStore])

# Define fitness function.
@engine.fitness_register
def fitness(indv):
コード例 #5
0
    length_part1 = int(np.floor(n * 0.5))
    maxValue_part1=2**length_part1

    length_part2=n-length_part1
    maxValue_part2=2**length_part2
    print('maxValue part1:',maxValue_part1)
    print('maxValue part2:',maxValue_part2)
    print('length part1 part2:',length_part1,length_part2)

    ranges=[(0, maxValue_part1),(0,maxValue_part2)]
    eps = [1, 1]
    var1,var2,var_all=greedy_initialize_variants(n,edges,length_part1,length_part2,graph)
    vars=[var1,var2]
    merge_variants_to_state(vars, length_part1, length_part2, graph=graph, ranges=ranges, eps=eps)
    merge_variants_to_state([var_all], n, 0, graph=graph, ranges=[(0,maxValue)], eps=[1])
    indv_template = GAIndividual(ranges=ranges, encoding='binary', eps=eps)

    indv_template.variants=[var1,var2]
    indv_template.chromsome=indv_template.encode()


    # var=greedy_initialize_one_variants(n,edges,graph)
    # indv_template = GAIndividual(ranges=[(0, maxValue)], encoding='binary', eps=[1])
    # indv_template.variants=[var]
    # indv_template.chromsome=indv_template.encode()
    #####################Initialize approach2##############################

    indv_list=[]
    for i in range(popsize):
        indv_list.append(indv_template.clone())
コード例 #6
0
    pc = args.pc
    pe = args.pe
    pm = args.pm
    numEpoch = args.numEpoch
    rseed=args.rseed
    npseed=args.npseed
    tselect=args.tselect

    random.seed(rseed)
    np.random.seed(npseed)
    inputdir= os.path.join(inputpath,filename)

    n,edges,graph=construct_graph(inputdir)
    maxValue = 2 ** n-1

    indv_template = GAIndividual(ranges=[(0, maxValue)], encoding='binary', eps=[1])
    population = GAPopulation(indv_template=indv_template, size=popsize).init()

    # Create genetic operators.
    # selection = TournamentSelection()#RouletteWheelSelection()
    if tselect:
        print('Tournament selection')
        selection = TournamentSelection()
    else:
        print('RouletteWheelSelection')
        selection = RouletteWheelSelection()

    crossover = UniformCrossover(pc=pc, pe=pe)
    mutation = FlipBitMutation(pm=pm)

    # Create genetic algorithm engine.