ugcomp.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcomp.java
````````````````
Compiling a model into a BIM file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2002
********************************************************/
import com.dashoptimization.*;
public class ugcomp
{
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
mosel = new XPRM(); // Initialize Mosel
System.out.println("Compiling `burglar2'");
mosel.compile("burglar2.mos"); // Compile the model burglar2.mos,
// output the file burglar2.bim
}
}
|
|
ugcomptmp.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcomptmp.java
```````````````````
Compiling a model into a BIM file saved in Mosel's
temporary directory, then load and run it.
(c) 2013 Fair Isaac Corporation
author: S. Heipcke, Apr. 2013
********************************************************/
import com.dashoptimization.*;
public class ugcomptmp
{
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
mosel = new XPRM(); // Initialize Mosel
System.out.println("Compiling `burglar2'");
mosel.compile("", "burglar2.mos", "tmp:burglar2.bim", "");
System.out.println("Loading `burglar2'");
mod = mosel.loadModel("tmp:burglar2.bim");
System.out.println("Executing `burglar2'");
mod.run();
System.out.println("`burglar2' returned: " + mod.getResult());
}
}
|
|
ugrun.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugrun.java
```````````````
Compiling a model into a BIM file,
then load and run it.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2002
********************************************************/
import com.dashoptimization.*;
public class ugrun
{
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
mosel = new XPRM(); // Initialize Mosel
System.out.println("Compiling `burglar2'");
mosel.compile("burglar2.mos");
System.out.println("Loading `burglar2'");
mod = mosel.loadModel("burglar2.bim");
System.out.println("Executing `burglar2'");
mod.run();
System.out.println("`burglar2' returned: " + mod.getResult());
}
}
|
|
ugdefstream.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugdefstream.java
```````````````````
Redirecting model output.
(c) 2013 Fair Isaac Corporation
author: S. Heipcke, Apr. 2013
********************************************************/
import com.dashoptimization.*;
public class ugdefstream
{
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
mosel = new XPRM(); // Initialize Mosel
System.out.println("Compiling `burglar2'");
mosel.compile("burglar2.mos");
System.out.println("Loading `burglar2'");
mod = mosel.loadModel("burglar2.bim");
// Uncomment one of the following:
// Disable model output
mod.setDefaultStream(XPRM.F_OUTPUT, "null:");
// Double model output (file+stdout)
// mod.setDefaultStream(XPRM.F_OUTPUT, "tee:burgres.txt&");
System.out.println("Executing `burglar2'");
mod.run();
System.out.println("`burglar2' returned: " + mod.getResult());
}
}
|
|
ugarray.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugarray.java
`````````````````
Accessing modeling objects (sparse arrays).
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2004
********************************************************/
import com.dashoptimization.*;
public class ugarray
{
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
mosel.compile("transport.mos"); // Compile, load & run the model
mod = mosel.loadModel("transport.bim");
mod.run();
varr=(XPRMArray)mod.findIdentifier("flow"); // Get model object 'flow'
// it must be an array
dim = varr.getDimension(); // Get the number of dimensions
// of the array
sets = varr.getIndexSets(); // Get the indexing sets
indices = varr.getFirstTEIndex(); // Get the first true entry index
do
{
System.out.print("flow(");
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(); // Reset the model
}
}
|
|
ugcb.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcb.java
``````````````
Retrieve model output via callback-style functionality.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2006
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class ugcb
{
// OutputStream class to handle default output
public static class MyOut extends OutputStream
{
public void flush()
{ System.out.flush(); }
public void write(byte[] b)
{
System.out.print("Mosel: ");
System.out.write(b, 0, b.length);
}
// The following methods are not used by Mosel:
public void write(byte[] b, int off, int len) {}
public void write(int b) {}
public void close() {}
}
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
MyOut cbmsg = new MyOut(); // Define output stream as "MyOut"
mosel = new XPRM(); // Initialize Mosel
// Redirect error stream to default output
mosel.setDefaultStream(XPRM.F_ERROR, "java:java.lang.System.out");
mosel.bind("mycb", cbmsg); // Associate Java object with a name in Mosel
// Set default output stream to cbmsg
mosel.setDefaultStream(XPRM.F_OUTPUT|XPRM.F_LINBUF, "java:mycb");
mosel.compile("burglar2.mos"); // Compile, load & run the model
mod = mosel.loadModel("burglar2.bim");
mod.run();
}
}
|
|
ugsol.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugsol.java
```````````````
Accessing modeling objects and solution information.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2004, rev. Apr. 2013
********************************************************/
import com.dashoptimization.*;
public class ugsol
{
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
XPRMArray varr, darr;
XPRMMPVar x;
XPRMSet set;
int[] indices;
double val;
mosel = new XPRM(); // Initialize Mosel
mosel.compile("burglar3.mos"); // Compile, load & run the model
mod = mosel.loadModel("burglar3.bim");
mod.run();
if(mod.getProblemStatus()!=mod.PB_OPTIMAL)
System.exit(1); // Stop if no solution found
System.out.println("Objective value: " + mod.getObjectiveValue());
// Print the objective function value
varr=(XPRMArray)mod.findIdentifier("take"); // Get model object 'take',
// it must be an array
darr=(XPRMArray)mod.findIdentifier("VALUE"); // Get model object 'VALUE',
// it must be an array
set=(XPRMSet)mod.findIdentifier("ITEMS"); // Get model object 'ITEMS',
// it must be a set
indices = varr.getFirstIndex(); // Get the first entry of array varr
// (we know that the array is dense)
do
{
x = varr.get(indices).asMPVar(); // Get a variable from varr
val = darr.getAsReal(indices); // Get the corresponding value
// Alternative:
// System.out.println("take(" + varr.getIndexSets()[0].get(indices[0]) + "): " +
System.out.println("take(" + set.get(indices[0]) + "): " +
x.getSolution() + "\t (item value: " + val + ")");
// Print the solution value
} while(varr.nextIndex(indices)); // Get the next index
mod.reset(); // Reset the model
}
}
|
|
ugparam.java |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugparam.java
`````````````````
Passing parameters to a Mosel program.
Accessing modeling objects (sets).
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2002
********************************************************/
import com.dashoptimization.*;
public class ugparam
{
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
XPRMSet set;
int LIM=500, first, last;
mosel = new XPRM(); // Initialize Mosel
System.out.println("Compiling `prime'");
mosel.compile("prime.mos");
System.out.println("Loading `prime'");
mod = mosel.loadModel("prime.bim");
System.out.println("Executing `prime'");
mod.execParams = "LIMIT=" + LIM;
mod.run();
System.out.println("`prime' returned: " + mod.getResult());
set=(XPRMSet)mod.findIdentifier("SPrime"); // Get the object 'SPrime'
// 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("Prime numbers from 2 to " + LIM);
for(int i=first;i<=last;i++) // Print all set elements
System.out.print(" " + set.getAsInteger(i) + ",");
System.out.println();
}
}
}
|
|
burglar2.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar2.mos
`````````````````
Use of index sets.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001, rev. 2006
*******************************************************!)
model Burglar2
uses "mmxprs"
declarations
WTMAX = 102 ! 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
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
initializations from 'burglar.dat'
VALUE WEIGHT
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
! Print out the solution
writeln("Solution:\n Objective: ", getobjval)
forall(i in ITEMS) writeln(" take(", i, "): ", getsol(take(i)))
end-model
|
|
transport.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file transport.mos
``````````````````
Using dynamic arrays.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2001, rev. Dec 2009
*******************************************************!)
model Transport
uses "mmxprs"
declarations
REGION: set of string ! Set of customer regions
PLANT: set of string ! Set of plants
DEMAND: array(REGION) of real ! Demand at regions
PLANTCAP: array(PLANT) of real ! Production capacity at plants
PLANTCOST: array(PLANT) of real ! Unit production cost at plants
TRANSCAP: dynamic array(PLANT,REGION) of real
! Capacity on each route plant->region
DISTANCE: dynamic array(PLANT,REGION) of real
! Distance of each route plant->region
FUELCOST: real ! Fuel cost per unit distance
flow: dynamic array(PLANT,REGION) of mpvar ! Flow on each route
end-declarations
initializations from 'transprt.dat'
DEMAND
[PLANTCAP,PLANTCOST] as 'PLANTDATA'
[DISTANCE,TRANSCAP] as 'ROUTES'
FUELCOST
end-initializations
! Create the flow variables that exist
forall(p in PLANT, r in REGION | exists(TRANSCAP(p,r)) ) create(flow(p,r))
! Objective: minimize total cost
MinCost:= sum(p in PLANT, r in REGION | exists(flow(p,r)))
(FUELCOST * DISTANCE(p,r) + PLANTCOST(p)) * flow(p,r)
! Limits on plant capacity
forall(p in PLANT) sum(r in REGION) flow(p,r) <= PLANTCAP(p)
! Satisfy all demands
forall(r in REGION) sum(p in PLANT) flow(p,r) = DEMAND(r)
! Bounds on flows
forall(p in PLANT, r in REGION | exists(flow(p,r)))
flow(p,r) <= TRANSCAP(p,r)
minimize(MinCost) ! Solve the problem
end-model
|
|
prime.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file prime.mos
``````````````
Working with sets.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001
*******************************************************!)
model Prime
parameters
LIMIT=100 ! Search for prime numbers in 2..LIMIT
end-parameters
declarations
SNumbers: set of integer ! Set of numbers to be checked
SPrime: set of integer ! Set of prime numbers
end-declarations
SNumbers:={2..LIMIT}
writeln("Prime numbers between 2 and ", LIMIT, ":")
n:=2
repeat
while (not(n in SNumbers)) n+=1
SPrime += {n} ! n is a prime number
i:=n
while (i<=LIMIT) do ! Remove n and all its multiples
SNumbers-= {i}
i+=n
end-do
until SNumbers={}
writeln(SPrime)
writeln(" (", getsize(SPrime), " prime numbers.)")
end-model
|
|
burglar3.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar3.mos
`````````````````
Same as burglar2.mos but no output printing.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001, rev. 2006
*******************************************************!)
model Burglar3
uses "mmxprs"
declarations
WTMAX = 102 ! 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
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
initializations from 'burglar.dat'
VALUE WEIGHT
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
end-model
|
|