def __init__(self, map_path2yaml, agents, main_sensor, saving=False, test_name="", acq=-1):
        """
        Simulator(map_path2yaml, agents, render)

        Returns a new Simulator.

        Parameters
        ----------
        map_path2yaml : string
            Absolute path to map yaml archive  , e.g., ``"C:/data/yaml"``.
        agents : list of bin.Environment.simple_agent
            Agents

        Returns
        -------
        out : Simulator

        See Also
        --------

        Examples
        --------
        """
        self.saving = saving

        self.environment = Env(map_path2yaml=map_path2yaml)
        self.agents = agents
        self.sensors = set()

        self.init_maps()
        self.load_envs_into_agents()

        if main_sensor in self.sensors:
            self.main_sensor = main_sensor

        for agent in self.agents:
            agent.randomize_pos()
            # agent.randomize_pos()
            # agent.randomize_pos()
            # agent.randomize_pos()

        self.sender = Sender()

        if acq == 1:
            acq = "LD"
        elif acq == 2:
            acq = "LU"
        elif acq == 3:
            acq = "RD"
        elif acq == 4:
            acq = "RU"
        else:
            raise Exception

        self.coordinator = Coordinator(self.agents, self.environment.grid, self.main_sensor, acq)
        # self.coordinator.acquisition = "maxvalue_entropy_search"
        self.sender.send_new_acq_msg(self.coordinator.acquisition)

        for agent in self.agents:
            agent.next_pose = self.coordinator.generate_new_goal(pose=agent.pose)
            agent.step()
            agent.distance_travelled = 0
            self.sender.send_new_drone_msg(agent.pose)

        self.use_cih_as_initial_points = True
        if self.use_cih_as_initial_points:
            import numpy as np
            reads = [
                [np.array([785, 757]), self.environment.maps["t"][757, 785]],  # CSNB       (757, 785)
                # [np.array([492, 443]), self.environment.maps["t"][443, 492]],  # YVY        (443, 492)
                [np.array([75, 872]), self.environment.maps["t"][872, 75]],  # PMAregua   (872, 75)
                self.agents[0].read()
            ]
            print(reads)
            self.sender.send_new_sensor_msg(str(reads[0][0][0]) + "," + str(reads[0][0][1]) + "," + str(reads[0][1]))
            self.sender.send_new_sensor_msg(str(reads[1][0][0]) + "," + str(reads[1][0][1]) + "," + str(reads[1][1]))
            self.sender.send_new_sensor_msg(str(reads[2][0][0]) + "," + str(reads[2][0][1]) + "," + str(reads[2][1]))

        else:
            reads = [agent.read() for agent in self.agents]

            for i in range(2):
                self.agents[0].randomize_pos()
                reads.append(self.agents[0].read())
            self.sender.send_new_sensor_msg(str(reads[0][0][0]) + "," + str(reads[0][0][1]) + "," + str(reads[0][1]))
            self.sender.send_new_sensor_msg(str(reads[1][0][0]) + "," + str(reads[1][0][1]) + "," + str(reads[1][1]))
            self.sender.send_new_sensor_msg(str(reads[2][0][0]) + "," + str(reads[2][0][1]) + "," + str(reads[2][1]))

        self.coordinator.initialize_data_gpr(reads)

        plt.imshow(self.environment.render_maps()["t"], origin='lower', cmap='inferno')
        plt.colorbar(orientation='vertical')
        CS = plt.contour(self.environment.render_maps()["t"], colors=('gray', 'gray', 'gray', 'k', 'k', 'k', 'k'),
                         alpha=0.6, linewidths=1.0)
        plt.clabel(CS, inline=1, fontsize=10)
        plt.title("Ground Truth")
        plt.xlabel("x")
        plt.ylabel("y")
        plt.draw()
        # plt.pause(0.0001)
        plt.show(block=True)

        if saving:
            self.f = open("E:/ETSI/Proyecto/results/csv_results/{}_{}.csv".format(test_name, int(time.time())), "a")
            self.f.write("kernel,acq,masked\n")
            self.f.write(str(
                "LM,{},{}\n".format(self.coordinator.acquisition, self.coordinator.acq_method)))
            self.f.write("step,mse,t_dist\n")
            mse = self.coordinator.get_mse(self.environment.maps['t'].T.flatten())
            self.f.write("{},{},{}\n".format(0, mse, self.agents[0].distance_travelled))
