Exemplo n.º 1
0
def conflict_detection(owner, intruders):
    conflict_UAV = []
    antecedent = [None] * 3
    for i in intruders:
        temp_conflict = []
        d = get_distance([owner[0], owner[1]], [i[0], i[1]])
        if d < 24:
            Relative_location = Get_Relative_Location([owner[0], owner[1]],
                                                      [i[0], i[1]], owner[3])
            # print(Relative_location)
            if Relative_location:
                temp_conflict.append(Relative_location)

        if temp_conflict:
            conflict_UAV.append(temp_conflict)
            if temp_conflict[0] == 'F':
                if d < 6:
                    antecedent[1] = 'N'
                elif d > 16:
                    antecedent[1] = 'F'
                else:
                    antecedent[1] = 'M'
            if temp_conflict[0] == 'R':
                if d < 10:
                    antecedent[2] = 'N'
                else:
                    antecedent[2] = 'M'
            if temp_conflict[0] == 'L':
                if d < 10:
                    antecedent[0] = 'N'
                else:
                    antecedent[0] = 'M'

    if conflict_UAV:
        # print(conflict_UAV)
        if antecedent[0] == None:
            antecedent[0] = 'M'
        if antecedent[1] == None:
            antecedent[1] = 'F'
        if antecedent[2] == None:
            antecedent[2] = 'M'
        # print(antecedent)
        consequence = fuzzy_control(antecedent)
        # print(consequence)
        # print(defuzzificztion(consequence))

        return defuzzificztion(consequence)[0], defuzzificztion(consequence)[1]
Exemplo n.º 2
0
def main(P1, P2, P3, P4):
    start = time.time()
    used_time = 0
    turtle.setup(700, 700, 600, 10)
    # turtle.clear()
    r1 = Robot(P1[0], P1[1], 'red')
    r2 = Robot(P2[0], P2[1], 'blue')
    r3 = Robot(P3[0], P3[1], 'green')
    r4 = Robot(P4[0], P4[1], 'black')
    robot_list = [r1, r2, r3, r4]

    Tr = [[[] for i in range(2)] for j in range(len(robot_list))]

    while True:

        # calculate the detection params
        detection_params = [[] for i in range(len(robot_list))]
        for r in robot_list:
            detection_params[robot_list.index(r)].append(
                [r.location_x, r.location_y, r.velocity, r.heading])
            temp = copy.copy(robot_list)
            index = temp.index(r)
            del temp[index]
            intruders_temp = []
            for j in temp:
                intruders_temp.append(
                    [j.location_x, j.location_y, j.velocity, j.heading])
            detection_params[robot_list.index(r)].append(intruders_temp)

        target = 0
        # target used to check if reach target position
        for r in robot_list:
            if get_distance([r.location_x, r.location_y],
                            [r.target_x, r.target_y]) < 20:
                target = target + 1
            else:
                order = conflict_detection(
                    detection_params[robot_list.index(r)][0],
                    detection_params[robot_list.index(r)][1])
                if order:
                    r.move_heading_velocity(order[0], order[1])
                    Tr[robot_list.index(r)][0].append(r.location_x)
                    Tr[robot_list.index(r)][1].append(r.location_y)
                else:
                    r.move()
                    Tr[robot_list.index(r)][0].append(r.location_x)
                    Tr[robot_list.index(r)][1].append(r.location_y)
                target = target + 0
        if target == len(robot_list):
            end = time.time()
            time.sleep(5)
            turtle.clearscreen()
            excepted_legth = 0
            real_legth = 0
            for i in Tr:
                temp = get_distance([i[0][0], i[1][0]], [i[0][-1], i[1][-1]])
                excepted_legth = excepted_legth + temp
            # print("期望距离:",excepted_legth)
            for j in Tr:
                for k in range(len(j[0]) - 1):
                    real_legth = real_legth + get_distance(
                        [j[0][k], j[1][k]], [j[0][k + 1], j[1][k + 1]])
            # print("实际距离:",real_legth)
            # print("路径度量",real_legth/excepted_legth)
            Tra_metric = real_legth / excepted_legth

            used_time = end - start
            episode = []
            for i in Tr:
                episode.append(len(i[0]))
            # print(episode)
            # print("到达目标所需步骤:", episode)
            # print("总共花费实际时间:", used_time)
            # print("时间度量:", (used_time/max(episode))/4)
            Time_metric = (used_time / max(episode)) / 4

            mean_min_separation = []
            for j in range(len(episode)):
                for k in range(len(Tr)):
                    for m in range(k + 1, len(Tr)):
                        mean_min_separation.append(
                            get_distance([Tr[k][0][j], Tr[k][0][j]],
                                         [Tr[m][0][j], Tr[m][1][j]]))

            # print("最小间隔:", min(mean_min_separation)/2)
            Sep_metric = min(mean_min_separation)
            print("本次度量:", Sep_metric, Tra_metric, Time_metric, '\n')
            return Sep_metric, Tra_metric, Time_metric