2017-08-29 83 views
0

我想檢查一下我的數據是否可線性分離。爲此,我使用了此處提及的方程式link。下面是我使用的代碼:如何動態地指定紙漿中的約束條件?

try: 
     import os 
     #import random 
     import traceback 
     import datetime 
     #import numpy as np 
     import scipy.io as sio 
     import pulp 
     os.system('cls') 
     dicA = sio.loadmat('A1.mat') 
     A = dicA.get('A1')   
     var = pulp.LpVariable.dicts("var",range(11),pulp.LpContinuous) 
     A = A[:,0:10] 
     model = pulp.LpProblem("Data linearly seaparable", pulp.LpMinimize) 
     model+= 0 
     print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) 
     for i in range(len(A)): 
      expr = pulp.LpAffineExpression() 
      for j in range(len(A[i])): 
       expr += var[j]*A[i][j] 
      expr+= var[10] <= -1 
      model+= expr 
     print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) 
     model.solve() 
     print(pulp.LpStatus[model.status]) 
     print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) 
except: 
     print('exception') 
     tb = traceback.format_exc() 
     print(tb) 
finally: 
     print('reached finally') 

這裏是我得到的輸出:我添加0指定有作爲鏈接中沒有提到的目標函數

C:\Users\puneet\Anaconda3\lib\site-packages\pulp\pulp.py:1348: UserWarning: Overwriting previously set objective. 
    warnings.warn("Overwriting previously set objective.") 
2017-08-29 10:06:21 
exception 
Traceback (most recent call last): 
    File "C:/Hackerearth Challenge/Machine Learning #3/LInearlySeaparblePulp.py", line 31, in <module> 
    model.solve() 
    File "C:\Users\puneet\Anaconda3\lib\site-packages\pulp\pulp.py", line 1664, in solve 
    status = solver.actualSolve(self, **kwargs) 

    File "C:\Users\puneet\Anaconda3\lib\site-packages\pulp\solvers.py", line 1362, in actualSolve 
    return self.solve_CBC(lp, **kwargs) 
    File "C:\Users\puneet\Anaconda3\lib\site-packages\pulp\solvers.py", line 1384, in solve_CBC 
    tmpMps, rename = 1) 
    File "C:\Users\puneet\Anaconda3\lib\site-packages\pulp\pulp.py", line 1484, in writeMPS 
    f.write(" LO BND  %-8s % .12e\n" % (n, v.lowBound)) 
TypeError: must be real number, not str 

reached finally 

。另外,由於A變量中有大約12000行,因此我試圖動態地創建約束。但是似乎存在一些問題。因此,我做錯了什麼?

+1

回溯顯示您所使用的東西是一個字符串,而不是一個數字。我想知道這是否與你如何導入數據有關 – thomaskeefe

回答

2
var = pulp.LpVariable.dicts("var",range(11),pulp.LpContinuous) 

必須

var = pulp.LpVariable.dicts("var",range(11),cat=pulp.LpContinuous) 

爲LpVariable.dicts FN看起來像這樣

def dicts(self, name, indexs, lowBound = None, upBound = None, cat = LpContinuous, indexStart = []): 
+0

謝謝它解決了錯誤。我也得到了無限的結果。那麼,這是否意味着數據不是線性可分的? –