forked from KevinCoble/AIToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstraintPropogation.swift
624 lines (501 loc) · 23 KB
/
ConstraintPropogation.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//
// ConstraintPropogation.swift
// AIToolbox
//
// Created by Kevin Coble on 3/6/15.
// Copyright (c) 2015 Kevin Coble. All rights reserved.
//
import Foundation
// MARK: ConstraintProblemVariable
/// Class for assignment variable
open class ConstraintProblemVariable {
open let domainSize: Int
var possibleSettings : [Bool]
var remainingPossibilityCount: Int
var assignedValueIndex: Int?
public init(sizeOfDomain: Int) {
// Store the number of settings
self.domainSize = sizeOfDomain
// Allocate an array of the possible settings
possibleSettings = Array(repeating: true, count: sizeOfDomain)
// Set the number of remaining possibilities
remainingPossibilityCount = sizeOfDomain
}
open var hasNoPossibleSettings : Bool {
get {
return (remainingPossibilityCount == 0)
}
}
open var isSingleton : Bool {
get {
return (assignedValueIndex == nil && remainingPossibilityCount == 1)
}
}
open var assignedValue : Int? {
get {
return assignedValueIndex
}
set {
assignedValueIndex = assignedValue
}
}
open var smallestAllowedValue : Int? {
get {
for index in 0..<domainSize {
if (possibleSettings[index]) {return index}
}
return nil
}
}
open var largestAllowedValue : Int? {
get {
for index in 1...domainSize {
if (possibleSettings[domainSize - index]) {return domainSize - index}
}
return nil
}
}
open func reset() {
for index in 0..<domainSize {
possibleSettings[index] = true
remainingPossibilityCount = domainSize
}
}
open func removeValuePossibility(_ varValueIndex: Int) -> Bool { // Return true if the possibility was actually removed
if (varValueIndex < 0 || varValueIndex >= domainSize) { return false}
var result = false
if (possibleSettings[varValueIndex]) {
remainingPossibilityCount -= 1
possibleSettings[varValueIndex] = false
result = true
}
return result
}
open func allowValuePossibility(_ varValueIndex: Int) -> Bool { // Return true if the possibility was actually returned
if (varValueIndex < 0 || varValueIndex >= domainSize) { return false }
var result = false
if (!possibleSettings[varValueIndex]) {
remainingPossibilityCount += 1
possibleSettings[varValueIndex] = true
result = true
}
return result
}
open func assignToNextPossibleValue() ->Bool { // Returns false if value cannot be assigned
if (remainingPossibilityCount == 0) {return false}
if let currentAssignment = assignedValueIndex {
if (currentAssignment == domainSize-1) { // We are on the last possible assignment
assignedValueIndex = nil
return false
}
if (remainingPossibilityCount == 1) { // We are on the only possible assignment
assignedValueIndex = nil
return false
}
for index in currentAssignment+1..<domainSize { // Find the next available assignment
if (possibleSettings[index]) {
assignedValueIndex = index
return true
}
}
// No valid possibility left
assignedValueIndex = nil
return false
}
else {
// Not assigned, find the first assignment available
for index in 0..<domainSize {
if (possibleSettings[index]) {
assignedValueIndex = index
break
}
}
}
return true
}
open func assignSingleton() -> Bool {
if (remainingPossibilityCount != 1) { return false }
for index in 0..<domainSize {
if (possibleSettings[index]) {
assignedValueIndex = index
return true
}
}
return false
}
}
// MARK:- ConstraintProblemConstraint
/// Protocol for a constraint between two nodes
public protocol ConstraintProblemConstraint {
var isSelfConstraint: Bool { get }
func enforceConstraint(_ graphNodeList: [ConstraintProblemNode], forNodeIndex: Int) -> [EnforcedConstraint]
}
public struct EnforcedConstraint {
var nodeAffected: ConstraintProblemNode
var domainIndexRemoved: Int
}
public enum StandardConstraintType {
case cantBeSameValueInOtherNode
case mustBeSameValueInOtherNode
case cantBeValue
case mustBeGreaterThanOtherNode
case mustBeLessThanOtherNode
}
open class InternalConstraint: ConstraintProblemConstraint {
let tType: StandardConstraintType
let nIndex: Int // Variable set index for 'can't be value' types, else node index
public init(type: StandardConstraintType, index: Int) {
tType = type
nIndex = index
}
open func reciprocalConstraint(_ firstNodeIndex: Int) ->InternalConstraint? {
switch tType {
case .cantBeSameValueInOtherNode:
let constraint = InternalConstraint(type: .cantBeSameValueInOtherNode, index: firstNodeIndex)
return constraint
case .mustBeSameValueInOtherNode:
let constraint = InternalConstraint(type: .mustBeSameValueInOtherNode, index: firstNodeIndex)
return constraint
case .cantBeValue:
return nil // No reciprocal for this one
case .mustBeGreaterThanOtherNode:
let constraint = InternalConstraint(type: .mustBeLessThanOtherNode, index: firstNodeIndex)
return constraint
case .mustBeLessThanOtherNode:
let constraint = InternalConstraint(type: .mustBeGreaterThanOtherNode, index: firstNodeIndex)
return constraint
}
}
open var isSelfConstraint: Bool {
return (tType == .cantBeValue)
}
open func enforceConstraint(_ graphNodeList: [ConstraintProblemNode], forNodeIndex: Int) -> [EnforcedConstraint] {
var changeList : [EnforcedConstraint] = []
let variable = graphNodeList[forNodeIndex].variable
switch tType {
case .cantBeSameValueInOtherNode:
if let index = variable.assignedValueIndex {
let otherNode = graphNodeList[nIndex]
if (otherNode.variable.removeValuePossibility(index)) {
changeList.append(EnforcedConstraint(nodeAffected: otherNode, domainIndexRemoved: index))
}
}
case .mustBeSameValueInOtherNode:
if let index = variable.assignedValueIndex {
let otherNode = graphNodeList[nIndex]
for otherIndex in 0..<otherNode.variable.domainSize {
if (otherIndex != index) {
if (otherNode.variable.removeValuePossibility(otherIndex)) {
changeList.append(EnforcedConstraint(nodeAffected: otherNode, domainIndexRemoved: otherIndex))
}
}
}
}
case .cantBeValue:
if (variable.removeValuePossibility(nIndex)) {
changeList.append(EnforcedConstraint(nodeAffected: graphNodeList[forNodeIndex], domainIndexRemoved: nIndex))
}
case .mustBeGreaterThanOtherNode:
// Find smallest value allowed for the node
if let smallestValue = variable.smallestAllowedValue {
if (smallestValue > 0) {
let otherNode = graphNodeList[nIndex]
for otherIndex in 0..<smallestValue {
if (otherNode.variable.removeValuePossibility(otherIndex)) {
changeList.append(EnforcedConstraint(nodeAffected: otherNode, domainIndexRemoved: otherIndex))
}
}
}
}
case .mustBeLessThanOtherNode:
// Find largest value allowed for the node
if let largest = variable.largestAllowedValue {
let otherNode = graphNodeList[nIndex]
if (largest < otherNode.variable.domainSize-1) {
for otherIndex in largest+1..<otherNode.variable.domainSize {
if (otherNode.variable.removeValuePossibility(otherIndex)) {
changeList.append(EnforcedConstraint(nodeAffected: otherNode, domainIndexRemoved: otherIndex))
}
}
}
}
}
return changeList
}
}
// MARK:- ConstraintProblemNode
/// Class for a node with a variable and a set of constraints
open class ConstraintProblemNode {
let variable : ConstraintProblemVariable
var constraints : [ConstraintProblemConstraint] = []
var constraintsLastEnforced: [EnforcedConstraint] = []
var inQueue = false
open var variableIndexValue : Int? {
get {return variable.assignedValueIndex}
}
var nodeIndex = -1
public init(variableDomainSize: Int) {
variable = ConstraintProblemVariable(sizeOfDomain: variableDomainSize)
}
func clearConstraints() {
constraints = []
}
func addConstraint(_ constraint: ConstraintProblemConstraint) {
constraints.append(constraint)
}
open func resetVariable() {
variable.reset()
}
open func processSelfConstraints(_ graphNodeList: [ConstraintProblemNode]) -> Bool
{
for constraint in constraints {
if (constraint.isSelfConstraint) {
_ = constraint.enforceConstraint(graphNodeList, forNodeIndex: nodeIndex)
}
}
// Verify we have a non-empty domain left
return (!variable.hasNoPossibleSettings)
}
open func clearConstraintsLastEnforced() {
constraintsLastEnforced = []
}
open func enforceConstraints(_ graphNodeList: [ConstraintProblemNode], nodeEnforcingConstraints: ConstraintProblemNode) -> Bool {
// Get our assigned domain index
if let _ = variable.assignedValueIndex {
// Go through each attached constraint
for constraint in constraints {
nodeEnforcingConstraints.constraintsLastEnforced += constraint.enforceConstraint(graphNodeList, forNodeIndex: nodeIndex)
}
}
return true
}
open func removeConstraintsLastEnforced() {
for constraintEnforced in constraintsLastEnforced {
constraintEnforced.nodeAffected.resetVariableDomainIndex(constraintEnforced.domainIndexRemoved)
}
}
open func resetVariableDomainIndex(_ resetIndex: Int) {
_ = variable.allowValuePossibility(resetIndex)
// If we were assigned, this un-assigns the node
variable.assignedValueIndex = nil
}
open func assignSingleton() -> Bool {
return variable.assignSingleton()
}
func addChangedNodesToQueue(_ queue: Queue<ConstraintProblemNode>) {
for constraintEnforced in constraintsLastEnforced {
if (!constraintEnforced.nodeAffected.inQueue) {
queue.enqueue(constraintEnforced.nodeAffected)
constraintEnforced.nodeAffected.inQueue = true
}
}
}
}
// MARK: -
// MARK: ConstraintProblem Class
/// Class for a constraint problem consisting of a collection of nodes. The nodes are (sub)classes of ConstraintProblemNode.
/// Constraints are added with the problem set, or creating ConstraintProblemConstraint conforming classes and adding those
open class ConstraintProblem {
// Array of nodes in the list
var graphNodeList : [ConstraintProblemNode] = []
// Empty initializer
public init() {
}
/// Method to set an array of ConstraintProblemNode (or subclass) to the problmeem graph
open func setNodeList(_ list: [ConstraintProblemNode]) {
graphNodeList = list
// Number each of the nodes
var nIndex = 0
for node in graphNodeList {
node.nodeIndex = nIndex
nIndex += 1
}
}
/// Method to clear all constraints for the problem
open func clearConstraints() {
for node in graphNodeList {
node.clearConstraints()
}
}
/// Method to add a value constraint to a node
open func addValueConstraintToNode(_ node: Int, invalidValue: Int) {
let constraint = InternalConstraint(type: .cantBeValue, index: invalidValue)
graphNodeList[node].addConstraint(constraint)
}
/// Method to add a constraint between two nodes
open func addConstraintOfType(_ type: StandardConstraintType, betweenNodeIndex firstnode: Int, andNodeIndex secondNode : Int) {
let constraint = InternalConstraint(type: type, index: secondNode)
graphNodeList[firstnode].addConstraint(constraint)
}
/// Method to add a set of reciprocal constraints between two nodes
open func addReciprocalConstraintsOfType(_ type: StandardConstraintType, betweenNodeIndex firstNode: Int, andNodeIndex secondNode : Int) {
let constraint = InternalConstraint(type: type, index: secondNode)
graphNodeList[firstNode].addConstraint(constraint)
if let reciprocalConstraint = constraint.reciprocalConstraint(firstNode) {
graphNodeList[secondNode].addConstraint(reciprocalConstraint)
}
}
/// Method to add a custom constraint
open func addCustomConstraint(_ constraint: ConstraintProblemConstraint, toNode: Int) {
graphNodeList[toNode].addConstraint(constraint)
}
/// Method to attempt to solve the problem using basic forward constraint propogation
/// Return true if a solution was found. The node's variables will be set with the result
open func solveWithForwardPropogation() -> Bool {
// Reset the variable possibilites for each node, then process self-inflicted constraints
for node in graphNodeList {
node.resetVariable()
if (!node.processSelfConstraints(graphNodeList)) { return false } // self-constraints left an empty domain
}
// Start with the first possible value for node 0
return forwardDFS(0)
}
fileprivate func forwardDFS(_ node: Int) -> Bool { // Return fail if backtracking
// Assign this node
while (true) {
// Reset any previous consraint enforcements from the last assignment
graphNodeList[node].removeConstraintsLastEnforced()
// Assign to the next value
if (!graphNodeList[node].variable.assignToNextPossibleValue()) {
// Reset any previous consraint enforcements from this assignment
graphNodeList[node].removeConstraintsLastEnforced()
return false
}
// Process constraints
graphNodeList[node].clearConstraintsLastEnforced()
if (!graphNodeList[node].enforceConstraints(graphNodeList, nodeEnforcingConstraints: graphNodeList[node])) { continue }
// If this was the last node, return true
if (node == graphNodeList.count-1) { return true}
// Otherwise, iterate down
if (forwardDFS(node+1)) { return true }
}
}
/// Method to attempt to solve the problem using singleton propogation
/// Return true if a solution was found. The node's variables will be set with the result
open func solveWithSingletonPropogation() -> Bool {
// Reset the variable possibilites for each node, then process self-inflicted constraints
for node in graphNodeList {
node.resetVariable()
if (!node.processSelfConstraints(graphNodeList)) { return false } // self-constraints left an empty domain
}
// Start with the first possible value for node 0
return singletonDFS(0)
}
fileprivate func singletonDFS(_ node: Int) -> Bool { // Return fail if backtracking
let nextNode = node + 1
// If the node is assigned already (from singleton propogation), just iterate down
if (graphNodeList[node].variable.assignedValueIndex != nil) {
// If this was the last node, return true
if (nextNode == graphNodeList.count) { return true}
return singletonDFS(nextNode)
}
// Assign this node
assignIteration : while (true) {
// Reset any previous consraint enforcements from the last assignment
graphNodeList[node].removeConstraintsLastEnforced()
// Assign to the next value
if (!graphNodeList[node].variable.assignToNextPossibleValue()) {
// Reset any previous consraint enforcements from this assignment
graphNodeList[node].removeConstraintsLastEnforced()
return false
}
if (nextNode < graphNodeList.count) { // If last node, skip propogation as we are done
// Process constraints
graphNodeList[node].clearConstraintsLastEnforced()
if (!graphNodeList[node].enforceConstraints(graphNodeList, nodeEnforcingConstraints: graphNodeList[node])) { continue }
// Find the singletons
let queue = Queue<ConstraintProblemNode>()
for index in nextNode..<graphNodeList.count {
graphNodeList[index].inQueue = false
if graphNodeList[index].variable.isSingleton {
queue.enqueue(graphNodeList[index])
graphNodeList[index].inQueue = true
}
}
// Propogate all singletons information to constraint nodes
var singleton = queue.dequeue()
while (singleton != nil) {
// Assign the singleton
_ = singleton!.assignSingleton()
// Process constraints, adding the 'backtrack' list to this node
if (!singleton!.enforceConstraints(graphNodeList, nodeEnforcingConstraints: graphNodeList[node])) {
// Backtrack
continue assignIteration
}
// See if there are more singletons to add to the queue
for index in nextNode..<graphNodeList.count {
if (!graphNodeList[index].inQueue && graphNodeList[index].variable.isSingleton) {
queue.enqueue(graphNodeList[index])
graphNodeList[index].inQueue = true
}
}
singleton = queue.dequeue()
}
}
// If this was the last node, return true
if (nextNode == graphNodeList.count) { return true}
// Otherwise, iterate down
if (singletonDFS(nextNode)) { return true }
}
}
/// Method to attempt to solve the problem using full constraint propogation
/// Probably only useful if less-than, greater-than, or appropriate custom constraints are in the problem
/// Return true if a solution was found. The node's variables will be set with the result
open func solveWithFullPropogation() -> Bool {
// Reset the variable possibilites for each node, then process self-inflicted constraints
for node in graphNodeList {
node.resetVariable()
if (!node.processSelfConstraints(graphNodeList)) { return false } // self-constraints left an empty domain
}
// Start with the first possible value for node 0
return fullDFS(0)
}
fileprivate func fullDFS(_ node: Int) -> Bool { // Return fail if backtracking
let nextNode = node + 1
// If the node is assigned already (from constraint propogation), just iterate down
if (graphNodeList[node].variable.assignedValueIndex != nil) {
// If this was the last node, return true
if (nextNode == graphNodeList.count) { return true}
return fullDFS(nextNode)
}
// Assign this node
assignIteration : while (true) {
// Reset any previous consraint enforcements from the last assignment
graphNodeList[node].removeConstraintsLastEnforced()
// Assign to the next value
if (!graphNodeList[node].variable.assignToNextPossibleValue()) {
// Reset any previous consraint enforcements from this assignment
graphNodeList[node].removeConstraintsLastEnforced()
return false
}
if (nextNode < graphNodeList.count) { // If last node, skip propogation as we are done
// Create the queue, reset the 'in queue' flag
let queue = Queue<ConstraintProblemNode>()
for index in nextNode..<graphNodeList.count {
graphNodeList[index].inQueue = false
}
// Process constraints
graphNodeList[node].clearConstraintsLastEnforced()
if (!graphNodeList[node].enforceConstraints(graphNodeList, nodeEnforcingConstraints: graphNodeList[node])) { continue }
// Add the modified nodes to the queue
graphNodeList[node].addChangedNodesToQueue(queue)
// Propogate changes
var changedNode = queue.dequeue()
while (changedNode != nil) {
// Process constraints, adding the 'backtrack' list to this node
if (!changedNode!.enforceConstraints(graphNodeList, nodeEnforcingConstraints: graphNodeList[node])) {
// Backtrack
continue assignIteration
}
// Add the modified nodes to the queue
graphNodeList[node].addChangedNodesToQueue(queue)
changedNode = queue.dequeue()
}
}
// If this was the last node, return true
if (nextNode == graphNodeList.count) { return true}
// Otherwise, iterate down
if (fullDFS(nextNode)) { return true }
}
}
}