| |||||||||||||||||||||||||||
| |||||||||||||||||||||||||||
|
Folio - Advanced modelling and solving tasks Description Advanced modelling and solving tasks for a portfolio optimization problem:
Source Files By clicking on a file name, a preview is opened at the bottom of this page. Data Files
foliocb_sol.mos
(!******************************************************
Mosel Example Problems
======================
file foliocb_sol.mos
````````````````````
Modeling a MIP problem
to perform portfolio optimization.
Same model as in foliomip3.mos.
-- Defining an integer solution callback to
store solutions into an 'mpsol' entity --
*** This model cannot be run with a Community Licence
for the provided data instance ***
(c) 2021 Fair Isaac Corporation
author: S.Heipcke, June 2021, rev. Mar. 2022
*******************************************************!)
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 = "folio250.dat" ! File with problem data
MAXSOLS = 2
end-parameters
forward procedure storesol
declarations
SHARES: 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
Sol: dynamic array(SOLS:range) of mpsol
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)
! Set a MIP solution callback
setcallback(XPRS_CB_INTSOL, ->storesol)
! Solve the problem
maximize(Return)
! Solution printing
forall(i in SOLS | i <= MAXSOLS) do
writeln("\nSolution ", i)
writeln("Total return: ", getsol(Sol(i), Return))
forall(s in SHARES | getsol(Sol(i), frac(s))>0)
writeln(s, ": ", getsol(Sol(i), frac(s))*100, "% (", getsol(Sol(i), buy(s)), ")")
end-do
! **** Store intermediate solutions, keeping up to MAXSOLS in total ****
procedure storesol
numsol:= getparam("XPRS_MIPSOLS")
solid:= (numsol-1) mod MAXSOLS + 1
writeln("Storing solution ", numsol, " (Return=", Return.sol, ")")
if numsol>MAXSOLS then
reset(Sol(solid))
else
create(Sol(solid))
end-if
savesol(Sol(solid))
end-procedure
end-model
| |||||||||||||||||||||||||||
| © Copyright 2025 Fair Isaac Corporation. |