コード例 #1
0
ファイル: afpo.py プロジェクト: akloumann/Bot
    def main(self):
        self.af_pareto_math(self.xsd, self.ysd, self.ymax)
        print('\n############# test graph done #############\n')

        num_rand = c.num_rand_phc
        envs = ENVIRONMENTS()
        print('envs to run:', c.envsToRun, '   pop init:',
              num_rand + len(self.fit_ids))
        parents = POPULATION(c.num_rand_phc + len(self.fit_ids))
        parents.InitializeRandomPop()

        parents = self.phc_ga(parents, envs, 1)
        print('\n############# phc done #############\n')

        for i in range(0, len(parents.p)):
            parents.p[i].age += i
        for i in range(0, len(parents.p)):
            print('p[' + str(i) + '].age =', parents.p[i].age)
        # 3rd arg gens=2 by default
        parents = self.af_pareto_ga(parents, envs)

        # print('parents fits:')
        # parents.Print()

        for j in range(0, len(parents.p)):
            raw_input('press enter to continue...')
            for e in c.envsToRun:
                # for j in range(0, 1):
                parents.p[j].Start_Evaluation(envs.envs[e],
                                              pp=True,
                                              pb=False,
                                              send=True)
コード例 #2
0
    def simple_ga(self, envs):
        def inner(parents, envs, i):
            # children = POPULATION(len(parents.p))
            children = copy.deepcopy(parents)
            children.FillFrom(parents)
            children.Evaluate(envs, False, True)
            print("\n*** Generation", i, "***")
            print("\n***  Parents   ***")
            parents.Print()
            print("***  Children  ***")
            children.Print()
            print()
            parents = children
            return parents

        envs = ENVIRONMENTS()
        parents = POPULATION(c.popsize_simple_ga)
        print('envs:', c.envsToRun, '   pop init:', c.popsize_simple_ga)
        parents.InitializeRandomPop()
        parents.Evaluate(envs, False, True)

        # if c.gen_mode == 'time':
        #     t_end = time.time() + 60 * c.minutes
        #     i = 0
        #     while(time.time() < t_end):
        #         parents = inner(parents, envs, i)
        #         i += 1
        # elif c.gen_mode == 'gens':
        #     for i in range(0, c.gens):
        #         parents = inner(parents, envs, i)

        for i in range(0, c.numGensDivPop):
            parents = inner(parents, envs, i)

        return parents.p[0]
コード例 #3
0
ファイル: afpo5t.py プロジェクト: akloumann/Bot
    def afpo(self):
        envs = ENVIRONMENTS()
        print('envs to run:', c.envsToRun, '   pop init:',
              c.num_rand_phc + len(self.fit_ids))
        parents = POPULATION(c.num_rand_phc + len(self.fit_ids))
        parents.InitializeRandomPop()

        # parents = self.phc_ga(parents, envs, c.afpo2_phc_gens)
        # print('\n############# phc done #############\n')

        parents.Evaluate(envs, False, True)

        for i in range(0, len(parents.p)):
            parents.p[i].age += i
        # for i in range(0, len(parents.p)):
        #     print('p[' + str(i) + '].age =', parents.p[i].age)
        print("Initial population age adjusted")
        parents.Print()

        finalpop = self.af_pareto_ga(parents, envs)

        frontinds = self.af_pareto_math(finalpop.GetAges(), finalpop.GetFits())
        finalpopfront = POPULATION(len(frontinds))
        for i in range(0, len(frontinds)):
            finalpopfront.p[i] = finalpop.p[frontinds[i]]
        finalpopfront.Pickling618()
        self.ShowEvaluatePop(finalpopfront, envs, pp=True, pb=False)
コード例 #4
0
    def main(self):
        envs = ENVIRONMENTS()
        parents = POPULATION(c.num_rand_phc + len(self.fit_ids))
        print('envs:', c.envsToRun, '   pop init:',
              c.num_rand_phc + len(self.fit_ids))

        parents.InitializeRandomPop()
        parents.Evaluate(envs, False, True)
        for i in range(0, len(parents.p)):
            parents.p[i].age += i

        self.afpo(parents, envs)
コード例 #5
0
 def inner(parents, envs, i):
     print("\n##########********* generation", i, "*********##########")
     front = self.gen_par_front_pop(parents, envs)
     if len(front.p) < c.target_afpop:
         target_popsize = c.target_afpop
     else:
         target_popsize = len(front.p) * 2
     print("target_popsize:", target_popsize)
     new_pop = POPULATION(target_popsize)
     for i in range(0, target_popsize):
         new_pop.p[i] = copy.deepcopy(front.p[i % len(front.p)])
         if i >= len(front.p):
             new_pop.p[i].Mutate()
     return new_pop
