ugiocb.cs |
/********************************************************
Mosel User Guide Example Problems
=================================
file ugiocb.cs
``````````````
Example for the use of the Mosel libraries
(using 'dotnet' IO driver for data exchange)
(c) 2013 Fair Isaac Corporation
author: S.Heipcke, Mar. 2013
********************************************************/
using System;
using System.IO;
using Mosel;
namespace ugiocb.cs {
public class ugiocb {
/// <summary>
/// Arrays containing initialization data for the model
/// </summary>
static double[] vdata=new double[] {15,100,90,60,40,15,10, 1}; // VALUE
static double[] wdata=new double[] { 2, 20,20,30,40,30,60,10}; // WEIGHT
static string[] ind=new string[] {"camera", "necklace", "vase", "picture",
"tv", "video", "chest", "brick"}; // Index names
static int datasize=8;
/// <summary>
/// Structure to receive solution values
/// </summary>
class MySol {
public string ind; // index name
public double val; // solution value
}
static MySol[] solution;
static int solsize;
/// <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 "DATA":
ictx.Send(XPRMInitializeControl.OpenList);
for (int i=0;i<datasize;i++) {
ictx.Send(XPRMInitializeControl.OpenIndices);
ictx.Send(ind[i]);
ictx.Send(XPRMInitializeControl.CloseIndices);
ictx.Send(XPRMInitializeControl.OpenList);
ictx.Send(vdata[i]);
ictx.Send(wdata[i]);
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);
XPRMArray solarr;
XPRMValue[] vindex;
switch (label) {
case "SOL":
solarr=(XPRMArray)val;
solsize=solarr.Size;
solution = new MySol[solsize];
for(int i=0;i<solsize;i++) solution[i] = new MySol();
int ct=0;
// Enumerate solarr as sparse array
foreach(int [] indices in solarr.TEIndices) {
vindex = solarr.DereferenceIndex(indices);
solution[ct].ind = vindex[0].AsString();
solution[ct].val = solarr.GetAsReal(indices);
ct++;
}
return true;
default:
Console.WriteLine("Unknown output data item: '{0}'={1} not found", label, val);
return false;
}
}
/// <summary>
/// Main entry point for the application
/// </summary>
[STAThread]
static int Main(string[] args) {
// Initialize Mosel
XPRM mosel = XPRM.Init();
// Set Mosel work directory to folder containing our example source code
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Compile and load the Mosel model
XPRMModel model = mosel.CompileAndLoad("burglar13.mos");
// Set the execution parameters and bind the variables
model.SetExecParam("DATAFILE","dotnet:cbinitfrom");
model.SetExecParam("SOLFILE","dotnet:cbinitto");
model.Bind("cbinitfrom", new XPRMInitializationFrom(initializeFrom));
model.Bind("cbinitto", new XPRMInitializationTo(initializeTo));
// Run the model
model.Run();
if(model.ProblemStatus!=XPRMProblemStatus.PB_OPTIMAL)
return 1; // Stop if no solution found
// Display solution values obtained from the model
Console.WriteLine("Objective value: {0}", model.ObjectiveValue);
for(int i=0;i<solsize;i++)
Console.WriteLine(" take({0}): {1}", solution[i].ind, solution[i].val);
model.Reset(); // Reset the model
return 0;
}
}
}
|
|
ugiocb.csproj |
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Content Include="../burglar13.mos" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FICO.Xpress.XPRMdn" Version="5.6.1" />
</ItemGroup>
</Project>
|
|
ugiodense.cs |
/********************************************************
Mosel User Guide Example Problems
=================================
file ugiodense.cs
`````````````````
Exchanging data between model and host application.
- Dense data -
(c) 2012 Fair Isaac Corporation
author: S.Heipcke, Oct. 2012
********************************************************/
using System;
using System.IO;
using Mosel;
namespace ugiodense.cs {
public class ugiodense {
/// <summary>
/// Arrays containing initialization data for the model
/// </summary>
static double[] vdata = new double[] {15,100,90,60,40,15,10, 1}; // VALUE
static double[] wdata = new double[] { 2, 20,20,30,40,30,60,10}; // WEIGHT
/// <summary>
/// Main entry point for the application
/// </summary>
[STAThread]
static void Main(string[] args) {
// Initialize Mosel
XPRM mosel = XPRM.Init();
// Set Mosel work directory to folder containing our example source code
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Compile and load the Mosel model
XPRMModel model = mosel.CompileAndLoad("burglar8d.mos");
// Associate the .NET objects with names in Mosel
model.Bind("vdat", vdata);
model.Bind("wdat", wdata);
// Create a new array for solution data and bind that to the name 'SOL'
double[] solution = new double[8];
mosel.Bind("sol", solution);
// Pass data location as a parameter to the model
model.ExecParams = "VDATA='noindex,vdat',WDATA='noindex,wdat',SOL='noindex,sol'";
// Run the model
model.Run();
// Print the solution
Console.WriteLine("Objective value: {0}", model.ObjectiveValue);
for (int i=0;i<8;i++)
Console.Write(" take({0}): {1}", (i+1), solution[i]);
Console.WriteLine();
}
}
}
|
|
ugiodense.csproj |
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Content Include="../burglar8d.mos" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FICO.Xpress.XPRMdn" Version="5.6.1" />
</ItemGroup>
</Project>
|
|
ugioscalar.cs |
/********************************************************
Mosel User Guide Example Problems
=================================
file ugioscalar.cs
``````````````````
Exchanging data between model and host application.
- Scalars -
(c) 2013 Fair Isaac Corporation
author: S.Heipcke, Apr. 2013
********************************************************/
using System;
using System.IO;
using Mosel;
namespace ugioscalar.cs {
public class ugioscalar {
/// <summary>
/// Structure to receive solution values
/// </summary>
class MyData {
public int wmax;
public int numitem;
public double objval;
}
/// <summary>
/// Main entry point for the application
/// </summary>
[STAThread]
static int Main(string[] args) {
MyData data=new MyData();
data.wmax=100;
// Initialize Mosel
XPRM mosel = XPRM.Init();
// Set Mosel work directory to folder containing our example source code
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Compile and load a model
XPRMModel model = mosel.CompileAndLoad("burglar11.mos");
// Associate the .NET object with a name in Mosel
model.Bind("data", data);
// Run the model, passing data location as parameters
model.ExecParams =
"WMAX='data(wmax)',NUM='data(numitem)',SOLVAL='data(objval)'," +
"IODRV='dotnetraw:'";
model.Run();
if(model.ProblemStatus!=XPRMProblemStatus.PB_OPTIMAL)
return 1; // Stop if no solution found
// Display solution values obtained from the model
Console.WriteLine("Objective value: " + data.objval);
Console.WriteLine("Total number of items: " + data.numitem);
model.Reset(); // Reset the model
return 0;
}
}
}
|
|
ugioscalar.csproj |
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Content Include="../burglar11.mos" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FICO.Xpress.XPRMdn" Version="5.6.1" />
</ItemGroup>
</Project>
|
|
ugiosparse.cs |
/********************************************************
Mosel User Guide Example Problems
=================================
file ugiosparse.cs
``````````````````
Exchanging data between model and host application.
- Sparse data -
(c) 2012 Fair Isaac Corporation
author: S.Heipcke, Oct. 2012
********************************************************/
using System;
using System.IO;
using Mosel;
namespace ugiosparse.cs {
public class ugiosparse {
/// <summary>
/// Arrays containing initialization data for the model
/// </summary>
static double[] vdata = new double[] {15,100,90,60,40,15,10, 1}; // VALUE
static double[] wdata = new double[] { 2, 20,20,30,40,30,60,10}; // WEIGHT
/// <summary>
/// Structure to store initial values for the array 'data'
/// </summary>
class MyData {
public string ind;
public double val;
public double wght;
public MyData(string i, double v, double w) {
this.ind = i;
this.val = v;
this.wght = w;
}
}
/// <summary>
/// Structure to receive solution values
/// </summary>
class MySol {
public string ind;
public double val;
}
/// <summary>
/// The initial values for the array 'data'
/// </summary>
private static MyData[] data = new MyData[] {
new MyData("camera",15,2), new MyData("necklace",100,20),
new MyData("vase",90,20), new MyData("picture",60,30),
new MyData("tv",40,40), new MyData("video",15,30),
new MyData("chest",10,60), new MyData("brick",1,10) };
/// <summary>
/// Main entry point for the application
/// </summary>
[STAThread]
static void Main(string[] args) {
// Initialize Mosel
XPRM mosel = XPRM.Init();
// Set Mosel work directory to folder containing our example source code
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Compile and load the Mosel model
XPRMModel model = mosel.CompileAndLoad("burglar9d.mos");
// Associate the .NET object with a name in Mosel
model.Bind("dt", data);
// Create a new array for solution data and bind that to the name 'SOL'
MySol[] solution=new MySol[8];
for(int i=0;i<8;i++) solution[i] = new MySol();
mosel.Bind("sol", solution);
// Pass data location as a parameter to the model
model.ExecParams = "DATA='dt(ind,val,wght)',SOL='sol(ind,val)'";
// Run the model
model.Run();
// Print the solution
Console.WriteLine("Objective value: {0}", model.ObjectiveValue);
for (int i=0;i<8;i++)
Console.Write(" take({0}): {1}", solution[i].ind, solution[i].val);
Console.WriteLine();
}
}
}
|
|
ugiosparse.csproj |
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Content Include="../burglar9d.mos" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FICO.Xpress.XPRMdn" Version="5.6.1" />
</ItemGroup>
</Project>
|
|
ugdatastream.cs |
/********************************************************
Mosel User Guide Example Problems
=================================
file ugdatastream.cs
````````````````````
Example for the use of the Mosel libraries
(using 'dotnet' IO driver for data exchange)
(c) 2012 Fair Isaac Corporation
author: S.Heipcke, Oct. 2012
********************************************************/
using System;
using System.IO;
using Mosel;
namespace ugdatastream.cs {
public class ugdatastream_Class {
/// <summary>
/// String containing initialization data for the model
/// </summary>
const string BurglarDat =
"DATA: [(\"camera\") [15 2] (\"necklace\") [100 20] (\"vase\") [90 20]" +
"(\"picture\") [60 30] (\"tv\") [40 40] (\"video\") [15 30] " +
"(\"chest\") [10 60] (\"brick\") [1 10] ]\n";
/// <summary>
/// Main entry point for the application
/// </summary>
[STAThread]
static void Main(string[] args) {
// Initialize Mosel
XPRM mosel = XPRM.Init();
// Set Mosel work directory to folder containing our example source code
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Compile and load the Mosel model
XPRMModel model = mosel.CompileAndLoad("burglar13.mos");
// Bind a stream based on the BurglarDat data to the name 'BurglarIni'
// where the model will expect to find its initialization data
model.Bind("BurglarIni", new StringReader(BurglarDat));
// Pass data location as a parameter to the model
model.SetExecParam("DATAFILE","dotnet:BurglarIni");
// Run the model
model.Run();
}
}
}
|
|
ugdatastream.csproj |
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Content Include="../burglar13.mos" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FICO.Xpress.XPRMdn" Version="5.6.1" />
</ItemGroup>
</Project>
|
|
burglar13.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar13.mos
``````````````````
Same as burglar2.mos, with input from/output to
calling application.
*** Not intended to be run standalone - run from ugiocb.* ***
(c) 2009 Fair Isaac Corporation
author: S. Heipcke, Nov. 2009
*******************************************************!)
model Burglar13
uses "mmxprs"
parameters
DATAFILE = '' ! Location of input data
SOLFILE = '' ! Location for solution data output
WTMAX = 102 ! Maximum weight allowed
end-parameters
declarations
ITEMS: set of string ! Index set for items
VALUE: array(ITEMS) of real ! Value of items
WEIGHT: array(ITEMS) of real ! Weight of items
soltake: array(ITEMS) of real ! Solution values
end-declarations
initializations from DATAFILE
[VALUE,WEIGHT] as "DATA"
end-initializations
declarations
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
! 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
maximize(MaxVal) ! Solve the MIP-problem
! Output solution to calling application
forall(i in ITEMS) soltake(i):= getsol(take(i))
initializations to SOLFILE
soltake as "SOL"
end-initializations
end-model
|
|
burglar8d.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar8d.mos
```````````````````
Model as in burglar.mos, with data input/ouput
from/to calling .NET application.
*** Not intended to be run standalone - run from ugiodense.cs ***
(c) 2012 Fair Isaac Corporation
author: S. Heipcke, Oct. 2012
*******************************************************!)
model Burglar8
uses "mmxprs"
parameters
VDATA = ''; WDATA = '' ! Location of input data
SOL = '' ! Location for solution data output
WTMAX = 102 ! Maximum weight allowed
end-parameters
declarations
ITEMS = 1..8 ! Index range 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
soltake: array(ITEMS) of real ! Solution values
end-declarations
initializations from 'dotnetraw:'
VALUE as VDATA WEIGHT as WDATA
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
maximize(MaxVal) ! Solve the MIP-problem
! Output solution to calling application
forall(i in ITEMS) soltake(i):= getsol(take(i))
initializations to 'dotnetraw:'
soltake as SOL
end-initializations
end-model
|
|
burglar11.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar11.mos
``````````````````
Same as burglari.mos, with some scalars
input from/output to calling application.
*** Not intended to be run standalone - run from ugioscalar.* ***
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, Mar. 2008, rev. Apr. 2013
*******************************************************!)
model Burglar11
uses "mmxprs"
parameters
NUM = '' ! Location for no. of items output
SOLVAL = '' ! Location for objective value output
WMAX = '' ! Maximum weight allowed
IODRV = 'jraw:'
end-parameters
declarations
WTMAX: integer ! 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
end-declarations
VALUE :: (["camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"])[15,100,90,60,40,15,10,1]
WEIGHT:: (["camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"])[2,20,20,30,40,30,60,10]
initializations from IODRV
WTMAX as WMAX
end-initializations
declarations
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
! 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
maximize(MaxVal) ! Solve the MIP-problem
! Print out the solution
writeln("Solution:")
forall(i in ITEMS) writeln(" take(", i, "): ", getsol(take(i)))
! Output solution to calling application
initializations to IODRV
evaluation of getobjval as SOLVAL
evaluation of round(sum(i in ITEMS) getsol(take(i))) as NUM
end-initializations
end-model
|
|
burglar9d.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar9d.mos
```````````````````
Same as burglar2.mos, with input from/output to
calling .NET application.
*** Not intended to be run standalone - run from ugiosparse.cs ***
(c) 2012 Fair Isaac Corporation
author: S. Heipcke, Oct. 2012
*******************************************************!)
model Burglar9d
uses "mmxprs"
parameters
DATA = '' ! Location of input data
SOL = '' ! Location for solution data output
WTMAX = 102 ! Maximum weight allowed
end-parameters
declarations
ITEMS: set of string ! Index set for items
VALUE: array(ITEMS) of real ! Value of items
WEIGHT: array(ITEMS) of real ! Weight of items
soltake: array(ITEMS) of real ! Solution values
end-declarations
initializations from 'dotnetraw:'
[VALUE,WEIGHT] as DATA
end-initializations
declarations
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
! 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
maximize(MaxVal) ! Solve the MIP-problem
! Output solution to calling application
forall(i in ITEMS) soltake(i):= getsol(take(i))
initializations to 'dotnetraw:'
soltake as SOL
end-initializations
end-model
|
|