Exemplo n.º 1
0
        self.odom_x += dx * math.cos(self.odom_yaw) - dy * math.sin(
            self.odom_yaw)
        self.odom_y += dx * math.sin(self.odom_yaw) + dy * math.cos(
            self.odom_yaw)

        #Submit
        submit_prediction([self.odom_x, self.odom_y])

        ##################结束#################################
        # 第一项任务和第二项任务无需修改以下代码
        # 仅在第三项任务中需要修改下面的代码

        #get teleop data
        wheel_torque = self.teleop.getControlInput()

        #Output control signals
        set_control_input(
            {"wheels": [{
                "torque": wheel_torque[i]
            } for i in range(4)]})

        ##################结束#################################


app = QApplication([])
player = TrajectoryPlayer(
    TRAJECTORY_FILE) if MODE == "trajectory" else TeleOp()
controller = Controller(player)
simulator = Simulator(controller)
startRenderer(app, simulator, size=(1280, 720))  #可传入size参数调整GUI的大小
Exemplo n.º 2
0

options['SARSA']['burnInTime'] = options['runTime']['learningRandomTime']/2.0



# # setup the training time
options['runTime']['supervisedTrainingTime'] = 10
options['runTime']['learningRandomTime'] = 20
options['runTime']['learningEvalTime'] = 10
options['runTime']['defaultControllerTime'] = 10




sim = Simulator(autoInitialize=False, verbose=False)
sim.options = copy.deepcopy(options)
sim.initialize()
sim.run(launchApp=False)



# save it to a file
#sim.saveToFile('test')
#sim.setupPlayback()

# Testing
#
#
#
# # sim2 = Simulator(autoInitialize=False, verbose=False)
Exemplo n.º 3
0
#save_pickle(pre_embd_w, 'pre_embd_w.pickle')
#save_pickle(system_acts, 'system_acts.pickle')
pre_embd_w = load_pickle('pre_embd_w.pickle')

opts = {'use_ctx': True, 'use_embd': True, 'use_prev': True, 'use_mask': False}
model = HybridCodeNetwork(len(vocab), args.embd_size, args.hidden_size,
                          len(system_acts), pre_embd_w, **opts)
if torch.cuda.is_available():
    model.cuda()
optimizer = torch.optim.Adadelta(
    filter(lambda p: p.requires_grad, model.parameters()))

if args.resume is not None and os.path.isfile(args.resume):
    print("=> loading checkpoint '{}'".format(args.resume))
    ckpt = torch.load(args.resume)
    start_epoch = ckpt['epoch'] + 1 if 'epoch' in ckpt else args.start_epoch
    model.load_state_dict(ckpt['state_dict'])
    optimizer.load_state_dict(ckpt['optimizer'])
else:
    print("=> no checkpoint found")

user_source = 'example_phrases_dict.pickle'
#user_source = 'simulator_uttrs.pickle'

user_simulator = Simulator(user_source, entities)
if args.test != 1:
    train(model, train_data, optimizer, w2i, act2i, args.n_epochs,
          args.batch_size)
#simulate_test_dialogs(10, print_simulated_dialog=True)
test(model, test_data, w2i, act2i)
            gamma * Y[7] + tau * (Y[5] - Y[8])]

start_infection_n1 = 2 / 50
t = np.arange(0, 365, 1)
asol = 100 * integrate.odeint(solver, [1 - R0, R0, 0,
                                       1, 0, 0,
                                       1, 0, 0], t)

plt.plot(t, asol[:, 1], ls='-', color='black', alpha=0.8, lw=2)
plt.plot(t, asol[:, 4], ls='-', color='black', alpha=0.8, lw=2)
ptrue, = plt.plot(t, asol[:, 7], ls='-', color='black', alpha=0.8, lw=2)

for i in range(0, 10):
    state = State(regions, routes, verbose=True)
    state.set_outbreak('Region 0', R0 * N)
    sim = Simulator(state, transfer_prob=tau, beta=beta, gamma=gamma, verbose=True)

    solution = np.zeros((365+1, 9))
    for i, state in enumerate(sim.run(iterations=365)):
        print(i)
        for region_id, sir in state.region_sir.items():
            print(str(sir))
            solution[i, region_id * 3 + 0] = state.region_sir[region_id].susceptible / N
            solution[i, region_id * 3 + 1] = state.region_sir[region_id].infected / N
            solution[i, region_id * 3 + 2] = state.region_sir[region_id].removed / N

    asol = np.asarray(solution) * 100
    t = np.arange(0, asol.shape[0])

    p1, = plt.plot(t, asol[:, 0], ls='-', color='SteelBlue')
    p2, = plt.plot(t, asol[:, 1], ls='-', color='IndianRed')
Exemplo n.º 5
0
import sys
sys.path.append("../../src/")
sys.path.append("dist_activity_citylayout/activity_tracking/")
from trafficModels import *
from generated_city import City
from simulator import Simulator
import time
if __name__ == '__main__':
    city = City()
    from mpi4py import MPI
    if MPI.COMM_WORLD.Get_size() == 1:
        city.setLocation(0, force=True)
    sim = Simulator(city)
    #sim.setVerbose(None)
    sim.setStateSaving('custom')
    #sim.setMemoization()
    sim.setMessageCopy('custom')
    sim.setTerminationTime(5000.0)
    sim.setSchedulerHeapSet()
    sim.setGVTInterval(2)
    sim.setActivityRelocatorCustom('relocator', 'CityRelocator')
    start = time.time()
    sim.simulate()
    print('Arrived: ' + str(len(city.collector.state.cars) / 468.0))
    print("Simulation time: " + str(time.time() - start))
Exemplo n.º 6
0
import matplotlib.pyplot as plt

