Skip to content

Commit

Permalink
fixed pyipopt test in tests/experimental_tests.py
Browse files Browse the repository at this point in the history
  • Loading branch information
b45ch1 committed Apr 14, 2011
1 parent 2279b3d commit 5faea4a
Showing 1 changed file with 68 additions and 29 deletions.
97 changes: 68 additions & 29 deletions tests/experimental_tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
or complicated_tests.py or complicated_tests.py
""" """
import numpy import numpy
import scipy import scipy;
from numpy.testing import * from numpy.testing import *
from adolc import * from adolc import *


Expand Down Expand Up @@ -35,17 +35,17 @@ def test_big_tape():
#tape_to_latex(0,numpy.array([1.]),numpy.array([0.])) #tape_to_latex(0,numpy.array([1.]),numpy.array([0.]))




def test_arc_hyperbolic_functions(): # def test_arc_hyperbolic_functions():
x = 3. # x = 3.
ax = adouble(x) # ax = adouble(x)


aarcsh = numpy.arcsinh(ax) # aarcsh = numpy.arcsinh(ax)
aarcch = numpy.arccosh(ax) # aarcch = numpy.arccosh(ax)
aarcth = numpy.arctanh(ax) # aarcth = numpy.arctanh(ax)


assert_almost_equal(aarcsh.val, numpy.arcsinh(x)) # assert_almost_equal(aarcsh.val, numpy.arcsinh(x))
assert_almost_equal(aarcch.val, numpy.arccosh(x)) # assert_almost_equal(aarcch.val, numpy.arccosh(x))
assert_almost_equal(aarcth.val, numpy.arctanh(x)) # assert_almost_equal(aarcth.val, numpy.arctanh(x))






Expand Down Expand Up @@ -74,6 +74,14 @@ def test_ipopt_optimization():
#print '"pyipopt is not installed, skipping test' #print '"pyipopt is not installed, skipping test'
#return #return
raise NotImplementedError("pyipopt is not installed, skipping test") raise NotImplementedError("pyipopt is not installed, skipping test")

try:
import scipy.sparse as sparse
except:
#print '"pyipopt is not installed, skipping test'
#return
raise NotImplementedError("scipy is not installed, skipping test")

import time import time


nvar = 4 nvar = 4
Expand Down Expand Up @@ -120,7 +128,9 @@ def eval_jac_g(x, flag, user_data = None):
2.0*x[1], 2.0*x[1],
2.0*x[2], 2.0*x[2],
2.0*x[3] ]) 2.0*x[3] ])




nnzh = 10 nnzh = 10
def eval_h(x, lagrange, obj_factor, flag, user_data = None): def eval_h(x, lagrange, obj_factor, flag, user_data = None):
if flag: if flag:
Expand Down Expand Up @@ -207,30 +217,59 @@ def eval_jac_g_adolc(x, flag, user_data = None):
return (numpy.asarray(result[1],dtype=int), numpy.asarray(result[2],dtype=int)) return (numpy.asarray(result[1],dtype=int), numpy.asarray(result[2],dtype=int))
else: else:
return result[3] return result[3]

def eval_h_adolc(x, lagrange, obj_factor, flag, user_data = None): def eval_h_adolc(x, lagrange, obj_factor, flag, user_data = None):
options = numpy.array([0,0],dtype=int)
assert numpy.ndim(x) == 1
assert numpy.size(x) == 4
result_f = colpack.sparse_hess_no_repeat(1, x, options)
result_g0 = colpack.sparse_hess_no_repeat(3, x,options)
result_g1 = colpack.sparse_hess_no_repeat(4, x,options)
Hf = scipy.linalg.sparse.coo_matrix( (result_f[3], (result_f[1], result_f[2])), shape=(4, 4))
Hg0 = scipy.linalg.sparse.coo_matrix( (result_g0[3], (result_g0[1], result_g0[2])), shape=(4, 4))
Hg1 = scipy.linalg.sparse.coo_matrix( (result_g1[3], (result_g1[1], result_g1[2])), shape=(4, 4))

