Initializing help system before first use

Retrieving data from a Mosel model


Type: Programming
Rating: 3 (intermediate)
Description: mmexset.java: Using sets in Mosel (requires burglari.bim)
  • retrieve a set by its model name
  • get the set size
  • get first and last set element
  • get the name or index of a set element
mmexas.java: Using arrays with index sets (requires trans.bim)
  • get indexing sets of an array
  • get array type
  • enumerate array entries in usual and transposed order
  • enumerate true array entries
mmexlst.java: Using lists in Mosel (requires euler.mos and euler.dat)
  • retrieve a list by its model name
  • get the list size
  • enumerate the list elements
  • get value of list element
mmexrec.java: Using records in Mosel (requires burglar_rec.mos and burglar_rec.dat)
  • retrieve an array of records (user type) by its model name
  • retrieve the record field information (field name, type, and number)
  • enumerate the array of records
  • for each array entry (record) get the value of all its fields
mmexprob.java: Accessing problems and solution information with Mosel (requires blend2.bim)
  • export problem to a file (MPS or LP format)
  • get problem status
  • get objective function value
  • get primal/dual solution values, and constraint activity
Note that these examples require the provided mos files to be pre-compiled.
File(s): mmexset.java, mmexas.java, mmexlst.java, mmexrec.java, mmexprob.java
Data file(s): burglari.mos, trans.mos, euler.mos, euler.dat, burglar_rec.mos, burglar_rec.dat, blend2.mos


mmexset.java
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexset.java                                   */
/*  `````````````````                                   */
/*  Example for the use of the Mosel libraries          */
/*  (accessing sets in Mosel)                           */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: S. Heipcke, 2004                        */
/********************************************************/

import com.dashoptimization.*;

public class mmexset
{
public static void main(String[] args) throws Exception
{
 XPRM mosel;
 XPRMModel mod;
 XPRMSet set;
 int first,last;

 mosel=new XPRM();                          // Initialize Mosel
 mod=mosel.loadModel("Models/burglari.bim");// Load a BIM file
 mod.run();                                 // Run the model

 set=(XPRMSet)mod.findIdentifier("ITEMS"); // Get the model object named 'ITEMS'
                                           // it must be a set
  
 if(!set.isEmpty())
 {
  first = set.getFirstIndex();       // Get the number of the first index
  last = set.getLastIndex();         // Get the number of the last index
  System.out.println("Elements of set ITEMS:");
  for(int i=first;i<=last;i++)       // Print names of all set elements
   System.out.print(" " + set.getAsString(i) + ",");
  System.out.println();  
 }

 if(set.getIndex("CD player")<0)
  System.out.println("'CD player' is not contained in 'ITEMS'."); 
}
}

mmexas.java
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexas.java                                    */
/*  ````````````````                                    */
/*  Example for the use of the Mosel libraries          */
/*  (using arrays with index sets: different ways       */
/*   of enumerating arrays)                             */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: S. Heipcke, 2004                        */
/********************************************************/

import com.dashoptimization.*;

public class mmexas
{
public static void main(String[] args) throws Exception
{
 XPRM mosel;
 XPRMModel mod;
 XPRMArray varr;
 XPRMSet[] sets;
 int[] indices;
 int dim;

 mosel=new XPRM();                        // Initialize Mosel
 mod=mosel.loadModel("Models/trans.bim"); // Load a BIM file
 mod.run();                               // Run the model

 varr=(XPRMArray)mod.findIdentifier("x"); // Get the model object named 'x'
                                          // it must be an array
  
 dim=varr.getDimension();                 // Get the number of dimensions of
                                          // the array
 sets=varr.getIndexSets();                // Get the indexing sets
   
 System.out.println("\n1. Logic entries:");
 indices=varr.getFirstIndex();            // Get the first entry of varr
 do
 {
  System.out.print("x(");
  for(int i=0;i<dim-1;i++)
   System.out.print(sets[i].get(indices[i])+",");
  System.out.print(sets[dim-1].get(indices[dim-1])+"), ");
 } while(varr.nextIndex(indices));        // Get the next index tuple

 if(varr.isDynamic())                     /* There would be no difference to
                                             the first way of enumerating 
                                             in the case of a dense array */
 { 
  System.out.println("\n\n2. True entries:");
  indices=varr.getFirstTEIndex();          // Get the first true entry index
  do
  {
   System.out.print("x(");
   for(int i=0;i<dim-1;i++)
    System.out.print(sets[i].get(indices[i])+",");
   System.out.print(sets[dim-1].get(indices[dim-1])+"), ");
  } while(varr.nextTEIndex(indices));     // Get next true entry index tuple
 } 
 System.out.println();

 mod.reset();
}
}

mmexlst.java
/*******************************************************
   Mosel Library Examples
   ====================== 

   file mmexlst.java
   `````````````````
   Accessing modeling objects 
   (enumerating the elements of a list).
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2007
********************************************************/

import com.dashoptimization.*;

