Batch Optimization
The SCONE Command-line Interface
If you need to automate multiple optimizations, you can use the sconecmd
, the SCONE command-line interface. By default, it is located in C:\Program Files\SCONE\sconecmd.exe
, and can be used as follows:
"C:\Program Files\SCONE\sconecmd.exe" -o "your_scenario.scone"
The -o
command line starts an optimization of a scenario. The -e
command line switch can be used to evaluate a .scone
or a .par
file.
Changing Scenario Parameters
You can change any parameter in your .scone scenario by specifying it in the command line, for example:
sconecmd.exe -o "scenario.scone" CmaOptimizer.random_seed=10
You can also access settings by index, which is useful for settings that are not uniquely identifiable by name:
sconecmd.exe -o "scenario.scone" CmaOptimizer.SimulationObjective.CompositeController.#1.value=10
This sets the value
parameter for first child of CompositeController
. The second, third, etc. element can be accessed via #2
, #3
, etc.
Batch Optimizations
For batch processing, you can create a Windows .bat
file which contains multiple commands:
set SCONECMD="C:\Program Files\SCONE\bin\sconecmd.exe" %SCONECMD% -o "scenario.scone" CmaOptimizer.random_seed=10 %SCONECMD% -o "scenario.scone" CmaOptimizer.SimulationObjective.ModelOpenSim3.model_file=data/model.osim
If you wish to run multiple optimizations in parallel, you can do so by prefixing your command with start
. The first argument after start (“SCONE”) is the title of the console window that is spawned.
set SCONECMD="C:\Program Files\SCONE\bin\sconecmd.exe" start "SCONE" %SCONECMD% -o "scenario.scone" CmaOptimizer.random_seed=10 start "SCONE" %SCONECMD% -o "scenario.scone" CmaOptimizer.SimulationObjective.ModelOpenSim3.model_file=data/model.osim
If you wish to optimize all scenarios in a folder, you can use the following .bat script to optimize all .scone scenarios in the current directory:
set SCONECMD="C:\Program Files\SCONE\bin\sconecmd.exe" for %%f in (*.scone) do ( start "%%f" %SCONECMD% -o "%%f" timeout 1 )
The SCONE Python Interface
Since SCONE version 2.4.0, it is also possible to run batch optimizations using the SconePy Python interface:
import numpy as np import time from sconetools import sconepy # Load an example scenario scenario = sconepy.load_scenario("../Examples/Jump2D - H0918 - Hyfydy.scone"); # Start multiple optimizations with different settings num_optimizers = 6 opts = [] for i in range(0, num_optimizers): # Change some settings based on i scenario.set("CmaOptimizer.random_seed", str(i+1)) scenario.set("CmaOptimizer.SimulationObjective.FeedForwardController.symmetric", str(i%2)) # Start the optimization as background task opts.append(scenario.start_optimization()) # Wait for all optimizations to finish num_finished = 0 while num_finished < num_optimizers: num_finished = 0 time.sleep(1) # Iterate over all optimizers for i in range(0, num_optimizers): # Create a status string str = f"Optimization {i}: step={opts[i].current_step()} fitness={opts[i].fitness():.2f}"; if opts[i].finished(): # This optimization is finished, add to status string str += " FINISHED" num_finished += 1 elif opts[i].current_step() >= 1000: # Optional: terminal optimizations after 1000 steps str += " TERMINATING" opts[i].terminate() # Print status print(str, flush=True) # At this point, all optimizations have finished for i in range(0, num_optimizers): print(f"Finished optimization {i}: steps={opts[i].current_step()} fitness={opts[i].fitness():.2f}", flush=True)