[Top][All Lists]

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

[Help-glpk] None

From: address@hidden
Subject: [Help-glpk] None
Date: Wed, 30 Apr 2003 15:47:28 +0200

I am new to glpk and just find out how good it is.
I think that glpk is a great tool and as far as I know it's getting more
efficient and robust.
The only thing that I don't like about c-api is the way to build the model, 
mainly because it's not object oriented.
I had the same opinion about cplex before they came out with Concert Technology.
When I first saw ILOG concert technology I soon realized that it was a big
step ahead toword an easy way of developing optimization programs.
I thought that building an object oriented framework around glpk was a good
idea to let c++ programmers use this tool in an easy way.
For this reason I started the "glpkxx project" a GPL software that allow to
easily build linear optimization models in object oriented fashion and provide 
all the
feature of glpk.

The idea is to have an object GlpkProblem that interact with the solver.
The user add and removes variables and constraints as object, without 
the problem of storing indeces and so on; then when the
optimization is called, the problem is set up in glpk format and solved.
Then the user calls the query routine using its object, regardless of the
real index in the tableau.
Furthermore the use of expressions allows to write constraint in a more
"natural" way and both by row and by column, and to evaluate them after the
optimization. I also include function like Sum(varArray) or 
ScalarProduc(valueArray, varArray).

Memory is considered through the use of handles, to avoid excessive usage,
and freed by the solver itself when it is deleted.
Arrays of data and variables use STL array so that the user is not concerned
with the problem of allocating and deallocating memory.
So far I'm just in a beta phase and I only implemented some feature but
enough to
build and run the example below.

I would like to get your opinion about this project and any other hints.

Best regards
Ivan Luzzi

Below there is a comparison between the glpk code and its glpkxx version:

/* sample.c */

#include <stdio.h>
#include <stdlib.h>
#include "glpk.h"

int main(void)
{ LPX *lp;
int rn[1+9], cn[1+9];
double a[1+9], Z, x1, x2, x3;

s1: lp = lpx_create_prob();
s2: lpx_set_prob_name(lp, "sample");

s3: lpx_add_rows(lp, 3);

s4: lpx_set_row_name(lp, 1, "p");
s5: lpx_set_row_bnds(lp, 1, LPX_UP, 0.0, 100.0);
s6: lpx_set_row_name(lp, 2, "q");
s7: lpx_set_row_bnds(lp, 2, LPX_UP, 0.0, 600.0);
s8: lpx_set_row_name(lp, 3, "r");
s9: lpx_set_row_bnds(lp, 3, LPX_UP, 0.0, 300.0);

s10: lpx_add_cols(lp, 3);

s11: lpx_set_col_name(lp, 1, "x1");
s12: lpx_set_col_bnds(lp, 1, LPX_LO, 0.0, 0.0);
s13: lpx_set_col_name(lp, 2, "x2");
s14: lpx_set_col_bnds(lp, 2, LPX_LO, 0.0, 0.0);
s15: lpx_set_col_name(lp, 3, "x3");
s16: lpx_set_col_bnds(lp, 3, LPX_LO, 0.0, 0.0);

s17: rn[1] = 1, cn[1] = 1, a[1] = 1.0;
s18: rn[2] = 1, cn[2] = 2, a[2] = 1.0;
s19: rn[3] = 1, cn[3] = 3, a[3] = 1.0;
s20: rn[4] = 2, cn[4] = 1, a[4] = 10.0;
s21: rn[5] = 3, cn[5] = 1, a[5] = 2.0;
s22: rn[6] = 2, cn[6] = 2, a[6] = 4.0;
s23: rn[7] = 3, cn[7] = 2, a[7] = 2.0;
s24: rn[8] = 2, cn[8] = 3, a[8] = 5.0;
s25: rn[9] = 3, cn[9] = 3, a[9] = 6.0;
s26: lpx_load_mat3(lp, 9, rn, cn, a);

s27: lpx_set_obj_dir(lp, LPX_MAX);

s28: lpx_set_col_coef(lp, 1, 10.0);
s29: lpx_set_col_coef(lp, 2, 6.0);
s30: lpx_set_col_coef(lp, 3, 4.0);

s31: lpx_simplex(lp);

s32: Z = lpx_get_obj_val(lp);
s33: lpx_get_col_info(lp, 1, NULL, &x1, NULL);
s34: lpx_get_col_info(lp, 2, NULL, &x2, NULL);
s35: lpx_get_col_info(lp, 3, NULL, &x3, NULL);

s36: printf("\nZ = %g; x1 = %g; x2 = %g; x3 = %g\n", Z, x1, x2, x3);

s37: lpx_delete_prob(lp);

return 0;

/* eof */

/* prova.cpp */

#include <stdio.h>
#include <stdlib.h>
#include "glpkxx.h"

int main(void)
GlpkProblem lp("prova");

// create and add variables
GlpkVar x1, x2, x3;
x1 = lp.addVar(LPX_LO, 0.0, 0.0, "x1");
x2 = lp.addVar(LPX_LO, 0.0, 0.0, "x2");
x3 = lp.addVar(LPX_LO, 0.0, 0.0, "x3");

GlpkExpr expr1 = x1 + x2 + x3;
GlpkExpr expr2;
expr2 += 10*x1 + 4*x2 + 5*x3;

// create and add constraints
GlpkCon c1, c2, c3;
c1 = lp.addCon(expr1 <= 100);
c2 = lp.addCon(expr2 <= 600);
c3 = lp.addCon(2*x1+ 2*x2+6*x3 <= 600);

// create objective function
GlpkExpr objExpr = lp.addObj(10*x1 + 6*x2 + 4*x3);


double Z, v1, v2, v3;
Z = lp.getObjVal();
v1 = lp.getValue(x1);
v2 = lp.getValue(x2);
v3 = lp.getValue(x3);

printf("\nZ = %g; x1 = %g; x2 = %g; x3 = %g\n", Z, v1, v2, v3);


return 0;

/* eof */

reply via email to

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