コード例 #6
0
    def gen_par_front_pop(self, parents, envs):
        ages = parents.GetAges()
        fits = parents.GetFits()
        fits_inv = parents.GetFitsInv()

        dom_inds = self.calc_pareto_front(ages, fits_inv)

        self.graph_both3(ages, fits_inv, dom_inds)

        par_front_pop = POPULATION(len(dom_inds))
        for i in range(0, len(dom_inds)):
            par_front_pop.p[i] = copy.deepcopy(parents.p[dom_inds[i]])

        return par_front_pop
コード例 #7
0
ファイル: afpo2.py プロジェクト: akloumann/Bot
    def main(self):
        t_end = time.time() + 60 * c.minutes
        envs = ENVIRONMENTS()
        print('envs to run:', c.envsToRun, '   initial popsize:', c.num_rand_phc)
        parents = POPULATION(c.num_rand_phc + len(self.fit_ids))
        parents.InitializeRandomPop()
        parents.Evaluate(envs, pp=False, pb=True)
        parents.Print()

        if c.alg == 'afpo2':
            self.afpo()
        elif c.alg == 'garef':
            parents = self.garef(c.numGensDivPop, envs, parents, t_end)

        self.ShowEvaluatePop(parents, envs)
コード例 #8
0
    def combo_simple_phc_ga(self):
        envs = ENVIRONMENTS()
        # parents = POPULATION(c.num_rand_phc)
        # print('envs:', c.envsToRun, '   pop init:', c.num_rand_phc)
        # parents.InitializeRandomPop()
        # parents.Evaluate(envs, False, True)

        pop_phc = POPULATION(c.combo_ga_num_slots)

        for k in range(0, c.combo_ga_num_slots):
            print("***** Slot number", k, "*****")
            pop_phc.p[k] = self.simple_ga(envs)
            pop_phc.p[k].ID = k

        pop_phc = self.phc_ga(pop_phc, envs)
        pop_phc.Pickling618()
        self.ShowEvaluatePop(pop_phc, envs, True, False)
コード例 #9
0
ファイル: afpo3.py プロジェクト: akloumann/Bot
    def afpo(self):
        envs = ENVIRONMENTS()
        print('envs to run:', c.envsToRun, '   pop init:',
              c.num_rand_phc + len(self.fit_ids))
        parents = POPULATION(c.num_rand_phc + len(self.fit_ids))
        parents.InitializeRandomPop()

        parents = self.phc_ga(parents, envs, c.afpo2_phc_gens)
        print('\n############# phc done #############\n')

        for i in range(0, len(parents.p)):
            parents.p[i].age += i
        for i in range(0, len(parents.p)):
            print('p[' + str(i) + '].age =', parents.p[i].age)
        # 3rd arg gens=2 by default
        parents = self.af_pareto_ga(parents, envs, c.afpo2_afpo_gens)
        parents.Pickling618()
コード例 #10
0
ファイル: afpo.py プロジェクト: akloumann/evo-bot
    def main(self):
        num_rand = c.num_rand
        envs = ENVIRONMENTS()
        print('envs to run', c.envsToRun)
        parents = POPULATION(c.num_rand + len(self.fit_ids), 0)
        parents.InitializeRandomPop()
        for ind in range(0, num_rand + len(self.fit_ids)):
            print('ind', ind)

        self.af_pareto(self.xs, self.ys, self.popsize, self.ymax)
        parents = self.phc_ga(parents, envs, 3)  # like phc

        for j in range(0, len(parents.p)):
            raw_input('press enter to continue...')
            for e in c.envsToRun:
                # for j in range(0, 1):
                parents.p[j].Start_Evaluation(envs.envs[e],
                                              pp=True,
                                              pb=False,
                                              send=True)
コード例 #11
0
ファイル: phc.py プロジェクト: akloumann/Bot
import copy
import pickle

### new
# parents0 = POPULATION(100)
# parents0.Initialize()
# parents0.Evaluate(envs, pp=False, pb=True)
# parents0.Print()
# print('********************* done')
### new end

i = 0
t_end = time.time() + 60 * c.minutes

envs = ENVIRONMENTS()
parentsF = POPULATION(len(c.fit_labels), -1)

fit_labels = c.fit_labels
num_rand = 0
print('fit labels', fit_labels)

for k in range(0, len(c.fit_labels) + num_rand):
    print('**********************\nindividual num', k,
          'begin\n**********************')
    if k < num_rand:
        first = np.random.random_sample((c.numInputs, c.numHidden)) * 2 - 1
        second = np.random.random_sample((c.numHidden, c.numOutputs)) * 2 - 1
        genome = [first, second]
    else:

        filename = 'genome' + str(fit_labels[0])
