前提・実現したいこと
交通シミュレータSimulation of Urban MObility(SUMO)において以下のエラーを修正し、動作するようにしたい
発生している問題・エラーメッセージ
Step #0.00 (0ms ?*RT. ?UPS, TraCI: 13ms, vehicles TOT 0 ACT 0 BUF 0) Step #0.00 (0ms ?*RT. ?UPS, TraCI: 13ms, vehicles TOT 0 ACT 0 BUF 0) Retrying in 1 seconds Traceback (most recent call last): File "test.py", line 51, in <module> model.learn(total_timesteps=400000) File "stable_baselines/a2c/a2c.py", line 238, in learn runner = A2CRunner(self.env, self, n_steps=self.n_steps, gamma=self.gamma) File "stable_baselines/a2c/a2c.py", line 319, in __init__ super(A2CRunner, self).__init__(env=env, model=model, n_steps=n_steps) File "stable_baselines/common/runners.py", line 19, in __init__ self.obs[:] = env.reset() ValueError: could not broadcast input array from shape (2) into shape (2,21)
要素数2の配列は2×21にブロードキャストできないというエラーでした。
該当のソースコード
Python
1#------------------------------test.py--------------------------------------------------- 2import gym 3import argparse 4import os 5import sys 6if 'SUMO_HOME' in os.environ: 7 tools = os.path.join(os.environ['SUMO_HOME'], 'tools') 8 sys.path.append(tools) 9else: 10 sys.exit("Please declare the environment variable 'SUMO_HOME'") 11 12os.environ ['KMP_DUPLICATE_LIB_OK'] = 'True' 13 14import pandas as pd 15from gym import spaces 16import numpy as np 17from sumo_rl.environment.env import SumoEnvironment 18from sumo_rl.util.gen_route import write_route_file 19import traci 20from stable_baselines.common.vec_env import SubprocVecEnv 21from stable_baselines import A2C 22from stable_baselines.common.policies import MlpPolicy 23 24 25 26# multiprocess environment 27if __name__ == "__main__": 28 n_env = 2 29 env = SubprocVecEnv([lambda: SumoEnvironment(net_file='test.net.xml ', 30 route_file='test.rou.xml', 31 out_csv_name='test.result', 32 33 use_gui=True, 34 num_seconds=80000, 35 time_to_load_vehicles=120, 36 max_depart_delay=8000, 37 phases=[ 38 traci.trafficlight.Phase(34, "rrrGGrrrrGGr"), 39 traci.trafficlight.Phase(3, "rrryyrrrryyr"), 40 traci.trafficlight.Phase(34, "rrrrrGrrrrrG"), 41 traci.trafficlight.Phase(3, "rrrrryrrrrry"), 42 traci.trafficlight.Phase(34, "GGrrrrGGrrrr"), 43 traci.trafficlight.Phase(3, "yyrrrryyrrrr"), 44 traci.trafficlight.Phase(34, "rrGrrrrrGrrr"), 45 traci.trafficlight.Phase(3, "rryrrrrryrrr"), 46 47 ]) for i in range(n_env)]) 48 49 50 model = A2C(policy=MlpPolicy, env=env , verbose=1, learning_rate=1e-3, lr_schedule='constant') 51 model.learn(total_timesteps=80000) 52 53 54#------------------------------runners.py------------------------------------------------ 55 56import numpy as np 57from abc import ABC, abstractmethod 58 59class AbstractEnvRunner(ABC): 60 def __init__(self, *, env, model, n_steps): 61 """ 62 A runner to learn the policy of an environment for a model 63 64 :param env: (Gym environment) The environment to learn from 65 :param model: (Model) The model to learn 66 :param n_steps: (int) The number of steps to run for each environment 67 """ 68 self.env = env 69 self.model = model 70 n_env = env.num_envs 71 self.batch_ob_shape = (n_env*n_steps,) + env.observation_space.shape 72 self.obs = np.zeros((n_env,) + env.observation_space.shape, dtype=env.observation_space.dtype.name) 73 self.obs[:] = env.reset() 74 self.n_steps = n_steps 75 self.states = model.initial_state 76 self.dones = [False for _ in range(n_env)] 77 78 @abstractmethod 79 def run(self): 80 """ 81 Run a learning step of the model 82 """ 83 raise NotImplementedError
補足情報(FW/ツールのバージョンなど)
Python 3.6
SUMO 1.7.0
筆者/まったくの初心者
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/29 15:54