FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Basic embedding tasks

  • ugcomptmp.cs: Compiling a model into a BIM file saved in Mosel's temporary directory (requires burglar2.mos, burglar.dat)
  • ugrun.cs: Compiling a model into a BIM file, then load and run it (requires burglar2.bim, burglar.dat)
  • ugdefstream.cs: Redirecting the model output (requires burglar2.mos, burglar.dat)
  • ugarray.cs: Accessing modeling objects: sparse arrays (requires transport.mos, transprt.dat)
  • ugcb.cs: Retrieve model output via a callback into a custom TextWriter (requires burglar2.mos, burglar.dat)
  • ugcb2.cs: Retrieve model output via a callback into a string (requires burglar2.mos, burglar.dat)
  • ugparam.cs: Passing parameters to a Mosel model (requires prime.mos)
  • Accessing modeling objects and solution information (requires burglar3.mos, burglar.dat)

Source Files

Data Files


   Mosel User Guide Example Problems

   file ugcb.cs
   Retrieve model output via callback-style functionality.
   (c) 2013 Fair Isaac Corporation
       author: S.Heipcke, J.Farmer, Mar. 2013 

using System;
using System.IO;
using System.Text;
using Mosel;

namespace ugcb.cs {

  public class ugcb {
    /// <summary>
    /// Main entry point for the application
    /// </summary>
    static void Main(string[] args) {
      // Initialize Mosel
      XPRM mosel = XPRM.Init();

      // Associate .NET object with a name in Mosel
      mosel.Bind("mycb", new MyOut());

      // Redirect error stream to stdout
      mosel.SetDefaultStream(XPRMStreamType.F_ERROR, Console.Out);

      // Compile and load the Mosel model
      XPRMModel model = mosel.CompileAndLoad("burglar2.mos");
      // Redirect the model's output to a custom TextWriter
      MyOut modelOut = new MyOut();
      model.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF, modelOut);

      // Alternative:
      // Redirect the model's output to our printing function 'cbmsg'
      model.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF, "dotnet:mycb");

      // Run the model


  public class MyOut: TextWriter
   private bool atStartOfLine = true;
   public override void Write(char b)
     if (atStartOfLine) {
      Console.Write("Mosel: ");
     if (b=='\n') {
     else if (b=='\r') {
      // ignore
     else {
   public override Encoding Encoding {
    get {
     return Encoding.UTF8;

Back to examples browserPrevious exampleNext example