Skip to content

Commit

Permalink
Removed scipy dependency from three examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
moorepants committed Sep 22, 2018
1 parent 9bec1eb commit 81c9fcb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ script:
# Make sure the library installs.
- python setup.py install
- python -c "import ipopt"
# Make sure the test script runs. SciPy is required for this script.
- conda install scipy
- python test/examplehs071.py
- python test/exception_handling.py
- python test/lasso.py
- conda install scipy
- python test/rosen.py
# Make sure the docs build.
- cd doc && make html && cd ..
13 changes: 4 additions & 9 deletions test/examplehs071.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from __future__ import print_function, unicode_literals

import numpy as np
import scipy.sparse as sps
import ipopt


Expand Down Expand Up @@ -67,10 +66,8 @@ def hessianstructure(self):
# this function is redundant. I include it as an example for structure
# callback.
#
global hs

hs = sps.coo_matrix(np.tril(np.ones((4, 4))))
return (hs.col, hs.row)
return np.nonzero(np.tril(np.ones((4, 4))))

def hessian(self, x, lagrange, obj_factor):
#
Expand All @@ -90,11 +87,9 @@ def hessian(self, x, lagrange, obj_factor):

H += lagrange[1]*2*np.eye(4)

#
# Note:
#
#
return H[hs.row, hs.col]
row, col = self.hessianstructure()

return H[row, col]

def intermediate(
self,
Expand Down
13 changes: 4 additions & 9 deletions test/exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# Based on matlab code by Peter Carbonetto.

import numpy as np
import scipy.sparse as sps
import ipopt


Expand Down Expand Up @@ -71,10 +70,8 @@ def hessianstructure(self):
# this function is redundant. I include it as an example for structure
# callback.
#
global hs

hs = sps.coo_matrix(np.tril(np.ones((4, 4))))
return (hs.col, hs.row)
return np.nonzero(np.tril(np.ones((4, 4))))

def hessian(self, x, lagrange, obj_factor):
#
Expand All @@ -94,11 +91,9 @@ def hessian(self, x, lagrange, obj_factor):

H += lagrange[1]*2*np.eye(4)

#
# Note:
#
#
return H[hs.row, hs.col]
row, col = self.hessianstructure()

return H[row, col]

def intermediate(
self,
Expand Down
21 changes: 11 additions & 10 deletions test/lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,36 @@ def jacobianstructure(self):
#
# Create a sparse matrix to hold the jacobian structure
#
self.js = sps.coo_matrix(np.tile(np.eye(self._m), (2, 2)))

return (self.js.row, self.js.col)
return np.nonzero(np.tile(np.eye(self._m), (2, 2)))

def jacobian(self, x):

I = np.eye(self._m)

J = np.vstack((np.hstack((I, I)), np.hstack((-I, I))))
return J[self.js.row, self.js.col]

row, col = self.jacobianstructure()

return J[row, col]

def hessianstructure(self):

h = np.zeros((2*self._m, 2*self._m))
h[:self._m,:self._m] = np.tril(np.ones((self._m, self._m)))
h[:self._m, :self._m] = np.tril(np.ones((self._m, self._m)))

#
# Create a sparse matrix to hold the hessian structure
#
self.hs = sps.coo_matrix(h)

return (self.hs.row, self.hs.col)
return np.nonzero(h)

def hessian(self, x, lagrange, obj_factor):

H = np.zeros((2*self._m, 2*self._m))
H[:self._m,:self._m] = np.tril(np.tril(np.dot(self._A.T, self._A)))
H[:self._m, :self._m] = np.tril(np.tril(np.dot(self._A.T, self._A)))

row, col = self.hessianstructure()

return obj_factor*H[self.hs.row, self.hs.col]
return obj_factor*H[row, col]


if __name__ == '__main__':
Expand Down

0 comments on commit 81c9fcb

Please sign in to comment.