Exemplo n.º 1
0
def ttt_test(config):
    gens = config['max_generations']
    
    pop = None
    start_genome = None
    
    #//Hold records for each run
    evals = [ 0 for i in range(neatconfig.num_runs) ]
    genes = [ 0 for i in range(neatconfig.num_runs) ]
    nodes = [ 0 for i in range(neatconfig.num_runs) ]

    # Made as lists for pass-by-reference
    winnernum = [ 0 ]
    winnergenes = [ 0 ]
    winnernodes = [ 0 ]
    
    #//For averaging
    totalevals = 0
    totalgenes = 0
    totalnodes = 0
    
    dprint(DEBUG_INFO, "START TTT TEST")

    if config['seed_with_start_genome']:
        gene_filename = neatconfig.genedir + "/" + config['start_genome_file']
        iFile = open(gene_filename, "r")
        if not iFile:
            dprint(DEBUG_ERROR, "Unable to open starting genome file %s." % (gene_filename, ))
            return pop
        dprint(DEBUG_INFO, "Opened start genome file %s." % (gene_filename, ))
    

        #//Read in the start Genome
        dprint(DEBUG_INFO, "Reading in the start genome")
        line = iFile.readline()
        words = line.strip().split()
        curword = words[0]
        gid = int(words[1])
        if curword != "genomestart":
            dprint(DEBUG_ERROR, "Bad starting genome file %s." % (gene_filename, ))
            return pop
        
        dprint(DEBUG_INFO, "Reading in Genome id %d." % (gid,))
        start_genome = Genome()
        start_genome.SetFromFile(gid, iFile)
        iFile.close()
    
        dprint(DEBUG_INFO, "Verifying start_genome")
        if not start_genome.verify():
            dprint(DEBUG_ERROR, "genome.verify() failed:", start_genome.deep_string())

    #Complete a number of runs
    for expcount in range(neatconfig.num_runs):
        
        if config['seed_with_start_genome']:
        
            #//Spawn the Population
            dprint(DEBUG_INFO, "Spawning Population off Genome")
            pop = Population()
            pop.SetFromGenome(start_genome, neatconfig.pop_size)
            
            dprint(DEBUG_INFO, "Verifying Spawned Population[%d]" % (expcount, ))
            if not pop.verify():
                dprint(DEBUG_ERROR, "Population[%d] verification failed" % (expcount, ))
                
        elif config['seed_with_previous_population']:
            population_filename = neatconfig.genedir + "/" + config['previous_population_file']
            dprint(DEBUG_INFO, "Reading Population from %s" % (population_filename, ))
            pop = Population()
            pop.SetFromFilename(population_filename)
            
            dprint(DEBUG_INFO, "Verifying Start Population[%d]" % (expcount, ))
            if not pop.verify():
                dprint(DEBUG_ERROR, "Population[%d] verification failed" % (expcount, ))


        #// evolve up to gens generations
        gen = 1
        while gen <= gens:
            dprint(DEBUG_INFO, "Evaluating Spawned Population[%d] Epoch[%d]" % (expcount, gen))
            # if not pop.verify():
            #     dprint(DEBUG_ERROR, "Population[%d] Epoch[%d] verification failed" % (expcount, gen))

            #print "Epoch", gen
            generation_filename = neatconfig.generationdir + "/tttgen_%d" % (gen,)

            # Evaluate one generation, checking for a successful end
            #//Check for success
            if ttt_epoch(pop, gen, generation_filename, winnernum, winnergenes, winnernodes):
                evals[expcount] = neatconfig.pop_size * (gen-1) + winnernum[0]
                genes[expcount] = winnergenes[0]
                nodes[expcount] = winnernodes[0]
                break

            # in case we want to change after run has started
            config = ttt_read_config(neatconfig.configdir + '/ttt.config')
            gens = config['max_generations']
            gen += 1

        # end of generation loop
        if g_found_optimal:
            break
    # end of num_runs loop

    #//Average and print stats
    dprint(DEBUG_INFO, "Nodes: ")
    for expcount in range(neatconfig.num_runs):
        dprint(DEBUG_INFO, nodes[expcount])
        totalnodes += nodes[expcount]
    
    dprint(DEBUG_INFO, "Genes: ")
    for expcount in range(neatconfig.num_runs):
        dprint(DEBUG_INFO, genes[expcount])
        totalgenes += genes[expcount]
    
    dprint(DEBUG_INFO, "Evals ")
    samples = 0
    for expcount in range(neatconfig.num_runs):
        dprint(DEBUG_INFO, evals[expcount])
        if evals[expcount] > 0:
            totalevals += evals[expcount]
            samples += 1

    if samples > 0:
        avgnodes = float(totalnodes)/samples
        avggenes = float(totalgenes)/samples
        avgevals = float(totalevals)/samples
    else:
        avgnodes = 0
        avggenes = 0
        avgevals = 0
        
    dprint(DEBUG_INFO, "Failures:", (neatconfig.num_runs - samples), "out of", neatconfig.num_runs, "runs")
    dprint(DEBUG_INFO, "Average Nodes:", avgnodes)
    dprint(DEBUG_INFO, "Average Genes:", avggenes)
    dprint(DEBUG_INFO, "Average Evals:", avgevals)

    return pop
