Initializing help system before first use

Switching to solving with Xpress Optimizer


Type: Embedding
Rating: 4 (medium-difficult)
Description: Passing from Mosel to solving with Xpress Solver (requires burglar4.mos, burglar.dat)
File(s): ugxprs1.c, ugxprs2.c, burglar4.mos
Data file(s): burglar.dat

ugxprs1.c
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file ugxprs1.c
   ``````````````
   Passing from Mosel to solving with Xpress Optimizer.
   Running the BIM file.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. Nov. 2010
********************************************************/

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

#ifdef _WIN64
#define strtoadr _strtoui64
#else
#define strtoadr strtoul
#endif

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 Xpress Optimizer */
 if((dso=XPRMfinddso("mmxprs"))==NULL)
  return 4;             
 if(XPRMgetdsoparam(mod, dso, "xprs_problem", &result, &rvalue))
  return 5;
 prob=(XPRSprob)strtoadr(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_INPUTCOLS, &ncol))
   return 9; 
  if((sol = (double *)malloc(ncol * sizeof(double)))==NULL)
   return 10;
  if(XPRSgetsolution(prob, NULL, sol, 0, ncol-1))
   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;
}


ugxprs2.c
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file ugxprs2.c
   ``````````````
   Passing from Mosel to solving with Xpress Optimizer.
   Executing a model file.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2002, rev. Nov. 2010
********************************************************/

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

#ifdef _WIN64
#define strtoadr _strtoui64
#else
#define strtoadr strtoul
#endif

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(XPRMexecmod(NULL,"burglar4.mos",NULL,&result,&mod))
  return 2;                             /* Execute a model file */

  /* Retrieve the pointer to the problem loaded in the Xpress Optimizer */
 if((dso=XPRMfinddso("mmxprs"))==NULL)
  return 4;             
 if(XPRMgetdsoparam(mod, dso, "xprs_problem", &result, &rvalue))
  return 5;
 prob=(XPRSprob)strtoadr(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_INPUTCOLS, &ncol))
   return 9; 
  if((sol = (double *)malloc(ncol * sizeof(double)))==NULL)
   return 10;
  if(XPRSgetsolution(prob, NULL, sol, 0, ncol-1))
   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;
}


burglar4.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file burglar4.mos
   `````````````````
   Same as burglar3.mos but no optimization.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. June 2018
*******************************************************!)

model Burglar4
 uses "mmxprs"
 
 public 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)

end-model

© 2001-2025 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.