from model_parameters import ModelParameters
from simulation_data import SimulationData
from simulator import Simulator

# model_parameters_inst trzyma parametry całego ura
model_parameters_inst = ModelParameters()

# simulation_data_inst trzyma parametry symulacji
simulation_data_inst = SimulationData(trajectory_number=2,
                                      trajectory_a_coefficient=2,
                                      trajectory_b_coefficient=4)

# w perform_simulation jest pętla while która ma simulation_data_inst.steps iteracji
simulator_inst = Simulator(simulation_data_inst, model_parameters_inst)
simulator_inst.perform_simulation()

# poniżej tylko sprawdzenie jak ładnie działa
plot1 = plt.figure(1)
plt.subplot(2, 1, 1)
plt.plot(simulation_data_inst.time_vector, simulator_inst.speed_left, 'r')
plt.plot(simulation_data_inst.time_vector, simulator_inst.omega_l, 'b')
plt.subplot(2, 1, 2)
plt.plot(simulation_data_inst.time_vector, simulator_inst.speed_right, 'r')
plt.plot(simulation_data_inst.time_vector, simulator_inst.omega_p, 'b')

plot2 = plt.figure(2)
plt.plot(simulator_inst.qr[:, 0], simulator_inst.qr[:, 1], 'b')
plt.plot(simulator_inst.x, simulator_inst.y, 'r')
plt.show()
Exemplo n.º 7
0
def run():
    """ Driving function for running the simulation. 
        Press ESC to close the simulation, or [SPACE] to pause the simulation. """

    import os
    os.chdir(os.getcwd().rsplit(os.sep, 1)[0])

    # agent configurations: basix, Q_learning, optimized Q_learning
    #conf = 'basic'
    #conf = 'Q_learning'
    conf = 'optimized_Q_learning'

    ##############
    # Create the environment
    # Flags:
    #   verbose     - set to True to display additional output from the simulation
    #   num_dummies - discrete number of dummy agents in the environment, default is 100
    #   grid_size   - discrete number of intersections (columns, rows), default is (8, 6)
    env = Environment()

    ##############
    # Create the driving agent
    # Flags:
    #   learning   - set to True to force the driving agent to use Q-learning
    #    * epsilon - continuous value for the exploration factor, default is 1
    #    * alpha   - continuous value for the learning rate, default is 0.5

    if conf == 'basic':
        agent = env.create_agent(LearningAgent, learning=False)

        ##############
        # Follow the driving agent
        # Flags:
        #   enforce_deadline - set to True to enforce a deadline metric
        env.set_primary_agent(agent)
        env.set_primary_agent(agent, enforce_deadline=True)

        ##############
        # Create the simulation
        # Flags:
        #   update_delay - continuous time (in seconds) between actions, default is 2.0 seconds
        #   display      - set to False to disable the GUI if PyGame is enabled
        #   log_metrics  - set to True to log trial and simulation results to /logs
        #   optimized    - set to True to change the default log file name
        sim = Simulator(env,
                        update_delay=.001,
                        display=False,
                        log_metrics=True,
                        optimized=False)

        ##############
        # Run the simulator
        # Flags:
        #   tolerance  - epsilon tolerance before beginning testing, default is 0.05
        #   n_test     - discrete number of testing trials to perform, default is 0
        sim.run(n_test=10)

    elif conf == 'Q_learning':
        agent = env.create_agent(LearningAgent,
                                 learning=True,
                                 epsilon_decay=.05)
        env.set_primary_agent(agent, enforce_deadline=True)
        sim = Simulator(env,
                        update_delay=.001,
                        display=False,
                        log_metrics=True,
                        optimized=False)
        sim.run(n_test=10)

    else:
        agent = env.create_agent(LearningAgent,
                                 learning=True,
                                 epsilon=1,
                                 alpha=.4,
                                 epsilon_decay=.0025)
        env.set_primary_agent(agent, enforce_deadline=True)
        sim = Simulator(env,
                        update_delay=.001,
                        display=False,
                        log_metrics=True,
                        optimized=True)
        sim.run(tolerance=0.01, n_test=30)
Exemplo n.º 8
0
    sys.exit(1)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        usage()
    filename = sys.argv[1]
    # Initialize scanner
    scanner  = Scanner(1, 1, '', '', [], [], False, False, False, False)
    # Pass in file name, return list of tokens
    tokens   = scanner.scan(filename)
    tokens.append(('EOF', 0, 0, 0))
    # Initialize parser
    parser   = Parser(tokens, 0)
    # Return the AST using tokens
    ast      = parser.parse()
    simulator = Simulator(ast['decorated_nodes'], ast['symtable'] )

    instructions = []
    for inst in ast['decorated_nodes']:
        instructions.append( [inst['instruction'], inst['value']] )
    print "\n"
    print "[Simulator]: Generated instructions for stack machine"
    print "\n"
    ip = 0 
    for inst in instructions:
        if ip > 9:
            print "I" + str(ip) + ":     " + str(instructions[ip])
        else:
            print "I" + str(ip) + ":      " + str(instructions[ip])
        ip += 1 
    print "\n"
