Ejemplo n.º 1
0
    def test_sim_dsr_route_find(self):
        event = Look_For_Destination_DSR("1", "3")
        config_p = ConfigurationParameters(max_cycles=6, events=[event], type_of_nodes=DsrNode, protocol_manager=DsrProtocolManager)
        plane_p = PlaneParameters(scenario_file = MAP_DIR + "map1.txt")

        simulation_parameters = SimulationParameters(config_parameters=config_p, plane_parameters=plane_p)

        sim1 = Simulation(simulation_parameters)

        sim1.run(cycles_to_run=1)

        new_sim_event = sim1.event_broker.get_simulator_event()
        new_message = sim1.message_broker.get_message_to_be_transmitted()
        sim1.message_broker.add_to_be_transmitted(new_message)

        self.assertFalse(new_sim_event)
        self.assertTrue(isinstance(new_message, SearchTargetMessage))
        self.assertFalse(sim1.event_broker.get_asynchronous_event())

        sim1.run()
        new_sim_event = sim1.event_broker.get_simulator_event()
        new_message = sim1.message_broker.get_message_to_be_transmitted()

        self.assertTrue(isinstance(new_sim_event, RouteFoundEvent))
        self.assertFalse(new_message)
        self.assertFalse(sim1.event_broker.get_asynchronous_event())
Ejemplo n.º 2
0
class Job(object):
    """
    This class represents a simulation job to be run
    """

    def __init__(self, job_name, simulation_parameters, x_variable_field, x_variable_name, x_variable_values, y_variables, debug=False):
        """
        Constructor for the parameters present in a job
        """
        counter = 0
        for x_var in x_variable_values:
            counter += 1
            _set_attribute_recursively(simulation_parameters, x_variable_field, x_var)

            if debug:
                import time

                print "\n\n%s -- Running %d of %d with variable %s with value %s" % (time.strftime("%H:%M:%S"), counter, len(x_variable_values), x_variable_name, str(x_var))

            self.sim = Simulation(simulation_parameters)
            self.sim.run()
            self.sim.end_simulation()

            if DRAW_MAPS:
                import plot_plane
                plot_plane.plot_plane(self.sim.plane, self.sim.all_nodes, str(job_name) + str(counter))

            for y_var_name in y_variables:
                y_value = y_variables[y_var_name](self.sim.protocol_manager)
                _save_to_file_statistics(job_name, x_variable_name, y_var_name, x_var, y_value)
Ejemplo n.º 3
0
    def _start_simulation(self):
        """Start simulation in pygame."""
        self.number_of_atoms = int(self.AtomNumInput.get())
        self.radius = int(self.AtomRadInput.get())
        self.velocity = int(self.AtomVelocityInput.get())
        self.time_coefficient = int(self.TimeInput.get())

        if self.number_of_atoms <= 100:
            self._scaling()
            # Create frame for pygame
            self.simulation_window = tk.Frame(self.master,
                                              height=CONTAINER_SIZE[0],
                                              width=CONTAINER_SIZE[1])
            self.simulation_window.grid(row=5, columnspan=2, padx=10, pady=10)

            # Embed pygame into frame
            # FIXME: Broken on Linux
            if os.name == "nt":
                os.environ["SDL_WINDOWID"] = str(
                    self.simulation_window.winfo_id())
            # Start simulation
            self.simulation = Simulation(
                self.radius,
                self.velocity,
                self.number_of_atoms,
                self.time_coefficient,
                self.simulation_window,
            )
            self.simulation._start()
        else:
            messagebox.showerror(
                "Błąd", "Liczba atomów nie może być większa niż 100.")
Ejemplo n.º 4
0
    def test_bfg_with_high_degree_generated_plane(self):
        config_p = ConfigurationParameters(max_cycles=80, events=[], type_of_nodes=BfgNode, protocol_manager=BfgProtocolManager)
        plane_p = PlaneParameters(x_size=20, y_size=20, min_degree=5, max_degree=8, number_of_nodes=300, plane_builder_method=plane_builders.degree_plane_builder)
        simulation_parameters = SimulationParameters(config_parameters=config_p, plane_parameters=plane_p)

        sim1 = Simulation(simulation_parameters)
        print sim1.plane

        sim1.run(cycles_to_run=4)
        event = LookForDestinationBfg("1", "16")
        sim1.event_broker.add_asynchronous_event(event)
        sim1.run()

        new_event = sim1.event_broker.get_simulator_event()
        self.assertTrue(isinstance(new_event, RouteFoundEvent))
