foliomemio.mos |
(!******************************************************
Mosel Example Problems
======================
file foliomemio.mos
```````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.mos with
simplified data structures (replaced arrays of structured types).
-- Data input/output in memory --
Run modes for this model:
1. Stand-alone: data I/O to text files
(run this model from Workbench or from Mosel command line)
2. Submodel to another Mosel model: data exchange in memory
(run model 'runfolio.mos' or 'runfoliopar.mos' to execute this model)
3a. C: I/O to C program runfolio.c or runfoliod.c
(compile and run C program 'runfolio.c' or 'runfoliod.c')
3b. Java: I/O to Java program runfolio.java, runfoliob.java, or
runfoliod.java
(compile and run Java program 'runfolio.java', 'runfoliob.java',
or 'runfoliod.java')
3c. (Windows only) C#: I/O to C# program runfolio.cs or runfoliob.cs
(compile and run C# program 'runfolio.cs' or 'runfoliob.cs')
4. Remote execution from another Mosel model:
(run model 'runfoliodistr.mos' or 'runfoliopardistr.mos' to execute
this model)
5a. Remote execution from C: I/O to C program distfolio.c or distfoliopar.c
(compile and run C program 'distfolio.c' or 'distfoliopar.c')
5b. Remote execution from Java: I/O to Java program distfolio.java or
distfoliopar.java
(compile and run Java program 'distfolio.java' or 'distfoliopar.java')
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, Jan. 2009, rev. Sep. 2018
*******************************************************!)
model "Portfolio optimization with MIP"
uses "mmxprs"
parameters
MAXRISK = 1/3 ! Max. investment into high-risk values
MINREG = 0.2 ! Min. investment per geogr. region
MAXREG = 0.5 ! Max. investment per geogr. region
MAXSEC = 0.25 ! Max. investment per ind. sector
MAXVAL = 0.2 ! Max. investment per share
MINVAL = 0.1 ! Min. investment per share
MAXNUM = 15 ! Max. number of different assets
DATAFILE = "folio10.dat" ! File with problem data
OUTPUTFILE = "sol10out.dat" ! File for solution output
RISKDATA = "RISK" ! Locations of input data
RETDATA = "RET"
LOCDATA = "LOCTAB"
SECDATA = "SECTAB"
FRACSOL = "FRAC" ! Locations for solution output
BUYSOL = "BUY"
NUMSHARES = "NUMSHARES"
RETSOL = "RETSOL"
SOLSTATUS = "SOLSTATUS"
end-parameters
declarations
SHARES,S: set of string ! Set of shares
RISK: set of string ! Set of high-risk values among shares
REGIONS: set of string ! Geographical regions
TYPES: set of string ! Share types (ind. sectors)
LOCTAB: dynamic array(REGIONS,SHARES) of boolean ! Shares per geogr. region
RET: array(SHARES) of real ! Estimated return in investment
SECTAB: dynamic array(TYPES,SHARES) of boolean ! Shares per industry sector
end-declarations
initializations from DATAFILE
RISK as RISKDATA
RET as RETDATA
LOCTAB as LOCDATA
SECTAB as SECDATA
end-initializations
declarations
frac: array(SHARES) of mpvar ! Fraction of capital used per share
buy: array(SHARES) of mpvar ! 1 if asset is in portfolio, 0 otherwise
end-declarations
! Objective: total return
Return:= sum(s in SHARES) RET(s)*frac(s)
! Limit the percentage of high-risk values
sum(s in RISK) frac(s) <= MAXRISK
! Limits on geographical distribution
forall(r in REGIONS) do
sum(s in SHARES | exists(LOCTAB(r,s))) frac(s) >= MINREG
sum(s in SHARES | exists(LOCTAB(r,s))) frac(s) <= MAXREG
end-do
! Diversification across industry sectors
forall(t in TYPES) sum(s in SHARES | exists(SECTAB(t,s))) frac(s) <= MAXSEC
! Spend all the capital
sum(s in SHARES) frac(s) = 1
! Upper bounds on the investment per share
forall(s in SHARES) frac(s) <= MAXVAL
! Limit the total number of assets
sum(s in SHARES) buy(s) <= MAXNUM
forall(s in SHARES) do
buy(s) is_binary ! Turn variables into binaries
frac(s) <= MAXVAL*buy(s) ! Linking the variables
frac(s) >= MINVAL*buy(s) ! Linking the variables
end-do
! Display Optimizer log
setparam("XPRS_verbose", true)
! Solve the problem
maximize(Return)
! Adapt Mosel comparison tolerance to Optimizer feasibility tolerance
setparam("zerotol", getparam("XPRS_feastol")/10)
! Solution output
function getvalues(v: array(SHARES) of mpvar): dynamic array(S) of real
forall(s in SHARES | v(s).sol<>0) returned(s):= v(s).sol
end-function
initializations to OUTPUTFILE
evaluation of Return.sol as RETSOL
evaluation of sum(s in SHARES | buy(s).sol<>0) 1 as NUMSHARES
evaluation of getvalues(frac) as FRACSOL
evaluation of getvalues(buy) as BUYSOL
evaluation of getprobstat as SOLSTATUS
end-initializations
end-model
|
|
foliomemio2.mos |
(!******************************************************
Mosel Example Problems
======================
file foliomemio2.mos
````````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.mos with
simplified data structures (replaced arrays of structured types).
-- Data input/output in memory --
-- Grouping arrays with identical index sets --
Run modes for this model:
1. Stand-alone: data I/O to text files
(run this model from Workbench or from Mosel command line)
2. Submodel to another Mosel model: data exchange in memory
(run model 'runfolio2.mos' to execute this model)
3a. C: I/O to C program folio2.c
(compile and run C program 'folio2.c')
3b. Java: I/O to Java program folio2.java
(compile and run Java program 'folio2.java')
3c. C# (Windows only): I/O to C# program folio2.cs
(compile and run C# program 'folio2.cs')
(c) 2009 Fair Isaac Corporation
author: S.Heipcke, Feb. 2009, rev. Sep. 2018
*******************************************************!)
model "Portfolio optimization with MIP"
uses "mmxprs"
parameters
MAXRISK = 1/3 ! Max. investment into high-risk values
MINREG = 0.2 ! Min. investment per geogr. region
MAXREG = 0.5 ! Max. investment per geogr. region
MAXSEC = 0.25 ! Max. investment per ind. sector
MAXVAL = 0.2 ! Max. investment per share
MINVAL = 0.1 ! Min. investment per share
MAXNUM = 15 ! Max. number of different assets
DATAFILE = "folio10.dat" ! File with problem data
OUTPUTFILE = "sol10out.dat" ! File for solution output
RISKDATA = "RISK" ! Locations of input data
RETDATA = "RET"
LOCDATA = "LOCTAB"
SECDATA = "SECTAB"
FRACBUYSOL = "FRACBUY" ! Locations for solution output
NUMSHARES = "NUMSHARES"
RETSOL = "RETSOL"
SOLSTATUS = "SOLSTATUS"
end-parameters
declarations
SHARES,S: set of string ! Set of shares
RISK: set of string ! Set of high-risk values among shares
REGIONS: set of string ! Geographical regions
TYPES: set of string ! Share types (ind. sectors)
LOCTAB: dynamic array(REGIONS,SHARES) of boolean ! Shares per geogr. region
RET: array(SHARES) of real ! Estimated return in investment
SECTAB: dynamic array(TYPES,SHARES) of boolean ! Shares per industry sector
end-declarations
initializations from DATAFILE
RISK as RISKDATA
RET as RETDATA
LOCTAB as LOCDATA
SECTAB as SECDATA
end-initializations
declarations
frac: array(SHARES) of mpvar ! Fraction of capital used per share
buy: array(SHARES) of mpvar ! 1 if asset is in portfolio, 0 otherwise
end-declarations
! Objective: total return
Return:= sum(s in SHARES) RET(s)*frac(s)
! Limit the percentage of high-risk values
sum(s in RISK) frac(s) <= MAXRISK
! Limits on geographical distribution
forall(r in REGIONS) do
sum(s in SHARES | exists(LOCTAB(r,s))) frac(s) >= MINREG
sum(s in SHARES | exists(LOCTAB(r,s))) frac(s) <= MAXREG
end-do
! Diversification across industry sectors
forall(t in TYPES) sum(s in SHARES | exists(SECTAB(t,s))) frac(s) <= MAXSEC
! Spend all the capital
sum(s in SHARES) frac(s) = 1
! Upper bounds on the investment per share
forall(s in SHARES) frac(s) <= MAXVAL
! Limit the total number of assets
sum(s in SHARES) buy(s) <= MAXNUM
forall(s in SHARES) do
buy(s) is_binary ! Turn variables into binaries
frac(s) <= MAXVAL*buy(s) ! Linking the variables
frac(s) >= MINVAL*buy(s) ! Linking the variables
end-do
! Display Optimizer log
setparam("XPRS_verbose", true)
! Solve the problem
maximize(Return)
! Adapt Mosel comparison tolerance to Optimizer feasibility tolerance
setparam("zerotol", getparam("XPRS_feastol")/10)
! Solution output
function getvalues(v: array(SHARES) of mpvar): dynamic array(S) of real
forall(s in SHARES | v(s).sol<>0) returned(s):= v(s).sol
end-function
initializations to OUTPUTFILE
evaluation of Return.sol as RETSOL
evaluation of sum(s in SHARES | buy(s).sol<>0) 1 as NUMSHARES
[evaluation of getvalues(frac) , evaluation of getvalues(buy)] as FRACBUYSOL
evaluation of getprobstat as SOLSTATUS
end-initializations
end-model
|
|
foliocbio.mos |
(!******************************************************
Mosel Example Problems
======================
file foliocbio.mos
``````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.mos.
-- Defining an integer solution callback
to write out solution information to external program --
*** This model cannot be run with a Community Licence
for the provided data instance ***
(c) 2011 Fair Isaac Corporation
author: S.Heipcke, July 2011, rev. Sep. 2018
*******************************************************!)
model "Portfolio optimization with MIP"
uses "mmxprs"
uses "mmjobs"
parameters
MAXRISK = 1/3 ! Max. investment into high-risk values
MINREG = 0.2 ! Min. investment per geogr. region
MAXREG = 0.5 ! Max. investment per geogr. region
MAXSEC = 0.25 ! Max. investment per ind. sector
MAXVAL = 0.2 ! Max. investment per share
MINVAL = 0.1 ! Min. investment per share
MAXNUM = 15 ! Max. number of different assets
DATAFILE = "folio250.dat" ! File with problem data
OUTPUTFILE = "sol10out.dat" ! File for solution output
FRACSOL = "FRAC" ! Locations for solution output
BUYSOL = "BUY"
NUMSHARES = "NUMSHARES"
RETSOL = "RETSOL"
SOLCOUNT = "SOLCOUNT"
end-parameters
forward public procedure printsol
declarations
SHARES,S: set of string ! Set of shares
RISK: set of string ! Set of high-risk values among shares
REGIONS: set of string ! Geographical regions
TYPES: set of string ! Share types (ind. sectors)
LOC: array(REGIONS) of set of string ! Sets of shares per geogr. region
RET: array(SHARES) of real ! Estimated return in investment
SEC: array(TYPES) of set of string ! Sets of shares per industry sector
end-declarations
initializations from DATAFILE
RISK RET LOC SEC
end-initializations
declarations
frac: array(SHARES) of mpvar ! Fraction of capital used per share
buy: array(SHARES) of mpvar ! 1 if asset is in portfolio, 0 otherwise
end-declarations
! Objective: total return
Return:= sum(s in SHARES) RET(s)*frac(s)
! Limit the percentage of high-risk values
sum(s in RISK) frac(s) <= MAXRISK
! Limits on geographical distribution
forall(r in REGIONS) do
sum(s in LOC(r)) frac(s) >= MINREG
sum(s in LOC(r)) frac(s) <= MAXREG
end-do
! Diversification across industry sectors
forall(t in TYPES) sum(s in SEC(t)) frac(s) <= MAXSEC
! Spend all the capital
sum(s in SHARES) frac(s) = 1
! Upper bounds on the investment per share
forall(s in SHARES) frac(s) <= MAXVAL
! Limit the total number of assets
sum(s in SHARES) buy(s) <= MAXNUM
forall(s in SHARES) do
buy(s) is_binary ! Turn variables into binaries
frac(s) <= MAXVAL*buy(s) ! Linking the variables
frac(s) >= MINVAL*buy(s) ! Linking the variables
end-do
! Display Optimizer log
setparam("XPRS_verbose", true)
! Adapt Mosel comparison tolerance to Optimizer feasibility tolerance
setparam("zerotol", getparam("XPRS_feastol")/10)
! Set a MIP solution callback
setcallback(XPRS_CB_INTSOL, "printsol")
! Solve the problem
maximize(Return)
if getprobstat <> XPRS_OPT then exit(1); end-if
!******** Solution output********
!**** Auxiliary function creating an array of solution values ****
function getvalues(v: array(SHARES) of mpvar): dynamic array(S) of real
forall(s in SHARES | v(s).sol<>0) returned(s):= v(s).sol
end-function
!**** Definition of the MIP solution callback function ****
public procedure printsol
initializations to OUTPUTFILE
evaluation of getparam("XPRS_MIPSOLS") as SOLCOUNT
evaluation of sum(s in SHARES | buy(s).sol<>0) 1 as NUMSHARES
evaluation of getsol(Return) as RETSOL
evaluation of getvalues(frac) as FRACSOL
evaluation of getvalues(buy) as BUYSOL
end-initializations
end-procedure
end-model
|
|
foliorun.c |
/********************************************************
Mosel Library Example Problems
==============================
file foliorun.c
```````````````
Loading and running a BIM file.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Aug. 2003
********************************************************/
#include <stdio.h>
#include "xprm_rt.h"
int main(int argc, char *argv[])
{
XPRMmodel model;
int result;
XPRMinit(); /* Initialize Mosel */
model = XPRMloadmod("foliodata.bim", NULL); /* Load compiled model */
XPRMrunmod(model, &result, NULL);
XPRMunloadmod(model);
return 0;
}
|
|
runfolio2.c |
/*******************************************************
Mosel Example Problems
======================
file runfolio2.c
````````````````
Running a Mosel model from a C application
with data exchange between model and host application.
(Sparse data format using string indices)
-- Grouping arrays with identical index sets --
(c) 2009 Fair Isaac Corporation
author: S. Heipcke, Feb. 2009, rev. Feb. 2017
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
#define maxnum 15
/* Arrays to store initial values for data */
struct MyArrayTwo
{
const char *ind1,*ind2; /* index names */
int val; /* data value */
};
struct MyArrayOne
{
const char *ind; /* index name */
double val; /* data value */
};
/* Array to receive solution values */
struct MySolArray
{
const char *ind; /* index name */
double val1,val2; /* solution values */
};
struct MySolArray sol[maxnum];
char* riskset[] = {"hardware1", "hardware2", "hardware3", "hardware4",
"hardware5", "hardware6", "hardware7", "hardware8", "hardware9",
"hardware10", "theater1", "theater2", "theater3", "theater4", "theater5",
"theater6", "theater7", "theater8", "theater9", "theater10", "telecom1",
"telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7",
"telecom8", "telecom9", "telecom10", "software1", "software2", "software3",
"software4", "software5", "software6", "software7", "software8",
"software9", "software10", "electronics1", "electronics2", "electronics3",
"electronics4", "electronics5", "electronics6", "electronics7",
"electronics8", "electronics9", "electronics10"};
struct MyArrayTwo locdata[] = {{"EU","treasury1",1},
{"EU","treasury10",1}, {"EU","hardware7",1},
{"EU","hardware9",1}, {"EU","theater8",1},
{"EU","theater10",1}, {"EU","telecom3",1},
{"EU","telecom9",1}, {"EU","telecom10",1},
{"EU","brewery1",1}, {"EU","brewery4",1},
{"EU","brewery6",1}, {"EU","brewery7",1},
{"EU","brewery8",1}, {"EU","highways2",1},
{"EU","highways5",1}, {"EU","highways7",1},
{"EU","highways10",1}, {"EU","cars4",1},
{"EU","cars7",1}, {"EU","cars8",1}, {"EU","cars9",1}, {"EU","cars10",1},
{"EU","bank1",1}, {"EU","bank3",1}, {"EU","bank5",1}, {"EU","bank6",1},
{"EU","bank7",1}, {"EU","bank10",1},
{"EU","software3",1}, {"EU","software4",1},
{"EU","software5",1}, {"EU","software6",1},
{"EU","electronics2",1}, {"NA","treasury2",1},
{"NA","treasury6",1}, {"NA","treasury7",1},
{"NA","treasury8",1}, {"NA","hardware4",1},
{"NA","hardware6",1}, {"NA","theater1",1},
{"NA","theater3",1}, {"NA","theater7",1},
{"NA","theater9",1}, {"NA","telecom2",1},
{"NA","telecom4",1}, {"NA","telecom8",1},
{"NA","highways3",1}, {"NA","highways4",1},
{"NA","cars5",1}, {"NA","cars6",1},
{"NA","bank2",1}, {"NA","bank4",1},{"NA","bank8",1}, {"NA","bank9",1},
{"NA","software1",1}, {"NA","software2",1},
{"NA","software9",1}, {"NA","electronics3",1},
{"NA","electronics5",1}, {"NA","electronics6",1},
{"NA","electronics8",1}, {"NA","electronics9",1},
{"NA","electronics10",1}, {"APAC","treasury4",1},
{"APAC","treasury5",1}, {"APAC","treasury9",1},
{"APAC","hardware1",1}, {"APAC","hardware2",1},
{"APAC","hardware5",1}, {"APAC","hardware8",1},
{"APAC","hardware10",1}, {"APAC","theater2",1},
{"APAC","theater4",1}, {"APAC","telecom1",1},
{"APAC","telecom5",1}, {"APAC","telecom6",1},
{"APAC","telecom7",1}, {"APAC","brewery9",1},
{"APAC","brewery10",1}, {"APAC","highways8",1},
{"APAC","cars1",1}, {"APAC","cars2",1},
{"APAC","software7",1}, {"APAC","software8",1},
{"APAC","software10",1},{"APAC","electronics1",1},
{"APAC","electronics4",1}, {"APAC","electronics7",1}};
struct MyArrayTwo secdata[] = {{"bonds","treasury1",1},
{"bonds","treasury10",1}, {"bonds","treasury2",1},
{"bonds","treasury6",1}, {"bonds","treasury7",1},
{"bonds","treasury8",1}, {"bonds","treasury4",1},
{"bonds","treasury5",1}, {"bonds","treasury9",1}, {"bonds","treasury3",1},
{"technology","hardware7",1}, {"technology","hardware9",1},
{"technology","software3",1}, {"technology","software4",1},
{"technology","software5",1}, {"technology","software6",1},
{"technology","electronics2",1}, {"technology","hardware4",1},
{"technology","hardware6",1}, {"technology","software1",1},
{"technology","software2",1}, {"technology","software9",1},
{"technology","electronics3",1}, {"technology","electronics5",1},
{"technology","electronics6",1}, {"technology","electronics8",1},
{"technology","electronics9",1}, {"technology","electronics10",1},
{"technology","hardware1",1}, {"technology","hardware2",1},
{"technology","hardware5",1}, {"technology","hardware8",1},
{"technology","hardware10",1}, {"technology","software7",1},
{"technology","software8",1}, {"technology","software10",1},
{"technology","electronics1",1}, {"technology","electronics4",1},
{"technology","electronics7",1}, {"technology","hardware3",1},
{"entertainment","theater8",1}, {"entertainment","theater10",1},
{"entertainment","theater1",1}, {"entertainment","theater3",1},
{"entertainment","theater7",1}, {"entertainment","theater9",1},
{"entertainment","theater2",1}, {"entertainment","theater4",1},
{"entertainment","theater5",1}, {"entertainment","theater6",1},
{"telecom","telecom3",1}, {"telecom","telecom9",1},
{"telecom","telecom10",1}, {"telecom","telecom2",1},
{"telecom","telecom4",1}, {"telecom","telecom8",1},
{"telecom","telecom1",1}, {"telecom","telecom5",1},
{"telecom","telecom6",1}, {"telecom","telecom7",1},
{"food","brewery1",1}, {"food","brewery4",1},
{"food","brewery6",1}, {"food","brewery7",1},
{"food","brewery8",1}, {"food","brewery9",1},
{"food","brewery10",1}, {"food","brewery2",1},
{"food","brewery3",1}, {"food","brewery5",1},
{"construction","highways2",1}, {"construction","highways5",1},
{"construction","highways7",1}, {"construction","highways10",1},
{"construction","highways3",1}, {"construction","highways4",1},
{"construction","highways8",1}, {"construction","highways1",1},
{"construction","highways6",1}, {"construction","highways9",1},
{"manufacturing","cars4",1}, {"manufacturing","cars7",1},
{"manufacturing","cars8",1}, {"manufacturing","cars9",1},
{"manufacturing","cars10",1}, {"manufacturing","cars5",1},
{"manufacturing","cars6",1}, {"manufacturing","cars1",1},
{"manufacturing","cars2",1}, {"manufacturing","cars3",1},
{"finance","bank1",1}, {"finance","bank3",1},
{"finance","bank5",1}, {"finance","bank6",1},
{"finance","bank7",1}, {"finance","bank10",1},
{"finance","bank2",1}, {"finance","bank4",1},
{"finance","bank8",1}, {"finance","bank9",1}};
struct MyArrayOne retdata[] = {{"treasury1",5.29},
{"treasury2",2.8}, {"treasury3",3.59},
{"treasury4",6.97}, {"treasury5",3.3},
{"treasury6",3.02}, {"treasury7",2.98},
{"treasury8",3.41}, {"treasury9",3.93},
{"treasury10",3.86}, {"hardware1",11.36},
{"hardware2",17.23}, {"hardware3",16.31},
{"hardware4",21.1}, {"hardware5",13.76},
{"hardware6",9.31}, {"hardware7",16.99},
{"hardware8",24.85}, {"hardware9",18.52},
{"hardware10",12.79}, {"theater1",36.1},
{"theater2",19.4}, {"theater3",23.5},
{"theater4",34.61}, {"theater5",16.91},
{"theater6",27.04}, {"theater7",25.82},
{"theater8",24.99}, {"theater9",36.89},
{"theater10",31.71}, {"telecom1",10.89},
{"telecom2",17.98}, {"telecom3",12.31},
{"telecom4",6.53}, {"telecom5",6.11},
{"telecom6",15.89}, {"telecom7",12.46},
{"telecom8",11.11}, {"telecom9",16.6},
{"telecom10",13.93}, {"brewery1",8.79},
{"brewery2",9.35}, {"brewery3",8.88},
{"brewery4",11.63}, {"brewery5",6.21},
{"brewery6",9.26}, {"brewery7",4.77},
{"brewery8",5.98}, {"brewery9",10.92},
{"brewery10",5.26}, {"highways1",5.39},
{"highways2",4.85}, {"highways3",8.34},
{"highways4",8.48}, {"highways5",6.42},
{"highways6",11.04}, {"highways7",13.15},
{"highways8",10.01}, {"highways9",7.71},
{"highways10",11.07}, {"cars1",5.74},
{"cars2",7.13}, {"cars3",8.75}, {"cars4",5.08}, {"cars5",6.23},
{"cars6",8.19}, {"cars7",8.03}, {"cars8",5.96}, {"cars9",4.17},
{"cars10",8.11}, {"bank1",6.79}, {"bank2",3.01}, {"bank3",4.92},
{"bank4",4.14}, {"bank5",8.98}, {"bank6",8.81}, {"bank7",4.7},
{"bank8",8.06}, {"bank9",6.22}, {"bank10",4.47}, {"software1",34.59},
{"software2",44.94}, {"software3",43.05},
{"software4",15.58}, {"software5",42.05},
{"software6",20.66}, {"software7",20.76},
{"software8",19.85}, {"software9",20.05},
{"software10",45.38}, {"electronics1",23.07},
{"electronics2",19.1}, {"electronics3",23.83},
{"electronics4",16.54}, {"electronics5",28.33},
{"electronics6",25.88}, {"electronics7",22.14},
{"electronics8",22.65}, {"electronics9",12.59}, {"electronics10",28.1}};
/********************************************************/
int main()
{
XPRMmodel mod;
char riskdata_name[40], locdata_name[40], secdata_name[40], retdata_name[40];
/* File names of input data */
char solfracbuy_name[40]; /* File names of solution arrays */
char objval_name[40], num_name[40], stat_name[40];
char params[1000]; /* Parameter string for model execution */
int i,result,numshares,status;
double objval;
double maxrisk = 1.0/3; /* Model parameter settings */
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
/* Prepare file names for 'initializations' using the 'raw' driver: */
/* "rawoption[,...],filename" */
/* (Here, 'filename' uses the 'mem' driver, data is stored in memory) */
/* Options for 'raw': */
/* 'slength=0': strings are represented by pointers to null terminated */
/* arrays of characters (C-string) instead of fixed size arrays*/
sprintf(riskdata_name, "slength=0,mem:%p/%d", riskset, (int)sizeof(riskset));
sprintf(locdata_name, "slength=0,mem:%p/%d", locdata, (int)sizeof(locdata));
sprintf(secdata_name, "slength=0,mem:%p/%d", secdata, (int)sizeof(secdata));
sprintf(retdata_name, "slength=0,mem:%p/%d", retdata, (int)sizeof(retdata));
sprintf(solfracbuy_name, "slength=0,mem:%p/%d", sol, (int)sizeof(sol));
sprintf(objval_name, "mem:%p/%d", &objval, (int)sizeof(objval));
sprintf(num_name, "mem:%p/%d", &numshares, (int)sizeof(numshares));
sprintf(stat_name, "mem:%p/%d", &status, (int)sizeof(status));
/* Pass file names as execution param.s */
sprintf(params, "MAXRISK=%g,MINREG=%g,MAXREG=%g,MAXSEC=%g,MAXVAL=%g,MINVAL=%g,MAXNUM=%d,DATAFILE='raw:',OUTPUTFILE='raw:',RISKDATA='%s',LOCDATA='%s',RETDATA='%s',SECDATA='%s',FRACBUYSOL='%s',RETSOL='%s',NUMSHARES='%s',SOLSTATUS='%s'",
maxrisk, minreg, maxreg, maxsec, maxval, minval, maxnum,
riskdata_name, locdata_name, retdata_name, secdata_name,
solfracbuy_name, objval_name, num_name, stat_name);
if(XPRMinit()) /* Initialize Mosel */
return 1;
if(XPRMexecmod(NULL, "foliomemio2.mos", params, &result, &mod))
return 2; /* Execute the model file (only during
development phase, the deployed
application would only use BIM) */
if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT)
return 3; /* Stop if no solution available */
/* Display solution values obtained from the model */
printf("Total return: %g\n", objval);
/* Alternatively:
printf("Total return: %g\n", XPRMgetobjval(mod));
*/
for(i=0;i<numshares;i++)
printf("%s: %g%% (%g)\n", sol[i].ind, sol[i].val1*100, sol[i].val2);
XPRMresetmod(mod); /* Reset the model */
return 0;
}
|
|
runfoliod.c |
/*******************************************************
Mosel Example Problems
======================
file runfoliod.c
````````````````
Running a Mosel model from a C application
with data exchange between model and host application.
(Passing data via callback)
(c) 2009 Fair Isaac Corporation
author: S. Heipcke, Oct. 2009, rev. Feb. 2017
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xprm_mc.h"
#define maxnum 15
char* riskset[] = {"hardware1", "hardware2", "hardware3", "hardware4",
"hardware5", "hardware6", "hardware7", "hardware8", "hardware9",
"hardware10", "theater1", "theater2", "theater3", "theater4", "theater5",
"theater6", "theater7", "theater8", "theater9", "theater10", "telecom1",
"telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7",
"telecom8", "telecom9", "telecom10", "software1", "software2", "software3",
"software4", "software5", "software6", "software7", "software8",
"software9", "software10", "electronics1", "electronics2", "electronics3",
"electronics4", "electronics5", "electronics6", "electronics7",
"electronics8", "electronics9", "electronics10"};
int risksize=50;
char* locdataind1[] = {"EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC"};
char* locdataind2[] = {"treasury1", "treasury10", "hardware7", "hardware9", "theater8", "theater10", "telecom3", "telecom9", "telecom10", "brewery1", "brewery4", "brewery6", "brewery7", "brewery8", "highways2", "highways5", "highways7", "highways10", "cars4", "cars7", "cars8", "cars9", "cars10", "bank1", "bank3", "bank5", "bank6", "bank7", "bank10", "software3", "software4", "software5", "software6", "electronics2", "treasury2", "treasury6", "treasury7", "treasury8", "hardware4", "hardware6", "theater1", "theater3", "theater7", "theater9", "telecom2", "telecom4", "telecom8", "highways3", "highways4", "cars5", "cars6", "bank2", "bank4", "bank8", "bank9", "software1", "software2", "software9", "electronics3", "electronics5", "electronics6", "electronics8", "electronics9", "electronics10", "treasury4", "treasury5", "treasury9", "hardware1", "hardware2", "hardware5", "hardware8", "hardware10", "theater2", "theater4", "telecom1", "telecom5", "telecom6", "telecom7", "brewery9", "brewery10", "highways8", "cars1", "cars2", "software7", "software8", "software10", "electronics1", "electronics4", "electronics7"};
int locdata[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int locsize=89;
char* secdataind1[] = {"bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "food", "food", "food", "food", "food", "food", "food", "food", "food", "food", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "finance", "finance", "finance", "finance", "finance", "finance", "finance", "finance", "finance", "finance"};
char* secdataind2[] = {"treasury1", "treasury2", "treasury3", "treasury4", "treasury5", "treasury6", "treasury7", "treasury8", "treasury9", "treasury10", "hardware1", "hardware2", "hardware3", "hardware4", "hardware5", "hardware6", "hardware7", "hardware8", "hardware9", "hardware10", "software1", "software2", "software3", "software4", "software5", "software6", "software7", "software8", "software9", "software10", "electronics1", "electronics2", "electronics3", "electronics4", "electronics5", "electronics6", "electronics7", "electronics8", "electronics9", "electronics10", "theater1", "theater2", "theater3", "theater4", "theater5", "theater6", "theater7", "theater8", "theater9", "theater10", "telecom1", "telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7", "telecom8", "telecom9", "telecom10", "brewery1", "brewery2", "brewery3", "brewery4", "brewery5", "brewery6", "brewery7", "brewery8", "brewery9", "brewery10", "highways1", "highways2", "highways3", "highways4", "highways5", "highways6", "highways7", "highways8", "highways9", "highways10", "cars1", "cars2", "cars3", "cars4", "cars5", "cars6", "cars7", "cars8", "cars9", "cars10", "bank1", "bank2", "bank3", "bank4", "bank5", "bank6", "bank7", "bank8", "bank9", "bank10"};
int secdata[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int secsize=100;
char* retdataind[] = {"treasury1", "treasury2", "treasury3", "treasury4", "treasury5", "treasury6", "treasury7", "treasury8", "treasury9", "treasury10", "hardware1", "hardware2", "hardware3", "hardware4", "hardware5", "hardware6", "hardware7", "hardware8", "hardware9", "hardware10", "theater1", "theater2", "theater3", "theater4", "theater5", "theater6", "theater7", "theater8", "theater9", "theater10", "telecom1", "telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7", "telecom8", "telecom9", "telecom10", "brewery1", "brewery2", "brewery3", "brewery4", "brewery5", "brewery6", "brewery7", "brewery8", "brewery9", "brewery10", "highways1", "highways2", "highways3", "highways4", "highways5", "highways6", "highways7", "highways8", "highways9", "highways10", "cars1", "cars2", "cars3", "cars4", "cars5", "cars6", "cars7", "cars8", "cars9", "cars10", "bank1", "bank2", "bank3", "bank4", "bank5", "bank6", "bank7", "bank8", "bank9", "bank10", "software1", "software2", "software3", "software4", "software5", "software6", "software7", "software8", "software9", "software10", "electronics1", "electronics2", "electronics3", "electronics4", "electronics5", "electronics6", "electronics7", "electronics8", "electronics9", "electronics10"};
double retdata[] = {5.29, 2.8, 3.59, 6.97, 3.3, 3.02, 2.98, 3.41, 3.93, 3.86, 11.36, 17.23, 16.31, 21.1, 13.76, 9.31, 16.99, 24.85, 18.52, 12.79, 36.1, 19.4, 23.5, 34.61, 16.91, 27.04, 25.82, 24.99, 36.89, 31.71, 10.89, 17.98, 12.31, 6.53, 6.11, 15.89, 12.46, 11.11, 16.6, 13.93, 8.79, 9.35, 8.88, 11.63, 6.21, 9.26, 4.77, 5.98, 10.92, 5.26, 5.39, 4.85, 8.34, 8.48, 6.42, 11.04, 13.15, 10.01, 7.71, 11.07, 5.74, 7.13, 8.75, 5.08, 6.23, 8.19, 8.03, 5.96, 4.17, 8.11, 6.79, 3.01, 4.92, 4.14, 8.98, 8.81, 4.7, 8.06, 6.22, 4.47, 34.59, 44.94, 43.05, 15.58, 42.05, 20.66, 20.76, 19.85, 20.05, 45.38, 23.07, 19.1, 23.83, 16.54, 28.33, 25.88, 22.14, 22.65, 12.59, 28.1};
int retsize=100;
/* Array to receive solution values */
struct MySolArray
{
const char *ind; /* index name */
double val; /* solution value */
};
struct MySolArray *solfrac;
struct MySolArray *solbuy;
double objval;
int numshares, status;
/********************************************/
/* Callback for generating Model input data */
/********************************************/
int XPRM_RTC cbinit_from(XPRMcbinit cb, void *info, const char *label,
int type, void *ref)
{
int i;
if(strcmp(label,"RISK")==0)
{
XPRMcb_sendctrl(cb,XPRM_CBC_OPENLST,0);
for(i=0;i<risksize;i++)
{
XPRMcb_sendstring(cb,riskset[i],-1,0);
}
XPRMcb_sendctrl(cb,XPRM_CBC_CLOSELST,0);
return 0;
}
else
if(strcmp(label,"RET")==0)
{
XPRMcb_sendctrl(cb,XPRM_CBC_OPENLST,0);
for(i=0;i<retsize;i++)
{
XPRMcb_sendctrl(cb,XPRM_CBC_OPENNDX,0);
XPRMcb_sendstring(cb,retdataind[i],-1,0);
XPRMcb_sendctrl(cb,XPRM_CBC_CLOSENDX,0);
XPRMcb_sendreal(cb,retdata[i],0);
}
XPRMcb_sendctrl(cb,XPRM_CBC_CLOSELST,0);
return 0;
}
else
if(strcmp(label,"LOCTAB")==0)
{
XPRMcb_sendctrl(cb,XPRM_CBC_OPENLST,0);
for(i=0;i<locsize;i++)
{
XPRMcb_sendctrl(cb,XPRM_CBC_OPENNDX,0);
XPRMcb_sendstring(cb,locdataind1[i],-1,0);
XPRMcb_sendstring(cb,locdataind2[i],-1,0);
XPRMcb_sendctrl(cb,XPRM_CBC_CLOSENDX,0);
XPRMcb_sendint(cb,locdata[i],0);
}
XPRMcb_sendctrl(cb,XPRM_CBC_CLOSELST,0);
return 0;
}
else
if(strcmp(label,"SECTAB")==0)
{
XPRMcb_sendctrl(cb,XPRM_CBC_OPENLST,0);
for(i=0;i<secsize;i++)
{
XPRMcb_sendctrl(cb,XPRM_CBC_OPENNDX,0);
XPRMcb_sendstring(cb,secdataind1[i],-1,0);
XPRMcb_sendstring(cb,secdataind2[i],-1,0);
XPRMcb_sendctrl(cb,XPRM_CBC_CLOSENDX,0);
XPRMcb_sendint(cb,secdata[i],0);
}
XPRMcb_sendctrl(cb,XPRM_CBC_CLOSELST,0);
return 0;
}
else
{
fprintf(stderr,"Label `%s' not found.\n",label);
return 1;
}
}
/******************************************/
/* Callback for getting model output data */
/******************************************/
int XPRM_RTC cbinit_to(XPRMcbinit cb, void *info, const char *label,
int type, XPRMalltypes *ref)
{
static const char *typename[]={"none","integer","real","string","boolean"};
const char *tn,*sn;
XPRMarray solarr;
XPRMset sets[1]; /* We know all sol. arrays have 1 dimension */
int indices[1];
XPRMalltypes rvalue;
int asize, ct;
if(strcmp(label,"FRAC")==0)
{
solarr=ref->array;
asize=XPRMgetarrsize(solarr);
solfrac = (struct MySolArray *)malloc(asize * sizeof(struct MySolArray));
XPRMgetarrsets(solarr,sets); /* Get the indexing sets
(we know array has 1 dimension) */
ct=0;
XPRMgetfirstarrtruentry(solarr,indices); /* Get the first true index tuple */
do
{
solfrac[ct].ind=XPRMgetelsetval(sets[0],indices[0],&rvalue)->string;
XPRMgetarrval(solarr,indices,&rvalue);
solfrac[ct].val=rvalue.real;
ct++;
} while(!XPRMgetnextarrtruentry(solarr,indices));
}
else
if(strcmp(label,"BUY")==0)
{
solarr=ref->array;
asize=XPRMgetarrsize(solarr);
solbuy = (struct MySolArray *)malloc(asize * sizeof(struct MySolArray));
XPRMgetarrsets(solarr,sets); /* Get the indexing sets
(we know array has 1 dimension) */
ct=0;
XPRMgetfirstarrtruentry(solarr,indices); /* Get the first true index tuple */
do
{
solbuy[ct].ind=XPRMgetelsetval(sets[0],indices[0],&rvalue)->string;
XPRMgetarrval(solarr,indices,&rvalue);
solbuy[ct].val=rvalue.real;
ct++;
} while(!XPRMgetnextarrtruentry(solarr,indices));
}
else
if(strcmp(label,"RETSOL")==0)
{
objval=ref->real;
}
else
if(strcmp(label,"NUMSHARES")==0)
{
numshares=ref->integer;
}
else
if(strcmp(label,"SOLSTATUS")==0)
{
status=ref->integer;
}
else
{
if(XPRM_TYP(type)<=4) tn=typename[XPRM_TYP(type)];
else tn="external";
switch(XPRM_STR(type))
{
case XPRM_STR_CONST:
case XPRM_STR_REF: sn="ref";break;
case XPRM_STR_ARR: sn="array";break;
case XPRM_STR_SET: sn="set";break;
case XPRM_STR_LIST: sn="list";break;
default: sn="unknown";
}
printf("Unknown output data item: %s %s %s %p\n",label,sn,tn,ref);
}
return 0;
}
/********************************************************/
int main()
{
XPRMmodel mod;
char datafile_name[40], outfile_name[40]; /* File names input / output */
char params[1000]; /* Parameter string for model execution */
int i,result;
double maxrisk = 1.0/3; /* Model parameter settings */
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
sprintf(datafile_name, "cb:%p", cbinit_from);
sprintf(outfile_name, "cb:%p", cbinit_to);
/* Pass file names as execution param.s */
sprintf(params, "MAXRISK=%g,MINREG=%g,MAXREG=%g,MAXSEC=%g,MAXVAL=%g,MINVAL=%g,MAXNUM=%d,DATAFILE='%s',OUTPUTFILE='%s'",
maxrisk, minreg, maxreg, maxsec, maxval, minval, maxnum,
datafile_name, outfile_name);
if(XPRMinit()) /* Initialize Mosel */
return 1;
if(XPRMexecmod(NULL, "foliomemio.mos", params, &result, &mod))
return 2; /* Execute the model file (only during
development phase, the deployed
application would only use BIM) */
if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT)
return 3; /* Stop if no solution available */
/* Display solution values obtained from the model */
printf("Total return: %g\n", objval);
for(i=0;i<numshares;i++)
printf("%s: %g%% (%g)\n", solfrac[i].ind, solfrac[i].val*100, solbuy[i].val);
XPRMresetmod(mod); /* Reset the model */
return 0;
}
|
|
runfoliocbio.c |
/*******************************************************
Mosel Example Problems
======================
file runfoliocbio.c
```````````````````
Running a Mosel model from a C application
with data exchange between model and host application
during the optimization run.
(Passing data via callback)
*** The model started by this program cannot be run with
a Community Licence for the provided data instance ***
(c) 2012 Fair Isaac Corporation
author: S. Heipcke, May 2012, rev. Feb. 2017
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xprm_mc.h"
#include <stdio.h>
#include "xprm_mc.h"
/* Array to receive solution values */
struct MySolArray
{
const char *ind; /* index name */
double val; /* solution value */
};
struct MySolArray *solfrac;
struct MySolArray *solbuy;
int ifprint;
/******************************************/
/* Callback for getting model output data */
/******************************************/
int XPRM_RTC cbinit_to(XPRMcbinit cb, void *info, const char *label,
int type, XPRMalltypes *ref)
{
static const char *typename[]={"none","integer","real","string","boolean"};
const char *tn,*sn;
char *index;
XPRMarray solarr;
XPRMset sets[1]; /* We know all sol. arrays have 1 dimension */
int indices[1];
XPRMalltypes rvalue;
int asize,ct,i;
double objval;
int numshares, solcount;
if(strcmp(label,"FRAC")==0)
{
solarr=ref->array;
asize=XPRMgetarrsize(solarr);
solfrac = (struct MySolArray *)malloc(asize * sizeof(struct MySolArray));
XPRMgetarrsets(solarr,sets); /* Get the indexing sets
(we know array has 1 dimension) */
ct=0;
XPRMgetfirstarrtruentry(solarr,indices); /* Get the first true index tuple */
do
{
solfrac[ct].ind=XPRMgetelsetval(sets[0],indices[0],&rvalue)->string;
XPRMgetarrval(solarr,indices,&rvalue);
solfrac[ct].val=rvalue.real;
ct++;
} while(!XPRMgetnextarrtruentry(solarr,indices));
if (ifprint==0)
{ ifprint=1; }
else
{
ifprint=0;
for(i=0;i<asize;i++)
printf(" %s: %g%% (%g)\n", solfrac[i].ind, solfrac[i].val*100,
solbuy[i].val);
free(solbuy);
free(solfrac);
}
}
else if(strcmp(label,"BUY")==0)
{
solarr=ref->array;
asize=XPRMgetarrsize(solarr);
solbuy = (struct MySolArray *)malloc(asize * sizeof(struct MySolArray));
XPRMgetarrsets(solarr,sets); /* Get the indexing sets
(we know array has 1 dimension) */
ct=0;
XPRMgetfirstarrtruentry(solarr,indices); /* Get the first true index tuple */
do
{
solbuy[ct].ind=XPRMgetelsetval(sets[0],indices[0],&rvalue)->string;
XPRMgetarrval(solarr,indices,&rvalue);
solbuy[ct].val=rvalue.real;
ct++;
} while(!XPRMgetnextarrtruentry(solarr,indices));
if (ifprint==0)
{ ifprint=1; }
else
{
ifprint=0;
for(i=0;i<asize;i++)
printf(" %s: %g%% (%g)\n", solfrac[i].ind, solfrac[i].val*100,
solbuy[i].val);
free(solbuy);
free(solfrac);
}
}
else if(strcmp(label,"RETSOL")==0)
{
objval=ref->real;
printf("Total return: %g\n", objval);
}
else if(strcmp(label,"NUMSHARES")==0)
{
numshares=ref->integer;
printf("Number of shares: %d\n", numshares);
}
else if(strcmp(label,"SOLCOUNT")==0)
{
solcount=ref->integer;
printf("Solution number: %d\n", solcount);
}
else
{
if(XPRM_TYP(type)<=4) tn=typename[XPRM_TYP(type)];
else tn="external";
switch(XPRM_STR(type))
{
case XPRM_STR_CONST:
case XPRM_STR_REF: sn="ref";break;
case XPRM_STR_ARR: sn="array";break;
case XPRM_STR_SET: sn="set";break;
case XPRM_STR_LIST: sn="list";break;
default: sn="unknown";
}
printf("Unknown output data item: %s %s %s %p\n",label,sn,tn,ref);
}
return 0;
}
/********************************************************/
int main()
{
XPRMmodel mod;
char solution_name[40]; /* File name of solution callback */
char params[1000]; /* Parameter string for model execution */
int i,result;
double maxrisk = 1.0/3; /* Model parameter settings */
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
ifprint = 0;
/* Prepare file names for 'initializations' using the 'cb' driver */
sprintf(solution_name, "cb:%p", cbinit_to);
/* Pass file names as execution param.s */
sprintf(params, "MAXRISK=%g,MINREG=%g,MAXREG=%g,MAXSEC=%g,MAXVAL=%g,MINVAL=%g,MAXNUM=%d,DATAFILE='folio250.dat',OUTPUTFILE='%s'",
maxrisk, minreg, maxreg, maxsec, maxval, minval, maxnum, solution_name);
if(XPRMinit()) /* Initialize Mosel */
return 1;
if(XPRMexecmod(NULL, "foliocbio.mos", params, &result, &mod))
return 2; /* Execute the model file (only during
development phase, the deployed
application would only use BIM) */
if(result != XPRM_RT_OK){
printf("Error during model execution: %d\n", result);
return 3;
}
if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT) {
printf("Problem not optimal"); /* No solution available */
return 4;
}
XPRMresetmod(mod); /* Reset the model */
return 0;
}
|
|
runfolio.cs |
/*******************************************************
Mosel Example Problems
======================
file runfolio.cs
````````````````
Running a Mosel model from a .NET application
with data exchange between model and host application.
(Sparse data format using string indices)
(c) 2009 Fair Isaac Corporation
author: J. Farmer, Jun. 2009, rev. May. 2021
********************************************************/
using System;
using System.IO;
using Mosel;
namespace mosel_getting_started {
public class runfolio
{
// Classes to store initial values for data
public class MyArrayTwo
{
public string ind1,ind2; // index names
public bool val; // data value
public MyArrayTwo(string i1, string i2, bool v)
{ ind1=i1; ind2=i2; val=v; }
public MyArrayTwo(string i1, string i2, int v)
{ ind1=i1; ind2=i2; val=(v==1); }
}
public class MyArrayOne
{
public string ind; // index name
public double val; // data value
public MyArrayOne(string i, double v)
{ ind=i; val=v; }
}
// Class to receive solution values of decision variables
public class MySolArray
{
public string ind; // index name
public double val; // solution value
}
// Class with scalar solution information
// (scalars cannot be passed directly)
public class MySol
{
public double objval; // objective function value
public int numshares; // number of shares in solution
public int status; // problem status
}
public static void Main(string[] args)
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
// Data for 'initializations from' (input data values)
string[] riskset = new string[] {"hardware1", "hardware2", "hardware3", "hardware4",
"hardware5", "hardware6", "hardware7", "hardware8", "hardware9",
"hardware10", "theater1", "theater2", "theater3", "theater4", "theater5",
"theater6", "theater7", "theater8", "theater9", "theater10", "telecom1",
"telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7",
"telecom8", "telecom9", "telecom10", "software1", "software2", "software3",
"software4", "software5", "software6", "software7", "software8",
"software9", "software10", "electronics1", "electronics2", "electronics3",
"electronics4", "electronics5", "electronics6", "electronics7",
"electronics8", "electronics9", "electronics10"};
MyArrayTwo[] locdata = new MyArrayTwo[] {new MyArrayTwo("EU","treasury1",1),
new MyArrayTwo("EU","treasury10",1), new MyArrayTwo("EU","hardware7",1),
new MyArrayTwo("EU","hardware9",1), new MyArrayTwo("EU","theater8",1),
new MyArrayTwo("EU","theater10",1), new MyArrayTwo("EU","telecom3",1),
new MyArrayTwo("EU","telecom9",1), new MyArrayTwo("EU","telecom10",1),
new MyArrayTwo("EU","brewery1",1), new MyArrayTwo("EU","brewery4",1),
new MyArrayTwo("EU","brewery6",1), new MyArrayTwo("EU","brewery7",1),
new MyArrayTwo("EU","brewery8",1), new MyArrayTwo("EU","highways2",1),
new MyArrayTwo("EU","highways5",1), new MyArrayTwo("EU","highways7",1),
new MyArrayTwo("EU","highways10",1), new MyArrayTwo("EU","cars4",1),
new MyArrayTwo("EU","cars7",1), new MyArrayTwo("EU","cars8",1),
new MyArrayTwo("EU","cars9",1), new MyArrayTwo("EU","cars10",1),
new MyArrayTwo("EU","bank1",1), new MyArrayTwo("EU","bank3",1),
new MyArrayTwo("EU","bank5",1), new MyArrayTwo("EU","bank6",1),
new MyArrayTwo("EU","bank7",1), new MyArrayTwo("EU","bank10",1),
new MyArrayTwo("EU","software3",1), new MyArrayTwo("EU","software4",1),
new MyArrayTwo("EU","software5",1), new MyArrayTwo("EU","software6",1),
new MyArrayTwo("EU","electronics2",1), new MyArrayTwo("NA","treasury2",1),
new MyArrayTwo("NA","treasury6",1), new MyArrayTwo("NA","treasury7",1),
new MyArrayTwo("NA","treasury8",1), new MyArrayTwo("NA","hardware4",1),
new MyArrayTwo("NA","hardware6",1), new MyArrayTwo("NA","theater1",1),
new MyArrayTwo("NA","theater3",1), new MyArrayTwo("NA","theater7",1),
new MyArrayTwo("NA","theater9",1), new MyArrayTwo("NA","telecom2",1),
new MyArrayTwo("NA","telecom4",1), new MyArrayTwo("NA","telecom8",1),
new MyArrayTwo("NA","highways3",1), new MyArrayTwo("NA","highways4",1),
new MyArrayTwo("NA","cars5",1), new MyArrayTwo("NA","cars6",1),
new MyArrayTwo("NA","bank2",1), new MyArrayTwo("NA","bank4",1),
new MyArrayTwo("NA","bank8",1), new MyArrayTwo("NA","bank9",1),
new MyArrayTwo("NA","software1",1), new MyArrayTwo("NA","software2",1),
new MyArrayTwo("NA","software9",1), new MyArrayTwo("NA","electronics3",1),
new MyArrayTwo("NA","electronics5",1), new MyArrayTwo("NA","electronics6",1),
new MyArrayTwo("NA","electronics8",1), new MyArrayTwo("NA","electronics9",1),
new MyArrayTwo("NA","electronics10",1), new MyArrayTwo("APAC","treasury4",1),
new MyArrayTwo("APAC","treasury5",1), new MyArrayTwo("APAC","treasury9",1),
new MyArrayTwo("APAC","hardware1",1), new MyArrayTwo("APAC","hardware2",1),
new MyArrayTwo("APAC","hardware5",1), new MyArrayTwo("APAC","hardware8",1),
new MyArrayTwo("APAC","hardware10",1), new MyArrayTwo("APAC","theater2",1),
new MyArrayTwo("APAC","theater4",1), new MyArrayTwo("APAC","telecom1",1),
new MyArrayTwo("APAC","telecom5",1), new MyArrayTwo("APAC","telecom6",1),
new MyArrayTwo("APAC","telecom7",1), new MyArrayTwo("APAC","brewery9",1),
new MyArrayTwo("APAC","brewery10",1), new MyArrayTwo("APAC","highways8",1),
new MyArrayTwo("APAC","cars1",1), new MyArrayTwo("APAC","cars2",1),
new MyArrayTwo("APAC","software7",1), new MyArrayTwo("APAC","software8",1),
new MyArrayTwo("APAC","software10",1),
new MyArrayTwo("APAC","electronics1",1),
new MyArrayTwo("APAC","electronics4",1),
new MyArrayTwo("APAC","electronics7",1)};
MyArrayTwo[] secdata = new MyArrayTwo[] {new MyArrayTwo("bonds","treasury1",1),
new MyArrayTwo("bonds","treasury10",1),
new MyArrayTwo("bonds","treasury2",1),
new MyArrayTwo("bonds","treasury6",1),
new MyArrayTwo("bonds","treasury7",1),
new MyArrayTwo("bonds","treasury8",1),
new MyArrayTwo("bonds","treasury4",1),
new MyArrayTwo("bonds","treasury5",1),
new MyArrayTwo("bonds","treasury9",1),
new MyArrayTwo("bonds","treasury3",1),
new MyArrayTwo("technology","hardware7",1),
new MyArrayTwo("technology","hardware9",1),
new MyArrayTwo("technology","software3",1),
new MyArrayTwo("technology","software4",1),
new MyArrayTwo("technology","software5",1),
new MyArrayTwo("technology","software6",1),
new MyArrayTwo("technology","electronics2",1),
new MyArrayTwo("technology","hardware4",1),
new MyArrayTwo("technology","hardware6",1),
new MyArrayTwo("technology","software1",1),
new MyArrayTwo("technology","software2",1),
new MyArrayTwo("technology","software9",1),
new MyArrayTwo("technology","electronics3",1),
new MyArrayTwo("technology","electronics5",1),
new MyArrayTwo("technology","electronics6",1),
new MyArrayTwo("technology","electronics8",1),
new MyArrayTwo("technology","electronics9",1),
new MyArrayTwo("technology","electronics10",1),
new MyArrayTwo("technology","hardware1",1),
new MyArrayTwo("technology","hardware2",1),
new MyArrayTwo("technology","hardware5",1),
new MyArrayTwo("technology","hardware8",1),
new MyArrayTwo("technology","hardware10",1),
new MyArrayTwo("technology","software7",1),
new MyArrayTwo("technology","software8",1),
new MyArrayTwo("technology","software10",1),
new MyArrayTwo("technology","electronics1",1),
new MyArrayTwo("technology","electronics4",1),
new MyArrayTwo("technology","electronics7",1),
new MyArrayTwo("technology","hardware3",1),
new MyArrayTwo("entertainment","theater8",1),
new MyArrayTwo("entertainment","theater10",1),
new MyArrayTwo("entertainment","theater1",1),
new MyArrayTwo("entertainment","theater3",1),
new MyArrayTwo("entertainment","theater7",1),
new MyArrayTwo("entertainment","theater9",1),
new MyArrayTwo("entertainment","theater2",1),
new MyArrayTwo("entertainment","theater4",1),
new MyArrayTwo("entertainment","theater5",1),
new MyArrayTwo("entertainment","theater6",1),
new MyArrayTwo("telecom","telecom3",1),
new MyArrayTwo("telecom","telecom9",1),
new MyArrayTwo("telecom","telecom10",1),
new MyArrayTwo("telecom","telecom2",1),
new MyArrayTwo("telecom","telecom4",1),
new MyArrayTwo("telecom","telecom8",1),
new MyArrayTwo("telecom","telecom1",1),
new MyArrayTwo("telecom","telecom5",1),
new MyArrayTwo("telecom","telecom6",1),
new MyArrayTwo("telecom","telecom7",1),
new MyArrayTwo("food","brewery1",1), new MyArrayTwo("food","brewery4",1),
new MyArrayTwo("food","brewery6",1), new MyArrayTwo("food","brewery7",1),
new MyArrayTwo("food","brewery8",1), new MyArrayTwo("food","brewery9",1),
new MyArrayTwo("food","brewery10",1), new MyArrayTwo("food","brewery2",1),
new MyArrayTwo("food","brewery3",1), new MyArrayTwo("food","brewery5",1),
new MyArrayTwo("construction","highways2",1),
new MyArrayTwo("construction","highways5",1),
new MyArrayTwo("construction","highways7",1),
new MyArrayTwo("construction","highways10",1),
new MyArrayTwo("construction","highways3",1),
new MyArrayTwo("construction","highways4",1),
new MyArrayTwo("construction","highways8",1),
new MyArrayTwo("construction","highways1",1),
new MyArrayTwo("construction","highways6",1),
new MyArrayTwo("construction","highways9",1),
new MyArrayTwo("manufacturing","cars4",1),
new MyArrayTwo("manufacturing","cars7",1),
new MyArrayTwo("manufacturing","cars8",1),
new MyArrayTwo("manufacturing","cars9",1),
new MyArrayTwo("manufacturing","cars10",1),
new MyArrayTwo("manufacturing","cars5",1),
new MyArrayTwo("manufacturing","cars6",1),
new MyArrayTwo("manufacturing","cars1",1),
new MyArrayTwo("manufacturing","cars2",1),
new MyArrayTwo("manufacturing","cars3",1),
new MyArrayTwo("finance","bank1",1), new MyArrayTwo("finance","bank3",1),
new MyArrayTwo("finance","bank5",1), new MyArrayTwo("finance","bank6",1),
new MyArrayTwo("finance","bank7",1), new MyArrayTwo("finance","bank10",1),
new MyArrayTwo("finance","bank2",1), new MyArrayTwo("finance","bank4",1),
new MyArrayTwo("finance","bank8",1), new MyArrayTwo("finance","bank9",1)};
MyArrayOne[] retdata = new MyArrayOne[] {new MyArrayOne("treasury1",5.29),
new MyArrayOne("treasury2",2.8), new MyArrayOne("treasury3",3.59),
new MyArrayOne("treasury4",6.97), new MyArrayOne("treasury5",3.3),
new MyArrayOne("treasury6",3.02), new MyArrayOne("treasury7",2.98),
new MyArrayOne("treasury8",3.41), new MyArrayOne("treasury9",3.93),
new MyArrayOne("treasury10",3.86), new MyArrayOne("hardware1",11.36),
new MyArrayOne("hardware2",17.23), new MyArrayOne("hardware3",16.31),
new MyArrayOne("hardware4",21.1), new MyArrayOne("hardware5",13.76),
new MyArrayOne("hardware6",9.31), new MyArrayOne("hardware7",16.99),
new MyArrayOne("hardware8",24.85), new MyArrayOne("hardware9",18.52),
new MyArrayOne("hardware10",12.79), new MyArrayOne("theater1",36.1),
new MyArrayOne("theater2",19.4), new MyArrayOne("theater3",23.5),
new MyArrayOne("theater4",34.61), new MyArrayOne("theater5",16.91),
new MyArrayOne("theater6",27.04), new MyArrayOne("theater7",25.82),
new MyArrayOne("theater8",24.99), new MyArrayOne("theater9",36.89),
new MyArrayOne("theater10",31.71), new MyArrayOne("telecom1",10.89),
new MyArrayOne("telecom2",17.98), new MyArrayOne("telecom3",12.31),
new MyArrayOne("telecom4",6.53), new MyArrayOne("telecom5",6.11),
new MyArrayOne("telecom6",15.89), new MyArrayOne("telecom7",12.46),
new MyArrayOne("telecom8",11.11), new MyArrayOne("telecom9",16.6),
new MyArrayOne("telecom10",13.93), new MyArrayOne("brewery1",8.79),
new MyArrayOne("brewery2",9.35), new MyArrayOne("brewery3",8.88),
new MyArrayOne("brewery4",11.63), new MyArrayOne("brewery5",6.21),
new MyArrayOne("brewery6",9.26), new MyArrayOne("brewery7",4.77),
new MyArrayOne("brewery8",5.98), new MyArrayOne("brewery9",10.92),
new MyArrayOne("brewery10",5.26), new MyArrayOne("highways1",5.39),
new MyArrayOne("highways2",4.85), new MyArrayOne("highways3",8.34),
new MyArrayOne("highways4",8.48), new MyArrayOne("highways5",6.42),
new MyArrayOne("highways6",11.04), new MyArrayOne("highways7",13.15),
new MyArrayOne("highways8",10.01), new MyArrayOne("highways9",7.71),
new MyArrayOne("highways10",11.07), new MyArrayOne("cars1",5.74),
new MyArrayOne("cars2",7.13), new MyArrayOne("cars3",8.75),
new MyArrayOne("cars4",5.08), new MyArrayOne("cars5",6.23),
new MyArrayOne("cars6",8.19), new MyArrayOne("cars7",8.03),
new MyArrayOne("cars8",5.96), new MyArrayOne("cars9",4.17),
new MyArrayOne("cars10",8.11), new MyArrayOne("bank1",6.79),
new MyArrayOne("bank2",3.01), new MyArrayOne("bank3",4.92),
new MyArrayOne("bank4",4.14), new MyArrayOne("bank5",8.98),
new MyArrayOne("bank6",8.81), new MyArrayOne("bank7",4.7),
new MyArrayOne("bank8",8.06), new MyArrayOne("bank9",6.22),
new MyArrayOne("bank10",4.47), new MyArrayOne("software1",34.59),
new MyArrayOne("software2",44.94), new MyArrayOne("software3",43.05),
new MyArrayOne("software4",15.58), new MyArrayOne("software5",42.05),
new MyArrayOne("software6",20.66), new MyArrayOne("software7",20.76),
new MyArrayOne("software8",19.85), new MyArrayOne("software9",20.05),
new MyArrayOne("software10",45.38), new MyArrayOne("electronics1",23.07),
new MyArrayOne("electronics2",19.1), new MyArrayOne("electronics3",23.83),
new MyArrayOne("electronics4",16.54), new MyArrayOne("electronics5",28.33),
new MyArrayOne("electronics6",25.88), new MyArrayOne("electronics7",22.14),
new MyArrayOne("electronics8",22.65), new MyArrayOne("electronics9",12.59),
new MyArrayOne("electronics10",28.1)};
// Data structures for 'initializations to' (solution)
MySol solution = new MySol();
MySolArray[] solfrac = new MySolArray[maxnum];
MySolArray[] solbuy = new MySolArray[maxnum];
for(int i=0;i<maxnum;i++)
{ solfrac[i] = new MySolArray(); solbuy[i] = new MySolArray(); }
try{
mosel = XPRM.Init(); // Initialize Mosel
}catch(XPRMException e){
Console.WriteLine("License error" + e.Message);
throw new Exception("Error during execution");
}
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Set Mosel work directory to folder containing our example files
try{
mosel.Compile("foliomemio.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMException e){
Console.WriteLine(e.Message);
}
mod = mosel.LoadModel("foliomemio.bim"); // Load the model
// Associate the Java objects with names in Mosel
mosel.Bind("risk", riskset);
mosel.Bind("loc", locdata);
mosel.Bind("sec", secdata);
mosel.Bind("ret", retdata);
mosel.Bind("sfrac", solfrac);
mosel.Bind("sbuy", solbuy);
mosel.Bind("sol", solution);
// Pass model parameters through execution parameters
mod.ExecParams = "MAXRISK=" + maxrisk + ",MINREG=" + minreg +
",MAXREG=" + maxreg + ",MAXSEC=" + maxsec + ",MAXVAL=" +
maxval + ",MINVAL=" + minval + ",MAXNUM=" + maxnum +
// File names (IO driver)
",DATAFILE='dotnetraw:',OUTPUTFILE='dotnetraw:'," +
// Data locations in memory
"RISKDATA='risk',LOCDATA='loc(ind1,ind2,val)'," +
"RETDATA='ret(ind,val)',SECDATA='sec(ind1,ind2,val)'," +
"FRACSOL='sfrac(ind,val)',BUYSOL='sbuy(ind,val)'," +
"RETSOL='sol(objval)',NUMSHARES='sol(numshares)'," +
"SOLSTATUS='sol(status)'";
mod.Run(); // Run the model
if(mod.ExecStatus != XPRMRunResult.RT_OK){
throw new Exception("Error during model execution");
}
if(mod.ProblemStatus != XPRMProblemStatus.PB_OPTIMAL){
throw new Exception("Problem not optimal");
} // Stop if no solution available
// Display solution values obtained from the model
Console.WriteLine("Total return: " + solution.objval);
// Alternatively:
// Console.WriteLine("Objective value: " + mod.getObjectiveValue());
for(int i=0;i<solution.numshares;i++)
Console.WriteLine(solfrac[i].ind + ": " + solfrac[i].val*100 + "% (" + solbuy[i].val + ")");
mod.Reset(); // Reset the model
}
}
}
|
|
runfolio2.cs |
/*******************************************************
Mosel Example Problems
======================
file runfolio2.cs
`````````````````
Running a Mosel model from a .NET application
with data exchange between model and host application.
(Sparse data format using string indices)
-- Grouping arrays with identical index sets --
Note: This example will only work after the first
Xpress 7.0 maintenance release.
(c) 2009 Fair Isaac Corporation
author: J. Farmer, Jun. 2021, rev. May. 2021
********************************************************/
using Mosel;
using System;
using System.IO;
namespace mosel_getting_started {
public class runfolio2
{
// Classes to store initial values for data
public class MyArrayTwo
{
public string ind1,ind2; // index names
public bool val; // data value
public MyArrayTwo(string i1, string i2, bool v)
{ ind1=i1; ind2=i2; val=v; }
public MyArrayTwo(string i1, string i2, int v)
{ ind1=i1; ind2=i2; val=(v==1); }
}
public class MyArrayOne
{
public string ind; // index name
public double val; // data value
public MyArrayOne(string i, double v)
{ ind=i; val=v; }
}
// Class to receive solution values of decision variables
public class MySolArray
{
public string ind; // index name
public double val1,val2; // solution values
}
// Class with scalar solution information
// (scalars cannot be passed directly)
public class MySol
{
public double objval; // objective function value
public int numshares; // number of shares in solution
public int status; // problem status
}
public static void Main(string[] args)
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
// Data for 'initializations from' (input data values)
string[] riskset = new string[] {"hardware1", "hardware2", "hardware3", "hardware4",
"hardware5", "hardware6", "hardware7", "hardware8", "hardware9",
"hardware10", "theater1", "theater2", "theater3", "theater4", "theater5",
"theater6", "theater7", "theater8", "theater9", "theater10", "telecom1",
"telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7",
"telecom8", "telecom9", "telecom10", "software1", "software2", "software3",
"software4", "software5", "software6", "software7", "software8",
"software9", "software10", "electronics1", "electronics2", "electronics3",
"electronics4", "electronics5", "electronics6", "electronics7",
"electronics8", "electronics9", "electronics10"};
MyArrayTwo[] locdata = new MyArrayTwo[] {new MyArrayTwo("EU","treasury1",1),
new MyArrayTwo("EU","treasury10",1), new MyArrayTwo("EU","hardware7",1),
new MyArrayTwo("EU","hardware9",1), new MyArrayTwo("EU","theater8",1),
new MyArrayTwo("EU","theater10",1), new MyArrayTwo("EU","telecom3",1),
new MyArrayTwo("EU","telecom9",1), new MyArrayTwo("EU","telecom10",1),
new MyArrayTwo("EU","brewery1",1), new MyArrayTwo("EU","brewery4",1),
new MyArrayTwo("EU","brewery6",1), new MyArrayTwo("EU","brewery7",1),
new MyArrayTwo("EU","brewery8",1), new MyArrayTwo("EU","highways2",1),
new MyArrayTwo("EU","highways5",1), new MyArrayTwo("EU","highways7",1),
new MyArrayTwo("EU","highways10",1), new MyArrayTwo("EU","cars4",1),
new MyArrayTwo("EU","cars7",1), new MyArrayTwo("EU","cars8",1),
new MyArrayTwo("EU","cars9",1), new MyArrayTwo("EU","cars10",1),
new MyArrayTwo("EU","bank1",1), new MyArrayTwo("EU","bank3",1),
new MyArrayTwo("EU","bank5",1), new MyArrayTwo("EU","bank6",1),
new MyArrayTwo("EU","bank7",1), new MyArrayTwo("EU","bank10",1),
new MyArrayTwo("EU","software3",1), new MyArrayTwo("EU","software4",1),
new MyArrayTwo("EU","software5",1), new MyArrayTwo("EU","software6",1),
new MyArrayTwo("EU","electronics2",1), new MyArrayTwo("NA","treasury2",1),
new MyArrayTwo("NA","treasury6",1), new MyArrayTwo("NA","treasury7",1),
new MyArrayTwo("NA","treasury8",1), new MyArrayTwo("NA","hardware4",1),
new MyArrayTwo("NA","hardware6",1), new MyArrayTwo("NA","theater1",1),
new MyArrayTwo("NA","theater3",1), new MyArrayTwo("NA","theater7",1),
new MyArrayTwo("NA","theater9",1), new MyArrayTwo("NA","telecom2",1),
new MyArrayTwo("NA","telecom4",1), new MyArrayTwo("NA","telecom8",1),
new MyArrayTwo("NA","highways3",1), new MyArrayTwo("NA","highways4",1),
new MyArrayTwo("NA","cars5",1), new MyArrayTwo("NA","cars6",1),
new MyArrayTwo("NA","bank2",1), new MyArrayTwo("NA","bank4",1),
new MyArrayTwo("NA","bank8",1), new MyArrayTwo("NA","bank9",1),
new MyArrayTwo("NA","software1",1), new MyArrayTwo("NA","software2",1),
new MyArrayTwo("NA","software9",1), new MyArrayTwo("NA","electronics3",1),
new MyArrayTwo("NA","electronics5",1), new MyArrayTwo("NA","electronics6",1),
new MyArrayTwo("NA","electronics8",1), new MyArrayTwo("NA","electronics9",1),
new MyArrayTwo("NA","electronics10",1), new MyArrayTwo("APAC","treasury4",1),
new MyArrayTwo("APAC","treasury5",1), new MyArrayTwo("APAC","treasury9",1),
new MyArrayTwo("APAC","hardware1",1), new MyArrayTwo("APAC","hardware2",1),
new MyArrayTwo("APAC","hardware5",1), new MyArrayTwo("APAC","hardware8",1),
new MyArrayTwo("APAC","hardware10",1), new MyArrayTwo("APAC","theater2",1),
new MyArrayTwo("APAC","theater4",1), new MyArrayTwo("APAC","telecom1",1),
new MyArrayTwo("APAC","telecom5",1), new MyArrayTwo("APAC","telecom6",1),
new MyArrayTwo("APAC","telecom7",1), new MyArrayTwo("APAC","brewery9",1),
new MyArrayTwo("APAC","brewery10",1), new MyArrayTwo("APAC","highways8",1),
new MyArrayTwo("APAC","cars1",1), new MyArrayTwo("APAC","cars2",1),
new MyArrayTwo("APAC","software7",1), new MyArrayTwo("APAC","software8",1),
new MyArrayTwo("APAC","software10",1),
new MyArrayTwo("APAC","electronics1",1),
new MyArrayTwo("APAC","electronics4",1),
new MyArrayTwo("APAC","electronics7",1)};
MyArrayTwo[] secdata = new MyArrayTwo[] {new MyArrayTwo("bonds","treasury1",1),
new MyArrayTwo("bonds","treasury10",1),
new MyArrayTwo("bonds","treasury2",1),
new MyArrayTwo("bonds","treasury6",1),
new MyArrayTwo("bonds","treasury7",1),
new MyArrayTwo("bonds","treasury8",1),
new MyArrayTwo("bonds","treasury4",1),
new MyArrayTwo("bonds","treasury5",1),
new MyArrayTwo("bonds","treasury9",1),
new MyArrayTwo("bonds","treasury3",1),
new MyArrayTwo("technology","hardware7",1),
new MyArrayTwo("technology","hardware9",1),
new MyArrayTwo("technology","software3",1),
new MyArrayTwo("technology","software4",1),
new MyArrayTwo("technology","software5",1),
new MyArrayTwo("technology","software6",1),
new MyArrayTwo("technology","electronics2",1),
new MyArrayTwo("technology","hardware4",1),
new MyArrayTwo("technology","hardware6",1),
new MyArrayTwo("technology","software1",1),
new MyArrayTwo("technology","software2",1),
new MyArrayTwo("technology","software9",1),
new MyArrayTwo("technology","electronics3",1),
new MyArrayTwo("technology","electronics5",1),
new MyArrayTwo("technology","electronics6",1),
new MyArrayTwo("technology","electronics8",1),
new MyArrayTwo("technology","electronics9",1),
new MyArrayTwo("technology","electronics10",1),
new MyArrayTwo("technology","hardware1",1),
new MyArrayTwo("technology","hardware2",1),
new MyArrayTwo("technology","hardware5",1),
new MyArrayTwo("technology","hardware8",1),
new MyArrayTwo("technology","hardware10",1),
new MyArrayTwo("technology","software7",1),
new MyArrayTwo("technology","software8",1),
new MyArrayTwo("technology","software10",1),
new MyArrayTwo("technology","electronics1",1),
new MyArrayTwo("technology","electronics4",1),
new MyArrayTwo("technology","electronics7",1),
new MyArrayTwo("technology","hardware3",1),
new MyArrayTwo("entertainment","theater8",1),
new MyArrayTwo("entertainment","theater10",1),
new MyArrayTwo("entertainment","theater1",1),
new MyArrayTwo("entertainment","theater3",1),
new MyArrayTwo("entertainment","theater7",1),
new MyArrayTwo("entertainment","theater9",1),
new MyArrayTwo("entertainment","theater2",1),
new MyArrayTwo("entertainment","theater4",1),
new MyArrayTwo("entertainment","theater5",1),
new MyArrayTwo("entertainment","theater6",1),
new MyArrayTwo("telecom","telecom3",1),
new MyArrayTwo("telecom","telecom9",1),
new MyArrayTwo("telecom","telecom10",1),
new MyArrayTwo("telecom","telecom2",1),
new MyArrayTwo("telecom","telecom4",1),
new MyArrayTwo("telecom","telecom8",1),
new MyArrayTwo("telecom","telecom1",1),
new MyArrayTwo("telecom","telecom5",1),
new MyArrayTwo("telecom","telecom6",1),
new MyArrayTwo("telecom","telecom7",1),
new MyArrayTwo("food","brewery1",1), new MyArrayTwo("food","brewery4",1),
new MyArrayTwo("food","brewery6",1), new MyArrayTwo("food","brewery7",1),
new MyArrayTwo("food","brewery8",1), new MyArrayTwo("food","brewery9",1),
new MyArrayTwo("food","brewery10",1), new MyArrayTwo("food","brewery2",1),
new MyArrayTwo("food","brewery3",1), new MyArrayTwo("food","brewery5",1),
new MyArrayTwo("construction","highways2",1),
new MyArrayTwo("construction","highways5",1),
new MyArrayTwo("construction","highways7",1),
new MyArrayTwo("construction","highways10",1),
new MyArrayTwo("construction","highways3",1),
new MyArrayTwo("construction","highways4",1),
new MyArrayTwo("construction","highways8",1),
new MyArrayTwo("construction","highways1",1),
new MyArrayTwo("construction","highways6",1),
new MyArrayTwo("construction","highways9",1),
new MyArrayTwo("manufacturing","cars4",1),
new MyArrayTwo("manufacturing","cars7",1),
new MyArrayTwo("manufacturing","cars8",1),
new MyArrayTwo("manufacturing","cars9",1),
new MyArrayTwo("manufacturing","cars10",1),
new MyArrayTwo("manufacturing","cars5",1),
new MyArrayTwo("manufacturing","cars6",1),
new MyArrayTwo("manufacturing","cars1",1),
new MyArrayTwo("manufacturing","cars2",1),
new MyArrayTwo("manufacturing","cars3",1),
new MyArrayTwo("finance","bank1",1), new MyArrayTwo("finance","bank3",1),
new MyArrayTwo("finance","bank5",1), new MyArrayTwo("finance","bank6",1),
new MyArrayTwo("finance","bank7",1), new MyArrayTwo("finance","bank10",1),
new MyArrayTwo("finance","bank2",1), new MyArrayTwo("finance","bank4",1),
new MyArrayTwo("finance","bank8",1), new MyArrayTwo("finance","bank9",1)};
MyArrayOne[] retdata = new MyArrayOne[] {new MyArrayOne("treasury1",5.29),
new MyArrayOne("treasury2",2.8), new MyArrayOne("treasury3",3.59),
new MyArrayOne("treasury4",6.97), new MyArrayOne("treasury5",3.3),
new MyArrayOne("treasury6",3.02), new MyArrayOne("treasury7",2.98),
new MyArrayOne("treasury8",3.41), new MyArrayOne("treasury9",3.93),
new MyArrayOne("treasury10",3.86), new MyArrayOne("hardware1",11.36),
new MyArrayOne("hardware2",17.23), new MyArrayOne("hardware3",16.31),
new MyArrayOne("hardware4",21.1), new MyArrayOne("hardware5",13.76),
new MyArrayOne("hardware6",9.31), new MyArrayOne("hardware7",16.99),
new MyArrayOne("hardware8",24.85), new MyArrayOne("hardware9",18.52),
new MyArrayOne("hardware10",12.79), new MyArrayOne("theater1",36.1),
new MyArrayOne("theater2",19.4), new MyArrayOne("theater3",23.5),
new MyArrayOne("theater4",34.61), new MyArrayOne("theater5",16.91),
new MyArrayOne("theater6",27.04), new MyArrayOne("theater7",25.82),
new MyArrayOne("theater8",24.99), new MyArrayOne("theater9",36.89),
new MyArrayOne("theater10",31.71), new MyArrayOne("telecom1",10.89),
new MyArrayOne("telecom2",17.98), new MyArrayOne("telecom3",12.31),
new MyArrayOne("telecom4",6.53), new MyArrayOne("telecom5",6.11),
new MyArrayOne("telecom6",15.89), new MyArrayOne("telecom7",12.46),
new MyArrayOne("telecom8",11.11), new MyArrayOne("telecom9",16.6),
new MyArrayOne("telecom10",13.93), new MyArrayOne("brewery1",8.79),
new MyArrayOne("brewery2",9.35), new MyArrayOne("brewery3",8.88),
new MyArrayOne("brewery4",11.63), new MyArrayOne("brewery5",6.21),
new MyArrayOne("brewery6",9.26), new MyArrayOne("brewery7",4.77),
new MyArrayOne("brewery8",5.98), new MyArrayOne("brewery9",10.92),
new MyArrayOne("brewery10",5.26), new MyArrayOne("highways1",5.39),
new MyArrayOne("highways2",4.85), new MyArrayOne("highways3",8.34),
new MyArrayOne("highways4",8.48), new MyArrayOne("highways5",6.42),
new MyArrayOne("highways6",11.04), new MyArrayOne("highways7",13.15),
new MyArrayOne("highways8",10.01), new MyArrayOne("highways9",7.71),
new MyArrayOne("highways10",11.07), new MyArrayOne("cars1",5.74),
new MyArrayOne("cars2",7.13), new MyArrayOne("cars3",8.75),
new MyArrayOne("cars4",5.08), new MyArrayOne("cars5",6.23),
new MyArrayOne("cars6",8.19), new MyArrayOne("cars7",8.03),
new MyArrayOne("cars8",5.96), new MyArrayOne("cars9",4.17),
new MyArrayOne("cars10",8.11), new MyArrayOne("bank1",6.79),
new MyArrayOne("bank2",3.01), new MyArrayOne("bank3",4.92),
new MyArrayOne("bank4",4.14), new MyArrayOne("bank5",8.98),
new MyArrayOne("bank6",8.81), new MyArrayOne("bank7",4.7),
new MyArrayOne("bank8",8.06), new MyArrayOne("bank9",6.22),
new MyArrayOne("bank10",4.47), new MyArrayOne("software1",34.59),
new MyArrayOne("software2",44.94), new MyArrayOne("software3",43.05),
new MyArrayOne("software4",15.58), new MyArrayOne("software5",42.05),
new MyArrayOne("software6",20.66), new MyArrayOne("software7",20.76),
new MyArrayOne("software8",19.85), new MyArrayOne("software9",20.05),
new MyArrayOne("software10",45.38), new MyArrayOne("electronics1",23.07),
new MyArrayOne("electronics2",19.1), new MyArrayOne("electronics3",23.83),
new MyArrayOne("electronics4",16.54), new MyArrayOne("electronics5",28.33),
new MyArrayOne("electronics6",25.88), new MyArrayOne("electronics7",22.14),
new MyArrayOne("electronics8",22.65), new MyArrayOne("electronics9",12.59),
new MyArrayOne("electronics10",28.1)};
// Data structures for 'initializations to' (solution)
MySol solution = new MySol();
MySolArray[] sol = new MySolArray[maxnum];
for(int i=0;i<maxnum;i++)
sol[i] = new MySolArray();
try{
mosel = XPRM.Init(); // Initialize Mosel
}catch(XPRMException e){
Console.WriteLine("License error" + e.Message);
throw new Exception("Error during execution");
}
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Set Mosel work directory to folder containing our example files
try{
mosel.Compile("foliomemio2.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMException e){
Console.WriteLine(e.Message);
}
mod = mosel.LoadModel("foliomemio2.bim"); // Load the model
// Associate the Java objects with names in Mosel
mosel.Bind("risk", riskset);
mosel.Bind("loc", locdata);
mosel.Bind("sec", secdata);
mosel.Bind("ret", retdata);
mosel.Bind("sfracbuy", sol);
mosel.Bind("sol", solution);
// Pass model parameters through execution parameters
mod.ExecParams = "MAXRISK=" + maxrisk + ",MINREG=" + minreg +
",MAXREG=" + maxreg + ",MAXSEC=" + maxsec + ",MAXVAL=" +
maxval + ",MINVAL=" + minval + ",MAXNUM=" + maxnum +
// File names (IO driver)
",DATAFILE='dotnetraw:',OUTPUTFILE='dotnetraw:'," +
// Data locations in memory
"RISKDATA='risk',LOCDATA='loc(ind1,ind2,val)'," +
"RETDATA='ret(ind,val)',SECDATA='sec(ind1,ind2,val)'," +
"FRACBUYSOL='sfracbuy(ind,val1,val2)'," +
"RETSOL='sol(objval)',NUMSHARES='sol(numshares)'," +
"SOLSTATUS='sol(status)'";
mod.Run(); // Run the model
if(mod.ExecStatus != XPRMRunResult.RT_OK){
throw new Exception("Error during model execution");
}
if(mod.ProblemStatus != XPRMProblemStatus.PB_OPTIMAL){
throw new Exception("Problem not optimal");
} // Stop if no solution available
// Display solution values obtained from the model
Console.WriteLine("Total return: " + solution.objval);
// Alternatively:
// Console.WriteLine("Objective value: " + mod.getObjectiveValue());
for(int i=0;i<solution.numshares;i++)
Console.WriteLine(sol[i].ind + ": " + sol[i].val1*100 + "% (" + sol[i].val2 + ")");
mod.Reset(); // Reset the model
}
}
}
|
|
runfoliob.cs |
/*******************************************************
Mosel Example Problems
======================
file runfoliob.cs
`````````````````
Running a Mosel model from a .NET application
with data exchange between model and host application.
(Passing data through buffers)
(c) 2009 Fair Isaac Corporation
author: J. Farmer, Jun. 2009, rev. May. 2021
********************************************************/
using Mosel;
using System;
using System.IO;
namespace mosel_getting_started {
public class runfoliob
{
private const string DATAFILE = "folio10.dat";
public static void Main(string[] args)
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
// Input and output handling
MemoryStream inputData = new MemoryStream();
MemoryStream resultOutput;
long last;
byte[] output;
// Read input data from text file in Mosel format, into the input buffer
string dataDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
FileStream filereader = new FileStream(dataDir+"/"+DATAFILE, FileMode.Open, FileAccess.Read);
byte[] buf = new byte[128];
int b;
while ((b=filereader.Read(buf,0,buf.Length))!=0)
{
inputData.Write(buf, 0, b);
}
filereader.Close();
inputData.Seek(0, SeekOrigin.Begin);
resultOutput = new MemoryStream(4096); // Create 4K byte buffer
try{
mosel = XPRM.Init(); // Initialize Mosel
}catch(XPRMException e){
Console.WriteLine("License error" + e.Message);
throw new Exception("Error during execution");
}
mosel.WorkDir = dataDir; // Set Mosel work directory to folder containing our example files
try{
mosel.Compile("foliomemio.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMException e){
Console.WriteLine(e.Message);
}
mod = mosel.LoadModel("foliomemio.bim"); // Load the model
// Associate the .NET objects with names in Mosel
mosel.Bind("inputdata", inputData);
mosel.Bind("outputdata", resultOutput);
// Pass model parameters through execution parameters
mod.ExecParams = "MAXRISK=" + maxrisk + ",MINREG=" + minreg +
",MAXREG=" + maxreg + ",MAXSEC=" + maxsec + ",MAXVAL=" +
maxval + ",MINVAL=" + minval + ",MAXNUM=" + maxnum +
// File names (IO driver)
",DATAFILE='dotnetstream:inputdata',OUTPUTFILE='dotnetstream:outputdata',";
mod.Run(); // Run the model
if(mod.ExecStatus != XPRMRunResult.RT_OK){
throw new Exception("Error during model execution");
}
if(mod.ProblemStatus != XPRMProblemStatus.PB_OPTIMAL){
throw new Exception("Problem not optimal");
} // Stop if no solution available
// Display result output obtained from the model
Console.WriteLine("Solution:");
last = resultOutput.Length;
output = resultOutput.GetBuffer();
for (long i=0; i<last; i++)
Console.Write((char)output[i]);
mod.Reset(); // Reset the model
}
}
}
|
|
runfoliocbio.cs |
/*******************************************************
Mosel Example Problems
======================
file runfoliocbio.cs
````````````````````
Running a Mosel model from a C# application
with data exchange between model and host application
during the optimization run.
(Passing data via callback)
*** The model started by this program cannot be run with
a Community Licence for the provided data instance ***
(c) 2012 Fair Isaac Corporation
author: S. Heipcke, July 2011
J. Farmer, May 2012
********************************************************/
using System;
using System.IO;
using Mosel;
namespace mosel_examples {
// Class to receive solution values of decision variables
public class MySolArray
{
public string ind; // index name
public double val; // solution value
}
public class runfoliocbio
{
static MySolArray[] solfrac;
static MySolArray[] solbuy;
static bool ifprint;
/*************************************************/
/* A method to initialize model data via callback */
/*************************************************/
/**** Retrieving data from Mosel ****/
public static bool initializeTo(string label, XPRMValue value)
{
double retsol;
int numshares, solcount;
XPRMArray solarr;
XPRMSet[] sets;
int[] indices;
int asize, ct;
if(label.Equals("FRAC"))
{
solarr=(XPRMArray)value;
asize=solarr.Size;
solfrac = new MySolArray[asize];
for(int i=0;i<asize;i++) solfrac[i] = new MySolArray();
sets = solarr.IndexSets; // Get the indexing sets
ct=0;
indices = solarr.FirstTEIndex; // Get the first entry of the array
do
{
solfrac[ct].ind=sets[0].GetAsString(indices[0]);
solfrac[ct].val=solarr.GetAsReal(indices);
ct++;
} while(solarr.NextTEIndex(indices)); // Get the next index
if (ifprint==false)
{ ifprint=true; }
else
{
ifprint=false;
for(int i=0;i<asize;i++)
Console.WriteLine(solfrac[i].ind + ": " + solfrac[i].val*100 + "% (" +
solbuy[i].val + ")");
solbuy = null;
solfrac = null;
}
}
else if(label.Equals("BUY"))
{
solarr=(XPRMArray)value;
asize=solarr.Size;
solbuy = new MySolArray[asize];
for(int i=0;i<asize;i++) solbuy[i] = new MySolArray();
sets = solarr.IndexSets; // Get the indexing sets
ct=0;
indices = solarr.FirstTEIndex; // Get the first entry of the array
do
{
solbuy[ct].ind=sets[0].GetAsString(indices[0]);
solbuy[ct].val=solarr.GetAsReal(indices);
ct++;
} while(solarr.NextTEIndex(indices)); // Get the next index
if (ifprint==false)
{ ifprint=true; }
else
{
ifprint=false;
for(int i=0;i<asize;i++)
Console.WriteLine(solfrac[i].ind + ": " + solfrac[i].val*100 + "% (" +
solbuy[i].val + ")");
solbuy = null;
solfrac = null;
}
}
else if(label.Equals("RETSOL"))
{
retsol=value.AsReal();
Console.WriteLine("Total return: " + retsol);
}
else if(label.Equals("NUMSHARES"))
{
numshares=value.AsInteger();
Console.WriteLine("Number of shares: " + numshares);
}
else if(label.Equals("SOLCOUNT"))
{
solcount=value.AsInteger();
Console.WriteLine("Solution number: " + solcount);
}
else Console.WriteLine("Unknown output data item: " + label + "=" + value);
return true;
}
public static void Main(string[] args)
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
ifprint = false;
try{
mosel = XPRM.Init(); // Initialize Mosel
}catch(XPRMException e){
Console.WriteLine("License error" + e.Message);
throw new Exception("Error during execution");
}
mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
// Set Mosel work directory to folder containing our example files
try{
mosel.Compile("foliocbio.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMCompileException e){
Console.WriteLine(e.Message);
}
mod = mosel.LoadModel("foliocbio.bim"); // Load the model
// Pass model parameters through execution parameters
mod.SetExecParam("MAXRISK",maxrisk);
mod.SetExecParam("MINREG",minreg);
mod.SetExecParam("MAXREG",maxreg);
mod.SetExecParam("MAXSEC",maxsec);
mod.SetExecParam("MAXVAL",maxval);
mod.SetExecParam("MINVAL",minval);
mod.SetExecParam("MAXNUM",maxnum);
mod.SetExecParam("DATAFILE","rmt:folio250.dat");
mod.SetExecParam("OUTPUTFILE","dotnet:runfoliocbio_cbinit");
mod.Bind("runfoliocbio_cbinit", new XPRMInitializationTo(initializeTo));
mod.Run(); // Run the model
if(mod.ExecStatus != XPRMRunResult.RT_OK){
throw new Exception("Error during model execution");
}
if(mod.ProblemStatus != XPRMProblemStatus.PB_OPTIMAL){
throw new Exception("Problem not optimal");
} // Stop if no solution available
mod.Reset(); // Reset the model
}
}
}
|
|
runfolio.java |
/*******************************************************
Mosel Example Problems
======================
file runfolio.java
``````````````````
Running a Mosel model from a Java application
with data exchange between model and host application.
(Sparse data format using string indices)
(c) 2009 Fair Isaac Corporation
author: S. Heipcke, Jan. 2009
********************************************************/
import com.dashoptimization.*;
public class runfolio
{
// Classes to store initial values for data
public static class MyArrayTwo
{
public String ind1,ind2; // index names
public boolean val; // data value
MyArrayTwo(String i1, String i2, boolean v)
{ ind1=i1; ind2=i2; val=v; }
MyArrayTwo(String i1, String i2, int v)
{ ind1=i1; ind2=i2; val=(v==1); }
}
public static class MyArrayOne
{
public String ind; // index name
public double val; // data value
MyArrayOne(String i, double v)
{ ind=i; val=v; }
}
// Class to receive solution values of decision variables
public static class MySolArray
{
public String ind; // index name
public double val; // solution value
}
// Class with scalar solution information
// (scalars cannot be passed directly)
public static class MySol
{
public double objval; // objective function value
public int numshares; // number of shares in solution
public int status; // problem status
}
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
// Data for 'initializations from' (input data values)
String[] riskset = {"hardware1", "hardware2", "hardware3", "hardware4",
"hardware5", "hardware6", "hardware7", "hardware8", "hardware9",
"hardware10", "theater1", "theater2", "theater3", "theater4", "theater5",
"theater6", "theater7", "theater8", "theater9", "theater10", "telecom1",
"telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7",
"telecom8", "telecom9", "telecom10", "software1", "software2", "software3",
"software4", "software5", "software6", "software7", "software8",
"software9", "software10", "electronics1", "electronics2", "electronics3",
"electronics4", "electronics5", "electronics6", "electronics7",
"electronics8", "electronics9", "electronics10"};
MyArrayTwo locdata[] = {new MyArrayTwo("EU","treasury1",1),
new MyArrayTwo("EU","treasury10",1), new MyArrayTwo("EU","hardware7",1),
new MyArrayTwo("EU","hardware9",1), new MyArrayTwo("EU","theater8",1),
new MyArrayTwo("EU","theater10",1), new MyArrayTwo("EU","telecom3",1),
new MyArrayTwo("EU","telecom9",1), new MyArrayTwo("EU","telecom10",1),
new MyArrayTwo("EU","brewery1",1), new MyArrayTwo("EU","brewery4",1),
new MyArrayTwo("EU","brewery6",1), new MyArrayTwo("EU","brewery7",1),
new MyArrayTwo("EU","brewery8",1), new MyArrayTwo("EU","highways2",1),
new MyArrayTwo("EU","highways5",1), new MyArrayTwo("EU","highways7",1),
new MyArrayTwo("EU","highways10",1), new MyArrayTwo("EU","cars4",1),
new MyArrayTwo("EU","cars7",1), new MyArrayTwo("EU","cars8",1),
new MyArrayTwo("EU","cars9",1), new MyArrayTwo("EU","cars10",1),
new MyArrayTwo("EU","bank1",1), new MyArrayTwo("EU","bank3",1),
new MyArrayTwo("EU","bank5",1), new MyArrayTwo("EU","bank6",1),
new MyArrayTwo("EU","bank7",1), new MyArrayTwo("EU","bank10",1),
new MyArrayTwo("EU","software3",1), new MyArrayTwo("EU","software4",1),
new MyArrayTwo("EU","software5",1), new MyArrayTwo("EU","software6",1),
new MyArrayTwo("EU","electronics2",1), new MyArrayTwo("NA","treasury2",1),
new MyArrayTwo("NA","treasury6",1), new MyArrayTwo("NA","treasury7",1),
new MyArrayTwo("NA","treasury8",1), new MyArrayTwo("NA","hardware4",1),
new MyArrayTwo("NA","hardware6",1), new MyArrayTwo("NA","theater1",1),
new MyArrayTwo("NA","theater3",1), new MyArrayTwo("NA","theater7",1),
new MyArrayTwo("NA","theater9",1), new MyArrayTwo("NA","telecom2",1),
new MyArrayTwo("NA","telecom4",1), new MyArrayTwo("NA","telecom8",1),
new MyArrayTwo("NA","highways3",1), new MyArrayTwo("NA","highways4",1),
new MyArrayTwo("NA","cars5",1), new MyArrayTwo("NA","cars6",1),
new MyArrayTwo("NA","bank2",1), new MyArrayTwo("NA","bank4",1),
new MyArrayTwo("NA","bank8",1), new MyArrayTwo("NA","bank9",1),
new MyArrayTwo("NA","software1",1), new MyArrayTwo("NA","software2",1),
new MyArrayTwo("NA","software9",1), new MyArrayTwo("NA","electronics3",1),
new MyArrayTwo("NA","electronics5",1), new MyArrayTwo("NA","electronics6",1),
new MyArrayTwo("NA","electronics8",1), new MyArrayTwo("NA","electronics9",1),
new MyArrayTwo("NA","electronics10",1), new MyArrayTwo("APAC","treasury4",1),
new MyArrayTwo("APAC","treasury5",1), new MyArrayTwo("APAC","treasury9",1),
new MyArrayTwo("APAC","hardware1",1), new MyArrayTwo("APAC","hardware2",1),
new MyArrayTwo("APAC","hardware5",1), new MyArrayTwo("APAC","hardware8",1),
new MyArrayTwo("APAC","hardware10",1), new MyArrayTwo("APAC","theater2",1),
new MyArrayTwo("APAC","theater4",1), new MyArrayTwo("APAC","telecom1",1),
new MyArrayTwo("APAC","telecom5",1), new MyArrayTwo("APAC","telecom6",1),
new MyArrayTwo("APAC","telecom7",1), new MyArrayTwo("APAC","brewery9",1),
new MyArrayTwo("APAC","brewery10",1), new MyArrayTwo("APAC","highways8",1),
new MyArrayTwo("APAC","cars1",1), new MyArrayTwo("APAC","cars2",1),
new MyArrayTwo("APAC","software7",1), new MyArrayTwo("APAC","software8",1),
new MyArrayTwo("APAC","software10",1),
new MyArrayTwo("APAC","electronics1",1),
new MyArrayTwo("APAC","electronics4",1),
new MyArrayTwo("APAC","electronics7",1)};
MyArrayTwo secdata[] = {new MyArrayTwo("bonds","treasury1",1),
new MyArrayTwo("bonds","treasury10",1),
new MyArrayTwo("bonds","treasury2",1),
new MyArrayTwo("bonds","treasury6",1),
new MyArrayTwo("bonds","treasury7",1),
new MyArrayTwo("bonds","treasury8",1),
new MyArrayTwo("bonds","treasury4",1),
new MyArrayTwo("bonds","treasury5",1),
new MyArrayTwo("bonds","treasury9",1),
new MyArrayTwo("bonds","treasury3",1),
new MyArrayTwo("technology","hardware7",1),
new MyArrayTwo("technology","hardware9",1),
new MyArrayTwo("technology","software3",1),
new MyArrayTwo("technology","software4",1),
new MyArrayTwo("technology","software5",1),
new MyArrayTwo("technology","software6",1),
new MyArrayTwo("technology","electronics2",1),
new MyArrayTwo("technology","hardware4",1),
new MyArrayTwo("technology","hardware6",1),
new MyArrayTwo("technology","software1",1),
new MyArrayTwo("technology","software2",1),
new MyArrayTwo("technology","software9",1),
new MyArrayTwo("technology","electronics3",1),
new MyArrayTwo("technology","electronics5",1),
new MyArrayTwo("technology","electronics6",1),
new MyArrayTwo("technology","electronics8",1),
new MyArrayTwo("technology","electronics9",1),
new MyArrayTwo("technology","electronics10",1),
new MyArrayTwo("technology","hardware1",1),
new MyArrayTwo("technology","hardware2",1),
new MyArrayTwo("technology","hardware5",1),
new MyArrayTwo("technology","hardware8",1),
new MyArrayTwo("technology","hardware10",1),
new MyArrayTwo("technology","software7",1),
new MyArrayTwo("technology","software8",1),
new MyArrayTwo("technology","software10",1),
new MyArrayTwo("technology","electronics1",1),
new MyArrayTwo("technology","electronics4",1),
new MyArrayTwo("technology","electronics7",1),
new MyArrayTwo("technology","hardware3",1),
new MyArrayTwo("entertainment","theater8",1),
new MyArrayTwo("entertainment","theater10",1),
new MyArrayTwo("entertainment","theater1",1),
new MyArrayTwo("entertainment","theater3",1),
new MyArrayTwo("entertainment","theater7",1),
new MyArrayTwo("entertainment","theater9",1),
new MyArrayTwo("entertainment","theater2",1),
new MyArrayTwo("entertainment","theater4",1),
new MyArrayTwo("entertainment","theater5",1),
new MyArrayTwo("entertainment","theater6",1),
new MyArrayTwo("telecom","telecom3",1),
new MyArrayTwo("telecom","telecom9",1),
new MyArrayTwo("telecom","telecom10",1),
new MyArrayTwo("telecom","telecom2",1),
new MyArrayTwo("telecom","telecom4",1),
new MyArrayTwo("telecom","telecom8",1),
new MyArrayTwo("telecom","telecom1",1),
new MyArrayTwo("telecom","telecom5",1),
new MyArrayTwo("telecom","telecom6",1),
new MyArrayTwo("telecom","telecom7",1),
new MyArrayTwo("food","brewery1",1), new MyArrayTwo("food","brewery4",1),
new MyArrayTwo("food","brewery6",1), new MyArrayTwo("food","brewery7",1),
new MyArrayTwo("food","brewery8",1), new MyArrayTwo("food","brewery9",1),
new MyArrayTwo("food","brewery10",1), new MyArrayTwo("food","brewery2",1),
new MyArrayTwo("food","brewery3",1), new MyArrayTwo("food","brewery5",1),
new MyArrayTwo("construction","highways2",1),
new MyArrayTwo("construction","highways5",1),
new MyArrayTwo("construction","highways7",1),
new MyArrayTwo("construction","highways10",1),
new MyArrayTwo("construction","highways3",1),
new MyArrayTwo("construction","highways4",1),
new MyArrayTwo("construction","highways8",1),
new MyArrayTwo("construction","highways1",1),
new MyArrayTwo("construction","highways6",1),
new MyArrayTwo("construction","highways9",1),
new MyArrayTwo("manufacturing","cars4",1),
new MyArrayTwo("manufacturing","cars7",1),
new MyArrayTwo("manufacturing","cars8",1),
new MyArrayTwo("manufacturing","cars9",1),
new MyArrayTwo("manufacturing","cars10",1),
new MyArrayTwo("manufacturing","cars5",1),
new MyArrayTwo("manufacturing","cars6",1),
new MyArrayTwo("manufacturing","cars1",1),
new MyArrayTwo("manufacturing","cars2",1),
new MyArrayTwo("manufacturing","cars3",1),
new MyArrayTwo("finance","bank1",1), new MyArrayTwo("finance","bank3",1),
new MyArrayTwo("finance","bank5",1), new MyArrayTwo("finance","bank6",1),
new MyArrayTwo("finance","bank7",1), new MyArrayTwo("finance","bank10",1),
new MyArrayTwo("finance","bank2",1), new MyArrayTwo("finance","bank4",1),
new MyArrayTwo("finance","bank8",1), new MyArrayTwo("finance","bank9",1)};
MyArrayOne retdata[] = {new MyArrayOne("treasury1",5.29),
new MyArrayOne("treasury2",2.8), new MyArrayOne("treasury3",3.59),
new MyArrayOne("treasury4",6.97), new MyArrayOne("treasury5",3.3),
new MyArrayOne("treasury6",3.02), new MyArrayOne("treasury7",2.98),
new MyArrayOne("treasury8",3.41), new MyArrayOne("treasury9",3.93),
new MyArrayOne("treasury10",3.86), new MyArrayOne("hardware1",11.36),
new MyArrayOne("hardware2",17.23), new MyArrayOne("hardware3",16.31),
new MyArrayOne("hardware4",21.1), new MyArrayOne("hardware5",13.76),
new MyArrayOne("hardware6",9.31), new MyArrayOne("hardware7",16.99),
new MyArrayOne("hardware8",24.85), new MyArrayOne("hardware9",18.52),
new MyArrayOne("hardware10",12.79), new MyArrayOne("theater1",36.1),
new MyArrayOne("theater2",19.4), new MyArrayOne("theater3",23.5),
new MyArrayOne("theater4",34.61), new MyArrayOne("theater5",16.91),
new MyArrayOne("theater6",27.04), new MyArrayOne("theater7",25.82),
new MyArrayOne("theater8",24.99), new MyArrayOne("theater9",36.89),
new MyArrayOne("theater10",31.71), new MyArrayOne("telecom1",10.89),
new MyArrayOne("telecom2",17.98), new MyArrayOne("telecom3",12.31),
new MyArrayOne("telecom4",6.53), new MyArrayOne("telecom5",6.11),
new MyArrayOne("telecom6",15.89), new MyArrayOne("telecom7",12.46),
new MyArrayOne("telecom8",11.11), new MyArrayOne("telecom9",16.6),
new MyArrayOne("telecom10",13.93), new MyArrayOne("brewery1",8.79),
new MyArrayOne("brewery2",9.35), new MyArrayOne("brewery3",8.88),
new MyArrayOne("brewery4",11.63), new MyArrayOne("brewery5",6.21),
new MyArrayOne("brewery6",9.26), new MyArrayOne("brewery7",4.77),
new MyArrayOne("brewery8",5.98), new MyArrayOne("brewery9",10.92),
new MyArrayOne("brewery10",5.26), new MyArrayOne("highways1",5.39),
new MyArrayOne("highways2",4.85), new MyArrayOne("highways3",8.34),
new MyArrayOne("highways4",8.48), new MyArrayOne("highways5",6.42),
new MyArrayOne("highways6",11.04), new MyArrayOne("highways7",13.15),
new MyArrayOne("highways8",10.01), new MyArrayOne("highways9",7.71),
new MyArrayOne("highways10",11.07), new MyArrayOne("cars1",5.74),
new MyArrayOne("cars2",7.13), new MyArrayOne("cars3",8.75),
new MyArrayOne("cars4",5.08), new MyArrayOne("cars5",6.23),
new MyArrayOne("cars6",8.19), new MyArrayOne("cars7",8.03),
new MyArrayOne("cars8",5.96), new MyArrayOne("cars9",4.17),
new MyArrayOne("cars10",8.11), new MyArrayOne("bank1",6.79),
new MyArrayOne("bank2",3.01), new MyArrayOne("bank3",4.92),
new MyArrayOne("bank4",4.14), new MyArrayOne("bank5",8.98),
new MyArrayOne("bank6",8.81), new MyArrayOne("bank7",4.7),
new MyArrayOne("bank8",8.06), new MyArrayOne("bank9",6.22),
new MyArrayOne("bank10",4.47), new MyArrayOne("software1",34.59),
new MyArrayOne("software2",44.94), new MyArrayOne("software3",43.05),
new MyArrayOne("software4",15.58), new MyArrayOne("software5",42.05),
new MyArrayOne("software6",20.66), new MyArrayOne("software7",20.76),
new MyArrayOne("software8",19.85), new MyArrayOne("software9",20.05),
new MyArrayOne("software10",45.38), new MyArrayOne("electronics1",23.07),
new MyArrayOne("electronics2",19.1), new MyArrayOne("electronics3",23.83),
new MyArrayOne("electronics4",16.54), new MyArrayOne("electronics5",28.33),
new MyArrayOne("electronics6",25.88), new MyArrayOne("electronics7",22.14),
new MyArrayOne("electronics8",22.65), new MyArrayOne("electronics9",12.59),
new MyArrayOne("electronics10",28.1)};
// Data structures for 'initializations to' (solution)
MySol solution = new MySol();
MySolArray[] solfrac = new MySolArray[maxnum];
MySolArray[] solbuy = new MySolArray[maxnum];
for(int i=0;i<maxnum;i++)
{ solfrac[i] = new MySolArray(); solbuy[i] = new MySolArray(); }
try{
mosel = new XPRM(); // Initialize Mosel
}catch(XPRMLicenseError e){
System.out.println("License error" + e.getMessage());
throw new java.lang.Exception("Error during execution");
}
try{
mosel.compile("foliomemio.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMCompileException e){
System.out.println(e.getMessage());
}
mod = mosel.loadModel("foliomemio.bim"); // Load the model
// Associate the Java objects with names in Mosel
mosel.bind("risk", riskset);
mosel.bind("loc", locdata);
mosel.bind("sec", secdata);
mosel.bind("ret", retdata);
mosel.bind("sfrac", solfrac);
mosel.bind("sbuy", solbuy);
mosel.bind("sol", solution);
// Pass model parameters through execution parameters
mod.execParams = "MAXRISK=" + maxrisk + ",MINREG=" + minreg +
",MAXREG=" + maxreg + ",MAXSEC=" + maxsec + ",MAXVAL=" +
maxval + ",MINVAL=" + minval + ",MAXNUM=" + maxnum +
// File names (IO driver)
",DATAFILE='jraw:',OUTPUTFILE='jraw:'," +
// Data locations in memory
"RISKDATA='risk',LOCDATA='loc(ind1,ind2,val)'," +
"RETDATA='ret(ind,val)',SECDATA='sec(ind1,ind2,val)'," +
"FRACSOL='sfrac(ind,val)',BUYSOL='sbuy(ind,val)'," +
"RETSOL='sol(objval)',NUMSHARES='sol(numshares)'," +
"SOLSTATUS='sol(status)'";
mod.run(); // Run the model
if(mod.getExecStatus() != XPRMModel.RT_OK){
throw new java.lang.Exception("Error during model execution");
}
if(mod.getProblemStatus() != mod.PB_OPTIMAL){
throw new java.lang.Exception("Problem not optimal");
} // Stop if no solution available
// Display solution values obtained from the model
System.out.println("Total return: " + solution.objval);
// Alternatively:
// System.out.println("Objective value: " + mod.getObjectiveValue());
for(int i=0;i<solution.numshares;i++)
System.out.println(solfrac[i].ind + ": " + solfrac[i].val*100 + "% (" + solbuy[i].val + ")");
mod.reset(); // Reset the model
}
}
|
|
runfolio2.java |
/*******************************************************
Mosel Example Problems
======================
file runfolio2.java
```````````````````
Running a Mosel model from a Java application
with data exchange between model and host application.
(Sparse data format using string indices)
-- Grouping arrays with identical index sets --
(c) 2009 Fair Isaac Corporation
author: S. Heipcke, Jan. 2009
********************************************************/
import com.dashoptimization.*;
public class runfolio2
{
// Classes to store initial values for data
public static class MyArrayTwo
{
public String ind1,ind2; // index names
public boolean val; // data value
MyArrayTwo(String i1, String i2, boolean v)
{ ind1=i1; ind2=i2; val=v; }
MyArrayTwo(String i1, String i2, int v)
{ ind1=i1; ind2=i2; val=(v==1); }
}
public static class MyArrayOne
{
public String ind; // index name
public double val; // data value
MyArrayOne(String i, double v)
{ ind=i; val=v; }
}
// Class to receive solution values of decision variables
public static class MySolArray
{
public String ind; // index name
public double val1,val2; // solution values
}
// Class with scalar solution information
// (scalars cannot be passed directly)
public static class MySol
{
public double objval; // objective function value
public int numshares; // number of shares in solution
public int status; // problem status
}
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
// Data for 'initializations from' (input data values)
String[] riskset = {"hardware1", "hardware2", "hardware3", "hardware4",
"hardware5", "hardware6", "hardware7", "hardware8", "hardware9",
"hardware10", "theater1", "theater2", "theater3", "theater4", "theater5",
"theater6", "theater7", "theater8", "theater9", "theater10", "telecom1",
"telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7",
"telecom8", "telecom9", "telecom10", "software1", "software2", "software3",
"software4", "software5", "software6", "software7", "software8",
"software9", "software10", "electronics1", "electronics2", "electronics3",
"electronics4", "electronics5", "electronics6", "electronics7",
"electronics8", "electronics9", "electronics10"};
MyArrayTwo locdata[] = {new MyArrayTwo("EU","treasury1",1),
new MyArrayTwo("EU","treasury10",1), new MyArrayTwo("EU","hardware7",1),
new MyArrayTwo("EU","hardware9",1), new MyArrayTwo("EU","theater8",1),
new MyArrayTwo("EU","theater10",1), new MyArrayTwo("EU","telecom3",1),
new MyArrayTwo("EU","telecom9",1), new MyArrayTwo("EU","telecom10",1),
new MyArrayTwo("EU","brewery1",1), new MyArrayTwo("EU","brewery4",1),
new MyArrayTwo("EU","brewery6",1), new MyArrayTwo("EU","brewery7",1),
new MyArrayTwo("EU","brewery8",1), new MyArrayTwo("EU","highways2",1),
new MyArrayTwo("EU","highways5",1), new MyArrayTwo("EU","highways7",1),
new MyArrayTwo("EU","highways10",1), new MyArrayTwo("EU","cars4",1),
new MyArrayTwo("EU","cars7",1), new MyArrayTwo("EU","cars8",1),
new MyArrayTwo("EU","cars9",1), new MyArrayTwo("EU","cars10",1),
new MyArrayTwo("EU","bank1",1), new MyArrayTwo("EU","bank3",1),
new MyArrayTwo("EU","bank5",1), new MyArrayTwo("EU","bank6",1),
new MyArrayTwo("EU","bank7",1), new MyArrayTwo("EU","bank10",1),
new MyArrayTwo("EU","software3",1), new MyArrayTwo("EU","software4",1),
new MyArrayTwo("EU","software5",1), new MyArrayTwo("EU","software6",1),
new MyArrayTwo("EU","electronics2",1), new MyArrayTwo("NA","treasury2",1),
new MyArrayTwo("NA","treasury6",1), new MyArrayTwo("NA","treasury7",1),
new MyArrayTwo("NA","treasury8",1), new MyArrayTwo("NA","hardware4",1),
new MyArrayTwo("NA","hardware6",1), new MyArrayTwo("NA","theater1",1),
new MyArrayTwo("NA","theater3",1), new MyArrayTwo("NA","theater7",1),
new MyArrayTwo("NA","theater9",1), new MyArrayTwo("NA","telecom2",1),
new MyArrayTwo("NA","telecom4",1), new MyArrayTwo("NA","telecom8",1),
new MyArrayTwo("NA","highways3",1), new MyArrayTwo("NA","highways4",1),
new MyArrayTwo("NA","cars5",1), new MyArrayTwo("NA","cars6",1),
new MyArrayTwo("NA","bank2",1), new MyArrayTwo("NA","bank4",1),
new MyArrayTwo("NA","bank8",1), new MyArrayTwo("NA","bank9",1),
new MyArrayTwo("NA","software1",1), new MyArrayTwo("NA","software2",1),
new MyArrayTwo("NA","software9",1), new MyArrayTwo("NA","electronics3",1),
new MyArrayTwo("NA","electronics5",1), new MyArrayTwo("NA","electronics6",1),
new MyArrayTwo("NA","electronics8",1), new MyArrayTwo("NA","electronics9",1),
new MyArrayTwo("NA","electronics10",1), new MyArrayTwo("APAC","treasury4",1),
new MyArrayTwo("APAC","treasury5",1), new MyArrayTwo("APAC","treasury9",1),
new MyArrayTwo("APAC","hardware1",1), new MyArrayTwo("APAC","hardware2",1),
new MyArrayTwo("APAC","hardware5",1), new MyArrayTwo("APAC","hardware8",1),
new MyArrayTwo("APAC","hardware10",1), new MyArrayTwo("APAC","theater2",1),
new MyArrayTwo("APAC","theater4",1), new MyArrayTwo("APAC","telecom1",1),
new MyArrayTwo("APAC","telecom5",1), new MyArrayTwo("APAC","telecom6",1),
new MyArrayTwo("APAC","telecom7",1), new MyArrayTwo("APAC","brewery9",1),
new MyArrayTwo("APAC","brewery10",1), new MyArrayTwo("APAC","highways8",1),
new MyArrayTwo("APAC","cars1",1), new MyArrayTwo("APAC","cars2",1),
new MyArrayTwo("APAC","software7",1), new MyArrayTwo("APAC","software8",1),
new MyArrayTwo("APAC","software10",1),
new MyArrayTwo("APAC","electronics1",1),
new MyArrayTwo("APAC","electronics4",1),
new MyArrayTwo("APAC","electronics7",1)};
MyArrayTwo secdata[] = {new MyArrayTwo("bonds","treasury1",1),
new MyArrayTwo("bonds","treasury10",1),
new MyArrayTwo("bonds","treasury2",1),
new MyArrayTwo("bonds","treasury6",1),
new MyArrayTwo("bonds","treasury7",1),
new MyArrayTwo("bonds","treasury8",1),
new MyArrayTwo("bonds","treasury4",1),
new MyArrayTwo("bonds","treasury5",1),
new MyArrayTwo("bonds","treasury9",1),
new MyArrayTwo("bonds","treasury3",1),
new MyArrayTwo("technology","hardware7",1),
new MyArrayTwo("technology","hardware9",1),
new MyArrayTwo("technology","software3",1),
new MyArrayTwo("technology","software4",1),
new MyArrayTwo("technology","software5",1),
new MyArrayTwo("technology","software6",1),
new MyArrayTwo("technology","electronics2",1),
new MyArrayTwo("technology","hardware4",1),
new MyArrayTwo("technology","hardware6",1),
new MyArrayTwo("technology","software1",1),
new MyArrayTwo("technology","software2",1),
new MyArrayTwo("technology","software9",1),
new MyArrayTwo("technology","electronics3",1),
new MyArrayTwo("technology","electronics5",1),
new MyArrayTwo("technology","electronics6",1),
new MyArrayTwo("technology","electronics8",1),
new MyArrayTwo("technology","electronics9",1),
new MyArrayTwo("technology","electronics10",1),
new MyArrayTwo("technology","hardware1",1),
new MyArrayTwo("technology","hardware2",1),
new MyArrayTwo("technology","hardware5",1),
new MyArrayTwo("technology","hardware8",1),
new MyArrayTwo("technology","hardware10",1),
new MyArrayTwo("technology","software7",1),
new MyArrayTwo("technology","software8",1),
new MyArrayTwo("technology","software10",1),
new MyArrayTwo("technology","electronics1",1),
new MyArrayTwo("technology","electronics4",1),
new MyArrayTwo("technology","electronics7",1),
new MyArrayTwo("technology","hardware3",1),
new MyArrayTwo("entertainment","theater8",1),
new MyArrayTwo("entertainment","theater10",1),
new MyArrayTwo("entertainment","theater1",1),
new MyArrayTwo("entertainment","theater3",1),
new MyArrayTwo("entertainment","theater7",1),
new MyArrayTwo("entertainment","theater9",1),
new MyArrayTwo("entertainment","theater2",1),
new MyArrayTwo("entertainment","theater4",1),
new MyArrayTwo("entertainment","theater5",1),
new MyArrayTwo("entertainment","theater6",1),
new MyArrayTwo("telecom","telecom3",1),
new MyArrayTwo("telecom","telecom9",1),
new MyArrayTwo("telecom","telecom10",1),
new MyArrayTwo("telecom","telecom2",1),
new MyArrayTwo("telecom","telecom4",1),
new MyArrayTwo("telecom","telecom8",1),
new MyArrayTwo("telecom","telecom1",1),
new MyArrayTwo("telecom","telecom5",1),
new MyArrayTwo("telecom","telecom6",1),
new MyArrayTwo("telecom","telecom7",1),
new MyArrayTwo("food","brewery1",1), new MyArrayTwo("food","brewery4",1),
new MyArrayTwo("food","brewery6",1), new MyArrayTwo("food","brewery7",1),
new MyArrayTwo("food","brewery8",1), new MyArrayTwo("food","brewery9",1),
new MyArrayTwo("food","brewery10",1), new MyArrayTwo("food","brewery2",1),
new MyArrayTwo("food","brewery3",1), new MyArrayTwo("food","brewery5",1),
new MyArrayTwo("construction","highways2",1),
new MyArrayTwo("construction","highways5",1),
new MyArrayTwo("construction","highways7",1),
new MyArrayTwo("construction","highways10",1),
new MyArrayTwo("construction","highways3",1),
new MyArrayTwo("construction","highways4",1),
new MyArrayTwo("construction","highways8",1),
new MyArrayTwo("construction","highways1",1),
new MyArrayTwo("construction","highways6",1),
new MyArrayTwo("construction","highways9",1),
new MyArrayTwo("manufacturing","cars4",1),
new MyArrayTwo("manufacturing","cars7",1),
new MyArrayTwo("manufacturing","cars8",1),
new MyArrayTwo("manufacturing","cars9",1),
new MyArrayTwo("manufacturing","cars10",1),
new MyArrayTwo("manufacturing","cars5",1),
new MyArrayTwo("manufacturing","cars6",1),
new MyArrayTwo("manufacturing","cars1",1),
new MyArrayTwo("manufacturing","cars2",1),
new MyArrayTwo("manufacturing","cars3",1),
new MyArrayTwo("finance","bank1",1), new MyArrayTwo("finance","bank3",1),
new MyArrayTwo("finance","bank5",1), new MyArrayTwo("finance","bank6",1),
new MyArrayTwo("finance","bank7",1), new MyArrayTwo("finance","bank10",1),
new MyArrayTwo("finance","bank2",1), new MyArrayTwo("finance","bank4",1),
new MyArrayTwo("finance","bank8",1), new MyArrayTwo("finance","bank9",1)};
MyArrayOne retdata[] = {new MyArrayOne("treasury1",5.29),
new MyArrayOne("treasury2",2.8), new MyArrayOne("treasury3",3.59),
new MyArrayOne("treasury4",6.97), new MyArrayOne("treasury5",3.3),
new MyArrayOne("treasury6",3.02), new MyArrayOne("treasury7",2.98),
new MyArrayOne("treasury8",3.41), new MyArrayOne("treasury9",3.93),
new MyArrayOne("treasury10",3.86), new MyArrayOne("hardware1",11.36),
new MyArrayOne("hardware2",17.23), new MyArrayOne("hardware3",16.31),
new MyArrayOne("hardware4",21.1), new MyArrayOne("hardware5",13.76),
new MyArrayOne("hardware6",9.31), new MyArrayOne("hardware7",16.99),
new MyArrayOne("hardware8",24.85), new MyArrayOne("hardware9",18.52),
new MyArrayOne("hardware10",12.79), new MyArrayOne("theater1",36.1),
new MyArrayOne("theater2",19.4), new MyArrayOne("theater3",23.5),
new MyArrayOne("theater4",34.61), new MyArrayOne("theater5",16.91),
new MyArrayOne("theater6",27.04), new MyArrayOne("theater7",25.82),
new MyArrayOne("theater8",24.99), new MyArrayOne("theater9",36.89),
new MyArrayOne("theater10",31.71), new MyArrayOne("telecom1",10.89),
new MyArrayOne("telecom2",17.98), new MyArrayOne("telecom3",12.31),
new MyArrayOne("telecom4",6.53), new MyArrayOne("telecom5",6.11),
new MyArrayOne("telecom6",15.89), new MyArrayOne("telecom7",12.46),
new MyArrayOne("telecom8",11.11), new MyArrayOne("telecom9",16.6),
new MyArrayOne("telecom10",13.93), new MyArrayOne("brewery1",8.79),
new MyArrayOne("brewery2",9.35), new MyArrayOne("brewery3",8.88),
new MyArrayOne("brewery4",11.63), new MyArrayOne("brewery5",6.21),
new MyArrayOne("brewery6",9.26), new MyArrayOne("brewery7",4.77),
new MyArrayOne("brewery8",5.98), new MyArrayOne("brewery9",10.92),
new MyArrayOne("brewery10",5.26), new MyArrayOne("highways1",5.39),
new MyArrayOne("highways2",4.85), new MyArrayOne("highways3",8.34),
new MyArrayOne("highways4",8.48), new MyArrayOne("highways5",6.42),
new MyArrayOne("highways6",11.04), new MyArrayOne("highways7",13.15),
new MyArrayOne("highways8",10.01), new MyArrayOne("highways9",7.71),
new MyArrayOne("highways10",11.07), new MyArrayOne("cars1",5.74),
new MyArrayOne("cars2",7.13), new MyArrayOne("cars3",8.75),
new MyArrayOne("cars4",5.08), new MyArrayOne("cars5",6.23),
new MyArrayOne("cars6",8.19), new MyArrayOne("cars7",8.03),
new MyArrayOne("cars8",5.96), new MyArrayOne("cars9",4.17),
new MyArrayOne("cars10",8.11), new MyArrayOne("bank1",6.79),
new MyArrayOne("bank2",3.01), new MyArrayOne("bank3",4.92),
new MyArrayOne("bank4",4.14), new MyArrayOne("bank5",8.98),
new MyArrayOne("bank6",8.81), new MyArrayOne("bank7",4.7),
new MyArrayOne("bank8",8.06), new MyArrayOne("bank9",6.22),
new MyArrayOne("bank10",4.47), new MyArrayOne("software1",34.59),
new MyArrayOne("software2",44.94), new MyArrayOne("software3",43.05),
new MyArrayOne("software4",15.58), new MyArrayOne("software5",42.05),
new MyArrayOne("software6",20.66), new MyArrayOne("software7",20.76),
new MyArrayOne("software8",19.85), new MyArrayOne("software9",20.05),
new MyArrayOne("software10",45.38), new MyArrayOne("electronics1",23.07),
new MyArrayOne("electronics2",19.1), new MyArrayOne("electronics3",23.83),
new MyArrayOne("electronics4",16.54), new MyArrayOne("electronics5",28.33),
new MyArrayOne("electronics6",25.88), new MyArrayOne("electronics7",22.14),
new MyArrayOne("electronics8",22.65), new MyArrayOne("electronics9",12.59),
new MyArrayOne("electronics10",28.1)};
// Data structures for 'initializations to' (solution)
MySol solution = new MySol();
MySolArray[] sol = new MySolArray[maxnum];
for(int i=0;i<maxnum;i++)
sol[i] = new MySolArray();
try{
mosel = new XPRM(); // Initialize Mosel
}catch(XPRMLicenseError e){
System.out.println("License error" + e.getMessage());
throw new java.lang.Exception("Error during execution");
}
try{
mosel.compile("foliomemio2.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMCompileException e){
System.out.println(e.getMessage());
}
mod = mosel.loadModel("foliomemio2.bim"); // Load the model
// Associate the Java objects with names in Mosel
mosel.bind("risk", riskset);
mosel.bind("loc", locdata);
mosel.bind("sec", secdata);
mosel.bind("ret", retdata);
mosel.bind("sfracbuy", sol);
mosel.bind("sol", solution);
// Pass model parameters through execution parameters
mod.execParams = "MAXRISK=" + maxrisk + ",MINREG=" + minreg +
",MAXREG=" + maxreg + ",MAXSEC=" + maxsec + ",MAXVAL=" +
maxval + ",MINVAL=" + minval + ",MAXNUM=" + maxnum +
// File names (IO driver)
",DATAFILE='jraw:',OUTPUTFILE='jraw:'," +
// Data locations in memory
"RISKDATA='risk',LOCDATA='loc(ind1,ind2,val)'," +
"RETDATA='ret(ind,val)',SECDATA='sec(ind1,ind2,val)'," +
"FRACBUYSOL='sfracbuy(ind,val1,val2)'," +
"RETSOL='sol(objval)',NUMSHARES='sol(numshares)'," +
"SOLSTATUS='sol(status)'";
mod.run(); // Run the model
if(mod.getExecStatus() != XPRMModel.RT_OK){
throw new java.lang.Exception("Error during model execution");
}
if(mod.getProblemStatus() != mod.PB_OPTIMAL){
throw new java.lang.Exception("Problem not optimal");
} // Stop if no solution available
// Display solution values obtained from the model
System.out.println("Total return: " + solution.objval);
// Alternatively:
// System.out.println("Objective value: " + mod.getObjectiveValue());
for(int i=0;i<solution.numshares;i++)
System.out.println(sol[i].ind + ": " + sol[i].val1*100 + "% (" + sol[i].val2 + ")");
mod.reset(); // Reset the model
}
}
|
|
runfoliob.java |
/*******************************************************
Mosel Example Problems
======================
file runfoliob.java
```````````````````
Running a Mosel model from a Java application
with data exchange between model and host application.
(Passing data through buffers)
(c) 2009 Fair Isaac Corporation
author: S. Heipcke, Mar. 2009
********************************************************/
import java.io.*;
import java.nio.*;
import com.dashoptimization.*;
public class runfoliob
{
private static final String DATAFILE = "./folio10.dat";
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
// Input and output handling
ByteBuffer inputData;
ByteBuffer resultOutput;
StringBuffer dataBuffer = new StringBuffer();
int last;
byte[] output;
// Read input data from text file in Mosel format
BufferedReader filereader = new BufferedReader(new FileReader(DATAFILE));
String dataLine = filereader.readLine();
while(dataLine != null)
{
dataBuffer.append(dataLine); // For general case: add separator (space)
dataLine = filereader.readLine();
}
filereader.close();
inputData = ByteBuffer.wrap(dataBuffer.toString().getBytes());
resultOutput = ByteBuffer.allocate(4096); // Create 4K byte buffer
try{
mosel = new XPRM(); // Initialize Mosel
}catch(XPRMLicenseError e){
System.out.println("License error" + e.getMessage());
throw new java.lang.Exception("Error during execution");
}
try{
mosel.compile("foliomemio.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMCompileException e){
System.out.println(e.getMessage());
}
mod = mosel.loadModel("foliomemio.bim"); // Load the model
// Associate the Java objects with names in Mosel
mosel.bind("inputdata", inputData);
mosel.bind("outputdata", resultOutput);
// Pass model parameters through execution parameters
mod.execParams = "MAXRISK=" + maxrisk + ",MINREG=" + minreg +
",MAXREG=" + maxreg + ",MAXSEC=" + maxsec + ",MAXVAL=" +
maxval + ",MINVAL=" + minval + ",MAXNUM=" + maxnum +
// File names (IO driver)
",DATAFILE='java:inputdata',OUTPUTFILE='java:outputdata',";
mod.run(); // Run the model
if(mod.getExecStatus() != XPRMModel.RT_OK){
throw new java.lang.Exception("Error during model execution");
}
if(mod.getProblemStatus() != mod.PB_OPTIMAL){
throw new java.lang.Exception("Problem not optimal");
} // Stop if no solution available
// Display result output obtained from the model
System.out.println("Solution:");
last = resultOutput.position();
resultOutput.rewind();
output = resultOutput.array();
for (int i=0; i<last; i++)
System.out.print((char)output[i]);
mod.reset(); // Reset the model
}
}
|
|
runfoliod.java |
/*******************************************************
Mosel Example Problems
======================
file runfoliod.java
```````````````````
Running a Mosel model from a Java application
with data exchange between model and host application.
(Passing data via callback)
(c) 2009 Fair Isaac Corporation
author: S. Heipcke, Oct. 2009
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class runfoliod
{
// Data for 'initializations from' (input data values)
static final String[] riskset = {"hardware1", "hardware2", "hardware3", "hardware4",
"hardware5", "hardware6", "hardware7", "hardware8", "hardware9",
"hardware10", "theater1", "theater2", "theater3", "theater4", "theater5",
"theater6", "theater7", "theater8", "theater9", "theater10", "telecom1",
"telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7",
"telecom8", "telecom9", "telecom10", "software1", "software2", "software3",
"software4", "software5", "software6", "software7", "software8",
"software9", "software10", "electronics1", "electronics2", "electronics3",
"electronics4", "electronics5", "electronics6", "electronics7",
"electronics8", "electronics9", "electronics10"};
static final int risksize=50;
static final String[] locdataind1 = {"EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "EU", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC", "APAC"};
static final String[] locdataind2 = {"treasury1", "treasury10", "hardware7", "hardware9", "theater8", "theater10", "telecom3", "telecom9", "telecom10", "brewery1", "brewery4", "brewery6", "brewery7", "brewery8", "highways2", "highways5", "highways7", "highways10", "cars4", "cars7", "cars8", "cars9", "cars10", "bank1", "bank3", "bank5", "bank6", "bank7", "bank10", "software3", "software4", "software5", "software6", "electronics2", "treasury2", "treasury6", "treasury7", "treasury8", "hardware4", "hardware6", "theater1", "theater3", "theater7", "theater9", "telecom2", "telecom4", "telecom8", "highways3", "highways4", "cars5", "cars6", "bank2", "bank4", "bank8", "bank9", "software1", "software2", "software9", "electronics3", "electronics5", "electronics6", "electronics8", "electronics9", "electronics10", "treasury4", "treasury5", "treasury9", "hardware1", "hardware2", "hardware5", "hardware8", "hardware10", "theater2", "theater4", "telecom1", "telecom5", "telecom6", "telecom7", "brewery9", "brewery10", "highways8", "cars1", "cars2", "software7", "software8", "software10", "electronics1", "electronics4", "electronics7"};
static final int[] locdata = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
static final int locsize=89;
static final String[] secdataind1 = {"bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "bonds", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "technology", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "entertainment", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "telecom", "food", "food", "food", "food", "food", "food", "food", "food", "food", "food", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "construction", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "manufacturing", "finance", "finance", "finance", "finance", "finance", "finance", "finance", "finance", "finance", "finance"};
static final String[] secdataind2 = {"treasury1", "treasury2", "treasury3", "treasury4", "treasury5", "treasury6", "treasury7", "treasury8", "treasury9", "treasury10", "hardware1", "hardware2", "hardware3", "hardware4", "hardware5", "hardware6", "hardware7", "hardware8", "hardware9", "hardware10", "software1", "software2", "software3", "software4", "software5", "software6", "software7", "software8", "software9", "software10", "electronics1", "electronics2", "electronics3", "electronics4", "electronics5", "electronics6", "electronics7", "electronics8", "electronics9", "electronics10", "theater1", "theater2", "theater3", "theater4", "theater5", "theater6", "theater7", "theater8", "theater9", "theater10", "telecom1", "telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7", "telecom8", "telecom9", "telecom10", "brewery1", "brewery2", "brewery3", "brewery4", "brewery5", "brewery6", "brewery7", "brewery8", "brewery9", "brewery10", "highways1", "highways2", "highways3", "highways4", "highways5", "highways6", "highways7", "highways8", "highways9", "highways10", "cars1", "cars2", "cars3", "cars4", "cars5", "cars6", "cars7", "cars8", "cars9", "cars10", "bank1", "bank2", "bank3", "bank4", "bank5", "bank6", "bank7", "bank8", "bank9", "bank10"};
static final double[] secdata = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
static final int secsize=100;
static final String[] retdataind = {"treasury1", "treasury2", "treasury3", "treasury4", "treasury5", "treasury6", "treasury7", "treasury8", "treasury9", "treasury10", "hardware1", "hardware2", "hardware3", "hardware4", "hardware5", "hardware6", "hardware7", "hardware8", "hardware9", "hardware10", "theater1", "theater2", "theater3", "theater4", "theater5", "theater6", "theater7", "theater8", "theater9", "theater10", "telecom1", "telecom2", "telecom3", "telecom4", "telecom5", "telecom6", "telecom7", "telecom8", "telecom9", "telecom10", "brewery1", "brewery2", "brewery3", "brewery4", "brewery5", "brewery6", "brewery7", "brewery8", "brewery9", "brewery10", "highways1", "highways2", "highways3", "highways4", "highways5", "highways6", "highways7", "highways8", "highways9", "highways10", "cars1", "cars2", "cars3", "cars4", "cars5", "cars6", "cars7", "cars8", "cars9", "cars10", "bank1", "bank2", "bank3", "bank4", "bank5", "bank6", "bank7", "bank8", "bank9", "bank10", "software1", "software2", "software3", "software4", "software5", "software6", "software7", "software8", "software9", "software10", "electronics1", "electronics2", "electronics3", "electronics4", "electronics5", "electronics6", "electronics7", "electronics8", "electronics9", "electronics10"};
static final double[] retdata = {5.29, 2.8, 3.59, 6.97, 3.3, 3.02, 2.98, 3.41, 3.93, 3.86, 11.36, 17.23, 16.31, 21.1, 13.76, 9.31, 16.99, 24.85, 18.52, 12.79, 36.1, 19.4, 23.5, 34.61, 16.91, 27.04, 25.82, 24.99, 36.89, 31.71, 10.89, 17.98, 12.31, 6.53, 6.11, 15.89, 12.46, 11.11, 16.6, 13.93, 8.79, 9.35, 8.88, 11.63, 6.21, 9.26, 4.77, 5.98, 10.92, 5.26, 5.39, 4.85, 8.34, 8.48, 6.42, 11.04, 13.15, 10.01, 7.71, 11.07, 5.74, 7.13, 8.75, 5.08, 6.23, 8.19, 8.03, 5.96, 4.17, 8.11, 6.79, 3.01, 4.92, 4.14, 8.98, 8.81, 4.7, 8.06, 6.22, 4.47, 34.59, 44.94, 43.05, 15.58, 42.05, 20.66, 20.76, 19.85, 20.05, 45.38, 23.07, 19.1, 23.83, 16.54, 28.33, 25.88, 22.14, 22.65, 12.59, 28.1};
static final int retsize=100;
// Class to receive solution values of decision variables
public static class MySolArray
{
public String ind; // index name
public double val; // solution value
}
static double retsol;
static int numshares, solstatus;
static MySolArray[] solfrac;
static MySolArray[] solbuy;
/*************************************************/
/* A class to initialize model data via callback */
/*************************************************/
public static class modelInit implements XPRMInitializationFrom, XPRMInitializationTo
{
public boolean initializeFrom(XPRMInitializeContext ictx, String label, XPRMTyped type)
{
int i;
try
{
if(label.equals("RISK"))
{
ictx.sendControl(ictx.CONTROL_OPENLST);
for(i=0;i<risksize;i++)
{
ictx.send(riskset[i]);
}
ictx.sendControl(ictx.CONTROL_CLOSELST);
return true;
}
else
if(label.equals("RET"))
{
ictx.sendControl(ictx.CONTROL_OPENLST);
for(i=0;i<retsize;i++)
{
ictx.sendControl(ictx.CONTROL_OPENNDX);
ictx.send(retdataind[i]);
ictx.sendControl(ictx.CONTROL_CLOSENDX);
ictx.send(retdata[i]);
}
ictx.sendControl(ictx.CONTROL_CLOSELST);
return true;
}
else
if(label.equals("LOCTAB"))
{
ictx.sendControl(ictx.CONTROL_OPENLST);
for(i=0;i<locsize;i++)
{
ictx.sendControl(ictx.CONTROL_OPENNDX);
ictx.send(locdataind1[i]);
ictx.send(locdataind2[i]);
ictx.sendControl(ictx.CONTROL_CLOSENDX);
ictx.send(locdata[i]);
}
ictx.sendControl(ictx.CONTROL_CLOSELST);
return true;
}
else
if(label.equals("SECTAB"))
{
ictx.sendControl(ictx.CONTROL_OPENLST);
for(i=0;i<secsize;i++)
{
ictx.sendControl(ictx.CONTROL_OPENNDX);
ictx.send(secdataind1[i]);
ictx.send(secdataind2[i]);
ictx.sendControl(ictx.CONTROL_CLOSENDX);
ictx.send(secdata[i]);
}
ictx.sendControl(ictx.CONTROL_CLOSELST);
return true;
}
else
{
System.err.println("Label `"+label+"' not found.");
return false;
}
}
catch(IOException e)
{
System.err.println("`"+label+"' could not be initialized - "+e);
return false;
}
}
/**** Retrieving data from Mosel ****/
public boolean initializeTo(String label, XPRMValue value)
{
XPRMArray solarr;
XPRMSet[] sets;
int[] indices;
int asize, ct;
if(label.equals("FRAC"))
{
solarr=(XPRMArray)value;
asize=solarr.getSize();
solfrac = new MySolArray[asize];
for(int i=0;i<asize;i++) solfrac[i] = new MySolArray();
sets = solarr.getIndexSets(); // Get the indexing sets
ct=0;
indices = solarr.getFirstTEIndex(); // Get the first entry of the array
do
{
solfrac[ct].ind=sets[0].getAsString(indices[0]);
solfrac[ct].val=solarr.getAsReal(indices);
ct++;
} while(solarr.nextTEIndex(indices)); // Get the next index
}
else if(label.equals("BUY"))
{
solarr=(XPRMArray)value;
asize=solarr.getSize();
solbuy = new MySolArray[asize];
for(int i=0;i<asize;i++) solbuy[i] = new MySolArray();
sets = solarr.getIndexSets(); // Get the indexing sets
ct=0;
indices = solarr.getFirstTEIndex(); // Get the first entry of the array
do
{
solbuy[ct].ind=sets[0].getAsString(indices[0]);
solbuy[ct].val=solarr.getAsReal(indices);
ct++;
} while(solarr.nextTEIndex(indices)); // Get the next index
}
else if(label.equals("RETSOL"))
{
retsol=value.asReal();
}
else if(label.equals("NUMSHARES"))
{
numshares=value.asInteger();
}
else if(label.equals("SOLSTATUS"))
{
solstatus=value.asInteger();
}
else System.out.println("Unknown output data item: " + label + "=" + value);
return true;
}
}
/*************************************************/
/* Interface objects are static: no need to bind */
/*************************************************/
public static modelInit cbinit=new modelInit();
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
try{
mosel = new XPRM(); // Initialize Mosel
}catch(XPRMLicenseError e){
System.out.println("License error" + e.getMessage());
throw new java.lang.Exception("Error during execution");
}
try{
mosel.compile("foliomemio.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMCompileException e){
System.out.println(e.getMessage());
}
mod = mosel.loadModel("foliomemio.bim"); // Load the model
// Pass model parameters through execution parameters
mod.execParams = "MAXRISK=" + maxrisk + ",MINREG=" + minreg +
",MAXREG=" + maxreg + ",MAXSEC=" + maxsec + ",MAXVAL=" +
maxval + ",MINVAL=" + minval + ",MAXNUM=" + maxnum +
// File names (IO driver)
",DATAFILE='java:runfoliod.cbinit'," +
"OUTPUTFILE='java:runfoliod.cbinit'";
mod.run(); // Run the model
if(mod.getExecStatus() != XPRMModel.RT_OK){
throw new java.lang.Exception("Error during model execution");
}
if(mod.getProblemStatus() != mod.PB_OPTIMAL){
throw new java.lang.Exception("Problem not optimal");
} // Stop if no solution available
// Display solution values obtained from the model
System.out.println("Total return: " + retsol);
for(int i=0;i<numshares;i++)
System.out.println(solfrac[i].ind + ": " + solfrac[i].val*100 + "% (" + solbuy[i].val + ")");
mod.reset(); // Reset the model
}
}
|
|
runfoliocbio.java |
/*******************************************************
Mosel Example Problems
======================
file runfoliocbio.java
``````````````````````
Running a Mosel model from a Java application
with data exchange between model and host application
during the optimization run.
(Passing data via callback)
*** The model started by this program cannot be run with
a Community Licence for the provided data instance ***
(c) 2011 Fair Isaac Corporation
author: S. Heipcke, July 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class runfoliocbio
{
// Class to receive solution values of decision variables
public static class MySolArray
{
public String ind; // index name
public double val; // solution value
}
static MySolArray[] solfrac;
static MySolArray[] solbuy;
static boolean ifprint;
/*************************************************/
/* A class to initialize model data via callback */
/*************************************************/
public static class modelInit implements XPRMInitializationTo
{
/**** Retrieving data from Mosel ****/
public boolean initializeTo(String label, XPRMValue value)
{
double retsol;
int numshares, solcount;
XPRMArray solarr;
XPRMSet[] sets;
int[] indices;
int asize, ct;
if(label.equals("FRAC"))
{
solarr=(XPRMArray)value;
asize=solarr.getSize();
solfrac = new MySolArray[asize];
for(int i=0;i<asize;i++) solfrac[i] = new MySolArray();
sets = solarr.getIndexSets(); // Get the indexing sets
ct=0;
indices = solarr.getFirstTEIndex(); // Get the first entry of the array
do
{
solfrac[ct].ind=sets[0].getAsString(indices[0]);
solfrac[ct].val=solarr.getAsReal(indices);
ct++;
} while(solarr.nextTEIndex(indices)); // Get the next index
if (ifprint==false)
{ ifprint=true; }
else
{
ifprint=false;
for(int i=0;i<asize;i++)
System.out.println(solfrac[i].ind + ": " + solfrac[i].val*100 + "% (" +
solbuy[i].val + ")");
solbuy = null;
solfrac = null;
}
}
else if(label.equals("BUY"))
{
solarr=(XPRMArray)value;
asize=solarr.getSize();
solbuy = new MySolArray[asize];
for(int i=0;i<asize;i++) solbuy[i] = new MySolArray();
sets = solarr.getIndexSets(); // Get the indexing sets
ct=0;
indices = solarr.getFirstTEIndex(); // Get the first entry of the array
do
{
solbuy[ct].ind=sets[0].getAsString(indices[0]);
solbuy[ct].val=solarr.getAsReal(indices);
ct++;
} while(solarr.nextTEIndex(indices)); // Get the next index
if (ifprint==false)
{ ifprint=true; }
else
{
ifprint=false;
for(int i=0;i<asize;i++)
System.out.println(solfrac[i].ind + ": " + solfrac[i].val*100 + "% (" +
solbuy[i].val + ")");
solbuy = null;
solfrac = null;
}
}
else if(label.equals("RETSOL"))
{
retsol=value.asReal();
System.out.println("Total return: " + retsol);
}
else if(label.equals("NUMSHARES"))
{
numshares=value.asInteger();
System.out.println("Number of shares: " + numshares);
}
else if(label.equals("SOLCOUNT"))
{
solcount=value.asInteger();
System.out.println("Solution number: " + solcount);
}
else System.out.println("Unknown output data item: " + label + "=" + value);
return true;
}
}
/*************************************************/
/* Interface objects are static: no need to bind */
/*************************************************/
public static modelInit cbinit=new modelInit();
public static void main(String[] args) throws Exception
{
XPRM mosel;
XPRMModel mod;
// Model parameter settings
double maxrisk = 1.0/3;
double minreg = 0.2;
double maxreg = 0.5;
double maxsec = 0.25;
double maxval = 0.2;
double minval = 0.1;
int maxnum = 15;
ifprint = false;
try{
mosel = new XPRM(); // Initialize Mosel
}catch(XPRMLicenseError e){
System.out.println("License error" + e.getMessage());
throw new java.lang.Exception("Error during execution");
}
try{
mosel.compile("foliocbio.mos"); // Compile the model (only required
// during development phase, deployed
// application would only use BIM)
}catch(XPRMCompileException e){
System.out.println(e.getMessage());
}
mod = mosel.loadModel("foliocbio.bim"); // Load the model
// Pass model parameters through execution parameters
mod.setExecParam("MAXRISK",maxrisk);
mod.setExecParam("MINREG",minreg);
mod.setExecParam("MAXREG",maxreg);
mod.setExecParam("MAXSEC",maxsec);
mod.setExecParam("MAXVAL",maxval);
mod.setExecParam("MINVAL",minval);
mod.setExecParam("MAXNUM",maxnum);
mod.setExecParam("DATAFILE","folio250.dat");
mod.setExecParam("OUTPUTFILE","java:runfoliocbio.cbinit");
mod.run(); // Run the model
if(mod.getExecStatus() != XPRMModel.RT_OK){
throw new java.lang.Exception("Error during model execution");
}
if(mod.getProblemStatus() != mod.PB_OPTIMAL){
throw new java.lang.Exception("Problem not optimal");
} // Stop if no solution available
mod.reset(); // Reset the model
}
}
|
|