-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_portfolio_4_transcost_test.go
177 lines (152 loc) · 5.42 KB
/
example_portfolio_4_transcost_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gmsk_test
import (
"fmt"
"log"
"os"
"github.com/fardream/gmsk"
)
// Portfolio optimization fixed setup costs and transaction costs as a mixed integer problem,
// reproduced from portfolio_4_transcost.c in MOSEK C api.
func Example_portfolio_4_transcost() {
checkOk := func(err error) {
if err != nil {
log.Fatalf("failed: %s", err.Error())
}
}
const n int32 = 8
mu := []float64{0.07197, 0.15518, 0.17535, 0.08981, 0.42896, 0.39292, 0.32171, 0.18379}
// GT must have size n rows
GT := [...][8]float64{
{0.30758, 0.12146, 0.11341, 0.11327, 0.17625, 0.11973, 0.10435, 0.10638},
{0.00000, 0.25042, 0.09946, 0.09164, 0.06692, 0.08706, 0.09173, 0.08506},
{0.00000, 0.00000, 0.19914, 0.05867, 0.06453, 0.07367, 0.06468, 0.01914},
{0.00000, 0.00000, 0.00000, 0.20876, 0.04933, 0.03651, 0.09381, 0.07742},
{0.00000, 0.00000, 0.00000, 0.00000, 0.36096, 0.12574, 0.10157, 0.0571},
{0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.21552, 0.05663, 0.06187},
{0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.22514, 0.03327},
{0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.2202},
}
const k int64 = 8 // this is const MSKint32t k = sizeof(GT) / (n * sizeof(MSKrealt));
x0 := []float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
const w float64 = 1
const gamma float64 = 0.36
f := make([]float64, n)
g := make([]float64, n)
for i := int32(0); i < n; i++ {
f[i] = 0.01
g[i] = 0.001
}
// Offset of variables
const numvar int32 = 3 * n
const voff_x int32 = 0
const voff_z int32 = n
const voff_y int32 = 2 * n
// Offset of constraints.
const numcon int32 = 3*n + 1
_ = numcon // this is not used
const coff_bud int32 = 0
const coff_abs1 int32 = 1
const coff_abs2 int32 = 1 + n
const coff_swi int32 = 1 + 2*n
var expret float64
/* Initial setup. */
env, err := gmsk.MakeEnv()
if err != nil {
log.Fatal(err)
}
defer gmsk.DeleteEnv(env)
task, err := gmsk.MakeTask(env, 0, 0)
if err != nil {
log.Fatal(err)
}
defer gmsk.DeleteTask(task)
checkOk(task.LinkFuncToTaskStream(gmsk.STREAM_LOG, os.Stderr))
// Variables (vector of x, z, y)
checkOk(task.AppendVars(numvar))
for j := int32(0); j < n; j++ {
/* Optionally we can give the variables names */
checkOk(task.PutVarName(voff_x+j, fmt.Sprintf("x[%d]", 1+j)))
checkOk(task.PutVarName(voff_z+j, fmt.Sprintf("z[%d]", 1+j)))
checkOk(task.PutVarName(voff_y+j, fmt.Sprintf("y[%d]", 1+j)))
/* Apply variable bounds (x >= 0, z free, y binary) */
checkOk(task.PutVarBound(voff_x+j, gmsk.BK_LO, 0, gmsk.INFINITY))
checkOk(task.PutVarBound(voff_z+j, gmsk.BK_FR, -gmsk.INFINITY, gmsk.INFINITY))
checkOk(task.PutVarBound(voff_y+j, gmsk.BK_RA, 0, 1))
checkOk(task.PutVarType(voff_y+j, gmsk.VAR_TYPE_INT))
}
// Linear constraints
// - Total budget
checkOk(task.AppendCons(1))
checkOk(task.PutConName(coff_bud, "budget"))
for j := int32(0); j < n; j++ {
/* Coefficients in the first row of A */
checkOk(task.PutAij(coff_bud, voff_x+j, 1))
checkOk(task.PutAij(coff_bud, voff_z+j, g[j]))
checkOk(task.PutAij(coff_bud, voff_y+j, f[j]))
}
U := w
for i := int32(0); i < n; i++ {
U += x0[i]
}
checkOk(task.PutConBound(coff_bud, gmsk.BK_FX, U, U))
// - Absolute value
checkOk(task.AppendCons(2 * n))
for i := int32(0); i < n; i++ {
checkOk(task.PutConName(coff_abs1+i, fmt.Sprintf("zabs1[%d]", 1+i)))
checkOk(task.PutAij(coff_abs1+i, voff_x+i, -1))
checkOk(task.PutAij(coff_abs1+i, voff_z+i, 1))
checkOk(task.PutConBound(coff_abs1+i, gmsk.BK_LO, -x0[i], gmsk.INFINITY))
checkOk(task.PutConName(coff_abs2+i, fmt.Sprintf("zabs2[%d]", 1+i)))
checkOk(task.PutAij(coff_abs2+i, voff_x+i, 1))
checkOk(task.PutAij(coff_abs2+i, voff_z+i, 1))
checkOk(task.PutConBound(coff_abs2+i, gmsk.BK_LO, x0[i], gmsk.INFINITY))
}
// - Switch
checkOk(task.AppendCons(n))
for i := int32(0); i < n; i++ {
checkOk(task.PutConName(coff_swi+i, fmt.Sprintf("switch[%d]", i+1)))
checkOk(task.PutAij(coff_swi+i, voff_z+i, 1))
checkOk(task.PutAij(coff_swi+i, voff_y+i, -U))
checkOk(task.PutConBound(coff_swi+i, gmsk.BK_UP, -gmsk.INFINITY, 0))
}
// ACCs
const aoff_q int64 = 0
// - (gamma, GTx) in Q(k+1)
// The part of F and g for variable x:
// [0, 0, 0] [gamma]
// F = [GT, 0, 0], g = [0 ]
checkOk(task.AppendAfes(k + 1))
checkOk(task.PutAfeG(aoff_q, gamma))
vslice_x := make([]int32, n)
for i := int32(0); i < n; i++ {
vslice_x[i] = voff_x + i
}
for i := int64(0); i < k; i++ {
checkOk(task.PutAfeFRow(aoff_q+i+1, n, vslice_x, GT[i][:]))
}
qdom, res := task.AppendQuadraticConeDomain(k + 1)
checkOk(res)
checkOk(task.AppendAccSeq(qdom, k+1, aoff_q, nil))
checkOk(task.PutAccName(aoff_q, "risk"))
// Objective: maximize expected return mu^T x
for j := int32(0); j < n; j++ {
checkOk(task.PutCJ(voff_x+j, mu[j]))
}
checkOk(task.PutObjSense(gmsk.OBJECTIVE_SENSE_MAXIMIZE))
/* No log output */
checkOk(task.PutIntParam(gmsk.IPAR_LOG, 0))
/* Dump the problem to a human readable PTF file. */
checkOk(task.WriteDataHandle(os.Stderr, gmsk.DATA_FORMAT_PTF, gmsk.COMPRESS_NONE))
_, res = task.OptimizeTrm()
/* Display the solution summary for quick inspection of results. */
checkOk(task.SolutionSummary(gmsk.STREAM_LOG))
checkOk(res)
for j := int32(0); j < n; j++ {
xx, res := task.GetXxSlice(gmsk.SOL_ITG, voff_x+j, voff_x+j+1, nil)
checkOk(res)
xj := xx[0]
expret += mu[j] * xj
}
fmt.Printf("\nExpected return %e for gamma %e\n", expret, gamma)
// Output: Expected return 4.119046e-01 for gamma 3.600000e-01
}