A set of benchmarks to compare a new prototype MiniZinc implementation
1import os, copy, io 2from collections import OrderedDict 3 4def try_float( sVal, dft=None ): 5 res = dft 6 try: 7 res = float( sVal ) 8 except: 9 pass 10 return res 11 12def mergeDict( dict1, dict2 ): 13 for key in dict2: 14 dict1[ key ] = dict2[ key ] 15 16def addMapValues( dict1, dict2 ): 17 for key in dict2: 18 if key in dict1: 19 dict1[key] += dict2[key] 20 else: 21 dict1[key] = dict2[key] 22 23class MatrixByStr: ### Matrix with string keys 24 def __init__( self, 25 rows, cols, ## each a list of tuples: (short, long) row/column names 26 defval = 0 ): 27 self.rows, self.cols = rows, cols 28 self.mbs = OrderedDict( (r[0], 29 OrderedDict( (c[0], copy.deepcopy(defval)) for c in cols )) for r in rows ) 30 31 def __getitem__( self, k ): 32 return self.mbs[ k[0] ][ k[1] ] 33 34 def __setitem__( self, k, v ): 35 self.mbs[ k[0] ][ k[1] ] = v 36 37 def stringify2D( self ): ## compact 2D print 38 return MyTab().tabulate( [ 39 [key] + list(row.values()) ## merge each row's key into the row 40 for key,row in self.mbs.items() ], 41 [""] + [ col[0] for col in self.cols ] ) 42 43 ## assumes each item is a list 44 def stringifyLists( self, title, rowIntro, colIntro ): 45 resIO = io.StringIO() 46 print( '#'*50, file=resIO ) 47 print( '#'*50, file=resIO ) 48 print( '#'*50, file=resIO ) 49 print( title, file=resIO ) 50 print( '#'*50, file=resIO ) 51 print( '#'*50, file=resIO ) 52 print( '#'*50, file=resIO ) 53 print( file=resIO ) 54 55 for kr, r in self.mbs.items(): 56 print( '#'*50, file=resIO ) 57 print( '#'*50, file=resIO ) 58 print( rowIntro, kr, file=resIO ) 59 print( '#'*50, file=resIO ) 60 print( '#'*50, file=resIO ) 61 print( file=resIO ) 62 63 for kc, c in r.items(): 64 print( '#'*50, file=resIO ) 65 print( rowIntro, kr, file=resIO ) 66 print( colIntro, " ", kc, " / ", len(c), file=resIO ) 67 print( '#'*50, file=resIO ) 68 69 for el in c: 70 print( el, file=resIO ) 71 print( file=resIO ) 72 73 return resIO.getvalue() 74 75class MyTab: 76 def __init__( self ): 77 self.sColSep=' ' 78 79 ## Receive table row-wise (matr) 80 ## hdr is a list of column headers 81 def tabulate( self, matr, hdr ): 82 res = "" 83 ## Compute max width 84 nWMax = [] 85 for iC in range( len( hdr ) ): 86 nWMax.append( len( str( hdr[ iC ] ) ) ) 87 for iR in range( len( matr ) ): 88 if iC<len( matr[ iR ] ): 89 if nWMax[ iC ]<len( str( matr[iR][iC] ) ): 90 nWMax[ iC ] = len( str( matr[iR][iC] ) ) 91 ## Printing 92 res =( ("{0:<"+str(nWMax[0])+'}').format(hdr[0]) + self.sColSep ) 93 for iC in range( 1, len( hdr ) ): 94 res += ( ("{0:"+str(nWMax[iC])+'}').format(hdr[iC]) 95 + ( self.sColSep if iC+1<len(hdr) else '\n' ) ) 96 for iR in range( len( matr ) ): 97 res += ( ("{0:<"+str(nWMax[0])+'}').format(matr[iR][0] if 0<len( matr[ iR ] ) else '-') + self.sColSep ) 98 for iC in range( 1, len( matr[iR] ) ): 99 res += ( ("{0:"+str(nWMax[iC])+'}').format(matr[iR][iC] if iC<len( matr[ iR ] ) else '-') 100 + ( self.sColSep if iC+1<len(hdr) else '\n' ) ) 101 return res 102 103## change string to be abke to become a filename 104def flnfy( sStr ): 105 keepcharacters = ('-','_') 106 return "".join(c if c.isalnum() or c in keepcharacters else 'I' for c in sStr).strip() 107 108def makeDirname( sFln ): 109 sDir = os.path.dirname( sFln ) 110 if 0<len(sDir): 111 try: 112 os.makedirs( sDir, exist_ok=True) 113 except: 114 print( "Failed to create dir '", sDir, "'.", sep='' ) 115 return False 116 return True 117 118def openFile_autoDir( sFln, sMode ): 119 makeDirname( sFln ) 120 return open( sFln, sMode ) 121 122## stringify a container, inserting a given prefix before each element 123def strNL( prf, cnt ): 124 return prf + prf.join( [ str( el ) for el in cnt ] ) 125