示例#2
0
    def __init__(self, map_path2yaml, agents: list, main_sensor, saving=False, test_name="", acq="gaussian_sei",
                 acq_mod="normal", file=0, initial_pos="circle"):
        """
        Simulator(map_path2yaml, agents, render)

        Returns a new Simulator.

        Parameters
        ----------
        map_path2yaml : string
            Absolute path to map yaml archive  , e.g., ``"C:/data/yaml"``.
        agents : list of sppa.SimpleAgent
            Agents

        Returns
        -------
        out : Simulator

        See Also
        --------

        Examples
        --------
        """
        self.saving = saving
        self.file_no = file

        self.environment = Env(map_path2yaml=map_path2yaml)
        self.agents = agents
        self.sensors = set()

        self.init_maps()
        self.load_envs_into_agents()

        if main_sensor in self.sensors:
            self.main_sensor = main_sensor
        self.sender = Sender()
        # if initial_pos == "none":
        #     print("random initial positions")
        # elif initial_pos == "circle":
        #     print("circle initial positions")
        #     initial_positions = get_init_pos4(n=len(agents))
        # elif initial_pos == "cvt":
        #     print("cvt initial positions")
        initial_positions = get_init_pos4(n=len(agents), map_data=self.environment.grid, expand=True)
        for agent in self.agents:
            aux_f = agent.position_flag
            agent.position_flag = False
            if initial_pos == "circle":
                agent.pose, initial_positions = initial_positions[0, :], initial_positions[1:, :]
            else:
                agent.randomize_pos()
                agent.randomize_pos()
            # agent.randomize_pos(near=False)
            agent.next_pose = deepcopy(agent.pose)
            agent.position_flag = aux_f
            self.sender.send_new_drone_msg(agent.pose, agent.drone_id)
            self.sender.send_new_goal_msg(agent.next_pose, agent.drone_id)

        self.coordinator = Coordinator(self.environment.grid, self.main_sensor, acq=acq, acq_mod=acq_mod)
        self.sender.send_new_acq_msg(self.coordinator.acquisition)

        reads = [
            # [np.array([785, 757]), self.environment.maps["t"][757, 785]],  # CSNB       (757, 785)
            # [np.array([492, 443]), self.environment.maps["t"][443, 492]],  # YVY        (443, 492)
            # [np.array([75, 872]), self.environment.maps["t"][872, 75]],  # PMAregua   (872, 75)
        ]
        [reads.append(read.read()) for read in self.agents]
        for [read, my_id] in zip(reads, [0, 1, 2, 3, 4]):
            self.sender.send_new_sensor_msg(str(read[0][0]) + "," + str(read[0][1]) + "," + str(read[1]),
                                            _id=my_id)

            self.sender.send_new_drone_msg(self.agents[my_id].pose, my_id)
            plt.pause(0.01)
            # plt.plot(reads[0][0][0], reads[0][0][1], '^y', markersize=12, label="Previous Positions")
        self.coordinator.initialize_data_gpr(reads)

        # from copy import copy
        # import matplotlib.cm as cm
        # current_cmap = copy(cm.get_cmap("inferno"))
        # current_cmap.set_bad(color="#eaeaf2")
        #
        # plt.imshow(self.environment.render_maps()["t"], origin='lower', cmap=current_cmap)  # YlGn_r
        # cbar = plt.colorbar(orientation='vertical')
        # cbar.ax.tick_params(labelsize=20)
        # CS = plt.contour(self.environment.render_maps()["t"], colors=('gray', 'gray', 'gray', 'k', 'k', 'k', 'k'),
        #                  alpha=0.6, linewidths=1.0)
        # plt.clabel(CS, inline=1, fontsize=10)
        # # plt.title("Mask", fontsize=30)
        # plt.xlabel("x", fontsize=20)
        # plt.ylabel("y", fontsize=20)
        # plt.xticks(fontsize=20)
        # plt.yticks(fontsize=20)
        # plt.draw()
        # plt.pause(0.0001)
        # plt.show(block=True)
        if saving:
            self.f = open(
                "E:/ETSI/Proyecto/results/{}_{}_{}.csv".format(test_name, int(time.time()), self.file_no),
                "a")
            self.f.write("num,acq,masked\n")
            self.f.write(str(
                "{},{},{}\n".format(len(self.agents)+50, self.coordinator.acquisition, self.coordinator.acq_mod)))
            self.f.write("step,mse,qty,time,t_dist\n")
            mse = self.coordinator.get_mse(self.environment.maps['t'].T.flatten())
            self.f.write("{},{},{},{},{}\n".format(0, mse, len(self.coordinator.data[1]), 0,
                                                   sum(c.distance_travelled for c in self.agents) / len(self.agents)))
