[Ipopt] Ipopt Digest, Vol 43, Issue 1

Edson Valle edsoncv at enq.ufrgs.br
Tue Jul 1 12:23:34 EDT 2008


  Hello Krish
I had similar problem which I solved the following way:

First create a class with its members that does not extends the TNLP, 
e.g myclass.
Create a member "SmartPtr<TNLP> p_optdata" that will point to the 
optimization problem.
Create a class that extends TNLP, e.g. mytnlp, and has a member pointer 
to myclass, eg pmyclass.
So now you can access members: mytnlp->pmyclass->field_1.
Attention you MUST dereference both pointers before and the problem to 
avoid memory leaks since p_optdata has a reference to a Smartpointer, 
this can be done this way:
In the mytnlp destructor:

this->myclass = NULL;

and before call this destructor you must call:

if(myclass != NULL){
myclass->p_optdata = NULL; // this will call the mytnlp destructor
/* here the myclass destructor will be called, don't forget to delete
variables allocated with "new" inside myclass here*/
delete myclass;
}

I think that it, don't forget to check memory leaks with valgrind, this 
code structure is similar to Jipopt.cpp which deals with two separated 
classes. Let me know if I'm doing something wrong.
Cheers



>
> ------------------------------
>
> Message: 4
> Date: Tue, 1 Jul 2008 07:02:55 -0700 (PDT)
> From: Krish Krishnan <rkrishnan8216 at yahoo.com>
> Subject: [Ipopt] Using smart pointers
> To: ipopt at list.coin-or.org
> Message-ID: <271527.63184.qm at web65511.mail.ac4.yahoo.com>
> Content-Type: text/plain; charset="us-ascii"
>
> Hi All:
>
> I have a windows application using MFC to create an optimized portfolio of common stock.  I used the cpp example as a template and derived my class from TNLP.  The difference between my implementation and that in the example is that I have many new members and a few more functions (to access data in databases ) etc.
>
> When I try to compile this, the compiler complains that the new data members are not part of the TNLP class.  For example some of the error messages are below:
>
> rojects\bmopt\bmopt\createcase.cpp(262) : error C2039: 'm_caseFileName' : is not a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
> 1>c:\documents and settings\kris\my documents\projects\bmopt\bmopt\createcase.cpp(264) : error C2039: 'm_assetBoundsDB' : is not a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
> 1>c:\documents and settings\kris\my documents\projects\bmopt\bmopt\createcase.cpp(265) : error C2039: 'm_assetBoundsTable' : is not a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
> 1>c:\documents and settings\kris\my documents\projects\bmopt\bmopt\createcase.cpp(267) : error C2039: 'm_assetReturnDB' : is not a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
>
>
>
> But this is not right, and these members are present in the derived class.  I declare the smart pointer thus:
>
>     SmartPtr<TNLP> p_optdata;
> and instantiate it as p_optdata = new COpt() where COpt is the derived class from TNLP.
>
> Is there some other way I should treat this derived class?  If any more information is needed, please let me know?  
>
> I even tried SmarPtr<COpt> p_optdata.  The compiler does not like this.  The complaints about the new data members are gone. but instead I get
>
> 1>c:\documents and settings\kris\my documents\projects\bmopt\bmopt\bmoptdoc.cpp(105) : error C2664: 'Ipopt::IpoptApplication::OptimizeTNLP' : cannot convert parameter 1 from 'Ipopt::SmartPtr<T>' to 'const Ipopt::SmartPtr<T> &'
> 1>        with
> 1>        [
> 1>            T=COpt
> 1>        ]
> 1>        and
> 1>        [
> 1>            T=Ipopt::TNLP
> 1>        ]
> 1>        Reason: cannot convert from 'Ipopt::SmartPtr<T>' to 'const Ipopt::SmartPtr<T>'
> 1>        with
> 1>        [
> 1>            T=COpt
> 1>        ]
> 1>        and
> 1>        [
> 1>            T=Ipopt::TNLP
> 1>        ]
> 1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
>
> I guess this is because oc the const declaration.
>
>
> Any way I can solve this issue?  This is the very last part of my implementation, and any help would be appreciated.
>
> Thanks in advance
>
> Krish
>
>
>       
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: http://list.coin-or.org/pipermail/ipopt/attachments/20080701/9d683e40/attachment-0001.html 
>
> ------------------------------
>
> _______________________________________________
> Ipopt mailing list
> Ipopt at list.coin-or.org
> http://list.coin-or.org/mailman/listinfo/ipopt
>
>
> End of Ipopt Digest, Vol 43, Issue 1
> ************************************
>   



More information about the Ipopt mailing list