Commit 86bb082e authored by Julien Lin's avatar Julien Lin
Browse files

--wip-- [skip ci]

parent aee39742
......@@ -2,11 +2,12 @@
# Algorithms
########################################################################
def random(func, init, again):
"""Iterative random search template."""
best_sol = init()
best_val = func(best_sol)
val,sol = best_val,best_sol
val, sol = best_val, best_sol
i = 0
while again(i, best_val, best_sol):
sol = init()
......@@ -22,7 +23,7 @@ def greedy(func, init, neighb, again):
"""Iterative randomized greedy heuristic template."""
best_sol = init()
best_val = func(best_sol)
val,sol = best_val,best_sol
val, sol = best_val, best_sol
i = 1
while again(i, best_val, best_sol):
sol = neighb(best_sol)
......@@ -34,13 +35,15 @@ def greedy(func, init, neighb, again):
i += 1
return best_val, best_sol
# TODO add a simulated-annealing template.
def simulated_annealing(func, init, neigh, temp, proba, rand, again):
"""Recuit simulé."""
best_sol, t = init()
best_val = func(best_sol)
cur_val, cur_sol = best_val,best_sol
cur_val, cur_sol = best_val, best_sol
k = 1
# t = 2000
while again(k, best_val, best_sol):
......@@ -55,7 +58,25 @@ def simulated_annealing(func, init, neigh, temp, proba, rand, again):
best_val = cur_val
k += 1
return best_val, best_sol
# TODO add a population-based stochastic heuristic template.
def evolutionary(func, init, best, selection, evaluate, variation, replacement, again):
pop = init()
best_sol = best(pop)
best_val = func(best_sol)
k = 1
while again(k, best_val, best_sol):
parents = selection(pop)
offsprings = variation(parents)
offsprings = evaluate(offsprings, func)
sol = best(parents, offsprings, func)
val = func(best_sol)
if val > best_val:
best_sol = sol
best_val = val
pop = replacement(parents, offsprings)
k += 1
return best_val, best_sol
......@@ -45,6 +45,7 @@ def rand(dim, scale):
def init_with_t(t,**kwargs):
"""Same as rand but add a time."""
return rand(**kwargs), t
########################################################################
......@@ -59,3 +60,27 @@ def neighb_square(sol, scale, domain_width):
new = sol + (np.random.random(len(sol)) * side - side/2)
return new
########################################################################
# Evolutionary specific functions
########################################################################
def best(population, population_val):
assert len(population) == len(population_val)
best_val = max(population_val)
best_individu_index = population_val.index(best_val)
best_individu = population[best_individu_index]
return best_individu
def selection(population, population_val, nb_selected):
assert len(population) == len(population_val)
assert nb_selected <= len(population)
sorted_population = sorted(zip(population, population_val), key=lambda el: el[1], reverse=True)
return sorted_population[:nb_selected], population_val[:nb_selected]
def variation(population, nb_variation):
new_population = []
for individu in population:
pass
\ No newline at end of file
......@@ -63,7 +63,7 @@ if __name__ == "__main__":
help="Random pseudo-generator seed (none for current epoch)",
)
solvers = ["num_greedy", "bit_greedy", "num_sim_anneal", "num_random"]
solvers = ["num_greedy", "bit_greedy", "num_sim_anneal", "num_random", "num_evolutionary"]
can.add_argument(
"-m",
"--solver",
......@@ -122,7 +122,7 @@ if __name__ == "__main__":
np.random.seed(the.seed)
# Weird numpy way to ensure single line print of array.
np.set_printoptions(linewidth=np.inf)
np.set_printoptions(linewidth=np.inf) # type: ignore
# Common termination and checkpointing.
history: list[Any] = []
......@@ -255,6 +255,16 @@ if __name__ == "__main__":
iters
)
sensors = num.to_sensors(sol)
# elif the.solver == "num_evolutionary":
# val, sol = algo.evolutionary(
# make.func(
# num.cover_sum,
# domain_with=the.domain_width,
# sensor_range=the.sensor_range,
# dim = d*the.nb_sensors,
# ),
# )
# Fancy output.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment