Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
except:
for s in server_threads:
s.stop = True
print('did not receive an environment so closing...')
return
if len(environments) == 1:
action_space = _create_tuple_space(environments[0].action_spaces)
observation_space = _create_tuple_space(
environments[0].observation_spaces)
else:
action_space = [_create_tuple_space(e.action_spaces)for e in
environments]
observation_space = [_create_tuple_space(e.observation_spaces) for e in
environments]
action_space = gym.spaces.Tuple(action_space)
observation_space = gym.spaces.Tuple(observation_space)
# Set up actor model
actor_module_str, actor_class_str = args.actor.split(":")
actor_module = importlib.import_module(actor_module_str)
actor_class = getattr(actor_module, actor_class_str)
# Actor initialization
actor = actor_class(action_space, observation_space, args.xml)
while True:
# get state from GRPC link
try:
res = queues[0]['action_response'].get(timeout=60)
except queue.Empty:
print('Not receiving sensor info from scrimmage')
res = ExternalControl_pb2.ActionResult(done=True)
if res.done:
print("Received end of simulation")
def _dump_msg1(o, parts):
if isinstance(o, dict):
return dict([(k, _dump_msg1(v, parts)) for k,v in o.items()])
elif isinstance(o, list):
return [_dump_msg1(v, parts) for v in o]
elif isinstance(o, np.ndarray):
if str(o.dtype) == 'object':
return dict(__type='ndarray', dtype=str(o.dtype), shape=o.shape, flat=[_dump_msg1(x, parts) for x in o.flat])
else:
partno = len(parts)
parts.append(o.tobytes())
return dict(__type='ndarray', dtype=str(o.dtype), shape=o.shape, partno=partno)
elif isinstance(o, gym.Space):
if isinstance(o, gym.spaces.Box):
return dict(__type='Box', low=_dump_msg1(o.low, parts), high=_dump_msg1(o.high, parts))
elif isinstance(o, gym.spaces.Tuple):
return dict(__type='Tuple', spaces=_dump_msg1(o.spaces, parts))
elif isinstance(o, gym.spaces.Discrete):
return dict(__type='Discrete', n=_dump_msg1(o.n, parts))
else:
raise Exception('Unknown space %s' % str(o))
elif isinstance(o, float):
if np.isposinf(o):
return dict(__type='number', value='+inf')
elif np.isneginf(o):
return dict(__type='number', value='-inf')
elif np.isnan(o):
return dict(__type='number', value='nan')
else:
return o
elif isinstance(o, tuple):
return dict(__type='tuple', elems=[_dump_msg1(v, parts) for v in o])
# calcula promedio y stddev
self.stddev = [(x / self.num_ticks) ** 0.5 for x in self.stddev]
# reward function 0=equity variation, 1=Table
self.reward_function = 0
# IF REWARD TABLE IS USED, SET THE NUMBER OR STATE COLS TO 18?
if (self.reward_function == 0):
# matrix for the state(order status, equity variation, reward and statistics (from reward table))
self.state_columns = 3
else:
self.state_columns = 18
# Serial data - to - parallel observation matrix and state matrix
self.obs_matrix = self.num_columns * [deque(self.obs_ticks * [0.0], self.obs_ticks)]
self.state = self.state_columns * [deque(self.obs_ticks * [0.0], self.obs_ticks)]
# action space = discrete(nop,buy,sell),box(volume, take_profit, stop_loss)
self.action_space = spaces.Tuple([
spaces.Discrete(3), # nop, buy, sell
spaces.Box(low=float(-1.0), high=float(1.0), shape=(3,), dtype=np.float32), # vol,tp,sl
])
# observation_space=(16 columns + 3 state variables)* obs_ticks, shape=(width,height, channels?)
self.observation_space = spaces.Box(low=float(-1.0), high=float(1.0), shape=(self.obs_ticks, 1, 19), dtype=np.float32)
self.order_time = 0
# TODO; Quitar cuando se controle SL Y TP
self.sl = self.max_sl
self.tp = self.max_tp
def response_space(self):
res_space = self._response_model_ctor.response_space()
return spaces.Tuple(tuple([
res_space,
] * self._slate_size))
def _load_msg1(o, parts):
if isinstance(o, dict):
t = o.get('__type', None)
if t is not None:
if t == 'ndarray':
if o['dtype'] == 'object':
return np.array(o['flat']).reshape(o['shape'])
else:
return np.frombuffer(parts[o['partno']], dtype=o['dtype']).reshape(o['shape'])
elif t == 'Box':
return gym.spaces.Box(low=_load_msg1(o['low'], parts), high=_load_msg1(o['high'], parts))
elif t == 'Tuple':
return gym.spaces.Tuple(spaces=_load_msg1(o['spaces'], parts))
elif t == 'Discrete':
return gym.spaces.Discrete(n=_load_msg1(o['n'], parts))
elif t == 'tuple':
return tuple(_load_msg1(o['elems'], parts))
elif t == 'number':
if o['value'] == '+inf':
return np.inf
elif o['value'] == '-inf':
return -np.inf
elif o['value'] == 'nan':
return np.nan
else:
raise Exception('Unknown value %s' % o['value'])
else:
logger.warn('Unimplemented object to reconstruct %s', t)
return o
self.shape = np.asscalar(np.asarray(n))
class Continuous(Space, _spaces.Box):
def __init__(self, low, high, shape=None, dtype=None):
super(Continuous, self).__init__(low, high, shape, dtype=dtype)
def reshape(self, new_shape):
low_value = np.min(self.low)
high_value = np.min(self.high)
self.low = low_value + np.zeros(new_shape)
self.high = high_value + np.zeros(new_shape)
self.shape = self.low.shape
class Tuple(Space, _spaces.Tuple):
def __init__(self, *spaces):
self._spaces = spaces
self._shape = tuple([space.shape for space in spaces])
@property
def shape(self):
return self._shape
@property
def spaces(self):
return self._spaces
def reshape(self, new_shape):
raise NotImplementedError("Use reshape separately for each space in Tuple.")
def main():
args = parser.parse_args()
config = generate_config(args)
# env = CityFlowEnvRay(config)
# eng = cityflow.Engine(config["cityflow_config_file"], thread_num = config["thread_num"])
# config["eng"] = [eng,]
# print(config["eng"])
num_agents = len(config["intersection_id"])
grouping = {
"group_1":[id_ for id_ in config["intersection_id"]]
}
obs_space = Tuple([
CityFlowEnvRay.observation_space for _ in range(num_agents)
])
act_space = Tuple([
CityFlowEnvRay.action_space for _ in range(num_agents)
])
register_env(
"cityflow_multi",
lambda config_: CityFlowEnvRay(config_).with_agent_groups(
grouping, obs_space=obs_space, act_space=act_space))
if args.algo == "QMIX":
config_ = {
# "num_workers": 2,
"num_gpus_per_worker":0,
"sample_batch_size": 4,
"num_cpus_per_worker": 3,
self.action_spaces = []
if len(discrete_actions) > 0:
self.action_spaces.append(spaces.Discrete(len(discrete_actions)))
self.action_names.append(discrete_actions)
if len(continuous_actions) > 0:
self.action_spaces.append(spaces.Box(-1, 1, (len(continuous_actions),)))
self.action_names.append(continuous_actions)
if len(multidiscrete_actions) > 0:
self.action_spaces.append(spaces.MultiDiscrete(multidiscrete_action_ranges))
self.action_names.append(multidiscrete_actions)
# if there is only one action space, don't wrap it in Tuple
if len(self.action_spaces) == 1:
self.action_space = self.action_spaces[0]
else:
self.action_space = spaces.Tuple(self.action_spaces)
logger.debug(self.action_space)
def __init__(self):
self.shape = (3, 3)
self.n = 2 # number of agents
self.nA = 9 # number of actions per player
self.nS = 3**9 # number of observations per player
self.action_space = spaces.Tuple((spaces.Discrete(self.n),
spaces.Discrete(self.nA)))
self.observation_space = spaces.Tuple((spaces.Discrete(self.n),
spaces.Discrete(self.nS)))
self.grid = None
self.active_agent = None
self.agent_sym = [X, O]
if hasattr(space, 'dtype') and gym.spaces.Box not in dtype_map:
dtype = space.dtype
else:
dtype = dtype_map.get(gym.spaces.Box, np.float32)
minimum = np.asarray(space.low, dtype=dtype)
maximum = np.asarray(space.high, dtype=dtype)
if simplify_box_bounds:
minimum = try_simplify_array_to_value(minimum)
maximum = try_simplify_array_to_value(maximum)
return specs.BoundedArraySpec(
shape=space.shape,
dtype=dtype,
minimum=minimum,
maximum=maximum,
name=name)
elif isinstance(space, gym.spaces.Tuple):
return tuple(
[nested_spec(s, 'tuple_%d' % i) for i, s in enumerate(space.spaces)])
elif isinstance(space, gym.spaces.Dict):
return collections.OrderedDict([
(key, nested_spec(s, key)) for key, s in space.spaces.items()
])
else:
raise ValueError(
'The gym space {} is currently not supported.'.format(space))