コード例 #12
0
ファイル: afpo3.py プロジェクト: akloumann/Bot
    def af_pareto_spop(self, front, envs):
        target_popsize = 20
        pcc = POPULATION(target_popsize)

        single = POPULATION(1)
        single.InitializeRandomPop()
        single.Evaluate(envs, False, True)

        num_remain = target_popsize - (len(front.p) + 1)
        if num_remain > 0:
            pccr = POPULATION(num_remain)
            for i in range(0, num_remain):
                pccr.p[i] = copy.deepcopy(pcc.p[i % len(front.p)])
            pccr.Mutate()
            pccr.Evaluate()

        for i in range(0, target_popsize):
            if i == 0:
                pcc.p[i] = single.p[0]
            elif i < len(front.p) + 1:
                pcc.p[i] = front.p[i - 1]
            else:
                pcc.p[i] = pccr[i - (len(front) + 1)]

        ages = pcc.GetAges()
        fits = pcc.GetFits()
        print('fits', fits, '  ages', ages)

        fits_inv = []
        for i in range(0, len(fits)):
            if fits[i] != 0:
                fits_inv.append(fits[i]**-1)
            else:
                fits_inv.append(10**6)

        dominant_inds = np.array(self.af_pareto_math(ages, fits_inv))
        new_pop = POPULATION(len(dominant_inds))
        for i in range(0, target_popsize):
            dominant_inds_index = i % len(dominant_inds)
            new_pop.p[i] = copy.deepcopy(pcc.p[dominant_inds_index])
            if i >= len(dominant_inds):
                new_pop.p[i].Mutate()

        del pcc
        new_pop.Print()
        return new_pop
コード例 #13
0
ファイル: afpo.py プロジェクト: akloumann/Bot
    def af_pareto_spop(self, parents, envs):
        children = copy.deepcopy(parents)
        children.Mutate()

        single = POPULATION(1)
        single.InitializeRandomPop()
        single.Evaluate(envs, pp=False, pb=True)
        parents.Evaluate(envs, pp=False, pb=True)
        children.Evaluate(envs, pp=False, pb=True)

        pcc = POPULATION(1 + 2 * len(parents.p))
        pcc.p[0] = single.p[0]
        for i in range(1, len(parents.p) + 1):
            pcc.p[i] = parents.p[i - 1]
        for i in range(
                len(parents.p) + 1,
                len(parents.p) + len(children.p) + 1):
            pcc.p[i] = children.p[i - len(children.p) - 1]

        s_fits = single.GetFits()
        s_ages = single.GetAges()
        p_fits = parents.GetFits()
        p_ages = parents.GetAges()
        c_fits = children.GetFits()
        c_ages = children.GetAges()

        pc_fits = np.concatenate((p_fits, c_fits))
        pc_fits = np.concatenate((s_fits, pc_fits))
        pc_ages = np.concatenate((p_ages, c_ages))
        pc_ages = np.concatenate((s_ages, pc_ages))
        pc_fits_trim = {}
        pc_ages_trim = {}
        pc_fits_nz_inds = []
        for i in range(0, len(pc_fits)):
            if pc_fits[i] != 0:
                pc_fits_trim[i] = pc_fits[i]
                pc_ages_trim[i] = pc_ages[i]
                pc_fits_nz_inds.append(i)

        if len(pc_fits_trim) == 0:
            print('none fit')
            exit(0)

        pcc_trim = POPULATION(len(pc_fits_trim))
        for i in range(0, len(pc_fits_trim)):
            pcc_trim.p[i] = pcc.p[pc_fits_nz_inds[i]]

        pc_fits_inv_trim = {}
        print('pc_fits_trim', pc_fits_trim)
        for i in pc_fits_trim.keys():
            pc_fits_inv_trim[i] = pc_fits_trim[i]**-1
        # find max val in pc_fits_inv_trim
        maxval = 0
        for i in pc_fits_inv_trim.values():
            if i > maxval: maxval = i

        # returns a list right now
        dominant_inds = np.array(
            self.af_pareto_math(pc_ages_trim, pc_fits_inv_trim, maxval * 1.05))
        if c.print_func == 1: print('***** exit af_pareto_math *****\n')
        print('dominant inds', dominant_inds)

        # new_pop = POPULATION(len(dominant_inds))
        # count = 0

        print('******* len of pcc_trim.p', len(pcc_trim.p), '*******')

        # for i in dominant_inds:
        #     print('i in dominant inds is', i)
        #     new_pop.p[count] = pcc_trim.p[i]
        #     count += 1
        for i in range(0, c.target_afpop):
            print('dinds!', dominant_inds[i % len(dominant_inds)])

        new_pop = POPULATION(c.target_afpop)
        for i in range(0, c.target_afpop):
            new_pop.p[i] = copy.deepcopy(
                pcc_trim.p[[dominant_inds[i % len(dominant_inds)]]])

        print('new pop len', len(new_pop.p))

        return new_pop
