• No se han encontrado resultados

El proceso, sus tres dimensiones y la información que revelan

PARTE IV: Discusión

4.1. El proceso, sus tres dimensiones y la información que revelan

In the first instance we are going to consider a version of the ‘Burglar’ model that corresponds to the very first version we have seen in Section2.1where all arrays are indexed by the range set ITEMS = 1..8. In our C program ugiodense.c below, this corresponds to storing data in standard C arrays that are communicated to the Mosel model at the start of its execution.

#include <stdio.h> #include "xprm_mc.h"

double vdata[8]={15,100,90,60,40,15,10, 1}; /* Input data: VALUE */ double wdata[8]={ 2, 20,20,30,40,30,60,10}; /* Input data: WEIGHT */ double solution[8]; /* Array for solution values */

int main() {

XPRMmodel mod; int i,result;

char vdata_name[40]; /* File name of input data ’vdata’ */ char wdata_name[40]; /* File name of input data ’wdata’ */ char solution_name[40]; /* File name of solution values */

char params[96]; /* Parameter string for model execution */

if(XPRMinit()) /* Initialize Mosel */ return 1;

/* Prepare file names for ’initializations’ using the ’raw’ driver */ sprintf(vdata_name, "noindex,mem:%#lx/%u", (unsigned long)vdata,

sizeof(vdata));

sprintf(wdata_name, "noindex,mem:%#lx/%u", (unsigned long)wdata, sizeof(wdata));

sprintf(solution_name, "noindex,mem:%#lx/%u", (unsigned long)solution, sizeof(solution));

/* Pass file names as execution param.s */ sprintf(params, "VDATA=’%s’,WDATA=’%s’,SOL=’%s’", vdata_name, wdata_name,

solution_name);

if(XPRMexecmod(NULL, "burglar6.mos", params, &result, &mod)) return 2; /* Execute a model file */

if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT)

return 3; /* Test whether a solution is found */

/* Display solution values obtained from the model */ printf("Objective value: %g\n", XPRMgetobjval(mod)); for(i=0;i<8;i++)

printf(" take(%d): %g\n", i+1, solution[i]);

XPRMresetmod(mod); /* Reset the model */

return 0; }

In this example we use the raw IO driver for communication between the application and the model it executes. Employing this driver means that data is saved in binary format. File names used with the raw driver have the form rawoption[,...],filename. The option noindex for this driver indicates that data is to be stored in dense format, that is, just the data entries without any information about the indices—this format supposes that the index set(s) is known in the Mosel model before data is read in. The filename uses the mem driver, this means that data is stored in memory. The actual location of the data is specified by giving the address of the corresponding memory block and its size.

The program above works with the following version of the ‘Burglar’ model where the locations of input and output data are specified by the calling application through model parameters. Instead of printing out the solution in the model, we copy the solution values of the decision variables take into the array of reals soltake that is written to memory and will be processed by the host application.

model Burglar6 uses "mmxprs"

parameters

VDATA = ’’; WDATA = ’’ ! Locations of input data

SOL = ’’ ! Location for solution data output WTMAX = 102 ! Maximum weight allowed

end-parameters

declarations

ITEMS = 1..8 ! Index range for items

VALUE: array(ITEMS) of real ! Value of items WEIGHT: array(ITEMS) of real ! Weight of items

take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise soltake: array(ITEMS) of real ! Solution values

end-declarations

initializations from ’raw:’ VALUE as VDATA WEIGHT as WDATA end-initializations

! Objective: maximize total value

MaxVal:= sum(i in ITEMS) VALUE(i)*take(i)

! Weight restriction

sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX

! All variables are 0/1

forall(i in ITEMS) take(i) is_binary

maximize(MaxVal) ! Solve the MIP-problem

! Output solution to calling application

forall(i in ITEMS) soltake(i):= getsol(take(i))

initializations to ’raw:’ soltake as SOL

end-initializations

end-model

13.4.2

Sparse arrays

Let us now take a look at the case where we use a set of strings instead of a simple range set to index the various arrays in our model. Storing the indices with the data values makes necessary slightly more complicated structures in our C program for the input and solution data. In the C program below (file ugiosparse.c), every input data entry defines both, the value and the weight coefficient for the corresponding index.

#include <stdio.h> #include "xprm_mc.h"

const struct

{ /* Initial values for array ’data’: */ const char *ind; /* index name */

double val,wght; /* value and weight data entries */ } data[]={{"camera",15,2}, {"necklace",100,20}, {"vase",90,20},

{"picture",60,30}, {"tv",40,40}, {"video",15,30}, {"chest",10,60}, {"brick",1,10}};

const struct

{ /* Array to receive solution values: */ const char *ind; /* index name */

double val; /* solution value */ } solution[8];

int main() {

XPRMmodel mod; int i,result;

char data_name[40]; /* File name of input data ’data’ */ char solution_name[40]; /* File name of solution values */

char params[96]; /* Parameter string for model execution */

if(XPRMinit()) /* Initialize Mosel */ return 1;

/* Prepare file names for ’initializations’ using the ’raw’ driver */ sprintf(data_name, "slength=0,mem:%#lx/%u", (unsigned long)data,

sizeof(data));

sprintf(solution_name, "slength=0,mem:%#lx/%u", (unsigned long)solution, sizeof(solution));

/* Pass file names as execution param.s */ sprintf(params, "DATA=’%s’,SOL=’%s’", data_name, solution_name);

if(XPRMexecmod(NULL, "burglar7.mos", params, &result, &mod)) return 2; /* Execute a model file */

if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT)

return 3; /* Test whether a solution is found */

/* Display solution values obtained from the model */ printf("Objective value: %g\n", XPRMgetobjval(mod)); for(i=0;i<8;i++)

printf(" take(%s): %g\n", solution[i].ind, solution[i].val);

XPRMresetmod(mod);

return 0; }

The use of the two IO drivers is quite similar to what we have seen before. We now pass on data in sparse format, this means that every data entry is saved together with its index (tuple). Option slength=0of the raw driver indicates that strings are represented by pointers to null terminated arrays of characters (C-string) instead of fixed size arrays.

Similarly to the model of the previous section, the model burglar7.mos executed by the C program above reads and writes data from/to memory using the raw driver and the locations are specified by the calling application through the model parameters. Since the contents of the index set ITEMS is not defined in the model we have moved the declaration of the decision variables after the data input where the contents of the set is known, thus avoiding the creation of the array of decision variables as a dynamic array.

model Burglar7 uses "mmxprs"

parameters

DATA = ’’ ! Location of input data

SOL = ’’ ! Location for solution data output WTMAX = 102 ! Maximum weight allowed

end-parameters

declarations

ITEMS: set of string ! Index set for items VALUE: array(ITEMS) of real ! Value of items WEIGHT: array(ITEMS) of real ! Weight of items end-declarations

initializations from ’raw:’ [VALUE,WEIGHT] as DATA end-initializations

declarations

take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise end-declarations

! Objective: maximize total value

MaxVal:= sum(i in ITEMS) VALUE(i)*take(i)

! Weight restriction

sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX

! All variables are 0/1

forall(i in ITEMS) take(i) is_binary

maximize(MaxVal) ! Solve the MIP-problem

! Output solution to calling application

forall(i in ITEMS) soltake(i):= getsol(take(i))

initializations to ’raw:’ soltake as SOL

end-initializations

end-model

Documento similar