Skip to content

Commit 557488c

Browse files
author
Chris Elion
authored
better logging for ports and versions (#3048) (#3069)
1 parent a64d02a commit 557488c

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

UnitySDK/Assets/ML-Agents/Scripts/Academy.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public EnvironmentConfiguration(
9393
public abstract class Academy : MonoBehaviour
9494
{
9595
const string k_ApiVersion = "API-12";
96+
const int k_EditorTrainingPort = 5004;
9697

9798
/// Temporary storage for global gravity value
9899
/// Used to restore oringal value when deriving Academy modifies it
@@ -240,7 +241,7 @@ public void LazyInitialization()
240241
}
241242

242243
// Used to read Python-provided environment parameters
243-
static int ReadArgs()
244+
static int ReadPortFromArgs()
244245
{
245246
var args = System.Environment.GetCommandLineArgs();
246247
var inputPort = "";
@@ -252,7 +253,22 @@ static int ReadArgs()
252253
}
253254
}
254255

255-
return int.Parse(inputPort);
256+
try
257+
{
258+
return int.Parse(inputPort);
259+
}
260+
catch
261+
{
262+
// No arg passed, or malformed port number.
263+
#if UNITY_EDITOR
264+
// Try connecting on the default editor port
265+
return k_EditorTrainingPort;
266+
#else
267+
// This is an executable, so we don't try to connect.
268+
return -1;
269+
#endif
270+
}
271+
256272
}
257273

258274
/// <summary>
@@ -267,23 +283,15 @@ void InitializeEnvironment()
267283
InitializeAcademy();
268284

269285
// Try to launch the communicator by using the arguments passed at launch
270-
try
286+
var port = ReadPortFromArgs();
287+
if (port > 0)
271288
{
272289
Communicator = new RpcCommunicator(
273290
new CommunicatorInitParameters
274291
{
275-
port = ReadArgs()
276-
});
277-
}
278-
catch
279-
{
280-
#if UNITY_EDITOR
281-
Communicator = new RpcCommunicator(
282-
new CommunicatorInitParameters
283-
{
284-
port = 5004
285-
});
286-
#endif
292+
port = port
293+
}
294+
);
287295
}
288296

289297
if (Communicator != null)
@@ -308,6 +316,10 @@ void InitializeEnvironment()
308316
}
309317
catch
310318
{
319+
Debug.Log($"" +
320+
$"Couldn't connect to trainer on port {port} using API version {k_ApiVersion}. " +
321+
"Will perform inference instead."
322+
);
311323
Communicator = null;
312324
}
313325

ml-agents-envs/mlagents/envs/environment.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def __init__(
9292
self.executable_launcher(file_name, docker_training, no_graphics, args)
9393
else:
9494
logger.info(
95-
"Start training by pressing the Play button in the Unity Editor."
95+
f"Listening on port {self.port}. "
96+
f"Start training by pressing the Play button in the Unity Editor."
9697
)
9798
self._loaded = True
9899

@@ -108,9 +109,10 @@ def __init__(
108109
if self._unity_version != self._version_:
109110
self._close()
110111
raise UnityEnvironmentException(
111-
"The API number is not compatible between Unity and python. Python API : {0}, Unity API : "
112-
"{1}.\nPlease go to https://github.com/Unity-Technologies/ml-agents to download the latest version "
113-
"of ML-Agents.".format(self._version_, self._unity_version)
112+
f"The API number is not compatible between Unity and python. "
113+
f"Python API: {self._version_}, Unity API: {self._unity_version}.\n"
114+
f"Please go to https://github.com/Unity-Technologies/ml-agents/releases/tag/latest_release"
115+
f"to download the latest version of ML-Agents."
114116
)
115117
self._n_agents: Dict[str, int] = {}
116118
self._is_first_message = True

ml-agents/mlagents/trainers/learn.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ def from_argparse(args: Any) -> "CommandLineOptions":
5757

5858

5959
def get_version_string() -> str:
60-
return f""" Version information:\n
61-
ml-agents: {mlagents.trainers.__version__},
62-
ml-agents-envs: {mlagents.envs.__version__},
63-
Communicator API: {UnityEnvironment.API_VERSION},
64-
TensorFlow: {tf_utils.tf.__version__}
65-
"""
60+
return f""" Version information:
61+
ml-agents: {mlagents.trainers.__version__},
62+
ml-agents-envs: {mlagents.envs.__version__},
63+
Communicator API: {UnityEnvironment.API_VERSION},
64+
TensorFlow: {tf_utils.tf.__version__}"""
6665

6766

6867
def parse_command_line(argv: Optional[List[str]] = None) -> CommandLineOptions:
@@ -171,7 +170,7 @@ def parse_command_line(argv: Optional[List[str]] = None) -> CommandLineOptions:
171170
"--cpu", default=False, action="store_true", help="Run with CPU only"
172171
)
173172

174-
parser.add_argument("--version", action="version", version=get_version_string())
173+
parser.add_argument("--version", action="version", version="")
175174

176175
args = parser.parse_args(argv)
177176
return CommandLineOptions.from_argparse(args)
@@ -395,6 +394,7 @@ def main():
395394
)
396395
except Exception:
397396
print("\n\n\tUnity Technologies\n")
397+
print(get_version_string())
398398
options = parse_command_line()
399399
trainer_logger = logging.getLogger("mlagents.trainers")
400400
env_logger = logging.getLogger("mlagents.envs")

0 commit comments

Comments
 (0)