-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
183 additions
and
145 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...en-core/src/main/kotlin/com/holokenmod/creation/dlx/ConstraintsFromGridCagesCalculator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.holokenmod.creation.dlx | ||
|
||
class ConstraintsFromGridCagesCalculator( | ||
private val dlxGrid: DLXGrid, | ||
private val numberOfCages: Int | ||
) { | ||
fun calculateConstraints(): List<BooleanArray> { | ||
val contraints = mutableListOf<BooleanArray>() | ||
|
||
for (creator in dlxGrid.creators) { | ||
for (possibleCageCombination in creator.possibleNums) { | ||
val constraint = BooleanArray( | ||
dlxGrid.possibleDigits.size * (dlxGrid.gridSize.width + dlxGrid.gridSize.height) + | ||
numberOfCages | ||
) | ||
|
||
for (i in possibleCageCombination.indices) { | ||
val indexOfDigit = dlxGrid.digitSetting.indexOf(possibleCageCombination[i]) | ||
|
||
val (columnConstraint, rowConstraint) = dlxGrid.columnAndRowConstraints( | ||
indexOfDigit, | ||
creator, | ||
i | ||
) | ||
|
||
constraint[columnConstraint] = true | ||
constraint[rowConstraint] = true | ||
} | ||
|
||
val cageConstraint = dlxGrid.cageConstraint(creator.id) | ||
|
||
constraint[cageConstraint] = true | ||
|
||
contraints += constraint | ||
} | ||
} | ||
|
||
return contraints | ||
} | ||
|
||
fun numberOfNodes(): Int { | ||
var result = 0 | ||
|
||
for (creator in dlxGrid.creators) { | ||
result += creator.possibleNums.size * (2 * creator.numberOfCells + 1) | ||
} | ||
|
||
return result | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
.../src/main/kotlin/com/holokenmod/creation/dlx/ConstraintsFromRectangularGridsCalculator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.holokenmod.creation.dlx | ||
|
||
class ConstraintsFromRectangularGridsCalculator( | ||
private val dlxGrid: DLXGrid, | ||
private val numberOfCages: Int | ||
) { | ||
private val cartesianProductOfRectangularPossibles = if (dlxGrid.gridSize.isSquare) { | ||
emptyList() | ||
} else { | ||
uniqueIndexSetsOfGivenLength( | ||
dlxGrid.possibleDigits.indices.toList(), | ||
dlxGrid.gridSize.largestSide() - dlxGrid.gridSize.smallestSide() | ||
) | ||
} | ||
|
||
fun calculateConstraints(): List<BooleanArray> { | ||
if (dlxGrid.gridSize.isSquare) { | ||
return emptyList() | ||
} | ||
|
||
var cageId = dlxGrid.creators.size | ||
|
||
val contraints = mutableListOf<BooleanArray>() | ||
|
||
for (rowOrColumn in 0 until dlxGrid.gridSize.largestSide()) { | ||
for (indexesOfDigits in cartesianProductOfRectangularPossibles) { | ||
val constraint = BooleanArray( | ||
dlxGrid.possibleDigits.size * (dlxGrid.gridSize.width + dlxGrid.gridSize.height) + | ||
numberOfCages | ||
) | ||
|
||
for (indexOfDigit in indexesOfDigits) { | ||
val (columnConstraint, rowConstraint) = dlxGrid.columnAndRowConstraints( | ||
indexOfDigit, | ||
rowOrColumn, | ||
rowOrColumn | ||
) | ||
|
||
if (dlxGrid.gridSize.width < dlxGrid.gridSize.height) { | ||
constraint[rowConstraint] = true | ||
} else { | ||
constraint[columnConstraint] = true | ||
} | ||
|
||
val cageConstraint = dlxGrid.cageConstraint(cageId) | ||
constraint[cageConstraint] = true | ||
} | ||
|
||
contraints += constraint | ||
} | ||
cageId++ | ||
} | ||
|
||
return contraints | ||
} | ||
|
||
private fun uniqueIndexSetsOfGivenLength(values: List<Int>, numberOfCopies: Int): Set<Set<Int>> { | ||
return UniqueIndexSetsOfGivenLength(values, numberOfCopies).calculateProduct() | ||
} | ||
|
||
fun numberOfNodes(): Int { | ||
return if (dlxGrid.gridSize.isSquare) { | ||
0 | ||
} else { | ||
dlxGrid.gridSize.largestSide() * cartesianProductOfRectangularPossibles.size * 200 | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
holoken-core/src/main/kotlin/com/holokenmod/creation/dlx/DLXGrid.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.holokenmod.creation.dlx | ||
|
||
import com.holokenmod.creation.cage.GridSingleCageCreator | ||
import com.holokenmod.grid.Grid | ||
|
||
class DLXGrid( | ||
val grid: Grid | ||
) { | ||
val gridSize = grid.gridSize | ||
val digitSetting = grid.options.digitSetting | ||
val possibleDigits = digitSetting.getPossibleDigits(grid.gridSize) | ||
|
||
val creators = grid.cages.map { | ||
GridSingleCageCreator(grid.variant, it) | ||
} | ||
|
||
fun columnAndRowConstraints( | ||
indexOfDigit: Int, | ||
creator: GridSingleCageCreator, | ||
cellOfCage: Int | ||
): Pair<Int, Int> { | ||
return columnAndRowConstraints( | ||
indexOfDigit, | ||
creator.getCell(cellOfCage).column, | ||
creator.getCell(cellOfCage).row | ||
) | ||
} | ||
|
||
fun columnAndRowConstraints( | ||
indexOfDigit: Int, | ||
column: Int, | ||
row: Int | ||
): Pair<Int, Int> { | ||
val columnConstraint = grid.gridSize.width * indexOfDigit + column | ||
val rowConstraint = ( | ||
grid.gridSize.width * possibleDigits.size + | ||
grid.gridSize.height * indexOfDigit + row | ||
) | ||
|
||
return Pair(columnConstraint, rowConstraint) | ||
} | ||
|
||
fun cageConstraint(cageId: Int): Int { | ||
return possibleDigits.size * (grid.gridSize.width + grid.gridSize.height) + cageId | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters