[Ipopt] Passing additional data to TNLP methods

Michael Braun braunm at mit.edu
Thu May 12 19:48:10 EDT 2011


Stefan:

Thanks for your response.  I finally found some time to get back to this project, and some more related questions have arisen:

1.  When I compile my code, at the line for 

 status = app->OptimizeTNLP(mynlp);

I get the following compiler error:  no suitable user-defined conversion from "Ipopt::SmartPtr<MyTNLP>" to "const Ipopt::SmartPtr<Ipopt::TNLP>" exists.

I found another Mailing List thread in which you recommend passing the raw pointer, as status = app->OptimizeTNLP(GetRawPtr(mynlp)), and that seems to work in some special cases of my application.  However, in other cases (for which I cannot find a common, repeatable pattern), I will get various runtime errors that suggest that Ipopt is trying to free an object that has already been freed.   An example from gdb is:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000004750a825d0
0x0000000103b21ed3 in Ipopt::IpoptCalculatedQuantities::trial_compl_x_U () at IpTNLP.hpp:-1
Line number -1 out of range; IpTNLP.hpp has 301 lines.
(gdb) bt
#0  0x0000000103b21ed3 in Ipopt::IpoptCalculatedQuantities::trial_compl_x_U () at IpTNLP.hpp:-1
#1  0x0000000103af899c in Ipopt::IpoptAlgorithm::AcceptTrialPoint () at IpTNLP.hpp:-1
Previous frame inner to this frame (gdb could not unwind past this frame)

But because of the "Line -1 out of range" message, I am not even sure which part of the Ipopt code is freeing the previously freed object, whatever it might be.

2.  In my  MyTNLP class, I have added several different members.  Some of these members are primitives (e.g., Index n for number of variables),  some are pointers to arrays (e.g., Number * start_x to point to starting values), and some are pointers to objects (for which I would prefer a void pointer, so I can pass generic object types, but this is not strictly necessary).  I am not using SmartPtr for any of these, and I wonder if this is the problem.  From the documentation, I am having trouble figuring out when smart pointers are necessary, and when they aren't.  And in particular, I'm not sure how to construct smart pointers for non-Ipopt classes (and I cannot change the classes directly).  In IpSmartPtr.hpp, there is a reference to wrappers, but I'm not entirely clear what this wrapper should look like, and again, if it is even necessary.  Of course, this problem could be completely unrelated to smart pointers.  I just don't know.


So I am trying to decide if the best approach is to construct a SmartPtr for MyTNLP, and not pass the GetRawPtr, or to define the members of MyTNLP as smart pointers themselves.  Or perhaps none of the above.  Do you have any further suggestions or (preferably) examples that might help?

Thanks,

Michael



What seems odd to me is the Line number -1 in the IpTNLP.hpp file.


On Apr 25, 2011, at 10:49 AM, Stefan Vigerske wrote:

> Hi,
> 
>> Ipopt looks like a terrific package, and I am interested in trying it out using the C and C++ interfaces for some statistical estimation problems.  But in going through the documentation and mailing lists, I do not see how one could pass additional objects (like an array of data) to the functions that compute the objective, gradient and Hessian.  Is there a way to do that, or do model parameters need to be hard-coded in?  I've considered using global variables, but that just doesn't seem like it should be the best solution.
>> 
>> Could someone kindly point me to an example of what I need to do?  I should mention up front that I am  new to C++, having done most of my coding in C in the past, so many object-oriented concepts are new to me.  But an example of how to do this would be very helpful.
> 
> Well, it was discussed on the mailing list from time to time, see, e.g., 
> the "Runtime modification of the model" thread at 
> http://list.coin-or.org/pipermail/ipopt/2011-February/thread.html
> 
> What you have to do is to derive your own class from the TNLP class such 
> that it has some additional members to store your data. You have to pass 
> in your data (e.g., in the constructor) to a TNLP instantionation before 
> you pass it to the IpoptApplications' OptimizeTNLP method.
> 
> So something like
> class MyTNLP : public Ipopt::TNLP {
>   double a;
> public:
>   MyTNLP(double& a_)
>   : a(a_)
>   { }
> 
>   /* implementation of usual TNLP functions...
>      they can access the member variable a */
> }
> 
> and use this class via
> 
> SmartPtr<TNLP> mytnlp = new MyTNLP(42.0);
> ipopt->OptimizeTNLP(mytnlp);
> 
> Stefan
> 
>> 
>> Thanks,
>> 
>> Michael Braun
>> braunm _at_ mit.edu
>> 
>> 
>> 
>> 
>> _______________________________________________
>> Ipopt mailing list
>> Ipopt at list.coin-or.org
>> http://list.coin-or.org/mailman/listinfo/ipopt
>> 
> 

-------------------------------------------
Michael Braun
Homer A. Burnell (1928) Career Development Professor, 
	and Assistant Professor of Management Science (Marketing Group)
MIT Sloan School of Management
100 Main St.., E62-535
Cambridge, MA 02139
braunm at mit.edu
617-253-3436





More information about the Ipopt mailing list