Changeset 5086 for coopr.pyomo/trunk/coopr/pyomo/base/piecewise.py
 Timestamp:
 10/12/11 18:30:10 (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/base/piecewise.py
r4744 r5086 70 70 else: 71 71 return ( (x & (x  1)) == 0 ) 72 72 73 def isIncreasing(l): 73 74 """ … … 154 155 return None 155 156 156 class _GrayCode(object): 157 """ 158 Generates an nbit graycode which is requred for LOG branching scheme 159 """ 160 161 def __init__(self, nbits): 162 self._nbits = nbits 163 self._grayCode = [] 164 self.__generate() 165 166 def __getitem__(self, i): 167 return self._grayCode[i] 168 169 def __str__(self): 170 return str(self._grayCode) 171 172 __repr__ = __str__ 173 174 def __iter__(self): 175 return self._grayCode.__iter__() 176 177 def __isOdd(self,integer): 178 return integer % 2 == 1 179 180 def __isEven(self,integer): 181 return integer % 2 == 0 182 183 def __list_to_string(self,li): 184 return ''.join(map(str, li)) 185 186 def __generate(self): 187 li = [0 for i in xrange(self._nbits)] 188 self._grayCode.append(self.__list_to_string(li)) 189 190 for term in xrange(2, (1<<self._nbits)+1): 191 if self.__isOdd(term): 192 for i in xrange(1,(self._nbits),1): 193 if li[i]==1: 194 li[i1]=li[i1]^1 195 break 196 197 if self.__isEven(term): 198 li[1]=li[1]^1 199 200 self._grayCode.append(self.__list_to_string(li)) 157 def _GrayCode(nbits): 158 159 bitset = [0 for i in xrange(nbits)] 160 graycode = [''.join(map(str,bitset))] 161 162 for i in xrange(2,(1<<nbits)+1): 163 if i%2: 164 for j in xrange(1,nbits,1): 165 if bitset[j]: 166 bitset[j1]=bitset[j1]^1 167 break 168 else: 169 bitset[1]=bitset[1]^1 170 171 graycode.append(''.join(map(str,bitset))) 172 173 return graycode 174 201 175 202 176 def _DLog_Branching_Scheme(L): … … 207 181 MAX = 2**L 208 182 mylists1 = {} 209 for i in range(1,L+1):183 for i in xrange(1,L+1): 210 184 mylists1[i] = [] 211 185 start = 1 212 186 step = MAX/(2**i) 213 187 while(start < MAX): 214 mylists1[i].extend([j for j in range(start,start+step)])188 mylists1[i].extend([j for j in xrange(start,start+step)]) 215 189 start += 2*step 216 190 217 biglist = [i for i in range(1,MAX+1)]191 biglist = [i for i in xrange(1,MAX+1)] 218 192 mylists2 = {} 219 193 for i in sorted(mylists1.keys()): … … 232 206 233 207 BIGL = 2**n 234 S = [j for j in range(1,n+1)] 235 code = list(_GrayCode(n)) 236 G = {} 237 for i in range(1,len(code)+1): 238 G[i] = [] 239 for j in range(len(code[i1])): 240 G[i].extend([int(code[i1][j])]) 241 208 S = [j for j in xrange(1,n+1)] 209 code = _GrayCode(n) 210 G = dict((i, [int(j) for j in code[i1]]) for i in xrange(1,len(code)+1)) 211 242 212 L = {} 243 213 R = {} … … 257 227 # determines convexity or concavity 258 228 # return 1 (convex), 1 (concave), 0 (neither) 259 260 SLOPE = [] 261 for i in xrange(1,len(points)): 262 SLOPE.append( (f(model,points[i])f(model,points[i1]))/(points[i]points[i1]) ) 263 264 if isNonDecreasing(SLOPE): 229 230 SLOPES = [(f(model,points[i])f(model,points[i1]))/(points[i]points[i1]) for i in xrange(1,len(points))] 231 232 if isNonDecreasing(SLOPES): 265 233 # convex 266 234 return 1 267 elif isNonIncreasing(SLOPE ):235 elif isNonIncreasing(SLOPES): 268 236 # concave 269 237 return 1 … … 478 446 elif len(args) == 1: 479 447 return getattr(model,name)[args[0]] 480 else: 481 return getattr(model,name) 448 return getattr(model,name) 482 449 483 450 def _getset(self,model,name,*targs): … … 487 454 elif len(args) == 1: 488 455 return getattr(model,name)[args[0]] 489 else: 490 return getattr(model,name) 456 return getattr(model,name) 491 457 492 458 def pprint(self, *args, **kwds): … … 792 758 def linear_estimators_LINEAR_indices_init(model): 793 759 if self._element_mode: 794 return [i for i in xrange(len(self._grad_pts()))]795 else: 796 return [t+(i,) for t in INDEX_SET_TANGENT.tuplize() \797 for i in xrange(len(self._grad_pts(t))) ]760 return (i for i in xrange(len(self._grad_pts()))) 761 else: 762 return (t+(i,) for t in INDEX_SET_TANGENT.tuplize() \ 763 for i in xrange(len(self._grad_pts(t)))) 798 764 tangent_indices = self._update_dicts('tangent_indices',Set(dimen=self._index_dimen+1, ordered=True, rule=linear_estimators_LINEAR_indices_init)) 799 765 … … 826 792 def simple_bounds_indices_init(model): 827 793 if self._element_mode: 828 return [i for i in xrange(len(self._pw_pts())1)]829 else: 830 return [t+(i,) for t in INDEX_SET_SIMPLE_BOUNDS.tuplize() \831 for i in xrange(len(self._pw_pts(t))1) ]794 return (i for i in xrange(len(self._pw_pts())1)) 795 else: 796 return (t+(i,) for t in INDEX_SET_SIMPLE_BOUNDS.tuplize() \ 797 for i in xrange(len(self._pw_pts(t))1)) 832 798 simple_bounds_indices = self._update_dicts('simplified_bounds_indices',Set(dimen=self._index_dimen+1,ordered=True, rule=simple_bounds_indices_init)) 833 799 … … 847 813 def SOS_indices_init(*t): 848 814 if self._element_mode: 849 return [i for i in xrange(len(self._pw_pts(t)))]815 return (i for i in xrange(len(self._pw_pts(t)))) 850 816 else: 851 817 if type(t[0]) is tuple: 852 return [t[0]+(i,) for i in xrange(len(self._pw_pts(t)))]818 return (t[0]+(i,) for i in xrange(len(self._pw_pts(t)))) 853 819 else: 854 return [(t[0],i) for i in xrange(len(self._pw_pts(t)))]820 return ((t[0],i) for i in xrange(len(self._pw_pts(t)))) 855 821 def y_indices_init(model): 856 822 if self._element_mode: 857 return [i for i in xrange(len(self._pw_pts()))]858 else: 859 return [t+(i,) for t in INDEX_SET_SOS2.tuplize() for i in xrange(len(self._pw_pts(t)))]823 return (i for i in xrange(len(self._pw_pts()))) 824 else: 825 return (t+(i,) for t in INDEX_SET_SOS2.tuplize() for i in xrange(len(self._pw_pts(t)))) 860 826 861 827 SOS_indices = '' … … 1003 969 def DCC_POLYTOPES_init(model): 1004 970 if self._element_mode: 1005 return [i for i in xrange(1,len(self._pw_pts()))]1006 1007 else: 1008 return [t+(i,) for t in DCC_SET.tuplize() \1009 for i in xrange(1,len(self._pw_pts(t))) ]971 return (i for i in xrange(1,len(self._pw_pts()))) 972 973 else: 974 return (t+(i,) for t in DCC_SET.tuplize() \ 975 for i in xrange(1,len(self._pw_pts(t)))) 1010 976 def DCC_poly_init(*t): 1011 return [i for i in xrange(1,len(self._pw_pts(t)))]977 return (i for i in xrange(1,len(self._pw_pts(t)))) 1012 978 def DCC_VERTICES_init(model): 1013 979 if self._element_mode: 1014 return [i for i in xrange(1,len(self._pw_pts())+1)]1015 else: 1016 return [t+(i,) for t in DCC_SET.tuplize() \1017 for i in xrange(1,len(self._pw_pts(t))+1) ]980 return (i for i in xrange(1,len(self._pw_pts())+1)) 981 else: 982 return (t+(i,) for t in DCC_SET.tuplize() \ 983 for i in xrange(1,len(self._pw_pts(t))+1)) 1018 984 def DCC_vert_init(args): 1019 985 try: … … 1022 988 except: 1023 989 p = args 1024 return [i for i in xrange(p,p+2)]990 return (i for i in xrange(p,p+2)) 1025 991 def DCC_lamba_set_init(model): 1026 992 if self._element_mode is True: 1027 return [(p,v) for p in xrange(1,len(self._pw_pts())) \1028 for v in xrange(1,len(self._pw_pts())+1) ]993 return ((p,v) for p in xrange(1,len(self._pw_pts())) \ 994 for v in xrange(1,len(self._pw_pts())+1)) 1029 995 else: 1030 996 # BOTTLENECK: speed/memory, this set can be extremely large and time consuming to 1031 997 # build when number of indices and breakpoints is large. 1032 return [t+(p,v) for t in DCC_SET.tuplize() \998 return (t+(p,v) for t in DCC_SET.tuplize() \ 1033 999 for p in xrange(1,len(self._pw_pts(t))) \ 1034 for v in xrange(1,len(self._pw_pts(t))+1) ]1000 for v in xrange(1,len(self._pw_pts(t))+1)) 1035 1001 1036 1002 DCC_POLYTOPES = self._update_dicts('DCC_POLYTOPES',Set(ordered=True,dimen=self._index_dimen+1,initialize=DCC_POLYTOPES_init)) … … 1100 1066 def DLOG_POLYTOPES_init(model): 1101 1067 if self._element_mode: 1102 return [i for i in xrange(1,len(self._pw_pts()))]1103 1104 else: 1105 return [t+(i,) for t in DLOG_SET.tuplize() \1106 for i in xrange(1,len(self._pw_pts(t))) ]1068 return (i for i in xrange(1,len(self._pw_pts()))) 1069 1070 else: 1071 return (t+(i,) for t in DLOG_SET.tuplize() \ 1072 for i in xrange(1,len(self._pw_pts(t)))) 1107 1073 def DLOG_LENGTH_POLY_init(model): 1108 1074 if self._element_mode: 1109 return [i for i in xrange(1,L_i+1)]1110 else: 1111 return [t+(i,) for t in DLOG_SET.tuplize() \1112 for i in xrange(1,L[t]+1) ]1075 return (i for i in xrange(1,L_i+1)) 1076 else: 1077 return (t+(i,) for t in DLOG_SET.tuplize() \ 1078 for i in xrange(1,L[t]+1)) 1113 1079 def DLOG_poly_init(*t): 1114 return [i for i in xrange(1,len(self._pw_pts(t)))]1080 return (i for i in xrange(1,len(self._pw_pts(t)))) 1115 1081 def DLOG_poly_one_init(args): 1116 1082 if self._element_mode: … … 1131 1097 def DLOG_VERTICES_init(model): 1132 1098 if self._element_mode: 1133 return [i for i in xrange(1,len(self._pw_pts())+1)]1134 else: 1135 return [t+(i,) for t in DLOG_SET.tuplize() \1136 for i in xrange(1,len(self._pw_pts(t))+1) ]1099 return (i for i in xrange(1,len(self._pw_pts())+1)) 1100 else: 1101 return (t+(i,) for t in DLOG_SET.tuplize() \ 1102 for i in xrange(1,len(self._pw_pts(t))+1)) 1137 1103 def DLOG_vert_init(args): 1138 1104 try: … … 1141 1107 except: 1142 1108 p = args 1143 return [i for i in xrange(p,p+2)]1109 return (i for i in xrange(p,p+2)) 1144 1110 def DLOG_lamba_set_init(model): 1145 1111 if self._element_mode: 1146 return [(p,v) for p in xrange(1,len(self._pw_pts())) \1147 for v in xrange(1,len(self._pw_pts())+1) ]1112 return ((p,v) for p in xrange(1,len(self._pw_pts())) \ 1113 for v in xrange(1,len(self._pw_pts())+1)) 1148 1114 else: 1149 1115 # BOTTLENECK: speed/memory, this set can be extremely large and time consuming to 1150 1116 # build when number of indices and breakpoints is large. 1151 return [t+(p,v) for t in DLOG_SET.tuplize() \1117 return (t+(p,v) for t in DLOG_SET.tuplize() \ 1152 1118 for p in xrange(1,len(self._pw_pts(t))) \ 1153 for v in xrange(1,len(self._pw_pts(t))+1) ]1119 for v in xrange(1,len(self._pw_pts(t))+1)) 1154 1120 1155 1121 DLOG_POLYTOPES = self._update_dicts('DLOG_POLYTOPES',Set(ordered=True,dimen=self._index_dimen+1,initialize=DLOG_POLYTOPES_init)) … … 1210 1176 def CC_POLYTOPES_init(model): 1211 1177 if self._element_mode: 1212 return [i for i in xrange(1,len(self._pw_pts()))]1213 else: 1214 return [t+(i,) for t in CC_SET.tuplize() \1215 for i in xrange(1,len(self._pw_pts(t))) ]1178 return (i for i in xrange(1,len(self._pw_pts()))) 1179 else: 1180 return (t+(i,) for t in CC_SET.tuplize() \ 1181 for i in xrange(1,len(self._pw_pts(t)))) 1216 1182 def CC_poly_init(args): 1217 1183 if self._element_mode: … … 1234 1200 def CC_VERTICES_init(model): 1235 1201 if self._element_mode: 1236 return [i for i in xrange(1,len(self._pw_pts())+1)]1237 else: 1238 return [t+(i,) for t in CC_SET.tuplize() \1239 for i in xrange(1,len(self._pw_pts(t))+1) ]1202 return (i for i in xrange(1,len(self._pw_pts())+1)) 1203 else: 1204 return (t+(i,) for t in CC_SET.tuplize() \ 1205 for i in xrange(1,len(self._pw_pts(t))+1)) 1240 1206 def CC_vert_init(*t): 1241 return [i for i in xrange(1,len(self._pw_pts(t))+1)]1207 return (i for i in xrange(1,len(self._pw_pts(t))+1)) 1242 1208 def CC_lamba_set_init(model): 1243 1209 if self._element_mode: 1244 return [v for v in xrange(1,len(self._pw_pts())+1)]1245 else: 1246 return [t+(v,) for t in CC_SET.tuplize() \1247 for v in xrange(1,len(self._pw_pts(t))+1) ]1210 return (v for v in xrange(1,len(self._pw_pts())+1)) 1211 else: 1212 return (t+(v,) for t in CC_SET.tuplize() \ 1213 for v in xrange(1,len(self._pw_pts(t))+1)) 1248 1214 1249 1215 CC_VERTICES = self._update_dicts('CC_VERTICES',Set(ordered=True,dimen=self._index_dimen+1,initialize=CC_VERTICES_init)) … … 1317 1283 def LOG_POLYTOPES_init(model): 1318 1284 if self._element_mode: 1319 return [i for i in xrange(1,len(self._pw_pts()))]1320 else: 1321 return [t+(i,) for t in LOG_SET.tuplize() \1322 for i in xrange(1,len(self._pw_pts(t))) ]1285 return (i for i in xrange(1,len(self._pw_pts()))) 1286 else: 1287 return (t+(i,) for t in LOG_SET.tuplize() \ 1288 for i in xrange(1,len(self._pw_pts(t)))) 1323 1289 def LOG_poly_init(args): 1324 1290 if self._element_mode: … … 1342 1308 def LOG_VERTICES_init(model): 1343 1309 if self._element_mode: 1344 return [i for i in xrange(1,len(self._pw_pts())+1)]1345 else: 1346 return [t+(i,) for t in LOG_SET.tuplize() \1347 for i in xrange(1,len(self._pw_pts(t))+1) ]1310 return (i for i in xrange(1,len(self._pw_pts())+1)) 1311 else: 1312 return (t+(i,) for t in LOG_SET.tuplize() \ 1313 for i in xrange(1,len(self._pw_pts(t))+1)) 1348 1314 def LOG_vert_init(*t): 1349 return [i for i in xrange(1,len(self._pw_pts(t))+1)]1315 return (i for i in xrange(1,len(self._pw_pts(t))+1)) 1350 1316 def LOG_lamba_set_init(model): 1351 1317 if self._element_mode: 1352 return [v for v in xrange(1,len(self._pw_pts())+1)]1353 else: 1354 return [t+(v,) for t in LOG_SET.tuplize() \1355 for v in xrange(1,len(self._pw_pts(t))+1) ]1318 return (v for v in xrange(1,len(self._pw_pts())+1)) 1319 else: 1320 return (t+(v,) for t in LOG_SET.tuplize() \ 1321 for v in xrange(1,len(self._pw_pts(t))+1)) 1356 1322 def LOG_BRANCHING_SCHEME_init(model): 1357 1323 if self._element_mode: 1358 return [s for s in Si]1359 else: 1360 return [t+(s,) for t in LOG_SET.tuplize() \1361 for s in S[t] ]1324 return (s for s in Si) 1325 else: 1326 return (t+(s,) for t in LOG_SET.tuplize() \ 1327 for s in S[t]) 1362 1328 def LOG_BRANCHING_LEFT_init(args): 1363 1329 if self._element_mode: … … 1449 1415 def MC_POLYTOPES_init(model): 1450 1416 if self._element_mode: 1451 return [i for i in xrange(1,len(self._pw_pts()))]1452 else: 1453 return [t+(i,) for t in MC_SET.tuplize() \1454 for i in xrange(1,len(self._pw_pts(t))) ]1417 return (i for i in xrange(1,len(self._pw_pts()))) 1418 else: 1419 return (t+(i,) for t in MC_SET.tuplize() \ 1420 for i in xrange(1,len(self._pw_pts(t)))) 1455 1421 def MC_poly_init(*t): 1456 return [i for i in xrange(1,len(self._pw_pts(t)))]1422 return (i for i in xrange(1,len(self._pw_pts(t)))) 1457 1423 1458 1424 MC_POLYTOPES = self._update_dicts('MC_POLYTOPES',Set(ordered=True,dimen=self._index_dimen+1,initialize=MC_POLYTOPES_init)) … … 1512 1478 def INC_POLYTOPES_init(model): 1513 1479 if self._element_mode: 1514 return [i for i in xrange(1,len(self._pw_pts()))]1515 else: 1516 return [t+(i,) for t in INC_SET.tuplize() \1517 for i in xrange(1,len(self._pw_pts(t))) ]1480 return (i for i in xrange(1,len(self._pw_pts()))) 1481 else: 1482 return (t+(i,) for t in INC_SET.tuplize() \ 1483 for i in xrange(1,len(self._pw_pts(t)))) 1518 1484 def INC_poly_init(*t): 1519 return [i for i in xrange(1,len(self._pw_pts(t)))]1485 return (i for i in xrange(1,len(self._pw_pts(t)))) 1520 1486 def INC_VERTICES_init(model): 1521 1487 if self._element_mode: 1522 return [i for i in xrange(1,len(self._pw_pts())+1)]1523 else: 1524 return [t+(i,) for t in INC_SET.tuplize() \1525 for i in xrange(1,len(self._pw_pts(t))+1) ]1488 return (i for i in xrange(1,len(self._pw_pts())+1)) 1489 else: 1490 return (t+(i,) for t in INC_SET.tuplize() \ 1491 for i in xrange(1,len(self._pw_pts(t))+1)) 1526 1492 def INC_Y_init(model): 1527 1493 if self._element_mode: 1528 return [i for i in xrange(1,len(self._pw_pts())1)]1529 else: 1530 return [t+(i,) for t in INC_SET.tuplize() \1531 for i in xrange(1,len(self._pw_pts(t))1) ]1494 return (i for i in xrange(1,len(self._pw_pts())1)) 1495 else: 1496 return (t+(i,) for t in INC_SET.tuplize() \ 1497 for i in xrange(1,len(self._pw_pts(t))1)) 1532 1498 1533 1499 INC_POLYTOPES = self._update_dicts('INC_POLYTOPES',Set(ordered=True,dimen=self._index_dimen+1,initialize=INC_POLYTOPES_init))
Note: See TracChangeset
for help on using the changeset viewer.