Initializing help system before first use

Problem solving in C with Xpress Optimizer

In certain cases, for instance if the user wants to re-use parts of algorithms that he has written in C for the Xpress Optimizer, it may be necessary to pass from a problem formulation with Mosel to solving the problem in C by direct calls to the Optimizer. The following example shows how this may be done for the Burglar problem. We use a slightly modified version of the original Mosel model:

model Burglar4
 uses "mmxprs"

 declarations
  WTMAX=102                          ! Maximum weight allowed
  ITEMS={"camera", "necklace", "vase", "picture", "tv", "video",
         "chest", "brick"}           ! Index set 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
 end-declarations

 initializations from 'burglar.dat'
  VALUE  WEIGHT
 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

 setparam("XPRS_LOADNAMES", true)    ! Enable loading of object names
 loadprob(MaxVal)                    ! Load problem into the optimizer

end-model

The procedure maximize to solve the problem has been replaced by loadprob. This procedure loads the problem into the optimizer without solving it. We also enable the loading of names from Mosel into the optimizer so that we may obtain an easily readable output.

The following C program ugxprs1.c reads in the Mosel model and solves the problem by direct calls to Xpress Optimizer. To be able to address the problem loaded into the optimizer, we need to retrieve the optimizer problem pointer from Mosel. Since this information is a parameter (XPRS_PROBLEM) that is provided by module mmxprs, we first need to obtain the reference of this library (by using function XPRMfinddso).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xprm_rt.h"
#include "xprs.h"

int main()
{
 XPRMmodel mod;
 XPRMdsolib dso;
 XPRMalltypes rvalue;
 XPRSprob prob;
 int result, ncol, len, i;
 double *sol, val;
 char *names, *onecol;

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

 if((mod=XPRMloadmod("burglar4.bim", NULL))==NULL)  /* Load a BIM file */
  return 2;

 if(XPRMrunmod(mod, &result, NULL))     /* Run the model (no optimization) */
  return 3;

  /* Retrieve the pointer to the problem loaded in the Optimizer */
 if((dso=XPRMfinddso("mmxprs"))==NULL)
  return 4;
 if(XPRMgetdsoparam(mod, dso, "xprs_problem", &result, &rvalue))
  return 5;
 prob=(XPRSprob)strtoul(rvalue.ref,NULL,0);

 XPRSchgobjsense(prob, XPRS_OBJ_MAXIMIZE);  /* Set sense to maximization */
 if(XPRSmipoptimize(prob, ""))              /* Solve the problem */
  return 6;

 if(XPRSgetintattrib(prob, XPRS_MIPSTATUS, &result))
  return 7;
                                       /* Test whether a solution is found */
 if((result==XPRS_MIP_SOLUTION) || (result==XPRS_MIP_OPTIMAL))
 {
  if(XPRSgetdblattrib(prob, XPRS_MIPOBJVAL, &val))
   return 8;
  printf("Objective value: %g\n", val); /* Print the objective function value */

  if(XPRSgetintattrib(prob, XPRS_ORIGINALCOLS, &ncol))
   return 9;
  if((sol = (double *)malloc(ncol * sizeof(double)))==NULL)
   return 10;
  if(XPRSgetmipsol(prob, sol, NULL))
   return 11;                           /* Get the primal solution values */
  if(XPRSgetnamelist(prob, 2, NULL, 0, &len, 0, ncol-1))
   return 11;                           /* Get the name array length */
  if((names = (char *)malloc(len*sizeof(char)))==NULL)
   return 12;
  if(XPRSgetnamelist(prob, 2, names, len, NULL, 0, ncol-1))
   return 13;                           /* Get the variable names */
  onecol = names;
  for(i=0; i<ncol; i++) {               /* Print out the solution */
    printf("%s: %g\n", onecol, sol[i]);
    onecol = onecol+strlen(onecol)+1;
  }
  free(names);
  free(sol);
 }
 return 0;
}

Since the Mosel language provides ample programming facilities, in most applications there will be no need to switch from the Mosel language to problem solving in C. If nevertheless this type of implementation is chosen, it should be noted that it is not possible to get back to Mosel, once the Xpress Optimizer has been called directly from C: the solution information and any possible changes made to the problem directly in the optimizer are not communicated to Mosel.


© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.