Skip to content

Commit ff0edb4

Browse files
author
Chris Elion
authored
better logging for ports and versions (#3048)
1 parent 7562a25 commit ff0edb4

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
@@ -42,6 +42,7 @@ namespace MLAgents
4242
public abstract class Academy : MonoBehaviour
4343
{
4444
const string k_ApiVersion = "API-12";
45+
const int k_EditorTrainingPort = 5004;
4546

4647
/// Temporary storage for global gravity value
4748
/// Used to restore oringal value when deriving Academy modifies it
@@ -149,7 +150,7 @@ public void LazyInitialization()
149150
}
150151

151152
// Used to read Python-provided environment parameters
152-
static int ReadArgs()
153+
static int ReadPortFromArgs()
153154
{
154155
var args = System.Environment.GetCommandLineArgs();
155156
var inputPort = "";
@@ -161,7 +162,22 @@ static int ReadArgs()
161162
}
162163
}
163164

164-
return int.Parse(inputPort);
165+
try
166+
{
167+
return int.Parse(inputPort);
168+
}
169+
catch
170+
{
171+
// No arg passed, or malformed port number.
172+
#if UNITY_EDITOR
173+
// Try connecting on the default editor port
174+
return k_EditorTrainingPort;
175+
#else
176+
// This is an executable, so we don't try to connect.
177+
return -1;
178+
#endif
179+
}
180+
165181
}
166182

167183
/// <summary>
@@ -179,23 +195,15 @@ void InitializeEnvironment()
179195

180196

181197
// Try to launch the communicator by using the arguments passed at launch
182-
try
198+
var port = ReadPortFromArgs();
199+
if (port > 0)
183200
{
184201
Communicator = new RpcCommunicator(
185202
new CommunicatorInitParameters
186203
{
187-
port = ReadArgs()
188-
});
189-
}
190-
catch
191-
{
192-
#if UNITY_EDITOR
193-
Communicator = new RpcCommunicator(
194-
new CommunicatorInitParameters
195-
{
196-
port = 5004
197-
});
198-
#endif
204+
port = port
205+
}
206+
);
199207
}
200208

201209
if (Communicator != null)
@@ -217,6 +225,10 @@ void InitializeEnvironment()
217225
}
218226
catch
219227
{
228+
Debug.Log($"" +
229+
$"Couldn't connect to trainer on port {port} using API version {k_ApiVersion}. " +
230+
"Will perform inference instead."
231+
);
220232
Communicator = null;
221233
}
222234

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def __init__(
114114
self.executable_launcher(file_name, docker_training, no_graphics, args)
115115
else:
116116
logger.info(
117-
"Start training by pressing the Play button in the Unity Editor."
117+
f"Listening on port {self.port}. "
118+
f"Start training by pressing the Play button in the Unity Editor."
118119
)
119120
self._loaded = True
120121

@@ -130,9 +131,10 @@ def __init__(
130131
if self._unity_version != self._version_:
131132
self._close()
132133
raise UnityEnvironmentException(
133-
"The API number is not compatible between Unity and python. Python API : {0}, Unity API : "
134-
"{1}.\nPlease go to https://github.com/Unity-Technologies/ml-agents to download the latest version "
135-
"of ML-Agents.".format(self._version_, self._unity_version)
134+
f"The API number is not compatible between Unity and python. "
135+
f"Python API: {self._version_}, Unity API: {self._unity_version}.\n"
136+
f"Please go to https://github.com/Unity-Technologies/ml-agents/releases/tag/latest_release"
137+
f"to download the latest version of ML-Agents."
136138
)
137139
self._env_state: Dict[str, BatchedStepResult] = {}
138140
self._env_specs: Dict[str, AgentGroupSpec] = {}

ml-agents/mlagents/trainers/learn.py

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

6060

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

6968

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

173-
parser.add_argument("--version", action="version", version=get_version_string())
172+
parser.add_argument("--version", action="version", version="")
174173

175174
eng_conf = parser.add_argument_group(title="Engine Configuration")
176175
eng_conf.add_argument(
@@ -438,6 +437,7 @@ def main():
438437
)
439438
except Exception:
440439
print("\n\n\tUnity Technologies\n")
440+
print(get_version_string())
441441
options = parse_command_line()
442442
trainer_logger = logging.getLogger("mlagents.trainers")
443443
env_logger = logging.getLogger("mlagents.envs")

0 commit comments

Comments
 (0)