def __init__(self, filename, world_type="Minecraft", name="", owner="", engine=None, uid=None, version=1): """ Initializes spock client including MicropsiPlugin, starts minecraft communication thread. """ from micropsi_core.runtime import add_signal_handler # do spock things first, then initialize micropsi world because the latter requires self.spockplugin # register all necessary spock plugins # DefaultPlugins contain EventPlugin, NetPlugin, TimerPlugin, AuthPlugin, # ThreadPoolPlugin, StartPlugin and KeepalivePlugin plugins = DefaultPlugins plugins.append(ClientInfoPlugin) plugins.append(MovementPlugin) plugins.append(WorldPlugin) plugins.append(MicropsiPlugin) plugins.append(ReConnectPlugin) # get spock configs settings = self.get_config() # add plugin-specific settings settings['plugins'] = plugins settings['plugin_settings'] = { MicropsiPlugin: { "micropsi_world": self }, EventPlugin: { "killsignals": False } } # instantiate spock client if not yet done, which in turn instantiates its plugins # ( MicropsiPlugin sets self.spockplugin upon instantiation ) if self.instances['spock'] is None: self.instances['spock'] = Client(plugins=plugins, settings=settings) if self.instances['thread'] is None: # start new thread for minecraft comm" which starts spock client thread = Thread( target=self.instances['spock'].start, args=(settings['server'], settings['port'])) # Note: client.start() is attached in StartPlugin w/ setattr(self.client, 'start', self.start) thread.start() self.instances['thread'] = thread # add_signal_handler(self.kill_minecraft_thread) # once MicropsiPlugin is instantiated and running, initialize micropsi world World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version) # make data accessible to frontend self.data['assets'] = self.assets # copied from jonas' code as is self.current_step = 0 self.first_step = True self.chat_ping_counter = 0 self.the_image = None
def step(self): """ Is called on every world step to advance the simulation. """ World.step(self) # a 2D perspective projection self.get_perspective_projection(self.spockplugin.clientinfo.position)
def __init__(self, filename, world_type="", name="", owner="", uid=None, version=1): World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version) self.data['assets'] = self.assets self.scale_x = (self.assets['x'] / -(self.coords['x1'] - self.coords['x2'])) self.scale_y = (self.assets['y'] / -(self.coords['y1'] - self.coords['y2'])) self.stations = {} self.fahrinfo_berlin = {} self.current_step = 1 self.load_json_data()
def __init__(self, filename, world_type="DummyWorld", name="", owner="", uid=None, version=1): World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version) self.current_step = 0 self.data['assets'] = {}
def test_world_does_not_spawn_deleted_agents(test_world, resourcepath): from micropsi_core.world.world import World filename = os.path.join(resourcepath, 'worlds', 'foobar.json') data = """{ "filename": "%s", "name": "foobar", "owner": "Pytest User", "uid": "foobar", "version":1, "world_type": "Island", "agents": { "dummy": { "name": "Dummy", "position": [17, 17], "type": "Braitenberg", "uid": "dummy" } } }""" with open(filename, 'w') as fp: fp.write(data) world = World(filename, world_type='Island', name='foobar', owner='Pytest User', uid='foobar') assert 'dummy' not in world.agents
def __init__(self, filename, world_type="Island", name="", owner="", engine=None, uid=None, version=1, config={}): World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version) self.load_groundmap() # self.current_step = 0 self.data['assets'] = self.assets
def __init__(self, filename, world_type="Minecraft", name="", owner="", uid=None, version=1): from micropsi_core.runtime import add_signal_handler World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version) self.current_step = 0 self.data['assets'] = self.assets self.first_step = True self.chat_ping_counter = 0 self.the_image = None plugins = DefaultPlugins plugins.append(ClientInfoPlugin) plugins.append(MovementPlugin) plugins.append(WorldPlugin) plugins.append(spockplugin.MicropsiPlugin) settings = { 'username': '******', #minecraft.net username or name for unauthenticated servers 'password': '', #Password for account, ignored if not authenticated 'authenticated': False, #Authenticate with authserver.mojang.com 'bufsize': 4096, #Size of socket buffer 'sock_quit': True, #Stop bot on socket error or hangup 'sess_quit': True, #Stop bot on failed session login 'thread_workers': 5, #Number of workers in the thread pool 'plugins': plugins, 'plugin_settings': { spockplugin.MicropsiPlugin: {"worldadapter": self}, EventPlugin: {"killsignals": False} }, #Extra settings for plugins 'packet_trace': False, 'mc_username': "******", "mc_password": "******" } self.spock = Client(plugins=plugins, settings=settings) # the MicropsiPlugin will create self.spockplugin here on instantiation server_parameters = self.read_server_parameters() self.minecraft_communication_thread = Thread(target=self.spock.start, args=server_parameters) self.minecraft_communication_thread.start() add_signal_handler(self.kill_minecraft_thread)
def __init__(self, filename, *args, **kwargs): World.__init__(self, filename, *args, **kwargs) self.logger.info("world created.") #This sets up the API objects. self.agent_host = MalmoPython.AgentHost() try: self.agent_host.parse( sys.argv ) except RuntimeError as e: print('ERROR:',e) print(self.agent_host.getUsage()) exit(1) if self.agent_host.receivedArgument("help"): print(self.agent_host.getUsage()) exit(0) #Build a mission and give it the configuration of the world. Note that we can change that later with Python (not XML) code. self.my_mission = MalmoPython.MissionSpec(missionXML, True) self.my_mission_record = MalmoPython.MissionRecordSpec() #We may also store previous observations within Malmo, but let MicroPsi take care of remembering what they were. self.agent_host.setObservationsPolicy(MalmoPython.ObservationsPolicy.LATEST_OBSERVATION_ONLY)
def step(self): """ Is called on every world step to advance the simulation. """ World.step(self) self.project_and_render(self.spockplugin.world.map.columns, self.spockplugin.clientinfo.position)
def __init__(self, filename, world_type="Island", name="", owner="", uid=None, version=1): World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version) self.load_groundmap() self.current_step = 0 self.data['assets'] = self.assets
def step(self): World.step(self)
def __init__(self, filename, world_type="TimeSeries", name="", owner="", engine=None, uid=None, version=1, config={}): World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version, config=config) self.data['assets'] = self.assets filename = config.get('time_series_data_file', "timeseries.npz") if os.path.isabs(filename): path = filename else: path = os.path.join(cfg['micropsi2']['data_directory'], filename) self.logger.info("loading timeseries from %s for world %s" % (path, uid)) self.realtime_per_entry = int(config['realtime_per_entry']) self.last_realtime_step = datetime.utcnow().timestamp() * 1000 try: with np.load(path) as f: self.timeseries = f['data'] self.ids = f['ids'] self.timestamps = f['timestamps'] except IOError as error: self.logger.error("Could not load data file %s, error was: %s" % (path, str(error))) self.ids = [0] self.timeseries[[0, 0, 0]] self.timestamps = [0] self.len_ts = 1 return # todo use the new configurable world options. dummydata = config['dummy_data'] == "True" z_transform = config['z_transform'] == "True" clip_and_scale = config['clip_and_scale'] == "True" sigmoid = config['sigmoid'] == "True" self.shuffle = config['shuffle'] == "True" if clip_and_scale and sigmoid: self.logger.warn( "clip_and_scale and sigmoid cannot both be configured, choosing sigmoid" ) clip_and_scale = False def sigm(X): """ sigmoid that avoids float overflows for very small inputs. expects a numpy float array. """ cutoff = np.log(np.finfo(X.dtype).max) - 1 X[np.nan_to_num(X) <= -cutoff] = -cutoff return 1. / (1. + np.exp(-X)) if (z_transform or clip_and_scale or sigmoid) and not dummydata: data_z = np.empty_like(self.timeseries) data_z[:] = np.nan pstds = [] for i, row in enumerate(self.timeseries): if not np.all(np.isnan(row)): std = np.sqrt(np.nanvar(row)) if std > 0: if not clip_and_scale: row_z = (row - np.nanmean(row)) / std if clip_and_scale: row_z = row - np.nanmean(row) pstd = std * 4 row_z[np.nan_to_num(row_z) > pstd] = pstd row_z[np.nan_to_num(row_z) < -pstd] = -pstd row_z = ((row_z / pstd) + 1) * 0.5 data_z[i, :] = row_z self.timeseries = data_z if not sigmoid else sigm(data_z) if dummydata: self.logger.warn("! Using dummy data") n_ids = self.timeseries.shape[0] self.timeseries = np.tile(np.random.rand(n_ids, 1), (1, 10)) self.len_ts = self.timeseries.shape[1]
def __init__(self, filename, world_type="Minecraft", name="", owner="", engine=None, uid=None, version=1): """ Initializes spock client including MicropsiPlugin, starts minecraft communication thread. """ from micropsi_core.runtime import add_signal_handler # do spock things first, then initialize micropsi world because the latter requires self.spockplugin # register all necessary spock plugins # DefaultPlugins contain EventPlugin, NetPlugin, TimerPlugin, AuthPlugin, # ThreadPoolPlugin, StartPlugin and KeepalivePlugin plugins = DefaultPlugins plugins.append(ClientInfoPlugin) plugins.append(MovementPlugin) plugins.append(WorldPlugin) plugins.append(MicropsiPlugin) plugins.append(ReConnectPlugin) # get spock configs settings = self.get_config() # add plugin-specific settings settings['plugins'] = plugins settings['plugin_settings'] = { MicropsiPlugin: { "micropsi_world": self }, EventPlugin: { "killsignals": False } } # instantiate spock client if not yet done, which in turn instantiates its plugins # ( MicropsiPlugin sets self.spockplugin upon instantiation ) if self.instances['spock'] is None: self.instances['spock'] = Client(plugins=plugins, settings=settings) if self.instances['thread'] is None: # start new thread for minecraft comm" which starts spock client thread = Thread(target=self.instances['spock'].start, args=(settings['server'], settings['port'])) # Note: client.start() is attached in StartPlugin w/ setattr(self.client, 'start', self.start) thread.start() self.instances['thread'] = thread # add_signal_handler(self.kill_minecraft_thread) # once MicropsiPlugin is instantiated and running, initialize micropsi world World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version) # make data accessible to frontend self.data['assets'] = self.assets # copied from jonas' code as is self.current_step = 0 self.first_step = True self.chat_ping_counter = 0 self.the_image = None
def __init__(self, filename, world_type="TimeSeries", name="", owner="", engine=None, uid=None, version=1, config={}): World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version, config=config) self.data['assets'] = self.assets filename = config.get('time_series_data_file', "timeseries.npz") if os.path.isabs(filename): path = filename else: path = os.path.join(cfg['micropsi2']['data_directory'], filename) self.logger.info("loading timeseries from %s for world %s" % (path, uid)) self.realtime_per_entry = int(config['realtime_per_entry']) self.last_realtime_step = datetime.utcnow().timestamp() * 1000 try: with np.load(path) as f: self.timeseries = f['data'] self.ids = f['ids'] self.timestamps = f['timestamps'] except IOError as error: self.logger.error("Could not load data file %s, error was: %s" % (path, str(error))) self.ids = [0] self.timeseries[[0, 0, 0]] self.timestamps = [0] self.len_ts = 1 return # todo use the new configurable world options. dummydata = config['dummy_data'] == "True" z_transform = config['z_transform'] == "True" clip_and_scale = config['clip_and_scale'] == "True" sigmoid = config['sigmoid'] == "True" self.shuffle = config['shuffle'] == "True" if clip_and_scale and sigmoid: self.logger.warn("clip_and_scale and sigmoid cannot both be configured, choosing sigmoid") clip_and_scale = False def sigm(X): """ sigmoid that avoids float overflows for very small inputs. expects a numpy float array. """ cutoff = np.log(np.finfo(X.dtype).max) - 1 X[np.nan_to_num(X) <= -cutoff] = -cutoff return 1. / (1. + np.exp(-X)) if (z_transform or clip_and_scale or sigmoid) and not dummydata: data_z = np.empty_like(self.timeseries) data_z[:] = np.nan pstds = [] for i, row in enumerate(self.timeseries): if not np.all(np.isnan(row)): std = np.sqrt(np.nanvar(row)) if std > 0: if not clip_and_scale: row_z = (row - np.nanmean(row)) / std if clip_and_scale: row_z = row - np.nanmean(row) pstd = std * 4 row_z[np.nan_to_num(row_z) > pstd] = pstd row_z[np.nan_to_num(row_z) < -pstd] = -pstd row_z = ((row_z / pstd) + 1) * 0.5 data_z[i,:] = row_z self.timeseries = data_z if not sigmoid else sigm(data_z) if dummydata: self.logger.warn("! Using dummy data") n_ids = self.timeseries.shape[0] self.timeseries = np.tile(np.random.rand(n_ids,1),(1,10)) self.len_ts = self.timeseries.shape[1]