Ejemplo n.º 5
0
    def test_sim_bfg_route_find_hard(self):
        config_p = ConfigurationParameters(max_cycles=100, events=[], type_of_nodes=BfgNode, protocol_manager=BfgProtocolManager)
        plane_p = PlaneParameters(scenario_file = MAP_DIR + "map2.txt")
        protocol_p = ProtocolParameters(arguments={'random_walk_max_hop':100, 'random_walk_multiply':1})

        simulation_parameters = SimulationParameters(config_parameters=config_p, plane_parameters=plane_p, protocol_parameters=protocol_p)

        sim1 = Simulation(simulation_parameters)

        sim1.run(cycles_to_run=3)
        event = Look_For_Destination_BFG(origin_id="1", destiny_id="16", random_walk_multiply=1, random_walk_max_hop=100)
        new_event = sim1.event_broker.add_asynchronous_event(event)

        sim1.run()
        new_event = sim1.event_broker.get_simulator_event()
        self.assertTrue(isinstance(new_event, RouteFoundEvent))
Ejemplo n.º 6
0
 def newSimulation(self):
     """
     Creates a new simulation (loading or reloading the config files). This
     is called when the app is first started, but also when F2 is pressed.
     """
     self.simConfig = Config("default_config.py","config.py")
     self.simulation = Simulation(self.simConfig)
     tileWidth = self.gfxConfig.TILE_WIDTH
     self.displaySize = (self.simConfig.BOARD_WIDTH*tileWidth,self.simConfig.BOARD_HEIGHT*tileWidth)
     self.autoplaying = False
Ejemplo n.º 7
0
def pretrain():
    sim = Simulation(nodes)
    state = sim.get_state()
    for i in range(pretrain_length):
        a = np.random.randint(0, len(actions)) # Random action
        new_state, reward, done = sim.step(actions[a])

        if done:
            # We finished the episode
            new_state = np.zeros(state.shape)
            memory.add((state, a, reward, new_state, done)) # Add experience to memory
            sim = Simulation(nodes) # Start a new episode
            state = sim.get_state() # First we need a state

        else:
            memory.add((state, a, reward, new_state, done)) # Add experience to memory
            state = new_state # Our state is now the next_state
Ejemplo n.º 8
0
    def simulator(self, event):
        """ control start/stop simulation mode """
        target_id = event.target.text

        if target_id == 'reset':
            if self.simulation:
                self.simulation.reset()
            self.callback = self.on_select
            self.move_enabled = True
            self.ctx.clear(txt='>>> ')
        else:
            self.move_enabled = False
            oid = self.ctx.time()
            self.simulation = Simulation(oid, self)
            self.ctx.create(self.schema, oid)
            # FIXME add subscribe after websocket functions
            #self.ctx.subscribe(str(self.schema), str(oid))
            self.ctx.log(self.schema, oid, 'NEW')
            self.callback = self.on_trigger
Ejemplo n.º 9
0
            config['simulation']['save_data'] = False
        else:
            config['simulation']['save_data'] = True
    if (args.repeat):
        config['simulation']['repeat'] = args.repeat
    if (args.save_figs != None):
        if (args.save_figs == 0):
            config['analysis']['save_to_file'] = False
        else:
            config['analysis']['save_to_file'] = True
    if (args.logging):
        config['logging_level'] = args.logging
    if (args.market_type):
        config['market_type'] = args.market_type

    print "Going to run with the following configuration:"
    pprint.pprint(config)

    if not args.no_confirm:
        answer = raw_input('Do you want to continue? [Y/n]\r\n')

        if (answer == 'n'):
            exit(0)
    else:
        print "No confirm detected, continuing."

    logging.basicConfig(level=config["logging_level"])

    sim = Simulation(config, args.aggregate_id, args.name)
    sim.run()
Ejemplo n.º 10
0
    'delta_aileron': 0,
    'delta_rudder': 0,
    'delta_t': 0.5
}

trimmed_state, trimmed_controls = steady_state_trim(aircraft, environment, pos,
                                                    psi, TAS, controls0)

system = EulerFlatEarth(time0=0, tot_state=trimmed_state)

de0 = trimmed_controls['delta_elevator']

controls = controls = {
    'delta_elevator': Doublet(t_init=2, T=1, A=0.1, offset=de0),
    'delta_aileron': Constant(trimmed_controls['delta_aileron']),
    'delta_rudder': Constant(trimmed_controls['delta_rudder']),
    'delta_t': Constant(trimmed_controls['delta_t'])
}

