In [1]:
import numpy as np
import random
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from functions.plot_data import plot_data
from functions.visib import visib
from functions.prob_city import prob_city
from functions.tau_branch import tau_branch
from functions.tau_prune import tau_prune
from Ant import Ant
from Colony import Colony
from functions.tsp import tsp
from functions.colSelection import colSelection
from functions.colCreate import colCreate
from functions.fitness import fitness
from functions.colMutate import colMutate
from functions.colEvo import colEvo
In [3]:
# Get visibility matrix
n_mat = visib()
# Get the number of cities
N = len(n_mat[0,:])
''' Ants '''
# Define number of ants and list object that will hold them
number_of_ants = N
# Define global ants characteristics
#alpha = 1 # Pheromone effect
beta = 5 # Greed
Q = 100
init_city = 0
''' Stochastic Options'''
# Define random characteristics
random.seed(1)
''' Initialization '''
iters = 50 # Number of ant cycles
nColonies = 2
alpha_min = 0.99
alpha_max = 1.01
d_alpha_max = 0.01 # Max evo shift in alpha
# Fitness function parameters
c0 = 0
c1 = 1000
In [4]:
# Initialize a list of ant colonies
colony_list = colCreate(nColonies, number_of_ants, N, alpha_min,
alpha_max, beta, Q)
In [5]:
# Cycle each colony through TSP 'iters' times
for colony in colony_list:
colony = tsp(colony, iters, n_mat)
# Update minimum and total paths for the colony
colony.getMinL()
colony.getTotL()
In [6]:
for colony in colony_list:
print(colony.minL)
print(colony.L)
In [5]:
# Colony selection initialization
col_list = colony_list
nCols = len(col_list)
fitness_list = []
In [14]:
# Play with built in colony evolution method
random.seed(1)
col = col_list[1]
print(col.getAlphaDist())
#plt.hist(col.getAlphaDist())
#plt.show()
col.mutate(d_alpha_max)
print(col.getAlphaDist())
#plt.hist(col.getAlphaDist())
#plt.show()
print(col.minL)
In [12]:
# Colony selection - Get fitness list and total fitness
for colony in col_list:
fitness_list.append(fitness(colony, c0, c1))
totFit = sum(fitness_list)
# This array holds votes for each colony in col_list
colonyVotes = np.zeros(nCols)
print(fitness_list)
print(totFit)
In [13]:
# Colony selection - calculate votes
colonyVotes = np.zeros(nCols)
# Iterate over the total number of new colonies to be created
for j in range(nCols):
# Assign random number over size of total fitness
temp1 = random.uniform(0, totFit)
#print("Temp1 = " + str(temp1))
# Initialize an accumulator
tot = 0
# Get cumulative sum of fitnesses to decide on colony
for i in range(nCols):
# Update cumulative sum
tot = tot + fitness_list[i]
#print("Tot = " + str(tot))
# If less than the random number, increase vote
if temp1<tot:
colonyVotes[i] = colonyVotes[i] + 1
break
print(colonyVotes)
In [11]:
# General program parm initialization
''' Initialization '''
iters = 10 # Number of ant cycles
nColonies = 5
alpha_min = 0.5
alpha_max = 1.5
d_alpha_max = 0.1 # Max evo shift in alpha
evo_cycles = 5
fitness_mat = np.zeros((evo_cycles, nColonies))
# Fitness function parameters
c0 = 0
c1 = 1000
# Initialize a list of ant colonies
colony_list = colCreate(nColonies, number_of_ants, N, alpha_min,
alpha_max, beta, Q)
In [12]:
# Run tsp and evolution
for i in range(evo_cycles):
j = 0
# Cycle each colony through TSP 'iters' times
for colony in colony_list:
colony = tsp(colony, iters, n_mat)
# Update minimum and total paths for the colony
colony.getMinL()
colony.getTotL()
fitness_mat[i,j]=fitness(colony, c0, c1)
j = j+1
#colony_list = colEvo(colony_list, c0, c1, d_alpha_max)
In [39]:
# Get example colony
colony = colony_list[1]
colony.minL
Out[39]:
In [15]:
offspring_list = colonyVotes
print(offspring_list)
In [16]:
new_col_list = colEvo(col_list, c0, c1, d_alpha_max)
In [19]:
# Compare new and old colony lists
for colony in col_list:
print(colony.getAlphaDist())
In [20]:
for colony in new_col_list:
print(colony.getAlphaDist())
In [47]:
# Not the same! colEvo isn't doing what I expect. It's just copying colonies, not mutating
# colEvo
nColonies = len(col_list)
# Get number of offspring for each colony
offspring_list = colSelection(col_list, c0, c1)
print(offspring_list)
new_col_list = []
# Iterate over list of offspring
for i in range(nColonies):
# Get number of children for colony i
children = offspring_list[i]
if int(children)==0:
continue
for j in range(int(children)):
# Append mutated colony i for each number of children
print("Copying colony " + str(i))
print(col_list[i].getAlphaDist())
new_col_list.append(colMutate(col_list[i], d_alpha_max))
print(new_col_list[-1].getAlphaDist())
In [1]:
import numpy as np
from time import time
alphaTest = np.loadtxt('alphaBestMin2.txt', delimiter=',', skiprows=1)
fitnessTest = np.loadtxt('fitness_mat2.txt', delimiter=',', skiprows=1)
lMinTest = np.zeros((10,10)) # Nonsense to test
In [3]:
from functions.saveFiles import saveFiles
saveFiles(fitnessTest, alphaTest, lMinTest, 100, 5,
1, 1, 1, 1, int(time()))
In [2]:
from functions.antplot import plotAlphaDist
In [3]:
plotAlphaDist(alphaTest)
In [2]:
from time import time
int(time())
Out[2]:
In [ ]: