1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
# -*- coding: utf-8 -*-
from math import *
'''
Various methods for evaluation such as gap calculation, geometric means etc. and some printing methods
'''
FLOAT_INFINITY = 1e20
FLOAT_LARGE = 1e15
INT_INFINITY = 1e09
DEFAULT_MIN_GEOM_MEAN = 1.0
def getSoluFileProbName(probname):
return probname.split('.')[0]
def floatPrint(number, digitsbef=16, digitsafter=9, dispchar='g' ):
formatstring='%'+repr(digitsbef) + '.' + repr(digitsafter) + dispchar
if number >= FLOAT_INFINITY:
return 'Inf'
else:
return formatstring % (number)
def getTexName(name):
return name.replace('_', '\_')
def getGap(value, referencevalue, useCplexGap=False):
'''
calculate the gap between value and reference value in percent. Gap is either calculated
w.r.t the referencevalue, i.e., abs(value-referencevalue)/abs(referencevalue) * 100,
or in 'Cplex'-fashion, that is, abs(value-referencevalue)/max(abs(referencevalue), abs(value)) * 100
'''
if not useCplexGap:
if referencevalue == 0.0:
if value == 0.0:
return 0.0
else:
return FLOAT_INFINITY
elif referencevalue == FLOAT_INFINITY or value == FLOAT_INFINITY:
return FLOAT_INFINITY
else:
return abs( value - referencevalue )/float(abs(referencevalue)) * 100
else: # use the CPLEX gap
if value == FLOAT_INFINITY:
return FLOAT_INFINITY
maximum = max(abs(value), abs(referencevalue))
if maximum <= 10e-9:
return 0.0
else:
return abs(value - referencevalue) / maximum * 100
def listGetArithmeticMean(listofnumbers):
'''
returns the arithmetic mean of a list of numbers
'''
arithmeticmean = sum(listofnumbers)
arithmeticmean /= max(len(listofnumbers),1)*1.0
return arithmeticmean
def listGetGeomMean(listofnumbers, mingeommean=DEFAULT_MIN_GEOM_MEAN):
'''
returns the geometric mean of a list of numbers, where each element under consideration
has value min(element, mingeommean)
'''
geommean = 1.0
nitems = 0
for number in listofnumbers:
nitems = nitems + 1
nextnumber = max(number, mingeommean)
geommean = pow(geommean, (nitems - 1)/float(nitems)) * pow(nextnumber, 1 / float(nitems))
return geommean
def listGetShiftedGeometricMean(listofnumbers, shiftby=10.0):
'''
returns the shifted geometric mean of a list of numbers, where the additional shift defaults to
10.0 and can be set via shiftby
'''
geommean = 1.0
nitems = 0
for number in listofnumbers:
nitems = nitems + 1
nextnumber = number + shiftby
geommean = pow(geommean, (nitems - 1)/float(nitems)) * pow(nextnumber, 1 / float(nitems))
return geommean - shiftby
def cutString(string, char = '_', maxlength = -1):
iscuttable = True
stringcopy = string[0:len(string)]
while len(stringcopy) > maxlength and iscuttable:
iscuttable = False
listofstrings = stringcopy.split(char)
maxlen = 2
index = -1
for stringpart in listofstrings:
if len(stringpart) >= 3 and len(stringpart) > maxlen:
index = listofstrings.index(stringpart)
maxlen = len(stringpart)
iscuttable = True
else:
if iscuttable:
stringtocut = listofstrings[index]
assert len(stringtocut) >= 3
listofstrings[index]= stringtocut[0] + stringtocut[len(stringtocut)-1]
stringcopy = ''
for stringitem in listofstrings:
stringcopy = stringcopy + stringitem + char
else:
stringcopy.rstrip(char)
return stringcopy.rstrip(char)
def listGetMinColWidth(listofitems):
maxlen = -1
for item in listofitems:
if len(item) > maxlen:
maxlen = len(item)
return maxlen
|