H = Hf + Hg0 + Hg1
H = H.tocoo()


if flag: if flag:
hrow = H.row # return sparsity pattern of the hessian
hcol = H.col
return (numpy.array(hcol,dtype=int), numpy.array(hrow,dtype=int)) if eval_h_adolc.firstrun == True:

# this can be done more elegantly based directly on
# the sparsity pattern returned by adolc.sparse.hess_pat
options = numpy.array([0,0],dtype=int)
result_f = colpack.sparse_hess_no_repeat(1, x, options)
result_g0 = colpack.sparse_hess_no_repeat(3, x,options)
result_g1 = colpack.sparse_hess_no_repeat(4, x,options)
Hf = sparse.coo_matrix( (result_f[3], (result_f[1], result_f[2])), shape=(4, 4))
Hg0 = sparse.coo_matrix( (result_g0[3], (result_g0[1], result_g0[2])), shape=(4, 4))
Hg1 = sparse.coo_matrix( (result_g1[3], (result_g1[1], result_g1[2])), shape=(4, 4))

H = Hf + Hg0 + Hg1
H = H.tocoo()
hrow = H.row
hcol = H.col
hpat = (numpy.array(hcol,dtype=int), numpy.array(hrow,dtype=int))

eval_h_adolc.hpat = hpat
eval_h_adolc.firstrun = False

return eval_h_adolc.hpat


else: else:
# compute sparse hessian
assert numpy.ndim(x) == 1
assert numpy.size(x) == 4

options = numpy.array([0,0],dtype=int)
result_f = colpack.sparse_hess_no_repeat(1, x, options)
result_g0 = colpack.sparse_hess_no_repeat(3, x,options)
result_g1 = colpack.sparse_hess_no_repeat(4, x,options)
Hf = sparse.coo_matrix( (result_f[3], (result_f[1], result_f[2])), shape=(4, 4))
Hg0 = sparse.coo_matrix( (result_g0[3], (result_g0[1], result_g0[2])), shape=(4, 4))
Hg1 = sparse.coo_matrix( (result_g1[3], (result_g1[1], result_g1[2])), shape=(4, 4))

H = Hf + Hg0 + Hg1
H = H.tocoo()

values = numpy.zeros((10), float) values = numpy.zeros((10), float)
values[:] = H.data values[:] = H.data
return values return values
eval_h_adolc.firstrun = True





# function of f # function of f
assert_almost_equal(eval_f(x0), eval_f_adolc(x0)) assert_almost_equal(eval_f(x0), eval_f_adolc(x0))
Expand All @@ -252,8 +291,8 @@ def eval_h_adolc(x, lagrange, obj_factor, flag, user_data = None):
x0 = numpy.random.rand(4) x0 = numpy.random.rand(4)
result = (eval_h(x0, lagrange, obj_factor, False), eval_h(x0, lagrange, obj_factor, True)) result = (eval_h(x0, lagrange, obj_factor, False), eval_h(x0, lagrange, obj_factor, True))
result_adolc = (eval_h_adolc(x0, lagrange, obj_factor, False), eval_h_adolc(x0, lagrange, obj_factor, True)) result_adolc = (eval_h_adolc(x0, lagrange, obj_factor, False), eval_h_adolc(x0, lagrange, obj_factor, True))
H = scipy.linalg.sparse.coo_matrix( result, shape=(4, 4)) H = sparse.coo_matrix( result, shape=(4, 4))
H_adolc = scipy.linalg.sparse.coo_matrix( result_adolc, shape=(4, 4)) H_adolc = sparse.coo_matrix( result_adolc, shape=(4, 4))
H = H.todense() H = H.todense()
H_adolc = H_adolc.todense() H_adolc = H_adolc.todense()
assert_array_almost_equal( H, H_adolc.T) assert_array_almost_equal( H, H_adolc.T)
Expand Down

0 comments on commit 5faea4a

Please sign in to comment.