Skip to content

Commit e303baa

Browse files
adityapande-1995azeey
authored andcommitted
Set seed value using CLI (gazebosim#1618)
* expose --seed to CLI Signed-off-by: Aditya <[email protected]>
1 parent 602c07d commit e303baa

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/cmd/cmdsim.rb.in

+15-5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ COMMANDS = { 'sim' =>
104104
" --log-compress When recording, compress final log files. \n"\
105105
" Only valid if recording is enabled. \n"\
106106
"\n"\
107+
" --seed [arg] Pass a custom seed value to the random \n"\
108+
" number generator. \n"\
109+
"\n"\
107110
" --playback [arg] Use logging system to play back states. \n"\
108111
" Argument is path to recorded states. \n"\
109112
"\n"\
@@ -224,7 +227,8 @@ class Cmd
224227
'render_engine_gui' => '',
225228
'render_engine_server' => '',
226229
'wait_gui' => 1,
227-
'headless-rendering' => 0
230+
'headless-rendering' => 0,
231+
'seed' => 0
228232
}
229233

230234
usage = COMMANDS[args[0]]
@@ -317,6 +321,9 @@ class Cmd
317321
opts.on('--version') do
318322
options['version'] = '1'
319323
end
324+
opts.on('--seed [arg]', Integer) do |i|
325+
options['seed'] = i
326+
end
320327

321328
end # opt_parser do
322329

@@ -455,7 +462,7 @@ Please use [GZ_SIM_RESOURCE_PATH] instead."
455462
const char *, int, int, const char *,
456463
int, int, int, const char *, const char *,
457464
const char *, const char *, const char *,
458-
const char *, int, int)'
465+
const char *, int, int, int)'
459466

460467
# Import the runGui function
461468
Importer.extern 'int runGui(const char *, const char *, int, const char *)'
@@ -491,7 +498,8 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info."
491498
options['render_engine_server'], options['render_engine_gui'],
492499
options['file'], options['record-topics'].join(':'),
493500
options['wait_gui'],
494-
options['headless-rendering'])
501+
options['headless-rendering'], options['seed'])
502+
495503
end
496504

497505
guiPid = Process.fork do
@@ -528,8 +536,10 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info."
528536
options['playback'], options['physics_engine'],
529537
options['render_engine_server'], options['render_engine_gui'],
530538
options['file'], options['record-topics'].join(':'),
531-
options['wait_gui'], options['headless-rendering'])
532-
# Otherwise run the gui
539+
options['wait_gui'],
540+
options['headless-rendering'], options['seed'])
541+
# Otherwise run the gui
542+
533543
else options['gui']
534544
ENV['RMT_PORT'] = '1501'
535545
Importer.runGui(options['gui_config'], options['file'],

src/gz.cc

+7-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ extern "C" int runServer(const char *_sdfString,
136136
const char *_playback, const char *_physicsEngine,
137137
const char *_renderEngineServer, const char *_renderEngineGui,
138138
const char *_file, const char *_recordTopics, int _waitGui,
139-
int _headless)
139+
int _headless, int _seed)
140140
{
141141
std::string startingWorldPath{""};
142142
gz::sim::ServerConfig serverConfig;
@@ -396,6 +396,12 @@ extern "C" int runServer(const char *_sdfString,
396396
serverConfig.SetRenderEngineGui(_renderEngineGui);
397397
}
398398

399+
if (_seed != 0)
400+
{
401+
serverConfig.SetSeed(_seed);
402+
gzmsg << "Setting seed value: " << _seed << "\n";
403+
}
404+
399405
// Create the Gazebo server
400406
gz::sim::Server server(serverConfig);
401407

src/gz.hh

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern "C" const char *worldInstallDir();
5757
/// it receives a world path from GUI.
5858
/// null to record the default topics.
5959
/// \param[in] _headless True if server rendering should run headless
60+
/// \param[in] _seed --seed value to be used for random number generator.
6061
/// \return 0 if successful, 1 if not.
6162
extern "C" int runServer(const char *_sdfString,
6263
int _iterations, int _run, float _hz, int _levels,
@@ -65,7 +66,7 @@ extern "C" int runServer(const char *_sdfString,
6566
int _logCompress, const char *_playback,
6667
const char *_physicsEngine, const char *_renderEngineServer,
6768
const char *_renderEngineGui, const char *_file,
68-
const char *_recordTopics, int _waitGui, int _headless);
69+
const char *_recordTopics, int _waitGui, int _headless, int _seed);
6970

7071
/// \brief External hook to run simulation GUI.
7172
/// \param[in] _guiConfig Path to Gazebo GUI configuration file.

src/gz_TEST.cc

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(CachedFuelWorld))
109109
<< output;
110110
}
111111

112+
/////////////////////////////////////////////////
113+
TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(RandomSeedValue))
114+
{
115+
std::string cmd = kGzCommand + " -r -v 4 --seed 5 --iterations 5";
116+
std::cout << "Running command [" << cmd << "]" << std::endl;
117+
118+
std::string output = customExecStr(cmd);
119+
EXPECT_NE(output.find("Setting seed value"), std::string::npos)
120+
<< output;
121+
}
122+
112123
/////////////////////////////////////////////////
113124
TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(SimServer))
114125
{

0 commit comments

Comments
 (0)