help-glpk
[Top][All Lists]
Advanced

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

[Help-glpk] Re [Fwd: Error in Visual C for MIP]


From: Robbie Morrison
Subject: [Help-glpk] Re [Fwd: Error in Visual C for MIP]
Date: Thu, 23 Jun 2011 04:46:34 +1200 (NZST)
User-agent: SquirrelMail/1.4.17

------------------------------------------------------------
To:          address@hidden
Subject:     Error in Visual C for MIP
From:        sheetal murkute <address@hidden>
Date:        Wed, 22 Jun 2011 01:38:07 -0400
------------------------------------------------------------

Hello Sheetal

Without having seen all your code, it would appear that
you would need to create the six columns before trying
to load them with objective function coefficients.  See
below:

> printf("Enter objective function coefficients /n");
> for(i=1 ; i<=6; i++)
> scanf("%f", &ObjCoeff[i]);

glp_add_cols(mip, 6); /* NOTE: add some columns first */

> glp_set_obj_dir(mip, GLP_MIN);
> for(i=1 ; i<=6; i++)
> {
> glp_set_obj_coef(mip,i,ObjCoeff[i]);
> }

Or else you forgot to allocate storage for the
'ObjCoeff' array!

On that note, you can use C++ Standard Library
'std::vector's as C-style arrays.  This is because C++
vectors are guaranteed to be in contiguous (unbroken)
memory.  Personally I prefer this idiom.  Taking the
example in the front of the GLPK manual:

  std::vector<int>    ia;
  std::vector<int>    ja;
  std::vector<double> ar;
  ...
  ia.push_back(1); ja.push_back(1); ar.push_back(1.0);
  ...
  const int cnt = ar.size();
  glpk_load_matrix(lp, cnt, &ia[0], &ja[0], &ar[0]);

When the next C++ standard is released, you will be
able to do in one hit (and yes everybody, I know this
is currently possible with C-style arrays):

  ia = {   1,   1,   1,    2,   3,   2,   3,   2,   3 };
  ja = {   1,   2,   3,    1,   1,   2,   2,   3,   3 };
  ar = { 1.0, 1.0, 1.0, 10.0, 2.0, 4.0, 2.0, 5.0, 6.0 };

using initializer lists:

  http://en.wikipedia.org/wiki/C%2B%2BOx#Initializer_lists

It is probably better to state the type in a 'for' loop
as follows:

  for(int i = 1; i <= 6; i++)  // note the "int" part

And one other small thing.  You may find your code
easier to read and fix if you routinely put 'for' loops
in blocks:

  for(int i = 1; i <= 6; i++)
    {
      scanf("%f", &ObjCoeff[i]);
    }

Its a style thing I know, but it usually helps.

good luck
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]