# Commits

committed f950828

tools

• Participants
• Parent commits ac65026

# File ThreeWireMatrix/rl.py

`+import itertools`
`+from math import sqrt`
`+`
`+# This assumes a pin arrangement of:`
`+# GND->a->p1->b->p2->c->p3->d->p4->e->p5->f->p6->g->p7->h->VCC`
`+`
`+class AnalogMatrixKeypadSolver:`
`+  def __init__(self,rows,cols,tap):`
`+    self.tap = tap`
`+    self.rows = rows`
`+    self.cols = cols`
`+`
`+  def calcResistances(self,resistors):`
`+    ka = {}`
`+    assert len(resistors) == len(self.rows)+len(self.cols)+1`
`+    for r in range(0,len(self.rows)):`
`+      for c in range(0,len(self.cols)):`
`+        key = r*len(self.cols)+c+1`
`+        if self.rows[r] < self.cols[c]:`
`+          a = self.rows[r]`
`+          b = self.cols[c]`
`+        else:`
`+          b = self.rows[r]`
`+          a = self.cols[c]`
`+        totalResistanceList = list(resistors)`
`+        del totalResistanceList[a:b]`
`+        groundResistanceList = list(resistors[0:self.tap])`
`+        del groundResistanceList[a:b]`
`+        ka[key] = {'g':groundResistanceList,'t':totalResistanceList}`
`+    return ka`
`+        `
`+  def printKeyFunctions(self,resistors):`
`+    assert len(resistors) == len(self.rows)+len(self.cols)+1`
`+    rs = self.calcResistances(resistors)`
`+    for r in rs:`
`+      print "k%02i = (%s)/(%s)" % (r,"+".join([str(s) for s in rs[r]['g']]),"+".join([str(s) for s in rs[r]['t']]))`
`+`
`+  def printKeyFunctions(self,resistors):`
`+    assert len(resistors) == len(self.rows)+len(self.cols)+1`
`+    rs = self.calcResistances(resistors)`
`+    for r in rs:`
`+      print "k%02i = (%s)/(%s)" % (r,"+".join([str(s) for s in rs[r]['g']]),"+".join([str(s) for s in rs[r]['t']]))`
`+`
`+  def printKeyFunctionsSolved(self,resistors):`
`+    assert len(resistors) == len(self.rows)+len(self.cols)+1`
`+    rs = self.calcResistances(resistors)`
`+    for r in rs:`
`+      print "k%02i = (%s)/(%s) = %s" % (r,"+".join([str(s) for s in rs[r]['g']]),"+".join([str(s) for s in rs[r]['t']]),float(sum(rs[r]['g']))/float(sum(rs[r]['t']))*1024)`
`+`
`+  def minDiff(self,resistors):`
`+    assert len(resistors) == len(self.rows)+len(self.cols)+1,"Expecting %s resistor values, but got %s" % (len(self.rows)+len(self.cols)+1,len(resistors))`
`+    rs = self.calcResistances(resistors)`
`+    numberList = [(float(sum(rs[r]['g']))/float(sum(rs[r]['t'])))*1024 for r in rs]`
`+    a=sorted(numberList)`
`+    return int(min(y-x for x,y in zip(a,a[1:])))`
`+`
`+  def stdDev(self,resistors):`
`+    assert len(resistors) == len(self.rows)+len(self.cols)+1,"Expecting %s resistor values, but got %s" % (len(self.rows)+len(self.cols)+1,len(resistors))`
`+    rs = self.calcResistances(resistors)`
`+    numberList = [(float(sum(rs[r]['g']))/float(sum(rs[r]['t'])))*1024 for r in rs]`
`+    n = len(numberList)`
`+    mean = sum(numberList) / float(n)`
`+    std = 0`
`+    for a in numberList:`
`+      std = std + (a - mean)**2`
`+    std = sqrt(std / float(n-1))`
`+    return std`
`+`
`+###############################################################################`
`+`
`+TAP_PIN = 3`
`+MATRIX_ROWS = [2,7,6,4]`
`+MATRIX_COLS = [3,1,5]`
`+RESISTORS = [`
`+  560,   # a`
`+  8200,  # b`
`+  18000, # c`
`+  910,   # d`
`+  9100,  # e`
`+  15000, # f`
`+  5100,  # g`
`+  51     # h`
`+]`
`+RESISTORS_NAME = [`
`+  'a',`
`+  'b',`
`+  'c',`
`+  'd',`
`+  'e',`
`+  'f',`
`+  'g',`
`+  'h'`
`+]`
`+`
`+def main():`
`+  E12 = [1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2]`
`+  E12C = itertools.product(E12 + [x * 10 for x in E12] + [x * 100 for x in E12] + [x * 1000 for x in E12] + [x * 10000 for x in E12] + [x * 100000 for x in E12] + [x * 1000000 for x in E12] + [x * 10000000 for x in E12], repeat=len(MATRIX_COLS)+len(MATRIX_ROWS)+1)`
`+  E12C = itertools.product(E12 + [x * 10 for x in E12] + [x * 100 for x in E12] + [x * 1000 for x in E12], repeat=len(MATRIX_COLS)+len(MATRIX_ROWS)+1)`
`+  E12C = itertools.product([x * 100 for x in E12], repeat=len(MATRIX_COLS)+len(MATRIX_ROWS)+1)`
`+  amks = AnalogMatrixKeypadSolver(MATRIX_COLS,MATRIX_ROWS,TAP_PIN)`
`+  amks.printKeyFunctions(RESISTORS_NAME)`
`+  g = 0`
`+  skip = 200000000`
`+  for i,p in enumerate(itertools.islice(E12C,skip,None)):`
`+    g1 = amks.minDiff(list(p))`
`+    d = amks.stdDev(list(p))`
`+    if g1 >= g:`
`+      g = g1`
`+      print i+skip,d,g,":",list(p)`
`+  `
`+if __name__ == '__main__':`
`+  main()`

