Пример #1
0
def test1(ctx):
    ctxobj = ctx.obj
    verbose = ctxobj.get('verbose')

    ga = GenAlg(
        size=4,
        elitism=ctxobj.get('elitism'),
        crossover=ctxobj.get('crossover'),
        pureMutation=ctxobj.get('puremutation'),
        chromoClass=MyChromo,
        #selectionFcn = GenAlgOps.tournamentSelection,
        crossoverFcn=MyCrossover212,
        mutationFcn=GenAlgOps.mutateFew,
        # for pure-mutation of all chromos .. no need to run tournament selection
        #pureMutationSelectionFcn = lambda x: [0,0],
        #pureMutationFcn = GenAlgOps.mutateAll,
        pureMutationSelectionFcn=GenAlgOps.simpleSelection2,
        pureMutationFcn=MyMutate,
        #feasibleSolnFcn = GenAlgOps.disallowDupes,
        minOrMax='min',
        showBest=0,
        # optional params ..
        params={
            'mutateNum': ctxobj.get('mutatenum'),
            'parentPct': 0.50,
        })

    # otherwise, init the gen-alg library from scratch
    ga.initPopulation()
    print('Created random init data')

    # simulate a cross-over
    mother = ga.population[0]
    print('mother', str(mother))
    father = ga.population[1]
    print('father', str(father))
    children = ga.crossoverFcn(mother, father, ga.params)
    for child in children:
        print('child', str(child))

    asm_prog = [
        'ROM0', 'XYZ0', 'MPY', 'ROM1', 'XYZ1', 'MPY', 'ADD', 'ROM2', 'ADD'
    ]
    prog = ga.population[2].cpu.compile(asm_prog)
    print('prog', prog)
    prog.extend([1, 1, 1])  # add the 3 rom/coeffs
    # but make these the last N steps of prog, so the output _is_ the prog output
    ga.population[2].data[-len(prog):] = prog
    fit = ga.population[2].calcFitness()
    print('code', ga.population[2].cpu.show_prog(show_pc=False, nl='/'))
    print('code', ga.population[2].cpu.show_prog_as_func())
    print('fitness', fit)
Пример #2
0
def test1(ctx):
    ctxobj = ctx.obj
    verbose = ctxobj.get('verbose')

    ga = GenAlg(
        size=4,
        elitism=ctxobj.get('elitism'),
        crossover=ctxobj.get('crossover'),
        pureMutation=ctxobj.get('puremutation'),
        chromoClass=MyChromo,
        #selectionFcn = GenAlgOps.tournamentSelection,
        crossoverFcn=GenAlgOps.crossover12,
        mutationFcn=GenAlgOps.mutateNone,
        # for pure-mutation of all chromos .. no need to run tournament selection
        #pureMutationSelectionFcn = lambda x: [0,0],
        #pureMutationFcn = GenAlgOps.mutateAll,
        pureMutationSelectionFcn=GenAlgOps.simpleSelectionParentPct,
        pureMutationFcn=GenAlgOps.mutateAll,
        #feasibleSolnFcn = GenAlgOps.disallowDupes,
        minOrMax='min',
        showBest=0,
        # optional params ..
        params={
            'mutateNum': ctxobj.get('mutatenum'),
            'parentPct': 0.50,
        })

    # otherwise, init the gen-alg library from scratch
    ga.initPopulation()
    print('Created random init data')

    mother = ga.population[0]
    mother.fitness = mother.calcFitness()
    print('mother', str(mother))
    #print( '   ', mother.dataRange )
    print('   ', mother.cpu.show_prog_as_func())

    father = ga.population[1]
    father.fitness = father.calcFitness()
    print('father', str(father))
    #print( '   ', father.dataRange )
    print('   ', father.cpu.show_prog_as_func())

    # simulate a cross-over
    children = ga.crossoverFcn(mother, father, ga.params)
    for child in children:
        child.fitness = child.calcFitness()
        print('child', str(child))