Latest Python MinLex for BigSdk from RichardGoodrich (Big)

Programs which generate, solve, and analyze Sudoku puzzles

Latest Python MinLex for BigSdk from RichardGoodrich (Big)

Postby RichardGoodrich » Fri Jan 26, 2024 6:40 pm

BigSdk minlex canon.py as of Jan 25, 2024
(Got this from Moritz Lenz (moritz.lenz@gmail.com) 10 years ago?)

I refactored in attempt to make more "pythonic", "DRY" and to fit BigSdk
No Good without perm.py - 1298 line PERMS list - willing to post - if NOT too big for forum?

Code: Select all
"""
    canon.py see end of file for comments
"""

# <editor-fold desc="python imports"
import sys
# </editor-fold>

# <editor-fold desc="local imports"
import settings as g
import sigs
from perm import PERMS
# </editor-fold>

# <editor-fold desc="globals"
BP = g.BREAK_POINT
# </editor-fold>


next_map = 1
_map = [0, -1, -1, -1, -1, -1, -1, -1, -1, -1]


def create(sudoku_string):
    try:
        global next_map
        global _map

        puzzle_81 = sudoku_string.replace('.', '0')

        data = [[0 for __ in range(9)] for _ in range(9)]
        for i in range(81):
            data[i % 9][i // 9] = int(puzzle_81[i])

        minlex = [9 for _ in range(81)]
        transposed = [[0 for __ in range(9)] for _ in range(9)]
        for y in range(9):
            for x in range(9):
                transposed[y][x] = data[x][y]
        for i in range(1296):
            for j in range(1296):
                compare_and_update(minlex, data, i, j)
                compare_and_update(minlex, transposed, i, j)
        canon = ''.join(str(digit) for digit in minlex)
        return canon

    except Exception as e:
        sigs.logger_except.exception(e)
        sys.exit()


def compare_and_update(least, tmp, i, j):
    try:
        global next_map
        global _map

        count = 0
        for y in range(9):
            for x in range(9):
                a = tmp[PERMS[i][x]][PERMS[j][y]]
                if _map[a] == -1:
                    _map[a] = next_map
                    next_map += 1
                if _map[a] < least[count]:
                    perm_copy(tmp, least, i, j)
                    return
                elif _map[a] > least[count]:
                    return
                count += 1
        return

    except Exception as e:
            sigs.logger_except.exception(e)
            sys.exit()


def perm_copy(source, dest, i, j):
    try:
        global next_map
        global _map

        for k in range(81):
            a = source[PERMS[i][k % 9]][PERMS[j][k // 9]]
            if _map[a] == -1:
                _map[a] = next_map
                next_map += 1
            dest[k] = _map[a]
        return

    except Exception as e:
            sigs.logger_except.exception(e)
            sys.exit()


if __name__ == '__main__':
    arto_inkala_givens = '800000000003600000070090200050007000000045700000100030001000068008500010090000400'
    arto_inkala_solved = '812753649943682175675491283154237896369845721287169534521974368438526917796318452'
    print(f'min_lex_canon = {create(arto_inkala_givens)}')
else:
    file = __file__
    print(f'importing {file} ')

Big1952
RichardGoodrich
 
Posts: 70
Joined: 12 December 2012
Location: Josephine, TX

Re: Latest Python MinLex for BigSdk from RichardGoodrich (B

Postby blue » Fri Jan 26, 2024 9:27 pm

RichardGoodrich, in another thread, wrote:Would someone fix "canonical.py"

I don't know python, but if "canonical.py" looks like "canon.py" below, then it seems to missing lines at the top of compare_and_update(), that would reset '_map[]' and 'next_map' to and [0,-1,...,-1] and 1.
blue
 
Posts: 980
Joined: 11 March 2013

Postby 1to9only » Fri Jan 26, 2024 10:59 pm

I've posted my (final) versions of minlex.py and maxlex.py here. They are my modifications to canonical.py. Usage is in the program. As noted before, it is SLOW.

I have my own faster minlex/maxlex programs in the Releases section. AFAIK they've been downloaded a few times! I consider these programs to be slow now, as I have developed faster versions!! I'll update the programs at a later date.
User avatar
1to9only
 
Posts: 4176
Joined: 04 April 2018

Re:

Postby RichardGoodrich » Sat Jan 27, 2024 3:55 am

1to9only wrote:I've posted my (final) versions of minlex.py and maxlex.py here. They are my modifications to canonical.py. Usage is in the program. As noted before, it is SLOW.

I have my own faster minlex/maxlex programs in the Releases section. AFAIK they've been downloaded a few times! I consider these programs to be slow now, as I have developed faster versions!! I'll update the programs at a later date.


How Cool! I will check it out. In mean time, I just got David Clamage's SudokuClassicMinLex https://github.com/dclamage/SudokuClassicMinLex installed
and imported to my canon.py and it seemed to work. I just need to find my latest post where I was comparing the minlex strings.
Big1952
RichardGoodrich
 
Posts: 70
Joined: 12 December 2012
Location: Josephine, TX

Re: Latest Python MinLex for BigSdk from RichardGoodrich (B

Postby RichardGoodrich » Sat Jan 27, 2024 3:58 am

blue wrote:
RichardGoodrich, in another thread, wrote:Would someone fix "canonical.py"

I don't know python, but if "canonical.py" looks like "canon.py" below, then it seems to missing lines at the top of compare_and_update(), that would reset '_map[]' and 'next_map' to and [0,-1,...,-1] and 1.


I don't know but will check. It runs for me, but of course the answer is not right!
Big1952
RichardGoodrich
 
Posts: 70
Joined: 12 December 2012
Location: Josephine, TX

Re:

Postby RichardGoodrich » Sat Jan 27, 2024 4:09 am

1to9only wrote:I've posted my (final) versions of minlex.py and maxlex.py here. They are my modifications to canonical.py. Usage is in the program. As noted before, it is SLOW.

I have my own faster minlex/maxlex programs in the Releases section. AFAIK they've been downloaded a few times! I consider these programs to be slow now, as I have developed faster versions!! I'll update the programs at a later date.


Hey 1to9only the David Clamage SudokuClassicMinLex did get the same answer as you guys! Now, I will check out your stuff.
Big1952
RichardGoodrich
 
Posts: 70
Joined: 12 December 2012
Location: Josephine, TX

Re: Re:

Postby RichardGoodrich » Sat Jan 27, 2024 4:45 am

RichardGoodrich wrote:
1to9only wrote:I've posted my (final) versions of minlex.py and maxlex.py here. They are my modifications to canonical.py. Usage is in the program. As noted before, it is SLOW.

I have my own faster minlex/maxlex programs in the Releases section. AFAIK they've been downloaded a few times! I consider these programs to be slow now, as I have developed faster versions!! I'll update the programs at a later date.


I tried both minlex.py and maxlex.py and they run wondefully - Thanks So Much! I was not used to running stuff from command line. PyCharm has spoiled me.
Yes, I see that they are slower, but all-in-all I prefer that you built the tables. I guess I could always use Python's pprint and make the lists.

First I was Min-Lex-less now I am Maxed out with it! Appreciate you doing the Max one, too!
Big1952
RichardGoodrich
 
Posts: 70
Joined: 12 December 2012
Location: Josephine, TX

Postby 1to9only » Thu Feb 01, 2024 3:01 pm

1to9only wrote:I've posted my (final) versions of minlex.py and maxlex.py here. They are my modifications to canonical.py. Usage is in the program. As noted before, it is SLOW.

I've converted canonical.py (python program minlexes 1 sudoku grid in about 1.8s) to canonical.c (this minlexes 1 sudoku grid in about 10ms, minlexes the 49158 17-clues sudokus in 447s = 110 grids per second). It is still SLOW compared to other minlexers currently floating about!!
User avatar
1to9only
 
Posts: 4176
Joined: 04 April 2018

Re: Latest Python MinLex for BigSdk from RichardGoodrich (B

Postby denis_berthier » Sat Feb 03, 2024 4:18 am

.
After the introduction of expansions by Singles in the search for the hardest puzzles, it has become interesting to use solution-minlex form.
Would it be difficult to write a variant producing the solution-minlex form?
.
denis_berthier
2010 Supporter
 
Posts: 3980
Joined: 19 June 2007
Location: Paris

Postby 1to9only » Sat Feb 03, 2024 8:34 am

denis_berthier wrote:After the introduction of expansions by Singles in the search for the hardest puzzles, it has become interesting to use solution-minlex form.
Would it be difficult to write a variant producing the solution-minlex form?

gsf's program(s) will do solution-minlex, E.g. sudoku.exe -qFN -f%#.c puzzles.txt > output.txt
If you can/want build a Mac version, then take a look at the source code here: https://github.com/1to9only/gsf-s-sudoku.exe.
I have made some modifications, but you should be able to build gsf's version.

Edit: The -qFN part is not really needed - this works just as well: sudoku.exe -f%#.c puzzles.txt > output.txt
User avatar
1to9only
 
Posts: 4176
Joined: 04 April 2018


Return to Software