Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- def d1(a, b): return abs(a - b) + (1 + b - a)*((a == 10 and b == 1) or (a == 1 and b == 10))
- def d2(a, b):
- if (a == 1 and b == 2) or (a == 9 and b == 10): return 1
- else:
- return (a != b)*(a**3 + b**3 - a**2*b - b**2*a + 4*a**2 - 4*b**2 + 4*a + 4*b) if b <= a else d2(b, a)
- def E(perm, d): return sum(d(perm[i], perm[i+1]) for i in range(9))
- def nowe_s(S):
- i = random.randint(1, 9)
- j = random.randint(0, i - 1)
- S[i], S[j] = S[j], S[i]
- return S
- def accept(T, S, S1, d):
- rand = random.random()
- return E(S1, d) - E(S, d) < 0 or rand < math.e**((E(S, d) - E(S1, d))/T)
- def nowe_t(t, T):
- return T/(1+math.log(t+1))
- def annealing(M, L, T0, d):
- S = list(range(1, 11))
- random.shuffle(S) # losowy start
- t, accsol, T = 0, 0, T0
- while accsol < L:
- for x in range(0, M):
- S1 = nowe_s(S)
- if accept(T, S, S1, d):
- S = S1
- accsol += 1
- T = nowe_t(t, T)
- t += 1
- return S
- pom = annealing(10, 3, 100, d1)
- while E(pom , d1) > 15:
- pom = annealing(10, 3, 100, d1)
- print(pom, E(pom, d1))
- pom = annealing(10, 3, 100, d2)
- print(pom, E(pom, d2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement