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

Basic embedding tasks

Description
  • 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)
  • ugsol.java: Accessing modeling objects and solution information (requires burglar3.mos, burglar.dat)


Source Files

Data Files





ugsol.cs

/********************************************************
   Mosel User Guide Example Problems
   ================================= 


   file ugsol.cs
   `````````````
   Accessing modeling objects and solution information.
   
   (c) 2013 Fair Isaac Corporation
       author: S.Heipcke, Apr. 2013 
********************************************************/


using System;
using System.IO;
using Mosel;


namespace ugsol.cs {

  public class ugsol {
    /// <summary>
    /// Main entry point for the application
    /// </summary>
    [STAThread]
    static int Main(string[] args) {
      XPRMArray varr, darr;
      XPRMSet set;
      XPRMMPVar x;
      double val;

      // Initialize Mosel
      XPRM mosel = XPRM.Init();

      // Compile and load a model
      XPRMModel model = mosel.CompileAndLoad("burglar3.mos");

      // Run the model
      model.Run();

      if(model.ProblemStatus!=XPRMProblemStatus.PB_OPTIMAL) 
        return 1;                    // Stop if no solution found

      Console.WriteLine("Objective value: " + model.ObjectiveValue);
                                     // Print the objective function value

      // Get model object 'take', it must be an array
      varr=(XPRMArray)model.FindIdentifier("take");  
      // Get model object 'VALUE', it must be an array
      darr=(XPRMArray)model.FindIdentifier("VALUE");
      // Get model object 'ITEMS', it must be a set
      set=(XPRMSet)model.FindIdentifier("ITEMS");

      // Enumerate all entries of 'varr' (dense array)
      foreach(int[] indices in varr.Indices)
      {
    x = varr.Get(indices).AsMPVar();   // Get a variable from varr
        val = darr.GetAsReal(indices);     // Get the corresponding value
//        Console.WriteLine("take(" + set.GetAsString(indices[0]) + "): " +
        Console.WriteLine("take" + varr.IndexToString(indices) + ": " +
                    x.Solution + "\t (item value: " + val + ")");
      }

      model.Reset();                         // Reset the model
      return 0;
    }
  }
}

Back to examples browserPrevious exampleNext example