a = 0
b = np.inf

Billups = MCP(billups, a, b)


# ### Solve by applying Newton method
# * Using minmax formulation
# Initial guess is $x=0$
Billups.x0 = 0.0


# In[4]:

t1 = tic()
x1 =  Billups.zero(transform='minmax')
t1 = 100*toc(t1)
n1 = Billups.fnorm

# * Using semismooth formulation

# In[5]:

t2 = tic()
x2 = Billups.zero(transform='ssmooth')
t2 = 100*toc(t2)
n2 = Billups.fnorm

# ### Print results
# Hundreds of seconds required to solve hard nonlinear complementarity problem using Newton minmax and semismooth formulations
from demos.setup import np, tic, toc
from numpy.linalg import solve

""" Solving linear equations by different methods """

# Print table header
print('Hundreds of seconds required to solve n by n linear equation Ax=b')
print('m times using solve(A, b) and dot(inv(A), b), computing inverse only once.\n')
print('{:>5s} {:>5s} {:>12s} {:>12s}'.format('m', 'n', 'solve(A,b)', 'dot(inv(A), b)'))
print('-' * 40)

for m in [1, 100]:
    for n in [50, 500]:
        A = np.random.rand(n, n)
        b = np.random.rand(n, 1)

        tt = tic()
        for j in range(m):
            x = solve(A, b)

        f1 = 100 * toc(tt)

        tt = tic()
        Ainv = np.linalg.inv(A)
        for j in range(m):
            x = np.dot(Ainv, b)

        f2 = 100 * toc(tt)
        print('{:5d} {:5d} {:12.2f} {:12.2f}'.format(m, n, f1, f2))
''' Set up the problem '''
def g(x):
    return np.sqrt(x)

problem_as_fixpoint = NLP(g, xinit)

''' Equivalent Rootfinding Formulation '''
def f(x):
    fval = x - np.sqrt(x)
    fjac = 1-0.5 / np.sqrt(x)
    return fval, fjac

problem_as_zero = NLP(f, xinit)

''' Compute fixed-point using Newton method '''
t0 = tic()
x1 = problem_as_zero.newton()
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm

''' Compute fixed-point using Broyden method '''
t0 = tic()
x2 = problem_as_zero.broyden()
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm

''' Compute fixed-point using function iteration '''
t0 = tic()
x3 = problem_as_fixpoint.fixpoint()
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - x3)
from numpy.linalg import solve
""" Solving linear equations by different methods """

# Print table header
print('Hundreds of seconds required to solve n by n linear equation Ax=b')
print(
    'm times using solve(A, b) and dot(inv(A), b), computing inverse only once.\n'
)
print('{:>5s} {:>5s} {:>12s} {:>12s}'.format('m', 'n', 'solve(A,b)',
                                             'dot(inv(A), b)'))
print('-' * 40)

for m in [1, 100]:
    for n in [50, 500]:
        A = np.random.rand(n, n)
        b = np.random.rand(n, 1)

        tt = tic()
        for j in range(m):
            x = solve(A, b)

        f1 = 100 * toc(tt)

        tt = tic()
        Ainv = np.linalg.inv(A)
        for j in range(m):
            x = np.dot(Ainv, b)

        f2 = 100 * toc(tt)
        print('{:5d} {:5d} {:12.2f} {:12.2f}'.format(m, n, f1, f2))
示例#5
0
    return np.sqrt(x)


problem_as_fixpoint = NLP(g, xinit)
''' Equivalent Rootfinding Formulation '''


def f(x):
    fval = x - np.sqrt(x)
    fjac = 1 - 0.5 / np.sqrt(x)
    return fval, fjac


problem_as_zero = NLP(f, xinit)
''' Compute fixed-point using Newton method '''
t0 = tic()
x1 = problem_as_zero.newton()
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm
''' Compute fixed-point using Broyden method '''
t0 = tic()
x2 = problem_as_zero.broyden()
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm
''' Compute fixed-point using function iteration '''
t0 = tic()
x3 = problem_as_fixpoint.fixpoint()
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - x3)

print('Hundredths of seconds required to compute fixed-point of g(x)=sqrt(x)')
    return fval, fjac


a = 0
b = np.inf

Billups = MCP(billups, a, b)

# ### Solve by applying Newton method
# * Using minmax formulation
# Initial guess is $x=0$
Billups.x0 = 0.0

# In[4]:

t1 = tic()
x1 = Billups.zero(transform='minmax')
t1 = 100 * toc(t1)
n1 = Billups.fnorm

# * Using semismooth formulation

# In[5]:

t2 = tic()
x2 = Billups.zero(transform='ssmooth')
t2 = 100 * toc(t2)
n2 = Billups.fnorm

# ### Print results
# Hundreds of seconds required to solve hard nonlinear complementarity problem using Newton minmax and semismooth formulations