1

I'm trying to use a binary integer linear program to assign members of my staff to different shift. I have a 16x9 matrix of preferences for my staff in a csv (16 staff members, 9 slots to fill) and I used the following code to try and assign them:

weights = pd.read_csv("holiday_green day.csv", index_col= 0)
weights = weights.to_numpy().astype(float)
selection = cvx.Variable((9,16), boolean = True)
row_sum_vector = np.ones((16,1)).astype(float)
result_constraint = np.ones((9,1)).astype(float) * 2
objective = cvx.Minimize(cvx.trace(weights @ assignments))
prob = cvx.Problem(objective, [assignments @ row_sum_vector == result_constraint])
prob.solve()

When I try running this, I get the error TypeError: G must be a 'd' matrix and I don't know where to start debugging. I looked at this post, but it wasn't helpful. Can someone help me figure out what G is and what it means by 'd' matrix? Its my first time actually using CVXPY and I'm very lost.

Full Stack Trace:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-d07ad22cbc25> in <module>()
      6 objective = cvx.Minimize(cvx.atoms.affine.trace.trace(weights @ assignments))
      7 prob = cvx.Problem(objective, [assignments @ row_sum_vector == result_constraint])
----> 8 prob.solve()

3 frames
/usr/local/lib/python3.7/dist-packages/cvxpy/problems/problem.py in solve(self, *args, **kwargs)
    288         else:
    289             solve_func = Problem._solve
--> 290         return solve_func(self, *args, **kwargs)
    291 
    292     @classmethod

/usr/local/lib/python3.7/dist-packages/cvxpy/problems/problem.py in _solve(self, solver, warm_start, verbose, parallel, gp, qcp, **kwargs)
    570             self._intermediate_problem)
    571         solution = self._solving_chain.solve_via_data(
--> 572             self, data, warm_start, verbose, kwargs)
    573         full_chain = self._solving_chain.prepend(self._intermediate_chain)
    574         inverse_data = self._intermediate_inverse_data + solving_inverse_data

/usr/local/lib/python3.7/dist-packages/cvxpy/reductions/solvers/solving_chain.py in solve_via_data(self, problem, data, warm_start, verbose, solver_opts)
    194         """
    195         return self.solver.solve_via_data(data, warm_start, verbose,
--> 196                                           solver_opts, problem._solver_cache)

/usr/local/lib/python3.7/dist-packages/cvxpy/reductions/solvers/conic_solvers/glpk_mi_conif.py in solve_via_data(self, data, warm_start, verbose, solver_opts, solver_cache)
     73                                           data[s.B],
     74                                           set(int(i) for i in data[s.INT_IDX]),
---> 75                                           set(int(i) for i in data[s.BOOL_IDX]))
     76             results_dict = {}
     77             results_dict["status"] = results_tup[0]

TypeError: G must be a 'd' matrix

Edit: Tried casting all numpy arrays as float like they suggested in a different post. It didn't work.

Nakul Upadhya
  • 494
  • 4
  • 16
  • Could you post the whole traceback? – DrBwts Aug 13 '21 at 21:07
  • Does this answer your question? [How to fix the TypeError: G must be a 'd' matrix?](https://stackoverflow.com/questions/54171455/how-to-fix-the-typeerror-g-must-be-a-d-matrix) – Bill the Lizard Aug 13 '21 at 21:14
  • 1
    Can you try converting the numpy array to a [cvxpy matrix](https://cvxopt.org/userguide/matrices.html) with typecode d? – Nick ODell Aug 13 '21 at 21:15
  • Make this reproducible. Also tell us why you are using the low-lvl functions of cvxpy. What about cvx.trace? – sascha Aug 13 '21 at 23:31
  • 1
    @NickODell cvxpy and cvxopt are completely different projects. cvxopt is a solver with it's own matrices and cvxpy builds them internally when cvxopt is selected (interfacing purposes only). On the modelling layer howewer , cvxpy is solely based on numpy and scipy.sparse data-structures. cvxpy will choke on cvxopt matrices within the modelling-layer. OPs `cvx` is cvxpy, not cvxopt! – sascha Aug 13 '21 at 23:35
  • @sascha I used `cvx.atoms.affine.trace.trace` instead of `cvx.trace` because I've been making this code through a lot of googling, so I used the first function I found. I tried replacing it, but I still get the same error. – Nakul Upadhya Aug 14 '21 at 00:12

0 Answers0