def explore(): INDEX_EXPLORING = 10 here = get_best_from_all_gens(1)[0] best_vec = here["vector"] orig_res = here["results"] print(orig_res[0] / 1e11, orig_res[1] / 1e11) values = [] for i in range(-5, 5): if i == 0: continue copied = [j for j in best_vec] toadd = i * 1e-15 copied[INDEX_EXPLORING] += toadd # res = [0, 0] res = get_errors(TEAM_KEY, copied) print(i, res[0] / 1e11, res[1] / 1e11, (fitness(res) - fitness(orig_res)) / 1e11) values.append({ "index": INDEX_EXPLORING, "difference": toadd, "results": res }) with open("exploring.json") as f: oldvals = json.load(f) for val in values: oldvals["explorations"].append(val) with open("exploring.json", "w") as f: json.dump(oldvals, f)
def get_best(): here = get_best_from_all_gens(200) here = sorted(here, key=lambda i: fitness(i["results"])) for i in range(200): if (i == 2): get_vector_history(here[i]["vector"], here[i]["file"]) break
def select_from_children(children, POPULATION_SIZE, CHILDREN_SIZE): values = [] for i in range(CHILDREN_SIZE): values.append(children[i]["child"]) values = sorted(values, key=lambda i: fitness(i["results"])) ret = [] for i in range(POPULATION_SIZE): ret.append(values[i]) return ret
def select(vecs, POPULATION_SIZE): vecs = sorted(vecs, key=lambda i: fitness(i["results"])) ret = [] # while len(ret) < POPULATION_SIZE: # x = random.randint(0, POPULATION_SIZE - 1) # while x not in ret: # x = random.randint(0, POPULATION_SIZE - 1) # ret.append(x) for i in range(len(vecs)): x = random.random() # if x <= 0.8: ret.append(vecs[i]) if len(ret) == POPULATION_SIZE: break return ret
def load_inits(POPULATION_SIZE, VECTOR_SIZE=11): inits = [] with open("./generations.json") as f: res = json.load(f) res = res["results"] vecs = [] for x in res: y = x["vectors"] for z in y: vecs.append(z) vecs = sorted(vecs, key=lambda i: fitness(i["results"])) for i in range(POPULATION_SIZE): inits.append(vecs[i]) # for i in range(len(vecs)): # x = random.random() # if x <= 0.9: # inits.append(np.array(vecs[i]["vector"])) # if len(inits) == POPULATION_SIZE: # return inits return inits
def get_best_from_all_gens(POPULATION_SIZE): files = [ "new_gen_1.json", "new_gen_2.json", "new_gen_3.json", "new_gen_4.json", "new_new_gen_5.json", "fixed_6.json", "fixed_7.json", "fixed_8.json", "fixed_9.json" ] # files = ["fixed_9.json"] inits = [] for file in files: with open(file) as f: res = json.load(f) res = res["generations"] for gen in res: for vec in gen["vectors"]: if vec["results"] == [0, 0]: continue inits.append({ "vector": vec["vector"]["child"], "results": vec["results"], "generation": gen["generation"], "file": file }) # with open("./generations.json") as f: # res = json.load(f) # res = res["results"] # for x in res: # y = x["vectors"] # for z in y: # inits.append(z) inits = sorted(inits, key=lambda i: fitness(i["results"])) final_inits = [] for i in range(len(inits)): # if abs(inits[i]["results"][0] - inits[i]["results"][1])/1e11 <= 0.6: final_inits.append(inits[i]) if len(final_inits) == POPULATION_SIZE: break return final_inits
def main(): POPULATION_SIZE = 16 CHILDREN_SIZE = 20 # CREATE MATING POOL ON ITS OWN mating_pool = hand_picked() for generation in range(1, 11): print("Generation", generation) gen_here = {"crossed_over": [], "mutated": [], "selected": []} # SELECT PARENTS parents = get_mating_pool(POPULATION_SIZE, CHILDREN_SIZE) # DO CROSSOVER OF PARENTS children = [] for i in range(len(parents)): child1, child2 = BSC( np.array(mating_pool[parents[i][0]]["vector"]), np.array(mating_pool[parents[i][1]]["vector"]), N=min(6, 3 + generation / 10)) children.append({ "child": child1, "parents": [mating_pool[parents[i][0]], mating_pool[parents[i][1]]] }) children.append({ "child": child2, "parents": [mating_pool[parents[i][0]], mating_pool[parents[i][1]]] }) gen_here["crossed_over"].append(child1.tolist()) gen_here["crossed_over"].append(child2.tolist()) # # DO MUTATIONS ON CHILDREN for i in range(len(children)): children[i]["child"] = mutate(children[i]["child"], generation) gen_here["mutated"].append(children[i]["child"].tolist()) # GET ERRORS errors = [] for child in children: # res = [0, 0] res = get_errors(TEAM_KEY, child["child"].tolist()) print(res[0] / 1e11, res[1] / 1e11, fitness(res)) child["child"] = { "vector": child["child"].tolist(), "results": res } errors.append({"vector": child, "results": res}) # ADD CHILDREN TO LIST with open("new_new_gen_9.json") as f: oldres = json.load(f) oldres = oldres["generations"] oldres.append({"generation": generation, "vectors": errors}) oldres = {"generations": oldres} with open("new_new_gen_9.json", "w") as f: json.dump(oldres, f) # SELECT BEST CHILDREN mating_pool = select_from_children(children, POPULATION_SIZE, CHILDREN_SIZE) for mat in mating_pool: gen_here["selected"].append(mat["vector"]) with open("generations/generation_" + str(generation) + ".txt", "w+") as f: json.dump(gen_here, f)