forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simann_example.swift
206 lines (171 loc) · 5.37 KB
/
simann_example.swift
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// The MIT License (MIT)
// Copyright (c) 2017 Mike Taghavi (mitghi[at]me.com)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if os(OSX)
import Foundation
import Cocoa
#elseif os(Linux)
import Glibc
#endif
protocol Clonable {
init(current: Self)
}
extension Clonable {
func clone() -> Self {
return Self.init(current: self)
}
}
protocol SAObject: Clonable {
var count: Int { get }
func randSwap(a: Int, b: Int)
func currentEnergy() -> Double
func shuffle()
}
// MARK: - create a new copy of elements
extension Array where Element: Clonable {
func clone() -> Array {
var newArray = Array<Element>()
for elem in self {
newArray.append(elem.clone())
}
return newArray
}
}
typealias Points = [Point]
typealias AcceptanceFunc = (Double, Double, Double) -> Double
class Point: Clonable {
var x: Int
var y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
required init(current: Point){
self.x = current.x
self.y = current.y
}
}
// MARK: - string representation
extension Point: CustomStringConvertible {
public var description: String {
return "Point(\(x), \(y))"
}
}
// MARK: - return distance between two points using operator '<->'
infix operator <->: AdditionPrecedence
extension Point {
static func <-> (left: Point, right: Point) -> Double {
let xDistance = (left.x - right.x)
let yDistance = (left.y - right.y)
return Double((xDistance * xDistance) + (yDistance * yDistance)).squareRoot()
}
}
class Tour: SAObject {
var tour: Points
var energy: Double = 0.0
var count: Int {
get {
return self.tour.count
}
}
init(points: Points){
self.tour = points.clone()
}
required init(current: Tour) {
self.tour = current.tour.clone()
}
}
// MARK: - calculate current tour distance ( energy ).
extension Tour {
func randSwap(a: Int, b: Int) -> Void {
let (cpos1, cpos2) = (self[a], self[b])
self[a] = cpos2
self[b] = cpos1
}
func currentEnergy() -> Double {
if self.energy == 0 {
var tourEnergy: Double = 0.0
for i in 0..<self.count {
let fromCity = self[i]
var destCity = self[0]
if i+1 < self.count {
destCity = self[i+1]
}
let e = fromCity<->destCity
tourEnergy = tourEnergy + e
}
self.energy = tourEnergy
}
return self.energy
}
func shuffle() {
self.tour.shuffle()
}
}
// MARK: - subscript to manipulate elements of Tour.
extension Tour {
subscript(index: Int) -> Point {
get {
return self.tour[index]
}
set(newValue) {
self.tour[index] = newValue
}
}
}
func SimulatedAnnealing<T: SAObject>(initial: T, temperature: Double, coolingRate: Double, acceptance: AcceptanceFunc) -> T {
var temp: Double = temperature
var currentSolution = initial.clone()
currentSolution.shuffle()
var bestSolution = currentSolution.clone()
print("Initial solution: ", bestSolution.currentEnergy())
while temp > 1 {
let newSolution: T = currentSolution.clone()
let pos1: Int = Int.random(in: 0 ..< newSolution.count)
let pos2: Int = Int.random(in: 0 ..< newSolution.count)
newSolution.randSwap(a: pos1, b: pos2)
let currentEnergy: Double = currentSolution.currentEnergy()
let newEnergy: Double = newSolution.currentEnergy()
if acceptance(currentEnergy, newEnergy, temp) > Double.random(in: 0 ..< 1) {
currentSolution = newSolution.clone()
}
if currentSolution.currentEnergy() < bestSolution.currentEnergy() {
bestSolution = currentSolution.clone()
}
temp *= 1-coolingRate
}
print("Best solution: ", bestSolution.currentEnergy())
return bestSolution
}
let points: [Point] = [
(60 , 200), (180, 200), (80 , 180), (140, 180),
(20 , 160), (100, 160), (200, 160), (140, 140),
(40 , 120), (100, 120), (180, 100), (60 , 80) ,
(120, 80) , (180, 60) , (20 , 40) , (100, 40) ,
(200, 40) , (20 , 20) , (60 , 20) , (160, 20) ,
].map{ Point(x: $0.0, y: $0.1) }
let acceptance : AcceptanceFunc = {
(e: Double, ne: Double, te: Double) -> Double in
if ne < e {
return 1.0
}
return exp((e - ne) / te)
}
let result: Tour = SimulatedAnnealing(initial : Tour(points: points),
temperature : 100000.0,
coolingRate : 0.003,
acceptance : acceptance)