Python Pulp解算器无法正确解决

时间:2019-08-25 03:22:06

标签: python optimization pulp

This is the optimisation problem 我正在尝试使用Python中的Pulp解决此优化问题(如图)。但是,求解器在解决此问题时出现错误。 [-UHs-a] +和[-URs-a] +应为正数或0。[] +表示正数。解决问题后,U为[-UHs-a] +,显示为负。


Names = ["SMALL LoBM", "ME1 BM2", "ME1 BM3", "ME1 BM4", "ME1 BM5", "ME1 BM6", "ME1 BM7"]
HReturn = [[-99.99,-99.99,-99.99,-99.99,-99.99],
[12.3656,2.9904,-18.583,-4.1369,-8.2589],[-99.99,   -99.99,-99.99,-99.99,-99.99],
[-20.6349,8,-3.7037,-11.5385,34.7826],
[-6.4864,6.7495,    -5.0512,-5.3996,1.2296],
[-4.7429,-3.5639,-2.1739,-17.7778,0.2027],
[-4.8812,-4.1188,-4.5714,3.5554,-4.8789]]
NReturn = [[-99.99,-99.99,-99.99,-99.99,-99.99,-99.99,-99.99],
[6.8938,19.7269,11.6603,15.3235,8.8583,1.9892,10.4651],
[-99.99,-99.99,-99.99,-99.99,-99.99,-99.99,-99.99],
[-3.2258,-13.3333,15.3846,46.6667,1.1364,-8.9888,-16.0494],
[3.9414,-7.0051,-3.4712,-6.2271,-4.6012,3.4402,-5.7443],
[3.1018,-12.4918,12.1076,-17.8667,-7.1429,6.1189,-15.6507],
[-3.0395,13.8715,-4.1982,-9.6264,-2.7027,22.549,-4.2667]]

delta = 5
beta = 0.99

def CVAR(Names, HReturn, NReturn, delta, beta):
    print("The portfolio selection by Conditional Value at risk is as followings: ")
    #to record current time
    t = len(NReturn[0]) + 1
    ilist = range(0, len(Names))
    jlist = range(0, len(HReturn[0]))
    klist = range(0, len(NReturn[0]))

    #create the 'prob' variable to contain the problem data
    prob = LpProblem("Minimise Conditional Value at Risk", LpMinimize)

    #create problem variables
    #Wi is used to record the weight of each asset at time t, which is determined by t-1 data and data in delta series
    WVariable = [LpVariable(cat = LpContinuous, lowBound = 0.03, upBound = 1, name = Names[i]) for i in ilist]
    alpha = LpVariable("alpha", lowBound = None, upBound = None, cat = LpContinuous)
    U = LpVariable("summation of Delta returns", lowBound = None, upBound = None, cat = LpContinuous)
    V = LpVariable("summation of N returns", lowBound = None, upBound = None, cat = LpContinuous)

    #the objective function is added to the 'prob'
    HR = []
    r, c = 0, 0
    while c < len(HReturn[0]):
        HR.append([])
        c += 1
    r, c = 0, 0
    while r < len(HReturn):
        c = 0
        while c < len(HReturn[0]):           
            HR[c].append(float(HReturn[r][c]))
            c += 1
        r += 1

    NR = []
    r, c = 0, 0
    while c < len(NReturn[0]):
        NR.append([])
        c += 1
    r, c = 0, 0
    while r < len(NReturn):
        c = 0
        while c < len(NReturn[0]):           
            NR[c].append(float(NReturn[r][c]))
            c += 1
        r += 1

    prob += alpha + (1/((delta + t - 1) * (1 - beta)) * (U + V)), "Conditional Value at Risk; to be minimised"

    #adding constriants
    prob += lpSum(WVariable) == 1, "all weights add up to 1"
    prob += U >= lpSum(np.dot(np.array(WVariable), np.array(HR[j])) * (-1) - alpha for j in jlist if np.dot(np.array(WVariable), np.array(HR[j])) * (-1) - alpha >= 0)
    prob += V >= lpSum(np.dot(np.array(WVariable), np.array(NR[k])) * (-1) - alpha for k in klist if np.dot(np.array(WVariable), np.array(NR[k])) * (-1) - alpha >= 0)

    #the problem data is written to an .lp file
    prob.writeLP("MinimiseCVAR.lp")

    #the problem is solved using PuLP's choice of solver
    prob.solve()

    #The status of the solution is printed to the screen
    print("Status:", LpStatus[prob.status])

    #each of the variables is printed with it's resolved optimum value
    for v in prob.variables():
        print(v.name, "=", v.varValue)

    #The optimised objective function value is printed to the screen
    print("The value of performance function = ", value(prob.objective))
    print(prob)
    return [v.name, v.varValue]

0 个答案:

没有答案