Initializing help system before first use

Formulating the Mosel Model

Even if you are not a Mosel expert, you should be able to glean the intent of this code sample if you have some programming experience.
Copy and paste the code into your own editor. The source file is available in the examples folder in your Xpress installation directory.
Note The following code fragment contains line numbers, so that they can be referenced in the subsequent commentary.
01:(!
02:    File Type: Mosel model.
03:    Purpose: mminsight Mosel module example: Portfolio optimization app.
04:    (c) 2020 Fair Isaac Corporation.
05:!)
06:model "Portfolio Optimization with Mosel"
07:    version 1.0.0
08:
09:    options noimplicit
10:
11:    uses "mmxprs"
12:    uses "mmsystem"
13:
14:    public declarations
15:        ! Input entities
16:        MaxHighRisk: real
17:        MaxPerShare: real
18:        MinNorthAmerica: real
19:        ShareIds: set of string
20:
21:        ! Input and result entities indexed over ShareIds
22:        Shares_Return: array (ShareIds) of real
23:        Shares_HighRisk: array (ShareIds) of boolean
24:        Shares_NorthAmerica: array(ShareIds) of boolean
25:        Shares_fraction: array(ShareIds) of mpvar
26:
27:        ! Result entities
28:        TotalReturn: linctr
29:        SummaryIds: set of string
30:        SummaryValues: array (SummaryIds) of real
31:
32:        ! Constant<
33:        DATAFILE = "shares.csv"
34:    end-declarations
35:
36:    
37:    procedure read_data
38:        writeln("Loading data.")
39:        MaxHighRisk := 0.33  ! Max. investment into high-risk values
40:        MaxPerShare := 0.25    ! Max. investment per share
41:        MinNorthAmerica := 0.45				! Min. investment into N.-American values
42:
43:        initializations from "mmsheet.csv:skiph;" + DATAFILE
44:            [Shares_Return, Shares_HighRisk, Shares_NorthAmerica] as '[A:D]'
45:        end-initializations
46:
47:        writeln("Loading finished.")
48:    end-procedure
49:
50:    procedure solve_model
51:        declarations
52:            LimitHighRisk, LimitNorthAmerica 
					 : linctr
53:        end-declarations
54:
55:        writeln('Starting optimization.')
56:
57:        ! Objective: expected total return
58:        TotalReturn := sum (s in ShareIds) Shares_Return(s) * Shares_fraction(s)
59:
60:        ! Limit the percentage of high-risk values
61:        LimitHighRisk :=
62:            sum (s in ShareIds | Shares_HighRisk(s)) Shares_fraction(s) <= MaxHighRisk
63:
64:        ! Minimum amount of North-American values
65:        LimitNorthAmerica 
					  :=
66:            sum (s in ShareIds | Shares_NorthAmerica(s)) Shares_fraction(s) >= MinNorthAmerica
67:
68:        ! Spend all the capital
69:        sum (s in ShareIds) Shares_fraction(s) = 1
70:
71:        ! Upper bounds on the investment per share
72:        forall (s in ShareIds) do
73:            Shares_fraction(s) <= MaxPerShare
74:        end-do
75:
76:        ! Solve optimization problem
77:        maximize(TotalReturn)
78:
79:        ! Save key indicator values for GUI display
80:        SummaryValues("Expected total return") := TotalReturn.sol
81:        SummaryValues("Total high risk shares") := LimitHighRisk.act
82:        SummaryValues("Total North-American") := LimitNorthAmerica 
					 .act
83:        SummaryValues("Largest position") := max (s in ShareIds) Shares_fraction(s).sol
84:
85:        forall (SummaryId in SummaryIds) do
86:            writeln(formattext("%-23s%7.2f", SummaryId, SummaryValues(SummaryId)))
87:        end-do
88:
89:        writeln('Optimization finished.')
90:    end-procedure
91:
92:        read_data
93:        solve_model
94:
95:end-model

Commentary

Lines 14-34 contain the entity declarations.

Lines 39-41 establish the model's runtime parameters. The specified default values can be changed for every model execution without any need to edit the model source.

Line 43-45 initializes the entities with values from an external data file in csv format called shares.csv (specified on line 33) which contains the following data:
! Data file for ‘shares.csv’
shareIds,returnOnInvestment,highRisk,northAmerica
Bank,6.0,False,False
Brewery,8.0,False,False
Cars,7.0,False,False
Electronics,21.0,True,False
Hardware,17.0,True,True
Highways,9.0,False,False
Software,31.0,True,False
Telecom,12.0,True,True
Theater,26.0,True,True
Treasury,5.0,False,True
Note The shares.csv file is a representation of the preferences made by the investor in the section Introducing the Example Business Problem.

Line 58 contains an expression whose value is assigned to an entity called TotalReturn - the goal of the problem is ultimately to maximize this expression.

Lines 57-74 establish the constraints of the problem, while line 77 starts the optimization solver to maximize the value of TotalReturn.

Lines 85-87 display the results of the optimization.

Finally, lines 92 and 93 read the source data file and run the optimization.

© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.