notes/snippet/python/rsome_example.py

30 lines
1.0 KiB
Python

# coding=utf-8
from rsome import ro
from rsome import eco_solver as grb
import rsome as rso
import numpy as np
def main():
n = 150 # number of stocks
i = np.arange(1, n+1) # indices of stocks
p = 1.15 + i*0.05/150 # mean returns
delta = 0.05/450 * (2*i*n*(n+1))**0.5 # deviations of returns
Gamma = 5 # budget of uncertainty
model = ro.Model()
x = model.dvar(n) # fractions of investment
z = model.rvar(n) # random variables
model.maxmin((p + delta*z) @ x, # the max-min objective
rso.norm(z, np.infty) <= 1, # uncertainty set constraints
rso.norm(z, 1) <= Gamma) # uncertainty set constraints
model.st(sum(x) == 1) # summation of x is one
model.st(x >= 0) # x is non-negative
model.solve(grb) # solve the model by Gurobi
if __name__ == "__main__":
main()