Exemplo n.º 9
0
    def __init__(self, plot):
        self.abs_path = os.path.dirname(os.path.abspath(__file__))
        """ Reading the config """
        warnings.filterwarnings("ignore")
        self.init_serializer()
        config = self.serializer.read_config("config_mpc.yaml",
                                             path=self.abs_path)
        self.set_params(config)
        if self.seed < 0:
            """
            Generate a random seed that will be stored
            """
            self.seed = np.random.randint(0, 4294967295)
        np.random.seed(self.seed)

        logging_level = logging.WARN
        if config['verbose']:
            logging_level = logging.DEBUG
        logging.basicConfig(format='%(levelname)s: %(message)s',
                            level=logging_level)
        np.set_printoptions(precision=16)
        dir = self.abs_path + "/stats/mpc"
        tmp_dir = self.abs_path + "/tmp/mpc"
        self.utils = Utils()
        if not self.init_robot(self.robot_file):
            logging.error("MPC: Couldn't initialize robot")
            return
        if not self.setup_scene(self.environment_file, self.robot):
            return
        #self.run_viewer(self.robot_file, self.environment_file)
        self.clear_stats(dir)
        logging.info("Start up simulator")
        sim = Simulator()
        path_evaluator = PathEvaluator()
        path_planner = PathPlanningInterface()
        logging.info("MPC: Generating goal states...")
        goal_states = get_goal_states(
            "mpc", self.abs_path, self.serializer, self.obstacles, self.robot,
            self.max_velocity, self.delta_t, self.start_state,
            self.goal_position, self.goal_radius, self.planning_algortihm,
            self.path_timeout, self.num_generated_goal_states,
            self.continuous_collision, self.environment_file, self.num_cores)

        if len(goal_states) == 0:
            logging.error(
                "MPC: Couldn't generate any goal states. Problem seems to be infeasible"
            )
            return
        logging.info("MPC: Generated " + str(len(goal_states)) +
                     " goal states")
        sim.setup_reward_function(self.discount_factor, self.step_penalty,
                                  self.illegal_move_penalty, self.exit_reward)
        path_planner.setup(self.robot, self.obstacles, self.max_velocity,
                           self.delta_t, self.use_linear_path,
                           self.planning_algortihm, self.path_timeout,
                           self.continuous_collision, self.num_cores)
        if self.dynamic_problem:
            path_planner.setup_dynamic_problem(
                self.robot_file, self.environment_file,
                self.simulation_step_size, self.num_control_samples,
                self.min_control_duration, self.max_control_duration,
                self.add_intermediate_states, self.rrt_goal_bias,
                self.control_sampler)

        A, H, B, V, W, C, D, M_base, N_base = self.problem_setup(
            self.delta_t, self.robot_dof)
        time_to_generate_paths = 0.0
        if check_positive_definite([C, D]):
            m_covs = None
            if self.inc_covariance == "process":
                m_covs = np.linspace(self.min_process_covariance,
                                     self.max_process_covariance,
                                     self.covariance_steps)
            elif self.inc_covariance == "observation":
                m_covs = np.linspace(self.min_observation_covariance,
                                     self.max_observation_covariance,
                                     self.covariance_steps)
        for j in xrange(len(m_covs)):
            M = None
            N = None
            if self.inc_covariance == "process":
                """ The process noise covariance matrix """
                M = self.calc_covariance_value(self.robot, m_covs[j], M_base)
                #M = m_covs[j] * M_base
                """ The observation error covariance matrix """
                N = self.calc_covariance_value(self.robot,
                                               self.min_observation_covariance,
                                               N_base,
                                               covariance_type='observation')
            elif self.inc_covariance == "observation":
                M = self.calc_covariance_value(self.robot,
                                               self.min_process_covariance,
                                               M_base)
                N = self.calc_covariance_value(self.robot,
                                               m_covs[j],
                                               N_base,
                                               covariance_type='observation')
            P_t = np.array([[0.0 for k in xrange(2 * self.robot_dof)]
                            for l in xrange(2 * self.robot_dof)])

            path_planner.setup_path_evaluator(
                A, B, C, D, H, M, N, V, W, self.robot, self.sample_size,
                self.obstacles, self.goal_position, self.goal_radius,
                self.robot_file, self.environment_file)
            sim.setup_problem(A, B, C, D, H, V, W, M, N, self.robot,
                              self.enforce_control_constraints, self.obstacles,
                              self.goal_position, self.goal_radius,
                              self.max_velocity, self.show_viewer_simulation,
                              self.robot_file, self.environment_file)
            sim.set_stop_when_colliding(self.replan_when_colliding)
            if self.dynamic_problem:
                path_evaluator.setup_dynamic_problem()
                sim.setup_dynamic_problem(self.simulation_step_size)
            path_planner.setup_reward_function(self.step_penalty,
                                               self.exit_reward,
                                               self.illegal_move_penalty,
                                               self.discount_factor)
            mean_number_planning_steps = 0.0
            number_of_steps = 0.0
            mean_planning_time = 0.0
            num_generated_paths_run = 0.0
            successful_runs = 0
            num_collisions = 0.0
            final_states = []
            rewards_cov = []
            for k in xrange(self.num_simulation_runs):
                print "MPC: Run " + str(k + 1)
                self.serializer.write_line("log.log", tmp_dir,
                                           "RUN #" + str(k + 1) + " \n")
                current_step = 0
                x_true = self.start_state
                x_estimate = self.start_state
                x_tilde_linear = np.array(
                    [0.0 for i in xrange(2 * self.robot_dof)])
                P_t = np.array([[0.0 for i in xrange(2 * self.robot_dof)]
                                for i in xrange(2 * self.robot_dof)])
                deviation_covariance = np.array(
                    [[0.0 for i in xrange(2 * self.robot_dof)]
                     for i in xrange(2 * self.robot_dof)])
                estimated_deviation_covariance = np.array(
                    [[0.0 for i in xrange(2 * self.robot_dof)]
                     for i in xrange(2 * self.robot_dof)])
                total_reward = 0.0
                terminal = False
                while current_step < self.max_num_steps and not terminal:
                    path_planner.set_start_and_goal(x_estimate, goal_states,
                                                    self.goal_position,
                                                    self.goal_radius)
                    t0 = time.time()
                    (xs, us, zs, control_durations, num_generated_paths,
                     best_val, state_covariances, deviation_covariances,
                     estimated_deviation_covariances, mean_gen_times,
                     mean_eval_times, total_gen_times,
                     total_eval_times) = path_planner.plan_and_evaluate_paths(
                         self.num_paths, 0, current_step,
                         self.evaluation_horizon, P_t, deviation_covariance,
                         estimated_deviation_covariance, self.timeout)
                    mean_planning_time += time.time() - t0
                    mean_number_planning_steps += 1.0
                    num_generated_paths_run += num_generated_paths
                    if len(xs) == 0:
                        logging.error(
                            "MPC: Couldn't find any paths from start state" +
                            str(x_estimate) + " to goal states")

                        total_reward = np.array([-self.illegal_move_penalty
                                                 ])[0]
                        current_step += 1
                        break
                    x_tilde = np.array(
                        [0.0 for i in xrange(2 * self.robot_dof)])
                    n_steps = self.num_execution_steps

                    if n_steps > len(xs) - 1:
                        n_steps = len(xs) - 1
                    if current_step + n_steps > self.max_num_steps:
                        n_steps = self.max_num_steps - current_step
                    (x_true, x_tilde, x_tilde_linear, x_estimate, P_t,
                     current_step, total_reward, terminal, estimated_s,
                     estimated_c, history_entries) = sim.simulate_n_steps(
                         xs,
                         us,
                         zs,
                         control_durations,
                         x_true,
                         x_tilde,
                         x_tilde_linear,
                         x_estimate,
                         P_t,
                         total_reward,
                         current_step,
                         n_steps,
                         0.0,
                         0.0,
                         max_num_steps=self.max_num_steps)
                    #print "len(hist) " + str(len(history_entries))
                    #print "len(deviation_covariances) " + str(len(deviation_covariances))
                    #print "len(estimated_deviation_covariances) " + str(len(estimated_deviation_covariances))
                    deviation_covariance = deviation_covariances[
                        len(history_entries) - 1]
                    estimated_deviation_covariance = estimated_deviation_covariances[
                        len(history_entries) - 1]

                    if (current_step == self.max_num_steps) or terminal:
                        final_states.append(history_entries[-1].x_true)
                        print "len " + str(len(history_entries))
                        print "t " + str(history_entries[-1].t)
                        if terminal:
                            successful_runs += 1

                    history_entries[0].set_replanning(True)
                    for l in xrange(len(history_entries)):
                        history_entries[l].set_estimated_covariance(
                            state_covariances[l])
                        history_entries[l].serialize(tmp_dir, "log.log")
                        if history_entries[l].collided:
                            num_collisions += 1
                            collided = True
                    #logging.warn("MPC: Execution finished. True state is " + str(x_true))
                    #logging.warn("MPC: Estimated state is " + str(x_estimate))
                    logging.warn("MPC: Executed " + str(current_step) +
                                 " steps")
                    logging.warn("MPC: terminal " + str(terminal))
                    if terminal:
                        print "MPC: Final state: " + str(x_true)
                rewards_cov.append(total_reward)
                self.serializer.write_line(
                    "log.log", tmp_dir, "Reward: " + str(total_reward) + " \n")
                self.serializer.write_line("log.log", tmp_dir, "\n")
                number_of_steps += current_step
            mean_planning_time_per_step = mean_planning_time / mean_number_planning_steps
            mean_planning_time /= self.num_simulation_runs
            """ Calculate the distance to goal area
            """
            ee_position_distances = []
            for state in final_states:
                joint_angles = v_double()
                joint_angles[:] = [state[y] for y in xrange(len(state) / 2)]
                ee_position = v_double()
                self.robot.getEndEffectorPosition(joint_angles, ee_position)
                ee_position = np.array(
                    [ee_position[y] for y in xrange(len(ee_position))])
                dist = np.linalg.norm(
                    np.array(self.goal_position - ee_position))
                if dist < self.goal_radius:
                    dist = 0.0
                ee_position_distances.append(dist)
            logging.info("MPC: Done. total_reward is " + str(total_reward))
            try:
                n, min_max, mean_distance_to_goal, var, skew, kurt = scipy.stats.describe(
                    np.array(ee_position_distances))
            except:
                print ee_position_distances
                sleep

            self.serializer.write_line("log.log", tmp_dir,
                                       "################################# \n")
            self.serializer.write_line(
                "log.log", tmp_dir,
                "inc_covariance: " + str(self.inc_covariance) + "\n")
            if self.inc_covariance == "process":
                self.serializer.write_line(
                    "log.log", tmp_dir,
                    "Process covariance: " + str(m_covs[j]) + " \n")
                self.serializer.write_line(
                    "log.log", tmp_dir, "Observation covariance: " +
                    str(self.min_observation_covariance) + " \n")
            elif self.inc_covariance == "observation":
                self.serializer.write_line(
                    "log.log", tmp_dir, "Process covariance: " +
                    str(self.min_process_covariance) + " \n")
                self.serializer.write_line(
                    "log.log", tmp_dir,
                    "Observation covariance: " + str(m_covs[j]) + " \n")
            number_of_steps /= self.num_simulation_runs
            self.serializer.write_line(
                "log.log", tmp_dir,
                "Mean number of steps: " + str(number_of_steps) + " \n")
            self.serializer.write_line(
                "log.log", tmp_dir, "Mean num collisions per run: " +
                str(float(num_collisions) / float(self.num_simulation_runs)) +
                " \n")
            self.serializer.write_line(
                "log.log", tmp_dir, "Average distance to goal area: " +
                str(mean_distance_to_goal) + " \n")
            self.serializer.write_line(
                "log.log", tmp_dir,
                "Num successes: " + str(successful_runs) + " \n")
            print "succ " + str(
                (100.0 / self.num_simulation_runs) * successful_runs)
            self.serializer.write_line(
                "log.log", tmp_dir, "Percentage of successful runs: " + str(
                    (100.0 / self.num_simulation_runs) * successful_runs) +
                " \n")
            self.serializer.write_line(
                "log.log", tmp_dir, "Mean planning time per run: " +
                str(mean_planning_time) + " \n")
            self.serializer.write_line(
                "log.log", tmp_dir, "Mean planning time per planning step: " +
                str(mean_planning_time_per_step) + " \n")

            n, min_max, mean, var, skew, kurt = scipy.stats.describe(
                np.array(rewards_cov))
            print "mean_rewards " + str(mean)
            #plt.plot_histogram_from_data(rewards_cov)
            #sleep
            self.serializer.write_line("log.log", tmp_dir,
                                       "Mean rewards: " + str(mean) + " \n")
            self.serializer.write_line("log.log", tmp_dir,
                                       "Reward variance: " + str(var) + " \n")
            self.serializer.write_line(
                "log.log", tmp_dir,
                "Reward standard deviation: " + str(np.sqrt(var)) + " \n")
            self.serializer.write_line("log.log", tmp_dir,
                                       "Seed: " + str(self.seed) + " \n")
            cmd = "mv " + tmp_dir + "/log.log " + dir + "/log_mpc_" + str(
                m_covs[j]) + ".log"
            os.system(cmd)

        cmd = "cp " + self.abs_path + "/config_mpc.yaml " + dir
        os.system(cmd)

        if not os.path.exists(dir + "/environment"):
            os.makedirs(dir + "/environment")

        cmd = "cp " + self.abs_path + "/" + str(
            self.environment_file) + " " + str(dir) + "/environment"
        os.system(cmd)

        if not os.path.exists(dir + "/model"):
            os.makedirs(dir + "/model")

        cmd = "cp " + self.abs_path + "/" + self.robot_file + " " + dir + "/model"
        os.system(cmd)
        print "Done."
