## Wednesday, January 21, 2015

### Wednesday Night Hack #9 - Generation of constrained random stimulus using python-constraint with bit-level approach

One of the issues faced my in last experiment with constraint programming for random stimulus generation was that the constraint solver library only accepted input variables of finite domain.  As it turns out, all available constraint solvers share this same constraint.  The state space explodes when specifying 16-bit variables and it became difficult to solve a simple constraint.  For the purpose of stimulus generation, it is absolutely necessary to randomize large payloads;128 or 256 bit wide buses is not uncommon.  One approach is model one 2-bit variable for each bit of the random variable.  As it turns out, it works well.  This is commonly called, if I am not mistaken, a bit level approach.

On some other related notes :

I ordered this book on constraint based verification.  I suspect it will heavy on the computer science, but for \$17.99 I couldn't pass it up.  I will give it a quick read.

Also, I stumbled on this web site: http://www.personalkanban.com/ - be forewarned, it's new "agey", similar concepts to "GTD".  Well worth a read through if you are interested in personal growth.  Quoting: "Personal Kanban gives us clarity in our work and our lives by visualizing those tasks, expectations, and commitments we have and helping us prioritize and complete.  With only two simple rules, visualize your work and limit your work in progress."

Without further ado, here is the source code to tonight's experiment.  Enjoy!

```#!/usr/bin/env python2.7

from constraint import *
from random import seed

class Solver:
def __init__(self):
self.solver = Problem()
self.solver.setSolver(MinConflictsSolver(100))

# Each bit of input variable is in binary domain, internal format is name[bit position]
variables = ['{}[{}]'.format(name, i) for i in range(length)]
for variable in variables:

def randomize(self):
self.solution = self.solver.getSolution()

def get_variable(self, name):
# generate decimal representation from internal bitwise representation
remap = 0
for variable in self.solution:
if name in variable:
index = variable.replace('[', '_').replace(']', '').split('_')
remap += 2**int(index) * int(self.solution[variable])
return remap

#seed(1)  # fix random seed, if ndeed

solver = Solver()  # init solver object

solver.add_variable('xx', 32)  # rand bit [31:0] xx
solver.add_variable('yy', 15)  # rand bit [15:0] yy

# x is EVEN
solver.add_constraint(lambda x: x == 0, ['xx'])  # x is EVEN
# y is ODD
solver.add_constraint(lambda x: x == 1, ['yy'])  # y is EVEN

# solve the problem
solver.randomize()

# return random solutions for variable xx and yy
print(solver.get_variable('xx'))
print(solver.get_variable('yy'))
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7376265,"math_prob":0.8452165,"size":2098,"snap":"2020-45-2020-50","text_gpt3_token_len":516,"char_repetition_ratio":0.14422159,"word_repetition_ratio":0.0,"special_character_ratio":0.27407053,"punctuation_ratio":0.1884058,"nsfw_num_words":2,"has_unicode_error":false,"math_prob_llama3":0.9709815,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-23T05:39:05Z\",\"WARC-Record-ID\":\"<urn:uuid:4e61d2f4-605f-418d-95f6-52f8c0d20fff>\",\"Content-Length\":\"51228\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a7c0ccf7-7248-4657-aa66-a382cb6b2ede>\",\"WARC-Concurrent-To\":\"<urn:uuid:3828672a-53c6-4118-b970-b2955253597d>\",\"WARC-IP-Address\":\"172.217.5.243\",\"WARC-Target-URI\":\"http://blog.edmondcote.com/2015/01/wednesday-night-hack-generation-of.html\",\"WARC-Payload-Digest\":\"sha1:26OG6PH4TPFSID6QNGVMIASISS6V222U\",\"WARC-Block-Digest\":\"sha1:7TKPCBCH4GYWIHYSPD7WLRHYEKNSTXT7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107880656.25_warc_CC-MAIN-20201023043931-20201023073931-00511.warc.gz\"}"}