# Minimizing the distance between two nodes in space using the AnyOptKin study

Hello everyone,

i am trying to figure out how to systematically minimize the distance between two nodes in space using the AnyOptKin study. Therefore i created the small example attached to this post.

The first node is called Marker0, located in the origin of the global reference frame.
The second node is called Marker1, initially located at x = 0.1, y = 0.1 and z = 0.

I am trying to use the AnyOptKin study to change the x and y position of Marker1 in the range of min = -0.05, max = 0.05 to minimize its distance to Marker0.

The AnyDesVariables are PosXPointM1 and PosYPointM1.
The AnyDesMeasure is DistanceM0toM1

Right now, i am not quite sure if this is the right way to procede.

Test.any code:

Main =
{
AnyFolder Model =
{
AnyFixedRefFrame GlobalRef =
{
AnyDrawRefFrame drw = {RGB={1,0,0}; ScaleXYZ = 0.12*{1,1,1}; Visible = On;}; // drw

AnyRefNode Marker0 =
{
sRel = {0, 0, 0};
AnyDrawNode drw = {RGB = {1, 1, 0}; ScaleXYZ = 0.5*{0.025, 0.025, 0.025};}; // drw
}; // Marker0

AnyRefNode Marker1 =
{
sRel = {0.1, 0.1, 0};
AnyDrawNode drw = {RGB = {1, 1, 0}; ScaleXYZ = 0.5*{0.025, 0.025, 0.025};}; // drw
}; // Marker1

}; // GlobalRef

AnyFolder Measures =
{
// Measuring distance between nodes Marker0 and Marker1
AnyKinPLine DistanceM0toM1 =
{
AnyRefFrame &m0 = Main.Model.GlobalRef.Marker0;
AnyRefFrame &m1 = Main.Model.GlobalRef.Marker1;
}; // DistanceM0toM1
}; // Measures
}; // Model

AnyBodyStudy Study =
{
AnyFolder &Model = .Model;
Gravity = {0, -9.81, 0};
tEnd=2;
nStep=10;
}; // Study

AnyOptKinStudy OptKin =
{
Analysis =
{
AnyOperation &Kin = Main.Study.Kinematics;
};

AnyDesVar PosXNodeM1 =
{
Val = Main.Model.GlobalRef.Marker1.sRel[0];
Min = -0.05;
Max = 0.05;
}; // PosXNodeM1

AnyDesVar PosYNodeM1 =
{
Val = Main.Model.GlobalRef.Marker1.sRel[1];
Min = -0.05;
Max = 0.05;
}; // PosYNodeM1

AnyDesMeasure DistanceM0toM1 =
{
Val = Main.Model.Measures.DistanceM0toM1.Pos[0];
}; // DistanceM0toM1
}; // OptKin
}; // Main

Hi Omarel,

I think the problem here is that you try to use the AnyKinOptStudy for something it is not intended for.

https://anyscript.org/tutorials/Making_things_move/lesson4.html

This study can be used to minimize errors on soft kinematic constraints, by e.g. moving a joint location to provide a better fit on markers. So the objective function is implicit built into this study, and it is to minimize the sum of all errors on soft kinematic constraints. The problem is that you model has one segment and no soft kin constraints.

What you could have done instead is to use the more general AnyOptStudy see https://anyscript.org/tutorials/Parameter_studies_and_optimization/lesson2.html

This is a general optimization, and you can decide the objective function it is not implicit defined.

So in short:

• AnyKinOptStudy: used for optimizing paramters which will minimize errors on soft kinematic constraints
*AnyOptStudy: used for general purpose optimization the objetive function can be any output from the model either kinematics or kinetics.

Hope it helps

Søren

Hi Omar!

First of all, good to hear from you again!

I assume that what you are still working on is the scapula mechanism scaling as we talked about?

For that problem, the AnyOptKinStudy is the correct one to use, but the setup you have made here is not. What you specified here requires an AnyOptStudy as Søren mentions. What I think you need is to make the AnyKinPLine between marker 0 and 1 into an AnyKinLinear (to not only look at the distance between points but all three vector components), set that measure to Soft for all three directions and minimize that least-square error over all frames with the AnyOptKinStudy. You also have to use that linear kinematic measure in a driver in your model that you minimise the errors on.

Notice also that that study does not take inequalities (your -0.05 to 0.05) into account and was never build for that either.

I hope that helps.

Best regards
Michael

This topic was automatically closed 125 days after the last reply. New replies are no longer allowed.