...
B.1: Declare properties of the class
We create member variables using the OpenSim_DECLARE_PROPERTY and OpenSim_DECLARE_OPTIONAL_PROPERTY macros, which are described here. For simplicity, we've omitted many of the properties we have in the actual source code. For a member variable to be de/serializable, it must be delcared via this macro. Some of the properties help with general setup (e.g., results_directory, model_filename), while others modify the computation of the objective function value. These macros define getters/setters for us; we'll see the usage of the getters in other parts of the code.
Code Block | ||||
---|---|---|---|---|
| ||||
// General properties.
OpenSim_DECLARE_PROPERTY(results_directory, std::string,
"Directory in which to save optimization log and results");
OpenSim_DECLARE_PROPERTY(model_filename, std::string,
"Specifies path to model file, WITH .osim extension");
OpenSim_DECLARE_PROPERTY(num_optim_spline_points, int,
"Number of points being optimized in each spline function. "
"Constant across all splines. If an initial_parameters_filename is "
"provided, the functions specified in that file must have the "
"correct number of points. We do not error-check for this.");
// Properties related to the objective function.
OpenSim_DECLARE_PROPERTY(anterior_legs_down_weight, double,
"Adds terms to the objective to minimize final value of "
"(roll - Pi) and related speeds");
OpenSim_DECLARE_PROPERTY(posterior_legs_down_weight, double,
"Adds terms to the objective to minimize final value of "
"(twist - 0) and related speeds");
OpenSim_DECLARE_PROPERTY(sagittal_symmetry_weight, double,
"Adds a term to the objective to minimize final value of "
"(hunch + 2 * pitch)");
// Modifying the model before optimizing.
OpenSim_DECLARE_PROPERTY(use_coordinate_limit_forces, bool,
"TRUE: use coordinate limit forces, "
"FALSE: ignore coordinate limit forces");
// Setting initial parameters for the optimization.
OpenSim_DECLARE_OPTIONAL_PROPERTY(initial_parameters_filename, std::string,
"File containing FunctionSet of SimmSpline's used to initialize "
"optimization parameters. If not provided, initial parameters are "
"all 0.0, and this element must be DELETED from the XML file "
"(cannot just leave it blank). The name of each function must be "
"identical to that of the actuator it is for. x values are ignored. "
"The time values that are actually used in the simulation are "
"equally spaced from t = 0 to t = 1.0 s, and there should be "
"as many points in each function as given by the num_optim_spline_points "
"property. y values should be nondimensional and between -1 and 1 "
"(negative values normalized by minControl if minControl is "
"negative; otherwise the value is normalized by maxControl). "
"NOTE that the output optimized splines are NOT NONDIMENSIONAL. "
"Be careful, we do not do any error checking."); |
...
error checking."); |
B.2: Constructors
Classes are serialized by calling their print() method. To be able to deserialize a file, the associated class must have a constructor that takes in a filename, and passes it to the constructor of Object. The properties in the class are overwritten by the values in the serialization by calling the method Object::updateFromXMLDocument().
Code Block | ||||
---|---|---|---|---|
| ||||
FlippinFelinesOptimizerTool() : Object() { setNull(); constructProperties(); } // NOTE: This constructor allows for the de/serialization. FlippinFelinesOptimizerTool(const std::string &aFileName, bool aUpdateFromXMLNode=true) : Object(aFileName, aUpdateFromXMLNode) { setNull(); constructProperties(); // Must be called after constructProperties(): updateFromXMLDocument(); } |
Before we can do that though, we need to construct the properties we've declared.
B.3: Constructing the properties we've declared
These constructProperty functions, generated by the macros in part B.1, require an initial/default value for the property.
Code Block | ||||
---|---|---|---|---|
| ||||
void constructProperties() { constructProperty_results_directory("results"); constructProperty_model_filename("flippinfelines_*FILL THIS IN*.osim"); constructProperty_num_optim_spline_points(20); constructProperty_anterior_legs_down_weight(1.0); constructProperty_posterior_legs_down_weight(1.0); constructProperty_sagittal_symmetry_weight(1.0); constructProperty_use_coordinate_limit_forces(true); constructProperty_initial_parameters_filename(""); } |
A sample serialization
Below is what the XML serialization of this class looks like. This is exactly what we meant by theĀ optimizer_setup_file.xml
above. This is a simplified version of what's generated by the executable optimize_input_template, which is generated from optimize_input_template.cpp. We've given you this file, but we do not discuss it in this tutorial.
...