Skip to content

Commit

Permalink
Set seed value using CLI (#1618)
Browse files Browse the repository at this point in the history
* expose --seed to CLI
Signed-off-by: Aditya <aditya050995@gmail.com>
  • Loading branch information
adityapande-1995 authored Aug 24, 2022
1 parent 734ff3a commit 24b1e54
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/cmd/cmdsim.rb.in
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ COMMANDS = { 'sim' =>
" --log-compress When recording, compress final log files. \n"\
" Only valid if recording is enabled. \n"\
"\n"\
" --seed [arg] Pass a custom seed value to the random \n"\
" number generator. \n"\
"\n"\
" --playback [arg] Use logging system to play back states. \n"\
" Argument is path to recorded states. \n"\
"\n"\
Expand Down Expand Up @@ -224,7 +227,8 @@ class Cmd
'render_engine_gui' => '',
'render_engine_server' => '',
'wait_gui' => 1,
'headless-rendering' => 0
'headless-rendering' => 0,
'seed' => 0
}

usage = COMMANDS[args[0]]
Expand Down Expand Up @@ -317,6 +321,9 @@ class Cmd
opts.on('--version') do
options['version'] = '1'
end
opts.on('--seed [arg]', Integer) do |i|
options['seed'] = i
end

end # opt_parser do

Expand Down Expand Up @@ -455,7 +462,7 @@ Please use [GZ_SIM_RESOURCE_PATH] instead."
const char *, int, int, const char *,
int, int, int, const char *, const char *,
const char *, const char *, const char *,
const char *, int, int)'
const char *, int, int, int)'

# Import the runGui function
Importer.extern 'int runGui(const char *, const char *, int, const char *)'
Expand Down Expand Up @@ -490,7 +497,8 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info."
options['render_engine_server'], options['render_engine_gui'],
options['file'], options['record-topics'].join(':'),
options['wait_gui'],
options['headless-rendering'])
options['headless-rendering'], options['seed'])

end

guiPid = Process.fork do
Expand Down Expand Up @@ -527,8 +535,10 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info."
options['playback'], options['physics_engine'],
options['render_engine_server'], options['render_engine_gui'],
options['file'], options['record-topics'].join(':'),
options['wait_gui'], options['headless-rendering'])
# Otherwise run the gui
options['wait_gui'],
options['headless-rendering'], options['seed'])
# Otherwise run the gui

else options['gui']
if plugin.end_with? ".dylib"
puts "`gz sim` currently only works with the -s argument on macOS.
Expand Down
8 changes: 7 additions & 1 deletion src/gz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ extern "C" int runServer(const char *_sdfString,
const char *_playback, const char *_physicsEngine,
const char *_renderEngineServer, const char *_renderEngineGui,
const char *_file, const char *_recordTopics, int _waitGui,
int _headless)
int _headless, int _seed)
{
std::string startingWorldPath{""};
gz::sim::ServerConfig serverConfig;
Expand Down Expand Up @@ -396,6 +396,12 @@ extern "C" int runServer(const char *_sdfString,
serverConfig.SetRenderEngineGui(_renderEngineGui);
}

if (_seed != 0)
{
serverConfig.SetSeed(_seed);
gzmsg << "Setting seed value: " << _seed << "\n";
}

// Create the Gazebo server
gz::sim::Server server(serverConfig);

Expand Down
3 changes: 2 additions & 1 deletion src/gz.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ extern "C" const char *worldInstallDir();
/// it receives a world path from GUI.
/// null to record the default topics.
/// \param[in] _headless True if server rendering should run headless
/// \param[in] _seed --seed value to be used for random number generator.
/// \return 0 if successful, 1 if not.
extern "C" int runServer(const char *_sdfString,
int _iterations, int _run, float _hz, int _levels,
Expand All @@ -65,7 +66,7 @@ extern "C" int runServer(const char *_sdfString,
int _logCompress, const char *_playback,
const char *_physicsEngine, const char *_renderEngineServer,
const char *_renderEngineGui, const char *_file,
const char *_recordTopics, int _waitGui, int _headless);
const char *_recordTopics, int _waitGui, int _headless, int _seed);

/// \brief External hook to run simulation GUI.
/// \param[in] _guiConfig Path to Gazebo GUI configuration file.
Expand Down
11 changes: 11 additions & 0 deletions src/gz_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(CachedFuelWorld))
<< output;
}

/////////////////////////////////////////////////
TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(RandomSeedValue))
{
std::string cmd = kGzCommand + " -r -v 4 --seed 5 --iterations 5";
std::cout << "Running command [" << cmd << "]" << std::endl;

std::string output = customExecStr(cmd);
EXPECT_NE(output.find("Setting seed value"), std::string::npos)
<< output;
}

/////////////////////////////////////////////////
TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(SimServer))
{
Expand Down

0 comments on commit 24b1e54

Please sign in to comment.