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)
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)
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
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]) del fit_labels[0] pathLoad = c.pathLoad filename = pathLoad + filename g = open(filename, 'r') genome = pickle.load(g) g.close() # parents.InitializeUniformPop(genome) parents.p[k] = INDIVIDUAL(k, genome, 0) parents.Evaluate(envs, pp=False, pb=True) parents.Print() for i in range(0, c.numGensDivPop): # the POPULATION constructor creates an empty dictionary and stores the variable popSize children = POPULATION(c.popSize) # children.Print('bm') print() children.FillFrom(parents) print('seconds remaining', t_end - time.time()) # print('\n0', end=' ') parents.Print() children.Evaluate(envs, pp=False, pb=True) # print(i, end=' ') children.Print(i) children.PrintDists(i)
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()) # # print('\n0', end=' ') # parents.Print() # children.Evaluate(envs, pp=False, pb=True)
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()
filename = 'genome' + str(fit_labels[0]) del fit_labels[0] pathLoad = c.pathLoad filename = pathLoad + filename g = open(filename, 'r') genome = pickle.load(g) g.close() parentsF.p[k] = INDIVIDUAL(k, genome) for e in envs.envs: parentsF.p[k].Start_Evaluation(envs.envs[e], pp=False, pb=True, send=True) parentsF.Evaluate(envs, pp=False, pb=True) parentsF.Print('population size', k) print('################ Begin parallel hill climber ######################') while time.time() < t_end: # print('############# i', i) print('seconds remaining', t_end - time.time()) print('\nF', end='') parentsF.Print() childrenF = copy.deepcopy(parentsF) childrenF.Mutate() childrenF.Evaluate(envs, pp=False, pb=True) print('F', end='') childrenF.Print(i) parentsF.ReplaceWith(childrenF) print('W', end='')
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]) del fit_labels[0] pathLoad = c.pathLoad filename = pathLoad + filename g = open(filename, 'r') genome = pickle.load(g) g.close() parents.InitializeUniformPop(genome) parents.Evaluate(envs, pp=False, pb=True) parents.Print(-1) dists = [] fits = [] for i in range(0, c.numGensDivPop): # the POPULATION constructor creates an empty dictionary and stores the variable popSize children = POPULATION(c.popSize, i) # children.Print('bm') print() children.FillFrom(parents) print('seconds remaining', t_end - time.time()) # print('\n0', end=' ') parents.Print() children.Evaluate(envs, pp=False, pb=True) # print(i, end=' ')