Skip to content
Snippets Groups Projects
Commit a3e8af21 authored by Carlos Santos Garcia's avatar Carlos Santos Garcia
Browse files

crazy ant implemented

parent 0cb4a535
No related branches found
No related tags found
No related merge requests found
...@@ -63,7 +63,7 @@ def proba(i, alpha, tau, n1_size, n2_size, n3_size, n_cbx, n_cby, n_cbz): ...@@ -63,7 +63,7 @@ def proba(i, alpha, tau, n1_size, n2_size, n3_size, n_cbx, n_cby, n_cbz):
return (sequence, weights) return (sequence, weights)
def compute_path(tau, alpha, n1_size, n2_size, n3_size): def compute_path(tau, alpha, n1_size, n2_size, n3_size, fancy_strategy = 'AS'):
"""From the sizes of the problem and the pheromon matrix, give a ant path. """From the sizes of the problem and the pheromon matrix, give a ant path.
Args: Args:
...@@ -84,9 +84,33 @@ def compute_path(tau, alpha, n1_size, n2_size, n3_size): ...@@ -84,9 +84,33 @@ def compute_path(tau, alpha, n1_size, n2_size, n3_size):
path = [] path = []
sequence, weights = proba(0, alpha, tau, n1_size, n2_size, n3_size, n_cbx, n_cby, n_cbz) sequence, weights = proba(0, alpha, tau, n1_size, n2_size, n3_size, n_cbx, n_cby, n_cbz)
if fancy_strategy == 'crazy':
r = random.random()
if r < 0.1:
crazy = True
else:
crazy = False
new_node = rd.choices(sequence,weights)[0] new_node = rd.choices(sequence,weights)[0]
path.append(new_node) path.append(new_node)
if crazy:
# A crazy ant chooses a completely random path
new_node = rd.choices(sequence,np.ones(len(sequence)))[0]
path.append(new_node)
for i in range(5):
sequence, _ = proba(path[i], alpha, tau, n1_size, n2_size, n3_size, n_cbx, n_cby, n_cbz)
new_node = rd.choices(sequence,np.ones(len(sequence)))[0]
path.append(new_node)
if i == 1:
n1 = 256 * (2**(path[0]-1))
n2 = 256 * (2**(path[1] - n1_size - 1))
n3 = 256 * (2**(path[2] - n1_size - n2_size - 1))
n_cbx, n_cby, n_cbz = n1//16, n2, n3
else:
# A classi ant uses pheromones to choose its path
for i in range(5): for i in range(5):
sequence, weights = proba(path[i], alpha, tau, n1_size, n2_size, n3_size, n_cbx, n_cby, n_cbz) sequence, weights = proba(path[i], alpha, tau, n1_size, n2_size, n3_size, n_cbx, n_cby, n_cbz)
new_node = rd.choices(sequence,weights)[0] new_node = rd.choices(sequence,weights)[0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment