1

I am new in coding and need your help. I get the following error:

line 159, in _get_solution
    xs = np.array(ms.get_values(self.int_var)).reshape(self.path_n, self.orderbook_n)
AttributeError: 'NoneType' object has no attribute 'get_values'

after reaching this part of the code:

line 159, in _get_solution
    xs = np.array(ms.get_values(self.int_var)).reshape(self.path_n, self.orderbook_n)

When I use: print(dir(ms)) to check what could causing this it gives me the following:

['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

How could I proceed to get the code running?

The complete code for this part is:

def _get_solution(self):
    '''function to solve the optimization model, save result and print outputs'''
    self.print_content = ''
    self.trade_solution = OrderedDict()
    ms = self.solve()
    xs = np.array(ms.get_values(self.int_var)).reshape(self.path_n, self.orderbook_n)
    zs = xs * self.precision_matrix
    nonzeroZ = list(zip(*np.nonzero(zs)))
    nonzeroZ = sorted(nonzeroZ, key=lambda x: x[0])
Matthias
  • 12,873
  • 6
  • 42
  • 48
Bsarv
  • 15
  • 1
  • 4
  • What do you hope to find with `print(dir(ms))`? You can see that the result of `print(dir(None))` is the same. Fact is, that `ms` is `None` (Python dosn't lie to you and you can check this with `print(type(ms))`). Obviously `self.solve` returns `None`. Check that function. – Matthias Feb 27 '21 at 22:56

2 Answers2

1

The error is telling you that the variable ms has evaluated to None, which is why it has no get_values() method.

Assuming that line 159 from the error message is the corresponding line in _get_solution(), this means that in the line above

ms = self.solve()

the call to self.solve() returned None.

You need to inspect self.solve() to understand why it did that.

Since you are new to Python, keep in mind that, when a function or method has no return statement, or never reaches a valid return statement, it will return None by default.

Paul P
  • 3,346
  • 2
  • 12
  • 26
0

Model.solve() may return None if the model is infeasible. You should always check for None before assuming a solution has been found, as in:

s = model.solve()
if s:
   # do whatever is appropriate for a solution
else:
   print("model has no solution")

DOcplex has techniques and tools to handle infeasible models, see this notebook for a tutorial on infeasible models:

https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/jupyter/infeasible.ipynb

Philippe Couronne
  • 826
  • 1
  • 5
  • 6