sim = Simulation(aircraft, system, environment, controls, dt=0.3)
results_03 = sim.propagate(25)

#sim = Simulation(aircraft, system, environment, controls, dt=0.05)
#results_005 = sim.propagate(25)

kwargs = {'subplots': True, 'sharex': True, 'figsize': (12, 100)}

#ax = results_005.plot(marker='.', color='r', **kwargs)
#ax = resu  lts_03.plot(ax=ax, marker='x', color='k', ls='', **kwargs)
results_03.to_excel("output.xlsx")
#plt.show()
Ejemplo n.º 11
0
class SimulationApp(PygameApp):
    """
    This class contains all the GUI code for the PyEvoSim.
    
    It can be easily used as follows:
    SimulationApp.run()
    """
    def __init__(self):
        self.gfxConfig = Config("gfxconfig.py")
        PygameApp.__init__(self,displaySize=(1,1),maxFramerate=self.gfxConfig.MAX_FRAMERATE,caption=CAPTION,defaultColorKey=self.gfxConfig.COLOR_KEY)
        self.newSimulation()
        self.monsterImage = self.loadImage("monster.png")
        self.followedMonsterImage = self.loadImage("followedmonster.png")
        self.foodImage = self.loadImage("meat.png")
    
    def newSimulation(self):
        """
        Creates a new simulation (loading or reloading the config files). This
        is called when the app is first started, but also when F2 is pressed.
        """
        self.simConfig = Config("default_config.py","config.py")
        self.simulation = Simulation(self.simConfig)
        tileWidth = self.gfxConfig.TILE_WIDTH
        self.displaySize = (self.simConfig.BOARD_WIDTH*tileWidth,self.simConfig.BOARD_HEIGHT*tileWidth)
        self.autoplaying = False
    
    @property
    def autoplaying(self):
        return self._autoplaying
    
    @autoplaying.setter
    def autoplaying(self,newValue):
        self._autoplaying = newValue
        if newValue == True:
            self.caption = CAPTION+" (running)"
        else:
            self.caption = CAPTION
    
    def step(self):
        """
        Called every frame. Updates the simulation if it is autoplaying.
        """
        if self.autoplaying:
            self.simulation.oneStep()
    
    def draw(self,screen):
        # Draw stuff
        tileWidth = self.gfxConfig.TILE_WIDTH
        screen.fill(self.gfxConfig.BACKGROUND_COLOR)
        for coords,boardElement in self.simulation.iteritems():
            drawCoords = coords * tileWidth
            if boardElement == FOOD:
                screen.blit(self.foodImage,drawCoords)
            elif isinstance(boardElement,Monster):
                fillRect = pygame.Rect(drawCoords,(tileWidth,tileWidth))
                screen.fill(boardElement.color,fillRect)
                if boardElement.followed:
                    screen.blit(self.followedMonsterImage,drawCoords)
                else:
                    screen.blit(self.monsterImage,drawCoords)
    
    def on_quit(self,event):
        self.quit()
    def on_keyDown_escape(self,event):
        self.quit()
    
    def on_keyDown_space(self,event):
        if not self.autoplaying:
            self.simulation.oneStep()
    
    def on_keyDown_f2(self,event):
        self.newSimulation()
    
    def on_keyDown_return(self,event):
        self.autoplaying = not self.autoplaying
    
    def on_mouseButtonDown_left(self,event):
        """
        Outputs information about the monster under the cursor.
        Only works when not autoplaying.
        """
        if not self.autoplaying:
            screenCoords = Coords.make(event.pos)
            boardCoords = screenCoords // self.gfxConfig.TILE_WIDTH
            boardElement = self.simulation.get(boardCoords)
            if isinstance(boardElement,Monster):
                print "<{0}, {1}>".format(boardElement.infoString,boardCoords)
    
    def on_mouseButtonDown_right(self,event):
        """
        Follows or unfollows the monster under the cursor (naming it if it has
        not been named).
        Only works when not autoplaying.
        """
        if not self.autoplaying:
            screenCoords = Coords.make(event.pos)
            boardCoords = screenCoords // self.gfxConfig.TILE_WIDTH
            boardElement = self.simulation.get(boardCoords)
            if isinstance(boardElement,Monster):
                self.simulation.toggleMonsterFollowed(boardElement)
