|
| 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