Example #1
0
def getConstrDirection(p,  x, regularization = 1e-7):
    c, dc, h, dh, df = p.c(x), p.dc(x), p.h(x), p.dh(x), p.df(x)
    A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq))
    b = concatenate((-c, p.b-p.matmult(p.A,x)))
    beq = concatenate((-h, p.beq-p.matmult(p.Aeq,x)))
    lb = p.lb - x
    ub = p.ub - x

    nC = b.size

    #TODO: USE if nC == 0 instead
    if A.size == 0: A_LLS = hstack((zeros((nC, p.n)), diag(ones(nC))))
    else: A_LLS = hstack((A, diag(ones(nC))))

    if Aeq.size == 0:
        Awhole_LLS = vstack((A_LLS, hstack((diag(regularization*ones(p.n)), zeros((p.n, nC))))))
    else:
        Aeq_LLS = hstack((Aeq, zeros((beq.size, nC))))
        Awhole_LLS = vstack((Aeq_LLS, A_LLS, hstack((diag(regularization*ones(p.n)), zeros((p.n, nC))))))

    if p.getMaxResidual(x) > p.contol: dump_x = -p.getMaxConstrGradient(x)
    else: dump_x = -df

    bwhole_LLS = concatenate((beq, b, dump_x))
    lb_LLS = hstack((lb, zeros(nC)))
    ub_LLS = hstack((ub, inf*ones(nC)))
    p_sp = LLSP(Awhole_LLS,  bwhole_LLS,  lb = lb_LLS, ub = ub_LLS, iprint = -1)
    r_sp = p_sp.solve('BVLS', BVLS_inf=1e30)
    return r_sp.xf[:p.n]
Example #2
0
import sys
sys.path.append('/home/dmitrey/OOSuite/OpenOpt')

from numpy import empty, sin, cos, arange
from openopt import LLSP

M, N = 1500, 1000
C = empty((M, N))
d = empty(M)

for j in range(M):
    d[j] = 1.5 * N + 80 * sin(j)
    C[j] = 8 * sin(4.0 + arange(N)) + 15 * cos(j)
""" alternatively, try the sparse problem - lsqr solver can take benefits of it.
Also, if your C is too large for your RAM 
you can pass C of any scipy.sparse matrix format

for j in xrange(M):
    d[j] = 1.5*N+80*sin(j)
    C[j, j%N] = 15*cos(j) #+ 8*sin(4.0+arange(N))
    C[j, (1 + j)%N] = 15*cos(j) #+ 8*sin(4.0+arange(N))
"""

p = LLSP(C, d)
r = p.solve('lsmr')

print('f_opt: %f' % r.ff)  # 2398301.68347
#print 'x_opt:', r.xf
Example #3
0
__docformat__ = "restructuredtext en"

from numpy import diag,  ones, sin, cos, arange, sqrt, vstack, zeros, dot
from openopt import LLSP, NLP

N = 150
C1 = diag(sqrt(arange(N)))
C2 = (1.5+arange(N)).reshape(1, -1) * (0.8+arange(N)).reshape(-1, 1)
C = vstack((C1, C2))
d = arange(2*N)
lb = -2.0+sin(arange(N))
ub = 5+cos(arange(N))

############################LLSP################################
LLSPsolver = 'bvls'
p = LLSP(C, d, lb=lb, ub=ub)
r = p.solve(LLSPsolver)
#############################NLP################################
NLPsolver = 'scipy_lbfgsb'# you could try scipy_tnc or ralg as well
#NLPsolver = 'scipy_tnc'
p2 = LLSP(C, d, lb=lb, ub=ub)
r2=p2.solve('nlp:'+NLPsolver)
##################################################################
print '###########Results:###########'
print 'LLSP solver '+ LLSPsolver + ':', r.ff
print 'NLP solver '+ NLPsolver + ':', r2.ff
Example #4
0
# overdetermined system of 4 linear equations with 3 variables
# you can use "for" cycle, operations of sum() eg
# startPoint = {a:[0,10], b:0, c:0}
# f = [sum(a) + i * b + 4 * sqrt(i) * c - 2 * i**2 for i in range(40)]
f = [
    2 * a + 3 * b - 4 * c + 5, 2 * a + 13 * b + 15, a + 4 * b + 4 * c - 25,
    20 * a + 30 * b - 4 * c + 50
]

# alternatively, you can use the following vectorized form
measurements_a_koeff = [2, 2, 1, 20]
measurements_b_koeff = [3, 13, 4, 30]
measurements_c_koeff = [-4, 0, 4, -4]
d = [5, 15, -25, 50]
f = a * measurements_a_koeff + b * measurements_b_koeff + c * measurements_c_koeff + d
# you can set several oofuns with different output sizes,
# e.g. f = [myOOFun1, myOOFun2, ..., myOOfunN]

# assign prob
p = LLSP(f, startPoint)

