ImportError: numpy.core.multiarrary failed to import when using cvxpy

created at 08-01-2021 views: 10

description

my code

import cvxpy as cp
from numpy import array

c = array([40, 90])   
a = array([[9, 7], [-7, -20]])   
b = array([56, -70])   
x = cp.Variable(2, integer=True)  
obj = cp.Minimize(c @ x)  
cons = [a @ x <= b, x >= 0]  
prob = cp.Problem(obj, cons)   
prob.solve(solver='GLPK_MI', verbose=True)  
print("The optimal value is:", prob.value)
print("The optimal solution is:\n", x.value)

error message

ImportError :numpy.core.multiarrary failed to import

Solution

1 Reinstall numpy

importError: numpy.core.multiarrary failed to import Error in the numpy version.
It may be that pip install cvxpy...whl file disturbed the numpy version.
Solution steps:

  1. Enter the Anaconda promp command.
  2. Uninstall numpy(pip uninstall numpy)
  3. Reinstall numpy (pip install numpy).
    But it may cause your tensoflow version to match the current numpy version port, so when using tf later, you need to reinstall the corresponding version of numpy. But the problem is not big, these installations are very simple.

In this way, cvxpy can run successfully, and the results of the above code are as follows:

  D:\ProgramData\Anaconda3\python.exe C:/Users/Administrator/PycharmProjects/pythonProject/数学建模/线性规划/线性规划3.py
  ===============================================================================
                                       CVXPY                                     
                                      v1.1.13                                    
  ===============================================================================
  (CVXPY) Jul 31 12:36:41 PM: Your problem has 2 variables, 2 constraints, and 0 parameters.
  (CVXPY) Jul 31 12:36:41 PM: It is compliant with the following grammars: DCP, DQCP
  (CVXPY) Jul 31 12:36:41 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
  (CVXPY) Jul 31 12:36:41 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
  -------------------------------------------------------------------------------
                                    Compilation                                  
  -------------------------------------------------------------------------------
  (CVXPY) Jul 31 12:36:41 PM: Compiling problem (target solver=GLPK_MI).
  (CVXPY) Jul 31 12:36:41 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> GLPK_MI
  (CVXPY) Jul 31 12:36:41 PM: Applying reduction Dcp2Cone
  (CVXPY) Jul 31 12:36:41 PM: Applying reduction CvxAttr2Constr
  (CVXPY) Jul 31 12:36:41 PM: Applying reduction ConeMatrixStuffing
  (CVXPY) Jul 31 12:36:41 PM: Applying reduction GLPK_MI
  (CVXPY) Jul 31 12:36:41 PM: Finished problem compilation (took 3.001e-03 seconds).
  -------------------------------------------------------------------------------
                                  Numerical solver                               
  -------------------------------------------------------------------------------
  (CVXPY) Jul 31 12:36:41 PM: Invoking solver GLPK_MI  to obtain a solution.
        0: obj =   2.700000000e+02 inf =   6.250e-01 (1)
        1: obj =   3.150000000e+02 inf =   0.000e+00 (0)
  Long-step dual simplex will be used
  +     1: mip =     not found yet >=              -inf        (1; 0)
  Solution found by heuristic: 360
  +     2: >>>>>   3.500000000e+02 >=   3.500000000e+02   0.0% (1; 0)
  +     2: mip =   3.500000000e+02 >=     tree is empty   0.0% (0; 1)
  -------------------------------------------------------------------------------
                                      Summary                                    
  -------------------------------------------------------------------------------
  (CVXPY) Jul 31 12:36:41 PM: Problem status: optimal
  (CVXPY) Jul 31 12:36:41 PM: Optimal value: 3.500e+02
  (CVXPY) Jul 31 12:36:41 PM: Compilation took 3.001e-03 seconds
  (CVXPY) Jul 31 12:36:41 PM: Solver (including time spent in interface) took 0.000e+00 seconds
  The optimal value is: 350.0
  The optimal solution is
   [2. 3.]

2 Reinstall cvxpy

  1. Enter the Anaconda promp command, pip uninstall cvxpy, if you can’t uninstall it, go directly to D:\ProgramData\Anaconda3\Lib\site-packages and find the cvxpy folder to delete it. (Note: You have to find your own installation path. When I use Anaconda, it is the path above)
  2. Enter two lines of commands
     conda install -c conda-forge lapacl
     conda install -c cvxgrp cvxpy

Then the installation may be successful.

created at:08-01-2021
edited at: 08-01-2021: