Code Monkey home page Code Monkey logo

Comments (8)

Levi-Armstrong avatar Levi-Armstrong commented on July 3, 2024

When you create a problem, you assign variable sets like what is shown below with unique names. Then when you add a constraint or cost to the problem it gets linked to the problems variable set. So inside the cost or constraint object you have access to the member function GetVariables() which gives you access to all variable sets. Then during the solve it will call the GetValues() and FillJacobianBlock. I also included an example of a constraint implementation which may help.

  Problem nlp;
  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set0"));
  nlp.AddVariableSet(std::make_shared<ExVariables>("var_set1"));

Example:
https://github.com/tesseract-robotics/trajopt/blob/master/trajopt_ifopt/include/trajopt_ifopt/constraints/joint_velocity_constraint.h
https://github.com/tesseract-robotics/trajopt/blob/master/trajopt_ifopt/src/constraints/joint_velocity_constraint.cpp

from ifopt.

naguil avatar naguil commented on July 3, 2024

Thank you @Levi-Armstrong. But, as I understand, you are getting the values of the external representation (as a VectorXd). I was hoping I could get the internal representation mentioned in the following code snippet. I'd like to define some x0_ and x1_ as Fourier series, for example, and make use of them inside the Constraint and Cost classes.

// Here is where you can transform the Eigen::Vector into whatever
// internal representation of your variables you have (here two doubles, but
// can also be complex classes such as splines, etc..
void SetVariables(const VectorXd& x) override
{
x0_ = x(0);
x1_ = x(1);
};

from ifopt.

Levi-Armstrong avatar Levi-Armstrong commented on July 3, 2024

You need to create your own variable set by inheriting from VariableSet like what is shown in this example.

Are you wanting to know how to access the x0_ and x1_ from your cost or constraint function?

from ifopt.

naguil avatar naguil commented on July 3, 2024

Yes, I already have a variable set, and I wanted to access x0_ and x1_ (two Fourier series in my case) from the constraint set. If it isn't possible, then I fail to see the overall usefulness of x0_ and x1_

from ifopt.

Levi-Armstrong avatar Levi-Armstrong commented on July 3, 2024

Add two methods to your variable set class getX0() and getX1() then in your cost or constraint.

auto my_set = std::dynamic_pointer_cast<MyVariableType>(GetVariables()->GetComponent("var_set1"));
... now call your methods exposing x0_ and x1_

from ifopt.

naguil avatar naguil commented on July 3, 2024

Thank you, I'll try it out.

from ifopt.

naguil avatar naguil commented on July 3, 2024

Add two methods to your variable set class getX0() and getX1() then in your cost or constraint.

auto my_set = std::dynamic_pointer_cast<MyVariableType>(GetVariables()->GetComponent("var_set1"));
... now call your methods exposing x0_ and x1_

Thank you very much. It worked like magic. I hadn't heard about dynamic_pointer_cast. I was afraid the solution was something much simpler that I wasn't seeing.

from ifopt.

Levi-Armstrong avatar Levi-Armstrong commented on July 3, 2024

Yea, I think this is the only option you have if you want implementation specific access.

from ifopt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.