public class mmexlst
{
 public static void main(String[] args) throws Exception
 {
  XPRM mosel;
  XPRMModel mod;
  XPRMList lst;

  mosel = new XPRM();                  // Initialize Mosel
                           // Disable output from Mosel (make model silent)
  mosel.setDefaultStream(XPRM.F_OUTPUT, "null:");
                                 
  mosel.compile("Models/euler.mos");   // Compile, load & run the model
  mod = mosel.loadModel("Models/euler.bim");
  mod.run();

                           // Get the model object named 'TOUR'
  lst=(XPRMList)mod.findIdentifier("TOUR");      

  System.out.print("Tour: ");          // Print out all list elements
  for(XPRMListElements el=lst.elements(); el.hasNext();)
  {
   int value=el.nextAsInteger();
   if (el.hasNext())  
    System.out.print(value + " -> ");
   else
    System.out.println(value);   
  }

  mod.reset();                         // Reset the model 
 }
}


mmexrec.java
/*******************************************************
   Mosel Library Examples
   ====================== 

   file mmexrec.java
   `````````````````
   Accessing modeling objects 
   (enumerating an array of records and
    printing the value of each record field).
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2007
********************************************************/

import com.dashoptimization.*;

public class mmexrec
{
 public static void main(String[] args) throws Exception
 {
  XPRM mosel;
  XPRMModel mod;
  XPRMArray arr;
  XPRMUserType type;
  XPRMSet[] set;
  XPRMRecord rec;
  XPRMRecordField[] field=new XPRMRecordField[2];
  int ct;
  int[] indices;

  mosel = new XPRM();                      // Initialize Mosel
                                 
  mosel.compile("Models/burglar_rec.mos"); // Compile, load & run the model
  mod = mosel.loadModel("Models/burglar_rec.bim");
  mod.run();

  arr=(XPRMArray)mod.findIdentifier("I");  // Get the model object named 'I'
                                           // Expand type of a user type
  type=(XPRMUserType)mod.expandType(arr.getTypeCode()); 
//  if (type.getStructCode()!=type.STR_REC)  
   
          // Retrieve record field info (we know there are 2 fields)
  ct=0;
  for(XPRMRecordFields fields=type.fields(); fields.hasNext();) 
   field[ct++]=(XPRMRecordField)fields.next();

          // Enumerate the array (we know it has a single dimension) 
  set=arr.getIndexSets();                  // Get the indexing set
 
  indices=arr.getFirstIndex();             // Get the first index tuple
  do
  {                                        // Retrieve the array index
   System.out.print("I(" + set[0].get(indices[0]) + "):  \t");
   rec=arr.get(indices).asRecord();        // Retrieve array entry (=record)
                                           // Contents of 1st record field
   System.out.print(field[0].getName() + "=" + 
                      rec.getValueAsReal(field[0]) + " ");
                                           // Contents of 2nd record field
   System.out.println(field[1].getName() + "=" + 
                      rec.getValueAsReal(field[1]) + " ");
  } while(arr.nextIndex(indices));         // Get the next index tuple 
 
  mod.reset();                             // Reset the model 
 }
}

mmexprob.java
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexprob.java                                  */
/*  ``````````````````                                  */
/*  Example for the use of the Mosel libraries          */
/*  (accessing problems and solution information)       */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: S. Heipcke, 2004                        */
/********************************************************/

import com.dashoptimization.*;

public class mmexprob
{
public static void main(String[] args) throws Exception
{
 XPRM mosel;
 XPRMModel mod;
 XPRMArray varr, darr;
 XPRMLinCtr lgrade;
 int[] indices;

 mosel=new XPRM();                        // Initialize Mosel
 mod=mosel.loadModel("Models/blend2.bim");// Load a BIM file
 mod.run();                               // Run the model
                                          // (it includes optimization)

 
 mod.exportProblem("p","blend");          // Export problem to a file in LP 
                                          // format (maximization)

 if(mod.getProblemStatus()==mod.PB_OPTIMAL) // Test whether optimal is found
  System.out.println("Solution is optimal");

 System.out.println("Objective value: "+mod.getObjectiveValue());
                                          // Print the objective function value

 varr=(XPRMArray)mod.findIdentifier("x"); // Get the model object named 'x'
                                          // it must be an array

 darr=(XPRMArray)mod.findIdentifier("COST");// Get the model object 'COST'
                                            // it must be an array
   
 indices=varr.getFirstIndex();       // Get the first entry of the array varr
 do
 {
  // Display solution value and corresponding cost 
  System.out.println(
    "x(" + indices[0] + ")=" + varr.get(indices).asMPVar().getSolution() +
    "(COST: " + darr.getAsReal(indices) +
    ")");
 } while(varr.nextIndex(indices));   // Get the next index

                                     // Get the model object 'LoGrade'
                                     // it must be a reference to
                                     // a linear constraint
 lgrade=((XPRMReference)mod.findIdentifier("LoGrade")).asLinCtr();

                                      
 System.out.println(                 // Display the activity and dual values
   "LoGrade: activity=" + lgrade.getActivity() +
   ", dual=" + lgrade.getDual() );

 mod.reset();
}
}

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