コード例 #14
0
ファイル: afpo2.py プロジェクト: akloumann/Bot
    def af_pareto_spop(self, parents, envs):
        children = copy.deepcopy(parents)
        children.Mutate()

        single = POPULATION(1)
        single.InitializeRandomPop()
        single.Evaluate(envs, pp=False, pb=True)
        parents.Evaluate(envs, pp=False, pb=True)
        children.Evaluate(envs, pp=False, pb=True)

        # combine single, parents, children into one pop
        pcc = POPULATION(1 + 2 * len(parents.p))
        pcc.p[0] = single.p[0]
        for i in range(1, len(parents.p) + 1):
            pcc.p[i] = parents.p[i - 1]
        for i in range(len(parents.p) + 1, len(parents.p) + len(children.p) + 1):
            pcc.p[i] = children.p[i - len(children.p) - 1]


        # these methods in pop class all return lists
        s_fits = single.GetFits()
        s_ages = single.GetAges()
        p_fits = parents.GetFits()
        p_ages = parents.GetAges()
        c_fits = children.GetFits()
        c_ages = children.GetAges()

        pc_fits = np.concatenate((p_fits, c_fits))
        pc_fits = np.concatenate((s_fits, pc_fits))
        pc_ages = np.concatenate((p_ages, c_ages))
        pc_ages = np.concatenate((s_ages, pc_ages))

        min_fitness_pc = self.return_second_highest_in_list_or_dict(pc_fits)
        for i in range(0, len(pc_fits)):
            if pc_fits[i] == 0:
                pc_fits[i] = min_fitness_pc / 10.0
                pcc.p[i].fitness = min_fitness_pc / 10.0

        pc_fits_inv = []
        print('pc_fits', pc_fits)
        for i in range(0, len(pc_fits)):
            pc_fits_inv.append(pc_fits[i]**-1)

        # returns a list right now
        dominant_inds = np.array(self.af_pareto_math(pc_ages, pc_fits_inv))
        if c.print_func == 1:
            print('********** exit af_pareto_math **********\n')
        print('dominant inds', dominant_inds)

        # new_pop = POPULATION(len(dominant_inds))
        # count = 0

        print('******* len of pcc.p', len(pcc.p), '*******')

        # for i in dominant_inds:
        #     print('i in dominant inds is', i)
        #     new_pop.p[count] = pcc_trim.p[i]
        #     count += 1
        if len(dominant_inds) < c.target_afpop:
            new_popsize = c.target_afpop
        else:
            new_popsize = len(dominant_inds)
        print("new pop size ", new_popsize)
        new_pop = POPULATION(new_popsize)
        for i in range(0, new_popsize):
            # print("type of pcc.p[dominant_inds[" + str(i % len(dominant_inds)) + "]]", type(pcc.p[dominant_inds[i % len(dominant_inds)]]))
            new_pop.p[i] = copy.deepcopy( pcc.p[dominant_inds[i % len(dominant_inds)]] )
            # print(new_pop.p[i].age)

        print('new pop len', len(new_pop.p))

        return new_pop
コード例 #15
0
ファイル: garef.py プロジェクト: akloumann/Bot
import copy
import pickle

### new
# parents0 = POPULATION(100)
# parents0.Initialize()
# parents0.Evaluate(envs, pp=False, pb=True)
# parents0.Print()
# print('********************* done')
### new end

i = 0
t_end = time.time() + 60 * c.minutes

envs = ENVIRONMENTS()
parentsF = POPULATION(len(c.fit_labels))

fit_labels = copy.deepcopy(c.fit_labels)
num_rand = c.num_rand_phc
print('fit labels', fit_labels)
print('envs to run', c.envsToRun)

for k in range(0, len(c.fit_labels) + num_rand):
    print('**********************\nindividual num', k,
          'begin\n**********************')
    parents = POPULATION(c.popSize)
    if k < num_rand:
        first = np.random.random_sample((c.numInputs, c.numHidden)) * 2 - 1
        second = np.random.random_sample((c.numHidden, c.numOutputs)) * 2 - 1
        genome = [first, second]
    else:
コード例 #16
0
import copy
import pickle