Ejemplo n.º 12
0
    results = np.zeros((5, 5))

    irs_thrs = [2, 4, 6, 8, 10]
    irs_max = [12, 14, 16, 18, 20]

    if (False):
        print "Going to sweep over Maximum and threshold IRS values"

        for (i, irs_ths) in enumerate(irs_thrs):
            for (j, irs_mx) in enumerate(irs_max):
                config['model']['irs_threshold'] = irs_ths
                config['model']['max_irs_value'] = irs_mx

                aggregate_id = str(uuid.uuid4())
                sim = Simulation(config, aggregate_id)
                sim.run()
                a = Aggregate(config['file_root'], aggregate_id, True)

                results[i, j] = a.default_distribution()

        print results
        path = orig_file_root + "%s.png"
        cbmax = int(np.max(results) + 0.5)
        #cbmax = int(5.2+0.5)
        heat_map(results, "Exponent heatmap", 'IrsThreshold', 'MaxIrsValue',
                 irs_thrs, irs_max, range(0, cbmax), path, "IrsThs_IrsMax")

        min_alpha = np.min(results[np.where(results > 0)])

        (xr, yr) = np.where(results == min_alpha)
Ejemplo n.º 13
0
    alpha = float(input("Alpha: "))
    discount = float(input("Discount: "))
    series = int(input("Series: "))
    #layers = [int(x) for x in input("Layers: ").split()]
    print()
    print("Configuration:")
    print("\tEpsilon:  %f" % eps)
    print("\tAlpha:    %f" % alpha)
    print("\tDiscount: %f" % discount)
    print("\tSeries:   %d" % series)
    #print("\tLayers:   %s" % str(layers))

    sarsa_agent = get_fourier_sarsa_agent(eps, alpha, discount, series)
    random_agent = RandomAgent()

    sarsa_sim = Simulation(sarsa_agent, discount)
    random_sim = Simulation(random_agent, discount)

    sarsa_vals = []
    random_vals = []

    print("====================================")
    print("          AGENT COMPARISON          ")
    print("====================================")

    print()
    print("SARSA agent with Fourier series vs Random Action agent")
    print()
    print("Simulating...")
    for i in range(1, ep_count+1):
        print("Episode %d" % i)
Ejemplo n.º 14
0
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from simulator import Simulation
import matplotlib.pyplot as plt
import numpy as np


if __name__ == "__main__":
    # both stand
    sim = Simulation(width=20, height_floor=20, length_escalator=10,
                     x_exit_stand = [9,10], x_exit_walk = [],
                     speed_stander=[1,1], speed_walker=[],
                     mu=0.0, beta=10)
    N_iter = 10
    duration_both_stand = np.zeros((N_iter,))
    for n in range(N_iter):
        sim.initialize(n_stander=100, n_walker=0)
        duration_both_stand[n] = sim.run_all_pass(1000)
    baseline = np.median(duration_both_stand)
    
    # stand and walk
    sim = Simulation(width=20, height_floor=20, length_escalator=10,
                     x_exit_stand = [9], x_exit_walk = [10],
                     speed_stander=[1], speed_walker=[2],
                     mu=0.0, beta=10)

    ns_s = np.linspace(1, 100, 20).astype(np.int)
    
    duration = np.zeros((len(ns_s), N_iter))
    