示例#3
0
    def __init__(self,
                 map_path2yaml,
                 agents,
                 main_sensor,
                 saving=False,
                 test_name="",
                 acq="gaussian_sei",
                 acq_mod="normal",
                 file=0):
        """
        Simulator(map_path2yaml, agents, render)

        Returns a new Simulator.

        Parameters
        ----------
        map_path2yaml : string
            Absolute path to map yaml archive  , e.g., ``"C:/data/yaml"``.
        agents : list of bin.Environment.simple_agent
            Agents

        Returns
        -------
        out : Simulator

        See Also
        --------

        Examples
        --------
        """
        self.saving = saving
        self.file_no = file

        self.environment = Env(map_path2yaml=map_path2yaml)
        self.agents = agents
        self.sensors = set()

        self.init_maps()
        self.load_envs_into_agents()

        if main_sensor in self.sensors:
            self.main_sensor = main_sensor

        for agent in self.agents:
            agent.randomize_pos()
            agent.randomize_pos()
            # agent.randomize_pos()
            # agent.randomize_pos()

        self.sender = Sender()

        self.coordinator = Coordinator(self.environment.grid,
                                       self.main_sensor,
                                       acq=acq,
                                       acq_mod=acq_mod)
        self.sender.send_new_acq_msg(self.coordinator.acquisition)

        self.use_cih_as_initial_points = True
        if self.use_cih_as_initial_points:
            reads = [
                [np.array([785, 757]),
                 self.environment.maps["t"][757,
                                            785]],  # CSNB       (757, 785)
                # [np.array([492, 443]), self.environment.maps["t"][443, 492]],  # YVY        (443, 492)
                [np.array([75, 872]),
                 self.environment.maps["t"][872, 75]],  # PMAregua   (872, 75)
                self.agents[0].read()
            ]
            self.sender.send_new_sensor_msg(
                str(reads[0][0][0]) + "," + str(reads[0][0][1]) + "," +
                str(reads[0][1]))
            self.sender.send_new_sensor_msg(
                str(reads[1][0][0]) + "," + str(reads[1][0][1]) + "," +
                str(reads[1][1]))
            self.sender.send_new_sensor_msg(
                str(reads[2][0][0]) + "," + str(reads[2][0][1]) + "," +
                str(reads[2][1]))
            # plt.plot(reads[0][0][0], reads[0][0][1], '^y', markersize=12, label="Previous Positions")
            # plt.plot(reads[1][0][0], reads[1][0][1], '^y', markersize=12)
            # plt.plot(reads[2][0][0], reads[2][0][1], '^y', markersize=12)

        else:
            reads = [agent.read() for agent in self.agents]

            for i in range(2):
                self.agents[0].randomize_pos()
                reads.append(self.agents[0].read())
            self.sender.send_new_sensor_msg(
                str(reads[0][0][0]) + "," + str(reads[0][0][1]) + "," +
                str(reads[0][1]))
            self.sender.send_new_sensor_msg(
                str(reads[1][0][0]) + "," + str(reads[1][0][1]) + "," +
                str(reads[1][1]))
            self.sender.send_new_sensor_msg(
                str(reads[2][0][0]) + "," + str(reads[2][0][1]) + "," +
                str(reads[2][1]))

        self.coordinator.initialize_data_gpr(reads)
        # from copy import copy
        # import matplotlib.cm as cm
        # current_cmap = copy(cm.get_cmap("inferno"))
        # current_cmap.set_bad(color="#eaeaf2")

        # plt.imshow(self.environment.render_maps()["t"], origin='lower', cmap=current_cmap)  # YlGn_r
        # cbar = plt.colorbar(orientation='vertical')
        # cbar.ax.tick_params(labelsize=20)
        # CS = plt.contour(self.environment.render_maps()["t"], colors=('gray', 'gray', 'gray', 'k', 'k', 'k', 'k'),
        #                  alpha=0.6, linewidths=1.0)
        # plt.clabel(CS, inline=1, fontsize=10)
        # # plt.title("Mask", fontsize=30)
        # plt.xlabel("x", fontsize=20)
        # plt.ylabel("y", fontsize=20)
        # plt.xticks(fontsize=20)
        # plt.yticks(fontsize=20)
        # plt.draw()
        # plt.pause(0.0001)
        # plt.show(block=True)

        if saving:
            self.f = open(
                "E:/ETSI/Proyecto/results/csv_results/{}_{}.csv".format(
                    test_name, int(time.time())), "a")
            self.f.write("kernel,acq,masked\n")
            self.f.write(
                str("{},{},{}\n".format(self.coordinator.k_name,
                                        self.coordinator.acquisition,
                                        self.coordinator.acq_mod)))
            self.f.write("step,mse,t_dist,time\n")
            mse = self.coordinator.get_mse(
                self.environment.maps['t'].T.flatten())
            self.f.write("{},{},{},0\n".format(
                0, mse, self.agents[0].distance_travelled))