# File ThreeWireMatrix/rl.txt

`+The goal is to find the set of (a:h) values that results in the largest deviation between all values in the set (k01:k12).  This problem is a bit simplified, as the real set of possible values for (a:h) is closer to 1200 values.`
`+For this case, the (a:h) can be any of the following values (duplicates allowed):`
`+(1,1.1,1.2,1.3,1.5,1.6,1.8,2.0,2.2,2.4,2.7,3.0,3.3,3.6,3.9,4.3,4.7,5.1,5.6,6.2,6.8,7.5,8.2,9.1)`
`+`
`+k[] = (a+b)/(a+b+d+e+f+g+h)`
`+k[] = (a+b+c)/(a+b+c+h)`
`+k[] = (a+b+c)/(a+b+c+g+h)`
`+k[] = (a+b+c)/(a+b+c+e+f+g+h)`
`+k[] = (a+c)/(a+c+d+e+f+g+h)`
`+k[] = (a)/(a+h)`
`+k[] = (a)/(a+g+h)`
`+k[] = (a)/(a+e+f+g+h)`
`+k[] = (a+b)/(a+b+f+g+h)`
`+k[] = (a+b+c)/(a+b+c+d+e+h)`
`+k[] = (a+b+c)/(a+b+c+d+e+g+h)`
`+k[] = (a+b+c)/(a+b+c+d+f+g+h)`
`+`
`+`
`+mean = `
`+http://pastebin.com/pZE2gtqH`
`+`
`+`
`+`
`+The goal is to find the set of (a:h) values that results in the largest minimum difference between all values in the set (k01:k12).  This problem is a bit simplified, as the real set of possible values for (a:h) is closer to 1200 values.`
`+For this case, the (a:h) can be any of the following values (duplicates allowed):`
`+`
`+    (1,1.1,1.2,1.3,1.5,1.6,1.8,2.0,2.2,2.4,2.7,3.0,3.3,3.6,3.9,4.3,4.7,5.1,5.6,6.2,6.8,7.5,8.2,9.1)`
`+`
`+The calculations for (k01:k12) are:`
`+`
`+    k01 = (a+b)/(a+b+d+e+f+g+h)`
`+    k02 = (a+b+c)/(a+b+c+h)`
`+    k03 = (a+b+c)/(a+b+c+g+h)`
`+    k04 = (a+b+c)/(a+b+c+e+f+g+h)`
`+    k05 = (a+c)/(a+c+d+e+f+g+h)`
`+    k06 = (a)/(a+h)`
`+    k07 = (a)/(a+g+h)`
`+    k08 = (a)/(a+e+f+g+h)`
`+    k09 = (a+b)/(a+b+f+g+h)`
`+    k10 = (a+b+c)/(a+b+c+d+e+h)`
`+    k11 = (a+b+c)/(a+b+c+d+e+g+h)`
`+    k12 = (a+b+c)/(a+b+c+d+f+g+h)`
`+ `
`+Other than brute force, how can I optimize this problem?`
`+`
`+Right now, I'm solving for each kN, `