Project Overview

MocoExtendProblem (MEP) is a sophisticated framework designed to rapidly develop novel goals for biomechanical optimal control problems using OpenSim Moco and MATLAB. This project bridges the gap between C++ development and MATLAB scripting, enabling researchers to create custom optimization objectives without rebuilding OpenSim or generating complex .omoco files.
The framework provides a streamlined workflow for:
- Custom Goal Development: Create novel optimization objectives in C++ with provided templates
- Seamless Integration: Automatically generate MATLAB interfaces for custom goals
- Rapid Prototyping: Test and iterate on new biomechanical objectives quickly
- Version Compatibility: Support for OpenSim 4.2-4.5 with automatic compatibility handling
Key Innovation
MocoExtendProblem eliminates the traditional bottleneck in biomechanical optimization research. Instead of requiring researchers to rebuild OpenSim or manually create complex interface files, MEP provides a procedural approach that automatically generates the necessary MATLAB wrappers and MEX interfaces from C++ goal implementations. This dramatically reduces development time and enables rapid iteration on novel biomechanical objectives.
System Architecture
MocoExtendProblem employs a multi-layered architecture that seamlessly bridges C++ goal implementations with MATLAB scripting environments.
Core Components
Component | Purpose | Key Features |
---|---|---|
build.m |
Main build orchestrator | CMake integration, MEX compilation, procedural class generation |
custom_goals/ |
C++ goal implementations | Template-based goal development, OpenSim 4.5+ compatibility |
custom_goals_compat/ |
Legacy goal implementations | OpenSim 4.2-4.4 compatibility, backward compatibility |
utils/ |
Build utilities | Regex parsing, procedural code generation, MEX interface creation |
extend_problem.m |
MATLAB wrapper class | Dynamic method generation, goal injection, MocoProblem integration |
Build Process
The framework uses a sophisticated build process that automatically adapts to different OpenSim versions:
- Version Detection: Automatically detects OpenSim version and selects appropriate goal directory
- CMake Integration: Generates Visual Studio solutions for C++ compilation
- Procedural Generation: Uses regex parsing to automatically generate MATLAB wrappers
- MEX Compilation: Creates MATLAB-executable interfaces for custom goals
Integration Flow
- C++ Goal Development: Create custom goals using provided templates
- Automatic Build: Run
build.m
to generate interfaces - MATLAB Integration: Use generated
extend_problem.m
class - Goal Injection: Add custom goals to MocoProblem instances
Key Features
1. Pre-built Custom Goals
MocoExtendProblem includes several specialized goals for biomechanical analysis:
- MocoActivationGoal: Minimize muscle activations (not squared)
- MocoActivationSquaredGoal: Minimize squared muscle activations
- MocoBOSGoal: Base of Support tracking for mass center
- MocoCoordinateAccelerationGoal: Explicit coordinate acceleration minimization
- MocoCOPGoal: Center of Pressure tracking
- MocoZMPGoal: Zero Moment Point optimization
- MocoMaxCoordinateGoal: Maximum coordinate value minimization
- MocoMarkerAccelerationGoal: Marker acceleration tracking
2. Template-Based Development
Rapid goal development using provided templates:
- Copy-Paste Workflow: Duplicate existing goals as templates
- Automated Renaming: Regex-based file and class name replacement
- Version Compatibility: Automatic selection of appropriate template directory
- Standardized Structure: Consistent file organization across all goals
3. Automated Interface Generation
Intelligent parsing and code generation:
- Regex Parsing: Automatically extracts setter functions and arguments
- Dynamic Method Generation: Creates MATLAB methods for each custom goal
- MEX Interface Creation: Generates C++ to MATLAB bridges
- Error Handling: Comprehensive validation and error reporting
4. Cross-Platform Compatibility
- OpenSim Version Support: 4.2-4.5 with automatic compatibility handling
- MATLAB Integration: Tested on MATLAB 2022a+
- Build System: CMake 3.23.3+ with Visual Studio 2019+
- Windows Focus: Optimized for Windows development environment
5. Research Workflow Integration
- Version Control Ready: Designed for submodule integration
- Lab Management: Supports multi-user development environments
- Testing Framework: Comprehensive test suite for validation
- Documentation: Extensive tutorials and examples
Usage Examples
Basic Setup and Build
% Clone the repository
git clone git@github.com:Aravind-Sundararajan/MocoExtendProblem.git
% Navigate to project directory
cd MocoExtendProblem
% Run the build script
build.m
Creating a New Custom Goal
% 1. Copy an existing goal as template
% Copy custom_goals/MocoActivationGoal to custom_goals/MocoCustomGoal
% 2. Rename files and update content
% - MocoActivationGoal.cpp → MocoCustomGoal.cpp
% - MocoActivationGoal.h → MocoCustomGoal.h
% - osimMocoActivationGoalDLL.h → osimMocoCustomGoalDLL.h
% - RegisterTypes_osimMocoActivationGoal.cpp → RegisterTypes_osimMocoCustomGoal.cpp
% - RegisterTypes_osimMocoActivationGoal.h → RegisterTypes_osimMocoCustomGoal.h
% 3. Update class names and include guards in all files
% Use find-and-replace: MocoActivationGoal → MocoCustomGoal
% 4. Rebuild the project
build.m
Using Custom Goals in MATLAB
% Create a MocoProblem
problem = MocoProblem();
% Get C-style pointer to the problem
cptr = uint64(problem.getCPtr(problem));
% Create ExtendProblem instance
ep = extend_problem(cptr);
% Add custom goals with parameters
ep.addMocoCustomGoal('custom_goal', 1.0, true, false, false, false);
ep.addMocoBOSGoal('bos_goal', 0.5, true, true, false);
ep.addMocoActivationGoal('activation_goal', 0.3, true, false, false);
% Solve the problem
solution = study.solve();
Complete Example Workflow
% Load OpenSim model
model = Model('gait2392.osim');
% Create MocoStudy
study = MocoStudy();
problem = study.updProblem();
% Set model and time bounds
problem.setModel(model);
problem.setTimeBounds(0, 1.0);
% Add standard Moco goals
problem.addGoal(MocoControlGoal('control_goal'));
% Add custom goals using MEP
cptr = uint64(problem.getCPtr(problem));
ep = extend_problem(cptr);
% Add custom biomechanical objectives
ep.addMocoBOSGoal('base_of_support', 1.0, true, true, false);
ep.addMocoActivationSquaredGoal('activation_squared', 0.5, true, false, false);
% Configure solver
solver = study.initCasADiSolver();
solver.set_num_mesh_intervals(50);
% Solve and analyze
solution = study.solve();
solution.write('custom_goals_solution.sto');
Advanced Goal Implementation
Custom goals can implement sophisticated biomechanical objectives:
- Multi-objective Optimization: Combine multiple biomechanical criteria
- Dynamic Constraints: Time-varying optimization objectives
- Model-specific Goals: Tailored to specific musculoskeletal models
- Performance Metrics: Energy efficiency, stability, and coordination objectives
Technical Implementation
Build System Architecture
The framework uses a sophisticated build system that adapts to different OpenSim versions:
- CMake Integration: Cross-platform build configuration
- Visual Studio Integration: Native Windows development environment
- MEX Compilation: MATLAB-executable interface generation
- Dynamic Library Loading: Runtime goal library management
Procedural Code Generation
Process | Implementation | Output |
---|---|---|
Header Parsing | Regex-based function extraction | Method signatures and parameters |
Class Generation | Template-based code generation | extend_problem.m MATLAB class |
MEX Interface | Automated MEX compilation | C++ to MATLAB bridge |
Library Linking | Dynamic library management | Runtime goal loading |
Memory Management
Robust memory management for C++/MATLAB integration:
- Smart Pointers: Automatic resource management in C++
- MEX Memory Handling: Proper cleanup of MATLAB arrays
- Library Lifecycle: Dynamic loading and unloading of goal libraries
- Error Recovery: Graceful handling of memory allocation failures
Performance Optimizations
- Compile-time Optimization: Release builds with optimization flags
- Lazy Loading: Goals loaded only when needed
- Efficient Parsing: Optimized regex patterns for header parsing
- Minimal Overhead: Direct C++ to MATLAB communication
Dependencies and Requirements
- OpenSim: Version 4.2-4.5 with MATLAB scripting support
- MATLAB: 2022a+ with MEX support
- Visual Studio: 2019+ with C++ development tools
- CMake: 3.23.3+ for build configuration
- Windows: Primary development platform
Error Handling and Validation
Comprehensive error handling throughout the framework:
- Build Validation: Automatic verification of compilation success
- Runtime Checks: Validation of goal parameters and model compatibility
- Version Compatibility: Automatic detection and handling of OpenSim versions
- User Feedback: Clear error messages and troubleshooting guidance
Testing and Validation
The framework includes comprehensive testing:
- Unit Tests: Individual goal functionality validation
- Integration Tests: End-to-end workflow testing
- Performance Tests: Optimization solver performance validation
- Compatibility Tests: Cross-version OpenSim compatibility