4 Approach: use second-order cone optimization.
6 Copyright (c) by Joao Pedro PEDROSO, Masahiro MURAMATSU and Mikio KUBO, 2012
8 from pyscipopt
import Model, quicksum, multidict
12 """markowitz -- simple markowitz model for portfolio optimization.
15 - sigma[i]: standard deviation of item i
16 - r[i]: revenue of item i
17 - alpha: acceptance threshold
18 Returns a model, ready to be solved.
20 model = Model(
"markowitz")
24 x[i] = model.addVar(vtype=
"C", name=
"x(%s)" % i)
26 model.addCons(
quicksum(r[i] * x[i]
for i
in I) >= alpha)
27 model.addCons(
quicksum(x[i]
for i
in I) == 1)
30 obj = model.addVar(vtype=
"C", name=
"objective", lb=
None, ub=
None)
31 model.addCons(
quicksum(sigma[i] ** 2 * x[i] * x[i]
for i
in I) <= obj)
32 model.setObjective(obj,
"minimize")
38 if __name__ ==
"__main__":
55 print(
"%5s\t%8s" % (
"i",
"x[i]"))
57 print(
"%5s\t%8g" % (i, model.getVal(x[i])))
58 print(
"sum:", sum(model.getVal(x[i])
for i
in I))
60 print(
"Optimal value:", model.getObjVal())