简体   繁体   English

如何使用纸浆解决只有约束而没有目标函数的线性程序?

[英]How to solve a linear program with only constraints and no objective function using pulp?

I want to check if a data set is linearly separable or not.我想检查数据集是否线性可分。 I am using the method mentioned at this link for the purpose.Here are the constraint equations that I want to implement using pulp:为此,我正在使用此链接中提到的方法。以下是我想使用纸浆实现的约束方程:

-h^ T a + B <= -1 -h^ T a + B <= -1

h^ T b - B <= -1 h^ T b - B <= -1

In the above equations 'a' represents data belonging to one class and 'b' represents data belonging to the other class.在上述等式中,“a”代表属于一类的数据,“b”代表属于另一类的数据。 The data stored in variable A has 11 columns.存储在变量 A 中的数据有 11 列。 The last column contains value -1 or 1 , depending on whether row belongs to first equation or second equation.最后一列包含值 -1 或 1 ,具体取决于行是属于第一个方程还是第二个方程。 Similarly, the rest of columns contains all negative values or positive values, depending on whether row belongs to first equation or second equation.类似地,其余列包含所有负值或正值,具体取决于行是属于第一个方程还是第二个方程。 Below is the code that I am using:下面是我正在使用的代码:

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),cat =pulp.LpContinuous)
        model = pulp.LpProblem("Data linearly seaparable", pulp.LpMinimize)
        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 = expr <= -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')

And, I am getting the following output:而且,我得到以下输出:

2017-08-31 07:28:30
2017-08-31 07:28:36
Infeasible
2017-08-31 07:28:42
reached finally

According to pulps documentation, the first equation that we add to the model should be the objective function, but there is no objective function in this case, so I am adding only constraints to the model.根据纸浆文档,我们添加到模型中的第一个方程应该是目标函数,但在这种情况下没有目标函数,所以我只向模型添加了约束。 Is this right or is there a way to specify that there is no objective function.这是正确的还是有办法指定没有目标函数。

According to pulps documentation, the first equation that we add to the model should be the objective function, but there is no objective function in this case根据纸浆文档,我们添加到模型中的第一个方程应该是目标函数,但在这种情况下没有目标函数

First thing you add to the "model" (problem) is not an equation but a formula, that acts as the objective function.您添加到“模型”(问题)的第一件事不是方程式,而是作为目标函数的公式。

If you have no objective function, add an arbitrary one.如果您没有目标函数,请添加任意一个。 Here's an example from the documentation:这是文档中的一个示例:

# The arbitrary objective function is added
prob += 0, "Arbitrary Objective Function"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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