Project Malmo  0.37.0
AgentHost.h
1 // --------------------------------------------------------------------------------------------------
2 // Copyright (c) 2016 Microsoft Corporation
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 // associated documentation files (the "Software"), to deal in the Software without restriction,
6 // including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all copies or
11 // substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 // --------------------------------------------------------------------------------------------------
19 
20 #ifndef _AGENTHOST_H_
21 #define _AGENTHOST_H_
22 
23 // Local:
24 #include "ArgumentParser.h"
25 #include "ClientConnection.h"
26 #include "ClientPool.h"
27 #include "MissionInitSpec.h"
28 #include "MissionRecord.h"
29 #include "MissionSpec.h"
30 #include "StringServer.h"
31 #include "VideoServer.h"
32 #include "WorldState.h"
33 #include "Logger.h"
34 #include "TCPClient.h"
35 
36 // Boost:
37 #include <boost/thread.hpp>
38 
39 // STL:
40 #include <string>
41 #include <exception>
42 
43 namespace malmo
44 {
45  class MissionException : public std::exception
46  {
47  public:
48  enum MissionErrorCode
49  {
50  MISSION_BAD_ROLE_REQUEST,
51  MISSION_BAD_VIDEO_REQUEST,
52  MISSION_ALREADY_RUNNING,
53  MISSION_INSUFFICIENT_CLIENTS_AVAILABLE,
54  MISSION_TRANSMISSION_ERROR,
55  MISSION_SERVER_WARMING_UP,
56  MISSION_SERVER_NOT_FOUND,
57  MISSION_NO_COMMAND_PORT,
58  MISSION_BAD_INSTALLATION,
59  MISSION_CAN_NOT_KILL_BUSY_CLIENT,
60  MISSION_CAN_NOT_KILL_IRREPLACEABLE_CLIENT
61  };
62 
63  MissionException(const std::string& message, MissionErrorCode code) : message(message), code(code) {}
64  ~MissionException() throw() {}
65  MissionErrorCode getMissionErrorCode() const { return this->code; }
66  std::string getMessage() const { return this->message; }
67  const char* what() const throw() { return this->message.c_str(); }
68 
69  private:
70  std::string message;
71  MissionErrorCode code;
72  };
73 
75  class AgentHost : public ArgumentParser
76  {
77  MALMO_LOGGABLE_OBJECT(AgentHost)
78  public:
79 
81  enum VideoPolicy {
84  };
85 
87  enum RewardsPolicy {
91  };
92 
97  };
98 
101 
104 
111  void startMission( const MissionSpec& mission, const ClientPool& client_pool, const MissionRecordSpec& mission_record, int role, std::string unique_experiment_id );
112 
116  void startMission(const MissionSpec& mission, const MissionRecordSpec& mission_record);
117 
120  bool killClient(const ClientInfo& client);
121 
125 
129 
132  std::string getRecordingTemporaryDirectory() const;
133 
135  void setDebugOutput(bool debug);
136 
139  void setVideoPolicy(VideoPolicy videoPolicy);
140 
143  void setRewardsPolicy(RewardsPolicy rewardsPolicy);
144 
147  void setObservationsPolicy(ObservationsPolicy observationsPolicy);
148 
152  void sendCommand(std::string command);
153 
158  void sendCommand(std::string command, std::string key);
159 
163  boost::shared_ptr<MissionInitSpec> getMissionInit();
164  const boost::shared_ptr<MissionInitSpec> getMissionInit() const;
165 
166  friend std::ostream& operator<<(std::ostream& os, const AgentHost& ah);
167  private:
168 
169  static void testSchemasCompatible();
170  static std::string extractVersionNumber(std::string name);
171 
172  void initializeOurServers(const MissionSpec& mission, const MissionRecordSpec& mission_record, int role, std::string unique_experiment_id);
173  std::string generateMissionInit();
174 
175  ClientPool reserveClients(const ClientPool& client_pool, int clients_required);
176  void findClient(const ClientPool& client_pool);
177  bool findServer(const ClientPool& client_pool);
178 
179  void listenForMissionControlMessages( int port );
180  boost::shared_ptr<VideoServer> listenForVideo(boost::shared_ptr<VideoServer> video_server, int port, short width, short height, short channels, TimestampedVideoFrame::FrameType frametype);
181  void listenForRewards( int port );
182  void listenForObservations( int port );
183 
184  void onMissionControlMessage(TimestampedString message);
185  void onVideo(TimestampedVideoFrame message);
186  void onReward(TimestampedString message);
187  void onObservation(TimestampedString message);
188 
189  void openCommandsConnection();
190 
191  void close();
192  void closeServers();
193  void closeRecording();
194 
195  void processReceivedReward( TimestampedReward reward );
196 
197  boost::asio::io_service io_service;
198  boost::shared_ptr<StringServer> mission_control_server;
199  boost::shared_ptr<VideoServer> video_server;
200  boost::shared_ptr<VideoServer> depth_server;
201  boost::shared_ptr<VideoServer> luminance_server;
202  boost::shared_ptr<VideoServer> colourmap_server;
203  boost::shared_ptr<StringServer> rewards_server;
204  boost::shared_ptr<StringServer> observations_server;
205  boost::optional<boost::asio::io_service::work> work;
206  std::vector<boost::shared_ptr<boost::thread>> background_threads;
207 
208  boost::shared_ptr<ClientConnection> commands_connection;
209  std::ofstream commands_stream;
210 
211  VideoPolicy video_policy;
212  RewardsPolicy rewards_policy;
213  ObservationsPolicy observations_policy;
214 
215  WorldState world_state;
216  mutable boost::mutex world_state_mutex;
217 
218  boost::shared_ptr<MissionInitSpec> current_mission_init;
219  boost::shared_ptr<MissionRecord> current_mission_record;
220  int current_role;
221 
222  Rpc rpc;
223  };
224 
225 }
226 
227 #endif
An agent host mediates between the researcher's code (the agent) and the Mod (the target environment)...
Definition: AgentHost.h:76
void startMission(const MissionSpec &mission, const MissionRecordSpec &mission_record)
Starts a mission running, in the simple case where there is only one agent running on the local machi...
~AgentHost()
Destructor.
RewardsPolicy
Specifies what to do when there are more rewards being received than can be processed.
Definition: AgentHost.h:87
@ LATEST_REWARD_ONLY
Discard all but the most recent reward.
Definition: AgentHost.h:88
@ SUM_REWARDS
Add up all the rewards received. This is the default.
Definition: AgentHost.h:89
@ KEEP_ALL_REWARDS
Attempt to store all the rewards.
Definition: AgentHost.h:90
void setObservationsPolicy(ObservationsPolicy observationsPolicy)
Specifies how you want to deal with multiple observations.
boost::shared_ptr< MissionInitSpec > getMissionInit()
Returns a pointer to the current MissionInitSpec, to allow retrieval of the ports being used.
void sendCommand(std::string command)
Sends a command to the game client.
bool killClient(const ClientInfo &client)
Attempts to remotely shut down a Minecraft client - useful in cases when Minecraft has become old and...
void setRewardsPolicy(RewardsPolicy rewardsPolicy)
Specifies how you want to deal with multiple rewards.
void sendCommand(std::string command, std::string key)
Sends a turn-based command to the game client.
std::string getRecordingTemporaryDirectory() const
Gets the temporary directory being used for the mission record, if recording is taking place.
void setDebugOutput(bool debug)
Switches on/off debug print statements. DEPRECATED - use Logger::setLogging instead.
WorldState peekWorldState() const
Gets the latest world state received from the game.
VideoPolicy
Specifies what to do when there are more video frames being received than can be processed.
Definition: AgentHost.h:81
@ KEEP_ALL_FRAMES
Attempt to store all of the frames.
Definition: AgentHost.h:83
@ LATEST_FRAME_ONLY
Discard all but the most recent frame. This is the default.
Definition: AgentHost.h:82
ObservationsPolicy
Specifies what to do when there are more observations being received than can be processed.
Definition: AgentHost.h:94
@ KEEP_ALL_OBSERVATIONS
Attempt to store all the observations.
Definition: AgentHost.h:96
@ LATEST_OBSERVATION_ONLY
Discard all but the most recent observation. This is the default.
Definition: AgentHost.h:95
WorldState getWorldState()
Gets the latest world state received from the game and resets it to empty.
void startMission(const MissionSpec &mission, const ClientPool &client_pool, const MissionRecordSpec &mission_record, int role, std::string unique_experiment_id)
Starts a mission running.
AgentHost()
Creates an agent host with default settings.
void setVideoPolicy(VideoPolicy videoPolicy)
Specifies how you want to deal with multiple video frames.
A general purpose command-line argument parser.
Definition: ArgumentParser.h:34
Definition: AgentHost.h:46
Specifies a mission to be run.
Definition: MissionSpec.h:37
A map of int:double storing a value on each dimension, with an attached timestamp saying when it was ...
Definition: TimestampedReward.h:35
Structure containing information about a simulation client's address and port.
Definition: ClientInfo.h:34
A pool of expected network locations of Mod clients.
Definition: ClientPool.h:34
Specifies the type of data that should be recorded from the mission.
Definition: MissionRecordSpec.h:42
A string with an attached timestamp saying when it was collected.
Definition: TimestampedString.h:36
An image with an attached timestamp saying when it was collected.
Definition: TimestampedVideoFrame.h:36
FrameType
Definition: TimestampedVideoFrame.h:42
Represents the state of the game world at a moment in time.
Definition: WorldState.h:35