Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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