Skip to content

Commit 290bb3d

Browse files
committed
made api more swift3 friendly
1 parent 324ebbf commit 290bb3d

File tree

9 files changed

+35
-35
lines changed

9 files changed

+35
-35
lines changed

Sources/Backtrack.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@
3232
/// - parameter lcv: SHould it use the lcv heuristic to try to improve performance (default false) NOT IMPLEMENTED YET
3333
/// - parameter mac3: SHould it use the mac3 heuristic to try to improve performance (default false) NOT IMPLEMENTED YET
3434
/// - returns: the assignment (solution), or nil if none can be found
35-
public func backtrackingSearch<V, D>(_ csp: CSP<V, D>, assignment: Dictionary<V, D> = Dictionary<V, D>(), mrv: Bool = false, lcv: Bool = false, mac3: Bool = false) -> Dictionary<V, D>?
35+
public func backtrackingSearch<V, D>(csp: CSP<V, D>, assignment: Dictionary<V, D> = Dictionary<V, D>(), mrv: Bool = false, lcv: Bool = false, mac3: Bool = false) -> Dictionary<V, D>?
3636
{
3737
// assignment is complete if it has as many assignments as there are variables
3838
if assignment.count == csp.variables.count { return assignment }
3939

4040
// get a var to assign
41-
let variable = selectUnassignedVariable(csp, assignment: assignment, mrv: mrv)
41+
let variable = selectUnassignedVariable(csp: csp, assignment: assignment, mrv: mrv)
4242

4343
// get the domain of it and try each value in the domain
44-
for value in orderDomainValues(variable, assignment: assignment, csp: csp, lcv: lcv) {
44+
for value in orderDomainValues(variable: variable, assignment: assignment, csp: csp, lcv: lcv) {
4545

4646
// if the value is consistent with the current assignment we continue
4747
var localAssignment = assignment
4848
localAssignment[variable] = value
4949
//println(assignment)
50-
if isConsistent(variable, value: value, assignment: localAssignment, csp: csp) {
50+
if isConsistent(variable: variable, value: value, assignment: localAssignment, csp: csp) {
5151
//println("Found \(variable) with value \(value) and other assignment \(assignment) consistent")
5252

5353
// do inferencing if we have that turned on
@@ -62,7 +62,7 @@ public func backtrackingSearch<V, D>(_ csp: CSP<V, D>, assignment: Dictionary<V,
6262

6363
if (result != False) return result; */
6464
} else {
65-
if let result = backtrackingSearch(csp, assignment: localAssignment, mrv: mrv, mac3: mac3, lcv: lcv) {
65+
if let result = backtrackingSearch(csp: csp, assignment: localAssignment, mrv: mrv, mac3: mac3, lcv: lcv) {
6666
return result
6767
}
6868
}
@@ -75,9 +75,9 @@ public func backtrackingSearch<V, D>(_ csp: CSP<V, D>, assignment: Dictionary<V,
7575
}
7676

7777
/// check if the value assignment is consistent by checking all constraints of the variable
78-
func isConsistent<V, D>(_ variable: V, value: D, assignment: Dictionary<V, D>, csp: CSP<V,D>) -> Bool {
78+
func isConsistent<V, D>(variable: V, value: D, assignment: Dictionary<V, D>, csp: CSP<V,D>) -> Bool {
7979
for constraint in csp.constraints[variable]! { //assume there are constraints for every variable
80-
if !constraint.isSatisfied(assignment) {
80+
if !constraint.isSatisfied(assignment: assignment) {
8181
return false
8282
}
8383
}
@@ -86,7 +86,7 @@ func isConsistent<V, D>(_ variable: V, value: D, assignment: Dictionary<V, D>, c
8686

8787
/// Return an unassigned variable - we may want to use some logic here to return the
8888
/// minimum-remaining values
89-
func selectUnassignedVariable<V, D>(_ csp: CSP<V, D>, assignment: Dictionary<V, D>, mrv: Bool) -> V {
89+
func selectUnassignedVariable<V, D>(csp: CSP<V, D>, assignment: Dictionary<V, D>, mrv: Bool) -> V {
9090
// do we want to use the mrv heuristic
9191
if (mrv) {
9292
//get the one with the biggest domain
@@ -109,7 +109,7 @@ func selectUnassignedVariable<V, D>(_ csp: CSP<V, D>, assignment: Dictionary<V,
109109
}
110110

111111
/// get the domain variables in a good order
112-
func orderDomainValues<V, D>(_ variable: V, assignment: Dictionary<V,D>, csp: CSP<V,D>, lcv: Bool) -> [D] {
112+
func orderDomainValues<V, D>(variable: V, assignment: Dictionary<V,D>, csp: CSP<V,D>, lcv: Bool) -> [D] {
113113
return csp.domains[variable]! //asume there is a domain for every variable
114114
/*if lcv { //not implemented yet
115115
/*// currently works only for binary constraints

Sources/CSP.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public struct CSP <V: Hashable, D> {
4848
/// Add a constraint to the CSP. It will automatically be applied to all the variables it includes. It should only include variables actually in the CSP.
4949
///
5050
/// - parameter constraint: The constraint to add.
51-
public mutating func addConstraint(_ constraint: Constraint<V, D>) {
51+
public mutating func addConstraint(constraint: Constraint<V, D>) {
5252
for variable in constraint.vars {
5353
if !variables.contains(variable) {
5454
print("Error: Could not find variable \(variable) from constraint \(constraint) in CSP.", terminator: "")

Sources/Constraint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class Constraint <V: Hashable, D> {
3232
///
3333
/// - parameter assignment: Potential domain selections for variables that are part of the constraint.
3434
/// - returns: Whether the constraint is satisfied.
35-
public func isSatisfied(_ assignment: Dictionary<V, D>) -> Bool {
35+
public func isSatisfied(assignment: Dictionary<V, D>) -> Bool {
3636
return true
3737
}
3838
/// The variables that make up the constraint.

SwiftCSP/SwiftCSP/AppDelegate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4747
var variables = circuitBoards
4848
var domains: Dictionary<CircuitBoard, [(Int, Int)]> = Dictionary<CircuitBoard, [(Int, Int)]>()
4949
for variable in variables {
50-
domains[variable] = variable.generateDomain(boardWidth, boardHeight: boardHeight)
50+
domains[variable] = variable.generateDomain(boardWidth: boardWidth, boardHeight: boardHeight)
5151
}
5252

5353
var cb_csp: CSP<CircuitBoard, (Int, Int)> = CSP<CircuitBoard, (Int, Int)>(variables: variables, domains: domains)
@@ -56,14 +56,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5656
for i in 0..<variables.count {
5757
for j in (i+1)..<variables.count {
5858
let cbconst = CircuitBoardConstraint(variable1: variables[i], variable2: variables[j])
59-
cb_csp.addConstraint(cbconst)
59+
cb_csp.addConstraint(constraint: cbconst)
6060
//println(cbconst.variable1.width)
6161
//println(cbconst.variable2.width)
6262
}
6363
}
6464

6565
//run the solution and calculate the time it took
66-
if let solution = backtrackingSearch(cb_csp, mrv: true) {
66+
if let solution = backtrackingSearch(csp: cb_csp, mrv: true) {
6767
for (variable, location) in solution {
6868
variable.location = location
6969
}

SwiftCSP/SwiftCSP/CircuitBoard.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CircuitBoard: NSObject { //get hashable for free and dynamic access
3333
var location: (Int, Int)?
3434

3535
//generate the domain as a list of tuples of bottom left corners
36-
func generateDomain(_ boardWidth: Int, boardHeight: Int) -> [(Int, Int)] {
36+
func generateDomain(boardWidth: Int, boardHeight: Int) -> [(Int, Int)] {
3737
var domain: [(Int, Int)] = []
3838
for x in 0..<(boardWidth - width + 1) {
3939
for y in 0..<(boardHeight - height + 1) {

SwiftCSP/SwiftCSP/CircuitBoardConstraint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CircuitBoardConstraint: BinaryConstraint<CircuitBoard, (Int, Int)> {
3535
//println(self.variable2.width)
3636
}
3737

38-
override func isSatisfied(_ assignment: Dictionary<CircuitBoard, (Int, Int)>) -> Bool {
38+
override func isSatisfied(assignment: Dictionary<CircuitBoard, (Int, Int)>) -> Bool {
3939
//if either variable is not in the assignment then it must be consistent
4040
//since they still have their domain
4141
if assignment[variable1] == nil || assignment[variable2] == nil {

SwiftCSP/SwiftCSPTests/AustralianMapColoringTest.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class MapColoringConstraint: BinaryConstraint <String, String> {
3333
super.init(variable1: place1, variable2: place2)
3434
}
3535

36-
override func isSatisfied(_ assignment: Dictionary<String, String>) -> Bool {
36+
override func isSatisfied(assignment: Dictionary<String, String>) -> Bool {
3737
// if either variable is not in the assignment then it must be consistent
3838
// since they still have their domain
3939
if assignment[variable1] == nil || assignment[variable2] == nil {
@@ -58,16 +58,16 @@ class AustralianMapColoringTest: XCTestCase {
5858
}
5959

6060
csp = CSP<String, String>(variables: variables, domains: domains)
61-
csp?.addConstraint(MapColoringConstraint(place1: "Western Australia", place2: "Northern Territory"));
62-
csp?.addConstraint( MapColoringConstraint(place1: "Western Australia", place2: "South Australia"));
63-
csp?.addConstraint( MapColoringConstraint(place1: "South Australia", place2: "Northern Territory"));
64-
csp?.addConstraint( MapColoringConstraint(place1: "Queensland", place2: "Northern Territory"));
65-
csp?.addConstraint( MapColoringConstraint(place1: "Queensland",
61+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "Western Australia", place2: "Northern Territory"));
62+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "Western Australia", place2: "South Australia"));
63+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "South Australia", place2: "Northern Territory"));
64+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "Queensland", place2: "Northern Territory"));
65+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "Queensland",
6666
place2: "South Australia"));
67-
csp?.addConstraint(MapColoringConstraint(place1: "Queensland", place2: "New South Wales"));
68-
csp?.addConstraint( MapColoringConstraint(place1: "New South Wales", place2: "South Australia"));
69-
csp?.addConstraint( MapColoringConstraint(place1: "Victoria", place2: "South Australia"));
70-
csp?.addConstraint( MapColoringConstraint(place1: "Victoria",place2: "New South Wales"));
67+
csp?.addConstraint(constraint:MapColoringConstraint(place1: "Queensland", place2: "New South Wales"));
68+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "New South Wales", place2: "South Australia"));
69+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "Victoria", place2: "South Australia"));
70+
csp?.addConstraint(constraint: MapColoringConstraint(place1: "Victoria",place2: "New South Wales"));
7171
}
7272

7373
override func tearDown() {
@@ -82,7 +82,7 @@ class AustralianMapColoringTest: XCTestCase {
8282
return
8383
}
8484

85-
if let solution = backtrackingSearch(cs, mrv: false) {
85+
if let solution = backtrackingSearch(csp: cs, mrv: false) {
8686
print(solution, terminator: "")
8787
XCTAssertEqual(solution, ["South Australia": "b", "New South Wales": "g", "Western Australia": "r", "Northern Territory": "g", "Victoria": "r", "Tasmania": "r", "Queensland": "r"], "Pass")
8888
} else {

SwiftCSP/SwiftCSPTests/EightQueensTest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class EightQueensConstraint: ListConstraint <Int, Int> {
3333
super.init(variables: variables)
3434
}
3535

36-
override func isSatisfied(_ assignment: Dictionary<Int, Int>) -> Bool {
36+
override func isSatisfied(assignment: Dictionary<Int, Int>) -> Bool {
3737
// not the most efficient check for attacking each other...
3838
// better to subtract one from the other and go from there
3939
for q in assignment.values {
@@ -78,7 +78,7 @@ final class EightQueensConstraint: ListConstraint <Int, Int> {
7878
}
7979
}
8080

81-
func drawQueens(_ solution: Dictionary<Int, Int>) {
81+
func drawQueens(solution: Dictionary<Int, Int>) {
8282
var output = "\n"
8383
for i in 0..<64 {
8484
if (solution.values.index(of: i) != nil) {
@@ -110,7 +110,7 @@ class EightQueensTest: XCTestCase {
110110

111111
csp = CSP<Int, Int>(variables: variables, domains: domains)
112112
let smmc = EightQueensConstraint(variables: variables)
113-
csp?.addConstraint(smmc)
113+
csp?.addConstraint(constraint: smmc)
114114

115115
}
116116

@@ -122,9 +122,9 @@ class EightQueensTest: XCTestCase {
122122
func testSolution() {
123123
// This is an example of a functional test case.
124124
if let cs: CSP<Int, Int> = csp {
125-
if let solution = backtrackingSearch(cs, mrv: true) {
125+
if let solution = backtrackingSearch(csp: cs, mrv: true) {
126126
print(solution, terminator: "")
127-
drawQueens(solution)
127+
drawQueens(solution: solution)
128128
XCTAssertEqual(solution, [2: 58, 4: 20, 5: 53, 6: 14, 7: 31, 0: 0, 1: 33, 3: 43], "Pass")
129129
} else {
130130
XCTFail("Fail")

SwiftCSP/SwiftCSPTests/SendMoreMoneyTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class SendMoreMoneyConstraint: ListConstraint <String, Int> {
3333
super.init(variables: variables)
3434
}
3535

36-
override func isSatisfied(_ assignment: Dictionary<String, Int>) -> Bool {
36+
override func isSatisfied(assignment: Dictionary<String, Int>) -> Bool {
3737
// if there are duplicate values then it's not correct
3838
let d = Set<Int>(assignment.values)
3939
if d.count < assignment.count {
@@ -77,7 +77,7 @@ class SendMoreMoneyTest: XCTestCase {
7777

7878
csp = CSP<String, Int>(variables: variables, domains: domains)
7979
let smmc = SendMoreMoneyConstraint(variables: variables)
80-
csp?.addConstraint(smmc)
80+
csp?.addConstraint(constraint: smmc)
8181

8282
}
8383

@@ -89,7 +89,7 @@ class SendMoreMoneyTest: XCTestCase {
8989
func testSolution() {
9090
// This is an example of a functional test case.
9191
if let cs: CSP<String, Int> = csp {
92-
if let solution = backtrackingSearch(cs, mrv: true) {
92+
if let solution = backtrackingSearch(csp: cs, mrv: true) {
9393
print(solution, terminator: "")
9494

9595
if let s = solution["S"], e = solution["E"], n = solution["N"], d = solution["D"], m = solution["M"], o = solution["O"], r = solution["R"], y = solution["Y"] {

0 commit comments

Comments
 (0)