def transpose(a, pucks, gates): # unorder_set = list(range(303)) s = copy.deepcopy(a) unassign = [] assigned = [] for index in range(s.shape[0]): if s[index, :].sum() == 0: unassign.append(index) else: assigned.append(index) cIndex = random.choice(assigned) gate = list(s[cIndex, :]).index(1) for index in range(303): if s[index, gate] == 1: unassign.append(index) s[:, gate] = np.zeros(303) newPucks = [pucks[k] for k in unassign] newPucks.sort( key=compare_func.cmp_to_key(puck_compare_depart_and_stay_time)) for item in newPucks: for index in range(69): if (can_add(s, pucks, gates, item.id, index)): add_puck(s, pucks, gates, item.id, index) return s
def can_add(allocation, pucks, gates, index_puck, index_gate): ''' Return if current allocation can add a new puck at gate :param allocation: :param pucks: :param gates: :param index_puck: :param index_gate: :return: True if add operation is possible ''' allocation_i = allocation[index_puck, :] if np.sum(allocation_i) == 1: return False if pucks[index_puck].arrive_type == 'D' and gates[ index_gate].arrive_type_D == 0: return False if pucks[index_puck].arrive_type == 'I' and gates[ index_gate].arrive_type_I == 0: return False if pucks[index_puck].depart_type == 'D' and gates[ index_gate].depart_type_D == 0: return False if pucks[index_puck].depart_type == 'I' and gates[ index_gate].depart_type_I == 0: return False if pucks[index_puck].flight_type != gates[index_gate].flight_type: return False pucks_at_gate = [] [rows, cols] = allocation.shape allocation_i = allocation[:, index_gate] for i in range(0, rows): if allocation_i[i] == 1: pucks_at_gate.append(pucks[i]) pucks_at_gate.append(pucks[index_puck]) pucks_at_gate.sort( key=compare_func.cmp_to_key(checkFeasibility.puck_compare_arrive_time)) for j in range(0, len(pucks_at_gate) - 1): if pucks_at_gate[j].depart_time + 9 > pucks_at_gate[j + 1].arrive_time: return False return True
def initialize_greedy(pucks, gates): ''' Initialize starting from least stay time. :param pucks: :param gates: :return: allocation matrix ''' pucks_copy = [s for s in pucks] pucks_copy.sort(key=compare_func.cmp_to_key(puck_compare_stay_time)) allocation_result = np.zeros((len(pucks_copy), len(gates))) for i in range(0, len(pucks_copy)): for j in range(len(gates) - 1, -1, -1): allocation_result[pucks_copy[i].id, j] = int(1) if checkFeasibility.check_feasibility(allocation_result, pucks, gates): print("{}-{}".format(pucks_copy[i].id, j)) break else: allocation_result[pucks_copy[i].id, j] = 0 return allocation_result
def repair(allocation, pucks, gates): ''' repair the allocation matrix which is non-feasible(may be generated from Genetic Algorithm). :param allocation: allocation matrix which is non-feasible :param pucks: all pucks. :param gates: all gates :return: new allocation matrix guaranteed to be feasible ''' if checkFeasibility.check_feasibility(allocation, pucks, gates): return allocation [rows, cols] = allocation.shape for i in range(0, rows): allocation_i = allocation[i, :] # if each puck is allocated at most once if np.sum(allocation_i) > 1: assert False for i in range(0, cols): puck_at_gate = [] for j in range(0, rows): if allocation[j, i] == 1: puck_at_gate.append(pucks[j]) puck_at_gate.sort(key=compare_func.cmp_to_key(initialize.puck_compare_stay_time)) j = 0 while j + 1 < len(puck_at_gate): flag = 0 # check if a 45 min gap is guaranteed if puck_at_gate[j].depart_time + 9 > puck_at_gate[j + 1].arrive_time: if random.random() < 0.7: allocation[puck_at_gate[j + 1].id, i] = 0 del puck_at_gate[j + 1] else: allocation[puck_at_gate[j].id, i] = 0 del puck_at_gate[j] flag = 1 j = j + 1 if flag == 1: j = 0 assert checkFeasibility.check_feasibility(allocation, pucks, gates) # place those who are misses pucks_at_gate = [[] for i in range(0, cols)] for i in range(0, cols): for j in range(0, rows): if allocation[j, i] == 1: pucks_at_gate[i].append(pucks[j]) not_visited = [s for s in range(0, rows)] while len(not_visited) != 0: i = np.random.choice(not_visited) # print("size is {}".format(len(not_visited))) allocation_i = allocation[i, :] not_visited.remove(i) if np.sum(allocation_i) == 0: available = [x for x in pucks[i].available_gates] while len(available) != 0: a = np.random.choice(available) pucks_existed = pucks_at_gate[a] pucks_existed_copy = [s for s in pucks_existed] pucks_existed_copy.append(pucks[i]) # check if ok can_change = 1 pucks_existed_copy.sort(key=compare_func.cmp_to_key(initialize.puck_compare_stay_time)) for j in range(0, len(pucks_existed_copy) - 1): if pucks_existed_copy[j].depart_time + 9 > pucks_existed_copy[j + 1].arrive_time: can_change = 0 break if can_change == 1: allocation[i, a] = 1 # print("insert {} to gate {}".format(i, a)) pucks_at_gate[a].append(pucks[i]) # print(checkFeasibility.check_feasibility(allocation, p, g)) break else: available.remove(a) return allocation
def check_feasibility(allocation, pucks, gates): ''' Check feasibility for the allocation :param allocation: an M*N matrix, (i, j) = 1 means puck i is allocated to gate j, M means the size of the pucks, N means the size of the Gates. :param pucks: all pucks :param gates: all gates :return: True if this allocation is feasible ''' [rows, cols] = allocation.shape indexes = {} for i in range(0, rows): allocation_i = allocation[i, :] # if each puck is allocated at most once if np.sum(allocation_i) > 1: print("false1") return False for j in range(0, cols): if allocation_i[j] == 1: indexes[i] = j break # if this puck's flight_type/arrive_type/depart_type meet the gate's for i in range(0, rows): puck_i = pucks[i] if i in indexes: gate_i = gates[indexes[i]] else: continue if puck_i.arrive_type == 'D' and gate_i.arrive_type_D == 0: print("false2") return False if puck_i.arrive_type == 'I' and gate_i.arrive_type_I == 0: print("false3") return False if puck_i.depart_type == 'D' and gate_i.depart_type_D == 0: print("false4") return False if puck_i.depart_type == 'I' and gate_i.depart_type_I == 0: print("false5") return False if puck_i.flight_type != gate_i.flight_type: print("false6") return False # check if a 45min gap is ok for i in range(0, cols): puck_at_gate = [] for j in range(0, rows): if allocation[j, i] == 1: puck_at_gate.append(pucks[j]) # check now puck_at_gate.sort( key=compare_func.cmp_to_key(puck_compare_arrive_time)) for j in range(0, len(puck_at_gate) - 1): if puck_at_gate[j].depart_time + 9 > puck_at_gate[j + 1].arrive_time: print("false7") return False return True