Creating an objective function

I’m creating a new objective function for parameter study and optimization.
I want to use the differentiated joint torque to create the objective function.

  1. Can I access to the differentiated value of joint torque?

  2. If not, I’m going to make my own value instead of the differentiated value.

2-1) I’m creating an AnyOutputFun in AnyBodyStudy for the joint torque differentiation.
Then I have to access the joint torque value of the very previous frame. Is it possible?

2-2) If still not, I’m going to create an AnyDesMeasure in AnyParamStudy instead.

AnyDesMeasure TorqueChangeRate = {
  Val = 0;

  AnyFolder &JntM = ..Study.JntM;

  AnyVar step = ..Study.nStep;
  AnyVar dt = (..Study.tEnd - ..Study.tStart)/(step-1);

  AnyVar dim = DimOf(JntM());
  AnyStringVar flag = strval(gtfun( dim, 1 ));

  #define FLAG 1
  #if FLAG == flag

  AnyVector dTorque = {
    (JntM()[1] - JntM()[0]) / dt,
    (JntM()[2] - JntM()[1]) / dt,
    (JntM()[step+1] - JntM()[step]) / dt
  Val = sum (dTorque);

I know that #if cannot come with the variable flag, so I cannot make it with the script above.
And without #if ~ #endif, it also does not work because the index greater than 0 is not valid not until the torque values of n steps are retrieved.

Is there anyway to use the differentiated joint torque value for the objective function?

Thank you in advance.

[non-meaningful post] an update to send this thread to the tracking system.


Here are some answers;

A1 : this is not possible
A2.1: this is not possible to do while the analysis is running, it can be done when all the timesteps has been finished.
A2.2: It is essentially not possible to differentiate while the analysis is running.

I see this way to do it in:[ol]
[li]Save the values you would like to differentiate into a txt file[/li][li]Make a new study just to do the differentiation.[/li][li]Create an interpolation function using the saved values of step1:[/li][li]Make an differentiation function which makes use of the interpolation function[/li][li]Ensure that the study in step 2 never exceeds the tstart and tend values you have saved in step 1. In other words you need to ensure that if you do a forward differentian your tend accounts for the delta_t and if you do a backwards diff tstart needs to be bigger.[/li][/ol]Would this approach solve your problem?

Alternative it would also be possible to create function in Phython which could do this, please see this page

Best regards

Hi Soren,

Thank you for the answer.
I hope some different answers(from 1 to 2.2), but I’m sorry that they were as what I expected.

The method you suggested 1~5 looks like differentiation is executed separately after the first analysis is done. Is that mean I have to save 100 times if I do a parameter study with 2 parameters of 10 steps?
If then, I think the same operation is possible on matlab or something.

You mentioned me about Python, but I don’t actually understand how far this function allows for the calculation and analysis.
Is it possible to make a differentiation function in Python, to recall it in AnyBody, and to appoint the diff value as the objective function of parameter study which is already running?

Thank you very much.


Hi Dong-Pyoung,

We have made a small example which can do differentiation using python, i think it should solve your problem.

The model has been added to the wiki please find it here:

Best regards

[SIZE=3] [SIZE=2]

I didn’t notice there was such a nice tip on the page!!
Many thanks. :slight_smile:


I was working on the python method in AnyBody and I have a question about it.

The example in the wiki shows that a text file is created to save a value of the current frame in AnyBody; when a text file does not exist in the folder, the python code generates a text file with the value of the first frame.

I found that the initial value of the result in InverseDynamics operation changed if I did not remove the text file created by python code. And I induce that this method does not initialize the saved value without deleting the file manually.

I think this means it does not look to fit for parameter study.
When the next step of param study begins, the last value of the previous step still exist without a new initialization of the next step because I cannot delete the text file every time when a new inverse dynamics of the next paramstudy step begins.
Is my guess right?

I tried to initialize the text file by calling ‘t’ or ‘iStep’ variable of AnyBodyStudy , or ‘StateCountCurrent’ of AnyParamStudy in order to catch if a new paramstudy step begins.
But those variables are not accessible because they are Denied-Access Members…
Is there any suggestion to initialize the value whenever a new paramstudy step starts?



Hi Dong-Pyong,

There are several ways to do that. One way is to check from Python which time step it is and delete the file prior to writing something new. Another option would be make an AnyOperationShellExec and add it to the InitialConditions.PreOperations, which will delete the previous file. A third option (somewhat similar to what you tried) would be to pass some sort of name specifier (related to the design variables of your parametric study) to the input of this function and it would simply write a case-specific file.