Skip to content

Commit

Permalink
Fixes Issue#371: Simulated Annealing demo crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jen authored and Jen committed Aug 3, 2018
1 parent 353960a commit 2b932ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
16 changes: 3 additions & 13 deletions Simulated annealing/simann.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
import Glibc
#endif

public extension Double {
public static func random(_ lower: Double, _ upper: Double) -> Double {
#if os(OSX)
return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
#elseif os(Linux)
return (Double(random()) / 0xFFFFFFFF) * (upper - lower) + lower
#endif
}
}

protocol Clonable {
init(current: Self)
}
Expand Down Expand Up @@ -85,13 +75,13 @@ func SimulatedAnnealing<T: SAObject>(initial: T, temperature: Double, coolingRat

while temp > 1 {
let newSolution: T = currentSolution.clone()
let pos1: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
let pos2: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
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(0, 1) {
if acceptance(currentEnergy, newEnergy, temp) > Double.random(in: 0 ..< 1) {
currentSolution = newSolution.clone()
}
if currentSolution.currentEnergy() < bestSolution.currentEnergy() {
Expand Down
36 changes: 10 additions & 26 deletions Simulated annealing/simann_example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@

#if os(OSX)
import Foundation
import Cocoa
#elseif os(Linux)
import Glibc
#endif

public extension Double {

public static func random(_ lower: Double, _ upper: Double) -> Double {
#if os(OSX)
return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
#elseif os(Linux)
return (Double(random()) / 0xFFFFFFFF) * (upper - lower) + lower
#endif
}
}

protocol Clonable {
init(current: Self)
}
Expand Down Expand Up @@ -96,8 +86,8 @@ extension Point {
static func <-> (left: Point, right: Point) -> Double {
let xDistance = (left.x - right.x)
let yDistance = (left.y - right.y)
return Double(sqrt(Double((xDistance * xDistance) + (yDistance * yDistance))))

return Double((xDistance * xDistance) + (yDistance * yDistance)).squareRoot()
}
}

Expand Down Expand Up @@ -128,15 +118,6 @@ extension Tour {
self[a] = cpos2
self[b] = cpos1
}

func shuffle() {
for i in stride(from: self.count - 1, through: 1, by: -1) {
let j = Int(arc4random()) % (i + 1)
if i != j {
swap(&self.tour[i], &self.tour[j])
}
}
}

func currentEnergy() -> Double {
if self.energy == 0 {
Expand All @@ -155,7 +136,10 @@ extension Tour {
}
return self.energy
}


func shuffle() {
self.shuffle()
}
}

// MARK: - subscript to manipulate elements of Tour.
Expand All @@ -180,13 +164,13 @@ func SimulatedAnnealing<T: SAObject>(initial: T, temperature: Double, coolingRat

while temp > 1 {
let newSolution: T = currentSolution.clone()
let pos1: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
let pos2: Int = Int(arc4random_uniform(UInt32(newSolution.count)))
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(0, 1) {
if acceptance(currentEnergy, newEnergy, temp) > Double.random(in: 0 ..< 1) {
currentSolution = newSolution.clone()
}
if currentSolution.currentEnergy() < bestSolution.currentEnergy() {
Expand Down

0 comments on commit 2b932ac

Please sign in to comment.