Exemplo n.º 2
0
def xor_test(gens):
    gene_filename = neatconfig.genedir + "/xorstartgenes"
    
    pop = None
    start_genome = None
    
    #//Hold records for each run
    evals = [ 0 for i in range(neatconfig.num_runs) ]
    genes = [ 0 for i in range(neatconfig.num_runs) ]
    nodes = [ 0 for i in range(neatconfig.num_runs) ]

    # Made as lists for pass-by-reference
    winnernum = [ 0 ]
    winnergenes = [ 0 ]
    winnernodes = [ 0 ]
    
    #//For averaging
    totalevals = 0
    totalgenes = 0
    totalnodes = 0

    iFile = open(gene_filename, "r")
    if not iFile:
        dprint(DEBUG_ERROR, "Unable to open starting genome file %s." % (gene_filename, ))
        return pop
    
    print "START XOR TEST"

    #//Read in the start Genome
    print "Reading in the start genome"
    line = iFile.readline()
    words = line.strip().split()
    curword = words[0]
    gid = int(words[1])
    if curword != "genomestart":
        print "Bad starting genome file %s." % (gene_filename, )
        return pop
        
    print "Reading in Genome id %d." % (gid,)
    start_genome = Genome()
    start_genome.SetFromFile(gid, iFile)
    iFile.close()
    
    print "Verifying start_genome"
    if not start_genome.verify():
        dprint(DEBUG_ERROR, "genome.verify() failed:", start_genome.deep_string())

    #Complete a number of runs
    for expcount in range(neatconfig.num_runs):
        #//Spawn the Population
        print "Spawning Population off Genome2"
        pop = Population()
        pop.SetFromGenome(start_genome, neatconfig.pop_size)
      
        print "Verifying Spawned Population[%d]" % (expcount, )
        if not pop.verify():
            dprint(DEBUG_ERROR, "Population[%d] verification failed" % (expcount, ))

        #// evolve up to gens generations
        for gen in range(1, gens+1):
            print "Verifying Spawned Population[%d] Epoch[%d]" % (expcount, gen)
            if not pop.verify():
                dprint(DEBUG_ERROR, "Population[%d] Epoch[%d] verification failed" % (expcount, gen))

            #print "Epoch", gen
            generation_filename = neatconfig.generationdir + "/gen_%d" % (gen,)

            # Evaluate one generation, checking for a successful end
            #//Check for success
            if xor_epoch(pop, gen, generation_filename, winnernum, winnergenes, winnernodes):
                evals[expcount] = neatconfig.pop_size * (gen-1) + winnernum[0]
                genes[expcount] = winnergenes[0]
                nodes[expcount] = winnernodes[0]
                break

        # end of generation loop
        if g_found_optimal:
            break
    # end of num_runs loop

    #//Average and print stats
    print "Nodes: "
    for expcount in range(neatconfig.num_runs):
        print nodes[expcount]
        totalnodes += nodes[expcount]
    
    print "Genes: "
    for expcount in range(neatconfig.num_runs):
        print genes[expcount]
        totalgenes += genes[expcount]
    
    print "Evals "
    samples = 0
    for expcount in range(neatconfig.num_runs):
        print evals[expcount]
        if evals[expcount] > 0:
            totalevals += evals[expcount]
            samples += 1

    if samples > 0:
        avgnodes = float(totalnodes)/samples
        avggenes = float(totalgenes)/samples
        avgevals = float(totalevals)/samples
    else:
        avgnodes = 0
        avggenes = 0
        avgevals = 0
        
    print "Failures:", (neatconfig.num_runs - samples), "out of", neatconfig.num_runs, "runs"
    print "Average Nodes:", avgnodes
    print "Average Genes:", avggenes
    print "Average Evals:", avgevals

    return pop