-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantoptimizer.py
78 lines (63 loc) · 3.19 KB
/
antoptimizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import os
from problem import Problem
from ant import Ant
class AntOptimizer:
# AntOptimizer performs the optimization process for a given problem
# attributes:
# pro: Problem with given parameters
# maxit: amount of iterations for one optimization process
# solution: order and length of known solution
# bestvalues: array of best solution for every iteration
# bestsamples: array of arrays: bestvalues for several repetitions of optimization process
def __init__(self, problemname, ro = 0.5, alpha = 1., beta = 1.5, maxiterations = 100, antnumber = 20, solution = 'False'):
self.pro = Problem(problemname, ro, alpha, beta)
self.pro.antsinit(antnumber)
self.maxit = maxiterations
if solution:
self.solutiontour, self.solutionlength = self.getsolution()
self.bestvalues = np.zeros(self.maxit)
self.besttour = np.zeros(self.pro.size)
self.bestsamples = np.zeros(self.maxit)
print('\nAnt colony optimization for Travelling Salesman problem: ', self.pro.name, ' with ', antnumber, ' ants')
# get path and length of known solution
# OUT: array of indices, length
def getsolution(self):
sol = self.pro.readsol()
solant = Ant(self.pro.size)
optlen = solant.solutionwalk(sol, self.pro.distances)
return sol, optlen
# do the optimization:
# in each iteration, all ants find a solution, the problem gets updated,
# the best solution and it's length are stored
def optimization(self):
self.pro.currentbest = self.pro.initguess()
print('initial guess: ', self.pro.currentbest)
for iteration in range(self.maxit):
#print('\n_______________________ iteration:', iteration, '____________________________\n')
for index, ant in enumerate(self.pro.ants):
ant.walk(0, self.pro.currentbest, self.pro.distances, self.pro.probabilities)
#print('ant index: ', index, '; ant tourlength = ', ant.tourlength, '; candidate? ', ant.candidate)
self.pro.decay()
self.pro.update()
self.pro.weigh()
self.bestvalues[iteration] = self.pro.currentbest
self.besttour = np.copy(self.pro.bestant.tour)
#print('current best: ', self.pro.currentbest)
#print('best tour by: ', self.pro.bestant)
for ant in self.pro.ants:
ant.reset()
print('\nsolution found: ', self.pro.currentbest)
print('found best tour: ', self.besttour)
print('\nknown solution: ' , self.solutionlength)
print('known best tour: ', self.solutiontour)
# does (samples) repetitions of the optimization process to get statistics
def optsamples(self, samples = 500):
self.bestsamples = np.zeros((self.maxit, samples))
for s in range(samples):
self.optimization()
self.bestsamples[:,s] = np.copy(self.bestvalues)
self.pro.reset()
return np.mean(self.bestsamples, axis = 1)