예제 #1
0
def singlecore(filename, delta, popsize, generations):
    """Run serially"""
    #set up the evo strategy
    best_list, mut_list = [], []
    evo = popstrat.Evostrategy(5000, popsize)
    children = evo.iterate(evo.pop)

    for i in range(generations):
        for child in children:
            results, conclist = run_grn(child['genome'], delta)
            bestidx = results.index(max(results))
            child['fitness'] = results[bestidx]
            print "fitness:", child['fitness']

        children = evo.iterate(children)
        bestgenome = evo.pop[-1]['genome']
        results, conclist = run_grn(bestgenome, delta)
        filename = "best_gen_" + str(i)
        graph.plot_2d(conclist, filename)

        if evo.adaptive:
            evo.adapt_mutation()

        best_list.append(evo.pop[-1]['fitness'])
        mut_list.append(evo.mut_rate)

    print "best overall fitness", evo.pop[-1]['fitness']

    graph.plot_2d([best_list], 'bestfit')
    graph.plot_2d([mut_list], 'mutrate')
예제 #2
0
def multicore():
    delta = 20
    #set up the evo strategy
    best_list, mut_list = [], []
    evo = popstrat.Evostrategy(5000, 50)
    children = evo.iterate(evo.pop)

    nodes = ("*",)
    job_server = pp.Server(8, ppservers=nodes)
    print "Starting pp with", job_server.get_ncpus(), "workers"

    start_time = time.time()

    for i in range(50):
        run_time = time.time()
        jobs = [(child, job_server.submit(run_grn, 
                                          (child['genome'], 
                                           delta),
                                           (),
                                           ("grn","numpy","math")))
                                           for child in children]
        for child, result in jobs:
            results, conclist = result()
            bestidx = results.index(max(results))
            child['fitness'] = results[bestidx]

        #plotting the best with colors
        children = evo.iterate(children)
        bestgenome = evo.pop[-1]['genome']
        bestresult, conclist = run_grn(bestgenome, delta)
        bestidx = bestresult.index(max(bestresult))
        filename = "best_gen_"+str("%03d" % i)
        print filename

        colors = []
        simplist = []
        for idx, result in enumerate(bestresult):
            if idx == len(bestresult)-1:
                simplist.append(conclist[idx])
                colors.append('k')
            elif idx == bestidx:
                colors.append('g')
                simplist.append(conclist[idx])
            # elif result == 0:
            #     colors.append('b')
            # else:
            #     colors.append('r')
        graph.plot_2d(simplist, filename, colors)

        print "gen:", evo.gen_count, "fitness:", evo.pop[-1]['fitness']

        if evo.adaptive:
            evo.adapt_mutation()

        best_list.append(evo.pop[-1]['fitness'])
        mut_list.append(evo.mut_rate)

    mutfile = open('mutrate.txt','a')
    mutfile.write(str(mut_list)+'\n')
    mutfile.close()
예제 #3
0
def multicore(filename, delta, popsize, generations):
    #set up the evo strategy
    evo = popstrat.Evostrategy(5000, popsize)
    children = evo.iterate(evo.pop)

    nodes = ("*", )
    job_server = pp.Server(8, ppservers=nodes)
    print "Starting pp with", job_server.get_ncpus(), "workers"

    start_time = time.time()

    for i in range(generations):
        run_time = time.time()
        jobs = [(child,
                 job_server.submit(run_grn, (child['genome'], delta), (),
                                   ("cgrn as grn", "numpy", "math")))
                for child in children]
        for child, result in jobs:
            results, conclist = result()
            bestidx = results.index(max(results))
            child['fitness'] = results[bestidx]

        #plotting the best with colors
        children = evo.iterate(children)
        bestgenome = evo.pop[-1]['genome']
        bestresult, conclist = run_grn(bestgenome, delta)
        bestidx = bestresult.index(max(bestresult))

        colors = []
        simplist = []
        for idx, result in enumerate(bestresult):
            if idx == len(bestresult) - 1:
                simplist.append(conclist[idx])
                colors.append('k')
            elif idx == bestidx:
                colors.append('g')
                simplist.append(conclist[idx])
            else:
                simplist.append(conclist[idx])
                colors.append('r')
        graph.plot_2d(simplist, filename, colors)
        print "gen:", evo.gen_count, "fitness:", evo.pop[-1]['fitness']

        if evo.adaptive:
            evo.adapt_mutation()

        res_file = open(filename, "a")
        res_file.write(str(evo.pop[-1]) + '\n')
        res_file.close()
예제 #4
0
def main():
    import time
    if(len(sys.argv) != 2):
        print "Usage: " + sys.argv[0] + " <rseed>"
        sys.exit(1)
    seed = int(sys.argv[1])
    random.seed(seed)

    #read in the compustat data for given companies
    delta = 20
    companies = [] 
    company_list = ['ARCHER-DANIELS-MIDLAND CO', 'ARTHUR J GALLAGHER & CO',
 'ASCENA RETAIL GROUP INC', 'ASTEC INDUSTRIES INC', 'ASTORIA FINANCIAL CORP',
 'BLACKBAUD INC', 'BMC SOFTWARE INC','BOSTON BEER INC  -CL A', 'BRADY CORP',
 'BRIGGS & STRATTON']
    
    database = csvreader.CSVReader('compustat.csv')
    for company in company_list:
        companies.append(database.get_company(company))

    #set up the evo strategy
    best_list, mut_list = [], []
    evo = popstrat.Evostrategy(5000,50)
    children = evo.iterate(evo.pop)

    for i in range(50):
        for child in children:
            start_time = time.time()
            fitness = run_grn(child['genome'], companies, delta)
            child['fitness'] = fitness
            print "fitness:",child['fitness'],"time taken", str(time.time()-start_time)
            
        children = evo.iterate(children)
        if evo.adaptive:
            evo.adapt_mutation()

        best_list.append(evo.pop[-1]['fitness'])
        mut_list.append(evo.mut_rate)

    graph.plot_2d([best_list], 'bestfit')
    graph.plot_2d([mut_list], 'mutrate')
예제 #5
0
def multicore(filename, syncsize, offset, delta, popsize, generations):
    """ Uses parallel python to evaluate, PP can also be used to
    distribute evaluations to machines accross the network"""
    #set up the evo strategy

    evo = popstrat.Evostrategy(5000, popsize)
    children = evo.iterate(evo.pop)

    nodes = ("*", )
    job_server = pp.Server(ncpus=8, ppservers=nodes)
    print "Starting pp with", job_server.get_ncpus(), "workers"

    for _ in range(generations):
        jobs = [(child,
                 job_server.submit(run_grn,
                                   (child['genome'], delta, syncsize, offset),
                                   (), ("cgrn as grn", "numpy", "math")))
                for child in children]
        for child, result in jobs:
            results, conclist = result()
            bestidx = results.index(max(results))
            child['fitness'] = results[bestidx]

        print "gen:", evo.gen_count, "fitness:", evo.pop[-1]['fitness']
        children = evo.iterate(children)

        if evo.adaptive:
            evo.adapt_mutation()

        res_file = open(filename, "a")
        res_file.write(str(evo.pop[-1]) + '\n')
        res_file.close()

    restopname = filename + ".rest"
    #plotting the best with colors
    bestgenome = evo.pop[-1]['genome']
    bestresult, conclist = run_grn(bestgenome, delta, syncsize, offset,
                                   restopname)

    bestidx = bestresult.index(max(bestresult))
    fitness = round(max(bestresult), 1)
    colors = []

    for idx, result in enumerate(bestresult):
        # draw the input in black
        if idx == len(bestresult) - 1:
            colors.append('k')
        # draw the best in green
        elif idx == bestidx:
            colors.append('g')
        # draw p_genes in red
        elif result > 0:
            colors.append('r')
        # draw TFs in blue
        else:
            colors.append('b')

    print "colors", colors
    graphname = filename.split('/')[2]
    graphname = graphname[:-4] + "-F" + str(fitness)
    graph.plot_2d(conclist, graphname, colors, (0, 1))
예제 #6
0
def main():
    import time
    if(len(sys.argv) != 2):
        print "Usage: " + sys.argv[0] + " <rseed>"
        sys.exit(1)
    seed = int(sys.argv[1])
    random.seed(seed)

    delta = 20
    filename = "results-"+str(seed)+".dat"
    
    #read in the compustat data for given companies
    companies = [] 
    company_list = ['ARCHER-DANIELS-MIDLAND CO', 'ARTHUR J GALLAGHER & CO',
                    'ASCENA RETAIL GROUP INC', 'ASTEC INDUSTRIES INC', 
                    'ASTORIA FINANCIAL CORP', 'BLACKBAUD INC', 
                    'BMC SOFTWARE INC','BOSTON BEER INC  -CL A',
                    'BRADY CORP', 'BRIGGS & STRATTON']

    database = csvreader.CSVReader('compustat.csv')
    for company in company_list:
        companies.append(database.get_company(company))
    
    #set up the evo strategy
    best_list, mut_list = [], []
    evo = popstrat.Evostrategy(5000, 50)
    children = evo.iterate(evo.pop)

    nodes = ("*",)
    job_server = pp.Server(0, ppservers=nodes)
    print "Starting pp with", job_server.get_ncpus(), "workers"

    start_time = time.time()

    for i in range(50):
        run_time = time.time()
        jobs = [(child, job_server.submit(run_grn, 
                                          (child['genome'], 
                                           companies,
                                           delta),
                                           (),
                                           ("grn",)))
                                           for child in children]
        for child, result in jobs:
            child['fitness'], child['testfit'] = result()

        children = evo.iterate(children)
        print "gen", evo.gen_count, "time taken", str(time.time()-run_time), "total time", str(time.time()-start_time)

        if evo.adaptive:
            evo.adapt_mutation()

        res_file = open(filename,"a")
        print "best fitness:", evo.pop[-1]['fitness'], evo.pop[-1]['testfit']
        res_file.write(str(evo.pop[-1])+'\n')
        res_file.close()
            
        best_list.append(evo.pop[-1]['fitness'])
        mut_list.append(evo.mut_rate)

    mutfile = open('mutrate.txt','a')
    mutfile.write(str(mut_list)+'\n')
    mutfile.close()