Ejemplo n.º 15
0
class App(tk.Frame):
    """Main interface GUI tkinter."""
    def __init__(self):
        self.master = None
        super().__init__()
        self._sanity_check()
        self.number_of_atoms = 0
        self.radius = 0
        self.velocity = 0
        self.time_coefficient = 0
        self.results = [[], [], [], []]
        self.simulation = None

        # Atom numbers input
        self.AtomNumLabel = tk.Label(self.master,
                                     text="Liczba atomów",
                                     font=10)
        self.AtomNumLabel.grid(row=0)
        self.AtomNumInput = tk.Entry(self.master)
        self.AtomNumInput.grid(row=0, column=1)

        # Atom radius input
        self.AtomRadLabel = tk.Label(self.master,
                                     text="Promień atomu",
                                     font=10)
        self.AtomRadLabel.grid(row=1)
        self.AtomRadInput = tk.Entry(self.master)
        self.AtomRadInput.grid(row=1, column=1)

        # Atom velocity input
        self.AtomVelocityLabel = tk.Label(self.master,
                                          text="Predkości atomów",
                                          font=10)
        self.AtomVelocityLabel.grid(row=2)
        self.AtomVelocityInput = tk.Entry(self.master)
        self.AtomVelocityInput.grid(row=2, column=1)

        # Time coefficient input
        self.TimeLabel = tk.Label(self.master,
                                  text="Wspolczynnik czasu",
                                  font=10)
        self.TimeLabel.grid(row=3)
        self.TimeInput = tk.Entry(self.master)
        self.TimeInput.grid(row=3, column=1)

        # Simulation start button
        self.StartButton = tk.Button(self.master,
                                     text="Start",
                                     command=self._start_simulation)
        self.StartButton.grid(row=4)

        # Close simulation button
        self.StopButton = tk.Button(self.master,
                                    text="Stop",
                                    command=self._close_simulation)
        self.StopButton.grid(row=4, column=1)

        # Draw plot button
        self.DrawPlotButton = tk.Button(self.master,
                                        text="Draw Plot",
                                        command=self._display_plot)
        self.DrawPlotButton.grid(row=6, columnspan=2)

        # Quit button
        self.QuitButton = tk.Button(self.master,
                                    text="Quit",
                                    command=self._exit)
        self.QuitButton.grid(row=7, columnspan=2)

        # Simulation
        self.simulation_window = None
        self.simulation = None

        # Plot
        self.plot_distance_window = None
        self.plot_frequency_window = None
        self.plot_distance = None
        self.plot_frequency = None
        self.plot_window = None

    def _sanity_check(self):
        """Workaround for Windows."""
        if os.name == "nt":
            os.environ["SDL_VIDEODRIVER"] = "windib"
        else:
            game.init()

    def _exit(self):
        """Exit the program."""
        print(self.results)
        sys.exit()

    def _close_simulation(self):
        """Close the simulation."""
        if self.simulation:
            self.results[0].append(self.number_of_atoms)
            self.results[1].append(self.time_coefficient)
            self.results[2].append(self.simulation.result_distance)
            self.results[3].append(self.simulation.result_frequency)
            self.simulation_window.destroy()
            self.simulation._exit()

    def _start_simulation(self):
        """Start simulation in pygame."""
        self.number_of_atoms = int(self.AtomNumInput.get())
        self.radius = int(self.AtomRadInput.get())
        self.velocity = int(self.AtomVelocityInput.get())
        self.time_coefficient = int(self.TimeInput.get())

        if self.number_of_atoms <= 100:
            self._scaling()
            # Create frame for pygame
            self.simulation_window = tk.Frame(self.master,
                                              height=CONTAINER_SIZE[0],
                                              width=CONTAINER_SIZE[1])
            self.simulation_window.grid(row=5, columnspan=2, padx=10, pady=10)

            # Embed pygame into frame
            # FIXME: Broken on Linux
            if os.name == "nt":
                os.environ["SDL_WINDOWID"] = str(
                    self.simulation_window.winfo_id())
            # Start simulation
            self.simulation = Simulation(
                self.radius,
                self.velocity,
                self.number_of_atoms,
                self.time_coefficient,
                self.simulation_window,
            )
            self.simulation._start()
        else:
            messagebox.showerror(
                "Błąd", "Liczba atomów nie może być większa niż 100.")

    def _display_plot(self):
        """Display results as plot"""
        self.plot_window = Plot(self.results)
        self.plot_window.generate_plot()

    def _scaling(self):
        """Scale atom radius and velocity based on display size and number of atoms"""
        scaling_parameter = CONTAINER_SIZE[0] / (self.radius *
                                                 self.number_of_atoms)
        self.radius = max(int(self.radius * scaling_parameter), 3)
        self.velocity = max(int(self.velocity * scaling_parameter), 1)
Ejemplo n.º 16
0
model.add(InputLayer(batch_input_shape=(1, len(nodes))))
model.add(Dense(5, activation='sigmoid'))
model.add(Dense(len(actions), activation='linear'))
model.compile(loss='mse', optimizer='adam', metrics=['mae'])
# Q-learning

num_episodes = 1000
y = 0.95
eps = 0.5 # Exploration rate
r_avg_list = []
for i in range(num_episodes):
    if i % 100 == 0:
        print("Episode {} of {}".format(i + 1, num_episodes))
    done = False
    r_sum = 0
    sim = Simulation(nodes)
    state = sim.get_state() # Initial state

    iteration = 0

    while not done:

        eps = 1/math.sqrt(iteration + 1)                # Gradually decrease exploration rate

        if np.random.random() < eps:
            a = np.random.randint(0, len(actions))      # Explore by picking a random action
        else:
            a = np.argmax(model.predict(state))             # Use network to predict which action to take

        action = actions[a]
        #print(action)