Exemplo n.º 10
0
def run():
    isOptimized = True
    isLearning = True

    dict_name = 'Q_learned_dict'
    dict_name_pikle = 'Q_learned_dict.pkl'

    def load_dict():
        with open(dict_name + '.pkl', 'rb') as f:
            return pickle.load(f)

    def save_dict(obj):
        with open(dict_name + '.pkl', 'wb') as f:
            pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)

    """ Driving function for running the simulation. 
        Press ESC to close the simulation, or [SPACE] to pause the simulation. """

    ##############
    # Create the environment
    # Flags:
    #   verbose     - set to True to display additional output from the simulation
    #   num_dummies - discrete number of dummy agents in the environment, default is 100
    #   grid_size   - discrete number of intersections (columns, rows), default is (8, 6)
    env = Environment()

    ##############
    # Create the driving agent
    # Flags:
    #    * learning   - set to True to force the driving agent to use Q-learning
    #    * epsilon - continuous value for the exploration factor, default is 1
    #    * alpha   - continuous value for the learning rate, default is 0.5
    Q_dict = {}

    if (isLearning and os.path.isfile(dict_name_pikle) and isOptimized):
        print('inside_if_condition')
        Q_dict = load_dict()
    print(len(Q_dict))

    l = LearningAgent(env,
                      learning=isLearning,
                      alpha=0.6,
                      epsilon=0.20,
                      dictn=Q_dict)
    agent = env.create_agent_new(
        l)  #, learning=True, alpha=0.5, epsilon=0.015)

    ##############
    # Follow the driving agent
    # Flags:
    #   enforce_deadline - set to True to enforce a deadline metric
    env.set_primary_agent(agent, enforce_deadline=True)

    ##############
    # Create the simulation
    # Flags:
    #   update_delay - continuous time (in seconds) between actions, default is 2.0 seconds
    #   display      - set to False to disable the GUI if PyGame is enabled
    #   log_metrics  - set to True to log trial and simulation results to /logs
    #   optimized    - set to True to change the default log file name
    sim = Simulator(env,
                    update_delay=0.01,
                    log_metrics=True,
                    optimized=isOptimized,
                    display=False)

    ##############
    # Run the simulator
    # Flags:
    #   tolerance  - epsilon tolerance before beginning testing, default is 0.05
    #   n_test     - discrete number of testing trials to perform, default is 0
    sim.run(n_test=50, tolerance=0.01)

    new_dict = LearningAgent.printDict(l)

    if (isLearning and (not isOptimized)):
        save_dict(new_dict)
        print('Directory Saved')
