Best JavaScript code snippet using playwright-internal
main_function.py
Source:main_function.py  
1# -*- coding: utf-8 -*-2"""3Created on Thu Nov 29 15:35:08 20184@author: Kuan5"""6import math7import copy8import datetime9import pandas as pd10import pyodbc 11import time12#import pprint13#%% sql sever setting14#server = '' 15#database = '' 16#username = '' 17#password = '' 18#cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)19server = '' 20database = '' 21username = '' 22password = ''23cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)24#%% parameter setting25Max_mile = 20 # upper limit for a ride26service_time = 5 # service time when  arrive locstion 27down_service_time = 128time_window = 30 # aditional time windows29constant_a = 330constant_b = 531Max_trip_interval = 15 #minutes32new_tw = 533Large_cost = 9999934#%% input three sql data sheet35## a funtion to transfer the form of data36## transfer into pandas or list 37def datafit(data,form):38    colList = []39    for colInfo in data.description:40        colList.append(colInfo[0])41    rowList = []42    while True:43        try:44            row = data.fetchone()45            if row:46                rowList.append(list(row))47            else:48                break49        except:   50            continue;51    if form == "pandas":52        sql_data = pd.DataFrame(rowList) 53        sql_data.columns = colList 54    elif form == "list":55        sql_data = rowList56    return sql_data57## delete the data has been create and recalculate 58cursor = cnxn.cursor()59cursor.execute('delete from VRP_HAS_MATCHED') 60cnxn.commit()61 62#print "---sql_resource---"63cursor.execute("SELECT * from VRP_CAR_USEABLE ")64sql_resource = datafit(cursor,"list") 65#print ("---sql_cost---")66cursor.execute("SELECT * from VRP_TRAVEL_MATRIX_DATA ")67sql_cost = datafit(cursor,"list")68#print ("---sql_request---")69cursor.execute("SELECT * from VRP_PRE_ORDER")70sql_request = datafit(cursor,"list")71## if you want the data in a specific day72#cursor.execute("SELECT * from VRP_CAR_USEABLE where take_date ='2019/01/14'")73#cursor.execute("SELECT * from VRP_TRAVEL_MATRIX_DATA where take_date ='2019/01/14'")74#cursor.execute("SELECT * from VRP_PRE_ORDER where take_date ='2019/01/14'")75## if you want the data in a specific period76#cursor.execute("SELECT * from VRP_CAR_USEABLE where take_date between '2019/01/14' and '2019/01/18'")77#cursor.execute("SELECT * from VRP_TRAVEL_MATRIX_DATA where take_date between '2019/01/14' and '2019/01/18'")78#cursor.execute("SELECT * from VRP_PRE_ORDER where take_date between '2019/01/14' and '2019/01/18'")79#%% Main procedure 80def main_funtion(request,cost,resource): 81      82    ## a funtion to transfer the time format83    def time_form(intime):84        str_intime = str(intime)85        int_intime = int(str_intime[11])*10*60+int(str_intime[12])*60+int(str_intime[14])*10+int(str_intime[15])86        return int_intime 87    88    ## calculate the route time89    def total_time(route):90        tt_time = 091        for c in range(len(route)-1):92            tt_time += t[Day_order[route[c]][1],Day_order[route[c+1]][1]]          93        return tt_time94    95    ## calculate the route distant96    def total_distant(route):97        tt_dist = 098        for c in range(len(route)-1):99            tt_dist += d[Day_order[route[c]][1],Day_order[route[c+1]][1]]          100        return tt_dist101    102    ## give the route a start customer103    ## 104    def route_seed_initialer(unroute_list,village,Day):105        ## sort the unroute customers acordding to the pick up time106        now_unroute_list = []107        for i in unroute_list:108            now_unroute_list.append(Day_order[i])109        sort_now_unroute_list = sorted(now_unroute_list, key = lambda x : (x[2]))110        sort_unroute_list = []111        for i in sort_now_unroute_list:112            sort_unroute_list.append(i[0]) 113        ## sort the car drivers acordding to the starting service time    114        all_car_key_info = []115        for i in unuse_resource_info[village][Day]:116            all_car_key_info.append(resource_info[i])117        sort_all_car_key_info = sorted(all_car_key_info, key = lambda x : (x[-7]))118        sort_all_car_key = []119        for i in sort_all_car_key_info:120            sort_all_car_key.append(i[0])  121        ## give the route a start customer122        ## return the unroute list without the customerhave been matched123        new_unroute_list = copy.copy(sort_unroute_list)124        for j in sort_unroute_list:125            unservable_time = 0126            break_sign = 0127            for i in sort_all_car_key: #iå°±æ¯è»è128                current_routeA = [i,j,-j,i]129                if check_feasible_route(current_routeA,i) == 0: #輸å
¥è·¯å¾èè»è130                    break_sign += 1131                    useed_resource_info[village][Day][i] = [i,j,-j,i]   132                    new_unroute_list.remove(j)133                    unuse_resource_info[village][Day].remove(i)134                    break135                else:136                    unservable_time += 1137                    len(sort_all_car_key[sort_all_car_key.index(i):])138            if unservable_time == len(sort_all_car_key):139                if Day not in unservable_requests.keys():140                    unservable_requests[Day] = []141                    unservable_requests[Day].append(j)142                else:143                    unservable_requests[Day].append(j)144                new_unroute_list.remove(j)145            if break_sign>0:146                break147            #åªè¦æä¸å人å¯ä»¥æ¾é²ä¸å°è»å°±å¯ä»¥148#        print "in ",useed_resource_info[village][Day]149        return new_unroute_list    150    151    def route_seed_initialer2(unroute_list,village,Day):152#        print unroute_list,village,Day153        now_unroute_list = []154        for i in unroute_list:155            now_unroute_list.append(Day_order[i])156        sort_now_unroute_list = sorted(now_unroute_list, key = lambda x : (x[2]))157        sort_unroute_list = []158        for i in sort_now_unroute_list:159            sort_unroute_list.append(i[0])160        seed_list = copy.copy(sort_unroute_list)161        new_unroute_list = []162        for i in range(len(sort_unroute_list)-1):163            req_i = sort_unroute_list[i]164            req_i_1 = sort_unroute_list[i+1]165            LDTk = Day_order[req_i][7]166            TTDkPk_1 = t[Day_order[req_i][5],Day_order[req_i_1][1]]167            EPTk_1 = Day_order[req_i_1][2]168            if LDTk+TTDkPk_1<=EPTk_1:169                seed_list.remove(req_i_1)170                new_unroute_list.append(req_i_1)171        for i in unuse_resource_info[village][Day]:172            unservable_time = 0173            for j in seed_list: 174                current_routeA = [i,j,-j,i]175                if check_feasible_route(current_routeA,i) == 0:176                    useed_resource_info[village][Day][i] = [i,j,-j,i]177                    seed_list.remove(j)178                    break                  179                else:180                    unservable_time += 1181        for i in useed_resource_info[village][Day].keys():182            if i in unuse_resource_info[village][Day]:183                unuse_resource_info[village][Day].remove(i)184#        print "in ",useed_resource_info[village][Day]185        return new_unroute_list+seed_list  186   187    def check_feasible_route(feasible_route,car_number):188#        print feasible_route,car_number189        debug_time = Day_order[feasible_route[0]][2] ## first customer pick time 190        max_car_capacity = resource_info[car_number][-1] ## car capacity191  192        route_feasible_or_not = True193        ## check if the trip exceeds the max trip miles194        if route_feasible_or_not == True:195            temp_store_list = [] 196            cal_sign = 0197            cal_anchor = 0198            199            for i in feasible_route :200                if type(i) ==  int:201                    temp_store_list.append(i)202                    if i not in temp_store_list or -i not in temp_store_list:        203                        cal_sign += 1204                    elif i < 0 and -i in temp_store_list:205                        cal_sign -= 1206                    if cal_sign == 0:207                        cal_route = temp_store_list[cal_anchor:feasible_route.index(i)+1]208#                        print cal_anchor,feasible_route.index(i)+1209                        o_dist = total_distant(cal_route)210#                        for test_c in range(len(cal_route)-1):211#                            o_dist += d[Day_order[cal_route[test_c]][1],Day_order[cal_route[test_c+1]][1]]         212                        213                        if o_dist >= Max_mile:214                            route_feasible_or_not = False 215#                        else:216#                            print cal_route,car_number,total_distant(cal_route)217                            218                        cal_anchor = feasible_route.index(i)+1 219        ## check if the capacity exceeds the car limit               220        passenger_in_car = 0 221        if route_feasible_or_not == True: 222            for x in feasible_route:223                if x > 0 :224                    passenger_in_car += Day_order[x][4] 225                elif x <0:226                    passenger_in_car -= Day_order[x][4]227                if passenger_in_car > max_car_capacity:228                    route_feasible_or_not = False229                    break230                231        ## check the route time window  232        debug_route_time = []233        debug_real_time = []    234        if route_feasible_or_not == True: 235            for i in range(len(feasible_route)):236                pre_node = Day_order[feasible_route[i-1]][1]            237                now_node = Day_order[feasible_route[i]][1]238    239                e_time = Day_order[feasible_route[i]][2] #earlyææ©ä¸è»240                l_time = Day_order[feasible_route[i]][3] #latest ææä¸è»241    242                if i == 0:243                    debug_route_time.append(debug_time)244                    debug_real_time.append(debug_time)245                    next_node = Day_order[feasible_route[i+1]][1]246                    if now_node == next_node:247                        continue248                    else:249                        if feasible_route[i] < 0:250#                            print feasible_route[i-1]251                            debug_time += down_service_time252#                            debug_time += service_time 253                        else:    254                            debug_time += service_time                   255                elif i == len(feasible_route)-1:    256                    debug_time = debug_time+t[pre_node,now_node]257                    debug_route_time.append(debug_time)258                    debug_real_time.append(l_time)259                elif i > 0:260#                    print pre_node,now_node261                    debug_time = max([debug_time+t[pre_node,now_node],e_time])262                    debug_route_time.append(debug_time)263                    debug_real_time.append(l_time)264                    next_node = Day_order[feasible_route[i+1]][1]265                    if now_node == next_node:266                        continue267                    else:268                        if feasible_route[i] < 0:269                            debug_time += down_service_time   270                        else:    271                            debug_time += service_time272            for i,j in zip(debug_route_time,debug_real_time):273                if  i > j:274#                    print i,j275#                    debug1 = 1276                    route_feasible_or_not = False                    277        if route_feasible_or_not == True:  278            debug_signal = 0279        else:280            debug_signal = 1281#        if 14410 in feasible_route:282#            print feasible_route,car_number,route_feasible_or_not283           284        return debug_signal285    def regret_insert(unroute_list,village,Day):286#        print "unroute_list",unroute_list,village,Day287#        for fadfad in range(10):288        while len(unroute_list) > 0:289#            print "unroute_list",unroute_list290            current_list = useed_resource_info[village][Day].values()291#            print useed_resource_info292#            print "current_list",current_list293            car_keys = useed_resource_info[village][Day].keys()        294            temp_route = copy.copy(current_list)295    #        print "temp_route:",temp_route296            c2_value = []297            c2_route = []298            c2_place = []299            c2_customer = []300#            print "----"301            for customer in unroute_list:302#                print customer303                min_c1_route = []304                min_c1_value = []305                min_c1_place = []306                customer_p = customer307                customer_d = -customer_p308                for o_route in temp_route: 309                    car_number = car_keys[current_list.index(o_route)]310                    o_route_tt_dist = 0311                    for test_c in range(len(o_route)-1):312                        o_route_tt_dist += d[Day_order[o_route[test_c]][1],Day_order[o_route[test_c+1]][1]]                      313                    c1_value = []314                    c1_place = []315                    for p_place in range(1,len(o_route)+1):         316                        test_p_route =  copy.copy(o_route)317                        test_p_route.insert(p_place,customer_p)318#                        print319#                        print "test_p_route-----------",test_p_route                        320#                        feasibility1 = check_feasible_route(test_p_route,car_number)321#                        print "-----------feasibility1",feasibility1322                        feasibility1 = 0323                        if feasibility1 == 0:324                            for d_place in range(p_place+1,len(test_p_route)):325                                #print d_place,"----"326                                test_d_route = copy.copy(test_p_route)327                                test_d_route.insert(d_place,customer_d)328#                                print"--"                                329                                feasibility2 = check_feasible_route(test_d_route,car_number)330#                                if (316 in test_d_route and 318 in test_d_route) or (317 in test_d_route and 318 in test_d_route):331#                                    print test_d_route,feasibility2332#                                    print "--"333#                                print"--"334                                if feasibility2 == 0:###############335                                    ##################################################################336#                                    tt_dist = total_distant(test_d_route)337#                                    print test_d_route338#                                    for test_c in range(len(test_d_route)-1):339##                                        print test_d_route[test_c],test_d_route[test_c+1],d[Day_order[test_d_route[test_c]][1],Day_order[test_d_route[test_c+1]][1]]340#                                        tt_dist += d[Day_order[test_d_route[test_c]][1],Day_order[test_d_route[test_c+1]][1]]341#                                    c1 = tt_dist-o_route_tt_dist342                                    c1 = total_distant(test_d_route)343#                                    print test_d_route,c1344                                    ##################################################################345                                    c1_value.append(c1)346                                    c1_place.append([p_place,d_place])347                                348                    min_c1_route.append(o_route)  349#                    print c1_value350                    if len(c1_value) > 0:351                        min_value = min(c1_value)352                        min_place = c1_place[c1_value.index(min_value)]                           353                        min_c1_value.append(min_value)354                        min_c1_place.append(min_place)355                    else:                356                        min_c1_value.append(Large_cost)357                        min_c1_place.append([-Large_cost,-Large_cost])     358#                print "min_c1_value",min_c1_value359                min_min_c1_value = min(min_c1_value)360                min_c1_index = min_c1_value.index(min_min_c1_value)361                min_min_c1_place = min_c1_place[min_c1_index]362                min_min_c1_route = min_c1_route[min_c1_index]363                c2_customer.append([customer,-customer])364#                print c2_customer365                c2 = 0366                if min_c1_value.count(Large_cost) != len(min_c1_value):367#                    print min_c1_value368                    for mmc in range(len(min_c1_value)):369#                        print min_c1_route[mmc],min_min_c1_route370#                        if min_c1_route[mmc] != min_min_c1_route:371                            372                        c2 += min_c1_value[mmc] - min_min_c1_value373#                        print "------------",c2374                else:375                    c2 = -Large_cost376                c2_value.append(c2)377                c2_route.append(min_min_c1_route)378                c2_place.append(min_min_c1_place)  379#                print c2_value,c2_route,c2_place380#            print c2_value  381            if c2_value.count(-Large_cost) != len(c2_value):382#                print "1111111111111"383                max_c2_value = max(c2_value)384#                print max_c2_value385                max_c2_index = c2_value.index(max_c2_value)386                max_c2_route = c2_route[max_c2_index]387                max_c2_place = c2_place[max_c2_index]388                max_c2_customer = c2_customer[max_c2_index]389                max_c2_index = current_list.index(max_c2_route)        390                for insert_p,insert_c in zip(max_c2_place,max_c2_customer):391                    max_c2_route.insert(insert_p,insert_c)392                current_list[max_c2_index] = max_c2_route393                unroute_list.remove(max_c2_customer[0])         394            else:395#                print "----",Day,unroute_list396#                unservable_requests[Day] = unroute_list ##route2397#                break398                unroute_list = route_seed_initialer(unroute_list,village,Day)399                400        return current_list401    402#    def tw_reducer(feasible_route,car_number):403##        print feasible_route404##        if True:405##            for i in feasible_route:406##                print Day_order[i][2],Day_order[i][3]407#        debug_time = Day_order[feasible_route[0]][2]408#        endtime = resource_info[car_number][-2]409#        debug_route_time = []410#        debug_real_time = []411#        for i in range(len(feasible_route)):          412#            pre_node = Day_order[feasible_route[i-1]][1]            413#            now_node = Day_order[feasible_route[i]][1]414#            e_time = Day_order[feasible_route[i]][2]415#            l_time = Day_order[feasible_route[i]][3]416#            if i == 0:417#                debug_route_time.append(debug_time)418#                debug_real_time.append(debug_time)419#                next_node = Day_order[feasible_route[i+1]][1]420#                if now_node == next_node:421#                    continue422#                else:423#                    if feasible_route[i] < 0:424##                            print feasible_route[i-1]425#                        debug_time += down_service_time426##                            debug_time += service_time427#                        428#                    else:    429#                        debug_time += service_time              430#            elif i == len(feasible_route)-1:431#                debug_time = debug_time+t[pre_node,now_node]432#                debug_route_time.append(debug_time)433#                debug_real_time.append(endtime)434#            elif i > 0:435#                debug_time = max([debug_time+t[pre_node,now_node],e_time])436#                debug_route_time.append(debug_time)437#                debug_real_time.append(l_time)   438#                next_node = Day_order[feasible_route[i+1]][1]439#                if now_node == next_node:440#                    continue441#                else:442#                    if feasible_route[i] < 0:443##                            print feasible_route[i-1]444#                        debug_time += down_service_time445##                            debug_time += service_time446#                        447#                    else:    448#                        debug_time += service_time                 449#        for i,j in zip(feasible_route,debug_route_time):450#451#            if i > 0:452#    453#                Day_order[i][2] = math.floor(j - new_tw/2)454#                Day_order[i][3] = math.floor(j + new_tw/2)455#            elif i < 0:456#    457#                Day_order[-i][6] = math.floor(j - new_tw/2)458#                Day_order[-i][7] = math.floor(j + new_tw/2)459#                Day_order[i][2] = math.floor(j - new_tw/2)460#                Day_order[i][3] = math.floor(j + new_tw/2)461    def relocation_operator(original_plan):462        all_reinsert_info = {}463        for i in original_plan:464            for j in original_plan[i]:465                if j > 0 :466                    all_reinsert_info[j] = i 467        all_route = original_plan.keys()468        469        relocation_for_remove_time = 0470        relocation_for_insert_time = 0471        for relocate_time in range(1):472            for i in all_reinsert_info:473                474                relocation_for_remove_start_time = time.time()475                car_c = all_reinsert_info[i]476                remoeve_route_c = original_plan[car_c][:]477                if type(i) == int:478                    remoeve_route_c.remove(i)479                    remoeve_route_c.remove(-i)480                    relocation_for_remove_end_time = time.time()481                    relocation_for_remove_time += (relocation_for_remove_end_time-relocation_for_remove_start_time)482                    for l in all_route:483                        if l != car_c:484                            for m in range(1,len(original_plan[l])+1):485                                for n in range(m+1,len(original_plan[l])+1):486                                    if i in original_plan[car_c]:  487                                        relocation_for_insert_start_time = time.time()488                                        insert__route_c = original_plan[l][:]489                                        insert__route_c.insert(m,i)490                                        insert__route_c.insert(n,-i)491                                        relocation_for_insert_end_time = time.time()492                                        relocation_for_insert_time += (relocation_for_insert_end_time-relocation_for_insert_start_time)493        #                                print insert__route_c,check_feasible_route(insert__route_c,l)494                                        if check_feasible_route(insert__route_c,l) == 0:495        #                                    print insert__route_c,check_feasible_route(insert__route_c,l)496                                            if total_distant(original_plan[car_c])+total_distant(original_plan[l])-total_distant(remoeve_route_c)-total_distant(insert__route_c) > 0:497#                                                print total_distant(original_plan[car_c])+total_distant(original_plan[l])-total_distant(remoeve_route_c)-total_distant(insert__route_c) 498                                                original_plan[car_c] = remoeve_route_c499                                                original_plan[l] = insert__route_c500                                                all_reinsert_info[i] = l   501                                            if len(remoeve_route_c) == 0 or len(insert__route_c) == 0 :502                                                print "need to cut car"                                            503                                    else:504                                        break 505        return original_plan506    def exchange_operator(original_plan):507        all_reinsert_info = {}508        for i in original_plan:509#            print i,original_plan[i]510            for j in original_plan[i]:511#                print j512                if j > 0 and type(j) == int:513                    all_reinsert_info[j] = i514        exchange_first_insert = 0515        exchange_second_insert = 0516    #    exchange_start_time = time.time()517        for exchange_time in range(1):518            for i in all_reinsert_info:519                for j in all_reinsert_info:520                    car_r = all_reinsert_info[i]521                    car_s = all_reinsert_info[j]522                    if i != j and car_r != car_s:523            #            print 524            #            print Day_order[i],Day_order[j]525                        EPTi = Day_order[i][2]526                        LPTi = Day_order[i][3]527                        EDTi = Day_order[i][6]528                        LDTi = Day_order[i][7]       529                        EPTj = Day_order[j][2]530                        LPTj = Day_order[j][3]531                        EDTj = Day_order[j][6]532                        LDTj = Day_order[j][7]           533                        if (EPTi <= LPTj and EPTj <= LPTi) or (EDTi <= LDTj and EDTj <= LDTi):                    534       535                            remoeve_route_r = original_plan[car_r][:]536                            remoeve_route_r.remove(i)537                            remoeve_route_r.remove(-i)538                            539                            best_insert_route_r = []540                            exchange_first_start_insert = time.time()541                            for m in range(1,len(remoeve_route_r)+1):542                                for n in range(m+1,len(remoeve_route_r)+1):543    #                                print m,n544                                    insert_route_r = remoeve_route_r[:]545    #                                insert_route_r = copy.deepcopy(remoeve_route_r)546                                    insert_route_r.insert(m,j)547                                    insert_route_r.insert(n,-j)548    #                                print insert_route_r,check_feasible_route(insert_route_r,car_r)549                                    if check_feasible_route(insert_route_r,car_r) == 0:550                                        if len(best_insert_route_r) == 0:551                                            best_insert_route_r = insert_route_r552                                        else:553                                            if total_distant(insert_route_r) < total_distant(best_insert_route_r):554                                                best_insert_route_r = insert_route_r555            556                            exchange_first_end_insert = time.time()            557                            exchange_first_insert += (exchange_first_end_insert-exchange_first_start_insert)    558                            if len(best_insert_route_r) > 0 :        559                                remoeve_route_s = original_plan[car_s][:]560                                remoeve_route_s.remove(j)561                                remoeve_route_s.remove(-j) 562                                exchange_second_start_insert = time.time()563                                best_insert_route_s = []564                                for m in range(1,len(remoeve_route_s)+1):565                                    for n in range(m+1,len(remoeve_route_s)+1):566                                        insert_route_s = remoeve_route_s[:]567                                        insert_route_s.insert(m,i)568                                        insert_route_s.insert(n,-i)                569                                        if check_feasible_route(insert_route_s,car_s) == 0:570                #                            print "-------------------"571                                            if len(best_insert_route_s) == 0:572                                                best_insert_route_s = insert_route_s573                                            else:574                                                if total_distant(insert_route_s) < total_distant(best_insert_route_s):575                                                    best_insert_route_s = insert_route_s576                                exchange_second_end_insert = time.time()577                                exchange_second_insert += (exchange_second_end_insert-exchange_second_start_insert)578                                if len(best_insert_route_s) > 0:579                                    if (total_distant(original_plan[car_r])+total_distant(original_plan[car_s]))-(total_distant(best_insert_route_r)+total_distant(best_insert_route_s))>0:580#                                        print (total_distant(original_plan[car_r])+total_distant(original_plan[car_s]))-(total_distant(best_insert_route_r)+total_distant(best_insert_route_s))581                                        original_plan[car_r] = best_insert_route_r                 582                                        original_plan[car_s] = best_insert_route_s583                                        all_reinsert_info[i] = car_s584                                        all_reinsert_info[j] = car_r  585                                        if len(best_insert_route_r) == 0 or len(best_insert_route_s) == 0 :586                                            print "need to cut car"587        return original_plan   588    def check_service_time(feasible_route,out_type = None):589#        print feasible_route590        debug_time = Day_order[feasible_route[0]][2]591#        print debug_time592        debug_service_time = []   593        for i in range(len(feasible_route)):594            pre_node = Day_order[feasible_route[i-1]][1]            595            now_node = Day_order[feasible_route[i]][1]596            e_time = Day_order[feasible_route[i]][2]597#            l_time = Day_order[feasible_route[i]][3]598            if i == 0:599                debug_service_time.append(debug_time)600            elif i == len(feasible_route)-1:        601                debug_time = debug_time+t[pre_node,now_node]602                debug_service_time.append(debug_time)603            else:604                debug_time = max([debug_time+t[pre_node,now_node],e_time])605                debug_service_time.append(debug_time)606                if now_node > 0:607                    debug_time += service_time608#                print "afterser:",debug_time609        ri_time = 0610        test_n = 0                    611        for i in range(len(feasible_route)):612    #        print i,test[i],test[i+1],t[Day_order[test[i]][1],Day_order[test[i+1]][1]]613            if type(feasible_route[i]) == int:614                test_n += feasible_route[i]615                if feasible_route[i] > 0:616                    test_target = feasible_route.index(-feasible_route[i])617                    618                    ride_time =  debug_service_time[test_target] - debug_service_time[i]   619                    ri_time += ride_time620        if out_type == "list":621            return debug_service_time622        else:623            return ri_time         624    def check_arrive_time(feasible_route,out_type = None):625#        print feasible_route626        debug_time = Day_order[feasible_route[0]][2]627#        print debug_time628        debug_arrive_time = []   629        for i in range(len(feasible_route)):630            pre_node = Day_order[feasible_route[i-1]][1]            631            now_node = Day_order[feasible_route[i]][1]632            e_time = Day_order[feasible_route[i]][2]633#            l_time = Day_order[feasible_route[i]][3]634            if i == 0:635                debug_arrive_time.append(debug_time)636            elif i == len(feasible_route)-1:        637                debug_time = debug_time+t[pre_node,now_node]638                debug_arrive_time.append(debug_time)639            else:640                debug_arrive_time.append(debug_time+t[pre_node,now_node])641                debug_time = max([debug_time+t[pre_node,now_node],e_time])642                643                if now_node > 0:644                    debug_time += service_time645#                print "afterser:",debug_time646        ri_time = 0647        test_n = 0  648                          649        for i in range(len(feasible_route)):650    #        print i,test[i],test[i+1],t[Day_order[test[i]][1],Day_order[test[i+1]][1]]651            if type(feasible_route[i]) == int:652                test_n += feasible_route[i]653                if feasible_route[i] > 0:654                    test_target = feasible_route.index(-feasible_route[i])655                    ride_time =  debug_arrive_time[test_target] - debug_arrive_time[i]   656                    ri_time += ride_time657#        print debug_arrive_time                658        if out_type == "list":659            return debug_arrive_time660        else:661            return ri_time                     662    def ride_counter(feasible_route,out_type = None):663#        print "    ",feasible_route664#        print "debug_time",debug_time665        node_service_time = check_service_time(feasible_route,"list")666        node_arrive_time = check_arrive_time(feasible_route,"list")667        ##########################668        test = feasible_route669        670        e_time_list = [Day_order[i][2] for i in feasible_route]671        l_time_list = [Day_order[i][3] for i in feasible_route]672#        print len(l_time_list)673        cuttime_list = []674        for i,j in zip(node_service_time,node_arrive_time):675            if j < i:676                cuttime_list.append(i-j)677            else:678                cuttime_list.append(0)679#        print cuttime_list                680#        print len(cuttime_list)681        ri_time = 0682#        route_arrive_time = check_service_time(test)683        test_n = 0684        test_ancher = 0685        if test[0] == 0:686            for i in range(len(test)-1):687                test_n += test[i]688                if test_n == 0:689                    if test[i] != 0:690                        for j in range(test_ancher,i+1):691                            692                            if cuttime_list[j] > 0:693                                if j == test_ancher:694                                    node_arrive_time[test_ancher] = e_time_list[test_ancher] 695                                else:696                                    697                                    change_new_timesign = 0698#                                    print "1   ",change_new_timesign699                                    for l in range(test_ancher,j):700                                        new_service_time = node_service_time[l] + cuttime_list[j]701                                        702                                        if new_service_time >= e_time_list[l] and new_service_time <= l_time_list[l]:703                                            continue704                                        else:705                                            change_new_timesign += 1706#                                    print "2   ",change_new_timesign707                                            708                                    if change_new_timesign == 0:709#                                        print test_ancher,j710                                        for l in range(test_ancher,j):711                                            node_service_time[l] = node_service_time[l] + cuttime_list[j]   712                                            node_arrive_time[l] = node_arrive_time[l] + cuttime_list[j]   713                                            714                                        node_arrive_time[j] = e_time_list[j] 715                                    else:716#                                        print "NOOOO",test[j],cuttime_list[j]   717                                        left_time_list = [l_time_list[l]-node_arrive_time[l]for l in range(test_ancher,j)]718                                     719                                        min_left_time = min(left_time_list)720                                        for l in range(test_ancher,j):721#                                            print test[l],left_time_list[l-test_ancher]722                                            node_service_time[l] = node_service_time[l] + min_left_time723                                            node_arrive_time[l] = node_arrive_time[l] + min_left_time724                                        node_arrive_time[j] = node_arrive_time[j] + min_left_time                                               725                        726                    test_ancher = i+1727#        print "    ",node_service_time            728#        print "    ",node_arrive_time 729        cuttime_list = []730        for i,j in zip(node_service_time,node_arrive_time):731            if j < i:732                cuttime_list.append(i-j)733            else:734                cuttime_list.append(0)    735#        print cuttime_list                736        for i in range(len(test)):737    #        print i,test[i],test[i+1],t[Day_order[test[i]][1],Day_order[test[i+1]][1]]738            if type(test[i]) == int:739                test_n += test[i]740                if test[i] > 0:741                    test_target = test.index(-test[i])742    #                print test[i],-test[i]743                    ride_time =  node_service_time[test_target] - node_service_time[i]   744                    ri_time += ride_time745#        print node_service_time746        if out_type == "list":747            return node_service_time748        elif out_type == "arrive":749            return node_arrive_time750        else:751            return ri_time               752#%% sql cost753    ## make the time & distant cost matrix754    cost_info = {}755    dist_info = {}   756    for temp_cost in cost:757        cost_info[temp_cost[2],temp_cost[3]] = temp_cost[4]758        cost_info[temp_cost[3],temp_cost[2]] = temp_cost[4]759        cost_info[temp_cost[2],temp_cost[2]] = 0760        cost_info[temp_cost[3],temp_cost[3]] = 0761        dist_info[temp_cost[2],temp_cost[3]] = temp_cost[5]762        dist_info[temp_cost[3],temp_cost[2]] = temp_cost[5]763        dist_info[temp_cost[2],temp_cost[2]] = 0764        dist_info[temp_cost[3],temp_cost[3]] = 0765    ## time cost matrix766    t = copy.deepcopy(cost_info)767    ## distant cost matrix768    d = copy.deepcopy(dist_info)769#%% sql request770    771    request_info = {} 772    unservable_requests = {}773    village_key = [request[0][1]]774    ## the dictionary includes every request in a day 775    Day_request_info = {}   776    Day_request_info[village_key[0]] = {}777    ## the dictionary of the information of every request 778    ## Day_order = {request_number:[request number,pick place, EPT, LPT, passenger number, deliver place, EDT, LDT]}779    ## Day_order[request_number] = [request number,pick place, EPT, LPT, passenger number, deliver place, EDT, LDT]780    Day_order = {}781    782    ## frame the time window783    for i in request:784        SENDORDER = i[3]785        ## if there are no pick windows        786#        if i[8] == None or i[9] == None:787        if True:788            Pp,Dp,n_C= i[5],i[6],i[7]789            790            LT = (time_form(i[10])+time_form(i[11]))/2.0791            792            EPT = LT - constant_a*t[Pp,Dp] - constant_b  793            EDT = LT -            t[Pp,Dp] - constant_b         794            LPT = time_form(i[10])795            LDT = time_form(i[11])796            797            Day_order[SENDORDER] = [SENDORDER]+[Pp,EPT,EDT,n_C,Dp,LPT,LDT]798            Day_order[-SENDORDER] = [SENDORDER]+[Dp,LPT,LDT,n_C]            799            temp_one = [str(i[0]),i[1],i[4],i[5],i[6],i[7],EPT,EDT,LPT,LDT]800#        ## if there are no deliver windows 801#        elif i[10] == None or i[11] == None:802#            803#            Pp,Dp,n_C= i[5],i[6],i[7]804#805#            EPT = time_form(i[8])806#            EDT = time_form(i[9])807#            LPT = EPT +            t[Pp,Dp] + constant_b808#            LDT = EPT + constant_a*t[Pp,Dp] + constant_b809#810#            Day_order[SENDORDER] = [SENDORDER]+[Pp,EPT,EDT,n_C,Dp,LPT,LDT]811#            Day_order[-SENDORDER] = [SENDORDER]+[Dp,LPT,LDT,n_C]            812#            temp_one = [str(i[0]),i[1],i[4],i[5],i[6],i[7],EPT,EDT,LPT,LDT]   813#        ## if both windows exist 814#        else:815#            Pp,Dp,n_C= i[5],i[6],i[7]816# 817#            EPT = time_form(i[8])818#            EDT = time_form(i[9])          819#            LPT = time_form(i[10])820#            LDT = time_form(i[11]) 821#            822#            Day_order[SENDORDER] = [SENDORDER]+[Pp,EPT,EDT,n_C,Dp,LPT,LDT]823#            Day_order[-SENDORDER] = [SENDORDER]+[Dp,LPT,LDT,n_C]             824#            temp_one = [str(i[0]),i[1],i[4],i[5],i[6],i[7],EPT,EDT,LPT,LDT]825        SENDORDER_date = "19000000"826        SENDORDER_date = str(SENDORDER_date[:4])+"/"+str(SENDORDER_date[4:6])+"/"+str(SENDORDER_date[6:])827        SENDORDER_ID = str(SENDORDER)828    829        for j in SENDORDER_ID:830            if int(j) !=0:831                SENDORDER_ID = int(SENDORDER_ID[SENDORDER_ID.index(j):])832                break833        ## build the day request dictionary 834        ## let us know which requests should be matched in the specific day835        request_info[SENDORDER_ID] = [SENDORDER_date]+temp_one[0:2]+[SENDORDER_ID]+temp_one[2:10]836        request_info_now = request_info[SENDORDER_ID]837#        if request_info_now[2] not in village_key:838#            village_key.append(request_info_now[2])839        840        if request_info_now[0] not in Day_request_info[village_key[0]].keys():841            Day_request_info[village_key[0]][request_info_now[0]] = {}842            Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]] = [request_info_now[3]]843        else:844            if request_info_now[1] not in Day_request_info[request_info_now[2]][request_info_now[0]].keys():845                Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]] = []846                Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]] = [request_info_now[3]]847            else:848                Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]].append(request_info_now[3])849#        print Day_request_info850#%% sql resource              851    resource_info = {}852    853    ## the dictionary of the car did not be used 854    unuse_resource_info = {}855    ## the dictionary of the car have been used 856    useed_resource_info = {}857    858    for i in resource:859        car_code = i[4]+'-'+str(resource.index(i)+1)860        start_time = time_form(i[6])861        close_time = time_form(i[7])   862        temp_resource = [car_code,i[0],i[1],i[2].strftime('%Y/%m/%d'),i[3],i[4],i[5],time_form(i[6]),time_form(i[7]),i[8]]863        ## pretend the driver's location as a day order 864        ## but it does not include in day request865        Day_order[car_code] = [car_code,i[9],start_time,close_time,0]866#        temp_carnumber = temp_resource[4]867#        868#        temp_village = temp_resource[3]869#        870#        temp_date = temp_resource[0]871        872        temp_carnumber = temp_resource[0]873        874#        temp_village = village_key[0]875        temp_village = temp_resource[4]876     877        temp_date = temp_resource[3]878        879        if temp_village not in unuse_resource_info.keys():880            unuse_resource_info[temp_village] = {}881            useed_resource_info[temp_village] = {}882        if  temp_date not in unuse_resource_info[temp_village].keys():883            unuse_resource_info[temp_village][temp_date] = [temp_carnumber]884            useed_resource_info[temp_village][temp_date] = {}885        else:886            unuse_resource_info[temp_village][temp_date].append(temp_carnumber)887        resource_info[temp_carnumber] = temp_resource      888#    print unuse_resource_info889#%% generate the initial route890    Day_request = copy.copy(Day_request_info)891    reserve_Plan = {}892    for village in village_key:893        Day_keys = sorted(Day_request[village].keys())894        reserve_Plan[village] = {}895        for day in Day_keys:896            reserve_Plan[village][day] = {}897            reser_Day_keys = sorted(Day_request[village][day].keys())898    #        for reser_Day in reser_Day_keys[:1]:899            for reser_Day in reser_Day_keys:900                unroute_Day_requests = copy.copy(Day_request[village][day][reser_Day])901#                print unroute_Day_requests902                reserve_Plan_key = reserve_Plan[village][day].keys()903                if reser_Day not in reserve_Plan_key:904                    unroute_Day_requests = route_seed_initialer(unroute_Day_requests,village,reser_Day)905#                    print "unroute_Day_requests",unroute_Day_requests906                    final_route = regret_insert(unroute_Day_requests,village,reser_Day)907#                    print final_route908#                    reserve_Plan[village][day][reser_Day] = final_route909                else:910                    final_route = regret_insert(unroute_Day_requests,village,reser_Day)911                reserve_Plan[village][day][reser_Day] = final_route912#%% show the initial result               913#    print "------initial result------"914#    for i in useed_resource_info[village][reser_Day]:915#        print useed_resource_info[village][reser_Day][i]                916#%% improvement the initial route            917    improvement = False918    if improvement == True:919#        print "+"920        for village in village_key:921            for reser_Day in useed_resource_info[village].keys(): 922                relocation_sign = True923                exchange_sign = True924                while True:925#                    926                ############## Trip relocation ##############                   927                    if relocation_sign == True:928#                        print "---relocation_operator---"   929                        old_plan = copy.copy(useed_resource_info[village][reser_Day])930                        new_plan = relocation_operator(useed_resource_info[village][reser_Day])931                        if old_plan != new_plan:932                            useed_resource_info[village][reser_Day] = new_plan933                        else:934                            relocation_sign = False   935#                        print "---relocation_operator---"                      936#                    937                ############## Trip exchange ##############                                       938                    if exchange_sign == True:939#                        print "---exchange_operator---"940                        old_plan = copy.copy(useed_resource_info[village][reser_Day])941                        new_plan = exchange_operator(useed_resource_info[village][reser_Day])942                        if old_plan != new_plan:943                            useed_resource_info[village][reser_Day] = new_plan944                        else:945                            exchange_sign = False  946#                        947                    if relocation_sign == False and exchange_sign ==  False:948                        break949#%% show the improved result                950    print "------improved result------"951    total_dist = 0952    for i in useed_resource_info[village][reser_Day]:953        print useed_resource_info[village][reser_Day][i],total_distant(useed_resource_info[village][reser_Day][i])954        total_dist += total_distant(useed_resource_info[village][reser_Day][i])955    print 'Total distance:', total_dist956#%% show the unservable result         957    print "------unservable result------"        958    print unservable_requests        959#%% waiting strategy & Fix TW      960    for village in village_key:961        for reser_Day in useed_resource_info[village].keys():  962            for route in useed_resource_info[village][reser_Day]:963#                    print useed_resource_info[village][reser_Day][route]964                fix_route = useed_resource_info[village][reser_Day][route]965#                if waiting_strategy == "drive_first":966#                    nodeservice_time =  check_service_time(fix_route,"list")     967#                elif waiting_strategy == "wait_first":968                nodeservice_time =  ride_counter(fix_route,"list")969                e_time_list = [Day_order[j][2] for j in fix_route]970                l_time_list = [Day_order[j][3] for j in fix_route] 971#                    print nodeservice_time972#                    print e_time_list973#                    print l_time_list974                for j,k,l,m in zip(e_time_list,nodeservice_time,l_time_list,fix_route):975                    if m != 0:976#                        print977#                        print m,Day_order[m][2],Day_order[m][3]978                        if round(j,2)+service_time/2.0 > round(k,2):979#                                print round(j,2),round(k,2),round(l,2),"up"980                            Day_order[m][3] = j + new_tw981                        elif round(l,2)-service_time/2.0 < round(k,2):982#                                print round(j,2),round(k,2),round(l,2),"down"983                            Day_order[m][2] = l - new_tw984                        else:985#                                print round(j,2),round(k,2),round(l,2),"+5-5"986                            Day_order[m][2] = k - new_tw/2                          987                            Day_order[m][3] = k + new_tw/2988#                        print m,Day_order[m][2],Day_order[m][3]989#%% calculate the trip & format the data 990#    result_start_time = time.time()991    total_num = 0992    for i in useed_resource_info[village][reser_Day]:993#        print useed_resource_info[village][reser_Day][i] 994        total_num += (len(useed_resource_info[village][reser_Day][i])-2)/2.0995#    print total_num                      996    result = []997    trip_number = 0998    trip_dist = dict.fromkeys(useed_resource_info.keys())999    trip_time = dict.fromkeys(useed_resource_info.keys())1000#    print useed_resource_info.keys()1001    for i in useed_resource_info.keys(): 1002        temp_trip_dist = dict.fromkeys(useed_resource_info[i].keys(),[])1003        temp_trip_time = dict.fromkeys(useed_resource_info[i].keys(),[])1004        for j in sorted(useed_resource_info[i].keys()):    1005            temp_trip_dist_list = []1006            temp_trip_time_list = []1007            for k in useed_resource_info[i][j]:1008                1009                test_route = useed_resource_info[i][j][k]        1010#                temp_store_list = [] 1011                1012#                customer_sign = 01013                cal_sign = 01014                cal_anchor = 11015                trip_list = []1016                trip_dict ={}1017#                print test_route1018                for cus in test_route :1019                    1020                    if type(cus) == int:1021                        cal_sign += cus1022                        if cal_sign == 0:1023            #                print "     ---",temp_store_list[cal_anchor:feasible_route.index(i)+1]1024                            cal_route = test_route[cal_anchor:test_route.index(cus)+1]1025#                            print "cal_route",cal_route1026                            trip_list.append(cal_route)1027                            cal_anchor = test_route.index(cus)+1 1028                run_sign = True1029                start_sign = 01030                while run_sign == True:1031                    if len(trip_list)>1:1032                        take_time = (Day_order[trip_list[start_sign][-1]][3]+Day_order[trip_list[start_sign][-1]][2])/21033                        arive_time = (Day_order[trip_list[start_sign+1][0]][2]+Day_order[trip_list[start_sign+1][0]][3])/2                        1034                        if arive_time - take_time > Max_trip_interval:1035                            start_sign += 11036                        else:1037                            test_trip = trip_list[start_sign] + trip_list[start_sign+1]1038                            if total_distant(test_trip) > Max_mile:1039                                start_sign += 11040                            else:1041                                trip_list[start_sign] = test_trip1042                                trip_list.remove(trip_list[start_sign+1])1043                        if start_sign == len(trip_list)-1:1044                            run_sign = False1045                    else:1046                        run_sign = False1047#                print "---"                        1048#                print trip_list      1049#                print "---"1050                p_new_trip = []1051                up_order = {}1052                down_order = {}   1053                for m in trip_list:1054                    1055                    temp_trip_dist_list.append(total_distant(m))1056                    temp_trip_time_list.append(total_time(m))1057                    temp_p_new_trip = []1058                    uporder = 01059                    downorder = 0                    1060                    for l in m:1061                        if l > 0:1062                            temp_p_new_trip.append(l)1063                            uporder += 1                            1064                            up_order[l] = uporder1065                        else:1066                            downorder += 1                            1067                            down_order[l] = downorder                            1068                    p_new_trip.append(temp_p_new_trip)1069#                print p_new_trip1070                for m in range(len(p_new_trip)):1071#                    print p_new_trip1072                    trip_number += 11073                    for l in range(len(p_new_trip[m])):1074#                        if p_new_trip[m][l] > 0:1075                        trip_dict[p_new_trip[m][l]] = trip_number1076                        sin_req = p_new_trip[m][l]1077                        UP_order = up_order[sin_req]1078                        DOWN_order = down_order[-sin_req]1079                        1080                        TAKE_TIME = str(int((Day_order[sin_req][2]+Day_order[sin_req][3])/2//60))+":"+str(int((Day_order[sin_req][2]+Day_order[sin_req][3])/2%60))1081                        AVR_TIME = str(int((Day_order[sin_req][6]+Day_order[sin_req][7])/2//60))+":"+str(int((Day_order[sin_req][6]+Day_order[sin_req][7])/2%60))1082                        TAKE_DATE = j[0:4]+"-"+j[5:7]+"-"+j[8:]1083                        TAKE_DATE = datetime.datetime.strptime(TAKE_DATE, "%Y-%m-%d")1084                        TAKE_TIME = datetime.datetime.strptime(TAKE_TIME, "%H:%M")1085                        AVR_TIME = datetime.datetime.strptime(AVR_TIME, "%H:%M")1086                        temp_result = [TAKE_DATE,i,sin_req,Day_order[sin_req][1],Day_order[sin_req][5],trip_number,"Vrp",UP_order,DOWN_order,resource_info[k][6],resource_info[k][5],TAKE_TIME,AVR_TIME]1087#                        print "temp_result",temp_result1088                        result.append(temp_result)    1089            temp_trip_dist[j] = temp_trip_dist_list1090            temp_trip_time[j] = temp_trip_time_list1091        trip_dist[i] = temp_trip_dist1092        trip_time[i] = temp_trip_time1093    return useed_resource_info,result1094#%% Data Input and Data Ouput1095useed_resource,result = main_funtion(sql_request,sql_cost,sql_resource)1096for i in range(len(result)):1097    cursor.execute("""INSERT INTO VRP_HAS_MATCHED (ENTERPRISEID,1098                                               TAKE_DATE, 1099                                                 GROUP_NAME, 1100                                                 PASSENGER_ORDER_ID, 1101                                                 START_PLACE, 1102                                                 END_PLACE, 1103                                                 SENDORDER_ID_VRP, 1104                                                 TYPE, 1105                                                 UP_ORDER,1106                                                 DOWN_ORDER,1107                                                 DRIVER_NAME, 1108                                                 CAR_NUMBER, 1109                                                 TAKE_TIME, 1110                                                 AVR_TIME)1111                                                 1112                                                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""","None",result[i][0],result[i][1],result[i][2],result[i][3],result[i][4],result[i][5],result[i][6],result[i][7],result[i][8],result[i][9],result[i][10],result[i][11],result[i][12])1113cursor.execute('SELECT * from VRP_HAS_MATCHED')1114VRP_HAS_MATCHED = datafit(cursor,"pandas") 1115cnxn.commit()1116=======1117# -*- coding: utf-8 -*-1118"""1119Created on Thu Nov 29 15:35:08 20181120@author: Kuan1121"""1122import math1123import copy1124import datetime1125import pandas as pd1126import pyodbc 1127import time1128#import pprint1129#%% sql sever setting1130#server = '' 1131#database = '' 1132#username = '' 1133#password = '' 1134#cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)1135server = '' 1136database = '' 1137username = '' 1138password = ''1139cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)1140#%% parameter setting1141Max_mile = 20 # upper limit for a ride1142service_time = 5 # service time when  arrive locstion 1143down_service_time = 11144time_window = 30 # aditional time windows1145constant_a = 31146constant_b = 51147Max_trip_interval = 15 #minutes1148new_tw = 51149Large_cost = 999991150#%% input three sql data sheet1151## a funtion to transfer the form of data1152## transfer into pandas or list 1153def datafit(data,form):1154    colList = []1155    for colInfo in data.description:1156        colList.append(colInfo[0])1157    rowList = []1158    while True:1159        try:1160            row = data.fetchone()1161            if row:1162                rowList.append(list(row))1163            else:1164                break1165        except:   1166            continue;1167    if form == "pandas":1168        sql_data = pd.DataFrame(rowList) 1169        sql_data.columns = colList 1170    elif form == "list":1171        sql_data = rowList1172    return sql_data1173## delete the data has been create and recalculate 1174cursor = cnxn.cursor()1175cursor.execute('delete from VRP_HAS_MATCHED') 1176cnxn.commit()1177 1178#print "---sql_resource---"1179cursor.execute("SELECT * from VRP_CAR_USEABLE ")1180sql_resource = datafit(cursor,"list") 1181#print ("---sql_cost---")1182cursor.execute("SELECT * from VRP_TRAVEL_MATRIX_DATA ")1183sql_cost = datafit(cursor,"list")1184#print ("---sql_request---")1185cursor.execute("SELECT * from VRP_PRE_ORDER")1186sql_request = datafit(cursor,"list")1187## if you want the data in a specific day1188#cursor.execute("SELECT * from VRP_CAR_USEABLE where take_date ='2019/01/14'")1189#cursor.execute("SELECT * from VRP_TRAVEL_MATRIX_DATA where take_date ='2019/01/14'")1190#cursor.execute("SELECT * from VRP_PRE_ORDER where take_date ='2019/01/14'")1191## if you want the data in a specific period1192#cursor.execute("SELECT * from VRP_CAR_USEABLE where take_date between '2019/01/14' and '2019/01/18'")1193#cursor.execute("SELECT * from VRP_TRAVEL_MATRIX_DATA where take_date between '2019/01/14' and '2019/01/18'")1194#cursor.execute("SELECT * from VRP_PRE_ORDER where take_date between '2019/01/14' and '2019/01/18'")1195#%% Main procedure 1196def main_funtion(request,cost,resource): 1197      1198    ## a funtion to transfer the time format1199    def time_form(intime):1200        str_intime = str(intime)1201        int_intime = int(str_intime[11])*10*60+int(str_intime[12])*60+int(str_intime[14])*10+int(str_intime[15])1202        return int_intime 1203    1204    ## calculate the route time1205    def total_time(route):1206        tt_time = 01207        for c in range(len(route)-1):1208            tt_time += t[Day_order[route[c]][1],Day_order[route[c+1]][1]]          1209        return tt_time1210    1211    ## calculate the route distant1212    def total_distant(route):1213        tt_dist = 01214        for c in range(len(route)-1):1215            tt_dist += d[Day_order[route[c]][1],Day_order[route[c+1]][1]]          1216        return tt_dist1217    1218    ## give the route a start customer1219    ## 1220    def route_seed_initialer(unroute_list,village,Day):1221        ## sort the unroute customers acordding to the pick up time1222        now_unroute_list = []1223        for i in unroute_list:1224            now_unroute_list.append(Day_order[i])1225        sort_now_unroute_list = sorted(now_unroute_list, key = lambda x : (x[2]))1226        sort_unroute_list = []1227        for i in sort_now_unroute_list:1228            sort_unroute_list.append(i[0]) 1229        ## sort the car drivers acordding to the starting service time    1230        all_car_key_info = []1231        for i in unuse_resource_info[village][Day]:1232            all_car_key_info.append(resource_info[i])1233        sort_all_car_key_info = sorted(all_car_key_info, key = lambda x : (x[-7]))1234        sort_all_car_key = []1235        for i in sort_all_car_key_info:1236            sort_all_car_key.append(i[0])  1237        ## give the route a start customer1238        ## return the unroute list without the customerhave been matched1239        new_unroute_list = copy.copy(sort_unroute_list)1240        for j in sort_unroute_list:1241            unservable_time = 01242            break_sign = 01243            for i in sort_all_car_key: #iå°±æ¯è»è1244                current_routeA = [i,j,-j,i]1245                if check_feasible_route(current_routeA,i) == 0: #輸å
¥è·¯å¾èè»è1246                    break_sign += 11247                    useed_resource_info[village][Day][i] = [i,j,-j,i]   1248                    new_unroute_list.remove(j)1249                    unuse_resource_info[village][Day].remove(i)1250                    break1251                else:1252                    unservable_time += 11253                    len(sort_all_car_key[sort_all_car_key.index(i):])1254            if unservable_time == len(sort_all_car_key):1255                if Day not in unservable_requests.keys():1256                    unservable_requests[Day] = []1257                    unservable_requests[Day].append(j)1258                else:1259                    unservable_requests[Day].append(j)1260                new_unroute_list.remove(j)1261            if break_sign>0:1262                break1263            #åªè¦æä¸å人å¯ä»¥æ¾é²ä¸å°è»å°±å¯ä»¥1264#        print "in ",useed_resource_info[village][Day]1265        return new_unroute_list    1266    1267    def route_seed_initialer2(unroute_list,village,Day):1268#        print unroute_list,village,Day1269        now_unroute_list = []1270        for i in unroute_list:1271            now_unroute_list.append(Day_order[i])1272        sort_now_unroute_list = sorted(now_unroute_list, key = lambda x : (x[2]))1273        sort_unroute_list = []1274        for i in sort_now_unroute_list:1275            sort_unroute_list.append(i[0])1276        seed_list = copy.copy(sort_unroute_list)1277        new_unroute_list = []1278        for i in range(len(sort_unroute_list)-1):1279            req_i = sort_unroute_list[i]1280            req_i_1 = sort_unroute_list[i+1]1281            LDTk = Day_order[req_i][7]1282            TTDkPk_1 = t[Day_order[req_i][5],Day_order[req_i_1][1]]1283            EPTk_1 = Day_order[req_i_1][2]1284            if LDTk+TTDkPk_1<=EPTk_1:1285                seed_list.remove(req_i_1)1286                new_unroute_list.append(req_i_1)1287        for i in unuse_resource_info[village][Day]:1288            unservable_time = 01289            for j in seed_list: 1290                current_routeA = [i,j,-j,i]1291                if check_feasible_route(current_routeA,i) == 0:1292                    useed_resource_info[village][Day][i] = [i,j,-j,i]1293                    seed_list.remove(j)1294                    break                  1295                else:1296                    unservable_time += 11297        for i in useed_resource_info[village][Day].keys():1298            if i in unuse_resource_info[village][Day]:1299                unuse_resource_info[village][Day].remove(i)1300#        print "in ",useed_resource_info[village][Day]1301        return new_unroute_list+seed_list  1302   1303    def check_feasible_route(feasible_route,car_number):1304#        print feasible_route,car_number1305        debug_time = Day_order[feasible_route[0]][2] ## first customer pick time 1306        max_car_capacity = resource_info[car_number][-1] ## car capacity1307  1308        route_feasible_or_not = True1309        ## check if the trip exceeds the max trip miles1310        if route_feasible_or_not == True:1311            temp_store_list = [] 1312            cal_sign = 01313            cal_anchor = 01314            1315            for i in feasible_route :1316                if type(i) ==  int:1317                    temp_store_list.append(i)1318                    if i not in temp_store_list or -i not in temp_store_list:        1319                        cal_sign += 11320                    elif i < 0 and -i in temp_store_list:1321                        cal_sign -= 11322                    if cal_sign == 0:1323                        cal_route = temp_store_list[cal_anchor:feasible_route.index(i)+1]1324#                        print cal_anchor,feasible_route.index(i)+11325                        o_dist = total_distant(cal_route)1326#                        for test_c in range(len(cal_route)-1):1327#                            o_dist += d[Day_order[cal_route[test_c]][1],Day_order[cal_route[test_c+1]][1]]         1328                        1329                        if o_dist >= Max_mile:1330                            route_feasible_or_not = False 1331#                        else:1332#                            print cal_route,car_number,total_distant(cal_route)1333                            1334                        cal_anchor = feasible_route.index(i)+1 1335        ## check if the capacity exceeds the car limit               1336        passenger_in_car = 0 1337        if route_feasible_or_not == True: 1338            for x in feasible_route:1339                if x > 0 :1340                    passenger_in_car += Day_order[x][4] 1341                elif x <0:1342                    passenger_in_car -= Day_order[x][4]1343                if passenger_in_car > max_car_capacity:1344                    route_feasible_or_not = False1345                    break1346                1347        ## check the route time window  1348        debug_route_time = []1349        debug_real_time = []    1350        if route_feasible_or_not == True: 1351            for i in range(len(feasible_route)):1352                pre_node = Day_order[feasible_route[i-1]][1]            1353                now_node = Day_order[feasible_route[i]][1]1354    1355                e_time = Day_order[feasible_route[i]][2] #earlyææ©ä¸è»1356                l_time = Day_order[feasible_route[i]][3] #latest ææä¸è»1357    1358                if i == 0:1359                    debug_route_time.append(debug_time)1360                    debug_real_time.append(debug_time)1361                    next_node = Day_order[feasible_route[i+1]][1]1362                    if now_node == next_node:1363                        continue1364                    else:1365                        if feasible_route[i] < 0:1366#                            print feasible_route[i-1]1367                            debug_time += down_service_time1368#                            debug_time += service_time 1369                        else:    1370                            debug_time += service_time                   1371                elif i == len(feasible_route)-1:    1372                    debug_time = debug_time+t[pre_node,now_node]1373                    debug_route_time.append(debug_time)1374                    debug_real_time.append(l_time)1375                elif i > 0:1376#                    print pre_node,now_node1377                    debug_time = max([debug_time+t[pre_node,now_node],e_time])1378                    debug_route_time.append(debug_time)1379                    debug_real_time.append(l_time)1380                    next_node = Day_order[feasible_route[i+1]][1]1381                    if now_node == next_node:1382                        continue1383                    else:1384                        if feasible_route[i] < 0:1385                            debug_time += down_service_time   1386                        else:    1387                            debug_time += service_time1388            for i,j in zip(debug_route_time,debug_real_time):1389                if  i > j:1390#                    print i,j1391#                    debug1 = 11392                    route_feasible_or_not = False                    1393        if route_feasible_or_not == True:  1394            debug_signal = 01395        else:1396            debug_signal = 11397#        if 14410 in feasible_route:1398#            print feasible_route,car_number,route_feasible_or_not1399           1400        return debug_signal1401    def regret_insert(unroute_list,village,Day):1402#        print "unroute_list",unroute_list,village,Day1403#        for fadfad in range(10):1404        while len(unroute_list) > 0:1405#            print "unroute_list",unroute_list1406            current_list = useed_resource_info[village][Day].values()1407#            print useed_resource_info1408#            print "current_list",current_list1409            car_keys = useed_resource_info[village][Day].keys()        1410            temp_route = copy.copy(current_list)1411    #        print "temp_route:",temp_route1412            c2_value = []1413            c2_route = []1414            c2_place = []1415            c2_customer = []1416#            print "----"1417            for customer in unroute_list:1418#                print customer1419                min_c1_route = []1420                min_c1_value = []1421                min_c1_place = []1422                customer_p = customer1423                customer_d = -customer_p1424                for o_route in temp_route: 1425                    car_number = car_keys[current_list.index(o_route)]1426                    o_route_tt_dist = 01427                    for test_c in range(len(o_route)-1):1428                        o_route_tt_dist += d[Day_order[o_route[test_c]][1],Day_order[o_route[test_c+1]][1]]                      1429                    c1_value = []1430                    c1_place = []1431                    for p_place in range(1,len(o_route)+1):         1432                        test_p_route =  copy.copy(o_route)1433                        test_p_route.insert(p_place,customer_p)1434#                        print1435#                        print "test_p_route-----------",test_p_route                        1436#                        feasibility1 = check_feasible_route(test_p_route,car_number)1437#                        print "-----------feasibility1",feasibility11438                        feasibility1 = 01439                        if feasibility1 == 0:1440                            for d_place in range(p_place+1,len(test_p_route)):1441                                #print d_place,"----"1442                                test_d_route = copy.copy(test_p_route)1443                                test_d_route.insert(d_place,customer_d)1444#                                print"--"                                1445                                feasibility2 = check_feasible_route(test_d_route,car_number)1446#                                if (316 in test_d_route and 318 in test_d_route) or (317 in test_d_route and 318 in test_d_route):1447#                                    print test_d_route,feasibility21448#                                    print "--"1449#                                print"--"1450                                if feasibility2 == 0:###############1451                                    ##################################################################1452#                                    tt_dist = total_distant(test_d_route)1453#                                    print test_d_route1454#                                    for test_c in range(len(test_d_route)-1):1455##                                        print test_d_route[test_c],test_d_route[test_c+1],d[Day_order[test_d_route[test_c]][1],Day_order[test_d_route[test_c+1]][1]]1456#                                        tt_dist += d[Day_order[test_d_route[test_c]][1],Day_order[test_d_route[test_c+1]][1]]1457#                                    c1 = tt_dist-o_route_tt_dist1458                                    c1 = total_distant(test_d_route)1459#                                    print test_d_route,c11460                                    ##################################################################1461                                    c1_value.append(c1)1462                                    c1_place.append([p_place,d_place])1463                                1464                    min_c1_route.append(o_route)  1465#                    print c1_value1466                    if len(c1_value) > 0:1467                        min_value = min(c1_value)1468                        min_place = c1_place[c1_value.index(min_value)]                           1469                        min_c1_value.append(min_value)1470                        min_c1_place.append(min_place)1471                    else:                1472                        min_c1_value.append(Large_cost)1473                        min_c1_place.append([-Large_cost,-Large_cost])     1474#                print "min_c1_value",min_c1_value1475                min_min_c1_value = min(min_c1_value)1476                min_c1_index = min_c1_value.index(min_min_c1_value)1477                min_min_c1_place = min_c1_place[min_c1_index]1478                min_min_c1_route = min_c1_route[min_c1_index]1479                c2_customer.append([customer,-customer])1480#                print c2_customer1481                c2 = 01482                if min_c1_value.count(Large_cost) != len(min_c1_value):1483#                    print min_c1_value1484                    for mmc in range(len(min_c1_value)):1485#                        print min_c1_route[mmc],min_min_c1_route1486#                        if min_c1_route[mmc] != min_min_c1_route:1487                            1488                        c2 += min_c1_value[mmc] - min_min_c1_value1489#                        print "------------",c21490                else:1491                    c2 = -Large_cost1492                c2_value.append(c2)1493                c2_route.append(min_min_c1_route)1494                c2_place.append(min_min_c1_place)  1495#                print c2_value,c2_route,c2_place1496#            print c2_value  1497            if c2_value.count(-Large_cost) != len(c2_value):1498#                print "1111111111111"1499                max_c2_value = max(c2_value)1500#                print max_c2_value1501                max_c2_index = c2_value.index(max_c2_value)1502                max_c2_route = c2_route[max_c2_index]1503                max_c2_place = c2_place[max_c2_index]1504                max_c2_customer = c2_customer[max_c2_index]1505                max_c2_index = current_list.index(max_c2_route)        1506                for insert_p,insert_c in zip(max_c2_place,max_c2_customer):1507                    max_c2_route.insert(insert_p,insert_c)1508                current_list[max_c2_index] = max_c2_route1509                unroute_list.remove(max_c2_customer[0])         1510            else:1511#                print "----",Day,unroute_list1512#                unservable_requests[Day] = unroute_list ##route21513#                break1514                unroute_list = route_seed_initialer(unroute_list,village,Day)1515                1516        return current_list1517    1518    def relocation_operator(original_plan):1519        all_reinsert_info = {}1520        for i in original_plan:1521            for j in original_plan[i]:1522                if j > 0 :1523                    all_reinsert_info[j] = i 1524        all_route = original_plan.keys()1525        1526        relocation_for_remove_time = 01527        relocation_for_insert_time = 01528        for relocate_time in range(1):1529            for i in all_reinsert_info:1530                1531                relocation_for_remove_start_time = time.time()1532                car_c = all_reinsert_info[i]1533                remoeve_route_c = original_plan[car_c][:]1534                if type(i) == int:1535                    remoeve_route_c.remove(i)1536                    remoeve_route_c.remove(-i)1537                    relocation_for_remove_end_time = time.time()1538                    relocation_for_remove_time += (relocation_for_remove_end_time-relocation_for_remove_start_time)1539                    for l in all_route:1540                        if l != car_c:1541                            for m in range(1,len(original_plan[l])+1):1542                                for n in range(m+1,len(original_plan[l])+1):1543                                    if i in original_plan[car_c]:  1544                                        relocation_for_insert_start_time = time.time()1545                                        insert__route_c = original_plan[l][:]1546                                        insert__route_c.insert(m,i)1547                                        insert__route_c.insert(n,-i)1548                                        relocation_for_insert_end_time = time.time()1549                                        relocation_for_insert_time += (relocation_for_insert_end_time-relocation_for_insert_start_time)1550        #                                print insert__route_c,check_feasible_route(insert__route_c,l)1551                                        if check_feasible_route(insert__route_c,l) == 0:1552        #                                    print insert__route_c,check_feasible_route(insert__route_c,l)1553                                            if total_distant(original_plan[car_c])+total_distant(original_plan[l])-total_distant(remoeve_route_c)-total_distant(insert__route_c) > 0:1554#                                                print total_distant(original_plan[car_c])+total_distant(original_plan[l])-total_distant(remoeve_route_c)-total_distant(insert__route_c) 1555                                                original_plan[car_c] = remoeve_route_c1556                                                original_plan[l] = insert__route_c1557                                                all_reinsert_info[i] = l   1558                                            if len(remoeve_route_c) == 0 or len(insert__route_c) == 0 :1559                                                print "need to cut car"                                            1560                                    else:1561                                        break 1562        return original_plan1563    def exchange_operator(original_plan):1564        all_reinsert_info = {}1565        for i in original_plan:1566#            print i,original_plan[i]1567            for j in original_plan[i]:1568#                print j1569                if j > 0 and type(j) == int:1570                    all_reinsert_info[j] = i1571        exchange_first_insert = 01572        exchange_second_insert = 01573    #    exchange_start_time = time.time()1574        for exchange_time in range(1):1575            for i in all_reinsert_info:1576                for j in all_reinsert_info:1577                    car_r = all_reinsert_info[i]1578                    car_s = all_reinsert_info[j]1579                    if i != j and car_r != car_s:1580            #            print 1581            #            print Day_order[i],Day_order[j]1582                        EPTi = Day_order[i][2]1583                        LPTi = Day_order[i][3]1584                        EDTi = Day_order[i][6]1585                        LDTi = Day_order[i][7]       1586                        EPTj = Day_order[j][2]1587                        LPTj = Day_order[j][3]1588                        EDTj = Day_order[j][6]1589                        LDTj = Day_order[j][7]           1590                        if (EPTi <= LPTj and EPTj <= LPTi) or (EDTi <= LDTj and EDTj <= LDTi):                    1591       1592                            remoeve_route_r = original_plan[car_r][:]1593                            remoeve_route_r.remove(i)1594                            remoeve_route_r.remove(-i)1595                            1596                            best_insert_route_r = []1597                            exchange_first_start_insert = time.time()1598                            for m in range(1,len(remoeve_route_r)+1):1599                                for n in range(m+1,len(remoeve_route_r)+1):1600    #                                print m,n1601                                    insert_route_r = remoeve_route_r[:]1602    #                                insert_route_r = copy.deepcopy(remoeve_route_r)1603                                    insert_route_r.insert(m,j)1604                                    insert_route_r.insert(n,-j)1605    #                                print insert_route_r,check_feasible_route(insert_route_r,car_r)1606                                    if check_feasible_route(insert_route_r,car_r) == 0:1607                                        if len(best_insert_route_r) == 0:1608                                            best_insert_route_r = insert_route_r1609                                        else:1610                                            if total_distant(insert_route_r) < total_distant(best_insert_route_r):1611                                                best_insert_route_r = insert_route_r1612            1613                            exchange_first_end_insert = time.time()            1614                            exchange_first_insert += (exchange_first_end_insert-exchange_first_start_insert)    1615                            if len(best_insert_route_r) > 0 :        1616                                remoeve_route_s = original_plan[car_s][:]1617                                remoeve_route_s.remove(j)1618                                remoeve_route_s.remove(-j) 1619                                exchange_second_start_insert = time.time()1620                                best_insert_route_s = []1621                                for m in range(1,len(remoeve_route_s)+1):1622                                    for n in range(m+1,len(remoeve_route_s)+1):1623                                        insert_route_s = remoeve_route_s[:]1624                                        insert_route_s.insert(m,i)1625                                        insert_route_s.insert(n,-i)                1626                                        if check_feasible_route(insert_route_s,car_s) == 0:1627                #                            print "-------------------"1628                                            if len(best_insert_route_s) == 0:1629                                                best_insert_route_s = insert_route_s1630                                            else:1631                                                if total_distant(insert_route_s) < total_distant(best_insert_route_s):1632                                                    best_insert_route_s = insert_route_s1633                                exchange_second_end_insert = time.time()1634                                exchange_second_insert += (exchange_second_end_insert-exchange_second_start_insert)1635                                if len(best_insert_route_s) > 0:1636                                    if (total_distant(original_plan[car_r])+total_distant(original_plan[car_s]))-(total_distant(best_insert_route_r)+total_distant(best_insert_route_s))>0:1637#                                        print (total_distant(original_plan[car_r])+total_distant(original_plan[car_s]))-(total_distant(best_insert_route_r)+total_distant(best_insert_route_s))1638                                        original_plan[car_r] = best_insert_route_r                 1639                                        original_plan[car_s] = best_insert_route_s1640                                        all_reinsert_info[i] = car_s1641                                        all_reinsert_info[j] = car_r  1642                                        if len(best_insert_route_r) == 0 or len(best_insert_route_s) == 0 :1643                                            print "need to cut car"1644        return original_plan   1645    def check_service_time(feasible_route,out_type = None):1646#        print feasible_route1647        debug_time = Day_order[feasible_route[0]][2]1648#        print debug_time1649        debug_service_time = []   1650        for i in range(len(feasible_route)):1651            pre_node = Day_order[feasible_route[i-1]][1]            1652            now_node = Day_order[feasible_route[i]][1]1653            e_time = Day_order[feasible_route[i]][2]1654#            l_time = Day_order[feasible_route[i]][3]1655            if i == 0:1656                debug_service_time.append(debug_time)1657            elif i == len(feasible_route)-1:        1658                debug_time = debug_time+t[pre_node,now_node]1659                debug_service_time.append(debug_time)1660            else:1661                debug_time = max([debug_time+t[pre_node,now_node],e_time])1662                debug_service_time.append(debug_time)1663                if now_node > 0:1664                    debug_time += service_time1665#                print "afterser:",debug_time1666        ri_time = 01667        test_n = 0                    1668        for i in range(len(feasible_route)):1669    #        print i,test[i],test[i+1],t[Day_order[test[i]][1],Day_order[test[i+1]][1]]1670            if type(feasible_route[i]) == int:1671                test_n += feasible_route[i]1672                if feasible_route[i] > 0:1673                    test_target = feasible_route.index(-feasible_route[i])1674                    1675                    ride_time =  debug_service_time[test_target] - debug_service_time[i]   1676                    ri_time += ride_time1677        if out_type == "list":1678            return debug_service_time1679        else:1680            return ri_time         1681    def check_arrive_time(feasible_route,out_type = None):1682#        print feasible_route1683        debug_time = Day_order[feasible_route[0]][2]1684#        print debug_time1685        debug_arrive_time = []   1686        for i in range(len(feasible_route)):1687            pre_node = Day_order[feasible_route[i-1]][1]            1688            now_node = Day_order[feasible_route[i]][1]1689            e_time = Day_order[feasible_route[i]][2]1690#            l_time = Day_order[feasible_route[i]][3]1691            if i == 0:1692                debug_arrive_time.append(debug_time)1693            elif i == len(feasible_route)-1:        1694                debug_time = debug_time+t[pre_node,now_node]1695                debug_arrive_time.append(debug_time)1696            else:1697                debug_arrive_time.append(debug_time+t[pre_node,now_node])1698                debug_time = max([debug_time+t[pre_node,now_node],e_time])1699                1700                if now_node > 0:1701                    debug_time += service_time1702#                print "afterser:",debug_time1703        ri_time = 01704        test_n = 0  1705                          1706        for i in range(len(feasible_route)):1707    #        print i,test[i],test[i+1],t[Day_order[test[i]][1],Day_order[test[i+1]][1]]1708            if type(feasible_route[i]) == int:1709                test_n += feasible_route[i]1710                if feasible_route[i] > 0:1711                    test_target = feasible_route.index(-feasible_route[i])1712                    ride_time =  debug_arrive_time[test_target] - debug_arrive_time[i]   1713                    ri_time += ride_time1714#        print debug_arrive_time                1715        if out_type == "list":1716            return debug_arrive_time1717        else:1718            return ri_time                     1719    def ride_counter(feasible_route,out_type = None):1720#        print "    ",feasible_route1721#        print "debug_time",debug_time1722        node_service_time = check_service_time(feasible_route,"list")1723        node_arrive_time = check_arrive_time(feasible_route,"list")1724        ##########################1725        test = feasible_route1726        1727        e_time_list = [Day_order[i][2] for i in feasible_route]1728        l_time_list = [Day_order[i][3] for i in feasible_route]1729#        print len(l_time_list)1730        cuttime_list = []1731        for i,j in zip(node_service_time,node_arrive_time):1732            if j < i:1733                cuttime_list.append(i-j)1734            else:1735                cuttime_list.append(0)1736#        print cuttime_list                1737#        print len(cuttime_list)1738        ri_time = 01739#        route_arrive_time = check_service_time(test)1740        test_n = 01741        test_ancher = 01742        if test[0] == 0:1743            for i in range(len(test)-1):1744                test_n += test[i]1745                if test_n == 0:1746                    if test[i] != 0:1747                        for j in range(test_ancher,i+1):1748                            1749                            if cuttime_list[j] > 0:1750                                if j == test_ancher:1751                                    node_arrive_time[test_ancher] = e_time_list[test_ancher] 1752                                else:1753                                    1754                                    change_new_timesign = 01755#                                    print "1   ",change_new_timesign1756                                    for l in range(test_ancher,j):1757                                        new_service_time = node_service_time[l] + cuttime_list[j]1758                                        1759                                        if new_service_time >= e_time_list[l] and new_service_time <= l_time_list[l]:1760                                            continue1761                                        else:1762                                            change_new_timesign += 11763#                                    print "2   ",change_new_timesign1764                                            1765                                    if change_new_timesign == 0:1766#                                        print test_ancher,j1767                                        for l in range(test_ancher,j):1768                                            node_service_time[l] = node_service_time[l] + cuttime_list[j]   1769                                            node_arrive_time[l] = node_arrive_time[l] + cuttime_list[j]   1770                                            1771                                        node_arrive_time[j] = e_time_list[j] 1772                                    else:1773#                                        print "NOOOO",test[j],cuttime_list[j]   1774                                        left_time_list = [l_time_list[l]-node_arrive_time[l]for l in range(test_ancher,j)]1775                                     1776                                        min_left_time = min(left_time_list)1777                                        for l in range(test_ancher,j):1778#                                            print test[l],left_time_list[l-test_ancher]1779                                            node_service_time[l] = node_service_time[l] + min_left_time1780                                            node_arrive_time[l] = node_arrive_time[l] + min_left_time1781                                        node_arrive_time[j] = node_arrive_time[j] + min_left_time                                               1782                        1783                    test_ancher = i+11784#        print "    ",node_service_time            1785#        print "    ",node_arrive_time 1786        cuttime_list = []1787        for i,j in zip(node_service_time,node_arrive_time):1788            if j < i:1789                cuttime_list.append(i-j)1790            else:1791                cuttime_list.append(0)    1792#        print cuttime_list                1793        for i in range(len(test)):1794    #        print i,test[i],test[i+1],t[Day_order[test[i]][1],Day_order[test[i+1]][1]]1795            if type(test[i]) == int:1796                test_n += test[i]1797                if test[i] > 0:1798                    test_target = test.index(-test[i])1799    #                print test[i],-test[i]1800                    ride_time =  node_service_time[test_target] - node_service_time[i]   1801                    ri_time += ride_time1802#        print node_service_time1803        if out_type == "list":1804            return node_service_time1805        elif out_type == "arrive":1806            return node_arrive_time1807        else:1808            return ri_time               1809#%% sql cost1810    ## make the time & distant cost matrix1811    cost_info = {}1812    dist_info = {}   1813    for temp_cost in cost:1814        cost_info[temp_cost[2],temp_cost[3]] = temp_cost[4]1815        cost_info[temp_cost[3],temp_cost[2]] = temp_cost[4]1816        cost_info[temp_cost[2],temp_cost[2]] = 01817        cost_info[temp_cost[3],temp_cost[3]] = 01818        dist_info[temp_cost[2],temp_cost[3]] = temp_cost[5]1819        dist_info[temp_cost[3],temp_cost[2]] = temp_cost[5]1820        dist_info[temp_cost[2],temp_cost[2]] = 01821        dist_info[temp_cost[3],temp_cost[3]] = 01822    ## time cost matrix1823    t = copy.deepcopy(cost_info)1824    ## distant cost matrix1825    d = copy.deepcopy(dist_info)1826#%% sql request1827    1828    request_info = {} 1829    unservable_requests = {}1830    village_key = [request[0][1]]1831    ## the dictionary includes every request in a day 1832    Day_request_info = {}   1833    Day_request_info[village_key[0]] = {}1834    ## the dictionary of the information of every request 1835    ## Day_order = {request_number:[request number,pick place, EPT, LPT, passenger number, deliver place, EDT, LDT]}1836    ## Day_order[request_number] = [request number,pick place, EPT, LPT, passenger number, deliver place, EDT, LDT]1837    Day_order = {}1838    1839    ## frame the time window1840    for i in request:1841        SENDORDER = i[3]1842        ## if there are no pick windows        1843#        if i[8] == None or i[9] == None:1844        if True:1845            Pp,Dp,n_C= i[5],i[6],i[7]1846            1847            LT = (time_form(i[10])+time_form(i[11]))/2.01848            1849            EPT = LT - constant_a*t[Pp,Dp] - constant_b  1850            EDT = LT -            t[Pp,Dp] - constant_b         1851            LPT = time_form(i[10])1852            LDT = time_form(i[11])1853            1854            Day_order[SENDORDER] = [SENDORDER]+[Pp,EPT,EDT,n_C,Dp,LPT,LDT]1855            Day_order[-SENDORDER] = [SENDORDER]+[Dp,LPT,LDT,n_C]            1856            temp_one = [str(i[0]),i[1],i[4],i[5],i[6],i[7],EPT,EDT,LPT,LDT]1857#        ## if there are no deliver windows 1858#        elif i[10] == None or i[11] == None:1859#            1860#            Pp,Dp,n_C= i[5],i[6],i[7]1861#1862#            EPT = time_form(i[8])1863#            EDT = time_form(i[9])1864#            LPT = EPT +            t[Pp,Dp] + constant_b1865#            LDT = EPT + constant_a*t[Pp,Dp] + constant_b1866#1867#            Day_order[SENDORDER] = [SENDORDER]+[Pp,EPT,EDT,n_C,Dp,LPT,LDT]1868#            Day_order[-SENDORDER] = [SENDORDER]+[Dp,LPT,LDT,n_C]            1869#            temp_one = [str(i[0]),i[1],i[4],i[5],i[6],i[7],EPT,EDT,LPT,LDT]   1870#        ## if both windows exist 1871#        else:1872#            Pp,Dp,n_C= i[5],i[6],i[7]1873# 1874#            EPT = time_form(i[8])1875#            EDT = time_form(i[9])          1876#            LPT = time_form(i[10])1877#            LDT = time_form(i[11]) 1878#            1879#            Day_order[SENDORDER] = [SENDORDER]+[Pp,EPT,EDT,n_C,Dp,LPT,LDT]1880#            Day_order[-SENDORDER] = [SENDORDER]+[Dp,LPT,LDT,n_C]             1881#            temp_one = [str(i[0]),i[1],i[4],i[5],i[6],i[7],EPT,EDT,LPT,LDT]1882        SENDORDER_date = "19000000"1883        SENDORDER_date = str(SENDORDER_date[:4])+"/"+str(SENDORDER_date[4:6])+"/"+str(SENDORDER_date[6:])1884        SENDORDER_ID = str(SENDORDER)1885    1886        for j in SENDORDER_ID:1887            if int(j) !=0:1888                SENDORDER_ID = int(SENDORDER_ID[SENDORDER_ID.index(j):])1889                break1890        ## build the day request dictionary 1891        ## let us know which requests should be matched in the specific day1892        request_info[SENDORDER_ID] = [SENDORDER_date]+temp_one[0:2]+[SENDORDER_ID]+temp_one[2:10]1893        request_info_now = request_info[SENDORDER_ID]1894#        if request_info_now[2] not in village_key:1895#            village_key.append(request_info_now[2])1896        1897        if request_info_now[0] not in Day_request_info[village_key[0]].keys():1898            Day_request_info[village_key[0]][request_info_now[0]] = {}1899            Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]] = [request_info_now[3]]1900        else:1901            if request_info_now[1] not in Day_request_info[request_info_now[2]][request_info_now[0]].keys():1902                Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]] = []1903                Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]] = [request_info_now[3]]1904            else:1905                Day_request_info[village_key[0]][request_info_now[0]][request_info_now[1]].append(request_info_now[3])1906#        print Day_request_info1907#%% sql resource              1908    resource_info = {}1909    1910    ## the dictionary of the car did not be used 1911    unuse_resource_info = {}1912    ## the dictionary of the car have been used 1913    useed_resource_info = {}1914    1915    for i in resource:1916        car_code = i[4]+'-'+str(resource.index(i)+1)1917        start_time = time_form(i[6])1918        close_time = time_form(i[7])   1919        temp_resource = [car_code,i[0],i[1],i[2].strftime('%Y/%m/%d'),i[3],i[4],i[5],time_form(i[6]),time_form(i[7]),i[8]]1920        ## pretend the driver's location as a day order 1921        ## but it does not include in day request1922        Day_order[car_code] = [car_code,i[9],start_time,close_time,0]1923#        temp_carnumber = temp_resource[4]1924#        1925#        temp_village = temp_resource[3]1926#        1927#        temp_date = temp_resource[0]1928        1929        temp_carnumber = temp_resource[0]1930        1931#        temp_village = village_key[0]1932        temp_village = temp_resource[4]1933     1934        temp_date = temp_resource[3]1935        1936        if temp_village not in unuse_resource_info.keys():1937            unuse_resource_info[temp_village] = {}1938            useed_resource_info[temp_village] = {}1939        if  temp_date not in unuse_resource_info[temp_village].keys():1940            unuse_resource_info[temp_village][temp_date] = [temp_carnumber]1941            useed_resource_info[temp_village][temp_date] = {}1942        else:1943            unuse_resource_info[temp_village][temp_date].append(temp_carnumber)1944        resource_info[temp_carnumber] = temp_resource      1945#    print unuse_resource_info1946#%% generate the initial route1947    Day_request = copy.copy(Day_request_info)1948    reserve_Plan = {}1949    for village in village_key:1950        Day_keys = sorted(Day_request[village].keys())1951        reserve_Plan[village] = {}1952        for day in Day_keys:1953            reserve_Plan[village][day] = {}1954            reser_Day_keys = sorted(Day_request[village][day].keys())1955    #        for reser_Day in reser_Day_keys[:1]:1956            for reser_Day in reser_Day_keys:1957                unroute_Day_requests = copy.copy(Day_request[village][day][reser_Day])1958#                print unroute_Day_requests1959                reserve_Plan_key = reserve_Plan[village][day].keys()1960                if reser_Day not in reserve_Plan_key:1961                    unroute_Day_requests = route_seed_initialer(unroute_Day_requests,village,reser_Day)1962#                    print "unroute_Day_requests",unroute_Day_requests1963                    final_route = regret_insert(unroute_Day_requests,village,reser_Day)1964#                    print final_route1965#                    reserve_Plan[village][day][reser_Day] = final_route1966                else:1967                    final_route = regret_insert(unroute_Day_requests,village,reser_Day)1968                reserve_Plan[village][day][reser_Day] = final_route1969#%% show the initial result               1970#    print "------initial result------"1971#    for i in useed_resource_info[village][reser_Day]:1972#        print useed_resource_info[village][reser_Day][i]                1973#%% improvement the initial route            1974    improvement = False1975    if improvement == True:1976#        print "+"1977        for village in village_key:1978            for reser_Day in useed_resource_info[village].keys(): 1979                relocation_sign = True1980                exchange_sign = True1981                while True:1982#                    1983                ############## Trip relocation ##############                   1984                    if relocation_sign == True:1985#                        print "---relocation_operator---"   1986                        old_plan = copy.copy(useed_resource_info[village][reser_Day])1987                        new_plan = relocation_operator(useed_resource_info[village][reser_Day])1988                        if old_plan != new_plan:1989                            useed_resource_info[village][reser_Day] = new_plan1990                        else:1991                            relocation_sign = False   1992#                        print "---relocation_operator---"                      1993#                    1994                ############## Trip exchange ##############                                       1995                    if exchange_sign == True:1996#                        print "---exchange_operator---"1997                        old_plan = copy.copy(useed_resource_info[village][reser_Day])1998                        new_plan = exchange_operator(useed_resource_info[village][reser_Day])1999                        if old_plan != new_plan:2000                            useed_resource_info[village][reser_Day] = new_plan2001                        else:2002                            exchange_sign = False  2003#                        2004                    if relocation_sign == False and exchange_sign ==  False:2005                        break2006#%% show the improved result                2007    print "------improved result------"2008    total_dist = 02009    for i in useed_resource_info[village][reser_Day]:2010        print useed_resource_info[village][reser_Day][i],total_distant(useed_resource_info[village][reser_Day][i])2011        total_dist += total_distant(useed_resource_info[village][reser_Day][i])2012    print 'Total distance:', total_dist2013#%% show the unservable result         2014    print "------unservable result------"        2015    print unservable_requests        2016#%% waiting strategy & Fix TW      2017    for village in village_key:2018        for reser_Day in useed_resource_info[village].keys():  2019            for route in useed_resource_info[village][reser_Day]:2020#                    print useed_resource_info[village][reser_Day][route]2021                fix_route = useed_resource_info[village][reser_Day][route]2022#                if waiting_strategy == "drive_first":2023#                    nodeservice_time =  check_service_time(fix_route,"list")     2024#                elif waiting_strategy == "wait_first":2025                nodeservice_time =  ride_counter(fix_route,"list")2026                e_time_list = [Day_order[j][2] for j in fix_route]2027                l_time_list = [Day_order[j][3] for j in fix_route] 2028#                    print nodeservice_time2029#                    print e_time_list2030#                    print l_time_list2031                for j,k,l,m in zip(e_time_list,nodeservice_time,l_time_list,fix_route):2032                    if m != 0:2033#                        print2034#                        print m,Day_order[m][2],Day_order[m][3]2035                        if round(j,2)+service_time/2.0 > round(k,2):2036#                                print round(j,2),round(k,2),round(l,2),"up"2037                            Day_order[m][3] = j + new_tw2038                        elif round(l,2)-service_time/2.0 < round(k,2):2039#                                print round(j,2),round(k,2),round(l,2),"down"2040                            Day_order[m][2] = l - new_tw2041                        else:2042#                                print round(j,2),round(k,2),round(l,2),"+5-5"2043                            Day_order[m][2] = k - new_tw/2                          2044                            Day_order[m][3] = k + new_tw/22045#                        print m,Day_order[m][2],Day_order[m][3]2046#%% calculate the trip & format the data 2047#    result_start_time = time.time()2048    total_num = 02049    for i in useed_resource_info[village][reser_Day]:2050#        print useed_resource_info[village][reser_Day][i] 2051        total_num += (len(useed_resource_info[village][reser_Day][i])-2)/2.02052#    print total_num                      2053    result = []2054    trip_number = 02055    trip_dist = dict.fromkeys(useed_resource_info.keys())2056    trip_time = dict.fromkeys(useed_resource_info.keys())2057#    print useed_resource_info.keys()2058    for i in useed_resource_info.keys(): 2059        temp_trip_dist = dict.fromkeys(useed_resource_info[i].keys(),[])2060        temp_trip_time = dict.fromkeys(useed_resource_info[i].keys(),[])2061        for j in sorted(useed_resource_info[i].keys()):    2062            temp_trip_dist_list = []2063            temp_trip_time_list = []2064            for k in useed_resource_info[i][j]:2065                2066                test_route = useed_resource_info[i][j][k]        2067#                temp_store_list = [] 2068                2069#                customer_sign = 02070                cal_sign = 02071                cal_anchor = 12072                trip_list = []2073                trip_dict ={}2074#                print test_route2075                for cus in test_route :2076                    2077                    if type(cus) == int:2078                        cal_sign += cus2079                        if cal_sign == 0:2080            #                print "     ---",temp_store_list[cal_anchor:feasible_route.index(i)+1]2081                            cal_route = test_route[cal_anchor:test_route.index(cus)+1]2082#                            print "cal_route",cal_route2083                            trip_list.append(cal_route)2084                            cal_anchor = test_route.index(cus)+1 2085                run_sign = True2086                start_sign = 02087                while run_sign == True:2088                    if len(trip_list)>1:2089                        take_time = (Day_order[trip_list[start_sign][-1]][3]+Day_order[trip_list[start_sign][-1]][2])/22090                        arive_time = (Day_order[trip_list[start_sign+1][0]][2]+Day_order[trip_list[start_sign+1][0]][3])/2                        2091                        if arive_time - take_time > Max_trip_interval:2092                            start_sign += 12093                        else:2094                            test_trip = trip_list[start_sign] + trip_list[start_sign+1]2095                            if total_distant(test_trip) > Max_mile:2096                                start_sign += 12097                            else:2098                                trip_list[start_sign] = test_trip2099                                trip_list.remove(trip_list[start_sign+1])2100                        if start_sign == len(trip_list)-1:2101                            run_sign = False2102                    else:2103                        run_sign = False2104#                print "---"                        2105#                print trip_list      2106#                print "---"2107                p_new_trip = []2108                up_order = {}2109                down_order = {}   2110                for m in trip_list:2111                    2112                    temp_trip_dist_list.append(total_distant(m))2113                    temp_trip_time_list.append(total_time(m))2114                    temp_p_new_trip = []2115                    uporder = 02116                    downorder = 0                    2117                    for l in m:2118                        if l > 0:2119                            temp_p_new_trip.append(l)2120                            uporder += 1                            2121                            up_order[l] = uporder2122                        else:2123                            downorder += 1                            2124                            down_order[l] = downorder                            2125                    p_new_trip.append(temp_p_new_trip)2126#                print p_new_trip2127                for m in range(len(p_new_trip)):2128#                    print p_new_trip2129                    trip_number += 12130                    for l in range(len(p_new_trip[m])):2131#                        if p_new_trip[m][l] > 0:2132                        trip_dict[p_new_trip[m][l]] = trip_number2133                        sin_req = p_new_trip[m][l]2134                        UP_order = up_order[sin_req]2135                        DOWN_order = down_order[-sin_req]2136                        2137                        TAKE_TIME = str(int((Day_order[sin_req][2]+Day_order[sin_req][3])/2//60))+":"+str(int((Day_order[sin_req][2]+Day_order[sin_req][3])/2%60))2138                        AVR_TIME = str(int((Day_order[sin_req][6]+Day_order[sin_req][7])/2//60))+":"+str(int((Day_order[sin_req][6]+Day_order[sin_req][7])/2%60))2139                        TAKE_DATE = j[0:4]+"-"+j[5:7]+"-"+j[8:]2140                        TAKE_DATE = datetime.datetime.strptime(TAKE_DATE, "%Y-%m-%d")2141                        TAKE_TIME = datetime.datetime.strptime(TAKE_TIME, "%H:%M")2142                        AVR_TIME = datetime.datetime.strptime(AVR_TIME, "%H:%M")2143                        temp_result = [TAKE_DATE,i,sin_req,Day_order[sin_req][1],Day_order[sin_req][5],trip_number,"Vrp",UP_order,DOWN_order,resource_info[k][6],resource_info[k][5],TAKE_TIME,AVR_TIME]2144#                        print "temp_result",temp_result2145                        result.append(temp_result)    2146            temp_trip_dist[j] = temp_trip_dist_list2147            temp_trip_time[j] = temp_trip_time_list2148        trip_dist[i] = temp_trip_dist2149        trip_time[i] = temp_trip_time2150    return useed_resource_info,result2151#%% Data Input and Data Ouput2152useed_resource,result = main_funtion(sql_request,sql_cost,sql_resource)2153for i in range(len(result)):2154    cursor.execute("""INSERT INTO VRP_HAS_MATCHED (ENTERPRISEID,2155                                               TAKE_DATE, 2156                                                 GROUP_NAME, 2157                                                 PASSENGER_ORDER_ID, 2158                                                 START_PLACE, 2159                                                 END_PLACE, 2160                                                 SENDORDER_ID_VRP, 2161                                                 TYPE, 2162                                                 UP_ORDER,2163                                                 DOWN_ORDER,2164                                                 DRIVER_NAME, 2165                                                 CAR_NUMBER, 2166                                                 TAKE_TIME, 2167                                                 AVR_TIME)2168                                                 2169                                                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""","None",result[i][0],result[i][1],result[i][2],result[i][3],result[i][4],result[i][5],result[i][6],result[i][7],result[i][8],result[i][9],result[i][10],result[i][11],result[i][12])2170cursor.execute('SELECT * from VRP_HAS_MATCHED')2171VRP_HAS_MATCHED = datafit(cursor,"pandas") 2172cnxn.commit()...pdbgen.py
Source:pdbgen.py  
1#!/usr/bin/python32from numpy import *3import numpy as np4import os5import sys6import os.path7from athena import ATHENA_DIR8_na_lib_dir = os.path.join( ATHENA_DIR, 'tools', 'na_library')9"""10PDBGen Version 1.511------------------12Contents13--------14  I. cndo_to_dnainfo15  II. Reference DNA classes (B-DNA)16  III. writePDBresidue17  IV. Matrix transformation functions18    1. getTransMat19    2. applyTransMat20    3. translate21    4. eultoaxisangle22    5. axisangletoeul23  V. Large number encoding functions24    1. base36encode25    2. hybrid36encode26  VI. Main pdbgen function27--------28  29"""30# I. cndo_to_dnainfo31# This function converts a .cndo data structure to a useable input32# for the main pdbgen function33def cndo_to_dnainfo(filename, inputdir):34    f = open(inputdir + filename + '.cndo', 'r')35    # Initialize variables to list36    dnaTop = []37    dNode = []38    triad = []39    id_nt = []40    # Read in the data structure41    for line in f:42        if 'dnaTop' in line:43            line = next(f)44            while line.strip() != '':45                linestr = line.strip()46                dnaTop.append(linestr.split(','))47                line = next(f)48        elif 'dNode' in line:49            line = next(f)50            while line.strip() != '':51                linestr = line.strip()52                dNode.append(linestr.split(','))53                line = next(f)54        elif 'triad' in line:55            line = next(f)56            while line.strip() != '':57                linestr = line.strip()58                triad.append(linestr.split(','))59                line = next(f)60        elif 'id_nt' in line:61            line = next(f)62            while line.strip() != '':63                linestr = line.strip()64                id_nt.append(linestr.split(','))65                # End of file requires this try-except break66                try:67                    line = next(f)68                except:69                    break70        else:71            pass72    return dnaTop, dNode, triad, id_nt73# II. Reference DNA Structures74# Class for reference B-DNA structure 75class BDNA(object):76    def __init__(self):77        """78        This class will parse the reference B-DNA files and return a 79        data structure which can be used during rotation of coordinate80        frames.81        Parameters82        ----------83        none84        Returns85        -------86        bdna:87            Structure that describes the PDB geometry of a reference 88            B-DNA assembly using the 3DNA convention.89            Substructures:90            AAA.scaf = adenine scaffold information91            AAA.stap = adenine staple information92            CCC.scaf = cytosine scaffold information93            CCC.stap = cytosine staple information94            GGG.scaf = guanine scaffold information95            GGG.stap = guanine staple information96            TTT.scaf = thymine scaffold information97            TTT.stap = thymine staple information98        Will load the reference files AAA.pdb, CCC.pdb, GGG.pdb, and TTT.pdb99        sequentially to acquire the necessary structural information.100        """101        # Run the basepairs102        self.AAA()103        self.CCC()104        self.GGG()105        self.TTT()106    def AAA(self):107        AAAtemp = np.loadtxt( os.path.join(_na_lib_dir, 'bdna_ath.pdb'), dtype=object)108        AAAlen = len(AAAtemp)109        # Calculate atoms in scaffold and staple strands110        scafatoms = 0111        stapatoms = 0112        for i in range(AAAlen):113            if AAAtemp[i,3] == 'ADE':114                scafatoms += 1115            elif AAAtemp[i,3] == 'THY':116                stapatoms += 1117        # Now transfer important PDB structural information to the refdna.bdna118        # object.119        # Initialize scaffold and staple structures120        self.Ascaf = np.zeros((scafatoms, 6), dtype=object)121        self.Tstap = np.zeros((stapatoms, 6), dtype=object)122        # Now transfer PDB structure line by line123        ss = 0124        aa = 0125        for i in range(AAAlen):126            # {atomtype, strand, residue, xcoord, ycoord, zcoord, atom}127            if AAAtemp[i,3] == 'ADE':128                self.Ascaf[ss,0] = AAAtemp[i,2]129                self.Ascaf[ss,1] = AAAtemp[i,4] 130                self.Ascaf[ss,2] = AAAtemp[i,5] 131                self.Ascaf[ss,3] = float(AAAtemp[i,6])132                self.Ascaf[ss,4] = float(AAAtemp[i,7])133                self.Ascaf[ss,5] = float(AAAtemp[i,8])134                ss += 1135            elif AAAtemp[i,3] == 'THY':136                self.Tstap[aa,0] = AAAtemp[i,2]137                self.Tstap[aa,1] = AAAtemp[i,4]138                self.Tstap[aa,2] = AAAtemp[i,5]139                self.Tstap[aa,3] = float(AAAtemp[i,6])140                self.Tstap[aa,4] = float(AAAtemp[i,7])141                self.Tstap[aa,5] = float(AAAtemp[i,8])142                aa += 1143        return self.Ascaf, self.Tstap144    145    def CCC(self):146        CCCtemp = np.loadtxt(os.path.join(_na_lib_dir, 'bdna_cgh.pdb'), dtype=object)147        CCClen = len(CCCtemp)148        # Calculate atoms in scaffold and staple strands149        scafatoms = 0150        stapatoms = 0151        for i in range(CCClen):152            if CCCtemp[i,3] == 'CYT':153                scafatoms += 1154            elif CCCtemp[i,3] == 'GUA':155                stapatoms += 1156        # Initialize scaffold and staple structures157        self.Cscaf = np.zeros((scafatoms, 6), dtype=object)158        self.Gstap = np.zeros((stapatoms, 6), dtype=object)159        # Now transfer PDB structure line by line160        ss = 0161        aa = 0162        # Now transfer important PDB structural information to the refdna.bdna163        # object.164        # Now transfer PDB structure line by line165        for i in range(CCClen):166            # {atomtype, strand, residue, xcoord, ycoord, zcoord, atom}167            if CCCtemp[i,3] == 'CYT':168                self.Cscaf[ss,0] = CCCtemp[i,2]169                self.Cscaf[ss,1] = CCCtemp[i,4]170                self.Cscaf[ss,2] = CCCtemp[i,5]171                self.Cscaf[ss,3] = float(CCCtemp[i,6])172                self.Cscaf[ss,4] = float(CCCtemp[i,7])173                self.Cscaf[ss,5] = float(CCCtemp[i,8])174                ss += 1175            elif CCCtemp[i,3] == 'GUA':176                self.Gstap[aa,0] = CCCtemp[i,2]177                self.Gstap[aa,1] = CCCtemp[i,4]178                self.Gstap[aa,2] = CCCtemp[i,5]179                self.Gstap[aa,3] = float(CCCtemp[i,6])180                self.Gstap[aa,4] = float(CCCtemp[i,7])181                self.Gstap[aa,5] = float(CCCtemp[i,8])182                aa += 1183        return self.Cscaf, self.Gstap184    def GGG(self):185        GGGtemp = np.loadtxt(os.path.join(_na_lib_dir, 'bdna_gch.pdb'), dtype=object)186        GGGlen = len(GGGtemp)187        # Calculate atoms in scaffold and staple strands188        scafatoms = 0189        stapatoms = 0190        for i in range(GGGlen):191            if GGGtemp[i,3] == 'GUA':192                scafatoms += 1193            elif GGGtemp[i,3] == 'CYT':194                stapatoms += 1195        # Initialize scaffold and staple structures196        self.Gscaf = np.zeros((scafatoms, 6), dtype=object)197        self.Cstap = np.zeros((stapatoms, 6), dtype=object)198        # Now transfer PDB structure line by line199        ss = 0200        aa = 0201        for i in range(GGGlen):202            # {atomtype, strand, residue, xcoord, ycoord, zcoord, atom}203            if GGGtemp[i,3] == 'GUA':204                self.Gscaf[ss,0] = GGGtemp[i,2]205                self.Gscaf[ss,1] = GGGtemp[i,4]206                self.Gscaf[ss,2] = GGGtemp[i,5]207                self.Gscaf[ss,3] = float(GGGtemp[i,6])208                self.Gscaf[ss,4] = float(GGGtemp[i,7])209                self.Gscaf[ss,5] = float(GGGtemp[i,8])210                ss += 1211            elif GGGtemp[i,3] == 'CYT':212                self.Cstap[aa,0] = GGGtemp[i,2]213                self.Cstap[aa,1] = GGGtemp[i,4]214                self.Cstap[aa,2] = GGGtemp[i,5]215                self.Cstap[aa,3] = float(GGGtemp[i,6])216                self.Cstap[aa,4] = float(GGGtemp[i,7])217                self.Cstap[aa,5] = float(GGGtemp[i,8])218                aa += 1219        return self.Gscaf, self.Cstap220    def TTT(self):221        TTTtemp = np.loadtxt(os.path.join(_na_lib_dir, 'bdna_tah.pdb'), dtype=object)222        TTTlen = len(TTTtemp)223        # Calculate atoms in scaffold and staple strands224        scafatoms = 0225        stapatoms = 0226        for i in range(TTTlen):227            if TTTtemp[i,3] == 'THY':228                scafatoms += 1229            elif TTTtemp[i,3] == 'ADE':230                stapatoms += 1231        # Initialize scaffold and staple structures232        self.Tscaf = np.zeros((scafatoms, 6), dtype=object)233        self.Astap = np.zeros((stapatoms, 6), dtype=object)234        # Now transfer PDB structure line by line235        ss = 0236        aa = 0237        # Now transfer PDB structure line by line238        for i in range(TTTlen):239            # {atomtype, strand, residue, xcoord, ycoord, zcoord, atom}240            if TTTtemp[i,3] == 'THY':241                self.Tscaf[ss,0] = TTTtemp[i,2]242                self.Tscaf[ss,1] = TTTtemp[i,4]243                self.Tscaf[ss,2] = TTTtemp[i,5]244                self.Tscaf[ss,3] = float(TTTtemp[i,6])245                self.Tscaf[ss,4] = float(TTTtemp[i,7])246                self.Tscaf[ss,5] = float(TTTtemp[i,8])247                ss += 1248            elif TTTtemp[i,3] == 'ADE':249                self.Astap[aa,0] = TTTtemp[i,2]250                self.Astap[aa,1] = TTTtemp[i,4]251                self.Astap[aa,2] = TTTtemp[i,5]252                self.Astap[aa,3] = float(TTTtemp[i,6])253                self.Astap[aa,4] = float(TTTtemp[i,7])254                self.Astap[aa,5] = float(TTTtemp[i,8])255                aa += 1256        return self.Tscaf, self.Astap257# III. Functions for writing a PDB file atom-by-atom258# Requires current chain, resnum, restype259def writePDBresidue(filename, chain, chainnum, resnum, atomnum, mmatomnum,260                    segatomnum, restype, refatoms, basecrds, numchains, fid,261                    outputdir, fpdb, fmm, fseg):262    # Check that file lengths are consistent263    if len(refatoms) != len(basecrds[:,0]):264        fid.write('...Error: Base coord data is inconsistent. Aborting...\n')265    else:266        pass267    268    # This function will append coordinates to a PDB file residue by residue269    # 1. Single-model PDB with alphanumeric chains270    # Do not build this model if numchains > 63271    if numchains <= 63:272        #f = open(outputdir + filename + '.pdb', 'a')273        fid.write('...Chain ' + str(chain) + ', Residue ' + str(resnum) + \274                  ' printing coordinates...\n')275        element = ' '276        # Please see official PDB file format documentation for more 277        # information www.wwpdb.org/documentation/file-format278        #279        for i in range(len(refatoms)):280            # Data type: Record Name: Cols 1 - 6281            fpdb.write('{0:<6s}'.format('ATOM'))282            # Data type: Atom serial number: Cols 7 - 11283            if atomnum < 100000:284                fpdb.write('{0:>5d}'.format(int(atomnum)))285            else:286                hybatomnum = hybrid36encode(atomnum,5)287                fpdb.write('{0:>5s}'.format(str(hybatomnum)))288                #f.write('*****')289            fpdb.write(' ') # <-- One blank space290            # Data type: Atom name: Cols 13 - 16291            # This one is complicated and depends on size of string292            if len(str(refatoms[i])) == 1:293                fpdb.write(' ' + '{0:>1s}'.format(str(refatoms[i])) + '  ')294                element = str(refatoms[i])295            elif len(str(refatoms[i])) == 2:296                fpdb.write(' ' + '{0:>2s}'.format(str(refatoms[i])) + ' ')297                element = str(refatoms[i])[0]298            elif len(str(refatoms[i])) == 3:299                fpdb.write(' ' + '{0:>3s}'.format(str(refatoms[i])))300                element = str(refatoms[i])[0]301            elif len(str(refatoms[i])) == 4:302                fpdb.write('{0:>4s}'.format(str(refatoms[i])))303                element = str(refatoms[i])[1]304            # Data type: Alternate location indicator: Col 17 305            # <-- This is typically empty306            fpdb.write(' ')307            # Data type: Residue name: Cols 18 - 20308            fpdb.write('{0:>3s}'.format(str(restype)))309            # Data type: Chain identifier: Col 22 <-- Insert extra column 21310            fpdb.write('{0:>2s}'.format(str(chain)))311            # Data type: Residue sequence number: Cols 23 - 26312            if resnum < 10000:313                fpdb.write('{0:>4d}'.format(int(resnum)))314            else:315                hybresnum = hybrid36encode(resnum,4)316                fpdb.write('{0:>4s}'.format(str(hybresnum)))317                #f.write('****')318            fpdb.write('    ') # <-- Four blank spaces319            # Data type: X coordinates: Cols 31 - 38 (8.3)320            fpdb.write('{0:>8.3f}'.format(float(basecrds[i,0])))321            # Data type: Y coordinates: Cols 39 - 46 (8.3)322            fpdb.write('{0:>8.3f}'.format(float(basecrds[i,1])))323            # Data type: Z coordinates: Cols 47 - 54 (8.3)324            fpdb.write('{0:>8.3f}'.format(float(basecrds[i,2])))325            # Data type: Occupancy: Cols 55 - 60 (6.2)326            fpdb.write('{0:>6.2f}'.format(float(1.0)))327            # Data type: Temperature factor: Cols 61 - 66 (6.2)328            fpdb.write('{0:>6.2f}'.format(float(0.0)))329            fpdb.write('          ') # <-- Ten blank spaces330            # Data type: Element symbol: Cols 77 - 78331            fpdb.write('{0:>2s}'.format(str(element)))332            # Data type: Charge: Cols 79 - 80 <-- Currently leaving this blank333            fpdb.write('  \n') # <-- Move to next line334            # Iterate atom number335            atomnum += 1336    else:337        pass338    # 2. Multi-model PDB with chains = 'A'339    #f = open(outputdir + filename + '-multimodel.pdb', 'a')340    fid.write('...Model ' + str(chainnum + 1) + ', Residue ' + str(resnum) + \341              ' printing coordinates...\n')342    element = ' '343    # Please see official PDB file format documentation for more information344    # www.wwpdb.org/documentation/file-format345    #346    for i in range(len(refatoms)):347        # Data type: Record Name: Cols 1 - 6348        fmm.write('{0:<6s}'.format('ATOM'))349        # Data type: Atom serial number: Cols 7 - 11350        if mmatomnum < 100000:351            fmm.write('{0:>5d}'.format(int(mmatomnum)))352        else:353            mmhybatomnum = hybrid36encode(mmatomnum,5)354            fmm.write('{0:>5s}'.format(str(mmhybatomnum)))355            #f.write('*****')356        fmm.write(' ') # <-- One blank space357        # Data type: Atom name: Cols 13 - 16358        # This one is complicated and depends on size of string359        if len(str(refatoms[i])) == 1:360            fmm.write(' ' + '{0:>1s}'.format(str(refatoms[i])) + '  ')361            element = str(refatoms[i])362        elif len(str(refatoms[i])) == 2:363            fmm.write(' ' + '{0:>2s}'.format(str(refatoms[i])) + ' ')364            element = str(refatoms[i])[0]365        elif len(str(refatoms[i])) == 3:366            fmm.write(' ' + '{0:>3s}'.format(str(refatoms[i])))367            element = str(refatoms[i])[0]368        elif len(str(refatoms[i])) == 4:369            fmm.write('{0:>4s}'.format(str(refatoms[i])))370            element = str(refatoms[i])[1]371        # Data type: Alternate location indicator: Col 17 372        # <-- This is typically empty373        fmm.write(' ')374        # Data type: Residue name: Cols 18 - 20375        fmm.write('{0:>3s}'.format(str(restype)))376        # Data type: Chain identifier: Col 22 <-- Insert extra column 21377        # For multi-model PDB, this is always 'A'378        fmm.write('{0:>2s}'.format(str('A')))379        # Data type: Residue sequence number: Cols 23 - 26380        if resnum < 10000:381            fmm.write('{0:>4d}'.format(int(resnum)))382        else:383            hybresnum = hybrid36encode(resnum,4)384            fmm.write('{0:>4s}'.format(str(hybresnum)))385            #f.write('****')386        fmm.write('    ') # <-- Four blank spaces387        # Data type: X coordinates: Cols 31 - 38 (8.3)388        fmm.write('{0:>8.3f}'.format(float(basecrds[i,0])))389        # Data type: Y coordinates: Cols 39 - 46 (8.3)390        fmm.write('{0:>8.3f}'.format(float(basecrds[i,1])))391        # Data type: Z coordinates: Cols 47 - 54 (8.3)392        fmm.write('{0:>8.3f}'.format(float(basecrds[i,2])))393        # Data type: Occupancy: Cols 55 - 60 (6.2)394        fmm.write('{0:>6.2f}'.format(float(1.0)))395        # Data type: Temperature factor: Cols 61 - 66 (6.2)396        fmm.write('{0:>6.2f}'.format(float(0.0)))397        fmm.write('          ') # <-- Ten blank spaces398        # Data type: Element symbol: Cols 77 - 78399        fmm.write('{0:>2s}'.format(str(element)))400        # Data type: Charge: Cols 79 - 80 <-- Currently leaving this blank401        fmm.write('  \n') # <-- Move to next line402        # Iterate atom number403        mmatomnum += 1404    #fmm.close()405    # 3. Single-model PDB with chains = 'A' and iterative segid406    #f = open(outputdir + filename + '-chseg.pdb', 'a')407    fid.write('...Segment ' + str(chainnum + 1) + ', Residue ' + \408              str(resnum) + ' printing coordinates...\n')409    element = ' '410    # Please see official PDB file format documentation for more information411    # www.wwpdb.org/documentation/file-format412    #413    for i in range(len(refatoms)):414        # Data type: Record Name: Cols 1 - 6415        fseg.write('{0:<6s}'.format('ATOM'))416        # Data type: Atom serial number: Cols 7 - 11417        if segatomnum < 100000:418            fseg.write('{0:>5d}'.format(int(segatomnum)))419        else:420            hybatomnum = hybrid36encode(segatomnum,5)421            fseg.write('{0:>5s}'.format(str(hybatomnum)))422            #f.write('*****')423        fseg.write(' ') # <-- One blank space424        # Data type: Atom name: Cols 13 - 16425        # This one is complicated and depends on size of string426        if len(str(refatoms[i])) == 1:427            fseg.write(' ' + '{0:>1s}'.format(str(refatoms[i])) + '  ')428            element = str(refatoms[i])429        elif len(str(refatoms[i])) == 2:430            fseg.write(' ' + '{0:>2s}'.format(str(refatoms[i])) + ' ')431            element = str(refatoms[i])[0]432        elif len(str(refatoms[i])) == 3:433            fseg.write(' ' + '{0:>3s}'.format(str(refatoms[i])))434            element = str(refatoms[i])[0]435        elif len(str(refatoms[i])) == 4:436            fseg.write('{0:>4s}'.format(str(refatoms[i])))437            element = str(refatoms[i])[1]438        # Data type: Alternate location indicator: Col 17 439        # <-- This is typically empty440        fseg.write(' ')441        # Data type: Residue name: Cols 18 - 20442        fseg.write('{0:>3s}'.format(str(restype)))443        # Data type: Chain identifier: Col 22 <-- Insert extra column 21444        # Use chain 'A' here445        fseg.write('{0:>2s}'.format(str('A')))446        # Data type: Residue sequence number: Cols 23 - 26447        if resnum < 10000:448            fseg.write('{0:>4d}'.format(int(resnum)))449        else:450            hybresnum = hybrid36encode(atomnum,4)451            fseg.write('{0:>4s}'.format(str(hybresnum)))452            #f.write('****')453        fseg.write('    ') # <-- Four blank spaces454        # Data type: X coordinates: Cols 31 - 38 (8.3)455        fseg.write('{0:>8.3f}'.format(float(basecrds[i,0])))456        # Data type: Y coordinates: Cols 39 - 46 (8.3)457        fseg.write('{0:>8.3f}'.format(float(basecrds[i,1])))458        # Data type: Z coordinates: Cols 47 - 54 (8.3)459        fseg.write('{0:>8.3f}'.format(float(basecrds[i,2])))460        # Data type: Occupancy: Cols 55 - 60 (6.2)461        fseg.write('{0:>6.2f}'.format(float(1.0)))462        # Data type: Temperature factor: Cols 61 - 66 (6.2)463        fseg.write('{0:>6.2f}'.format(float(0.0)))464        fseg.write('      ') # <-- Six blank spaces465        # Write SEGID here <-- This is a NAMD hack that allows for a "large"466        # number of segments or chains (I've allowed for up to 9999 which is467        # unreasonably large)468        fseg.write('{0:>4d}'.format(chainnum+1))469        # Data type: Element symbol: Cols 77 - 78470        fseg.write('{0:>2s}'.format(str(element)))471        # Data type: Charge: Cols 79 - 80 <-- Currently leaving this blank472        fseg.write('  \n') # <-- Move to next line473        # Iterate atom number474        segatomnum += 1475    #f.close()476    return atomnum, mmatomnum, segatomnum477  478# IV. Matrix transformation functions 479# 1. Function to generate transformation matrix 480#    between two data sets481def getTransMat(mob, tar):482    mob_com = mob.mean(0)483    tar_com = tar.mean(0)484    mob = mob - mob_com485    tar = tar - tar_com486    matrix = np.dot(mob.T, tar)487    U, s, Vh = linalg.svd(matrix)488    Id = np.array([[1, 0, 0],489                   [0, 1, 0],490                   [0, 0, np.sign(linalg.det(matrix))]])491    rotation = np.dot(Vh.T, np.dot(Id, U.T))492    transmat = np.eye(4)493    transmat[:3,:3] = rotation494    transmat[:3, 3] = tar_com - mob_com495    return transmat496# 2. Function to transform coordinates497def applyTransMat(transmat, coords):498    return np.dot(coords, transmat[:3,:3].T) + transmat[:3, 3]499# 3. Function to translate coordinates500def translate(mob,trans):501    npts = mob.shape[0]502    503    for i in range(npts):504        mob[i,:] = mob[i,:] + trans505    return mob506# 4. Function to convert Euler rotation to Axis-Angle507def eultoaxisangle(mat):508    [[m00, m10, m20],\509     [m01, m11, m21],\510     [m02, m12, m22]] = mat511    angle = math.degrees(math.acos((m00 + m11 + m22 - 1)/2))512    xtemp = (m21 - m12) / math.sqrt(pow(m21 - m12,2) + pow(m02 - m20,2) + \513                                    pow(m10 - m01,2))514    ytemp = (m02 - m20) / math.sqrt(pow(m21 - m12,2) + pow(m02 - m20,2) + \515                                    pow(m10 - m01,2))516    ztemp = (m10 - m01) / math.sqrt(pow(m21 - m12,2) + pow(m02 - m20,2) + \517                                    pow(m10 - m01,2))518    axis = np.array([xtemp, ytemp, ztemp])519    return angle, axis520# 5. Function to convert Axis-Angle to Euler rotation521def axisangletoeul(angle, axis):522    c = math.cos(math.radians(angle))523    s = math.sin(math.radians(angle))524    t = 1.0 - c525    x, y, z = axis526    mat = [[t*x*x + c, t*x*y - z*s, t*x*z + y*s],\527           [t*x*y + z*s, t*y*y + c, t*y*z - x*s],\528           [t*x*z - y*s, t*y*z + x*s, t*z*z + c]]529    return mat530# V. Functions for encoding large numbers531# 1. Function to encode a decimal using base36 notation532def base36encode(number):533    # 36-character alphabet for base-36 notation534    alphab36 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', \535                'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', \536                'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']537    base36 = ''538    while number:539        number, i  = divmod(number, 36)540        base36 = alphab36[i] + base36541    return base36542# 2. Function to encode a decimal using hybrid36 notation543def hybrid36encode(number,digits):544    # 52-character alphabet for first digit of hybrid36 notation545    alphahyb36 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', \546                  'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', \547                  'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', \548                  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', \549                  'w', 'x', 'y', 'z']550    div, rem = divmod(number, 36 ** (digits - 1))551    b36rem = base36encode(rem)552    hybrem = alphahyb36[div]553    # Need to check length of b36rem string554    hyb36str = str(hybrem) + str(b36rem)555    return hyb36str556# VI. Main PDBGen Function Definition557def pdbgen(filename,abtype,natype,inputdir,outputdir,log):558    559    """560    This function creates PDB files for a data structure input as a .cndo561    filetype.562    563    Parameters564    ----------565      filename --> name of structure (omit .cndo)566      abtype --> type of DNA helical structure ('A' or 'B')567      natype --> 'DNA' or 'RNA'568      inputdir --> directory with input .cndo file569      outputdir --> directory for .pdb and .log output files570    571    Returns572    -------573      none574      575    """576    577    # Create reference DNA/RNA data structure:578    if abtype == 'B' and natype == 'DNA':579        bdna = BDNA()580    else:581        # Only BDNA available in this version pdbgen582        # Return if not selected583        sys.stdout.write('\nNucleic acid type not currently available. Aborting...\n\n')584        return585    586    # Use the given file-like logging object587    fid = log588    # Initialization block589    fid.write('\n\n')590    fid.write('    ##########################################\n')591    fid.write('    ##                                      ##\n')592    fid.write('    ##              PDBGen v1.5             ##\n')593    fid.write('    ##                                      ##\n')594    fid.write('    ##              written by              ##\n')595    fid.write('    ##          William P. Bricker          ##\n')596    fid.write('    ##                  at                  ##\n')597    fid.write('    ##         Mass. Inst. of Tech.         ##\n')598    fid.write('    ##                                      ##\n')599    fid.write('    ##             last updated             ##\n')600    fid.write('    ##             May 9th, 2019            ##\n')601    fid.write('    ##                                      ##\n')602    fid.write('    ##########################################\n')603    fid.write('\n\n')604    sys.stdout.write('\nPDB GENERATOR V1.5\n')605    sys.stdout.write('Begin PDB Generation...\n\n')606    sys.stdout.write('Check ' + filename + '-pdbgen.log for more detailed output...\n\n')607    # Step 1. Extract data from DNA topology file608    cndofilename = filename609    # Parse .cndo file to get dnaInfo610    dnaTop, dNode, triad, id_nt = cndo_to_dnainfo(cndofilename, str(inputdir))611    612    # Create array for dNode, triad613    dNode = np.asarray(dNode)614    triad = np.asarray(triad)615    # 1.1. dnaInfo.dnaTop contains the sequential topology616    # {dnaTop, id, up, down, across, seq}617    # 1.2. dnaInfo.dnaGeom.dNode contains the centroid of each node (bp)618    # {dNode, e0(1), e0(2), e0(3)}619    # 1.3. dnaInfo.dnaGeom.triad contains coordinate system of each node (bp)620    # {triad, e1(1), e1(2), e1(3), e2(1), e2(2), e2(3), e3(1), e3(2), e3(3)}621    # 1.4. dnaInfo.dnaGeom.id_nt contains the basepairing info622    # {id_nt, id1, id2}623    # The object dnaInfo.dnaTop is ordered by chain, starting with the scaffold624    # strand. From this we can sequentially build our PDB file, after a routing625    # procedure.626    fid.write('INPUT PARAMETERS:\n')627    fid.write('  filename = ' + filename + '\n')628    fid.write('  abtype = ' + abtype + '\n')629    fid.write('  natype = ' + natype + '\n')630    fid.write('  inputdir = ' + inputdir + '\n')631    fid.write('  outputdir = ' + outputdir + '\n\n')632    # If pdb files currently exist, delete them633    if os.path.exists(outputdir + filename + '.pdb'):634        os.remove(outputdir + filename + '.pdb')635    if os.path.exists(outputdir + filename + '-multimodel.pdb'):636        os.remove(outputdir + filename + '-multimodel.pdb')637    if os.path.exists(outputdir + filename + '-chseg.pdb'):638        os.remove(outputdir + filename + '-chseg.pdb')639        640    # Check dNode for physical XYZ size of system641    minx, miny, minz = np.amin(dNode[:,1].astype(float)),\642                       np.amin(dNode[:,2].astype(float)),\643                       np.amin(dNode[:,3].astype(float))644    maxx, maxy, maxz = np.amax(dNode[:,1].astype(float)),\645                       np.amax(dNode[:,2].astype(float)),\646                       np.amax(dNode[:,3].astype(float))647    minxyz = np.amin([minx,miny,minz])648    maxxyz = np.amax([maxx,maxy,maxz])649    sys.stdout.write('Minimum XYZ value is ' + str(minxyz) + 650                     ' and maximum XYZ value is ' + str(maxxyz) + '...\n\n')651    652    # PDB filetype is strict and only allows 4 digits positive or negative653    # Note: unless we shift the center of coordinates {0.0, 0.0, 0.0}, the654    # maximum effective size of the nanoparticle is 655    # {1999.998, 1999.998, 1999.998} Angstroms656    if minxyz <= float(-1000.0):657        sys.stdout.write('Minimum XYZ value is too large for ' + 658                         'PDB generation. Aborting...\n\n')659        return660    elif maxxyz >= float(10000.0):661        sys.stdout.write('Maximum XYZ value is too large for ' + 662                         'PDB generation. Aborting...\n\n')663        return664    else:665        pass666    # Initialize PDB Generation Values667    atomnum = 1668    mmatomnum = 1669    segatomnum = 1670    resnum = 1671    chainnum = 0672    numchains = 0673    chlist = 'A'674    cc = 0675    # Chain list consists of 63 alphanumeric characters used sequentially to 676    # number PDB chains.677    chainlist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', \678                 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', \679                 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', \680                 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', \681                 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', \682                 '8', '9']683    684    # First need to re-order the dnaInfo.dnaTop structure as it is not in the 685    # order needed to build a pdb file. Loop through data structure and save to 686    # routeTemp. The dnaInfo.dnaTop structure is ordered so that the scaffold 687    # strand is first. Next, the staple strands are not always contiguous, so 688    # they need to be reordered.689    fid.write('Starting DNA nanostructural routing procedure...\n')690    # Create array for unrouteTemp691    unrouteTemp = np.asarray(dnaTop)692    numbases = len(unrouteTemp[:,0])693    routeTemp = np.zeros((numbases,5),dtype=object)694    visited = np.zeros(numbases,dtype=int)695    routeindex = 0696    sys.stdout.write('There are ' + str(numbases) + 697                     ' total bases in the structure...\n')698    if numbases > 9999:699        sys.stdout.write('WARNING: Chain IDs greater than 9999 bases' +  700                         ' will be output in hybrid base36 notation\n')701    # Estimate number of atoms702    if natype == 'DNA':703        estatoms = int(np.ceil(31.75*numbases))704    sys.stdout.write('There are an estimated ' + str(estatoms) + 705                     ' total atoms in the structure...\n')706    if estatoms > 99999:707        sys.stdout.write('WARNING: Atom IDs greater than 99999 atoms will' +708                         ' be output in hybrid base36 notation\n')709    # Loop through all of the bases710    sys.stdout.write('\n1. DNA nanostructural routing... [                    ]   0%')711    for ii in range(numbases):712        713        # Print progress bar714        nn = int(np.ceil((float(ii+1) / float(numbases)) * 20.0))715        pp = int(np.ceil((float(ii+1) / float(numbases)) * 100.0))716        sys.stdout.write('\r1. DNA nanostructural routing... [' + 717                         '{:<20}'.format('='*nn) + ']' + '{:>4}'.format(pp) + 718                         '%')719        sys.stdout.flush()720        # Base-pairing info for base721        base = unrouteTemp[ii,1:]722        baseid = int(unrouteTemp[ii,1])723        baseup = int(unrouteTemp[ii,2])724        basedown = int(unrouteTemp[ii,3])725        baseacross = int(unrouteTemp[ii,4])726        baseseq = str(unrouteTemp[ii,5])727        # Check if base is a terminal 5' end and not visited yet728        if baseup == -1 and visited[ii] == 0:729            # Append base to route730            routeTemp[routeindex,:] = base731            routeindex += 1732            visited[ii] = 1733            # Set initial length of strand734            strlen = 1735            while basedown != -1:736                nextbaseid = basedown737                # Check if next base is correct one738                # Fails if ii == numbases - 1739                if ii + strlen < numbases - 1:740                    tempnextbaseid = int(unrouteTemp[ii+strlen,1])741                    if nextbaseid == tempnextbaseid:742                        # Base-pairing info for base743                        base = unrouteTemp[ii+strlen,1:]744                        baseid = int(unrouteTemp[ii+strlen,1])745                        baseup = int(unrouteTemp[ii+strlen,2])746                        basedown = int(unrouteTemp[ii+strlen,3])747                        baseacross = int(unrouteTemp[ii+strlen,4])748                        baseseq = str(unrouteTemp[ii+strlen,5])749                    else:750                        # First try unrouteTemp[basedown-1]751                        tempnextbaseid = int(unrouteTemp[basedown-1,1])752                        if nextbaseid == tempnextbaseid:753                            base = unrouteTemp[basedown-1,1:]754                            baseid = int(unrouteTemp[basedown-1,1])755                            baseup = int(unrouteTemp[basedown-1,2])756                            basedown = int(unrouteTemp[basedown-1,3])757                            baseacross = int(unrouteTemp[basedown-1,4])758                            baseseq = str(unrouteTemp[basedown-1,5])759                        else:760                            # If all else fails!761                            # Loop through to find next base in sequence762                            for jj in range(numbases):763                                # Base-pairing info for base764                                base = unrouteTemp[jj,1:]765                                baseid = int(unrouteTemp[jj,1])766                                baseup = int(unrouteTemp[jj,2])767                                basedown = int(unrouteTemp[jj,3])768                                baseacross = int(unrouteTemp[jj,4])769                                baseseq = str(unrouteTemp[jj,5])770                                if baseid == nextbaseid:771                                    break772                                else:773                                    continue774                else:775                    # Loop through to find next base in sequence776                    for jj in range(numbases):777                        # Base-pairing info for base778                        base = unrouteTemp[jj,1:]779                        baseid = int(unrouteTemp[jj,1])780                        baseup = int(unrouteTemp[jj,2])781                        basedown = int(unrouteTemp[jj,3])782                        baseacross = int(unrouteTemp[jj,4])783                        baseseq = str(unrouteTemp[jj,5])784                        if baseid == nextbaseid:785                            break786                        else:787                            continue788                routeTemp[routeindex,:] = base789                routeindex += 1790                strlen += 1791            else:792                # Base is a terminal 3' end793                # Continue the loop794                if basedown == -1:795                    numchains += 1796                    continue797                else:798                    fid.write('...Error in routing info...\n')799                    break      800    sys.stdout.write('\n\nThere are ' + str(numchains) + 801                     ' total chains in the structure...\n')802    if numchains > 63:803        sys.stdout.write('WARNING: Skipping standard PDB file generation' +804                         ' due to large (>63) number of chains.\n')805    806    # Open PDB files for appending807    if numchains <= 63:808        fpdb = open(outputdir + filename + '.pdb', 'a')809    else:810        fpdb = ''811        812    fmm = open(outputdir + filename + '-multimodel.pdb', 'a')813    fseg = open(outputdir + filename + '-segid.pdb', 'a')814    # Tags for ssDNA815    ssfirst = 0 # ID of first nucleotide in ss region816    sslast = 0 # ID of last nucleotide in ss region817    sslength = 0 # Length of ss region818    ssbases = []819    # Go through each base in routed structure820    fid.write('\nExtract coordinates for each base in routed structure...\n')821    sys.stdout.write('\n2. PDB generation... [                    ]   0%')822    for ii in range(numbases):823        # Print progress bar824        nn = int(np.ceil((float(ii+1) / float(numbases)) * 20.0))825        pp = int(np.ceil((float(ii+1) / float(numbases)) * 100.0))826        sys.stdout.write('\r2. PDB generation... [' + 827                         '{:<20}'.format('='*nn) + ']' + '{:>4}'.format(pp) + 828                         '%')829        sys.stdout.flush()830        # Get base-pairing info831        base = routeTemp[ii,:]832        baseid = int(routeTemp[ii,0])833        baseup = int(routeTemp[ii,1])834        basedown = int(routeTemp[ii,2])835        baseacross = int(routeTemp[ii,3])836        baseseq = str(routeTemp[ii,4])837        #print ii, baseid, baseup, basedown, baseacross, baseseq838        # Tag for type of base strand839        type = 0 # scaf = 1, stap = 2, ssdna = 3840        841        # Check if the base is 5'-end842        if baseup == -1:843            # Multi-model PDB starts new model here844            fmm.write('MODEL' + '{0:>9s}'.format(str(chainnum + 1)) + '\n')   845        #print sslength, sslast, baseid846        # Check if in an ssDNA region847        if sslength > 0:848            if sslength == 1:849                ssfirst = 0850                sslast = 0851                sslength = 0852                ssbases = []853                continue854            elif baseid != sslast:855                continue856            elif baseid == sslast:857                ssfirst = 0858                sslast = 0859                sslength = 0860                ssbases = []861                continue862        else:863            pass864        # First Check if base is unpaired865        if baseacross == -1:866            type = 3867            fid.write('...Unpaired base...\n')868            pass869        else:870            # Otherwise, Extract basepairid871            for j, bp in enumerate(id_nt):872                # Scaffold strand873                if baseid == int(bp[1]):874                    bpid = int(j)875                    type = 1876                    fid.write('...Scaffold strand...\n')877                    break878                # Staple strand879                elif baseid == int(bp[2]):880                    bpid = int(j)881                    type = 2882                    fid.write('...Staple strand...\n')883                    break884        #print base, type885        # Only basepaired sequences have coordinates886        if type == 1 or type == 2:887            # Extract Centroid of Base888            xx0, yy0, zz0 = float(dNode[bpid,1]), float(dNode[bpid,2]), \889                            float(dNode[bpid,3])890            #print xx0, yy0, zz0891            # Extract Coordinate System of Base892            xx1, xx2, xx3 = float(triad[bpid,1]), float(triad[bpid,2]), \893                            float(triad[bpid,3]) # X-axis894            yy1, yy2, yy3 = float(triad[bpid,4]), float(triad[bpid,5]), \895                            float(triad[bpid,6]) # Y-axis896            zz1, zz2, zz3 = float(triad[bpid,7]), float(triad[bpid,8]), \897                            float(triad[bpid,9]) # Z-axis898            #print xx1, xx2, xx3899            #print yy1, yy2, yy3900            #print zz1, zz2, zz3901            # Get transformation matrix for origin to base coordinate system902            xyzorigin = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])    903            xyzbase = np.array([[xx0, yy0, zz0],904                                [xx0 + xx1, yy0 + xx2, zz0 + xx3],905                                [xx0 + yy1, yy0 + yy2, zz0 + yy3],906                                [xx0 + zz1, yy0 + zz2, zz0 + zz3]])907            #print xyzorigin, xyzbase908            transformMat = getTransMat(xyzorigin, xyzbase)909        # For unpaired sequences, need to calculate the reference frame910        elif type == 3:911            # Whole ss region will be calculated within this statement912            ssfirst = baseid913            upbase = baseup914            sslast = baseid915            downbase = basedown916            ssbases.append(base)917            sslength = 1918            # Now find last unpaired nucleotide919            #print baseup, basedown920            itemp = ii921            while baseacross == -1:922                itemp += 1923                # Get base-pairing info924                base = routeTemp[itemp,:]925                baseid = int(routeTemp[itemp,0])926                baseup = int(routeTemp[itemp,1])927                basedown = int(routeTemp[itemp,2])928                baseacross = int(routeTemp[itemp,3])929                baseseq = str(routeTemp[itemp,4])930                if baseacross == -1:931                    sslast = baseid932                    ssbases.append(base)933                    downbase = basedown934                    sslength += 1935                    continue936                else:937                    break938            #print baseup, basedown939            # Print ssDNA characteristics940            fid.write('  ssDNA region (first: ' + str(ssfirst) + ', last: ' + 941                      str(sslast) + ', length: ' + str(sslength) + ')\n')     942            # Extract coordinates of upstream base943            for j, bp in enumerate(id_nt):944                # Scaffold strand945                if upbase == int(bp[1]):946                    bpidup = int(j)947                    fid.write('...Upstream base ID is ' + str(bpidup) + '...\n')948                    typeup = 1949                    fid.write('...Upstream base is scaffold strand...\n')950                    break951                # Staple strand952                elif upbase == int(bp[2]):953                    bpidup = int(j)954                    fid.write('...Upstream base ID is ' + str(bpidup) + '...\n')955                    typeup = 2956                    fid.write('...Upstream base is staple strand...\n')957                    break958            # Extract Centroid of Upstream Base959            xx0up, yy0up, zz0up = float(dNode[bpidup,1]), float(dNode[bpidup,2]), \960                                  float(dNode[bpidup,3])961            #print xx0up, yy0up, zz0up962            # Extract Coordinate System of Upstream Base963            xx1up, xx2up, xx3up = float(triad[bpidup,1]), float(triad[bpidup,2]), \964                                  float(triad[bpidup,3]) # X-axis965            yy1up, yy2up, yy3up = float(triad[bpidup,4]), float(triad[bpidup,5]), \966                                  float(triad[bpidup,6]) # Y-axis967            zz1up, zz2up, zz3up = float(triad[bpidup,7]), float(triad[bpidup,8]), \968                                  float(triad[bpidup,9]) # Z-axis969            xyzbase0 = np.array([[xx0up, yy0up, zz0up],970                                 [xx0up + xx1up, yy0up + xx2up, zz0up + xx3up],971                                 [xx0up + yy1up, yy0up + yy2up, zz0up + yy3up],972                                 [xx0up + zz1up, yy0up + zz2up, zz0up + zz3up]])973            # Extract coordinates of downstream base974            for j, bp in enumerate(id_nt):975                # Scaffold strand976                if downbase == int(bp[1]):977                    bpiddo = int(j)978                    fid.write('...Downstream base ID is ' + str(bpiddo) + '...\n')979                    typedo = 1980                    fid.write('...Downstream base is scaffold strand...\n')981                    break982                # Staple strand983                elif downbase == int(bp[2]):984                    bpiddo = int(j)985                    fid.write('...Downstream base ID is ' + str(bpiddo) + '...\n')986                    typedo = 2987                    fid.write('...Downstream base is staple strand...\n')988                    break989            # Extract Centroid of Downstream Base990            xx0do, yy0do, zz0do = float(dNode[bpiddo,1]), float(dNode[bpiddo,2]), \991                                  float(dNode[bpiddo,3])992            # Extract Coordinate System of Downstream Base993            xx1do, xx2do, xx3do = float(triad[bpiddo,1]), float(triad[bpiddo,2]), \994                                  float(triad[bpiddo,3]) # X-axis995            yy1do, yy2do, yy3do = float(triad[bpiddo,4]), float(triad[bpiddo,5]), \996                                  float(triad[bpiddo,6]) # Y-axis997            zz1do, zz2do, zz3do = float(triad[bpiddo,7]), float(triad[bpiddo,8]), \998                                  float(triad[bpiddo,9]) # Z-axis999            xyzbase3 = np.array([[xx0do, yy0do, zz0do],1000                                 [xx0do + xx1do, yy0do + xx2do, zz0do + xx3do],1001                                 [xx0do + yy1do, yy0do + yy2do, zz0do + yy3do],1002                                 [xx0do + zz1do, yy0do + zz2do, zz0do + zz3do]])1003            # Compute distance between upstream and downstream bases1004            dist12 = sqrt(pow(float(xx0do)-float(xx0up), 2) + \1005                          pow(float(yy0do)-float(yy0up), 2) + \1006                          pow(float(zz0do)-float(zz0up), 2))1007            # Debugging - ssDNA region distance1008            #print 'ssDNA Region Dist = ', str(dist12)1009            # Check that up- and downstream bases are same type1010            if typeup == typedo:1011                pass1012            elif typeup != typedo:1013                fid.write('...Error: up- and down-stream bases in ssDNA region' +1014                          '    are differing types...\n')1015                break1016            # Need two additional points for ss bulge region1017            # Check: make this work for additional types of ss regions1018            #1019            # Diagram shows a typical 'staple' ss bulge region1020            #1021            #       d1 ------ d21022            #       |         |1023            #       |         ^1024            #       |         |1025            #   <--(d0)       (d3)-->1026            #       |1027            #       v1028            #1029            # Point 0 is upstream base centroid1030            d0 = np.array([xx0up, yy0up, zz0up])1031            # Point 3 is downstream base centroid1032            d3 = np.array([xx0do, yy0do, zz0do])1033            # Distance to extend d0 - d1 and d3 - d2 axes1034            # To test later - Used a 5 Ang distance for points d1 and d2. 1035            # Is this dependent on ssDNA length?1036            dext = 5 # Angstroms1037            #dext = sslength # Angstroms (Try 1 Angstrom per ssDNA base)1038            # Point 1 is along Upstream Z-axis1039            # Scaffold1040            if typeup == 1:1041                d1 = np.array([dext*zz1up, dext*zz2up, dext*zz3up])1042            # Staple1043            elif typeup == 2:1044                d1 = np.array([-dext*zz1up, -dext*zz2up, -dext*zz3up])1045            else:1046                fid.write('...Error: Upstream base does not have a base type...\n')1047                break1048            # Point 2 is along Downstream Z-axis1049            # Scaffold1050            if typeup == 1:1051                d2 = np.array([(xx0do - dext*zz1do) - xx0up, 1052                               (yy0do - dext*zz2do) - yy0up, 1053                               (zz0do - dext*zz3do) - zz0up])1054            # Staple1055            elif typeup == 2:1056                d2 = np.array([(xx0do + dext*zz1do) - xx0up, 1057                               (yy0do + dext*zz2do) - yy0up, 1058                               (zz0do + dext*zz3do) - zz0up])1059            else:1060                fid.write('...Error: Upstream base does not have a base type...\n')1061                break1062            # Move upstream and downstream bases to {0,0,0}1063            xyzbase30 = np.array([[0, 0, 0],1064                                  [xx1do, xx2do, xx3do],1065                                  [yy1do, yy2do, yy3do],1066                                  [zz1do, zz2do, zz3do]])1067            1068            xyzbase00 = np.array([[0, 0, 0],1069                                  [xx1up, xx2up, xx3up],1070                                  [yy1up, yy2up, yy3up],1071                                  [zz1up, zz2up, zz3up]])1072            # Total rotation matrix between base0 and base3 at origin1073            Rtot = getTransMat(xyzbase30, xyzbase00)1074            # Extract rotation matrix from transformation matrix1075            Rrotate = Rtot[:3,:3]1076            # Convert to axis-angle representation1077            angle, axis = eultoaxisangle(Rrotate)1078            # Loop through ssDNA bases1079            for jj in range(sslength):        1080            1081                # Get ss base information1082                base = ssbases[jj]1083                baseid = int(base[0])1084                baseup = int(base[1])1085                basedown = int(base[2])1086                baseacross = int(base[3])1087                baseseq = str(base[4])1088                # Now pull reference base information1089                refcrds = []1090                refatoms = []1091                restype = ''1092                if typeup == 1 and abtype == 'B' and natype == 'DNA': # Scaffold strand1093                    if baseseq == 'A':1094                        refcrds = bdna.Ascaf[:,3:6]1095                        refatoms = bdna.Ascaf[:,0]1096                        restype = 'ADE'1097                    elif baseseq == 'C':1098                        refcrds = bdna.Cscaf[:,3:6]1099                        refatoms = bdna.Cscaf[:,0]1100                        restype = 'CYT'1101                    elif baseseq == 'G':1102                        refcrds = bdna.Gscaf[:,3:6]1103                        refatoms = bdna.Gscaf[:,0]1104                        restype = 'GUA'1105                    elif baseseq == 'T':1106                        refcrds = bdna.Tscaf[:,3:6]1107                        refatoms = bdna.Tscaf[:,0]1108                        restype = 'THY'1109                    else:1110                        fid.write('...Error: No base sequence for scaffold strand...\n')1111                elif typeup == 2 and abtype == 'B' and natype == 'DNA': # Staple strand1112                    if baseseq == 'A':1113                        refcrds = bdna.Astap[:,3:6]1114                        refatoms = bdna.Astap[:,0]1115                        restype = 'ADE'1116                    elif baseseq == 'C':1117                        refcrds = bdna.Cstap[:,3:6]1118                        refatoms = bdna.Cstap[:,0]1119                        restype = 'CYT'1120                    elif baseseq == 'G':1121                        refcrds = bdna.Gstap[:,3:6]1122                        refatoms = bdna.Gstap[:,0]1123                        restype = 'GUA'1124                    elif baseseq == 'T':1125                        refcrds = bdna.Tstap[:,3:6]1126                        refatoms = bdna.Tstap[:,0]1127                        restype = 'THY'1128                    else:1129                        fid.write('...Error: No base sequence for staple strand...\n')1130                else:1131                    fid.write('...Error: SS base sequence not labelled as scaffold or staple strand...\n')1132                    continue1133                # First move to upstream base position1134                xyzorigin = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])1135                # Transform upstream base to XYZ origin1136                transformMat = getTransMat(xyzorigin, xyzbase00)1137                upbasecrds = applyTransMat(transformMat, refcrds)1138        1139                # Reset temporary rotation matrix to total rotation matrix1140                Rtemp = Rtot1141                iangle = angle * (jj+1) / (sslength+1)1142                # Test - Use cubic Bezier function to interpolate curve1143                # d0 - upstream base, d3 - downstream base1144                #1145                # B(t) = (1 - t)^3 * d0 + 1146                #        3(1 - t)^2 * t * d1 +1147                #        3(1 - t) * t^2 * d2 +1148                #        t^3 * d31149                # Fraction along Bezier curve1150                t = (float(jj) + 1) / (float(sslength) + 1)1151                1152                # Use d0 (upstream base) as reference point for Bezier interpolation1153                d0 = np.array([0.0, 0.0, 0.0])1154                d3 = np.array([xx0do - xx0up, yy0do - yy0up, zz0do - zz0up])1155                # Bezier contributions1156                B3 = (1 - t)**3 * d01157                B2 = 3 * (1 - t)**2 * t * d11158                B1 = 3 * (1 - t) * t**2 * d21159                B0 = t**3 * d31160                # Total Bezier function for ssDNA region1161                Bt = B3 + B2 + B1 + B01162     1163                # Set interpolated vector to Bezier sum1164                ivec = Bt1165                # Switch back to Euler rotation matrix1166                Rrotiter = np.array(axisangletoeul(iangle,axis))1167                Rtemp[:3,:3] = Rrotiter1168                Rtemp[:3,3] = ivec1169                # Transform B-form base to Bezier position1170                basecrds = applyTransMat(Rtemp, upbasecrds)1171                # Move base back to upbase coordinates1172                basecrds = translate(basecrds,np.array([xx0up, yy0up, zz0up]))1173                # Write out PDB file sequentially1174                # Pass {filename, chain, residue number, atom number, residue type,1175                # atom types, base coords} to PDB writer1176                atomnum, mmatomnum, segatomnum = writePDBresidue(filename, chlist, 1177                                                 chainnum, resnum, atomnum, 1178                                                 mmatomnum, segatomnum, restype, 1179                                                 refatoms, basecrds, numchains, fid,1180                                                 outputdir, fpdb, fmm, fseg)1181                # Iterate residue indexing1182                resnum += 11183        # Now pull reference base information1184        refcrds = []1185        refatoms = []1186        restype = ''1187        if type == 1 and abtype == 'B' and natype == 'DNA': # Scaffold strand1188            if baseseq == 'A':1189                refcrds = bdna.Ascaf[:,3:6]1190                refatoms = bdna.Ascaf[:,0]1191                restype = 'ADE'1192            elif baseseq == 'C':1193                refcrds = bdna.Cscaf[:,3:6]1194                refatoms = bdna.Cscaf[:,0]1195                restype = 'CYT'1196            elif baseseq == 'G':1197                refcrds = bdna.Gscaf[:,3:6]1198                refatoms = bdna.Gscaf[:,0]1199                restype = 'GUA'1200            elif baseseq == 'T':1201                refcrds = bdna.Tscaf[:,3:6]1202                refatoms = bdna.Tscaf[:,0]1203                restype = 'THY'1204            else:1205                fid.write('...Error: No base sequence for scaffold strand...\n')1206        elif type == 2 and abtype == 'B' and natype == 'DNA': # Staple strand1207            if baseseq == 'A':1208                refcrds = bdna.Astap[:,3:6]1209                refatoms = bdna.Astap[:,0]1210                restype = 'ADE'1211            elif baseseq == 'C':1212                refcrds = bdna.Cstap[:,3:6]1213                refatoms = bdna.Cstap[:,0]1214                restype = 'CYT'1215            elif baseseq == 'G':1216                refcrds = bdna.Gstap[:,3:6]1217                refatoms = bdna.Gstap[:,0]1218                restype = 'GUA'1219            elif baseseq == 'T':1220                refcrds = bdna.Tstap[:,3:6]1221                refatoms = bdna.Tstap[:,0]1222                restype = 'THY'1223            else:1224                fid.write('...Error: No base sequence for staple strand...\n')1225        elif type == 3: # Single-stranded region1226            # Single-stranded regions already written out above1227            continue1228        else:1229            fid.write('...Error: Base sequence not labelled as scaffold or staple strand...\n')1230            continue1231        # Now transform reference coordinates to base coordinate system1232        basecrds = applyTransMat(transformMat, refcrds)1233        # Write out PDB file sequentially1234        # Pass {filename, chain, residue number, atom number, residue type, 1235        # atom types, base coords} to PDB writer 1236        atomnum, mmatomnum, segatomnum = writePDBresidue(filename, chlist, 1237                                         chainnum, resnum, atomnum, mmatomnum, 1238                                         segatomnum, restype, refatoms, 1239                                         basecrds, numchains, fid, outputdir,1240                                         fpdb, fmm, fseg)1241        # Iterate residue indexing1242        resnum += 11243        if basedown == -1:1244          1245            # Standard PDB end chain1246            if numchains <= 63:1247                fpdb.write('TER\n')1248            1249            # Chain segment PDB end chain1250            fseg.write('TER\n')1251            1252            # Multi-model PDB ends model here1253            fmm.write('TER\nENDMDL\n')1254            1255            # Iterate chainnum and return mmatomnum to 11256            chainnum += 11257            mmatomnum = 11258            resnum = 11259            # Chainlist definition1260            # Now deprecated due to not printing standard model if >631261            if chainnum < 62:1262                chlist = chainlist[chainnum]1263            elif chainnum == int(62 * (cc + 1)):1264                chlist = chainlist[chainnum - int(62*(cc+1))]1265                cc += 11266            else:1267                chlist = chainlist[chainnum - int(62*cc)]1268                1269    # Finalization of script1270    fid.write('\n  PDB Generation Successful!  \n\n')1271    sys.stdout.write('\n\nPDB Generation Successful!\n')1272    if numchains <= 63:1273        sys.stdout.write('Standard PDB file is output as ' + filename + '.pdb\n')1274        sys.stdout.write('    This file can be opened in any visualizer...\n')1275    sys.stdout.write('Multimodel PDB file is output as ' + filename + '-multimodel.pdb\n')1276    sys.stdout.write('    This file can be opened in UCSD Chimera...\n')1277    sys.stdout.write('Chain segment PDB file is output as ' + filename + '-chseg.pdb\n')1278    sys.stdout.write('    This file can be opened in VMD...\n')1279    sys.stdout.write('Happy PDB viewing!\n\n')1280    1281    # Close any open files1282    if numchains <= 63:1283        fpdb.close()1284    fmm.close()1285    fseg.close()1286    fid.close()1287    ...qrouter_depth.py
Source:qrouter_depth.py  
1import numpy as np2import networkx as nx3import copy4from py2qan.bench_arch import BenchArch5from py2qan.heuristic_mapper import HeuristicMapper6from py2qan.scheduler import Scheduler7from qiskit import QuantumCircuit8from qiskit.quantum_info import Operator9from qiskit.circuit.library.standard_gates import SwapGate, RZZGate10from qiskit.extensions.unitary import UnitaryGate11from qiskit.converters import *12class QuRouter(BenchArch):13    """14    The permutation-aware routing and scheduling heuristics developed in 2QAN [1]15    **References:**16    [1] Lingling Lao, Dan E. Browne. 2QAN: A quantum compiler for 2-local Hamiltonian simulation algorithms, 17    ISCA'22, arXiv:2108.02099.18    """19    def __init__(self, qasm, init_map, coupling_map=None, verbose=False):20        """21        Args:22            qasm: circuit in OpenQASM format23            lattice_xy(tuple): (x,y) if coupling_map is not given and the topology is grid24            init_map(dict): the initial qubit map {circuit qubit index: device qubit index}25            coupling_map(list): connectivity between qubits such as 26            [(0,1), (1,2), (2,3), (3,4), (4,5), (5,6), (6,7), (0,7), (0,8), (1,9), (0,10)]27            verbose(bool): True if print out intermediate mapping info28        """29        super().__init__(qasm=qasm, coupling_map=coupling_map)30        self.init_map = init_map31        self.init_instrs, self.unroute_instrs = self.find_r_ur_instrs(self.pairs, self.init_map)32        self.route_instrs = copy.deepcopy(self.init_instrs)33        self.maps = {}34        self.map_instrs = {}35        self.map_moves = {}36        self.swap_count = 037        self.dswap_count = 038        self.verbose = verbose39        if verbose:40            print('Initial vp-map ', self.init_map)41            print('NN gate (qubit pairs) ', self.init_instrs)42            print('Un-routed gate (qubit pairs) ', self.unroute_instrs)43    def find_r_ur_instrs(self, unrouted_pairs, vpmap):44        """45        Find routed and unrouted gates in a qubit map46        """47        route_instrs = []48        unroute_instrs = []49        for qpair in unrouted_pairs:50            q0, q1 = self.find_qlocs(qpair, vpmap)51            if self.distmat[q0][q1] == 1:52                route_instrs.append(qpair)53            else:54                unroute_instrs.append(qpair)55        return route_instrs, unroute_instrs56    def find_qlocs(self, qpair, vpmap):57        """58        Find the device locations of a qubit pair59        """60        q0 = vpmap[qpair[0]]61        q1 = vpmap[qpair[1]]62        ql0 = min(q0, q1)63        ql1 = max(q0, q1)64        return ql0, ql165    def check_nn(self, qpair, vpmap):66        q0, q1 = self.find_qlocs(qpair, vpmap)67        d = self.distmat[q0][q1]68        return d-169    def dist_instrs(self, instrs, vpmap):70        """71        Calculate the distances of a set of instructions in a given qubit map72        """73        dist_instrs = {}74        instrs_dists = {}75        for qpair in instrs:76            q0, q1 = self.find_qlocs(qpair, vpmap)77            dist = int(self.distmat[q0][q1])78            instrs_dists[qpair] = dist79            if dist in dist_instrs.keys():80                dist_instrs[dist].append(qpair)81            else:82                dist_instrs[dist] = [qpair]83        dmin = min(dist_instrs.keys())84        dsum = sum(instrs_dists.values())85        return dist_instrs[dmin], dsum86    def reverse_dict(self, vpmap):87        rev_map = dict([reversed(i) for i in vpmap.items()])88        return rev_map89    def swap_q_map(self, q0, q1, map):90        cmap = copy.deepcopy(map)91        p0 = cmap[q0]92        p1 = cmap[q1]93        cmap[q0] = p194        cmap[q1] = p095        return cmap96    def qbt_overlap(self, new_move, old_moves):97        overlap = 098        if old_moves:99            for mov in old_moves:100                if set(mov) & set(new_move):101                    overlap += 1102        return overlap103    def router(self):104        """The routing heuristic in 2QAN:105        For each qubit map, all NN gates will be routed regardless their order in the initial circuit106        One finds best movement set for each un-NN gate107        """108        route_instrs = copy.deepcopy(self.init_instrs)109        unroute_instrs = copy.deepcopy(self.unroute_instrs)110        instrs_canswap = copy.deepcopy(self.pairs)111        maps = {}112        current_map = copy.deepcopy(self.init_map)113        instr_sets = {}114        movements = {}115        map_id = 0116        new_cycles = [[]]117        def schedule_moves_insts(inst, cycles):118            new_cycles = copy.deepcopy(cycles)119            for i in range(len(new_cycles)):120                j = len(new_cycles) - 1 - i121                if self.qbt_overlap(inst, new_cycles[j]):122                    break123            if j+1 < len(new_cycles):124                new_cycles[j+1].append(inst)125            else:126                new_cycles.append([inst])127            return new_cycles, j128        while unroute_instrs:129            # Find the gates which are closest130            closest_unroute_ins, dsum = self.dist_instrs(unroute_instrs, current_map)131            pv_map = self.reverse_dict(current_map) 132            instr = closest_unroute_ins[0]133            # Find the shortest paths for this pair;134            # find all possible movement sets for each path;135            # find all possible first movement for each path, either from 136            # evaluate which movement leads to minimum overhead for all the unrouted gates.137            p0, p1 = self.find_qlocs(instr, current_map)138            paths = [p for p in nx.all_shortest_paths(self.topology, source=p0, target=p1)]139            tmaps = []140            moves = []141            dsums = []142            dps = []143            for path in paths:144                p0nn = path[1]145                p1nn = path[-2]146                move01 = [self.zz_tuple((pv_map[p0], pv_map[p0nn])), 147                            self.zz_tuple((pv_map[p1], pv_map[p1nn]))]148                tmap01 = [self.swap_q_map(pv_map[p0], pv_map[p0nn], current_map), 149                          self.swap_q_map(pv_map[p1], pv_map[p1nn], current_map)]150                dsum01 = [self.dist_instrs(unroute_instrs, tmap01[0])[1], 151                          self.dist_instrs(unroute_instrs, tmap01[1])[1]]152                if dsum01[0] == dsum01[1]:153                    cycles, dp0 = schedule_moves_insts(move01[0], new_cycles)154                    cycles, dp1 = schedule_moves_insts(move01[1], new_cycles)155                    if dp0 == dp1:156                        if move01[0] in instrs_canswap and move01[1] not in instrs_canswap:157                            iid = 0158                        elif move01[0] not in instrs_canswap and move01[1] in instrs_canswap:159                            iid = 1160                        else:161                            iid, ids = locate_min(dsum01)162                    else:163                        iid = np.argmin([dp0, dp1])164                    dp = [dp0,dp1][iid]165                else:166                    iid = np.argmin(dsum01)167                    cycles, dp = schedule_moves_insts(move01[iid], new_cycles)168                tmap = tmap01[iid]169                move = move01[iid]170                d = dsum01[iid]171                tmaps.append(tmap)172                moves.append(self.zz_tuple(move))173                dsums.append(d)174                dps.append(dp)175            176            dmin_id, ids = locate_min(dsums)177            if len(ids) >1:178                new_idps = {}179                for i in ids:180                    new_idps[i] = dps[i]181                dp_minid, idps = locate_min(list(new_idps.values()))182                if len(idps) > 1:183                    for i in idps:184                        if moves[list(new_idps.keys())[i]] in instrs_canswap:185                            dmin_id = i186                            break187            # TODO: if there are multiple minimal sets, 188            # select the one which leads to shortest depth, 189            # or select the one which has more movements on existing gate pairs.190            fmove = moves[dmin_id]191            if fmove in instrs_canswap:192                instrs_canswap.remove(fmove)193            current_map = self.swap_q_map(fmove[0], fmove[1], current_map)194            new_routed, unroute_instrs = self.find_r_ur_instrs(unroute_instrs, current_map)195            if map_id in movements.keys():196                movements[map_id].append(fmove)197            else:198                movements[map_id] = [fmove]199            new_cycles, dp = schedule_moves_insts(fmove, new_cycles)200            for inst in new_routed:201                new_cycles, dp = schedule_moves_insts(inst, new_cycles)202            if new_routed:203                maps[map_id] = current_map204                instr_sets[map_id] = new_routed205                map_id += 1206        if self.verbose:207            print('All sets of SWAPs ', movements)208            # print('All vp-maps after each SWAP set ', maps)209            print('New NN gate (qubit pairs) for each vp-map', instr_sets)210        self.maps = maps211        self.map_instrs = instr_sets212        self.map_moves = movements213    def critical_gates(self, unroute_instrs):214        """Find the gates in critical path"""215        qbt_gates = {}216        for qpair in unroute_instrs:217            for q in qpair:218                if q not in qbt_gates:219                    qbt_gates[q] = [qpair]220                else:221                    qbt_gates[q].append(qpair)222        values = list(qbt_gates.values())223        lens = [len(value) for value in values]224        keys = list(qbt_gates.keys())225        iid = np.argmax(lens)226        busy_q = keys[iid]227        cgates = values[iid]228        return cgates229    def graph_color(self, routed_instrs):230        # Schedule init_routed_instrs using graph coloring, all intructions could be exchangeable.231        G0 = self.instruction_graph(routed_instrs)232        LG_G0 = nx.line_graph(G0)233        Cl = nx.coloring.greedy_color(LG_G0, strategy="largest_first")234        cmax = max(Cl.values())235        order_g0 = {}236        for c in range(cmax+1): 237            order_g0[c] = []238            for ed in Cl:239                if Cl[ed] == c:240                    order_g0[c].append(self.zz_tuple(ed))241        new_init_routed = []242        for c in order_g0:243            new_init_routed += order_g0[c]244        return order_g0, new_init_routed245    def schedule_cycle(self, inst, cycle_insts, map_insts, vpmap):246        schedule_at = False247        nn = self.check_nn(inst, vpmap)248        if not nn:249            if not self.qbt_overlap(inst, cycle_insts):250                schedule_at = True251                map_insts.remove(inst)252                cycle_insts.append(inst)253        return map_insts, cycle_insts, schedule_at254    def scheduler(self):255        """256        Hybrid scheduling: using graph coloring algorithms to schedule the commutable gates and 257        using the normal dag-based approach to schedule uncommutable gates.258        """259        init_routed_instrs = copy.deepcopy(self.route_instrs)260        all_routed_instrs = copy.deepcopy(self.route_instrs)261        # Combine movement with circuit gate, and then specify movement types262        # In g_types, 1 represents a dressed movement (move+circuit gate), 263        # 0 represents move gate264        mv_types = {}265        g_types = {}266        swap_count = 0267        dswap_count = 0268        n_maps = len(self.map_moves)269        for i in range(len(self.map_moves)):270            mv_types[i] = {}271            swap_count += len(self.map_moves[i])272            for j in range(len(self.map_moves[i])):273                mv = self.map_moves[i][j]274                mv_types[i][mv] = 0275                if mv in self.pairs:276                    if mv not in g_types:277                        mv_types[i][mv] = 1278                        dswap_count += 1279                        g_types[mv] = 1280                        if mv in init_routed_instrs:281                            init_routed_instrs.remove(mv)282                        else:283                            for mn in range(n_maps):284                                if mv in self.map_instrs[mn]:285                                    self.map_instrs[mn].remove(mv)286        self.swap_count = swap_count287        self.dswap_count = dswap_count288        if self.verbose:289            print(f'Total number of SWAPs is {swap_count} and the dressed SWAP count is {dswap_count}')290            print(f'Total number of two-qubit gates is {len(self.instrs)+(swap_count-dswap_count)}')291            print(f'Total number of CX gates is {len(self.instrs)*2+3*(swap_count-dswap_count)+dswap_count}')292        # We first schedule the initially routed gates using graph coloring, 293        # this will help to reduce the circuit depth294        if init_routed_instrs:295            G0 = self.instruction_graph(init_routed_instrs)296            LG_G0 = nx.line_graph(G0)297            Cl = nx.coloring.greedy_color(LG_G0, strategy="largest_first")298            cmax = max(Cl.values())299            order_g0 = {}300            for c in range(cmax+1): 301                order_g0[c] = []302                for ed in Cl:303                    if Cl[ed] == c:304                        order_g0[c].append(self.zz_tuple(ed))305            new_init_routed = []306            for c in order_g0:307                new_init_routed += order_g0[c]308            init_routed_instrs =list(reversed(new_init_routed))309            init_routed_instrs =new_init_routed310        #End of graph coloring schedule311        if not n_maps:312            # no routing is required for this circuit313            return init_routed_instrs314        # Cycles, each cycle consists of the gates that are scheduled at this cycle315        cycles = [[]] 316        # Corresponding cycles, gtypes indicates the gate types for each instruction at each cycle317        gtypes = [[]] 318        # 2 represents an original circuit gate, 319        # 1 represents a move+circuit gate, 0 represents move gate320        cycle_maps = [self.maps[n_maps-1]]321        phy_cycles = [[]]322        dp = 0323        gn0 = len(self.instrs)+(swap_count-dswap_count) # The number of gates need to be scheduled324        gn1 = 0 # The number of gates have been scheduled325        while gn0 != gn1:326            # Check whether any gate that were routed undirectly can be scheduled at each cycle327            for i in range(n_maps):328                n = n_maps-1-i329                map_instrs = copy.deepcopy(self.map_instrs[n])330                for inst in map_instrs:331                    self.map_instrs[n], cycles[dp], scheduled = self.schedule_cycle(inst, cycles[dp], 332                                                                self.map_instrs[n], cycle_maps[dp])333                    if scheduled:334                        gtypes[dp].append(2)335                        phy_cycles[dp].append(self.find_qlocs(inst, cycle_maps[dp]))336            # Check whether any movement can be scheduled at each cycle337            for i in range(n_maps):338                n = n_maps-1-i339                moves = copy.deepcopy(self.map_moves[n])340                unscheduled = []341                for j in range(n, n_maps-1):342                    unscheduled += self.map_instrs[j]343                    if j < n_maps-1:344                        unscheduled += self.map_moves[j+1]345                for move in reversed(moves):346                    # Movement can be scheduled only if 347                    # the circuit gates that depend on it have been scheduled348                    if not self.qbt_overlap(move, unscheduled):349                        self.map_moves[n], cycles[dp], scheduled = self.schedule_cycle(move, cycles[dp], 350                                                                    self.map_moves[n], cycle_maps[dp])351                        if scheduled:352                            gtypes[dp].append(mv_types[n][move])353                            cycle_maps[dp] = self.swap_q_map(move[0], move[1], cycle_maps[dp])354                            phy_cycles[dp].append(self.find_qlocs(move, cycle_maps[dp]))355            # Check whether any initially routed gate can be scheduled at each cycle356            routeds = copy.deepcopy(init_routed_instrs)357            for inst in routeds:358                init_routed_instrs, cycles[dp], scheduled = self.schedule_cycle(inst, cycles[dp], 359                                                                init_routed_instrs, cycle_maps[dp])360                if scheduled:361                    gtypes[dp].append(2)362                    phy_cycles[dp].append(self.find_qlocs(inst, cycle_maps[dp]))363            current_map = cycle_maps[-1]364            gn1 = sum([len(gates) for gates in cycles])365            if gn0 != gn1:366                cycle_maps.append(current_map)367                cycles.append([])368                gtypes.append([])369                phy_cycles.append([])370                dp += 1371    372        if gn0 != gn1:373            print('Scheduled cycles', cycles)374            raise ValueError('Scheduled circuit doesnt cover all gates')375        ordered_all_instrs = []376        dressed_instrs = []377        ordered_all_instrs_phy = []378        for c in range(len(cycles)):379            i = len(cycles) - 1 - c380            ordered_all_instrs += cycles[i]381            dressed_instrs += gtypes[i]382            ordered_all_instrs_phy += phy_cycles[i]383        return ordered_all_instrs, dressed_instrs, ordered_all_instrs_phy384        385    def no_routing(self, bench):386        """387        Assuming all-to-all connectivity, only perform scheduling.388        """389        init_routed_instrs = self.pairs390        G0 = self.instruction_graph(init_routed_instrs)391        LG_G0 = nx.line_graph(G0)392        Cl = nx.coloring.greedy_color(LG_G0, strategy="largest_first")393        cmax = max(Cl.values())394        order_g0 = {}395        for c in range(cmax+1): 396            order_g0[c] = []397            for ed in Cl:398                if Cl[ed] == c:399                    order_g0[c].append(self.zz_tuple(ed))400        new_init_routed = []401        for c in order_g0:402            new_init_routed += order_g0[c]403        qc = QuantumCircuit(self.b_qbts, self.b_qbts)404        if self.q1_instrs[0]:405            for gate in self.q1_instrs[0]:406                qc.append(gate[0], [gate[1]])407        for qpair in new_init_routed:408            qc.append(self.instrs[self.zz_tuple(qpair)], qpair)409        410        if self.q1_instrs[1]:411            for gate in self.q1_instrs[1]:412                qc.append(gate[0], [gate[1]])413        return qc414    def construct_circ(self, ordered_all_instrs, dressed_instrs, 415                        ordered_all_instrs_phy, layers=1, msmt=False):416        """Construct the qiskit circuit after compilation, 417        for multiple trotter steps, we only perform compilation for the first layer,418        for odd layers, we use the same circuit as first layer,419        for even layers, we reverse the circuit.420        """421        swap_mat = SwapGate().to_matrix()422        new_circ = QuantumCircuit(self.n_qbits, self.b_qbts)423        if self.q1_instrs[0]:424            for gate in self.q1_instrs[0]:425                new_circ.append(gate[0], [self.init_map[gate[1]]])426        427        zz_id = 0428        dzz = 0429        for l in range(layers):430            for gid in range(len(ordered_all_instrs)):431                if (l%2):432                    gid = len(ordered_all_instrs) - 1 - gid433                qpair = ordered_all_instrs[gid]434                phqs = list(ordered_all_instrs_phy[gid])435                # apply operations on the hardware qubits 436                if dressed_instrs[gid] == 2:437                    new_circ.append(self.instrs[self.zz_tuple(qpair)], phqs)438                    zz_id += 1439                elif dressed_instrs[gid] == 1:440                    u_mat = np.matmul(self.instrs[qpair].to_matrix(), swap_mat)441                    if isinstance(self.instrs[qpair], RZZGate):442                        angle = self.instrs[qpair].params[0]443                        new_circ.unitary(Operator(u_mat), phqs, label='dZZ'+str(angle))444                    else:445                        new_circ.unitary(Operator(u_mat), phqs, label='d'+self.instrs[qpair].name)446                    dzz += 1447                elif dressed_instrs[gid] == 0:448                    new_circ.swap(phqs[0], phqs[1])449                else:450                    raise ValueError('unsupported instruction types')451            if self.maps:452                if (l%2):453                    fnl_vpmap = self.init_map454                else:455                    fnl_vpmap = self.maps[list(self.maps.keys())[-1]]456            else:457                fnl_vpmap = self.init_map458            for gate in self.q1_instrs[1]:459                new_circ.append(gate[0], [fnl_vpmap[gate[1]]])460        if msmt:461            # The measurement outcome goes to classical bits 462            # which are indexed by their virtual qubit indices463            for cv in range(self.b_qbts):464                new_circ.measure(fnl_vpmap[cv], cv)465        return new_circ466    def construct_qaoa(self, ordered_all_instrs, dressed_instrs, ordered_all_instrs_phy, 467                        layers=1, gammas=None, betas=None, msmt=False, init_map=None, maps=None):468        swap_mat = SwapGate().to_matrix()469        new_circ = QuantumCircuit(self.n_qbits, self.b_qbts)470        if self.q1_instrs[0]:471            for gate in self.q1_instrs[0]:472                new_circ.append(gate[0], [init_map[gate[1]]])473        474        zz_id = 0475        dzz = 0476        for l in range(layers):477            for gid in range(len(ordered_all_instrs)):478                if (l%2):479                    gid = len(ordered_all_instrs) - 1 - gid480                qpair = ordered_all_instrs[gid]481                phqs = list(ordered_all_instrs_phy[gid])482                # apply operations on the hardware qubits 483                if dressed_instrs[gid] == 2:484                    # new_circ.append(self.instrs[self.zz_tuple(qpair)], phqs)485                    new_circ.rzz(2*gammas[l], phqs[0], phqs[1])486                    zz_id += 1487                elif dressed_instrs[gid] == 1:488                    rzz_mat = RZZGate(2*gammas[l]).to_matrix()489                    u_mat = np.matmul(rzz_mat, swap_mat)490                    new_circ.unitary(Operator(u_mat), phqs, label='dZZ'+str(2*gammas[l]))491                    dzz += 1492                elif dressed_instrs[gid] == 0:493                    new_circ.swap(phqs[0], phqs[1])494                else:495                    raise ValueError('unsupported instruction types')496            if maps:497                if (l%2):498                    fnl_vpmap = init_map499                else:500                    fnl_vpmap = maps[list(maps.keys())[-1]]501            else:502                fnl_vpmap = init_map503            for gate in self.q1_instrs[1]:504                new_circ.rx(2*betas[l], [fnl_vpmap[gate[1]]])505        if msmt:506            # The measurement outcome goes to classical bits which are indexed 507            # by their virtual qubit indices508            for cv in range(self.b_qbts):509                new_circ.measure(fnl_vpmap[cv], cv)510        return new_circ511    def run_qaoa(self, layers=1, gammas=None, betas=None, msmt='False'):512        self.router()513        ordered_all_instrs, dressed_instrs, ordered_all_instrs_phy = self.scheduler()514        new_circ = self.construct_qaoa(ordered_all_instrs, dressed_instrs, 515                            ordered_all_instrs_phy, layers, gammas, betas, msmt, self.init_map, self.maps)516        return new_circ, (layers*self.swap_count, layers*self.dswap_count)517    def run(self, layers=1, msmt='False'):518        self.router()519        ordered_all_instrs, dressed_instrs, ordered_all_instrs_phy = self.scheduler()520        new_circ = self.construct_circ(ordered_all_instrs, dressed_instrs, 521                                        ordered_all_instrs_phy, layers, msmt)522        return new_circ, (layers*self.swap_count, layers*self.dswap_count)523def locate_min(a):524    smallest = min(a)525    ids = [index for index, element in enumerate(a) if smallest == element]...net_stuff.py
Source:net_stuff.py  
1import socket2import os3def un_routing():4    try:5        vivox_ms = socket.gethostbyname('mpx5j.vivox.com')6        r6s_vcs = socket.gethostbyname('disp-rbswp-5-1.vivox.com')7        r6s_vcs1 = socket.gethostbyname('mphpp-rbswp-5-1-1.vivox.com')8        r6s_vcs2 = socket.gethostbyname('mphpp-rbswp-5-1-2.vivox.com')9        r6s_vcs3 = socket.gethostbyname('mphpp-rbswp-5-1-3.vivox.com')10        r6s_vcs4 = socket.gethostbyname('mphpp-rbswp-5-1-4.vivox.com')11        r6s_vcs5 = socket.gethostbyname('mphpp-rbswp-5-1-5.vivox.com')12        pubg_vcs = socket.gethostbyname('ec2-18-220-247-8.us-east-2.compute.amazonaws.com')13        vxx1 = socket.gethostbyname('mphpp-zmxbp-5-2-5.vivox.com')14        vxx2 = socket.gethostbyname('tst5a.vivox.com')15        vxx3 = socket.gethostbyname('vdisp-plabdev-5-1.vivox.com')16        vxx4 = socket.gethostbyname('nmap.vivox.com')17        vxx5 = socket.gethostbyname('sg-xbd-5-2.vivox.com')18        vxx6 = socket.gethostbyname('vmph-hrpd-5-1.vivox.com')19        vxx7 = socket.gethostbyname('74.201.99.77')  # why im i even trying to get the hostname of those ip's ?20        vxx8 = socket.gethostbyname('74.201.98.0')21        vxx_fn = socket.gethostbyname('disp-fnwp-5-1.vivox.com')22        vxx_fn2 = socket.gethostbyname('mphpp-fnwp-5-1-10.vivox.com')23        vxx_fn3 = socket.gethostbyname('mphpp-fnwp-5-1-11.vivox.com')24        vxx_fn4 = socket.gethostbyname('mphpp-fnwp-5-1-2.vivox.com')25        vxx_fn5 = socket.gethostbyname('mphpp-fnwp-5-1-12.vivox.com')26        vxx_fn6 = socket.gethostbyname('mphpp-fnwp-5-1-3.vivox.com')27        vxx_fn7 = socket.gethostbyname('mphpp-fnwp-5-1-13.vivox.com')28        vxx_fn8 = socket.gethostbyname('mphpp-fnwp-5-1-4.vivox.com')29        vxx_fn9 = socket.gethostbyname('mphpp-fnwp-5-1-14.vivox.com')30        vxx_fn10 = socket.gethostbyname('mphpp-fnwp-5-1-15.vivox.com')31        vxx_fn11 = socket.gethostbyname('mphpp-fnwp-5-1-6.vivox.com')32        vxx_fn12 = socket.gethostbyname('mphpp-fnwp-5-1-7.vivox.com')33        vxx_fn13 = socket.gethostbyname('mphpp-fnwp-5-1-8.vivox.com')34        vxx_fn14 = socket.gethostbyname('mphpp-fnwp-5-1-9.vivox.com')35        vxx_h1Z1 = socket.gethostbyname('disp-h1zp-5-1.vivox.com')36        vxx_h1Z12 = socket.gethostbyname('mphpp-h1zp-5-1-1.vivox.com')37        vxx_h1Z13 = socket.gethostbyname('mphpp-h1zp-5-1-2.vivox.com')38        vxx_h1Z14 = socket.gethostbyname('mphpp-h1zp-5-1-3.vivox.com')39        vxx_h1Z15 = socket.gethostbyname('mphpp-h1zp-5-1-4.vivox.com')40        vxx_h1Z16 = socket.gethostbyname('mphpp-h1zp-5-1-5.vivox.com')41        unroute_master = 'cmdp.exe route delete ' + vivox_ms42        unroute_r6s = 'cmdp.exe route delete ' + r6s_vcs43        unroute_r6s1 = 'cmdp.exe route delete ' + r6s_vcs144        unroute_r6s2 = 'cmdp.exe route delete ' + r6s_vcs245        unroute_r6s3 = 'cmdp.exe route delete ' + r6s_vcs346        unroute_r6s4 = 'cmdp.exe route delete ' + r6s_vcs447        unroute_r6s5 = 'cmdp.exe route delete ' + r6s_vcs548        unroute_pubg = 'cmdp.exe route delete ' + pubg_vcs49        unroute_vxx1 = 'cmdp.exe route delete ' + vxx150        unroute_vxx2 = 'cmdp.exe route delete ' + vxx251        unroute_vxx3 = 'cmdp.exe route delete ' + vxx352        unroute_vxx4 = 'cmdp.exe route delete ' + vxx453        unroute_vxx5 = 'cmdp.exe route delete ' + vxx554        unroute_vxx6 = 'cmdp.exe route delete ' + vxx655        unroute_vxx7 = 'cmdp.exe route delete ' + vxx756        unroute_vxx8 = 'cmdp.exe route delete ' + vxx857        unroute_pubgx = 'cmdp.exe route delete  18.0.0.0/8'58        unroute_fn = 'cmdp.exe route delete ' + vxx_fn59        unroute_fn2 = 'cmdp.exe route delete ' + vxx_fn260        unroute_fn3 = 'cmdp.exe route delete ' + vxx_fn361        unroute_fn4 = 'cmdp.exe route delete ' + vxx_fn462        unroute_fn5 = 'cmdp.exe route delete ' + vxx_fn563        unroute_fn6 = 'cmdp.exe route delete ' + vxx_fn664        unroute_fn7 = 'cmdp.exe route delete ' + vxx_fn765        unroute_fn8 = 'cmdp.exe route delete ' + vxx_fn866        unroute_fn9 = 'cmdp.exe route delete ' + vxx_fn967        unroute_fn10 = 'cmdp.exe route delete ' + vxx_fn1068        unroute_fn11 = 'cmdp.exe route delete ' + vxx_fn1169        unroute_fn12 = 'cmdp.exe route delete ' + vxx_fn1270        unroute_fn13 = 'cmdp.exe route delete ' + vxx_fn1371        unroute_H1Z1 = 'cmdp.exe route delete ' + vxx_h1Z172        unroute_H1Z12 = 'cmdp.exe route delete ' + vxx_h1Z1273        unroute_H1Z13 = 'cmdp.exe route delete ' + vxx_h1Z1374        unroute_H1Z14 = 'cmdp.exe route delete ' + vxx_h1Z1475        unroute_H1Z15 = 'cmdp.exe route delete ' + vxx_h1Z1576        unroute_H1Z16 = 'cmdp.exe route delete ' + vxx_h1Z1677        os.system(unroute_master + '>nul 2>&1')78        os.system(unroute_pubg + '>nul 2>&1')79        os.system(unroute_pubgx + '>nul 2>&1')80        os.system(unroute_r6s + '>nul 2>&1')81        os.system(unroute_r6s1 + '>nul 2>&1')82        os.system(unroute_r6s2 + '>nul 2>&1')83        os.system(unroute_r6s3 + '>nul 2>&1')84        os.system(unroute_r6s4 + '>nul 2>&1')85        os.system(unroute_r6s5 + '>nul 2>&1')86        os.system(unroute_vxx1 + '>nul 2>&1')87        os.system(unroute_vxx2 + '>nul 2>&1')88        os.system(unroute_vxx3 + '>nul 2>&1')89        os.system(unroute_vxx4 + '>nul 2>&1')90        os.system(unroute_vxx5 + '>nul 2>&1')91        os.system(unroute_vxx6 + '>nul 2>&1')92        os.system(unroute_vxx7 + '>nul 2>&1')93        os.system(unroute_vxx8 + '>nul 2>&1')94        os.system(unroute_fn + '>nul 2>&1')95        os.system(unroute_fn2 + '>nul 2>&1')96        os.system(unroute_fn3 + '>nul 2>&1')97        os.system(unroute_fn4 + '>nul 2>&1')98        os.system(unroute_fn5 + '>nul 2>&1')99        os.system(unroute_fn6 + '>nul 2>&1')100        os.system(unroute_fn7 + '>nul 2>&1')101        os.system(unroute_fn8 + '>nul 2>&1')102        os.system(unroute_fn9 + '>nul 2>&1')103        os.system(unroute_fn10 + '>nul 2>&1')104        os.system(unroute_fn11 + '>nul 2>&1')105        os.system(unroute_fn12 + '>nul 2>&1')106        os.system(unroute_fn13 + '>nul 2>&1')107        os.system(unroute_H1Z1 + '>nul 2>&1')108        os.system(unroute_H1Z12 + '>nul 2>&1')109        os.system(unroute_H1Z13 + '>nul 2>&1')110        os.system(unroute_H1Z14 + '>nul 2>&1')111        os.system(unroute_H1Z15 + '>nul 2>&1')112        os.system(unroute_H1Z16 + '>nul 2>&1')113    except socket.error:114        pass115def routing_vvx():116    try:117        IP = socket.gethostbyname(socket.gethostname())118        vivox_ms = socket.gethostbyname('mpx5j.vivox.com')119        vxx1 = socket.gethostbyname('mphpp-zmxbp-5-2-5.vivox.com')120        vxx2 = socket.gethostbyname('tst5a.vivox.com')121        vxx3 = socket.gethostbyname('vdisp-plabdev-5-1.vivox.com')122        vxx4 = socket.gethostbyname('nmap.vivox.com')123        vxx5 = socket.gethostbyname('sg-xbd-5-2.vivox.com')124        vxx6 = socket.gethostbyname('vmph-hrpd-5-1.vivox.com')125        vxx7 = socket.gethostbyname('74.201.99.77')  # why im i even trying to get the hostname of those ip's ?126        vxx8 = socket.gethostbyname('74.201.98.0')127        route_vxx1 = 'cmdp.exe route -p add' + vxx1 + 'mask' + '255.255.255.255' + IP128        route_vxx2 = 'cmdp.exe route -p add' + vxx2 + 'mask' + '255.255.255.255' + IP129        route_vxx3 = 'cmdp.exe route -p add' + vxx3 + 'mask' + '255.255.255.255' + IP130        route_vxx4 = 'cmdp.exe route -p add' + vxx4 + 'mask' + '255.255.255.255' + IP131        route_vxx5 = 'cmdp.exe route -p add' + vxx5 + 'mask' + '255.255.255.255' + IP132        route_vxx6 = 'cmdp.exe route -p add' + vxx6 + 'mask' + '255.255.255.255' + IP133        route_vxx7 = 'cmdp.exe route -p add' + vxx7 + 'mask' + '255.255.255.255' + IP134        route_vxx8 = 'cmdp.exe route -p add' + vxx8 + 'mask' + '255.255.255.255' + IP135        route_master = 'cmdp.exe route -p add' + vivox_ms + 'mask' + '255.255.255.255' + IP136        os.system(route_vxx1 + '>nul 2>&1')137        os.system(route_vxx2 + '>nul 2>&1')138        os.system(route_vxx3 + '>nul 2>&1')139        os.system(route_vxx4 + '>nul 2>&1')140        os.system(route_vxx5 + '>nul 2>&1')141        os.system(route_vxx6 + '>nul 2>&1')142        os.system(route_vxx7 + '>nul 2>&1')143        os.system(route_vxx8 + '>nul 2>&1')144        os.system(route_master + '>nul 2>&1')145    except socket.error:146        pass147def routing_r6():148    try:149        IP = socket.gethostbyname(socket.gethostname())150        r6s_vcs = socket.gethostbyname('disp-rbswp-5-1.vivox.com')151        r6s_vcs1 = socket.gethostbyname('mphpp-rbswp-5-1-1.vivox.com')152        r6s_vcs2 = socket.gethostbyname('mphpp-rbswp-5-1-2.vivox.com')153        r6s_vcs3 = socket.gethostbyname('mphpp-rbswp-5-1-3.vivox.com')154        r6s_vcs4 = socket.gethostbyname('mphpp-rbswp-5-1-4.vivox.com')155        r6s_vcs5 = socket.gethostbyname('mphpp-rbswp-5-1-5.vivox.com')156        route_r6s = 'cmdp.exe route -p add' + r6s_vcs + 'mask' + '255.255.255.255 ' + IP157        route_r6s1 = 'cmdp.exe route -p add' + r6s_vcs1 + 'mask' + '255.255.255.255 ' + IP158        route_r6s2 = 'cmdp.exe route -p add' + r6s_vcs2 + 'mask' + '255.255.255.255 ' + IP159        route_r6s3 = 'cmdp.exe route -p add' + r6s_vcs3 + 'mask' + '255.255.255.255 ' + IP160        route_r6s4 = 'cmdp.exe route -p add' + r6s_vcs4 + 'mask' + '255.255.255.255 ' + IP161        route_r6s5 = 'cmdp.exe route -p add' + r6s_vcs5 + 'mask' + '255.255.255.255 ' + IP162        os.system(route_r6s + '>nul 2>&1')163        os.system(route_r6s1 + '>nul 2>&1')164        os.system(route_r6s2 + '>nul 2>&1')165        os.system(route_r6s3 + '>nul 2>&1')166        os.system(route_r6s4 + '>nul 2>&1')167        os.system(route_r6s5 + '>nul 2>&1')168    except socket.error:169        pass170def routing_r6_console():171    try:172        IP = socket.gethostbyname(socket.gethostname())173        r6s_vcsp = socket.gethostbyname('disp-rbspsp-5-1.vivox.com')174        r6s_vcsp1 = socket.gethostbyname('mphpp-rbspsp-5-1-10.vivox.com')175        r6s_vcsp2 = socket.gethostbyname('mphpp-rbspsp-5-1-1.vivox.com')176        r6s_vcsp3 = socket.gethostbyname('mphpp-rbspsp-5-1-11.vivox.com')177        r6s_vcsp4 = socket.gethostbyname('mphpp-rbspsp-5-1-2.vivox.com')178        r6s_vcsp5 = socket.gethostbyname('mphpp-rbspsp-5-1-12.vivox.com')179        r6s_vcsp6 = socket.gethostbyname('mphpp-rbspsp-5-1-3.vivox.com')180        r6s_vcsp7 = socket.gethostbyname('mphpp-rbspsp-5-1-13.vivox.com')181        r6s_vcsp8 = socket.gethostbyname('mphpp-rbspsp-5-1-4.vivox.com')182        r6s_vcsp9 = socket.gethostbyname('mphpp-rbspsp-5-1-14.vivox.com')183        r6s_vcsp10 = socket.gethostbyname('mphpp-rbspsp-5-1-5.vivox.com')184        r6s_vcsp11 = socket.gethostbyname('mphpp-rbspsp-5-1-15.vivox.com')185        r6s_vcsp12 = socket.gethostbyname('mphpp-rbspsp-5-1-6.vivox.com')186        r6s_vcsp13 = socket.gethostbyname('mphpp-rbspsp-5-1-16.vivox.com')187        r6s_vcsp14 = socket.gethostbyname('mphpp-rbspsp-5-1-7.vivox.com')188        r6s_vcsp15 = socket.gethostbyname('mphpp-rbspsp-5-1-8.vivox.com')189        r6s_vcsp16 = socket.gethostbyname('mphpp-rbspsp-5-1-9.vivox.com')190        r6s_vcsxb = socket.gethostbyname('disp-rbsxbp-5-1.vivox.com')191        r6s_vcsxb1 = socket.gethostbyname('mphpp-rbsxbp-5-1-2.vivox.com')192        r6s_vcsxb2 = socket.gethostbyname('mphpp-rbsxbp-5-1-3.vivox.com')193        r6s_vcsxb3 = socket.gethostbyname('mphpp-rbsxbp-5-1-4.vivox.com')194        r6s_vcsxb4 = socket.gethostbyname('mphpp-rbsxbp-5-1-5.vivox.com')195        r6s_vcsxb5 = socket.gethostbyname('mphpp-rbsxbp-5-1-6.vivox.com')196        r6s_vcsxb6 = socket.gethostbyname('mphpp-rbsxbp-5-1-7.vivox.com')197        r6s_vcsxb7 = socket.gethostbyname('mphpp-rbsxbp-5-1-8.vivox.com')198        route_r6s_vcsp = 'cmdp.exe route -p add' + r6s_vcsp + 'mask' + '255.255.255.255' + IP199        route_r6s_vcsp1 = 'cmdp.exe route -p add' + r6s_vcsp1 + 'mask' + '255.255.255.255' + IP200        route_r6s_vcsp2 = 'cmdp.exe route -p add' + r6s_vcsp2 + 'mask' + '255.255.255.255' + IP201        route_r6s_vcsp3 = 'cmdp.exe route -p add' + r6s_vcsp3 + 'mask' + '255.255.255.255' + IP202        route_r6s_vcsp4 = 'cmdp.exe route -p add' + r6s_vcsp4 + 'mask' + '255.255.255.255' + IP203        route_r6s_vcsp5 = 'cmdp.exe route -p add' + r6s_vcsp5 + 'mask' + '255.255.255.255' + IP204        route_r6s_vcsp6 = 'cmdp.exe route -p add' + r6s_vcsp6 + 'mask' + '255.255.255.255' + IP205        route_r6s_vcsp7 = 'cmdp.exe route -p add' + r6s_vcsp7 + 'mask' + '255.255.255.255' + IP206        route_r6s_vcsp8 = 'cmdp.exe route -p add' + r6s_vcsp8 + 'mask' + '255.255.255.255' + IP207        route_r6s_vcsp9 = 'cmdp.exe route -p add' + r6s_vcsp9 + 'mask' + '255.255.255.255' + IP208        route_r6s_vcsp10 = 'cmdp.exe route -p add' + r6s_vcsp10 + 'mask' + '255.255.255.255' + IP209        route_r6s_vcsp11 = 'cmdp.exe route -p add' + r6s_vcsp11 + 'mask' + '255.255.255.255' + IP210        route_r6s_vcsp12 = 'cmdp.exe route -p add' + r6s_vcsp12 + 'mask' + '255.255.255.255' + IP211        route_r6s_vcsp13 = 'cmdp.exe route -p add' + r6s_vcsp13 + 'mask' + '255.255.255.255' + IP212        route_r6s_vcsp14 = 'cmdp.exe route -p add' + r6s_vcsp14 + 'mask' + '255.255.255.255' + IP213        route_r6s_vcsp15 = 'cmdp.exe route -p add' + r6s_vcsp15 + 'mask' + '255.255.255.255' + IP214        route_r6s_vcsp16 = 'cmdp.exe route -p add' + r6s_vcsp16 + 'mask' + '255.255.255.255' + IP215        route_r6s_vcxb = 'cmdp.exe route -p add' + r6s_vcsxb + 'mask' + '255.255.255.255' + IP216        route_r6s_vcxb1 = 'cmdp.exe route -p add' + r6s_vcsxb1 + 'mask' + '255.255.255.255' + IP217        route_r6s_vcxb2 = 'cmdp.exe route -p add' + r6s_vcsxb2 + 'mask' + '255.255.255.255' + IP218        route_r6s_vcxb3 = 'cmdp.exe route -p add' + r6s_vcsxb3 + 'mask' + '255.255.255.255' + IP219        route_r6s_vcxb4 = 'cmdp.exe route -p add' + r6s_vcsxb4 + 'mask' + '255.255.255.255' + IP220        route_r6s_vcxb5 = 'cmdp.exe route -p add' + r6s_vcsxb5 + 'mask' + '255.255.255.255' + IP221        route_r6s_vcxb6 = 'cmdp.exe route -p add' + r6s_vcsxb6 + 'mask' + '255.255.255.255' + IP222        route_r6s_vcxb7 = 'cmdp.exe route -p add' + r6s_vcsxb7 + 'mask' + '255.255.255.255' + IP223        os.system(route_r6s_vcsp + '>nul 2>&1')224        os.system(route_r6s_vcsp1 + '>nul 2>&1')225        os.system(route_r6s_vcsp2 + '>nul 2>&1')226        os.system(route_r6s_vcsp3 + '>nul 2>&1')227        os.system(route_r6s_vcsp4 + '>nul 2>&1')228        os.system(route_r6s_vcsp5 + '>nul 2>&1')229        os.system(route_r6s_vcsp6 + '>nul 2>&1')230        os.system(route_r6s_vcsp7 + '>nul 2>&1')231        os.system(route_r6s_vcsp8 + '>nul 2>&1')232        os.system(route_r6s_vcsp10 + '>nul 2>&1')233        os.system(route_r6s_vcsp11 + '>nul 2>&1 ')234        os.system(route_r6s_vcsp12 + '>nul 2>&1')235        os.system(route_r6s_vcsp13 + '>nul 2>&1')236        os.system(route_r6s_vcsp14 + '>nul 2>&1')237        os.system(route_r6s_vcsp15 + '>nul 2>&1')238        os.system(route_r6s_vcsp16 + '>nul 2>&1')239        os.system(route_r6s_vcxb + '>nul 2>&1')240        os.system(route_r6s_vcxb1 + '>nul 2>&1')241        os.system(route_r6s_vcxb2 + '>nul 2>&1')242        os.system(route_r6s_vcxb3 + '>nul 2>&1')243        os.system(route_r6s_vcxb4 + '>nul 2>&1')244        os.system(route_r6s_vcxb5 + '>nul 2>&1')245        os.system(route_r6s_vcxb6 + '>nul 2>&1')246        os.system(route_r6s_vcxb7 + '>nul 2>&1')247    except socket.error:248        pass249def routing_pubg():250    try:251        IP = socket.gethostbyname(socket.gethostname())252        route_pubgx = 'cmdp.exe route -p add 18.0.0.0/8 ' + IP253        os.system(route_pubgx + '>nul 2>&1')254    except socket.error:255        pass256def routing_fortnite():257    try:258        IP = socket.gethostbyname(socket.gethostname())259        vxx_fn = socket.gethostbyname('disp-fnwp-5-1.vivox.com')260        vxx_fn2 = socket.gethostbyname('mphpp-fnwp-5-1-10.vivox.com')261        vxx_fn3 = socket.gethostbyname('mphpp-fnwp-5-1-11.vivox.com')262        vxx_fn4 = socket.gethostbyname('mphpp-fnwp-5-1-2.vivox.com')263        vxx_fn5 = socket.gethostbyname('mphpp-fnwp-5-1-12.vivox.com')264        vxx_fn6 = socket.gethostbyname('mphpp-fnwp-5-1-3.vivox.com')265        vxx_fn7 = socket.gethostbyname('mphpp-fnwp-5-1-13.vivox.com')266        vxx_fn8 = socket.gethostbyname('mphpp-fnwp-5-1-4.vivox.com')267        vxx_fn9 = socket.gethostbyname('mphpp-fnwp-5-1-14.vivox.com')268        vxx_fn10 = socket.gethostbyname('mphpp-fnwp-5-1-15.vivox.com')269        vxx_fn11 = socket.gethostbyname('mphpp-fnwp-5-1-6.vivox.com')270        vxx_fn12 = socket.gethostbyname('mphpp-fnwp-5-1-7.vivox.com')271        vxx_fn13 = socket.gethostbyname('mphpp-fnwp-5-1-8.vivox.com')272        vxx_fn14 = socket.gethostbyname('mphpp-fnwp-5-1-9.vivox.com')273        route_fn = 'cmdp.exe route -p add' + vxx_fn + 'mask' + '255.255.255.255' + IP274        route_fn2 = 'cmdp.exe route -p add' + vxx_fn2 + 'mask' + '255.255.255.255' + IP275        route_fn3 = 'cmdp.exe route -p add' + vxx_fn3 + 'mask' + '255.255.255.255' + IP276        route_fn4 = 'cmdp.exe route -p add' + vxx_fn4 + 'mask' + '255.255.255.255' + IP277        route_fn5 = 'cmdp.exe route -p add' + vxx_fn5 + 'mask' + '255.255.255.255' + IP278        route_fn6 = 'cmdp.exe route -p add' + vxx_fn6 + 'mask' + '255.255.255.255' + IP279        route_fn7 = 'cmdp.exe route -p add' + vxx_fn7 + 'mask' + '255.255.255.255' + IP280        route_fn8 = 'cmdp.exe route -p add' + vxx_fn8 + 'mask' + '255.255.255.255' + IP281        route_fn9 = 'cmdp.exe route -p add' + vxx_fn9 + 'mask' + '255.255.255.255' + IP282        route_fn10 = 'cmdp.exe route -p add' + vxx_fn10 + 'mask' + '255.255.255.255' + IP283        route_fn11 = 'cmdp.exe route -p add' + vxx_fn11 + 'mask' + '255.255.255.255' + IP284        route_fn12 = 'cmdp.exe route -p add' + vxx_fn12 + 'mask' + '255.255.255.255' + IP285        route_fn13 = 'cmdp.exe route -p add' + vxx_fn13 + 'mask' + '255.255.255.255' + IP286        route_fn14 = 'cmdp.exe route -p add' + vxx_fn14 + 'mask' + '255.255.255.255' + IP287        os.system(route_fn + '>nul 2>&1')288        os.system(route_fn2 + '>nul 2>&1')289        os.system(route_fn3 + '>nul 2>&1')290        os.system(route_fn4 + '>nul 2>&1')291        os.system(route_fn5 + '>nul 2>&1')292        os.system(route_fn6 + '>nul 2>&1')293        os.system(route_fn7 + '>nul 2>&1')294        os.system(route_fn8 + '>nul 2>&1')295        os.system(route_fn9 + '>nul 2>&1')296        os.system(route_fn10 + '>nul 2>&1')297        os.system(route_fn11 + '>nul 2>&1')298        os.system(route_fn12 + '>nul 2>&1 ')299        os.system(route_fn13 + '>nul 2>&1')300        os.system(route_fn14 + '>nul 2>&1')301    except socket.error:302        pass303def routing_h1z1():304    try:305        IP = socket.gethostbyname(socket.gethostname())306        vxx_h1Z1 = socket.gethostbyname('disp-h1zp-5-1.vivox.com')307        vxx_h1Z12 = socket.gethostbyname('mphpp-h1zp-5-1-1.vivox.com')308        vxx_h1Z13 = socket.gethostbyname('mphpp-h1zp-5-1-2.vivox.com')309        vxx_h1Z14 = socket.gethostbyname('mphpp-h1zp-5-1-3.vivox.com')310        vxx_h1Z15 = socket.gethostbyname('mphpp-h1zp-5-1-4.vivox.com')311        vxx_h1Z16 = socket.gethostbyname('mphpp-h1zp-5-1-5.vivox.com')312        route_h1z1 = 'cmdp.exe route -p add' + vxx_h1Z1 + 'mask' + '255.255.255.255' + IP313        route_h1z12 = 'cmdp.exe route -p add' + vxx_h1Z12 + 'mask' + '255.255.255.255' + IP314        route_h1z13 = 'cmdp.exe route -p add' + vxx_h1Z13 + 'mask' + '255.255.255.255' + IP315        route_h1z14 = 'cmdp.exe route -p add' + vxx_h1Z14 + 'mask' + '255.255.255.255' + IP316        route_h1z15 = 'cmdp.exe route -p add' + vxx_h1Z15 + 'mask' + '255.255.255.255' + IP317        route_h1z16 = 'cmdp.exe route -p add' + vxx_h1Z16 + 'mask' + '255.255.255.255' + IP318        os.system(route_h1z1 + '>nul 2>&1')319        os.system(route_h1z12 + '>nul 2>&1')320        os.system(route_h1z13 + '>nul 2>&1')321        os.system(route_h1z14 + '>nul 2>&1')322        os.system(route_h1z15 + '>nul 2>&1')323        os.system(route_h1z16 + '>nul 2>&1')324    except socket.error:325        pass326def dns_setup():327    try:328        os.system('netsh interface ip set dns name="Ethernet 2" source=static addr=none >nul 2>&1')329        os.system('netsh interface ip add dns name="Ethernet 2" addr=8.8.8.8 index=1 >nul 2>&1')330        os.system('netsh interface ip add dns name="Ethernet 2" addr=8.8.4.4 index=2 >nul 2>&1')331        os.system('netsh interface ip set dns name="Ethernet 1" source=static addr=none >nul 2>&1')332        os.system('netsh interface ip add dns name="Ethernet 1" addr=8.8.8.8 index=1 >nul 2>&1')333        os.system('netsh interface ip add dns name="Ethernet 1" addr=8.8.4.4 index=2  >nul 2>&1')334        os.system('netsh interface ip set dns name="Ethernet" source=static addr=none >nul 2>&1')335        os.system('netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=1 >nul 2>&1')336        os.system('netsh interface ip add dns name="Ethernet" addr=8.8.4.4 index=2 >nul 2>&1')337        os.system('netsh interface ip set dns name="Wi-Fi" source=static addr=none >nul 2>&1')338        os.system('netsh interface ip add dns name="Wi-Fi" addr=8.8.8.8 index=1 >nul 2>&1')339        os.system('netsh interface ip add dns name="Wi-Fi" addr=8.8.4.4 index=2 >nul 2>&1')340        os.system('netsh interface ip set dns name="Local Area Connection" source=static addr=none >nul 2>&1')341        os.system('netsh interface ip add dns name="Local Area Connection" addr=8.8.8.8 index=1 >nul 2>&1')342        os.system('netsh interface ip add dns name="Local Area Connection" addr=8.8.4.4 index=2 >nul 2>&1')343        os.system('netsh interface ip set dns name="Wireless Network Connection" source=static addr=none >nul 2>&1')344        os.system('netsh interface ip add dns name="Wireless Network Connection" addr=8.8.8.8 index=1 >nul 2>&1')345        os.system('netsh interface ip add dns name="Wireless Network Connection" addr=8.8.4.4 index=2 >nul 2>&1')346        os.system('netsh interface ip set dns name="Wi-Fi" source=static addr=none >nul 2>&1')347        os.system('netsh interface ip add dns name="Wi-Fi" addr=8.8.8.8 index=1 >nul 2>&1')348        os.system('netsh interface ip add dns name="Wi-Fi" addr=8.8.4.4 index=2 >nul 2>&1')349    except socket.error:...Manipulation.py
Source:Manipulation.py  
...185      self.body.orientation.unroute ( self.springPosition )186      self.springForce.unroute ( self.bodyForce )187      self.body.linearVelocity.unroute ( self.dampForce )188      self.dampForce.unroute ( self.bodyForce )189      self.bodyForce.unroute( self.manipulationBody.forces )190      self.body.angularVelocity.unroute ( self.dampTorque )191      self.dampTorque.unroute ( self.bodyTorque )192      self.bodyTorque.unroute ( self.body.torques )193194      # Remove all forces on the body195      self.body.forces.clear()196      self.body.torques.clear()197198      # Unroute and erase the graphical representation of manipulation199      self.springPosition.unroute ( self.jointSphere.translation )200      self.rootTrans.children.erase ( self.jointSphere )201      self.jointSphere= None202      self.springPosition.unroute ( self.drawLine )203      self.manipulationPoint.unroute ( self.drawLine )
...clos_routing.py
Source:clos_routing.py  
1#   Clos Routing and Generation2    3import math4import random5import sys6def route_clos(routing_vector,I,k,N):7    #In the current tested state, I+N must be divisible by k8    if (((I+N) % k) != 0):9        print "\n\n--------------  ERROR -----------------------"10        print "In the current state, only values for I and N" + \11              " are supported where I+N is dividable by k"12        print  "---------------------------------------------"13        sys.exit(0)14    #set the count of first stage crossbars15    P = int(math.ceil( (N + I)/ k))16    #first stage: r=P crossbars with k=m input pins17    #this list describe the mapping, between the output pins of this stage18    #and the interconnection block input pins.19    #so we have P lists in this list with k outputs containing the20    #input pin number21    stage1 = [[-1 for j in range(k)] for i in range(P)]22    #second stage: m=k crossbars with P=r input pins23    stage2 = [[-1 for j in range(N)] for i in range(k)]24    #tracks if the routing algo was sucessfully25    success = 026    #a count to prevent infinite loops.27    #break up the outer loop and ends the routing trial.28    #TODO: implement a configurable globs.params variable for this supercount.29    supercount = 030    while success is 0 and supercount < 180:31        supercount = supercount +132        #just a change of the representation of the routing vector.33        #make a list for every used pin of the clos network input,34        #append the ble index for this pin.35        # give the relation: input pin -> ble number36        in_to_out = [[] for i in range(I+N)]37        #len routing vector is N. the number of bles.38        for bleIndex in range(len(routing_vector)):39            #the source pin number.40            #pin is referred to the clos network input pin number41            for pin in routing_vector[bleIndex]:42                #skip unrouted pins43                if (pin != -1):44                    in_to_out[pin].append(bleIndex)45        unrouted = []46        #made a list 0,1,2,.. to I+N47        #describe the input pin numbers in a sequence we try to route them48        #start with the first number and end with the last.49        #therefore we will shuffle this index list.50        pins_to_route = [ i for i in range(len(in_to_out))]51        #permute the list52        random.shuffle(pins_to_route)53        #a counter to prevent infinite loops54        count = 055        #permute the target ble numbers of a pin56        for pin in pins_to_route:57            random.shuffle(in_to_out[pin])58        while success is 0 and count < 80:59            #a list of unrouted input pins.60            #format : list of tuple (input pin number, target ble index)61            unrouted = []62            count = count + 163            #a list 0,1,2 ... to k=m64            #describe the output pin numbers of a crossbar65            nlist = [ i for i in range(k)]66            random.shuffle(nlist)67            #the last try to route was not successfull. maybe we try it in an other sequence68            #therefore we shuffle the index list of pins we want try to route.69            random.shuffle(pins_to_route)70            #try to route the input pins step by step71            for pin in pins_to_route :72                #get the crossbar number of the first stage for this input pin73                x1 = int(pin/k)74                #get the targeted ble index numbers75                for dest in in_to_out[pin]:76                    #index of the output pin of the first stage crossbar, used for the routing77                    #only set when the complete routing trough both stages was successful78                    s1 = -179                    #index of the output pin of the second stage crossbar, used for the routing80                    s2 = -181                    #try to find a free output pin of the first stage crossbar to route the track82                    for i in nlist:83                        #remember: x1 is the crossbar number of the first stage.84                        # i is the output pin of this crossbar85                        # pin is the input pin number86                        #dest is the targeted ble number87                        #output of the first stage crossbar is free or already used for that pin88                        if stage1[x1][i] is -1 or stage1[x1][i] is pin: #unused89                            #see if this will route to desired mux90                            #the output pin of the corresponding second stage crossbar is free91                            # or already used for this pin92                            if stage2[i][dest] is -1 or stage2[i][dest] is pin: #unused93                                #this two output pins of both crossbars are used for the94                                #given input pin. save this input pin number95                                stage1[x1][i] = pin96                                stage2[i][dest] = pin97                                #variable to track if this ble was not routable98                                s1 = i99                                s2 = dest100                                break101                    #there was no possible output pin in the first or second stage crossbar102                    #to route this input pin number103                    if s1 is -1:104                        #save this unrouted pin together with the dest ble index105                        unrouted.append((pin, dest))106            pins_to_route = []107            #all pin have been routed108            if len(unrouted) is 0:109                success = 1110            #there are unrouted input pins111            for pin, dest in unrouted:112                #rip up other routed pins to make room113                for iterations in range(1 + int(count/20)): #unroute more pins as we do more iterations114                    pin_to_unroute = -1115                    #select the first stage crossbar of the unrouted input pin116                    x1 = int(pin/k)117                    # build a list from [ 0,1 to k-1]118                    #the outputs indexes of the crossbar we want to throw out some tracks119                    nlist = [ i for i in range(k)]120                    random.shuffle(nlist)121                    #this branch paste -1 in the unroute list. beaks the algo122                    #if random.random() < 0.6:123                    #   for i in nlist:124                    #       #the stage 2 crossbars output pin to the dest ble is not used125                    #       #so we have an input pin number we want to unroute126                    #       if stage2[i][dest] is -1:127                    #           pin_to_unroute = stage1[x1][i]128                    #           break129                    #just take a random input pin to reroute. should be I+N130                    #if random.random() < 0.06:131                    #   pin_to_unroute = random.choice(range(I+N))132                    pin_to_unroute = random.choice(range(I+N))133                    #there are still unrouted pins but no selection of pins to unroute134                    #take one random crossbar of the second stage135                    #and select the pin which leads to the dest ble136                    #can also break the algo through -1137                    #if pin_to_unroute < 0:138                    #   pin_to_unroute = stage2[random.randint(0,k-1)][dest]139                    #we have selected an input pin to reroute but we must140                    #cancel the routings in the crossbars for this pin141                    for i in range(P):142                        for j in range(k):143                            if stage1[i][j] is pin_to_unroute:144                                stage1[i][j] = -1145                    for i in range(k):146                        for j in range(N):147                            if stage2[i][j] is pin_to_unroute:148                                stage2[i][j] = -1149                    #now we can append the unrouted pin in the route todo list150                    pins_to_route.append(pin_to_unroute)151                #also append the still not routed pin152                pins_to_route.append(pin)153    if success is 0:154        print "\n\n--------------  ERROR -----------------------"155        print 'Routing Algo was not able to route the network'156        print "---------------------------------------------"157        sys.exit(0)...test_routing.py
Source:test_routing.py  
1# Copyright (C) 2018 Cuckoo Foundation.2# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org3# See the file 'docs/LICENSE' for copying permission.4import mock5import os6import pytest7import shutil8import tempfile9from cuckoo.common import abstracts10from cuckoo.common import config11from cuckoo.core.database import Database12from cuckoo.core.init import write_cuckoo_conf13from cuckoo.main import cuckoo_create14from cuckoo.misc import set_cwd15from cuckoo.common.routing import Route16class FakeTask(object):17    def __init__(self, options={}):18        self.id = 119        self.options = {} or options20class FakeMachine(object):21    def __init__(self):22        self.name = "machine1"23        self.label = "machine1"24        self.ip = "192.168.56.10"25        self.platform = "windows"26        self.options = ""27        self.interface = "tap0"28        self.snapshot = ""29        self.resultserver_ip = "192.168.56.1"30        self.resultserver_port = 424231        self.manager = "virtualbox"32        self.locked = True33class TestRoute(object):34    def setup(self):35        self.cwd = tempfile.mkdtemp()36        set_cwd(self.cwd)37        cuckoo_create()38        self.db = Database()39        self.db.connect()40    def teardown(self):41        if os.path.isdir(self.cwd):42            shutil.rmtree(self.cwd)43    @mock.patch("cuckoo.common.routing.rooter")44    def test_route_network_none(self, mr):45        route = Route(FakeTask(), FakeMachine())46        route.route_network()47        assert route.route == "none"48        assert route.interface is None49        assert route.rt_table is None50        mr.assert_not_called()51    @mock.patch("cuckoo.common.routing.rooter")52    def test_route_network_inetsim(self, mr):53        route = Route(FakeTask(options={"route": "inetsim"}), FakeMachine())54        route.route_network()55        assert route.route == "inetsim"56        assert route.interface is None57        assert route.rt_table is None58        mr.assert_called_once_with(59            "inetsim_enable", "192.168.56.10", "192.168.56.1",60            "vboxnet0", "2042", ""61        )62    @mock.patch("cuckoo.common.routing.rooter")63    def test_route_network_internet(self, mr):64        write_cuckoo_conf(cfg={65            "routing": {66                "routing": {67                    "internet": "eth0"68                }69            }70        })71        # Clear config cache so it will load new values72        config._cache = {}73        mr.return_value = True74        route = Route(FakeTask(options={"route": "internet"}), FakeMachine())75        route.route_network()76        assert route.route == "internet"77        assert route.interface == "eth0"78        assert route.rt_table == "main"79        mr.assert_has_calls([80            mock.call("nic_available", "eth0"),81            mock.call("drop_enable", "192.168.56.10", "192.168.56.1", "2042"),82            mock.call("forward_enable", "tap0", "eth0", "192.168.56.10"),83            mock.call("srcroute_enable", "main", "192.168.56.10")84        ])85    @mock.patch("cuckoo.common.routing.rooter")86    def test_route_network_tor(self, mr):87        route = Route(FakeTask(options={"route": "tor"}), FakeMachine())88        route.route_network()89        assert route.route == "tor"90        assert route.interface is None91        assert route.rt_table is None92        mr.assert_called_once_with(93            "proxy_enable", "192.168.56.10", "192.168.56.1", "5353", "9040"94        )95    @mock.patch("cuckoo.common.routing.rooter")96    def test_route_network_drop(self, mr):97        route = Route(FakeTask(options={"route": "drop"}), FakeMachine())98        route.route_network()99        assert route.route == "drop"100        assert route.interface is None101        assert route.rt_table is None102        mr.assert_called_once_with(103            "drop_enable", "192.168.56.10", "192.168.56.1", "2042"104        )105    @mock.patch("cuckoo.common.routing.rooter")106    def test_route_network_socks5(self, mr):107        write_cuckoo_conf(cfg={108            "auxiliary": {109                "redsocks": {110                    "enabled": True111                }112            }113        })114        route = Route(FakeTask(options={115            "route": "socks5",116            "socks5.localport": 4242117        }), FakeMachine())118        route.route_network()119        assert route.route == "socks5"120        assert route.interface is None121        assert route.rt_table is None122        mr.assert_called_once_with(123            "proxy_enable", "192.168.56.10", "192.168.56.1", "53", "4242"124        )125    @mock.patch("cuckoo.common.routing.rooter")126    def test_route_network_socks5_disabled(self, mr):127        write_cuckoo_conf(cfg={128            "auxiliary": {129                "redsocks": {130                    "enabled": False131                }132            }133        })134        route = Route(FakeTask(options={135            "route": "socks5",136            "socks5.localport": 4242137        }), FakeMachine())138        route.route_network()139        assert route.route == "none"140        assert route.task.options["route"] is "none"141        assert route.interface is None142        assert route.rt_table is None143        mr.assert_not_called()144    @mock.patch("cuckoo.common.routing.rooter")145    def test_route_network_socks5_noport(self, mr):146        write_cuckoo_conf(cfg={147            "auxiliary": {148                "redsocks": {149                    "enabled": True150                }151            }152        })153        route = Route(FakeTask(options={154            "route": "socks5"155        }), FakeMachine())156        route.route_network()157        assert route.route == "none"158        assert route.task.options["route"] is "none"159        assert route.interface is None160        assert route.rt_table is None161        mr.assert_not_called()162    @mock.patch("cuckoo.common.routing.rooter")163    def test_route_network_vpn(self, mr):164        mr.return_value = True165        route = Route(FakeTask(options={"route": "vpn0"}), FakeMachine())166        route.route_network()167        assert route.route == "vpn0"168        assert route.interface == "tun0"169        assert route.rt_table == "tun0"170        mr.assert_has_calls([171            mock.call("nic_available", "tun0"),172            mock.call("forward_enable", "tap0", "tun0", "192.168.56.10"),173            mock.call("srcroute_enable", "tun0", "192.168.56.10")174        ])175    @mock.patch("cuckoo.common.routing.rooter")176    def test_unroute_network_none(self, mr):177        route = Route(FakeTask(), FakeMachine())178        route.route = "none"179        route.unroute_network()180        mr.assert_not_called()181    @mock.patch("cuckoo.common.routing.rooter")182    def test_unroute_network_vpn(self, mr):183        route = Route(FakeTask(), FakeMachine())184        route.route = "vpn0"185        route.unroute_network()186        route.rt_table = "tun0"187        route.interface = "tun0"188        route.unroute_network()189        mr.assert_has_calls([190            mock.call("forward_disable", "tap0", "tun0", "192.168.56.10"),191            mock.call("srcroute_disable", "tun0", "192.168.56.10"),192        ])193    @mock.patch("cuckoo.common.routing.rooter")194    def test_unroute_network_inetsim(self, mr):195        route = Route(FakeTask(), FakeMachine())196        route.route = "inetsim"197        route.unroute_network()198        mr.assert_has_calls([199            mock.call(200                "inetsim_disable", "192.168.56.10", "192.168.56.1", "vboxnet0",201                "2042", ""202            )203        ])204    @mock.patch("cuckoo.common.routing.rooter")205    def test_unroute_network_tor(self, mr):206        route = Route(FakeTask(), FakeMachine())207        route.route = "tor"208        route.unroute_network()209        mr.assert_has_calls([210            mock.call(211                "proxy_disable", "192.168.56.10", "192.168.56.1", "5353",212                "9040"213            )214        ])215    @mock.patch("cuckoo.common.routing.rooter")216    def test_unroute_network_socks5(self, mr):217        route = Route(FakeTask(), FakeMachine())218        route.route = "socks5"219        route.task.options["socks5.localport"] = 4242220        route.unroute_network()221        mr.assert_has_calls([222            mock.call(223                "proxy_disable", "192.168.56.10", "192.168.56.1", "53",224                "4242",225            )...test_route.py
Source:test_route.py  
...10    # can be done in context manager11    alice.goto('/tests/')12    alice.route('**/status', handler)13    alice.click('tbody tr >> nth=0 >> .passBtn')14    alice.unroute('**/status', handler)15    assert request_data[0] == {'status': 'PASS'}16def test_fulfill(alice):17    def handler(route: Route, request: Request):18        route.fulfill(status=200, body=json.dumps({"runId": 1000}))19    # can be done in context manager20    alice.goto('/tests/')21    alice.click('tbody tr >> nth=0 >> .passBtn')22    alice.route('**/status', handler)23    alice.click('tbody tr >> nth=0 >> .failBtn')24    alice.unroute('**/status', handler)25    alice.reload()26    assert alice.text_content('.testRow_1 .ttStatus') == 'PASS'27def test_modify(alice):28    def handler(route: Route, request: Request):29        route.continue_(post_data=json.dumps({'status': 'FAIL'}))30    # can be done in context manager31    alice.goto('/tests/')32    alice.route('**/status', handler)33    alice.click('tbody tr >> nth=0 >> .passBtn')34    alice.unroute('**/status', handler)35    alice.reload()36    assert alice.text_content('.testRow_1 .ttStatus') == 'FAIL'37def test_abort(alice):38    def handler(route: Route, request: Request):39        route.abort()40    # can be done in context manager41    alice.goto('/tests/')42    alice.click('.testRow_1 .passBtn')43    alice.route('**/status', handler)44    alice.click('tbody tr >> nth=0 >> .failBtn')45    alice.unroute('**/status', handler)46    alice.reload()...Using AI Code Generation
1const { unroute } = require('@playwright/test/lib/server/routes');2const { chromium } = require('playwright');3(async () => {4  const browser = await chromium.launch({ headless: false });5  const context = await browser.newContext();6  const page = await context.newPage();7  unroute(page);8  await browser.close();9})();10const { chromium } = require('playwright');11(async () => {12  const browser = await chromium.launch({ headless: false });13  const context = await browser.newContext();14  const page = await context.newPage();15  const requests = await page.waitForRequest(request => request.url().includes('google'));16  await browser.close();17})();18const { chromium } = require('playwright');19(async () => {20  const browser = await chromium.launch({ headless: false });21  const context = await browser.newContext();22  const page = await context.newPage();23  const responses = await page.waitForResponse(response => response.url().includes('google'));24  await browser.close();25})();26const { chromium } = require('playwright');27(async () => {28  const browser = await chromium.launch({ headless: false });29  const context = await browser.newContext();30  const page = await context.newPage();31  const event = await page.waitForEvent('framenavigated');Using AI Code Generation
1const { unroute } = require("playwright/lib/server/network");2const { chromium } = require("playwright");3(async () => {4  const browser = await chromium.launch();5  const context = await browser.newContext();6  const page = await context.newPage();7  const route = await page.route("**", (route, request) => {8    console.log(request.url());9    route.continue();10  });11  unroute(route);12  await browser.close();13})();14const { route } = require("playwright/lib/server/network");15const { chromium } = require("playwright");16(async () => {17  const browser = await chromium.launch();18  const context = await browser.newContext();19  const page = await context.newPage();20  const route = await page.route("**", (route, request) => {21    console.log(request.url());22    route.continue({23      headers: {24        ...request.headers(),25      },26    });27  });28  unroute(route);29  await browser.close();30})();31const { route } = require("playwright/lib/server/network");32const { chromium } = require("playwright");Using AI Code Generation
1const { unroute } = require('playwright/lib/server/routes');2const { route } = require('playwright/lib/server/routes');3const { chromium } = require('playwright');4const { expect } = require('chai');5(async () => {6  const browser = await chromium.launch({ headless: false });7  const context = await browser.newContext();8  const page = await context.newPage();9  unroute('**/complete/search');10  route('**/complete/search', (req, res) => {11    res.statusCode = 200;12    res.end(JSON.stringify(['hello', 'world']));13  });14  await page.fill('input[name="q"]', 'hello world');15  const suggestions = await page.$$eval('#sbtc .sbl1', (items) =>16    items.map((item) => item.textContent),17  );18  expect(suggestions).to.deep.equal(['hello', 'world']);19  await browser.close();20})();Using AI Code Generation
1const { unroute } = require('@playwright/test/lib/server/routes');2const { test } = require('@playwright/test');3test.describe('My test suite', () => {4  test.beforeEach(async ({ page }) => {5  });6  test('My test', async ({ page }) => {7    await unroute(page);8  });9});Using AI Code Generation
1const { unroute } = require('@playwright/test');2const { test } = require('@playwright/test');3test('test', async ({ page }) => {4  unroute('**');5});6const { test } = require('@playwright/test');7test('test', async ({ page }) => {8  await page.route('**', (route) => {9    const headers = route.request.headers();10    headers['user-agent'] = 'My User Agent';11    route.continue({ headers });12  });13});14const { test } = require('@playwright/test');15test('test', async ({ page }) => {16  await page.route('**', (route) => {17    route.fulfill({18    });19  });20});Using AI Code Generation
1const { unroute } = require('@playwright/test/lib/server/routes');2const { test, expect } = require('@playwright/test');3test('test', async ({ page }) => {4  unroute();5  unroute('**/foo');6  unroute('**/foo', 'POST');7  unroute('**/foo', 'POST', (route, request) => {});8});Using AI Code Generation
1const { unroute } = require('@playwright/test/lib/server/routes');2const { test, expect } = require('@playwright/test');3test('unroute', async ({ page }) => {4  unroute();5  unroute('**/foo');6  unroute('**/foo', 'POST');7  unroute({ url: '**/foo', method: 'POST' });8  unroute({ url: '**/foo', method: 'POST' }, { url: '**/bar', method: 'POST' });9  unroute({ url: '**/foo', method: 'POST' }, { url: '**/bar', method: 'POST' }, { url: '**/baz', method: 'POST' });10  unroute([{ url: '**/foo', method: 'POST' }, { url: '**/bar', method: 'POST' }, { url: '**/baz', method: 'POST' }]);11});12const { unroute } = require('@playwright/test/lib/server/routes');13const { test, expect } = require('@playwright/test');14test('unroute', async ({ page }) => {15  unroute();16  unroute('**/foo');17  unroute('**/foo', 'POST');18  unroute({ url: '**/foo', method: 'POST' });19  unroute({ url: '**/foo', method: 'POST' }, { url: '**/bar', method: 'POST' });20  unroute({ url: '**/foo', method: 'POST' }, { url: '**/bar', method: 'POST' }, { url: '**/baz', methodLambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
