简体   繁体   中英

Working with Pulp in python: variables stalled

I'm trying to implement the following LP in Python/Pulp in order to compare to Sklearn:

LP for Support Vector Machine

    #universe
    from pulp import *
    import numpy as np
    from sklearn.metrics.pairwise import rbf_kernel

    #define the function to regresse
    N = 100
    x = np.linspace(0, 2 * np.pi, N)
    y = np.sin(x) + np.random.random(N) * 1
    x.resize(N, 1)
    K = rbf_kernel(x, gamma=5)

    # define de problem for Pulp

    # define the hyper parameters
    C = 10
    eps = 0.1

    # define the variables
    RANGE = list(range(N))
    alpha = pulp.LpVariable.dicts("alpha", (RANGE,), cat='Continuous')
    b = pulp.LpVariable("b", cat='Continuous')
    a = pulp.LpVariable.dicts("a", (RANGE,), lowBound=0, cat='Continuous')
    Eta = pulp.LpVariable.dicts("Eta", (RANGE,), lowBound=0, cat='Continuous')

    prob = LpProblem("Constrained SVM", LpMinimize)

    # implementing the constrains to Pulp
    for i in range(N):
        prob += lpDot(alpha, K[i]) + b - y[i] >= -Eta[i]
        prob += lpDot(alpha, K[i]) + b - y[i] <= Eta[i]
        prob += eps >= 0 #useless constraint
        prob += eps <= Eta[i]
        prob += alpha[i] <= a[i]
        prob += alpha[i] >= -a[i]

    # call the objective function
    prob += lpSum(a) + C * lpSum(Eta)

    #output file
    prob.writeLP("MySVM.lp")

    #launch solver and read result
    prob.solve()
    print("Status:", LpStatus[prob.status])

my issue is the solver doesn't to do anything for alpha and a . They are equal to zero at the end. Is it due to coding error or my LP is badly set?

import numpy as np import matplotlib.pyplot as plt

from sklearn.svm import SVR from sklearn.metrics.pairwise import rbf_kernel from pulp import *

def CompPulpSlk():

N = 100
x = np.linspace(0, 2 * np.pi, N)
y = np.sin(x) + np.random.random(N) * 1

x.resize(N, 1)
K = rbf_kernel(x, gamma=5)

# perform SVR
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=2)
y_rbf = svr_rbf.fit(x, y).predict(x)

# define de problem for Pulp

# define the hyper parameters
C = 10
eps = 0.1

# define the variables
Alpha = pulp.LpVariable.matrix("alpha", list(range(N)), lowBound=-1000, upBound=1000, cat='Continuous')
b = pulp.LpVariable("b", cat='Continuous')
a = pulp.LpVariable.matrix("a", list(range(N)), lowBound=0, cat='Continuous')
Eta = pulp.LpVariable.matrix("Eta", list(range(N)), lowBound=0, cat='Continuous')

prob = LpProblem("Constrained SVM", LpMinimize)

# implementing the constrains to Pulp
for i in range(N):

    prob += lpDot(K[i], Alpha) + b - y[i] >= -Eta[i]
    prob += lpDot(K[i], Alpha) + b - y[i] <= Eta[i]
    prob += eps >= 0
    prob += eps <= Eta[i]
    prob += Alpha[i] <= a[i]
    prob += Alpha[i] >= -a[i]

# call the objective function
prob += lpSum(a[i] for i in range(N)) + C * lpSum(Eta[i] for i in range(N))

#LpSolverDefault.msg = 1
prob.writeLP("MySVM.lp")
prob.solve()

print("Status:", LpStatus[prob.status])

alp = np.zeros(N)
for v in prob.variables():
    if v.name == 'b':
        b = v.varValue
    elif v.name[:5] == 'alpha':
        alp[int(v.name[6:])] = v.varValue

#clean up PULP variables
Alpha.clear()
Eta.clear()
a.clear()

#Projection of the solution from Pulp and Sci-learn
y_pulp = np.zeros(N)
for i in range(N):
    y_pulp[i] = np.dot(alp, K[i, :]) + b

#display result
lw = 2
plt.scatter(x, y, color='darkorange', label='data')
plt.hold('on')
plt.plot(x, y_rbf, color='navy', lw=lw, label='SciLearn')
plt.plot(x, y_pulp, color='cornflowerblue', lw=lw, label='Pulp')
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()

CompPulpSlk()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM