[go: nahoru, domu]

Skip to content

Commit

Permalink
Remove Python 2 compatibility code (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-andre committed Apr 23, 2024
1 parent efe4314 commit 5f9c760
Show file tree
Hide file tree
Showing 27 changed files with 17 additions and 98 deletions.
15 changes: 3 additions & 12 deletions cylp/cpp/IClpSimplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
#include "OsiClpSolverInterface.hpp"
#include <sstream>

// define PyInt_* macros for Python 3.x
#ifndef PyInt_Check
#define PyInt_Check PyLong_Check
#define PyInt_FromLong PyLong_FromLong
#define PyInt_AsLong PyLong_AsLong
#define PyInt_Type PyLong_Type
#endif


int IClpSimplex::argWeightedMax(PyObject* arr, PyObject* arr_ind, PyObject* w, PyObject* w_ind){
//_import_array();

Expand All @@ -28,9 +19,9 @@ int IClpSimplex::argWeightedMax(PyObject* arr, PyObject* arr_ind, PyObject* w, P
int wholeArray = false;

double w_num_val;
if (PyInt_Check(w)){
if (PyLong_Check(w)){
wIsNum = true;
w_num_val = (double)PyInt_AsLong(w);
w_num_val = PyLong_AsDouble(w);
}else if (PyFloat_Check(w)){
wIsNum = true;
w_num_val = PyFloat_AsDouble(w);
Expand All @@ -41,7 +32,7 @@ int IClpSimplex::argWeightedMax(PyObject* arr, PyObject* arr_ind, PyObject* w, P
}


if (PyInt_Check(arr_ind) || PyFloat_Check(arr_ind)){
if (PyLong_Check(arr_ind) || PyFloat_Check(arr_ind)){
wholeArray = true;
}else if (!PyArray_Check(arr_ind)){
PyErr_SetString(PyExc_ValueError,
Expand Down
16 changes: 4 additions & 12 deletions cylp/cpp/ICoinIndexedVector.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include "ICoinIndexedVector.hpp"

// define PyInt_* macros for Python 3.x
#ifndef PyInt_Check
#define PyInt_Check PyLong_Check
#define PyInt_FromLong PyLong_FromLong
#define PyInt_AsLong PyLong_AsLong
#define PyInt_Type PyLong_Type
#endif

ICoinIndexedVector::ICoinIndexedVector(){
_import_array();
}
Expand Down Expand Up @@ -68,8 +60,8 @@ void ICoinIndexedVector::assign(PyObject* ind, PyObject* other){

//_import_array();

if( PyInt_Check(other) ) {
val = (double)PyInt_AsLong(other);
if( PyLong_Check(other) ) {
val = PyLong_AsDouble(other);
other_is_num = 1;
} else if( PyFloat_Check(other) ) {
val = PyFloat_AsDouble(other);
Expand All @@ -84,8 +76,8 @@ void ICoinIndexedVector::assign(PyObject* ind, PyObject* other){
}


if( PyInt_Check(ind) ) {
idx = (double)PyInt_AsLong(ind);
if( PyLong_Check(ind) ) {
idx = PyLong_AsDouble(ind);
ind_is_num = 1;
} else if( PyFloat_Check(ind) ) {
idx = PyFloat_AsDouble(ind);
Expand Down
4 changes: 0 additions & 4 deletions cylp/cy/CyCbcModel.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# cython: embedsignature=True

from itertools import product
try:
from itertools import izip
except ImportError: # Python 3 does not have izip, use zip
izip = zip
from cylp.py.mip import NodeCompareBase
from cylp.py.modeling.CyLPModel import CyLPSolution
from cylp.cy.CyCutGeneratorPythonBase cimport CyCutGeneratorPythonBase
Expand Down
3 changes: 0 additions & 3 deletions cylp/cy/CyClpPrimalColumnSteepest.pyx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# cython: embedsignature=True

from __future__ import print_function


#cimport cClpPrimalColumnSteepest
from cClpPrimalColumnSteepest cimport c_ClpPrimalColumnSteepest, new_ClpPrimalColumnSteepest
#cimport cClpPrimalColumnPivot
Expand Down
8 changes: 1 addition & 7 deletions cylp/cy/CyClpSimplex.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
# cython: profile=True
# cython: embedsignature=True

from __future__ import print_function

import inspect
import os.path
from itertools import product
try:
from itertools import izip
except ImportError: # Python 3 does not have izip, use zip
izip = zip
import numpy as np
cimport numpy as np
from scipy import sparse
Expand Down Expand Up @@ -160,7 +154,7 @@ cdef class CyClpSimplex:
# self.setObjectiveCoefficient(i, o[0,i])
#if not isinstance(o, sparse.coo_matrix):
# o = o.tocoo()
#for i, j, v in izip(o.row, o.col, o.data):
#for i, j, v in zip(o.row, o.col, o.data):
# self.setObjectiveCoefficient(j, v)
#self.setObjectiveArray(
# self.cyLPModel.objective.astype(np.double))
Expand Down
2 changes: 0 additions & 2 deletions cylp/cy/CyCoinIndexedVector.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# cython: embedsignature=True

from __future__ import print_function

cdef class CyCoinIndexedVector:
'''
``CyCoinIndexedVector`` interfaces ``CoinIndexedVector``.
Expand Down
2 changes: 0 additions & 2 deletions cylp/cy/CyPEPivot.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# cython: embedsignature=True

from __future__ import print_function

import numpy as np
cimport numpy as np
from cylp.cy cimport CyPEPivot
Expand Down
1 change: 0 additions & 1 deletion cylp/cy/CySolve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import sys
from cylp.cy.CyTest import CySolve

Expand Down
1 change: 0 additions & 1 deletion cylp/cy/CyTest.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import sys
from time import perf_counter
from cylp.cy.CyClpSimplex cimport CyClpSimplex
Expand Down
1 change: 0 additions & 1 deletion cylp/input/fileDownloader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import urllib
f = urllib.urlopen("http://www.netlib.org/lp/data/")
lines = f.read().splitlines()
Expand Down
1 change: 0 additions & 1 deletion cylp/input/netlib/fileDownloader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import urllib
import os

Expand Down
1 change: 0 additions & 1 deletion cylp/py/PySolve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import sys
from time import perf_counter
import cProfile
Expand Down
1 change: 0 additions & 1 deletion cylp/py/QP/GQP.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import QP
import numpy as np
import Constants
Expand Down
1 change: 0 additions & 1 deletion cylp/py/QP/QP.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#UNDER DEVELOPMENT AND TEST

from __future__ import print_function
import sys
import cProfile
import inspect
Expand Down
1 change: 0 additions & 1 deletion cylp/py/QP/QPGen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
#import random
import numpy as np
from numpy import random
Expand Down
1 change: 0 additions & 1 deletion cylp/py/mip/GomoryCutGenerator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import os
import math
import numpy as np
Expand Down
12 changes: 2 additions & 10 deletions cylp/py/modeling/CyLPModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,7 @@
'''

from __future__ import print_function
from functools import reduce

# Python 3 does not have long, only int
try:
long
except NameError:
long = int

from itertools import product
from copy import deepcopy
from operator import mul
Expand All @@ -136,7 +128,7 @@
from scipy.sparse import identity, lil_matrix
from cylp.py.utils.util import Ind, getMultiDimMatrixIndex, getTupleIndex

NUMBERS = (int, float, long, np.int64, np.int32, np.double)
NUMBERS = (int, float, np.int64, np.int32, np.double)
def isNumber(n):
return (isinstance(n, NUMBERS) or
(isinstance(n, CyLPArray) and n.shape == ()))
Expand Down Expand Up @@ -524,7 +516,7 @@ def perform(self, opr, left=None, right=None):
#dim = left.parentDim
else:
# # FIXME: Think of the correct way to check bound dimensions
# if (not isinstance(right, (float, long, int)) and
# if (not isinstance(right, (float, int)) and
# right.shape[0] != self.nRows):
# #raise Exception('Bound dim: %d, expected dim: %d '
# # % (right.shape[0], self.nRows))
Expand Down
1 change: 0 additions & 1 deletion cylp/py/pivots/DualDantzigPivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
for testing purposes we implement one in Python.
'''

from __future__ import print_function
import sys
import numpy as np
from operator import itemgetter
Expand Down
1 change: 0 additions & 1 deletion cylp/py/pivots/PositiveEdgePivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pivot selection rule.
'''

from __future__ import print_function
import random
import numpy as np
from cylp.cy import CyCoinIndexedVector
Expand Down
7 changes: 1 addition & 6 deletions cylp/py/pivots/WolfePivot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
from __future__ import print_function
try:
from itertools import izip
except ImportError: # Python 3 does not have izip use zip
izip = zip
import numpy as np
from .PivotPythonBase import PivotPythonBase

Expand Down Expand Up @@ -96,6 +91,6 @@ def setComplement(self, model, v1, v2):
v2n = v2.name
listv1 = np.array(model.inds.varIndex[v1n])[v1.indices]
listv2 = np.array(model.inds.varIndex[v2n])[v2.indices]
for i, j in izip(listv1, listv2):
for i, j in zip(listv1, listv2):
self.complementarityList[i], self.complementarityList[j] = j, i

4 changes: 1 addition & 3 deletions cylp/py/pivots/WolfePivotPE.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function
from itertools import izip
import random
import numpy as np
from cylp.cy import CyCoinIndexedVector
Expand Down Expand Up @@ -251,7 +249,7 @@ def setComplement(self, model, v1, v2):
v2n = v2.name
listv1 = np.array(model.inds.varIndex[v1n])[v1.indices]
listv2 = np.array(model.inds.varIndex[v2n])[v2.indices]
for i, j in izip(listv1, listv2):
for i, j in zip(listv1, listv2):
(self.complementarityList[i], self.complementarityList[j]) = \
(self.complementarityList[j], self.complementarityList[i])

1 change: 0 additions & 1 deletion cylp/py/utils/readSetcovering.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import numpy as np
from scipy import sparse
from cylp.cy import CyClpSimplex
Expand Down
17 changes: 4 additions & 13 deletions cylp/py/utils/sparseUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@
regardless of their dimension alignments. Fills with zeros where necessary.
'''

# Python 3 does not have long, only int
try:
long
except NameError:
long = int

from scipy import sparse
import numpy as np



class csc_matrixPlus(sparse.csc_matrix):
def __init__(self, arg1, shape=None, dtype=None,
copy=False, fromMatrix=None):
Expand Down Expand Up @@ -53,7 +44,7 @@ def __setitem__(self, location, val):
'''
iRow, iCol = location
if not isinstance(val, (int, long, float)):
if not isinstance(val, (int, float)):
return sparse.csc_matrix.__setitem__(self, (iRow, iCol), val)

nCols = self.shape[1]
Expand Down Expand Up @@ -117,7 +108,7 @@ def addColumns(self, nCol):

def __getitem__(self, key):
ret = sparse.csc_matrix.__getitem__(self, key)
if isinstance(ret, (int, long, float)):
if isinstance(ret, (int, float)):
return ret
# This seems to cause some potential problems when the result is 1x1
# It should really be returned as an int/float in that case, but
Expand Down Expand Up @@ -268,7 +259,7 @@ def __setitem__(self, location, val):
'''
iRow, iCol = location
if not isinstance(val, (int, long, float)):
if not isinstance(val, (int, float)):
return sparse.csr_matrix.__setitem__(self, (iRow, iCol), val)

nRows = self.shape[0]
Expand Down Expand Up @@ -356,7 +347,7 @@ def addRows(self, nRow):

def __getitem__(self, key):
ret = sparse.csr_matrix.__getitem__(self, key)
if isinstance(ret, (int, long, float)):
if isinstance(ret, (int, float)):
return ret
# This seems to cause some potential problems when the result is 1x1
# It should really be returned as an int/float in that case, but
Expand Down
10 changes: 1 addition & 9 deletions cylp/py/utils/util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
from __future__ import print_function

# Python 3 does not have long, only int
try:
long
except NameError:
long = int

import numpy as np
from math import atan2
from cylp.py import Constants
Expand Down Expand Up @@ -138,7 +130,7 @@ def __init__(self, key, dim):
start = sl.start
self.indices = range(start, stop)
self.dim = dim
elif isinstance(key, (int, long)):
elif isinstance(key, int):
if key >= dim:
raise Exception('Index (%d) out of range (%d)' % (key, dim))
self.indices = [key]
Expand Down
1 change: 0 additions & 1 deletion cylp/tests/test_MIP.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import unittest
import inspect
import os
Expand Down
1 change: 0 additions & 1 deletion cylp/tests/test_QP.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import unittest
import inspect
import os
Expand Down
1 change: 0 additions & 1 deletion fixBinaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This module contains code that copies CBC libraries into CyLP's source,
and make CyLP binaries' refernces to them relative.
'''
from __future__ import print_function
import distutils.util
import sys
import os
Expand Down

0 comments on commit 5f9c760

Please sign in to comment.