Exemplo n.º 11
0
import unittest
from pfobjects import Cluster, SmearedCluster
from detectors.CMS import cms
from simulator import Simulator
from ROOT import TVector3
import math
import numpy as np
from ROOT import TFile, TH1F, TH2F

simulator = Simulator(cms)

class TestCluster(unittest.TestCase):

    def test_pt(self):
        '''Test that pT is correctly set.'''
        cluster = Cluster(10., TVector3(1,0,0), 1, 1)
        self.assertAlmostEqual(cluster.pt, 10.)
        cluster.set_energy(5.)
        self.assertAlmostEqual(cluster.pt, 5.)

    def test_smear(self):
        rootfile = TFile('test_cluster_smear.root', 'recreate')
        h_e = TH1F('h_e','cluster energy', 200, 5, 15.)
        energy = 10.
        cluster = Cluster(energy, TVector3(1,0,0), 1, 1)
        ecal = cms.elements['ecal']
        energies = []
        for i in range(10000):
            smeared = simulator.smear_cluster(cluster, ecal, accept=True)
            h_e.Fill(smeared.energy)
            energies.append(smeared.energy)
Exemplo n.º 12
0
import glob
import os
import simulator as s
from simulator import Simulator
from time import sleep
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

if __name__ == '__main__':
    GUI = False
    sim = Simulator(gui=GUI, g=-10, debug=False, epochs=240)
    sim.restore( '/home/mario/data/scenes/0_136ef91c95ca5c2d4b9a4e1a888c5f59_scene.csv', os.environ['MODELS_PATH'])
    cam = s.Camera(width=900, height=900,pos=[-0.75, -0.75, 1],target=[0.5, 0.5,0], far=6,up=[1., 1., 0.], fov=60)
    rgb,depth = cam.snap()
    plt.imshow(rgb)
    plt.show()
    exit()
    #sim.load('cube_small.urdf', pos=[0.2, 0.5,0.025])
    #sim.cam.target = [0.2, 0.5, 0.0]
    #sim.cam.project(sim.cam.target)
    #rgb,depth = sim.cam.snap()
    #if not GUI:
    #    plt.imshow(depth)
    #    plt.show()
    #u = 146
    #v = 151
    #d = depth[v, u]
    ##u = 201
    ##v = 155
