[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-glpk] [Fwd: Re: MathProg, glpk and C language]
From: |
Andrew Makhorin |
Subject: |
[Help-glpk] [Fwd: Re: MathProg, glpk and C language] |
Date: |
Sun, 08 Oct 2017 09:18:20 +0300 |
-------- Forwarded Message --------
From: Lyndon D'Arcy <address@hidden>
To: john tass <address@hidden>
Cc: Andrew Makhorin <address@hidden>, address@hidden
Subject: Re: [Help-glpk] MathProg, glpk and C language
Date: Sun, 8 Oct 2017 16:11:48 +1100
Hi all,
See below for:
* overview of my approach
* specific files that I've changed
* slightly updated example code
I've attached the updated source files. John, please let me know if you
have any issues recompiling with the updated files.
As Michael has noted, the example I've given doesn't do any processing
of the solution. I am considering writing an additional function to
output the solution produced by glp_print_sol into a char* instead of a
file. However it may be easier for your purposes John to simply process
the glp_prob object (lp in my example below) directly. You should be
able to use for example glp_get_obj_val or similar functions described
in Section 2 of the glpk manual for this purpose.
Also, John, regarding your earlier comment - yes you can use all of the
functionality of MathProg. All I've really changed is to redirect the
input to read from a location in memory instead of a file.
Andrew, I would be happy if these changes could be incorporated into an
upcoming GLPK release. Please let me know if there is anything about my
approach that you would like me to change for that purpose.
Thanks,
Lyndon
Overall approach:
I've created new versions of existing glpk functions, modified slightly
to be able to expect a char* argument containing a string with the
entire GMPL program.
The call sequence is below:
1. glp_mpl_read_buffer_into_model (new)
2. mpl_read_buffer_into_model (new)
3. open_buffer_input (new)
4. glp_buffer_open (new)
4. get_char(unchanged)
5. read_char(unchanged)
6. glp_getc (unchanged)
7. glp_read (modified)
Specific file changes:
src/glpk.h:
#include <stdint.h>
new function declaration - int glp_mpl_read_buffer_into_model(glp_tran
*tran, char *cbuf, int64_t clen, int skip);
src/api/mpl.c:
glp_mpl_read_buffer_into_model
- wraps mpl_read_buffer_into_model
src/env.h:
new function declaration - glp_file *glp_buffer_open(char *cbuf, int64_t
clen);
src/env/stream.c:
#include <stdint.h> and <assert.h>
struct glp_file (existing)
- added flag IOCBF to identify if the underlying file is a character
buffer
- added variables to track the buffer that is being copied from, and
position in that buffer
glp_buffer_open (new)
- new function that allocates a glp_file struct from char* and length
arguments
glp_read (existing)
- added a new code execution path if the glp_file being read from has
the IOCBF flag set
glp_close (existing)
- close appropriately if IOCBF is set
src/mpl/mpl.h:
new function declaration - void open_buffer_input(MPL *mpl, char *cbuf,
int64_t clen);
new function declaration - int mpl_read_buffer_into_model(MPL *mpl, char
*cbuf, int64_t clen, int skip);
src/mpl/mpl4.c:
open_buffer_input
- new function to initialise mpl for reading from a char*
mpl_read_model_from_buffer
- read an mpl model from a char*
Example:
int status;
int ret;
/* some template code - note the %s to be replaced by sprintf later */
char *template = "var x1;\
var x2;\
maximize obj: 0.6 * x1 + 0.5 * x2;\
s.t. c1: x1 + 2 * x2 <= 1;\
s.t. c2: 3 * x1 + x2 <= %s;\
end;";
/* code to insert in place of %s */
char *insert = "5";
/* allocate enough space for the new string */
char *buf = malloc(strlen(template)+strlen(insert)+1);
sprintf(buf, template, insert);
glp_prob *lp;
glp_tran *tran = glp_mpl_alloc_wksp();
status = glp_mpl_read_buffer_into_model(tran, buf, strlen(buf), 0);
/* after buffer has been read into model, can free it */
free(buf);
ck_assert_int_eq(status, 0);
if (!status) {
status = glp_mpl_generate(tran, NULL);
if (!status) {
lp = glp_create_prob();
glp_mpl_build_prob(tran, lp);
glp_simplex(lp, NULL);
ret = glp_mpl_postsolve(tran, lp, GLP_MIP);
if (ret != 0) {
fprintf(stderr, "Error on postsolving model\n");
} else {
/* code to handle solution goes here */
}
}
}
glp_mpl_free_wksp(tran);
glp_delete_prob(lp);
glp_free_env();
On 5 October 2017 at 07:54, Lyndon D'Arcy <address@hidden>
wrote:
Hi John,
Are you able to compile GLPK from source? I'll aim to provide
an updated source code and some more details/examples over the
weekend.
On Thu, 5 Oct 2017 at 2:17 am, john tass <address@hidden>
wrote:
Hi Lyndon, again.
Please do not forget to include in the example that you
possibly will send to me, the way of how to refer to
your extension and how I can have access to the
solution.
Thanks.
2017-10-04 18:11 GMT+03:00 john tass
<address@hidden>:
Hi Lyndon,
Yes, it is exactly what I was looking for. I
assume that by the way you suggest I shall be
able to write pure MathProg code. I make this
comment because I do not want simply to use just
2 or three variables (as the example you sent,
x1 and x2) but hundreds of them. So, I need the
flexibility that MathProg provides. The same
holds for the constraints that refer to those
variables, which are many, too.
Is it possible to send me an example which
contains say 50 variables (x1, x2, x3, ..., x50)
without having to hard-code them? In other
words, the example should declare the variables
by the use of indices. In addition, please make
an effort to mention how I get back to C
function the optimal objective value and the
values of the variables concerning the optimal
solution.
Thank you a lot.
2017-10-04 15:44 GMT+03:00 Lyndon D'Arcy
<address@hidden>:
John, the reason I ask is, I wrote a
small extension to allow reading a
MathProg model from memory instead of an
external file. I'm not sure if this is
what you are looking for exactly. But
it would allow you to generate the
MathProg code in-memory, and then read
the resulting program into GLPK. I
think I would need to add another
function to allow you to process the
solution in memory as well. Then you
would be able to do everything without
ever needing to leave the C environment,
or read/write text files, or maintain a
model file that had been generated
separately. Is that what you're looking
for?
Below is some example code for reading
MathProg using my extension:
int status;
char *buf = "var x1;\
var x2;\
maximize obj: 0.6 * x1 + 0.5 * x2;\
s.t. c1: x1 + 2 * x2 <= 1;\
s.t. c2: 3 * x1 + x2 <= 2;\
end;";
glp_prob *lp;
glp_tran *tran = glp_mpl_alloc_wksp();
status =
glp_mpl_read_buffer_into_model(tran,
buf, strlen(buf), 0);
ck_assert_int_eq(status, 0);
if (!status) {
status = glp_mpl_generate(tran,
NULL);
if (!status) {
lp = glp_create_prob();
glp_mpl_build_prob(tran, lp);
}
}
glp_mpl_free_wksp(tran);
glp_delete_prob(lp);
glp_free_env();
On 4 October 2017 at 22:49, Andrew
Makhorin <address@hidden> wrote:
> I am trying to write a
function in ANCI C programming
language. The
> aim of this function is to get
some input arguments from the
main C
> program, then solve a MIP
problem and return the value of
objective
> function along with the values
of structural variables back to
the C
> main program.
> The issue is that the MIP
problem I am about to solve via
glpk has a
> quite large number of
variables. So, I am not able to
hard-code them.
> Hence, I came across the idea
to use MathProg language, as it
is very
> easy to code my model.
> The question is, how to do
this? Is it possible to
incorporate
> MathProg code inside a C
language function? Please note
that I am
> aware of how to write a C
program that calls API routines
of glpk in
> order to solve a simple model,
but here I am not referring to
this
> case.
> Any suggestion of a site, pdf
or relevant document will be
very
> helpful.
Please see Section 3.2 "Routines
for processing MathProg models"
in the
glpk reference manual (file
glpk.pdf can be found in the
subdirectory
'doc' in every glpk distribution
tarball).
_______________________________________________
Help-glpk mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-glpk
--
Dr. Ioannis X. Tassopoulos, MSc., Ph.D.
--
Dr. Ioannis X. Tassopoulos, MSc., Ph.D.
- [Help-glpk] [Fwd: Re: MathProg, glpk and C language],
Andrew Makhorin <=