Metadata-Version: 2.5
Name: rlsmoc
Version: 1.0
Summary: Fast, highly-random latin squares generator 
Author-email: Larry Trammell <ridgerat@ridgerat-tech.us>
Description-Content-Type: text/x-rst
License-Expression: CC-BY-4.0
License-File: LICENSE.txt
Import-Name: rlsmoc

================================================
 ABOUT THE RANDOM LATIN SQUARE GENERATOR MODULE
================================================


Purpose
--------

The "rlsmoc.py" module implements the fastest known direct
generator method for constructing highly random Latin Squares.


What is a "Latin Square"?
-------------------------

For a given size parameter N, a Latin Square is a grid of
NxN cells, arranged in N rows and N columns, in which N
symbols are assigned to the cells in such a manner that each 
symbol appears exactly once in every row and exactly once in 
every column.  The Random Latin Square generator uses the
numerical values 0 through N-1 as its symbol set, consistent 
with indexing conventions of the Python language (and many 
others).


What is a "Random Latin Square"?
--------------------------------

It should be clear that the rigid "Latin Constraint" rules 
about "no symbol can appear twice in any row or in any 
column" result in a layout that is very non-random.  In the 
context of Latin Squares, however, "randomness" refers to the 
manner in which Latin Squares are selected ("generated").
If a generator algorithm produces Latin Square layouts such
that every possible Latin Square of the given size is equally
likely to be delivered, then the generator algorithm is
said to be "unbiased" and the set of Latin Squares produced
is said to be "random."  Sets of Latin Squares that are not 
truly random, but very difficult to distinguish from truly 
random ones, are typically good enough for most practical 
purposes.


How many Latin Squares are there?
---------------------------------

Lots, a vast number, even in the cases when N is relatively
small.  However, if you compare that number to the number of
possible ways that the same set of symbols could be randomly 
placed into the square grid, the Latin Squares are a
miniscule subset.


How are random Latin Squares typically generated?
-------------------------------------------------

The traditional method-of-choice has been the Jacobson-Matthews
scrambler.  To use this idea, you start with a Latin Square, 
and apply a very large (but unspecified) number of randomized 
scrambling "moves" to shuffle the symbols around the layout. 
The starting Latin Square is typically a special nonrandom 
case that is simple to compute.  The scrambling approach 
requires a relatively large amount of effort to sufficiently 
break up patterns present in the starting layout.  With enough
scrambling, the final results are biased only to a small 
degree.  The residual bias is almost undetectable for Latin 
Squares of size 10x10 and larger.


What does this Random Latin Square generator do differently?
------------------------------------------------------------

It is a direct method.  It starts from an empty layout, and 
attempts to place copies of every symbol as randomly as possible 
without violating the "Latin Constraint" rules.  After each
step, the number of empty cells is reduced by one.  If it turns
out that some bad randomized assignment choices are made somewhere
in the process, a heuristic "conflict resolution" operation is
applied to remove the temporary impasse while introducing as 
little bias as possible.  A detectable bias remains for small 
Latin Squares, but less than the residual bias produced by the 
scrambling methods.


Does the Random Latin Square Generator have other strange properties?
---------------------------------------------------------------------

Yes, but not necessarily bad.  It tends to run about 3 times
faster than a good implementation of a scrambling algorithm.
It can generate Latin Squares for any size N=1 or greater
(even when it is not a smart idea).  It can be implemented
in a manner that has guaranteed finite termination, unlike
the scramblers where you must make a judgment call about how
much mixing is "good enough."  This implementation, however,
sacrifices some theoretical guarantees for the sake of faster
execution.


Does the implementation have unusual code dependencies?
-------------------------------------------------------

The random Latin Square generator uses the numpy module for its 
highly-structured array types with efficient indexing.  Approximately 
5 x N-squared storage locations are used for the Latin Square 
storage and internal indexing work areas. The "short" integer data
type is used to keep data storage compact.  This constrains the 
size of the Latin Squares to N < 32768, but this is very unlikely 
to be a practical limitation.  Features of the numpy random number 
generator are used for making randomized choices.


Where can I find more information about the algorithm?
------------------------------------------------------

Glad you are interested.  A detailed explanation is provided in 
the rlsmoc.epub ("Random Least Squares by the Method of Covers")
document provided with this module.  It is tedious, but intended
to be understandable.  Don't be intimidated.  


Is the method published in the refereed journals?
-------------------------------------------------

No.  It has generated a remarkable lack of interest from the 
academic community.  Strange, because as the great Nikolai Lobachevsky 
once observed, the only thing that really matters is who publishes 
first.  And this "unpublished" method is low-hanging fruit that 
anyone hungry to publish could easily exploit.


Why is this implementation considered "experimental?"
-----------------------------------------------------

Not because it doesn't work, or isn't reliable...  There
are two main reasons.  One, it is coded for clarity of 
presentation rather than optimality for any particular purpose:
security, robustness to user error, ease of application,
etc..  Two, it does not have the years of field application 
experience that you would want for a thoroughly trusted method.  
It is expected that you will adapt/constrain/improve/test 
the coding thoroughly before applying it.  Various "optimizations" 
for specific purposes are left to you.


How do I use the Random Latin Square generator?
-----------------------------------------------

Easy.  Import the module.  Instantiate a Latin Square object
of the desired size, which reserves storage.  Apply the "fill" 
method to populate the Latin Square with the symbol assignments.  
Apply the "fill" method again to repopulate the Latin Square 
storage with new and different symbol assignments.  See the 
"example.py" code file.  


How do I access the results?
----------------------------

One of the arrays within the Random Latin Square object is a 
two dimensional array called "store" that you can access.  The 
standard Python/numpy modules provide a simple "print" command
that works fine for displaying small Latin Square results 
from that storage.  For larger Latin Squares, you will probably
need to develop some alternate storage and presentation methods, 
for example, copy the results into a file or database.  Keep in 
mind that you can use the row indices, column indices, and grid 
contents as index numbers.  For example, you could use the row 
index to access a list of "laboratory names", the column index 
to access a list of "test dates", and the cell contents to index 
a list of "trial codes," to organize and display a Latin Square 
design for a pharmaceutical safety test based on a Latin Square 
experiment design.


Are there any other obscure details?
------------------------------------

Just one.  Some remedial operations (somewhat reminiscent of 
scrambling operations used by other methods but on a greatly
reduced scale) are applied by default to reduce bias.  This 
has a measurable and perhaps relevant effect on small Latin 
Squares, but it is pointless for moderate to large Latin
Squares.  You can configure the generator to omit the 
bias-adjustment operations, and gain a little efficiency, by 
specifying the optional named parameter "comp=0" when the Latin 
Square generator object is instantiated.


