Hello again Sergey ------------------------------------------------------------ To: Robbie Morrison <[email protected]> Subject: Re: [Help-glpk] [Fwd: Problem with GLPK when trying to find value functions] Message-ID: <CAPv=T6App_8Xf_cgqN5qsjcMhYaycig-M=plzae7m4qjwou...@mail.gmail.com> From: Sergey Kuznetsov <[email protected]> Date: Fri, 27 Jul 2012 00:11:30 +0300 ------------------------------------------------------------
> As Andrew recommended, I allocated more memory for > constraint matrix (one element more, since it counts > from 1st, not 0th element) and it works now for all > instances. Before I was confused because it didn't work > for a few cases. I guess it worked in the old code > thanks to C++ new operator. Now problem solved! > Thanks for you help! Xypron's suggestion actually. I strongly recommend Boost library 'boost::shared_ptr' if you are using 'new'. On recent compilers, it is also 'std::tr1::shared_ptr' or 'std::shared_ptr'. > Sergey > > On Thu, Jul 26, 2012 at 10:38 PM, Robbie Morrison <[email protected]>wrote: > >> >> Hello again Sergey [snip] If you're using C++ too then you might like this idiom. It uses 'std::vector' from the standard library. It will fail just the same as C-style arrays if you try to read or write out-of-range, but it will throw an C++ exception with a much more understandable error message! You could even catch the exception if you wish and then exit a little more gracefully. The reason that std::vectors work is that vector elements are required to be held in contiguous memory. Josuttis (1999 p155) writes: "this example shows that whenever you need an array of type T for any reason (such as for an existing C library) you can use a std::vector<T> and pass it the address of the first element" In addition, you could fill the vectors using 'push_back' calls. They would then grow dynamically without problem. Take, for example, the standard GLPK tutorial problem: http://en.wikibooks.org/wiki/GLPK/Using_the_GLPK_callable_library#Short_example And then make the following modifications: #include <vector> // STL sequence container // declare variables glp_prob *lp; std::vector<int> ia(1001); // was: int ia[1+1000] std::vector<int> ja(1001); // was: int ja[1+1000] std::vector<double> ar(1001); // was: double ar[1+1000] double Z, x1, x2, x3; int solver_ret; // exit status of solver // create problem lp = glp_create_prob(); glp_set_prob_name(lp, "sample"); glp_set_obj_dir(lp, GLP_MAX); // fill problem glp_add_rows(lp, 3); ... The C++ exception handling part would be something like: glp_prob *lp; try { // most of the above code in here // plus the remainder not shown } catch ( const std::out_of_range& e ) { std::cout << "e.what()" << std::endl; glp_delete_prob(lp); glp_free_env(); // any other mopping up } There are more exotic wrappers you could write. Let me know and I can send you some sample code. REFERENCES Josuttis, Nicolai M. 1999. The C++ Standard Library : a tutorial and reference. Addison-Wesley, Boston, USA. ISBN 0-201-37926-0. HTH, Robbie --- Robbie Morrison PhD student -- policy-oriented energy system simulation Technical University of Berlin (TU-Berlin), Germany University email (redirected) : [email protected] Webmail (preferred) : [email protected] [from Webmail client] _______________________________________________ Help-glpk mailing list [email protected] https://lists.gnu.org/mailman/listinfo/help-glpk
