imaginationtech / constrainedrandom Goto Github PK
View Code? Open in Web Editor NEWA Python package for creating and solving constrained randomization problems.
Home Page: https://pypi.org/project/constrainedrandom/
License: MIT License
A Python package for creating and solving constrained randomization problems.
Home Page: https://pypi.org/project/constrainedrandom/
License: MIT License
I've been trying to compare PyVSC and constrainedrandom's performance when it comes to randomization of 2D-arrays. The test case is a "checkerboard randomization", where each field can be 0 or 1, but cannot have the same value as its neighbours.
Consider the following implementations
import vsc
import timeit
@vsc.randobj
class Checkers():
def __init__(self, N):
self.N = N
self.board = [vsc.rand_list_t(vsc.rand_uint8_t(), self.N) for _ in range(self.N)]
@vsc.constraint
def con_values(self):
for row in self.board:
with vsc.foreach(row) as c:
0 <= c and c <= 1
@vsc.constraint
def constraints(self):
for i in range(1, self.N):
with vsc.foreach(self.board[i], idx=True) as j:
if j == 0:
pass
(self.board[i][j] != self.board[i-1][j]) and (self.board[i][j] != self.board[i][j-1])
def benchmark():
numRounds = 10
for N in (4, 8, 16, 32, 64, 128, 256):
t = timeit.timeit(stmt='c.randomize()', setup=f'c = Checkers({N})', number=numRounds, globals=globals())
print(f"N={N:4d}: Runtime: {t/numRounds}")
benchmark()
from constrainedrandom import RandObj
import timeit
class Checkers(RandObj):
def __init__(self, N):
super().__init__(max_iterations=1000)
self.N = N
for i in range(self.N):
self.add_rand_var(f"list{i}", domain=range(2), length=self.N, disable_naive_list_solver=True)
for i in range(1, self.N):
for j in range(1, self.N):
self.add_constraint((lambda row1, row2: row1[j] != row2[j]), (f"list{i-1}", f"list{i}"))
self.add_constraint((lambda row: row[j-1] != row[j]), (f"list{i}", ))
def benchmark():
numRounds = 10
for N in (4, 8, 16, 32, 64, 128, 256):
t = timeit.timeit(stmt='c.randomize()', setup=f'c = Checkers({N})', number=numRounds, globals=globals())
print(f"N={N:4d}: Runtime: {t/numRounds}")
benchmark()
The average runtimes reported for the two on my machine are as follows. The fields labeled "DNF" took so long that I didn't bother waiting for them to complete
N | PyVSC | constrainedrandom |
---|---|---|
4 | 0.008 | 0.001 |
8 | 0.029 | 1.265 |
16 | 0.112 | 45.786 |
32 | 0.305 | DNF |
64 | 1.208 | DNF |
128 | 5.371 | DNF |
256 | 22.421 | DNF |
As you can see, the performance of PyVSC seems vastly superior to the performance of constrainedrandom. However, I am unsure whether my implementation with constrainedrandom is optimal, but I haven't found a better way of implementing 2D-arrays and randomization thereof.
Is there a better way of constraining this problem that would lead to better performance with constrainedrandom?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.