Skip to content

Commit 40d3471

Browse files
authored
Merge pull request kodecocodes#369 from ghost/sa
Simulated annealing
2 parents 95ed914 + a794dec commit 40d3471

File tree

3 files changed

+363
-0
lines changed

3 files changed

+363
-0
lines changed

Simulated annealing/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Simulated annealing
2+
3+
Simulated Annealing is a nature inspired global optimization technique and a metaheuristic to approximate global maxima in a (often discrete)large search space. The name comes from the process of annealing in metallurgy where a material is heated and cooled down under controlled conditions in order to improve its strength and durabilility. The objective is to find a minimum cost solution in the search space by exploiting properties of a thermodynamic system.
4+
Unlike hill climbing techniques which usually gets stuck in a local maxima ( downward moves are not allowed ), simulated annealing can escape local maxima. The interesting property of simulated annealing is that probability of allowing downward moves is high at the high temperatures and gradually reduced as it cools down. In other words, high temperature relaxes the acceptance criteria for the search space and triggers chaotic behavior of acceptance function in the algorithm (e.x initial/high temperature stages) which should make it possible to escape from local maxima and cooler temperatures narrows it and focuses on improvements.
5+
6+
Pseucocode
7+
8+
Input: initial, temperature, coolingRate, acceptance
9+
Output: Sbest
10+
Scurrent <- CreateInitialSolution(initial)
11+
Sbest <- Scurrent
12+
while temperature is not minimum:
13+
Snew <- FindNewSolution(Scurrent)
14+
if acceptance(Energy(Scurrent), Energy(Snew), temperature) > Rand():
15+
Scurrent = Snew
16+
if Energy(Scurrent) < Energy(Sbest):
17+
Sbest = Scurrent
18+
temperature = temperature * (1-coolingRate)
19+
20+
Common acceptance criteria :
21+
22+
P(accept) <- exp((e-ne)/T) where
23+
e is the current energy ( current solution ),
24+
ne is new energy ( new solution ),
25+
T is current temperature.
26+
27+
28+
We use this algorithm to solve a Travelling salesman problem instance with 20 cities. The code is in `simann_example.swift`
29+
30+
#See also
31+
32+
[Simulated annealing on Wikipedia](https://en.wikipedia.org/wiki/Simulated_annealing)
33+
34+
[Travelling salesman problem](https://en.wikipedia.org/wiki/Travelling_salesman_problem)
35+
36+
Written for Swift Algorithm Club by [Mike Taghavi](https://github.com/mitghi)

Simulated annealing/simann.swift

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// The MIT License (MIT)
2+
// Copyright (c) 2017 Mike Taghavi (mitghi[at]me.com)
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
// The above copyright notice and this permission notice shall be included in all
10+
// copies or substantial portions of the Software.
11+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17+
// SOFTWARE.
18+
19+
#if os(OSX)
20+
import Foundation
21+
#elseif os(Linux)
22+
import Glibc
23+
#endif
24+
25+
public extension Double {
26+
public static func random(_ lower: Double, _ upper: Double) -> Double {
27+
#if os(OSX)
28+
return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
29+
#elseif os(Linux)
30+
return (Double(random()) / 0xFFFFFFFF) * (upper - lower) + lower
31+
#endif
32+
}
33+
}
34+
35+
protocol Clonable {
36+
init(current: Self)
37+
}
38+
39+
// MARK: - create a clone from instance
40+
41+
extension Clonable {
42+
func clone() -> Self {
43+
return Self.init(current: self)
44+
}
45+
}
46+
47+
protocol SAObject: Clonable {
48+
var count: Int { get }
49+
func randSwap(a: Int, b: Int)
50+
func currentEnergy() -> Double
51+
func shuffle()
52+
}
53+
54+
// MARK: - create a new copy of elements
55+
56+
extension Array where Element: Clonable {
57+
func clone() -> Array {
58+
var newArray = Array<Element>()
59+
for elem in self {
60+
newArray.append(elem.clone())
61+
}
62+
63+
return newArray
64+
}
65+
}
66+
67+
typealias AcceptanceFunc = (Double, Double, Double) -> Double
68+
69+
func SimulatedAnnealing<T: SAObject>(initial: T, temperature: Double, coolingRate: Double, acceptance: AcceptanceFunc) -> T {
70+
// Step 1:
71+
// Calculate the initial feasible solution based on a random permutation.
72+
// Set best and current solutions to initial solution
73+
74+
var temp: Double = temperature
75+
var currentSolution = initial.clone()
76+
currentSolution.shuffle()
77+
var bestSolution = currentSolution.clone()
78+
79+
// Step 2:
80+
// Repeat while the system is still hot
81+
// Randomly modify the current solution by swapping its elements
82+
// Randomly decide if the new solution ( neighbor ) is acceptable and set current solution accordingly
83+
// Update the best solution *iff* it had improved ( lower energy = improvement )
84+
// Reduce temperature
85+
86+
while temp > 1 {
87+
let newSolution: T = currentSolution.clone()
88+
let pos1: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
89+
let pos2: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
90+
newSolution.randSwap(a: pos1, b: pos2)
91+
let currentEnergy: Double = currentSolution.currentEnergy()
92+
let newEnergy: Double = newSolution.currentEnergy()
93+
94+
if acceptance(currentEnergy, newEnergy, temp) > Double.random(0, 1) {
95+
currentSolution = newSolution.clone()
96+
}
97+
if currentSolution.currentEnergy() < bestSolution.currentEnergy() {
98+
bestSolution = currentSolution.clone()
99+
}
100+
101+
temp *= 1-coolingRate
102+
}
103+
104+
return bestSolution
105+
}
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
// The MIT License (MIT)
2+
// Copyright (c) 2017 Mike Taghavi (mitghi[at]me.com)
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
// The above copyright notice and this permission notice shall be included in all
10+
// copies or substantial portions of the Software.
11+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17+
// SOFTWARE.
18+
19+
#if os(OSX)
20+
import Foundation
21+
#elseif os(Linux)
22+
import Glibc
23+
#endif
24+
25+
public extension Double {
26+
27+
public static func random(_ lower: Double, _ upper: Double) -> Double {
28+
#if os(OSX)
29+
return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
30+
#elseif os(Linux)
31+
return (Double(random()) / 0xFFFFFFFF) * (upper - lower) + lower
32+
#endif
33+
}
34+
}
35+
36+
protocol Clonable {
37+
init(current: Self)
38+
}
39+
40+
extension Clonable {
41+
func clone() -> Self {
42+
return Self.init(current: self)
43+
}
44+
}
45+
46+
protocol SAObject: Clonable {
47+
var count: Int { get }
48+
func randSwap(a: Int, b: Int)
49+
func currentEnergy() -> Double
50+
func shuffle()
51+
}
52+
53+
// MARK: - create a new copy of elements
54+
55+
extension Array where Element: Clonable {
56+
func clone() -> Array {
57+
var newArray = Array<Element>()
58+
for elem in self {
59+
newArray.append(elem.clone())
60+
}
61+
62+
return newArray
63+
}
64+
}
65+
66+
typealias Points = [Point]
67+
typealias AcceptanceFunc = (Double, Double, Double) -> Double
68+
69+
class Point: Clonable {
70+
var x: Int
71+
var y: Int
72+
73+
init(x: Int, y: Int) {
74+
self.x = x
75+
self.y = y
76+
}
77+
78+
required init(current: Point){
79+
self.x = current.x
80+
self.y = current.y
81+
}
82+
}
83+
84+
// MARK: - string representation
85+
86+
extension Point: CustomStringConvertible {
87+
public var description: String {
88+
return "Point(\(x), \(y))"
89+
}
90+
}
91+
92+
// MARK: - return distance between two points using operator '<->'
93+
94+
infix operator <->: AdditionPrecedence
95+
extension Point {
96+
static func <-> (left: Point, right: Point) -> Double {
97+
let xDistance = (left.x - right.x)
98+
let yDistance = (left.y - right.y)
99+
100+
return Double(sqrt(Double((xDistance * xDistance) + (yDistance * yDistance))))
101+
}
102+
}
103+
104+
105+
class Tour: SAObject {
106+
var tour: Points
107+
var energy: Double = 0.0
108+
var count: Int {
109+
get {
110+
return self.tour.count
111+
}
112+
}
113+
114+
init(points: Points){
115+
self.tour = points.clone()
116+
}
117+
118+
required init(current: Tour) {
119+
self.tour = current.tour.clone()
120+
}
121+
}
122+
123+
// MARK: - calculate current tour distance ( energy ).
124+
125+
extension Tour {
126+
func randSwap(a: Int, b: Int) -> Void {
127+
let (cpos1, cpos2) = (self[a], self[b])
128+
self[a] = cpos2
129+
self[b] = cpos1
130+
}
131+
132+
func shuffle() {
133+
for i in stride(from: self.count - 1, through: 1, by: -1) {
134+
let j = Int(arc4random()) % (i + 1)
135+
if i != j {
136+
swap(&self.tour[i], &self.tour[j])
137+
}
138+
}
139+
}
140+
141+
func currentEnergy() -> Double {
142+
if self.energy == 0 {
143+
var tourEnergy: Double = 0.0
144+
for i in 0..<self.count {
145+
let fromCity = self[i]
146+
var destCity = self[0]
147+
if i+1 < self.count {
148+
destCity = self[i+1]
149+
}
150+
let e = fromCity<->destCity
151+
tourEnergy = tourEnergy + e
152+
153+
}
154+
self.energy = tourEnergy
155+
}
156+
return self.energy
157+
}
158+
159+
}
160+
161+
// MARK: - subscript to manipulate elements of Tour.
162+
163+
extension Tour {
164+
subscript(index: Int) -> Point {
165+
get {
166+
return self.tour[index]
167+
}
168+
set(newValue) {
169+
self.tour[index] = newValue
170+
}
171+
}
172+
}
173+
174+
func SimulatedAnnealing<T: SAObject>(initial: T, temperature: Double, coolingRate: Double, acceptance: AcceptanceFunc) -> T {
175+
var temp: Double = temperature
176+
var currentSolution = initial.clone()
177+
currentSolution.shuffle()
178+
var bestSolution = currentSolution.clone()
179+
print("Initial solution: ", bestSolution.currentEnergy())
180+
181+
while temp > 1 {
182+
let newSolution: T = currentSolution.clone()
183+
let pos1: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
184+
let pos2: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
185+
newSolution.randSwap(a: pos1, b: pos2)
186+
let currentEnergy: Double = currentSolution.currentEnergy()
187+
let newEnergy: Double = newSolution.currentEnergy()
188+
189+
if acceptance(currentEnergy, newEnergy, temp) > Double.random(0, 1) {
190+
currentSolution = newSolution.clone()
191+
}
192+
if currentSolution.currentEnergy() < bestSolution.currentEnergy() {
193+
bestSolution = currentSolution.clone()
194+
}
195+
196+
temp *= 1-coolingRate
197+
}
198+
199+
print("Best solution: ", bestSolution.currentEnergy())
200+
return bestSolution
201+
}
202+
203+
let points: [Point] = [
204+
(60 , 200), (180, 200), (80 , 180), (140, 180),
205+
(20 , 160), (100, 160), (200, 160), (140, 140),
206+
(40 , 120), (100, 120), (180, 100), (60 , 80) ,
207+
(120, 80) , (180, 60) , (20 , 40) , (100, 40) ,
208+
(200, 40) , (20 , 20) , (60 , 20) , (160, 20) ,
209+
].map{ Point(x: $0.0, y: $0.1) }
210+
211+
let acceptance : AcceptanceFunc = {
212+
(e: Double, ne: Double, te: Double) -> Double in
213+
if ne < e {
214+
return 1.0
215+
}
216+
return exp((e - ne) / te)
217+
}
218+
219+
let result: Tour = SimulatedAnnealing(initial : Tour(points: points),
220+
temperature : 100000.0,
221+
coolingRate : 0.003,
222+
acceptance : acceptance)

0 commit comments

Comments
 (0)