from trafficLightModel_classic import *

#    ======================================================================

# 1. Instantiate the (Coupled or Atomic) DEVS at the root of the
#  hierarchical model. This effectively instantiates the whole model
#  thanks to the recursion in the DEVS model constructors (__init__).
#
trafficSystem = TrafficSystem(name="trafficSystem")

#    ======================================================================

# 2. Link the model to a DEVS Simulator:
#  i.e., create an instance of the 'Simulator' class,
#  using the model as a parameter.
sim = Simulator(trafficSystem)

#    ======================================================================

# 3. Perform all necessary configurations, the most commonly used are:


# A. Termination time (or termination condition)
#    Using a termination condition will execute a provided function at
#    every simulation step, making it possible to check for certain states
#    being reached.
#    It should return True to stop simulation, or Falso to continue.
def terminate_whenStateIsReached(clock, model):
    return model.trafficLight.state.get() == "manual"

Exemplo n.º 14
0
	def __init__(self, p=2.0/3):
		self.sim = Simulator(p)
		self.start_state = State (-1, -1, -1)
Exemplo n.º 15
0
 def optimize(self):
     optimizer = Optimizer(self.plant, self.orderList,
                           Simulator(self.plant), Evaluator(self.plant))
     result = optimizer.run()
     print bestSolution(result)
LOW = 0
UP = 119

params = {
    "crossover": {
        "operator": tools.cxTwoPoint
    },
    "mutate": {
        "operator": tools.mutShuffleIndexes,
        "indpb": 0.1
    },
    "select": {
        "operator": tools.selBest,
        "k": int(math.sqrt(NUM_INDIVIDUALS // 2))
    },
    "populationGA1": None,
    "numGeneration1": 10,
    "numGeneration2": 3,
    "crossroads": 21,
    "timeSteps": 2,
    "numIndividuals1": NUM_INDIVIDUALS,
    "numIndividuals2": 10,
    "simulator": Simulator(10, 2, 3),
    "fitnessGA1": "1",
    "fitnessGA2": "1",
    "minLim": LOW,
    "maxLim": UP
}
controller = Controller(params)
print(controller.run1())
# it does not have to do anything other than be imported.
from util import path

from examples.minimalExample.SimulatorCrownstone import SimulatorCrownstone
from simulator import SimulationGui, JsonFileStore, Simulator

config = JsonFileStore('./minimalExample/config.json').getData()
userModule = JsonFileStore('./minimalExample/userData.json').getData()

root = SimulatorCrownstone(1, 0, 0)
root.root = True
simulatorCrownstones = [
    root,
    SimulatorCrownstone(2, 5, 3),
    SimulatorCrownstone(3, 10, 6),
    SimulatorCrownstone(4, 15, 9),
    SimulatorCrownstone(5, 15, 13),
]

a = SimulationGui()
a.loadSimulatorCrownstones(simulatorCrownstones)
a.loadConfig(config)

b = Simulator()
b.loadCrownstones(simulatorCrownstones)
b.loadConfig(config)
a.loadSimulator(
    b)  # this will load the user module into the simulator as a broadcaster.

a.startSimulation(2)
#a.run()
Exemplo n.º 18
0
    default=10000,
    type=int,
    help="Define a janela de busca pelo fim do período transiente")
parser.add_argument(
    "-a",
    "--alpha",
    default=0.05,
    type=float,
    help="Define o alpha (para cálculo do intervalo de confiança)")
parser.add_argument(
    "-s",
    "--seed",
    default=None,
    type=float,
    help=
    "Define a semente para o gerador de números aleatórios que alimenta as chegadas de fregueses e o tempo de serviço"
)

args = parser.parse_args()

simulations = []
for i in range(args.rounds):
    simulator = Simulator(args.use, args.events, i, args.transienterror,
                          args.window, args.seed)
    simulations.append(simulator)
    ret = simulator.start()
    if not ret:
        break

# Calcular intervalo de confiança
Exemplo n.º 19
0
from matplotlib import animation
from tqdm import tqdm
import matplotlib.pyplot as plt

from simulator import Simulator
from core import World
from viz import show_picture

ITERS = 100

sim = Simulator()

# First set up the figure, the axis, and the plot element we want to animate
fig, ax = plt.subplots()
pbar = tqdm(total=ITERS)


# animation function.  This is called sequentially
def animate(i):
    ax.cla()
    img = show_picture(sim.world, [actor.ant for actor in sim.actors])
    sim.iterate()

    pbar.update(1)
    im = ax.imshow(img)
    return im,


anim = animation.FuncAnimation(fig,
                               animate,
                               frames=ITERS,
Exemplo n.º 20
0
def run():
    """Run the agent for a finite number of trials."""
    import time

    alpha_range = [x / 100.0 for x in range(1, 21)]
    gamma_range = [x / 100.0 for x in range(1, 21)]

    max_result = 0
    best_alpha = 0
    best_gamma = 0

    timestr = time.strftime("%Y%m%d-%H%M%S")
    filename = 'gridsearch_' + timestr + '.log'
    with open(filename, 'a') as logfile:
        logfile.write(
            "Alpha,Gamma,SuccessRate,Last20RedLightViolations,Last20PlannerNoncompliance\n"
        )

    for alpha in alpha_range:
        for gamma in gamma_range:
            success_rates = []
            last20_redlight_violations = []
            last20_planner_noncompliance = []
            for count in range(10):
                # Set up environment and agent
                e = Environment(
                )  # create environment (also adds some dummy traffic)
                a = e.create_agent(LearningAgent)  # create agent
                a.gamma = gamma
                a.alpha = alpha
                e.set_primary_agent(
                    a, enforce_deadline=True)  # specify agent to track
                # NOTE: You can set enforce_deadline=False while debugging to allow longer trials

                # Now simulate it
                sim = Simulator(
                    e, update_delay=0.0000005, display=False
                )  # create simulator (uses pygame when display=True, if available)
                # NOTE: To speed up simulation, reduce update_delay and/or set display=False

                sim.run(n_trials=100)  # run for a specified number of trials
                # NOTE: To quit midway, press Esc or close pygame window, or hit Ctrl+C on the command-line
                #plot_agent_performance(a.alpha, a.gamma,a.success_rate, a.red_light_violations, a.planner_noncompliance, count)

                sum_last20_redlight_violations = sum(
                    a.red_light_violations[-20:])
                sum_last20_planner_noncompliance = sum(
                    a.planner_noncompliance[-20:])

                success_rates.append(a.success_rate)
                last20_redlight_violations.append(
                    sum_last20_redlight_violations)
                last20_planner_noncompliance.append(
                    sum_last20_planner_noncompliance)

            mean_success = sum(success_rates) / float(len(success_rates))
            mean_last20redlight = sum(last20_redlight_violations) / float(
                len(last20_redlight_violations))
            mean_last20planner = sum(last20_planner_noncompliance) / float(
                len(last20_planner_noncompliance))
            print 'Mean success rate: ', mean_success
            print 'Mean last 20 red light violationse: ', mean_last20redlight
            print 'Mean last 20 planner_noncompliance: ', mean_last20planner
            with open(filename, 'a') as logfile:
                logfile.write("%.2f,%.2f,%d,%.2f,%.2f\n" %
                              (alpha, gamma, mean_success, mean_last20redlight,
                               mean_last20planner))
Exemplo n.º 21
0
import pickle
from simulator import Simulator
import torch
import torch.nn as nn
from reinforcement.metrics import Metrics
from torch.autograd import Variable
from PolicyGradient import PolicyNetwork
from PolicyGradient import policy
from DQN import DQN
from DQN import DQN_policy

# if gpu is to be used
use_cuda = torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
ByteTensor = torch.cuda.ByteTensor if use_cuda else torch.ByteTensor
Tensor = FloatTensor

environment=Simulator()


policy_network = torch.load("saved_policy/REINFORCE.pkl")
model = torch.load("saved_policy/DQN.pkl")

metrics = Metrics(environment)
inc = metrics.compare_policy([("PolicyGradient",policy),("DQN",DQN_policy)], 20, False)

print(inc)

    # distribution = "constant"
    # distribution = "uniform"
    distribution = "exponential"

    index = []
    hit_ratio = []
    load = []
    for cachesize in range(50, 501, 25):
        index.append(cachesize)

        # new_simulator = NewSimulator(cachesize, amount, expected_value, total_rate, z, N, pattern)
        # hit_ratio.append(new_simulator.cache.totalHitRatio())
        # load.append(new_simulator.cache.totalLoad()/10000.0)
        env = simpy.Environment()
        simulator = Simulator(env, cachesize, amount, expected_value, total_rate, content, popularity, N, pattern,
                              distribution).getSimulator()
        simulator.setDuration(0.01)
        simulator.setDelta(50)
        env.process(simulator.updateSim())
        env.process(simulator.insertSim())
        env.run(until=simulation_time)
        hit_ratio.append(simulator.cache.totalHitRatio())
        load.append(simulator.cache.totalLoad()/float(simulation_time))
        print("simulation: ", simulator.cache.totalHitRatio())

        print(cachesize)

    print(hit_ratio)
    print(load)
    # print(hit_ratio_model_proactive_remove)
    # print(hit_ratio_model_proactive_renew)
Exemplo n.º 23
0
# custom modules
from utils import Options
from simulator import Simulator
from transitionTable import TransitionTable

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# NOTE:
# this script assumes you did generate your data with the get_data.py script
# you are of course allowed to change it and generate data here but if you
# want this to work out of the box first run get_data.py
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# 0. initialization
opt = Options()
sim = Simulator(opt.map_ind, opt.cub_siz, opt.pob_siz, opt.act_num)
trans = TransitionTable(opt.state_siz, opt.act_num, opt.hist_len,
                        opt.minibatch_size, opt.valid_size, opt.states_fil,
                        opt.labels_fil)

# 1. train
######################################
# TODO implement your training here!
# you can get the full data from the transition table like this:
#
# # both train_data and valid_data contain tupes of images and labels
# train_data = trans.get_train()
# valid_data = trans.get_valid()
#
# alternatively you can get one random mini batch line this
#


#-----------------------Clear Figures----------------------------
# clear figures
plt.close('all')


#-----------------Configuration / Parameters--------------------
tspan = np.array([0, 100])    # [sec]
L_cmd = np.zeros(3)			# initially command 0 torque


#----------------Initialize / Setup Workspace------------------
# setup sim
sim = Simulator(config)

# preallocate memory
T = np.arange(0, tspan[1]+config.tstep, config.tstep)
state_history = np.zeros((np.shape(T)[0], np.shape(sim.state)[0]))
state_history[0, :] = sim.state
B_ECI_history = np.zeros((np.shape(T)[0], 3))
B_body_history = np.zeros((np.shape(T)[0], 3))
command_history = np.zeros((np.shape(T)[0], 3))
#---------------------Propagate---------------------------
t = time.time()

for i, elapsed_t in enumerate(T[0:-1]):
	# Simulator
	sensors = sim.step(L_cmd)
	state_history[i+1, :] = sim.state
Exemplo n.º 25
0
z1 = 0
z2 = 3
for _ in range(new_objects):
    x1 = np.random.randint(0, 16)
    x2 = np.random.randint(0, 16)
    y1 = np.random.randint(0, 16)
    y2 = np.random.randint(0, 16)

    points = np.array([[x1, x1, x2, x2], [y1, y1, y2, y2], [z1, z2, z1, z2]])
    objects.append(Obstacle("rectangle", points))

env = Environment()

for o in objects:
    env.add_obstacle(o)

if __name__ == "__main__":
    c = SwarmController(drones, set_points)
    s = Simulator(environment=env,
                  drones=drones,
                  controller=c,
                  com_delay=0.1,
                  log_to_file="test_0.json",
                  env_to_file="env_test_0.json")
    s.simulate(0.05, 10)

    k = Logger()
    k.load_from_file("test_0.json")
    s = Simulator(logger=k)
    s.visualize()
Exemplo n.º 26
0
import sys
sys.path.append("../src")
from simulator import Simulator
from model import Cluster

#import stacktracer
#stacktracer.trace_start("trace.html",interval=5,auto=True) # Set auto flag to always update file!

model = Cluster(2)

sim = Simulator(model)
sim.setVerbose(None)
#sim.setTerminationTime(10.0)
sim.setStateSaving("custom")
sim.simulate()
Exemplo n.º 27
0
              velocity=Vector2(0.05, 0),
              mass=50,
              draw_radius=5,
              color=(0, 255, 0))
    b3 = Body(Vector2(0.05, 50),
              velocity=Vector2(0, 0.1),
              mass=50,
              draw_radius=5,
              color=(255, 0, 0))

    world = World()
    world.add(b1)
    world.add(b2)
    world.add(b3)

    simulator = Simulator(world, DummyEngine, RK4)

    screen_size = Vector2(800, 600)
    screen = Screen(screen_size, bg_color=(0, 0, 0), caption="Simulator")
    screen.camera.scale = 1

    # this coefficient controls the speed
    # of the simulation
    time_scale = 10

    simulator.solver.max_step_size = time_scale / 100

    print("Start program")
    while not screen.should_quit:
        dt = screen.tick(60)
Exemplo n.º 28
0
    "type": "LSTM",
    "units": 16,
    "return_sequences": True
}, {
    "type": "LSTM",
    "units": 16,
    "return_sequences": False
}, {
    "type": "Dense",
    "units": 16,
    "activation": "relu"
}, {
    "type": "Dense",
    "units": 16,
    "activation": "relu"
}]