### new
# parents0 = POPULATION(100)
# parents0.Initialize()
# parents0.Evaluate(envs, pp=False, pb=True)
# parents0.Print()
# print('********************* done')
### new end

i = 0
t_end = time.time() + 60 * c.minutes

envs = ENVIRONMENTS()
parentsF = POPULATION(len(c.fit_labels), -1)

fit_labels = copy.deepcopy(c.fit_labels)
num_rand = c.num_rand
print('fit labels', fit_labels)
print('envs to run', c.envsToRun)

for k in range(0, len(c.fit_labels) + num_rand):
    print('**********************\nindividual num', k,
          'begin\n**********************')
    parents = POPULATION(c.popSize, -1)
    if k < num_rand:
        first = np.random.random_sample((c.numInputs, c.numHidden)) * 2 - 1
        second = np.random.random_sample((c.numHidden, c.numOutputs)) * 2 - 1
        genome = [first, second]
    else:
コード例 #17
0
fit_labels = c.fit_labels
filename = 'genome' + str(fit_labels[-1])

pathLoad = c.pathLoad
filename = pathLoad + filename
g = open(filename, 'r')
genome = pickle.load(g)
g.close()

# first = np.random.random_sample((c.numInputs, c.numHidden)) * 2 - 1
# second = np.random.random_sample((c.numHidden, c.numOutputs)) * 2 - 1
# genome = [first, second]

envs = ENVIRONMENTS()
parents = POPULATION(1, -1)

parents.InitializeUniformPop(genome)
parents.Evaluate(envs, pp=False, pb=True)
parents.Print()

# # i = 0
# # t_end = time.time() + 60 * c.minutes
# for i in range(0, 1):
# # while time.time() < t_end:
#     # the POPULATION constructor creates an empty dictionary and stores the variable popSize
#     children = POPULATION(c.popSizeGA1, i)
#     children.Print()
#     children.FillFrom(parents)
#
#     # print('seconds remaining', t_end - time.time())
コード例 #18
0
ファイル: afpo5t.py プロジェクト: akloumann/Bot
    def af_pareto_spop(self, parents, envs):
        target_popsize = c.target_afpop
        # pcc new population with target popsize
        pcc = POPULATION(target_popsize)
        # pcc.InitializeRandomPop() # not necessary..?

        # add new random individual
        single = POPULATION(1)
        single.InitializeRandomPop()

        # print("parents print?")
        # parents.Print()

        pcc.p[0] = single.p[0]
        for i in range(1, target_popsize):
            # print("i?", i, "    size of pop?", len(parents.p))
            pcc.p[i] = copy.deepcopy(parents.p[i % len(parents.p)])
            if i % 2 == 0:
                pcc.p[i].Mutate()

        pcc.Evaluate(envs, False, True)

        ages = pcc.GetAges()
        fits = pcc.GetFits()
        print('fits', fits)
        print('ages', ages)

        # if fitness is 0 inverse fitness set to 10000
        fits_inv = []
        for i in range(0, len(fits)):
            if fits[i] != 0:
                fits_inv.append(fits[i]**-1)
            else:
                fits_inv.append(10**4)

        dominant_inds = np.array(self.af_pareto_math(ages, fits_inv))
        print("dominant inds", dominant_inds)
        self.graph_both3(ages, fits_inv, dominant_inds)

        # self.graph_both2(ages, fits_inv, dominant_inds) # dominant inds not right, should be list 1s and 0s
        # self.graph_both(pareto_front_x, pareto_front_y, xx_copy, yy_copy, popsize, ymax)

        new_pop = POPULATION(len(dominant_inds))
        for i in range(0, target_popsize):
            dominant_inds_index = i % len(dominant_inds)
            new_pop.p[i] = copy.deepcopy(pcc.p[dominant_inds_index])
            if i >= len(dominant_inds):
                new_pop.p[i].Mutate()

        del pcc
        # new_pop.Print()
        return new_pop
コード例 #19
0
from pop import POPULATION
from afpo5t2 import GENETIC_ALGORITHM
import const as c
import time
import pyrosim
import matplotlib.pyplot as plt
import numpy as np
import random
import copy
import pickle
import os

alg = GENETIC_ALGORITHM()

mode = 't'
mode = 'r'
if mode == 't':
    envs = ENVIRONMENTS()
    pop = POPULATION(10)
    pop.InitializeRandomPop()
    pop.Evaluate(envs, False, True)
    for i in range(0, len(pop.p)):
        pop.p[i].age += i

    pop.Print()

    frontpop = alg.gen_par_front_pop(pop, envs)
    frontpop.Print()
else:
    # alg.main()
    alg.combo_simple_phc_ga()