help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] [Fwd: Problem with GLPK when trying to find value functi


From: Robbie Morrison
Subject: Re: [Help-glpk] [Fwd: Problem with GLPK when trying to find value functions]
Date: Fri, 27 Jul 2012 10:36:00 +1200
User-agent: SquirrelMail/1.4.22

Hello again Sergey

------------------------------------------------------------
To:           Robbie Morrison <address@hidden>
Subject:      Re: [Help-glpk] [Fwd: Problem with GLPK when trying to find
value functions]
Message-ID: 
<address@hidden>
From:         Sergey Kuznetsov <address@hidden>
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
<address@hidden>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) : address@hidden
Webmail (preferred)           : address@hidden
[from Webmail client]





reply via email to

[Prev in Thread] Current Thread [Next in Thread]