Hi all,
I would like to perform an optimization with the OpenSim model using the API, preferably through MATLAB. However, I could not find out how I get all the necessary data from the model.
My thought was to start from the Static Optimization from the Analysis Tool but I cannot find its implementation file. I can find the header files but I would like to get a clear view of how to get the system parameters necessary for:
- contstraintFunc
- objectiveFunc (in Analyse Tool, the computation of muscle activations squared)
- gradientFunc
- constraintJacobian
These functions should be described somewhere for the Static Optimization Tool as well but I can't find them in any file.
I realize that this might, in its fundamentals, be more of a SimTK problem but since it is used in OpenSim and my application would be for use with/in OpenSim I decided to post it here.
Basically:
Where I can find the implementation files for the Static Optimization(Not the two header files) such that I can create my own Optimization (Tool) where I have additional/different optimization variables and constraints. Hints on how to access the system properties necessary for Optimization are also welcome.
Best regards,
Erik
Implementation of Static Optimization
- Daniel Krueger
- Posts: 24
- Joined: Fri Jul 09, 2010 12:05 am
Re: Implementation of Static Optimization
Hey Erik,
perhaps the following c++ code may help you:
Best regards,
Daniel
perhaps the following c++ code may help you:
Code: Select all
State s=this->model.initSystem();
Storage statesStore;
statesStore.setDescription("initial pose sequence");
Array<std::string> stateNames;
stateNames=this->model.getStateVariableNames();
stateNames.insert(0, "time");
statesStore.setColumnLabels(stateNames);
Array<double> stateVals;
this->model.getStateValues(s,stateVals);
//Fill state storage with a sequence of copies
for(int i=0;i<this->hz;i++){
double time=t0+i*this->dt;
statesStore.append(time,stateVals.size(),stateVals.get());
}
statesStore.setInDegrees(false);
//Static optimization
StaticOptimization so(&this->model);
so.setStatesStore(statesStore);
double ti, tf;
statesStore.getTime(0,ti);
statesStore.getTime(this->hz,tf);
so.setStartTime(ti);
so.setEndTime(tf);
//run the analysis
for(int i=0;i<this->hz;i++){
//copy states
statesStore.getData(i,s.getNY(),&s.updY()[0]);
double t=0.0;
statesStore.getTime(i,t);
s.setTime(t);
this->model.assemble(s);
this->model.getMultibodySystem().realize(s, SimTK::Stage::Velocity);
if(i==0){
so.begin(s);
}else if(i==this->hz){
so.end(s);
}else{
so.step(s,i);
}
}
//write the results into control vector
for(int i=0;i<this->hz;i++){
this->u[i]=Vector(this->szU);
so.getActivationStorage()->getData(i,this->u[i].size(),&this->u[i][0]);
}
Daniel
- Erik Dijkstra
- Posts: 21
- Joined: Wed Aug 18, 2010 5:24 am
Re: Implementation of Static Optimization
Daniel thank you for sharing your code. I think it will help me later on to get data from/feed data to the optimizer at intermediate levels and not just at the end.
In the mean time I think I found the code which can help me to build a modified optimization. I downloaded the source code for OpenSim, here more details are shown in the cpp files; StaticOptimizationTarget.cpp and StaticOptimization.cpp (compared to the header files accessible in standard OpenSim installation).
Best regards,
Erik
In the mean time I think I found the code which can help me to build a modified optimization. I downloaded the source code for OpenSim, here more details are shown in the cpp files; StaticOptimizationTarget.cpp and StaticOptimization.cpp (compared to the header files accessible in standard OpenSim installation).
Best regards,
Erik