Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions UnitySDK/Assets/ML-Agents/Scripts/Academy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public EnvironmentConfiguration(
public abstract class Academy : MonoBehaviour
{
const string k_ApiVersion = "API-12";
const int k_EditorTrainingPort = 5004;

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

// Used to read Python-provided environment parameters
static int ReadArgs()
static int ReadPortFromArgs()
{
var args = System.Environment.GetCommandLineArgs();
var inputPort = "";
Expand All @@ -252,7 +253,22 @@ static int ReadArgs()
}
}

return int.Parse(inputPort);
try
{
return int.Parse(inputPort);
}
catch
{
// No arg passed, or malformed port number.
#if UNITY_EDITOR
// Try connecting on the default editor port
return k_EditorTrainingPort;
#else
// This is an executable, so we don't try to connect.
return -1;
#endif
}

}

/// <summary>
Expand All @@ -267,23 +283,15 @@ void InitializeEnvironment()
InitializeAcademy();

// Try to launch the communicator by using the arguments passed at launch
try
var port = ReadPortFromArgs();
if (port > 0)
{
Communicator = new RpcCommunicator(
new CommunicatorInitParameters
{
port = ReadArgs()
});
}
catch
{
#if UNITY_EDITOR
Communicator = new RpcCommunicator(
new CommunicatorInitParameters
{
port = 5004
});
#endif
port = port
}
);
}

if (Communicator != null)
Expand All @@ -308,6 +316,10 @@ void InitializeEnvironment()
}
catch
{
Debug.Log($"" +
$"Couldn't connect to trainer on port {port} using API version {k_ApiVersion}. " +
"Will perform inference instead."
);
Communicator = null;
}

Expand Down
10 changes: 6 additions & 4 deletions ml-agents-envs/mlagents/envs/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def __init__(
self.executable_launcher(file_name, docker_training, no_graphics, args)
else:
logger.info(
"Start training by pressing the Play button in the Unity Editor."
f"Listening on port {self.port}. "
f"Start training by pressing the Play button in the Unity Editor."
)
self._loaded = True

Expand All @@ -108,9 +109,10 @@ def __init__(
if self._unity_version != self._version_:
self._close()
raise UnityEnvironmentException(
"The API number is not compatible between Unity and python. Python API : {0}, Unity API : "
"{1}.\nPlease go to https://github.com/Unity-Technologies/ml-agents to download the latest version "
"of ML-Agents.".format(self._version_, self._unity_version)
f"The API number is not compatible between Unity and python. "
f"Python API: {self._version_}, Unity API: {self._unity_version}.\n"
f"Please go to https://github.com/Unity-Technologies/ml-agents/releases/tag/latest_release"
f"to download the latest version of ML-Agents."
)
self._n_agents: Dict[str, int] = {}
self._is_first_message = True
Expand Down
14 changes: 7 additions & 7 deletions ml-agents/mlagents/trainers/learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ def from_argparse(args: Any) -> "CommandLineOptions":


def get_version_string() -> str:
return f""" Version information:\n
ml-agents: {mlagents.trainers.__version__},
ml-agents-envs: {mlagents.envs.__version__},
Communicator API: {UnityEnvironment.API_VERSION},
TensorFlow: {tf_utils.tf.__version__}
"""
return f""" Version information:
ml-agents: {mlagents.trainers.__version__},
ml-agents-envs: {mlagents.envs.__version__},
Communicator API: {UnityEnvironment.API_VERSION},
TensorFlow: {tf_utils.tf.__version__}"""


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

parser.add_argument("--version", action="version", version=get_version_string())
parser.add_argument("--version", action="version", version="")

args = parser.parse_args(argv)
return CommandLineOptions.from_argparse(args)
Expand Down Expand Up @@ -395,6 +394,7 @@ def main():
)
except Exception:
print("\n\n\tUnity Technologies\n")
print(get_version_string())
options = parse_command_line()
trainer_logger = logging.getLogger("mlagents.trainers")
env_logger = logging.getLogger("mlagents.envs")
Expand Down