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

1.1k views Asked by At

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^Ta + B <= -1

h^Tb - B <= -1

In the above equations 'a' represents data belonging to one class and 'b' represents data belonging to the other class. The data stored in variable A has 11 columns. The last column contains value -1 or 1 , depending on whether row belongs to first equation or second equation. 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.

1

There are 1 answers

1
Oddthinking On

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"