示例#4
0
    def __init__(self,
                 map_path2yaml,
                 agents: list,
                 acq="gaussian_ei",
                 saving=False,
                 acq_fusion="coupled",
                 acq_mod="normal",
                 id_file=0,
                 render2gui=True,
                 initial_pos="circle",
                 name_file="",
                 d=1.0):
        """

        :param map_path2yaml: file path to mapyaml
        :param agents: list of simple agents. ALL agents have the same set of sensors
        :param acq: str of the acquisition function name
        :param acq_mod: str of the modification of the AF
        :param id_file: int unique file id [0, 1, 2, ... , n]
        :param render2gui: bool if visualization mode is online (performed through MQTT)
        """
        # instancing variables
        self.environment = Env(map_path2yaml=map_path2yaml)
        self.noiseless_maps = False
        self.agents = agents
        # for agent in self.agents:
        #     assert isinstance(agent, Ga), "All agents should be instances of gym.SimpleAgent"
        self.file_no = id_file
        self.sensors = set()
        self._init_maps()
        self.coordinator = Coordinator(self.environment.grid,
                                       self.sensors,
                                       acq=acq,
                                       acq_mod=acq_mod,
                                       acq_fusion=acq_fusion,
                                       d=d)
        self.timestep = 0

        # initializing environment
        self._load_envs_into_agents()
        # initialize drone positions
        initial_positions = get_init_pos4(n=len(agents),
                                          map_data=self.environment.grid,
                                          expand=True)
        for agent in self.agents:
            aux_f = agent.position_flag
            agent.position_flag = False
            if initial_pos == "circle":
                agent.pose, initial_positions = initial_positions[
                    0, :], initial_positions[1:, :]
            else:
                agent.randomize_pos()
            agent.next_pose = deepcopy(agent.pose)
            agent.position_flag = aux_f
        # initial readings
        reads = []
        [reads.append(read.read()) for read in self.agents]
        self.coordinator.initialize_data_gpr(reads)

        self.saving = saving
        if self.saving:
            self.f = open(
                path[-1] + "/results/MAMS/{}_{}_{}.csv".format(
                    name_file, int(time()), self.file_no), "a")
            self.f.write(
                "n_agent,n_sensors,acq_fusion,kernels,acq,acq_mod,prop\n")
            self.f.write(
                str("{},{},{},{},{},{},{}\n".format(
                    len(self.agents), len(self.sensors), acq_fusion,
                    len(self.coordinator.gps), self.coordinator.acquisition,
                    self.coordinator.acq_mod, d)))
            # mses, scores, keys = self.reward()
            scores, keys = self.reward()
            titles = ""
            for sensor in keys:
                titles += f',score_{sensor}'
                # titles += f',mse_{sensor},score_{sensor}'
            results = ""
            # for mse, score in zip(mses, scores):
            #     results += f",{mse},{score}"
            for bic in scores:
                results += f",{bic}"
            # self.f.write(
            #     "step,qty,time,t_dist,avg_mse,avg_score{}\n".format(titles))
            self.f.write("step,qty,time,t_dist,avg_score{}\n".format(titles))
            # self.f.write("{},{},{},{},{},{}{}\n".format(self.timestep, len(self.coordinator.train_inputs), 0,
            #                                             sum(c.distance_travelled for c in self.agents) / len(
            #                                                 self.agents), mean(mses), mean(scores), results))
            self.f.write("{},{},{},{},{}{}\n".format(
                self.timestep, len(self.coordinator.train_inputs), 0,
                sum(c.distance_travelled
                    for c in self.agents) / len(self.agents), mean(scores),
                results))
            self.t0 = time()

        self.render2gui = render2gui
        if render2gui:
            self.sender = Sender()
            self.sender.send_new_acq_msg(self.coordinator.acquisition)
            for agent in self.agents:
                for sensor in agent.sensors:
                    self.sender.send_new_sensor_msg(
                        str(agent.read()["pos"][0]) + "," +
                        str(agent.read()["pos"][1]) + "," +
                        str(agent.read()[sensor]),
                        sensor=sensor,
                        _id=agent.drone_id)
                self.sender.send_new_drone_msg(agent.pose, agent.drone_id)
                self.sender.send_new_goal_msg(agent.next_pose, agent.drone_id)
