def search_es(max_gens, search_space, pop_size, num_children, prefix): f, tmpdir = init_search(prefix,names) population = es.init_population(search_space, pop_size, id=True) cost = run_particles(population,tmpdir+'/'+prefix) for p in population: p["fitness"] = cost[p["id"]] best = sorted(population, key=lambda k: k["fitness"])[0] try: for gen in range(max_gens): children = [es.mutate(population[i], search_space, id=i) for i in range(num_children)] cost = run_particles(children,tmpdir+'/'+prefix) for c in children: c["fitness"] = cost[c["id"]] union = children+population union = sorted(union, key=lambda k: k["fitness"]) if union[0]["fitness"] < best["fitness"]: best = union[0] population = union[:pop_size] f.write("%i %.6g " % (gen, best["fitness"])) [f.write("%.6g "%p) for p in best["pos"]] f.write("\n") print " > gen %i, fitness=%.4g" % (gen, best["fitness"]) print [names[i] + ": %.6g " % p for i,p in enumerate(best["pos"])] except KeyboardInterrupt: print "\033[91mInterruption by user. Exiting...\033[0m" pass f.close() return best
def search_pso(max_gens, search_space, pop_size, c1, c2, prefix): f, tmpdir = init_search(prefix,names) vel_space = pso.generate_vel_space(search_space,frac=1) pop = [pso.create_particle(search_space, vel_space, id=i) for i in range(pop_size)] cost = run_particles(pop,tmpdir+'/'+prefix) for p in pop: p["cost"] = cost[p["id"]] p["b_cost"] = p["cost"] gbest = pso.get_global_best(pop) part_best = {} part_best["pos"] = gbest["pos"] part_best["cost"] = gbest["cost"] try: for gen in range(max_gens): for particle in pop: pso.update_velocity(particle, gbest, vel_space) pso.update_position(particle, search_space) cost = run_particles(pop,tmpdir+'/'+prefix) for particle in pop: particle["cost"] = cost[particle["id"]] pso.update_best_position(particle) gbest = pso.get_global_best(pop, gbest) if gbest["cost"] < part_best["cost"]: part_best["pos"] = list(gbest["pos"]) part_best["cost"] = gbest["cost"] f.write("%i %.6g " % (gen, part_best["cost"])) [f.write("%.6g "%p) for p in part_best["pos"]] f.write("\n") print " > gen %i, fitness=%.6g" % (gen, part_best["cost"]) print [names[i] + ": %.6g " % p for i,p in enumerate(part_best["pos"])] except KeyboardInterrupt: print "\033[91mInterruption by user. Exiting...\033[0m" pass f.close() return part_best
def search_hs(search_space, max_iter, mem_size, consid_rate, adjust_rate, nrange, prefix): f, tmpdir = init_search(prefix,names) memory = [hs.create_random_harmony(search_space,id=i) for i in range(mem_size*3)] cost = run_particles(memory,tmpdir+'/'+prefix) for m in memory: m["fitness"] = cost[m["id"]] memory = sorted(memory, key=lambda k: k["fitness"])[:mem_size] best = memory[0] try: for iter in range(max_iter): harm = hs.create_harmony(search_space, memory, consid_rate, adjust_rate, nrange) harm["id"] = 0 cost = run_particles([harm],tmpdir+'/'+prefix) harm["fitness"] = cost[harm["id"]] if harm["fitness"] < best["fitness"]: best = harm memory.append(harm) memory = sorted(memory, key=lambda k: k["fitness"]) del memory[-1] f.write("%i %.6g " % (iter, best["fitness"])) [f.write("%.6g "%p) for p in best["pos"]] f.write("\n") print " > iteration %i, fitness=%.4g" % (iter, best["fitness"]) print [names[i] + ": %.6g " % p for i,p in enumerate(best["pos"])] except KeyboardInterrupt: print "\033[91mInterruption by user. Exiting...\033[0m" pass f.close() return best
def search(search_space, pop_size, prefix): f, tmpdir = init_search(prefix,names) #pop = init_population_random(search_space, pop_size) pop = init_population_lhs(search_space, pop_size) try: cost = run_particles(pop,tmpdir+'/'+prefix) for i,candidate in enumerate(pop): candidate["cost"] = cost[candidate["id"]] f.write("%.6g " % candidate["cost"]) [f.write("%.6g "%p) for p in candidate["pos"]] f.write("\n") #print candidate["pos"] except KeyboardInterrupt: print "\033[91mInterruption by user. Exiting...\033[0m" pass f.close()
def search_rs(search_space, max_iter, prefix): f, tmpdir = init_search(prefix,names) pop = [] for i in range(max_iter): candidate = {} candidate["id"] = i candidate["pos"] = rs.random_vector(search_space) pop.append(candidate) best = None cost = run_particles(pop,tmpdir+'/'+prefix) for i,candidate in enumerate(pop): candidate["cost"] = cost[candidate["id"]] if best is None or candidate["cost"] < best["cost"]: best = candidate f.write("%i %.6g " % (i, best["cost"])) [f.write("%.6g "%p) for p in best["pos"]] f.write("\n") print " > iteration %i, best=%.4g" % (i, best["cost"]) print [names[i] + ": %.6g " % p for i,p in enumerate(best["pos"])] f.close() return best
def search_ca(max_gens, search_space, pop_size, num_accepted, prefix): f, tmpdir = init_search(prefix,names) # initialize pop = [{"pos" : ca.random_vector(search_space) , "id" : i } for i in range(pop_size)] belief_space = ca.initialize_beliefspace(search_space) # evaluate cost = run_particles(pop,tmpdir+'/'+prefix) for p in pop: p["fitness"] = cost[p["id"]] best = sorted(pop, key=lambda k: k["fitness"])[0] # update situational knowledge ca.update_beliefspace_situational(belief_space, best) try: for gen in range(max_gens): # create next generation children = [ ca.mutate_with_inf(pop[i], belief_space, search_space, id=i) for i in range(pop_size)] # evaluate cost = run_particles(children,tmpdir+'/'+prefix) for c in children: c["fitness"] = cost[c["id"]] best = sorted(children, key=lambda k: k["fitness"])[0] # update situational knowledge ca.update_beliefspace_situational(belief_space, best) # select next generation pop = [ca.binary_tournament(children + pop) for i in range(pop_size)] # update normative knowledge pop = sorted(pop, key=lambda k: k["fitness"]) acccepted = pop[0:num_accepted] ca.update_beliefspace_normative(belief_space, acccepted) # user feedback f.write("%i %.6g " % (gen, belief_space["situational"]["fitness"])) [f.write("%.6g "%p) for p in belief_space["situational"]["pos"]] f.write("\n") print " > gen %i, f=%.4g" % (gen, belief_space["situational"]["fitness"]) print [names[i] + ": %.6g " % p for i,p in enumerate(belief_space["situational"]["pos"])] except KeyboardInterrupt: print "\033[91mInterruption by user. Exiting...\033[0m" pass f.close() return belief_space["situational"]