Page 1 of 1

Constraints part:2

Posted: Thu Jun 25, 2015 1:03 pm
by ex10192
Hi,

I have sort of implemented the member function, constraintfunc() with contents similar to objectivefunc(). Now I really want help with setting this constraint equation,

Equation I need : CoordinateB(finaltime) = CoordinateB (value=360deg)

Objective: The simulation shouldn't end before the set final time of integration, and when coordinate reaches final time the value should be around 360 (deg )in radians or vice versa.

This is how I tried,

LHS: I couldnt find any code which I can relate the coordinate & final time ?
RHS: I plan to set the value to 6 radians but dont know if its the right approach.

Code: Select all

Coordinate& codB = _model.getCoordinateSet().get("coordinateB");
int re = 6; //  360 deg approx radian
int tf = osimState.getTime();
if (tf=finalTime){
constraints[0] = codB.getValue(s) -[codB.setValue( s,re, true)];
OR
constraints[0] = codB.getValue(s) -6;
}
else
{
// Problem if simulation finish before final time if condition doesn't happen
};
This is the first time I am setting up an equation..and I am completely wrong, kindly help.

regards,

Nithin

Re: Constraints part:2

Posted: Thu Jun 25, 2015 3:44 pm
by sherm
Nithin, it is important to untangle the optimization from the integration! You have some optimization parameters p, some objective function f(p), and some constraints g(p). The optimizer will solve:

Code: Select all

minimize f(p)
subject to g(p)=0
I suggest that you consider carefully:
- what are your optimization parameters p? For example, is final time a parameter?
- what is your function f? Perhaps f is the result of an integration that runs until a particular time is reached, or maybe it is one that runs until a coordinate achieves a particular value. In the latter case the final time would be a *result* of that computation.
- what is your function g? Typically it will be some constraints using the same computation that was performed to evaluate f.

Possibly you are asking a question about how to terminate a simulation when a certain condition is reached. But it would be much easier to discuss if you can clarify p, f, and g.

Sherm

Re: Constraints part:2

Posted: Thu Jun 25, 2015 11:43 pm
by jp123909
Your purpose is unclear.

If you wanted Coordinate B to keep constant all the time during simulation, why not add constraint to B in the model,
and then minimize the constraint force of it?

If you just wanted Coordinate B to be a constant value at the final time of simulation, constraints[0] = codB.getValue(s) -6
were correct i think. In the case that simulation was stopped by a event trigger before final time you have set, the solution should be abandoned (?,i guess)

Re: Constraints part:2

Posted: Thu Jun 25, 2015 11:56 pm
by jp123909
Also some small problems of your code


int re = 6; should be double re=6*SimTK::PI/180
int tf = osimState.getTime(); should be double tf=osimState.getTime();
if (tf=finalTime) should be if(tf==finalTime) or if(fabs(tf-finalTime)<1.0e-6)