/********************************************************/
/* Mosel Library Examples */
/* ====================== */
/* */
/* file mmexcbdrvs_cbinit.cs */
/* ``````````````````` */
/* Example for the use of the Mosel libraries */
/* (using dotnet: I/O driver for data exchange via */
/* callbacks */
/* */
/* (c) 2010 Fair Isaac Corporation */
/* author: J.Farmer, Y. Colombani, 2010 */
/********************************************************/
using System;
using System.IO;
using Mosel;
namespace mmexdrvs_cbinit {
public class mmexdrvs_cbinit_Class {
/***************************************/
/* The source of the model as a string */
/***************************************/
const string source_of_model=
"model tstcb\n"+
"uses 'mmsystem'\n"+
"parameters\n"+
" ICB_INITFROM=''\n"+
" ICB_INITTO=''\n"+
"end-parameters\n"+
"public declarations\n"+
" v_i:integer\n"+
" v_r:real\n"+
" v_s:string\n"+
" v_b:boolean\n"+
" v_d:date\n"+
" s_i:set of integer\n"+
" l_i:list of integer\n"+
" s_d:set of date\n"+
" l_d:list of date\n"+
" a_i:array(range) of integer\n"+
" Rx:range\n"+
" a_s:array(Rx) of string\n"+
" a_r:array(Rx) of real\n"+
" R=record\n"+
" public i:integer\n"+
" public s:set of integer\n"+
" end-record\n"+
" r:R\n"+
" a_R:array(range) of R\n"+
"end-declarations\n"+
"initialisations from ICB_INITFROM\n"+
" v_i\n"+
" v_r\n"+
" v_s\n"+
" v_b\n"+
" v_d\n"+
" s_i\n"+
" l_i\n"+
" s_d\n"+
" l_d\n"+
" a_i\n"+
" [a_s,a_r] as 'ax'\n"+
" r\n"+
" a_R\n"+
"end-initialisations\n"+
" writeln('v_i=',v_i)\n"+
" writeln('v_r=',v_r)\n"+
" writeln('v_s=',v_s)\n"+
" writeln('v_b=',v_b)\n"+
" writeln('v_d=',v_d)\n"+
" writeln('s_i=',s_i)\n"+
" writeln('l_i=',l_i)\n"+
" writeln('s_d=',s_d)\n"+
" writeln('l_d=',l_d)\n"+
" writeln('a_i=',a_i)\n"+
" writeln('a_r=',a_r)\n"+
" writeln('a_s=',a_s)\n"+
" writeln('r=',r)\n"+
" writeln('a_R=',a_R)\n"+
"initialisations to ICB_INITTO\n"+
" v_i\n"+
" v_r\n"+
" v_s\n"+
" v_b\n"+
" v_d\n"+
" s_i\n"+
" l_i\n"+
" s_d\n"+
" l_d\n"+
" a_i\n"+
" r\n"+
" a_R\n"+
"end-initialisations\n"+
"end-model";
/// <summary>
/// A function to initialize the Mosel data-structures via callback
/// </summary>
public static bool initializeFrom(XPRMInitializeContext ictx,string label,XPRMTyped type)
{
try {
switch (label) {
case "v_i": // v_i:999
ictx.Send(999);
return true;
case "v_r": // v_r:999.99
ictx.Send(999.99);
return true;
case "v_b": // v_b:false
ictx.Send(false);
return true;
case "v_s": // v_s:"tralala"
ictx.Send("tralala");
return true;
case "v_d": // v_d:"2012-12-12"
ictx.Send("2012-12-12");
return true;
case "s_i": // s_i:[10 20 30 ... ]
case "l_i":
ictx.Send(XPRMInitializeControl.OpenList);
for (int i=1;i<=10;i++)
ictx.Send(i*10);
ictx.Send(XPRMInitializeControl.CloseList);
return true;
case "s_d": // s_d:["2001-01-11" "2002-02-21" ... ]
case "l_d":
ictx.Send(XPRMInitializeControl.OpenList);
for (int i=1;i<=10;i++)
ictx.Send(string.Format("{0}-{1}-{2}",2000+i,i,i+1));
ictx.Send(XPRMInitializeControl.CloseList);
return true;
case "a_i": // a_i:[ (1) 10 (2) 20 ... ]
ictx.Send(XPRMInitializeControl.OpenList);
for (int i=1;i<=10;i++) {
ictx.Send(XPRMInitializeControl.OpenIndices);
ictx.Send(i);
ictx.Send(XPRMInitializeControl.CloseIndices);
ictx.Send(i*10);
}
ictx.Send(XPRMInitializeControl.CloseList);
return true;
case "ax": // ax:[ (1) [ "aa1" 1.23 ] (2) [ "aa2" 2.46 ] ... ]
ictx.Send(XPRMInitializeControl.OpenList);
for (int i=1;i<=10;i++) {
ictx.Send(XPRMInitializeControl.OpenIndices);
ictx.Send(i);
ictx.Send(XPRMInitializeControl.CloseIndices);
ictx.Send(XPRMInitializeControl.OpenList);
ictx.Send(string.Format("aa{0}",i));
ictx.Send((double)i*1.23);
ictx.Send(XPRMInitializeControl.CloseList);
}
ictx.Send(XPRMInitializeControl.CloseList);
return true;
case "r": // r:[ 123 [ 10 20 30 ] ]
ictx.Send(XPRMInitializeControl.OpenList);
ictx.Send(123);
ictx.Send(XPRMInitializeControl.OpenList);
for (int i=1;i<=3;i++)
ictx.Send(i*10);
ictx.Send(XPRMInitializeControl.CloseList);
ictx.Send(XPRMInitializeControl.CloseList);
return true;
case "a_R": // a_R:[ (1) [10 [10 20 30] ] (1) [20 [20 40 60] ] ... ]
ictx.Send(XPRMInitializeControl.OpenList);
for (int i=1;i<=10;i++) {
ictx.Send(XPRMInitializeControl.OpenIndices);
ictx.Send(i);
ictx.Send(XPRMInitializeControl.CloseIndices);
ictx.Send(XPRMInitializeControl.OpenList);
ictx.Send(i*10);
ictx.Send(XPRMInitializeControl.OpenList);
for (int j=1;j<=3;j++)
ictx.Send(j*i*10);
ictx.Send(XPRMInitializeControl.CloseList);
ictx.Send(XPRMInitializeControl.CloseList);
}
ictx.Send(XPRMInitializeControl.CloseList);
return true;
default:
Console.WriteLine("Label '{0}' not found", label);
return false;
}
} catch (Exception e) {
Console.WriteLine("Label '{0}' could not be initialized - {1}", label, e.Message);
return false;
}
}
/// <summary>
/// A method to retrieve data from Mosel
/// </summary>
public static bool initializeTo(string label,XPRMValue val) {
Console.WriteLine(".NET: {0} = {1}", label, val);
return true;
}
/// <summary>
/// Main function
/// </summary>
[STAThread]
public static void Main(string[] args) {
XPRM mosel; // An instance of Mosel
XPRMModel mod; // The model
// Initialize Mosel
mosel=XPRM.Init();
// Set default output stream to stdout
mosel.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF,Console.Out);
// Compile and load the model
mod = mosel.CompileAndLoad(new StringReader(source_of_model));
// Set the execution parameters and bind the variables
mod.SetExecParam("ICB_INITFROM","dotnet:cbinitfrom");
mod.SetExecParam("ICB_INITTO","dotnet:cbinitto");
mod.Bind("cbinitfrom", new XPRMInitializationFrom(initializeFrom));
mod.Bind("cbinitto", new XPRMInitializationTo(initializeTo));
// Run the model
mod.Run();
}
}
}
|
/********************************************************/
/* Mosel Library Examples */
/* ====================== */
/* */
/* file mmexdrvs_raw.cs */
/* ```````````````````` */
/* Example for the use of the Mosel libraries */
/* (using 'dotnetraw' IOdriver for data exchange) */
/* */
/* (c) 2009 Fair Isaac Corporation */
/* author: J. Farmer */
/********************************************************/
using System;
using System.IO;
using Mosel;
namespace mmexdrvs_raw.cs {
public class mmexdrvs_raw_Class {
/// <summary>
/// String containing the model
/// </summary>
const string ModelSourceCode =
"model drivers\n" +
"parameters\n" +
" DATA=''\n" +
" SOL=''\n" +
"end-parameters\n" +
"public declarations\n" +
" S:set of string\n" +
" R:range\n" +
" data:array(S,R) of real\n" +
" sol:array(1..10) of real\n" +
"end-declarations\n" +
"initialisations from 'dotnetraw:'\n" +
" data as 'DATA(s,r,v)'\n" +
"end-initialisations\n" +
"writeln('set S=',S)\n" +
"writeln('range R=',R)\n" +
"writeln('array data=',data)\n" +
"forall(i in 1..10) sol(i):=i^2\n" +
"initialisations to 'dotnetraw:noindex'\n" +
" sol as 'SOL'\n" +
"end-initialisations\n" +
"end-model";
/// <summary>
/// Structure to store initial values for the array 'data'
/// </summary>
class ModelDataElement {
public string s;
public int r;
public double v;
public ModelDataElement(string s, int r, double v) {
this.s = s;
this.r = r;
this.v = v;
}
}
/// <summary>
/// The initial values for the array 'data'
/// </summary>
private static ModelDataElement[] ModelData = new ModelDataElement[] {
new ModelDataElement( "one" , 2 , 12.5 ),
new ModelDataElement( "two" , 1 , 15 ),
new ModelDataElement( "three" , 16 , 9 ),
new ModelDataElement(" hundred", 2 , 17 )
};
/// <summary>
/// Main entry point for the application
/// </summary>
[STAThread]
static void Main(string[] args) {
// Initialize Mosel
XPRM mosel = XPRM.Init();
// Use a StringReader to compile and load the Mosel model directly from a .NET string
XPRMModel model = mosel.CompileAndLoad(new StringReader(ModelSourceCode));
// Bind the initializaton data to the name "DATA" which the dotnetraw: driver
// will find.
mosel.Bind("DATA", ModelData);
// Create a new array for solution data and bind that to the name 'SOL'
double[] solution = new double[10];
mosel.Bind("SOL", solution);
// Collect the model's output into a string
StringWriter modelOut = new StringWriter();
model.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF, modelOut);
// Run the model
model.Run();
// Print the solution
Console.Write("Solution values:");
for (int i=0;i<10;i++)
Console.Write(" {0}", solution[i]);
Console.WriteLine();
Console.WriteLine();
// Print the output
string modelOutText = modelOut.ToString();
if (modelOutText.Length>0)
Console.WriteLine("The model's output was:\n{0}", modelOutText);
}
}
}
|
/********************************************************/
/* Mosel Library Examples */
/* ====================== */
/* */
/* file mmexdrvs_stream.cs */
/* ``````````````````````` */
/* Example for the use of the Mosel libraries */
/* (using 'dotnet' IOdriver for data exchange) */
/* */
/* (c) 2010 Fair Isaac Corporation */
/* author: J. Farmer */
/********************************************************/
using System;
using System.IO;
using Mosel;
namespace mmexdrvs_stream.cs {
public class mmexdrvs_stream_Class {
/// <summary>
/// String containing the model
/// </summary>
const string BlendMos =
"model Blend\n" +
"uses \"mmxprs\"\n" +
"public declarations\n" +
" ROres = 1..2\n" +
" REV = 125\n" +
" MINGRADE = 4\n" +
" MAXGRADE = 5\n" +
" COST: array(ROres) of real\n" +
" AVAIL: array(ROres) of real\n" +
" GRADE: array(ROres) of real\n" +
" x: array(ROres) of mpvar\n" +
"end-declarations\n" +
"\n" +
"initializations from 'dotnet:BlendIni'\n" +
" COST\n" +
" AVAIL\n" +
" GRADE\n" +
"end-initializations\n" +
"\n" +
"Profit:= sum(o in ROres) (REV-COST(o))*x(o)\n" +
"LoGrade:= sum(o in ROres) (GRADE(o)-MINGRADE) * x(o) >= 0\n" +
"UpGrade:= sum(o in ROres) (MAXGRADE-GRADE(o)) * x(o) >= 0\n" +
"\n" +
"forall(o in ROres) x(o)<=AVAIL(o)\n" +
"\n" +
"maximize(Profit)\n" +
"writeln(\"Objective:\", getobjval)\n" +
"end-model";
/// <summary>
/// String containing initialization data for the model
/// </summary>
const string BlendDat =
"COST: [85 93]\n" +
"AVAIL: [60 45]\n" +
"GRADE: [2.1 6.3]\n";
/// <summary>
/// Main entry point for the application
/// </summary>
[STAThread]
static void Main(string[] args) {
// Initialize Mosel
XPRM mosel = XPRM.Init();
// Use a StringReader to compile and load the Mosel model directly from a .NET string
XPRMModel model = mosel.CompileAndLoad(new StringReader(BlendMos));
// Bind a stream based on the BlendDat data to the name 'BlendIni' where the model
// will expect to find its initialization data
model.Bind("BlendIni", new StringReader(BlendDat));
// Collect the model's output into a string
StringWriter modelOut = new StringWriter();
model.SetDefaultStream(XPRMStreamType.F_OUTPUT_LINEBUF, modelOut);
// Run the model
model.Run();
// Print the output
string modelOutText = modelOut.ToString();
Console.WriteLine("The model's output was: {0}", modelOutText);
}
}
}
|