better end plot = fix fallback to RS

This commit is contained in:
Johann Dreo 2018-12-16 20:26:19 +01:00
commit 0d40f5c246
5 changed files with 72 additions and 46 deletions

View file

@ -12,7 +12,7 @@ def random(func, init, again):
while again(i, val, sol):
sol = init()
val = func(sol)
if val > best_val:
if val >= best_val:
best_val = val
best_sol = sol
i += 1
@ -28,7 +28,8 @@ def greedy(func, init, neighb, again):
while again(i, best_val, best_sol):
sol = neighb(best_sol)
val = func(sol)
if val > best_val:
# Use >= and not >, so as to fallback to random walk on plateus.
if val >= best_val:
best_val = val
best_sol = sol
i += 1

View file

@ -10,6 +10,7 @@ def max(i, val, sol, nb_it):
else:
return False
# Stopping criterions that are actually just checkpoints.
def several(i, val, sol, agains):

View file

@ -25,3 +25,45 @@ def coverage(domain, sensors, sensor_range):
return domain
def line(x0, y0, x1, y1):
"""Compute the set of pixels (integer coordinates) of the line
between the given line (x0,y0) -> (x1,y1).
Use the Bresenham's algorithm.
This make a generator that yield the start and the end points.
"""
dx = x1 - x0
dy = y1 - y0
if dx > 0:
xs = 1
else:
xs = -1
if dy > 0:
ys = 1
else:
xs = -1
dx = abs(dx)
dy = abs(dy)
if dx > dy:
ax, xy, yx, ay = xs, 0, 0, ys
else:
dx, dy = dy, dx
ax, xy, yx, ay = 0, ys, xs, 0
D = 2 * dy - dx
y = 0
for x in range(dx + 1):
yield x0 + x*ax + y*yx , y0 + x*xy + y*ay
if D >= 0:
y += 1
D -= 2 * dx
D += 2 * dy
#TODO maximin trajectories

View file

@ -18,7 +18,7 @@ def surface(ax, shape, f):
Z = np.zeros( shape )
for y in range(shape[0]):
for x in range(shape[1]):
Z[y][x] = f( (x,y), shape[0]/2 )
Z[y][x] = f( (x,y) )
X = np.arange(0,shape[0],1)
Y = np.arange(0,shape[1],1)
@ -61,38 +61,3 @@ def highlight_sensors(domain, sensors, val=2):
domain[y(s)][x(s)] = val
return domain
if __name__=="__main__":
import snp
w = 100
shape = (w,w)
history = []
val,sol = snp.greedy(
snp.make_func(sphere,
offset = w/2),
snp.make_init(snp.num_rand,
dim = 2 * 1,
scale = w),
snp.make_neig(snp.num_neighb_square,
scale = w/10),
snp.make_iter(
snp.several,
agains = [
snp.make_iter(snp.iter_max,
nb_it = 100),
snp.make_iter(snp.history,
history = history)
]
)
)
sensors = snp.num_to_sensors(sol)
#print("\n".join([str(i) for i in history]))
fig = plt.figure()
ax = fig.gca(projection='3d')
surface(ax, shape, sphere)
path(ax, shape, history)
plt.show()