# solve
r = p.solve('lsqr')
a_sol, b_sol, c_sol = r(a, b, c)
# print result
print(r.xf)

# Expected output:
# {a: array([-0.3091145]), b: array([-0.86376906]), c: array([ 4.03827441])}
Example #5
0
# in general case variables can be arrays,
# e.g. startPoint = {a:zeros(100), b:[0, 2], c:ones(20000)} 
 
# overdetermined system of 4 linear equations with 3 variables
# you can use "for" cycle, operations of sum() eg 
# startPoint = {a:[0,10], b:0, c:0} 
# f = [sum(a) + i * b + 4 * sqrt(i) * c - 2 * i**2 for i in range(40)]
f = [2*a+3*b-4*c+5, 2*a+13*b+15, a+4*b+4*c-25, 20*a+30*b-4*c+50]
 
# alternatively, you can use the following vectorized form
measurements_a_koeff = [2, 2, 1, 20]
measurements_b_koeff = [3, 13, 4, 30]
measurements_c_koeff = [-4, 0, 4, -4]
d = [5, 15, -25, 50] 
f = a * measurements_a_koeff + b * measurements_b_koeff + c * measurements_c_koeff + d
# you can set several oofuns with different output sizes, 
# e.g. f = [myOOFun1, myOOFun2, ..., myOOfunN] 
 
# assign prob
p = LLSP(f, startPoint)
 
# solve
r = p.solve('lsqr')
a_sol,  b_sol,  c_sol = r(a, b, c)
# print result
print(r.xf)
 
# Expected output:
# {a: array([-0.3091145]), b: array([-0.86376906]), c: array([ 4.03827441])}

Example #6
0
__docformat__ = "restructuredtext en"

from numpy import diag, ones, sin, cos, arange, sqrt, vstack, zeros, dot
from openopt import LLSP, NLP

N = 150
C1 = diag(sqrt(arange(N)))
C2 = (1.5 + arange(N)).reshape(1, -1) * (0.8 + arange(N)).reshape(-1, 1)
C = vstack((C1, C2))
d = arange(2 * N)
lb = -2.0 + sin(arange(N))
ub = 5 + cos(arange(N))

############################LLSP################################
LLSPsolver = 'bvls'
p = LLSP(C, d, lb=lb, ub=ub)
r = p.solve(LLSPsolver)
#############################NLP################################
NLPsolver = 'scipy_lbfgsb'  # you could try scipy_tnc or ralg as well
#NLPsolver = 'scipy_tnc'
p2 = LLSP(C, d, lb=lb, ub=ub)
r2 = p2.solve('nlp:' + NLPsolver)
##################################################################
print '###########Results:###########'
print 'LLSP solver ' + LLSPsolver + ':', r.ff
print 'NLP solver ' + NLPsolver + ':', r2.ff
Example #7
0
__docformat__ = "restructuredtext en"

from numpy import diag, ones, sin, cos, arange, sqrt, vstack, zeros, dot
from openopt import LLSP, NLP

N = 150
C1 = diag(sqrt(arange(N)))
C2 = (1.5 + arange(N)).reshape(1, -1) * (0.8 + arange(N)).reshape(-1, 1)
C = vstack((C1, C2))
d = arange(2 * N)
lb = -2.0 + sin(arange(N))
ub = 5 + cos(arange(N))

############################LLSP################################
LLSPsolver = "bvls"
p = LLSP(C, d, lb=lb, ub=ub)
r = p.solve(LLSPsolver)
#############################NLP################################
NLPsolver = "scipy_lbfgsb"  # you could try scipy_tnc or ralg as well
# NLPsolver = 'scipy_tnc'
p2 = LLSP(C, d, lb=lb, ub=ub)
r2 = p2.solve("nlp:" + NLPsolver)
##################################################################
print "###########Results:###########"
print "LLSP solver " + LLSPsolver + ":", r.ff
print "NLP solver " + NLPsolver + ":", r2.ff
Example #8
0
import sys
sys.path.append('/home/dmitrey/OOSuite/OpenOpt')

from numpy import empty, sin, cos, arange
from openopt import LLSP

M, N = 1500, 1000
C = empty((M,N))
d =  empty(M)

for j in range(M):
    d[j] = 1.5*N+80*sin(j)
    C[j] = 8*sin(4.0+arange(N)) + 15*cos(j)
    
""" alternatively, try the sparse problem - lsqr solver can take benefits of it.
Also, if your C is too large for your RAM 
you can pass C of any scipy.sparse matrix format

for j in xrange(M):
    d[j] = 1.5*N+80*sin(j)
    C[j, j%N] = 15*cos(j) #+ 8*sin(4.0+arange(N))
    C[j, (1 + j)%N] = 15*cos(j) #+ 8*sin(4.0+arange(N))
"""

p = LLSP(C, d)
r = p.solve('lsmr')

print('f_opt: %f' % r.ff) # 2398301.68347
#print 'x_opt:', r.xf