Commit 83ea7e30 authored by Julien Lin's avatar Julien Lin
Browse files

fix variation

parent 78dc90ba
......@@ -5,12 +5,13 @@
"version": "0.2.0",
"configurations": [
{
"name": "Python: num_evolutionary",
"type": "python",
"request": "launch",
"module": "snp",
"args": ["--solver","num_evolutionary", "--iters", "2000", "--steady-delta", "500"]
"args": ["--solver","num_evolutionary", "--nb-population","100", "--nb-offspring", "100", "--nb-selected", "10"]
},
{
"name": "Python: num_random",
......
......@@ -75,11 +75,10 @@ def evolutionary(func, init, best, selection, evaluate, variation, replacement,
parents, parents_val = selection(pop, pop_val)
offsprings = variation(parents)
offsprings_val = evaluate(offsprings, func)
sol = best(
sol, val = best(
np.concatenate((parents, offsprings), axis=0),
np.concatenate((parents_val, offsprings_val), axis=0),
)
val = func(best_sol)
if val > best_val:
best_sol = sol
best_val = val
......
......@@ -99,31 +99,24 @@ def selection(population, population_val, nb_selected):
)
def variation(population, nb_offspring, covariance_mat, dim, scale):
def variation(population, nb_offspring, nb_sensors, domain_width, variation_scale):
"""
population : is an array of individu. An individu is an np array of size dim * nb sensors.
"""
rng = np.random.default_rng()
def get_radar_in_neig(covariance_mat, rng: Generator, radar):
return rng.multivariate_normal(radar, covariance_mat)
individual_index = 0
new_population = []
for _ in range(nb_offspring):
individual = population[individual_index]
new_individu = []
for i in range(0, len(individual), dim):
radar = individual[i : i + dim]
new_radar = get_radar_in_neig(covariance_mat, rng, radar)
while not (
0 <= new_radar[0] <= scale and 0 <= new_radar[1] <= scale
): # While radar is not in the domain
new_radar = get_radar_in_neig(covariance_mat, rng, radar)
new_individu.append(new_radar[0])
new_individu.append(new_radar[1])
parent1, parent2 = population[np.random.randint(0,len(population)-1)],population[np.random.randint(0,len(population)-1)]
new_individu = neighb_square((parent1+parent2)/2, variation_scale, domain_width)
while not all(map(lambda x: 0<= x <= domain_width, new_individu)):
new_individu = neighb_square((parent1+parent2)/2, variation_scale, domain_width)
assert all(map(lambda x: 0<= x <= domain_width, new_individu))
new_population.append(np.array(new_individu))
individual_index = individual_index + 1 % len(population)
individual_index = (individual_index + 1) % len(population)
return np.array(new_population)
......
......@@ -140,10 +140,11 @@ if __name__ == "__main__":
)
can.add_argument(
"--variance",
"--quality-threshold",
metavar="DVAL",
default=1,
help="Variance of the multivariance distribution."
default=600,
type=float,
help="Quality threshold. Used to plot the probability of a run being under the quality threshold.",
)
the = can.parse_args()
......@@ -312,20 +313,19 @@ if __name__ == "__main__":
make.variation(
num.variation,
nb_offspring=the.nb_offspring,
covariance_mat=np.array([[the.variance, 0], [0, the.variance]]),
dim=d,
scale=the.domain_width,
domain_width=the.domain_width,
variation_scale=the.variation_scale,
nb_sensors=the.nb_sensors,
),
make.replacement(
num.replacement,
nb_next_generation=the.nb_selected,
strat=None,
),
iters
iters,
)
sensors = num.to_sensors(sol)
# Fancy output.
print("\n{} : {}".format(val, sensors))
......@@ -340,12 +340,20 @@ if __name__ == "__main__":
f = make.func(
num.cover_sum,
domain_width=the.domain_width,
sensor_range=the.sensor_range * the.domain_width,
sensor_range=the.sensor_range,
dim=d * the.nb_sensors
)
plot.surface(ax1, shape, f)
plot.path(ax1, shape, history)
else:
ax2 = fig.add_subplot(111)
ax2 = fig.add_subplot(121)
ax3 = fig.add_subplot(122)
values = np.array([epoch[0] for epoch in history])
values = values >= the.quality_threshold
proba = values
ax3.step(np.arange(len(history)), proba)
domain = np.zeros(shape)
domain = pb.coverage(domain, sensors, the.sensor_range * the.domain_width)
......
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