Module fitnessutil
[hide private]
[frames] | no frames]

Source Code for Module fitnessutil

  1  """ 
  2  fitnessutil 
  3  =========== 
  4  Contains different methods used for problem specific fitness functions. 
  5  These are domain dependent utilities that are used for computing the fitness  
  6  function. e.g. the problem is about reading a list of motion capture frames and 
  7  we need a function that gives indexes of different groups of frames.    
  8   
  9  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 10  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 11  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 12  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 13  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 14  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 15  THE SOFTWARE. 
 16   
 17  @author: by Mehdi Khoury 
 18  @version: 1.20 
 19  @copyright: (c) 2009 Mehdi Khoury under the mit license 
 20  http://www.opensource.org/licenses/mit-license.html 
 21  @contact: mehdi.khoury at gmail.com 
 22  """ 
 23   
 24  import timeit 
 25  import psyco 
 26  import random 
 27  import copy 
 28  #import settings 
 29  #import tutorial8 
 30  import csv 
 31   
 32  psyco.profile() 
 33   
34 -def GetIndexesOfGroupsInList(ml):
35 """ 36 Function: GetIndexesOfGroupsInList 37 =================================== 38 finds groups of identical elements in a list and gives their starting and last indexes. 39 40 41 @param ml: a list of elements. e.g. ml=[1,1,1,1,1,2,2,3,4,5,5,5,6] 42 43 returns: A list of tuples, where each tuple is of the form 44 (group_discrete_val,group_first_ind,group_last_ind). 45 e.g. [(1, 0, 4), (2, 5, 6), (3, 7, 7), (4, 8, 8), (5, 9, 11), (6, 12, 12)] 46 47 """ 48 temp=ml[0] 49 output=[] 50 group_discrete_val=temp 51 group_first_ind=0 52 group_last_ind=0 53 for i in xrange(1,len(ml)): 54 if ml[i]==temp: 55 group_last_ind=group_last_ind+1 56 if ml[i]!=temp: 57 output.append((group_discrete_val,group_first_ind,group_last_ind)) 58 temp=ml[i] 59 group_discrete_val=temp 60 group_first_ind=i 61 group_last_ind=i 62 if i==len(ml)-1: 63 output.append((group_discrete_val,group_first_ind,group_last_ind)) 64 return output
65
66 -def GetInputDataFromFile(myfile):
67 ifile = open(myfile, "rb") 68 reader = csv.reader(ifile, delimiter="\t") 69 data=[] 70 rownum = 0 71 for row in reader: 72 mystrline= row[0] 73 y = eval(mystrline.strip()) 74 data.append(GetIndexesOfGroupsInList(list(y))) 75 ifile.close() 76 return data
77
78 -def ReplaceUsingBinaryMask(listElem,binarymask,initial,replacement ):
79 try: 80 81 result=copy.deepcopy(listElem) 82 for i in xrange(len(listElem)): 83 for j in xrange(len(listElem[i][0])): 84 #print listElem[i][0][j][0] 85 if listElem[i][0][j][0]==initial and binarymask[i][j]: 86 result[i][0][j]=(replacement,listElem[i][0][j][1],listElem[i][0][j][2]) 87 88 return result 89 except: 90 raise WrongValues, "Wrong values sent to function node.\nCan't get result" 91 exit
92
93 -def UncompressList(listTuples):
94 result=[] 95 for elem in listTuples: 96 #temp=[] 97 for i in xrange(elem[1],elem[2]+1): 98 result.append(elem[0]) 99 return result
100 101 if __name__ == '__main__': 102 #ifile = open('python_testdata/Boxer3_11_2TestData.csv', "rb") 103 #reader = csv.reader(ifile, delimiter="\t") 104 105 #rownum = 0 106 #for row in reader: 107 # mystrline= row[0] 108 # y = eval(mystrline.strip()) 109 # print GetIndexesOfGroupsInList(list(y)) 110 111 #ifile.close() 112 print 'ok' 113 Boxer1_1_1TestData=GetInputDataFromFile('python_testdata/Boxer1_1_1TestData.csv') 114 #print ReplaceWithBinaryMask(settings.inputdata,tutorial8.IsLong(settings.inputdata),7,1) 115 116 # Boxer1_1_1IdealData=[(7, 0, 26), (1, 27, 57), (7, 58, 155), (1, 156, 192), (7, 193, 287), (1, 288, 327), (7, 328, 414), (1, 415, 452), (7, 453, 665)] 117 # Boxer1_1_2IdealData=[(7, 0, 16), (1, 17, 70), (7, 71, 155), (1, 156, 211), (7, 212, 294), (1, 295, 349), (7, 350, 426), (1, 427, 487), (7, 488, 960)] 118 # 119 # Boxer1_3_1IdealData=[(7, 0, 37), (6, 38, 80), (7, 81, 169), (6, 170, 214), (7, 215, 316), (6, 317, 364), (7, 365, 453), (6, 454, 500), (7, 501, 637)] 120 # Boxer1_3_2IdealData=[(7, 0, 50), (6, 51, 101), (7, 102, 208), (6, 209, 264), (7, 265, 371), (6, 372, 430), (7, 431, 536), (6, 537, 591), (7, 592, 770)] 121 # 122 # Boxer1_5_1IdealData=[(7, 0, 65), (3, 66, 122), (7, 123, 233), (3, 234, 284), (7, 285, 399), (3, 400, 453), (7, 454, 571), (3, 572, 624), (7, 625, 751)] 123 # Boxer1_5_2IdealData=[(7, 0, 52), (3, 53, 120), (7, 121, 222), (3, 223, 291), (7, 292, 403), (3, 404, 473), (7, 474, 579), (3, 580, 643), (7, 644, 769)] 124 # 125 # Boxer1_7_1IdealData=[(7, 0, 2), (2, 3, 67), (7, 68, 185), (2, 186, 254), (7, 255, 375), (2, 376, 447), (7, 448, 561), (2, 562, 631), (7, 632, 796)] 126 # Boxer1_7_2IdealData=[(7, 0, 61), (2, 62, 129), (7, 130, 238), (2, 239, 304), (7, 305, 419), (2, 420, 488), (7, 489, 616), (2, 617, 680), (7, 681, 801)] 127 # 128 # Boxer1_8_1IdealData=[(7, 0, 34), (5, 35, 142), (7, 143, 214), (5, 215, 327), (7, 328, 407), (5, 408, 517), (7, 518, 604), (5, 605, 705), (7, 706, 801)] 129 # Boxer1_8_2IdealData=[(7, 0, 76), (5, 77, 171), (7, 172, 270), (5, 271, 354), (7, 355, 454), (5, 455, 550), (7, 551, 644), (5, 645, 747), (7, 748, 876)] 130 # 131 # Boxer1_11_1IdealData=[(7, 0, 65), (1, 66, 83), (7, 84, 86), (6, 87, 123), (7, 124, 263), (1, 264, 282), (7, 283, 285), (6, 286, 322), (7, 323, 467), (1, 468, 486), (7, 487, 490), (6, 491, 529), (7, 530, 677), (1, 678, 699), (6, 700, 738), (7, 739, 861)] 132 # Boxer1_11_2IdealData=[(7, 0, 87), (1, 88, 110), (7, 111, 116), (6, 117, 167), (7, 168, 336), (1, 337, 356), (7, 357, 364), (6, 365, 416), (7, 417, 568), (1, 569, 589), (7, 590, 597), (6, 598, 650), (7, 651, 809), (1, 810, 832), (7, 833, 837), (6, 838, 888), (7, 889, 1015)] 133 # 134 # Boxer1_14_1IdealData=[(7, 0, 84), (1, 85, 100), (3, 101, 159), (7, 160, 358), (1, 359, 380), (3, 381, 442), (7, 443, 623), (1, 624, 642), (3, 643, 702), (7, 703, 864), (1, 865, 882), (3, 883, 940), (7, 941, 1068)] 135 # Boxer1_14_2IdealData=[(7, 0, 73), (1, 74, 88), (7, 89, 94), (3, 95, 165), (7, 166, 311), (1, 312, 326), (7, 327, 333), (3, 334, 401), (7, 402, 546), (1, 547, 569), (7, 570, 572), (3, 573, 642), (7, 643, 793), (1, 794, 811), (7, 812, 818), (3, 819, 887), (7, 888, 1015)] 136 # 137 # Boxer1_15_1IdealData=[(7, 0, 62), (1, 63, 73), (7, 74, 83), (6, 84, 104), (7, 105, 109), (2, 110, 153), (7, 154, 338), (1, 339, 355), (6, 356, 381), (7, 382, 383), (2, 384, 435), (7, 436, 603), (1, 604, 620), (7, 621, 623), (6, 624, 646), (2, 647, 707), (7, 708, 907), (1, 908, 924), (7, 925, 928), (6, 929, 950), (7, 951, 956), (2, 957, 1007), (7, 1008, 1184)] 138 # Boxer1_15_2IdealData=[(7, 0, 99), (1, 100, 117), (7, 118, 130), (6, 131, 158), (7, 159, 163), (2, 164, 218), (7, 219, 412), (1, 413, 430), (7, 431, 439), (6, 440, 476), (7, 477, 482), (2, 483, 538), (7, 539, 751), (1, 752, 771), (7, 772, 782), (6, 783, 821), (7, 822, 826), (2, 827, 886), (7, 887, 1092), (1, 1093, 1110), (7, 1111, 1122), (6, 1123, 1151), (7, 1152, 1157), (2, 1158, 1214), (7, 1215, 1451)] 139 # 140 # Boxer2_1_1IdealData=[(7, 0, 170), (1, 171, 189), (7, 190, 312), (1, 313, 329), (7, 330, 451), (1, 452, 472), (7, 473, 585), (1, 586, 612), (7, 613, 763)] 141 # Boxer2_1_2IdealData=[(7, 0, 244), (1, 245, 279), (7, 280, 404), (1, 405, 445), (7, 446, 561), (1, 562, 613), (7, 614, 724), (1, 725, 772), (7, 773, 940)] 142 # 143 # Boxer2_3_1IdealData=[(7, 0, 135), (6, 136, 185), (7, 186, 316), (6, 317, 374), (7, 375, 492), (6, 493, 549), (7, 550, 656), (6, 657, 713), (7, 714, 876)] 144 # Boxer2_3_2IdealData=[(7, 0, 148), (6, 149, 218), (7, 219, 339), (6, 340, 408), (7, 409, 530), (6, 531, 600), (7, 601, 715), (6, 716, 783), (7, 784, 918)] 145 # 146 # Boxer2_5_1IdealData=[(7, 0, 153), (3, 154, 203), (7, 204, 326), (3, 327, 377), (7, 378, 496), (3, 497, 545), (7, 546, 665), (3, 666, 724), (7, 725, 851)] 147 # Boxer2_5_2IdealData=[(7, 0, 145), (3, 146, 199), (7, 200, 339), (3, 340, 392), (7, 393, 517), (3, 518, 573), (7, 574, 692), (3, 693, 753), (7, 754, 885)] 148 # 149 # Boxer2_7_1IdealData=[(7, 0, 133), (2, 134, 169), (7, 170, 333), (2, 334, 386), (7, 387, 531), (2, 532, 589), (7, 590, 730), (2, 731, 791), (7, 792, 908)] 150 # Boxer2_7_2IdealData=[(7, 0, 139), (2, 140, 178), (7, 179, 346), (2, 347, 389), (7, 390, 544), (2, 545, 581), (7, 582, 737), (2, 738, 788), (7, 789, 907)] 151 # 152 # Boxer2_8_1IdealData=[(7, 0, 93), (5, 94, 190), (7, 191, 317), (5, 318, 416), (7, 417, 543), (5, 544, 637), (7, 638, 757), (5, 758, 858), (7, 859, 1008)] 153 # Boxer2_8_1IdealData=[(7, 0, 117), (5, 118, 196), (7, 197, 333), (5, 334, 425), (7, 426, 554), (5, 555, 645), (7, 646, 773), (5, 774, 868), (7, 869, 995)] 154 # 155 # Boxer2_11_1IdealData=[(7, 0, 157), (1, 158, 170), (7, 171, 178), (6, 179, 217), (7, 218, 359), (1, 360, 375), (7, 376, 380), (6, 381, 416), (7, 417, 571), (1, 572, 587), (7, 588, 592), (6, 593, 632), (7, 633, 783), (1, 784, 798), (7, 799, 803), (6, 804, 846), (7, 847, 990)] 156 # Boxer2_11_2IdealData=[(7, 0, 139), (1, 140, 156), (7, 157, 177), (3, 179, 224), (7, 225, 645), (1, 646, 654), (7, 655, 674), (6, 675, 721), (7, 722, 865), (1, 866, 879), (7, 880, 898), (6, 899, 946), (7, 947, 1081), (1, 1082, 1095), (7, 1096, 1109), (6, 1110, 1156), (7, 1157, 1288)] 157 # 158 # Boxer3_1_1IdealData=[(7, 0, 135), (1, 136, 147), (7, 148, 203), (1, 204, 209), (7, 210, 272), (1, 273, 282), (7, 283, 336), (1, 337, 348), (7, 349, 504)] 159 # Boxer3_1_2IdealData=[(7, 0, 206), (1, 207, 220), (7, 221, 311), (1, 312, 323), (7, 324, 408), (1, 409, 421), (7, 422, 503), (1, 504, 517), (7, 518, 644)] 160 # 161 # Boxer3_3_1IdealData=[(7, 0, 115), (6, 116, 144), (7, 145, 200), (6, 201, 231), (7, 232, 293), (6, 294, 326), (7, 327, 384), (6, 385, 417), (7, 418, 554)] 162 # Boxer3_3_2IdealData=[(7, 0, 146), (6, 147, 187), (7, 188, 296), (6, 297, 338), (7, 339, 430), (6, 431, 478), (7, 479, 565), (6, 566, 611), (7, 612, 761)] 163 # 164 # Boxer3_5_1IdealData=[(7, 0, 109), (3, 110, 156), (7, 157, 249), (3, 250, 295), (7, 296, 390), (3, 391, 436), (7, 437, 519), (3, 520, 566), (7, 567, 703)] 165 # Boxer3_5_2IdealData=[(7, 0, 174), (3, 175, 234), (7, 235, 335), (3, 336, 383), (7, 384, 487), (3, 488, 537), (7, 538, 641), (3, 642, 705), (7, 706, 842)] 166 # 167 # Boxer3_7_1IdealData=[(7, 0, 153), (2, 154, 186), (7, 187, 319), (2, 320, 361), (7, 362, 487), (2, 487, 526), (7, 527, 653), (2, 654, 696), (7, 697, 863)] 168 # Boxer3_7_2IdealData=[(7, 0, 160), (2, 161, 211), (7, 212, 346), (2, 347, 404), (7, 405, 528), (2, 529, 586), (7, 587, 701), (2, 702, 754), (7, 755, 909)] 169 # 170 # Boxer3_8_1IdealData=[(7, 0, 133), (5, 134, 199), (7, 200, 309), (5, 310, 380), (7, 381, 481), (5, 482, 556), (7, 557, 654), (5, 655, 727), (7, 728, 885)] 171 # Boxer3_8_2IdealData=[(7, 0, 134), (5, 135, 230), (7, 231, 301), (5, 302, 420), (7, 421, 514), (5, 515, 617), (7, 618, 699), (5, 700, 795), (7, 796, 941)] 172 # 173 # Boxer3_11_1IdealData=[(7, 0, 110), (1, 111, 116), (7, 117, 133), (6, 134, 162), (7, 163, 252), (1, 253, 259), (7, 260, 276), (6, 277, 304), (7, 305, 391), (1, 392, 400), (7, 401, 416), (6, 417, 443), (7, 444, 523), (1, 524, 530), (7, 531, 548), (6, 549, 578), (7, 579, 759)] 174 # Boxer3_11_2IdealData=[(7, 0, 133), (1, 134, 149), (7, 150, 176), (6, 177, 217), (7, 219, 348), (1, 349, 368), (7, 369, 392), (6, 393, 437), (7, 438, 565), (1, 566, 579), (7, 580, 598), (6, 599, 644), (7, 645, 752), (1, 753, 773), (7, 774, 791), (6, 792, 835), (7, 836, 1029)] 175 # 176 # 177 178 179 180 #ml=[] 181 #for i in xrange(1000): 182 # ml.append(random.randint(1,7)) 183 #t1 = timeit.Timer('fitnessutil.GetIndexesOfGroupsInList(ml)' , 'from __main__ import ml ;import fitnessutil') 184 #print t1.repeat(1,300) 185