def sort_pickable_dis(sub_bricks,model,tool=0): sub_que = copy.deepcopy(sub_bricks) pick = 0 npick = len(sub_bricks)-1 pickable = 0 for i in range(0,len(sub_bricks)): # sort bricks into pickable and non-pickable, boundary given by result of 'pick' constraints = fd.brick_constraints(sub_bricks[i],model) if sub_bricks[i]['b']==0: flag = 0 if tool == 1: # -----------------comment out for flexible assembly------------------- for j in range(0,len(pick_masks[2])): # if constraints allow a tool pick... if constraints&pick_masks[2][j][0] == 0: sub_que[pick] = copy.deepcopy(sub_bricks[i]) flag = 1 # --------------------------------------------------------------------- for j in range(0,len(pick_masks[0])): # if constraints allow a normal pick... if constraints&pick_masks[0][j][0] == 0: sub_que[pick] = copy.deepcopy(sub_bricks[i]) flag = 1 if flag == 1: pick += 1 else: # if not pickable... sub_que[npick] = copy.deepcopy(sub_bricks[i]) npick -= 1 elif sub_bricks[i]['b']==1: flag = 0 if tool == 1: # -----------------comment out for flexible assembly------------------- for j in range(0,len(pick_masks[3])): # if constraints allow a tool pick... if constraints&pick_masks[3][j][0] == 0: sub_que[pick] = copy.deepcopy(sub_bricks[i]) flag = 1 # --------------------------------------------------------------------- for j in range(0,len(pick_masks[1])): # if constraints allow a 2x2 pick... if constraints&pick_masks[1][j][0] == 0: sub_que[pick] = copy.deepcopy(sub_bricks[i]) flag = 1 if flag == 1: pick += 1 else: # if not pickable... sub_que[npick] = copy.deepcopy(sub_bricks[i]) npick -= 1 sub_groups = pick if npick == len(sub_bricks)-1: pickable = 1 #print 'pickable: ' #for i in range(0,sub_groups): # print sub_que[i]['x']+sub_que[i]['px'],', ',sub_que[i]['y']+sub_que[i]['py'] #print 'not pickable: ' #for i in range(sub_groups,len(sub_que)): # print sub_que[i]['x']+sub_que[i]['px'],', ',sub_que[i]['y']+sub_que[i]['py'] return sub_groups, sub_que, pickable
def list_picking(bricks, model): updated_model = list(model) P = 1 k = len(bricks) for i in range(0, k): constraints = fd.brick_constraints(bricks[i], updated_model) bricks[i] = dict(update_placing(bricks[i], constraints)) updated_model = fd.update_model(bricks[i:i + 1], updated_model) return bricks
def sort_placeable_ass(sub_bricks, model): sub_que = copy.deepcopy(sub_bricks) place = len(sub_bricks) - 1 nplace = 0 placeable = 0 for i in range( 0, len(sub_bricks) ): # sort bricks into pickable and non-pickable, boundary given by result of 'pick' constraints = fd.brick_constraints(sub_bricks[i], model) if sub_bricks[i]['b'] == 0: if constraints & place_masks[ 1] == 0: # if constraints allow a certain pick... sub_que[place] = copy.deepcopy(sub_bricks[i]) place -= 1 elif constraints & place_masks[0] == 0: sub_que[place] = copy.deepcopy(sub_bricks[i]) sub_que[place]['p'] = 0 place -= 1 elif constraints & place_masks[2] == 0: sub_que[place] = copy.deepcopy(sub_bricks[i]) sub_que[place]['p'] = 2 place -= 1 else: # if not pickable... sub_que[nplace] = copy.deepcopy(sub_bricks[i]) nplace += 1 elif sub_bricks[i]['b'] == 1: if constraints & place_masks1[ 1] == 0: # if constraints allow a certain pick... sub_que[place] = copy.deepcopy(sub_bricks[i]) sub_que[place]['p'] = 0 place -= 1 elif constraints & place_masks1[0] == 0: sub_que[place] = copy.deepcopy(sub_bricks[i]) sub_que[place]['p'] = 0 place -= 1 else: # if not pickable... sub_que[nplace] = copy.deepcopy(sub_bricks[i]) nplace += 1 #if place != len(sub_bricks)-1: # if no pickable bricks, can't disassemble so return error sub_groups = place + 1 #else: # return 'no picks' if nplace == 0: # if any unpickable bricks, remove pickable from the model, re-sort list of non-pickable placeable = 1 #print 'pickable: ' #for i in range(0,sub_groups): # print sub_que[i]['x']+sub_que[i]['px'],', ',sub_que[i]['y']+sub_que[i]['py'] #print 'not pickable: ' #for i in range(sub_groups,len(sub_que)): # print sub_que[i]['x']+sub_que[i]['px'],', ',sub_que[i]['y']+sub_que[i]['py'] return sub_groups, sub_que, placeable
def list_placing(bricks, model): updated_model = list(model) P = 1 k = len(bricks) for i in range(0, k): constraints = fd.brick_constraints(bricks[k - 1 - i], updated_model) bricks[k - 1 - i] = dict(update_placing( bricks[k - 1 - i], constraints)) #temp dict(update_placing) once deleted p #P = P*p updated_model = fd.update_model(bricks[k - 1 - i:k - i], updated_model) return bricks #,P #temp delete p
def get_replaceable(sub_bricks, updated_model): if len(sub_bricks) == 4: for i in range(0, len(sub_bricks)): constraints = fd.brick_constraints(sub_bricks[i], updated_model) if constraints & diamond == 0: replacement = dict(sub_bricks[i]) replacement['b'] = 1 return sub_bricks[i], replacement if len(sub_bricks) == 2: for i in range(0, len(sub_bricks)): constraints = fd.brick_constraints(sub_bricks[i], updated_model) if constraints & square == 0: if sub_bricks[i]['r'] == 0: replacement = dict(sub_bricks[i]) replacement['b'] = 1 replacement['y'] += 2 elif sub_bricks[i]['r'] == 90: replacement = dict(sub_bricks[i]) replacement['b'] = 1 return sub_bricks[i], replacement
def list_cost(bricks, model): cost = 0 #tic1 = time.time() updated_model = copy.deepcopy(model) #tic2 = time.time() k = len(bricks) #tic3=0 #tic4=0 #tic5=0 #tic6=0 for i in range(0, k): #tic3 += time.time() constraints = fd.brick_constraints(bricks[k - 1 - i], updated_model) #tic4 += time.time() cost += brick_cost(bricks[k - 1 - i], constraints) #tic5 += time.time() updated_model = fd.update_model(bricks[k - 1 - i:k - i], updated_model) #tic6 += time.time() #print "times: ",tic2-tic1,tic4-tic3,tic5-tic4,tic6-tic5 return cost
def reassemble(bricks_old, bricks_new, model_old, model_new): dis_list = [] updated_model = copy.deepcopy(model_old) # find old bricks not existant in new structure, remove all bricks above lowest = bricks_old[-1] for i in range(0, len(bricks_old)): flag = 0 for j in range(0, len(bricks_new)): if fd.match_bricks(bricks_old[i], bricks_new[j]) == 1: flag = 1 break if flag == 0: #brick doesn't exist in new structure dis_list.append(bricks_old[i]) if bricks_old[i]['z'] < lowest['z']: lowest = bricks_old[i] # find lowest new brick for i in range(0, len(bricks_new)): flag = 0 for j in range(0, len(bricks_old)): if fd.match_bricks(bricks_new[i], bricks_old[j]) == 1: flag = 1 break if flag == 0: #brick doesn't exist in old structure if bricks_new[i]['z'] < lowest['z']: lowest = bricks_new[i] # find min no. bricks to safely disasseble for i in range(0, len(dis_list)): constraints = fd.brick_constraints(dis_list[i], copy.deepcopy(updated_model)) if pickable( dis_list[i]['b'], constraints ) != 1: # if not pickable: find neighbouring bricks to remove remove_bricks = critical_neighbours(constraints, dis_list[i], updated_model) for j in range(0, len(remove_bricks)): dis_list.append(remove_bricks[j]) dis_list = copy.deepcopy(remove_duplicates(dis_list)) updated_model = fd.update_model(remove_bricks, updated_model) #above_bricks = critical_above # remove layers above lowest discrepancy index = len(bricks_old) for i in range(0, len(bricks_old)): if bricks_old[i]['z'] == lowest['z'] + 1: index = i break dis_list = dis_list + list(bricks_old[index:]) if len(dis_list) > 0: dis_list = copy.deepcopy(remove_duplicates(dis_list)) # update bricks_old list bricks_intermediate = [] for i in range(0, len(bricks_old)): flag = 0 for j in range(0, len(dis_list)): if fd.match_bricks(bricks_old[i], dis_list[j]) == 1: flag = 1 if flag == 0: bricks_intermediate.append(bricks_old[i]) # find new bricks not existant in intermediate structure, build all #lowest = bricks_new[-1] ass_list = [] for i in range(0, len(bricks_new)): flag = 0 for j in range(0, len(bricks_intermediate)): if fd.match_bricks(bricks_new[i], bricks_intermediate[j]) == 1: flag = 1 if flag == 0: #brick doesn't exist in old structure ass_list.append(bricks_new[i]) #if bricks_new[i]['z']<lowest['z']: # lowest = bricks_new[i] # only for gol # if not assemblable, sort will return bricks which cannot be assembled, and 'n', otherwise sorted list and 'y' blocking_bricks = [] que, opt = ass.sort_bricks_ass(ass_list, copy.deepcopy(model_new)) if opt == 'n' and len(que) != 0: for i in range(0, len(que)): bb = find_brick(que[i]['x'], que[i]['y'] - 1, que[i]['z'], model_new) if bb != None: blocking_bricks.append(bb) bb = find_brick(que[i]['x'] + 2, que[i]['y'], que[i]['z'], model_new) if bb != None: blocking_bricks.append(bb) bb = find_brick(que[i]['x'], que[i]['y'] + 2, que[i]['z'], model_new) if bb != None: blocking_bricks.append(bb) bb = find_brick(que[i]['x'] - 1, que[i]['y'], que[i]['z'], model_new) if bb != None: blocking_bricks.append(bb) blocking_bricks = list(remove_duplicates(blocking_bricks)) # check if 1 brick can be removed to fix assembly ass_list.append(blocking_bricks[0]) for i in range(0, len(blocking_bricks)): ass_list[-1] = blocking_bricks[i] que, opt = ass.sort_bricks_ass(ass_list, copy.deepcopy(model_new)) if opt == 'y': dis_list.append(blocking_bricks[i]) return dis_list, ass_list # check if 2 bricks can be removed to fix assembly ass_list[-1] = blocking_bricks[0] ass_list.append(blocking_bricks[1]) for i in range(0, len(blocking_bricks) - 1): ass_list[-2] = blocking_bricks[i] for j in range(i + 1, len(blocking_bricks)): ass_list[-1] = blocking_bricks[j] que, opt = ass.sort_bricks_ass(ass_list, copy.deepcopy(model_new)) if opt == 'y': dis_list.append(blocking_bricks[i]) dis_list.append(blocking_bricks[j]) return dis_list, ass_list return 'help' #index = len(bricks_new) #for i in range(0,len(bricks_new)): # if bricks_new[i]['z'] == lowest['z']+1: # index = i # break #ass_list = list(bricks_new[index:]) #ass_list.insert(0,lowest) return dis_list, ass_list
def reassemble(bricks_old, bricks_new, model_old, model_new): dis_list = [] updated_model = copy.deepcopy(model_old) # find old bricks not existant in new structure, remove all bricks above lowest = bricks_old[-1] for i in range(0, len(bricks_old)): flag = 0 for j in range(0, len(bricks_new)): if fd.match_bricks(bricks_old[i], bricks_new[j]) == 1: flag = 1 if flag == 0: #brick doesn't exist in old structure dis_list.append(bricks_old[i]) if bricks_old[i]['z'] < lowest['z']: lowest = bricks_old[i] # find lowest new brick for i in range(0, len(bricks_new)): flag = 0 for j in range(0, len(bricks_old)): if fd.match_bricks(bricks_new[i], bricks_old[j]) == 1: flag = 1 if flag == 0: #brick doesn't exist in old structure if bricks_new[i]['z'] < lowest['z']: lowest = bricks_new[i] # find min no. bricks to safely disasseble for i in range(0, len(dis_list)): constraints = fd.brick_constraints(dis_list[i], updated_model) if pickable( dis_list[i]['b'], constraints ) != 1: # if not pickable: find neighbouring bricks to remove remove_bricks = critical_neighbours(constraints, dis_list[i], updated_model) for j in range(0, len(remove_bricks)): dis_list.append(remove_bricks[j]) dis_list = copy.deepcopy(remove_duplicates(dis_list)) updated_model = fd.update_model(remove_bricks, updated_model) #above_bricks = critical_above # remove layers above lowest discrepancy index = len(bricks_old) for i in range(0, len(bricks_old)): if bricks_old[i]['z'] == lowest['z'] + 1: index = i break dis_list = dis_list + list(bricks_old[index:]) if len(dis_list) > 0: dis_list = copy.deepcopy(remove_duplicates(dis_list)) # update bricks_old list bricks_intermediate = [] for i in range(0, len(bricks_old)): flag = 0 for j in range(0, len(dis_list)): if fd.match_bricks(bricks_old[i], dis_list[j]) == 1: flag = 1 if flag == 0: bricks_intermediate.append(bricks_old[i]) # find new bricks not existant in intermediate structure, build all #lowest = bricks_new[-1] ass_list = [] for i in range(0, len(bricks_new)): flag = 0 for j in range(0, len(bricks_intermediate)): if fd.match_bricks(bricks_new[i], bricks_intermediate[j]) == 1: flag = 1 if flag == 0: #brick doesn't exist in old structure ass_list.append(bricks_new[i]) #if bricks_new[i]['z']<lowest['z']: # lowest = bricks_new[i] #index = len(bricks_new) #for i in range(0,len(bricks_new)): # if bricks_new[i]['z'] == lowest['z']+1: # index = i # break #ass_list = list(bricks_new[index:]) #ass_list.insert(0,lowest) return dis_list, ass_list