q_model = Q_Model("GRU",
                  state_dim=env.get_state().shape,
                  no_of_actions=env.no_of_actions,
                  layers=dense_model,
                  hyperparameters={"lr": 0.0001})
agent = Agent(q_model, batch_size=8, discount_factor=0.8, epsilon=1)

no_of_episodes_train = 100
no_of_episodes_test = 100

sim = Simulator(env, agent)
sim.train(no_of_episodes_train, epsilon_decay=0.997)
agent.model.save()
sim.test(no_of_episodes_test)
Exemplo n.º 29
0
# Supressing warnings about time clock being deprecated for higher versions of Python3
import warnings
warnings.filterwarnings("ignore")
from simulator import Simulator

now = time.clock()
# Initialize all the global variables
globals.initialize()

# Create the simulator with the given filename
if (len(sys.argv) < 3):
    sys.exit("Please include the input file and run time as arguments.\n example: \
        python3 main.py test_case1.json 20")
try:
   val = int(sys.argv[2])
except ValueError:
   sys.exit("Please enter an integer value in seconds for the runtime")

if (val > 100):
    sys.exit("This runtime seems too large. \n Please enter an integer value in seconds (less than 100) for the runtime.")

sim = Simulator(sys.argv[1], val)
print("Starting simulation for", sys.argv[1], ", running for", sys.argv[2], "seconds.")
sim.run()
print("The simulation finished.")
sim.plot_metrics()
end = time.clock()
elapsed = end - now
print("TIME ELAPSED: ")
print(elapsed)
Exemplo n.º 30
0
              draw_radius=10)
    b2 = Body(Vector2(1, 1),
              velocity=Vector2(0, 0.2),
              mass=5,
              color=(240, 128, 128),
              draw_radius=5)

    world = World()
    world.add(b1)
    world.add(b2)

    listbodies = [b1, b2]

    ChoixSolver = KuttaSolver  #choix du solveur : Dummy ou Kutta

    simulator = Simulator(world, DummyEngine, ChoixSolver)

    screen_size = Vector2(800, 600)
    screen = Screen(screen_size, bg_color=(0, 0, 0), caption="Simulator")
    screen.camera.scale = 10  #pas trop grand sinon on ne voit pas les deux particules
    #autour de 10
    # this coefficient controls the speed
    # of the simulation
    time_scale = 10

    print("Start program")
    while not screen.should_quit:
        dt = screen.tick(60)

        # simulate physics
        delta_time = time_scale * dt / 1000