예제 #1
0
파일: gui.py 프로젝트: ysam12345/ci_hw2
class GUI(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.root = master
        self.grid()
        self.data = self.load_data()
        self.car, self.road = self.init_components()
        self.state = State.PLAYING
        self.dataset_path = '../data/train6dAll.txt'
        self.rbfn_weight_path = '../data/6D_RBFN_params.txt'
        self.mode = '4D'
        self.rbfn = RBFN(J=6, input_dim=3)
        self.rbfn.load(path='../weights/4D_RBFN_params.txt')
        self.set_mode('6D')
        self.ga = GA()

        self.recorder = Recorder()
        self.recorder.add(self.car)
        self.create_widgets()
        self.clean_fig()
        self.draw_road(self.road.finish_area, self.road.road_edges)
        self.draw_car(self.car.loc(), self.car.car_degree, self.car.radius)

    def set_mode(self, mode):
        assert mode == '4D' or mode == '6D'
        self.mode = mode
        if self.mode == '4D':
            self.rbfn = RBFN(J=6, input_dim=3)
            self.rbfn.load(path='../weights/4D_RBFN_params.txt')
        elif self.mode == '6D':
            self.rbfn = RBFN(J=8, input_dim=5)
            self.rbfn.load(path='../weights/6D_RBFN_params.txt')

    def change_mode(self):
        if self.mode == '4D':
            self.set_mode('6D')
            self.im['text'] = '6D'
        else:
            self.set_mode('4D')
            self.im['text'] = '4D'

    def load_data(self):
        case_file_path = '../cases/case01.txt'
        d = Data(case_file_path)
        return d.get()

    def init_components(self):
        c = Car(self.data['start_point'], self.data['start_degree'])
        c.update_sensor(self.data['road_edges'])
        r = Road(self.data['finish_area'], self.data['road_edges'])
        return c, r

    def create_widgets(self):
        # 標題
        self.winfo_toplevel().title("Yochien CI HW2")

        # 自走車位置、方向、感測器距離
        _, self.loc = add_text(self, 0, "Car Location", self.car.loc())
        _, self.fl = add_text(self, 1, "Car Sensor Front Left",
                              self.car.sensor_dist['fl'])
        _, self.f = add_text(self, 2, "Car Sensor Front",
                             self.car.sensor_dist['f'])
        _, self.fr = add_text(self, 3, "Car Sensor Front Right",
                              self.car.sensor_dist['fr'])
        _, self.cd = add_text(self, 4, "Car Degree", self.car.car_degree)
        _, self.swd = add_text(self, 5, "Car Steering Wheel Degree",
                               self.car.steering_wheel_degree)
        # 更新車子
        _, self.next = add_button(self, 6, "Start Playing", "Run", self.run)
        # 目前狀態
        _, self.st = add_text(self, 7, "Status", self.state)

        # 迭代次數
        _, self.it = add_spinbox(self, 8, "Iterate Times", 10, 10000)
        # 族群大小
        _, self.ps = add_spinbox(self, 9, "Population Size", 10, 10000)
        # 突變機率
        _, self.mp = add_spinbox(self, 10, "Mutation Prob(%)", 1, 100)
        # 交配機率
        _, self.cp = add_spinbox(self, 11, "Cross Prob(%)", 1, 100)

        # 選取訓練資料集
        _, self.td = add_text(self, 12, "Training Dataset", self.dataset_path)
        _, self.next = add_button(self, 13, "Select Dataset", "Select",
                                  self.select_dataset_file)
        # 選取RBFN weight
        _, self.rbfn_wf = add_text(self, 14, "RBFN weight file",
                                   self.rbfn_weight_path)
        _, self.srbfn_wf = add_button(self, 15, "Select RBFN weight", "Select",
                                      self.select_rbfn_weight_file)
        # 選取Mode
        _, self.im = add_text(self, 16, "Input Mode (4D/6D)", self.mode)
        _, self.cm = add_button(self, 17, "Change Mode", "Change",
                                self.change_mode)
        # Train RBFN
        _, self.tb = add_button(self, 18, "Train RBFN", "Train", self.train_ga)

        # 地圖與道路
        self.road_fig = Figure(figsize=(5, 5), dpi=120)
        self.road_canvas = FigureCanvasTkAgg(self.road_fig, self)
        self.road_canvas.draw()
        self.road_canvas.get_tk_widget().grid(row=19, column=0, columnspan=3)

    def train_ga(self):
        if self.mode == '4D':
            J = 6
            input_dim = 3
        else:
            J = 8
            input_dim = 5
        iteration_times = int(self.it.get())
        populations_size = int(self.ps.get())
        mutation_prob = int(self.mp.get()) / 100
        crossover_prob = int(self.cp.get()) / 100
        print(iteration_times, populations_size, mutation_prob, crossover_prob)
        self.GA = GA(iteration_times=iteration_times,
                     populations_size=populations_size,
                     mutation_prob=mutation_prob,
                     crossover_prob=crossover_prob,
                     J=J,
                     input_dim=input_dim,
                     dataset_path=self.dataset_path)
        self.GA.train()

    def select_rbfn_weight_file(self):
        try:
            filename = askopenfilename()
            self.rbfn_wf["text"] = filename
            self.rbfn_weight_path = filename
            self.rbfn.load(path=filename)
        except Exception as e:
            print(e)
            self.rbfn_wf["text"] = ""

    def select_dataset_file(self):
        try:
            filename = askopenfilename()
            self.td["text"] = filename
            self.dataset_path = filename
            self.ga = GA(dataset_path=filename)
        except Exception as e:
            print(e)
            self.td["text"] = ""

    def turn_steering_wheel(self, degree):
        self.car.turn_steering_wheel(degree)

    def run(self):
        while self.state == State.PLAYING:
            self.update()
            sleep(0.02)

    def update(self):
        self.update_state()
        self.update_car()
        self.recorder.add(self.car)

    def update_state(self):
        if self.road.is_crash(self.car):
            self.state = State.CRASH
        elif self.road.is_finish(self.car):
            self.state = State.FINISH
            self.recorder.to_file()

        self.st["text"] = self.state

    def update_car(self):
        fl, f, fr = self.car.update_sensor(self.data['road_edges'])

        if self.mode == '4D':
            self.turn_steering_wheel(self.rbfn.output([f, fr, fl]))
        elif self.mode == '6D':
            x, y = self.car.loc()
            self.turn_steering_wheel(self.rbfn.output([x, y, f, fr, fl]))

        self.car.next()
        self.loc["text"] = self.car.loc()
        self.cd["text"] = self.car.car_degree
        self.swd["text"] = self.car.steering_wheel_degree
        self.clean_fig()
        self.draw_road(self.road.finish_area, self.road.road_edges)
        self.draw_car(self.car.loc(), self.car.car_degree, self.car.radius)
        self.draw_route()
        self.road_canvas.draw()

    def clean_fig(self):
        # 清空並初始化影像
        self.road_fig.clf()
        self.road_fig.ax = self.road_fig.add_subplot(111)
        self.road_fig.ax.set_aspect(1)
        self.road_fig.ax.set_xlim([-20, 60])
        self.road_fig.ax.set_ylim([-10, 60])

    def draw_road(self, finish_area, road_edges):
        # 車道邊界
        for i in range(len(road_edges) - 1):
            self.road_fig.ax.text(
                road_edges[i][0], road_edges[i][1],
                '({},{})'.format(road_edges[i][0], road_edges[i][1]))
            self.road_fig.ax.plot([road_edges[i][0], road_edges[i + 1][0]],
                                  [road_edges[i][1], road_edges[i + 1][1]],
                                  'k')
        # 終點區域
        a, b = finish_area[0]
        c, d = finish_area[1]
        self.road_fig.ax.plot([a, c], [b, b], 'r')
        self.road_fig.ax.plot([c, c], [b, d], 'r')
        self.road_fig.ax.plot([c, a], [d, d], 'r')
        self.road_fig.ax.plot([a, a], [d, b], 'r')

    def draw_car(self, loc, car_degree, radius):
        # 車子範圍
        self.road_fig.ax.plot(loc[0], loc[1], '.b')
        circle = plt.Circle(loc, radius, color='b', fill=False)
        self.road_fig.ax.add_artist(circle)
        # 感測器
        self.fl["text"], self.f["text"], self.fr[
            "text"] = self.car.update_sensor(self.data['road_edges'])
        for s in self.car.sensor_point:
            self.road_fig.ax.plot([loc[0], self.car.sensor_point[s][0]],
                                  [loc[1], self.car.sensor_point[s][1]], 'r')
            self.road_fig.ax.plot(self.car.sensor_point[s][0],
                                  self.car.sensor_point[s][1], '.b')

    def draw_route(self):
        records = self.recorder.get()
        for r in records:
            self.road_fig.ax.plot(int(float(r[0]) + 0.0001),
                                  int(float(r[1]) + 0.0001), '.y')
예제 #2
0
def main():
    print("\n[1] > Execute the Sum")
    print("[2] > Execute the Multiplication")
    print("[3] > Execute the Factorial")
    print("[0] > Exit program")
    while True:
        option = input("\nEnter your option: ")
        if option == '0':
            print("End of program!")
            exit()
        elif option == '1':
            print("\nEnter the values of the Recorders A and B: ")
            temp = int(input("A: "))
            A = Recorder(temp)
            temp = int(input("B: "))
            B = Recorder(temp)
            C = D = Recorder(0)
            program = sum.get()
            break
        elif option == '2':
            print("\nEnter the values of the Recorders A and B: ")
            temp = int(input("A: "))
            A = Recorder(temp)
            temp = int(input("B: "))
            B = Recorder(temp)
            C = D = Recorder(0)
            program = multiplication.get()
            break
        elif option == '3':
            print(
                "\nEnter the value of Recorder A, where it will be the result of the factorial: "
            )
            temp = int(input("A: "))
            A = Recorder(temp)
            B = C = D = Recorder(0)
            program = factorial.get()
            break
        else:
            print("(!) This option does not exist. type again!")
    instructions = []
    for linha in program:
        instructions += [[linha[0], linha[1], linha[2], linha[3], linha[4]]]
    i = 0
    print(
        "\nStart of program\n-> (A->{}, B->{}, C->{}, D->{}) [First Instruction -> {}]"
        .format(A.value, B.value, C.value, D.value, instructions[i][1]))
    while i < len(instructions):
        try:
            if instructions[i][1] == 'ADD':
                if instructions[i][2] == 'A':
                    A.add()
                elif instructions[i][2] == 'B':
                    B.add()
                elif instructions[i][2] == 'C':
                    C.add()
                else:
                    D.add()
                i = instructions[i][3]
                i -= 1
                print("((A->{}, B->{}, C->{}, D->{}), {}) > [{}]".format(
                    A.value, B.value, C.value, D.value, i, instructions[i][1]))
            elif instructions[i][1] == 'SUB':
                if instructions[i][2] == 'A':
                    A.sub()
                elif instructions[i][2] == 'B':
                    B.sub()
                elif instructions[i][2] == 'C':
                    C.sub()
                else:
                    D.sub()
                i = instructions[i][3]
                i -= 1
                print("((A->{}, B->{}, C->{}, D->{}), {}) > [{}]".format(
                    A.value, B.value, C.value, D.value, i + 1,
                    instructions[i][1]))
            elif instructions[i][1] == 'ZER':
                if instructions[i][2] == 'A':
                    i = instructions[i][3] if (
                        A.zer()) == 1 else instructions[i][4]
                elif instructions[i][2] == 'B':
                    i = instructions[i][3] if (
                        B.zer()) == 1 else instructions[i][4]
                elif instructions[i][2] == 'C':
                    i = instructions[i][3] if (
                        C.zer()) == 1 else instructions[i][4]
                else:
                    i = instructions[i][3] if (
                        D.zer()) == 1 else instructions[i][4]
                i -= 1
                print("((A->{}, B->{}, C->{}, D->{}), {}) > [{}]".format(
                    A.value, B.value, C.value, D.value, i + 1,
                    instructions[i][1]))
        except IndexError:
            print("\nEnd of program. The recorders now have these values: ")
            print("A: ", A.value)
            print("B: ", B.value)
            print("C: ", C.value)
            print("D: ", D.value)
예제 #3
0
class GUI(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.root = master
        self.grid()
        self.data = self.load_data()
        self.car, self.road = self.init_components()
        self.state = State.PLAYING
        self.recorder = Recorder()
        self.recorder.add(self.car)
        self.create_widgets()
        self.clean_fig()
        self.draw_road(self.road.finish_area, self.road.road_edges)
        self.draw_car(self.car.loc(), self.car.car_degree, self.car.radius)

    def load_data(self):
        case_file_path = '../cases/case01.txt'
        d = Data(case_file_path)
        return d.get()

    def init_components(self):
        c = Car(self.data['start_point'], self.data['start_degree'])
        c.update_sensor(self.data['road_edges'])
        r = Road(self.data['finish_area'], self.data['road_edges'])
        return c, r

    def create_widgets(self):
        # 標題
        self.winfo_toplevel().title("Yochien CI HW1")

        # 自走車位置、方向、感測器距離
        _, self.loc = add_text(self, 0, "Car Location", self.car.loc())
        _, self.fl = add_text(self, 1, "Car Sensor Front Left",
                              self.car.sensor_dist['fl'])
        _, self.f = add_text(self, 2, "Car Sensor Front",
                             self.car.sensor_dist['f'])
        _, self.fr = add_text(self, 3, "Car Sensor Front Right",
                              self.car.sensor_dist['fr'])
        _, self.cd = add_text(self, 4, "Car Degree", self.car.car_degree)
        _, self.swd = add_text(self, 5, "Car Steering Wheel Degree",
                               self.car.steering_wheel_degree)
        # 更新車子
        _, self.next = add_button(self, 6, "Start Playing", "Run", self.run)
        # 目前狀態
        _, self.st = add_text(self, 7, "Status", self.state)

        # 地圖與道路
        self.road_fig = Figure(figsize=(5, 5), dpi=120)
        self.road_canvas = FigureCanvasTkAgg(self.road_fig, self)
        self.road_canvas.draw()
        self.road_canvas.get_tk_widget().grid(row=8, column=0, columnspan=3)

    def turn_steering_wheel(self, degree):
        self.car.turn_steering_wheel(degree)

    def run(self):
        while self.state == State.PLAYING:
            self.update()
            sleep(0.02)

    def update(self):
        self.update_state()
        self.update_car()
        self.recorder.add(self.car)

    def update_state(self):
        if self.road.is_crash(self.car):
            self.state = State.CRASH
        elif self.road.is_finish(self.car):
            self.state = State.FINISH
            self.recorder.to_file()

        self.st["text"] = self.state

    def update_car(self):
        fl, f, fr = self.car.update_sensor(self.data['road_edges'])
        fl = Fuzzifier.fl(fl)
        f = Fuzzifier.f(f)
        fr = Fuzzifier.fr(fr)
        self.turn_steering_wheel(Rules.apply(fl, f, fr))

        self.car.next()
        self.loc["text"] = self.car.loc()
        self.cd["text"] = self.car.car_degree
        self.swd["text"] = self.car.steering_wheel_degree
        self.clean_fig()
        self.draw_road(self.road.finish_area, self.road.road_edges)
        self.draw_car(self.car.loc(), self.car.car_degree, self.car.radius)
        self.draw_route()
        self.road_canvas.draw()

    def clean_fig(self):
        # 清空並初始化影像
        self.road_fig.clf()
        self.road_fig.ax = self.road_fig.add_subplot(111)
        self.road_fig.ax.set_aspect(1)
        self.road_fig.ax.set_xlim([-20, 60])
        self.road_fig.ax.set_ylim([-10, 60])

    def draw_road(self, finish_area, road_edges):
        # 車道邊界
        for i in range(len(road_edges) - 1):
            self.road_fig.ax.text(
                road_edges[i][0], road_edges[i][1],
                '({},{})'.format(road_edges[i][0], road_edges[i][1]))
            self.road_fig.ax.plot([road_edges[i][0], road_edges[i + 1][0]],
                                  [road_edges[i][1], road_edges[i + 1][1]],
                                  'k')
        # 終點區域
        a, b = finish_area[0]
        c, d = finish_area[1]
        self.road_fig.ax.plot([a, c], [b, b], 'r')
        self.road_fig.ax.plot([c, c], [b, d], 'r')
        self.road_fig.ax.plot([c, a], [d, d], 'r')
        self.road_fig.ax.plot([a, a], [d, b], 'r')

    def draw_car(self, loc, car_degree, radius):
        # 車子範圍
        self.road_fig.ax.plot(loc[0], loc[1], '.b')
        circle = plt.Circle(loc, radius, color='b', fill=False)
        self.road_fig.ax.add_artist(circle)
        # 感測器
        self.fl["text"], self.f["text"], self.fr[
            "text"] = self.car.update_sensor(self.data['road_edges'])
        for s in self.car.sensor_point:
            self.road_fig.ax.plot([loc[0], self.car.sensor_point[s][0]],
                                  [loc[1], self.car.sensor_point[s][1]], 'r')
            self.road_fig.ax.plot(self.car.sensor_point[s][0],
                                  self.car.sensor_point[s][1], '.b')

    def draw_route(self):
        records = self.recorder.get()
        for r in records:
            self.road_fig.ax.plot(int(float(r[0]) + 0.0001),
                                  int(float(r[1]) + 0.0001), '.y')
예제 #4
0
            logging.debug("The {} iteration!".format(cnt))

            lidar_data, extra_data = vlp.update()
            if not args.no_camera:
                frame = shot(cam)
                data_dict = {
                    "lidar_data": lidar_data,
                    "extra_data": extra_data,
                    "frame": frame
                }
            else:
                data_dict = {
                    "lidar_data": lidar_data,
                    "extra_data": extra_data
                }
            recorder.add(data_dict)
            cnt += 1
            if args.timestep > 0 and cnt == args.timestep:
                break
    except KeyboardInterrupt:
        logging.critical("KEYBOARD INTERRUPTED!")
    finally:
        et = time.time()
        logging.info(
            "Recording Finish! It take {} seconds and collect {} data! Average FPS {}."
            .format(et - st, cnt, cnt / (et - st)))
        close_vlp(vlp)
        if not args.no_camera:
            close_camera(cam)
        recorder.close()