Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
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]
Beispiel #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)
Beispiel #5
0
    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
Beispiel #6
0
    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)
Beispiel #7
0
    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()
Beispiel #8
0
    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
Beispiel #9
0
    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)
Beispiel #10
0
    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
Beispiel #11
0
    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
Beispiel #12
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()