示例#5
0
    def __init__(self,
                 map_path2yaml,
                 agents: list,
                 acq="GALM",
                 saving=False,
                 acq_fusion="GALM",
                 acq_mod="truncated",
                 id_file=0,
                 render2gui=True,
                 name_file="",
                 d=1.0):
        """

        :param map_path2yaml: file path to mapyaml
        :param agents: list of simple agents. ALL agents have the same set of sensors
        :param acq: str of the acquisition function name
        :param acq_mod: str of the modification of the AF
        :param id_file: int unique file id 0, 1, 2, ... , n
        :param render2gui: bool if visualization mode is online (performed through MQTT)
        """
        # instancing variables
        self.environment = Env(map_path2yaml=map_path2yaml)
        self.noiseless_maps = False
        self.agents = agents
        self.file_no = id_file
        self.sensors = set()
        self._init_maps()
        if acq == "ga":
            self.coordinator = GACoordinator(self.environment.grid,
                                             self.sensors,
                                             d=d,
                                             no_drones=len(agents))
        elif acq == "lm":
            self.coordinator = LMCoordinator(self.environment.grid,
                                             self.sensors,
                                             d=d,
                                             no_drones=len(agents),
                                             acq=id_file)
        self.timestep = 0

        self._load_envs_into_agents()
        for agent in enumerate(self.agents):
            aux_f = agent[1].position_flag
            agent[1].position_flag = False
            agent[1].pose = self.coordinator.current_goals[agent[0]]
            agent[1].next_pose = deepcopy(agent[1].pose)
            agent[1].position_flag = aux_f
        # initial readings
        # import matplotlib.pyplot as plt
        # for sx, sy, cg in zip(self.coordinator.fpx, self.coordinator.fpy, self.coordinator.current_goals):
        #     plt.plot(sx, sy, '-x')
        #     plt.plot([cg[0], sx[0]], [cg[1], sy[0]], '-x')
        # for a in agents:
        #     plt.plot(a.pose[0], a.pose[1], '*')
        # plt.show(block=True)

        reads = []
        [reads.append(read.read()) for read in self.agents]
        self.coordinator.initialize_data_gpr(reads)

        self.saving = saving
        if self.saving:
            self.f = open(
                path[-1] + "/results/MAMS/{}_{}_{}.csv".format(
                    name_file, int(time()), self.file_no), "a")
            self.f.write(
                "n_agent,n_sensors,acq_fusion,kernels,acq,acq_mod,prop\n")
            self.f.write(
                str("{},{},{},{},{},{},{}\n".format(len(self.agents),
                                                    len(self.sensors),
                                                    acq_fusion,
                                                    len(self.coordinator.gps),
                                                    acq, acq_mod, d)))
            # mses, scores, keys = self.reward()
            scores, keys = self.reward()
            titles = ""
            for sensor in keys:
                titles += f',score_{sensor}'
                # titles += f',mse_{sensor},score_{sensor}'
            results = ""
            # for mse, score in zip(mses, scores):
            #     results += f",{mse},{score}"
            for bic in scores:
                results += f",{bic}"
            # self.f.write(
            #     "step,qty,time,t_dist,avg_mse,avg_score{}\n".format(titles))
            self.f.write("step,qty,time,t_dist,avg_score{}\n".format(titles))
            # self.f.write("{},{},{},{},{},{}{}\n".format(self.timestep, len(self.coordinator.train_inputs), 0,
            #                                             sum(c.distance_travelled for c in self.agents) / len(
            #                                                 self.agents), mean(mses), mean(scores), results))
            self.f.write("{},{},{},{},{}{}\n".format(
                self.timestep, len(self.coordinator.train_inputs), 0,
                sum(c.distance_travelled
                    for c in self.agents) / len(self.agents), mean(scores),
                results))
            self.t0 = time()

        self.render2gui = render2gui
        if render2gui:
            self.sender = Sender()
            self.sender.send_new_acq_msg(self.coordinator.acquisition)
            for agent in self.agents:
                for sensor in agent.sensors:
                    self.sender.send_new_sensor_msg(
                        str(agent.read()["pos"][0]) + "," +
                        str(agent.read()["pos"][1]) + "," +
                        str(agent.read()[sensor]),
                        sensor=sensor,
                        _id=agent.drone_id)
                self.sender.send_new_drone_msg(agent.pose, agent.drone_id)
                self.sender.send_new_goal_msg(agent.next_pose, agent.drone_id)