当前位置: 首页>>代码示例>>Python>>正文


Python NLP.minimize方法代码示例

本文整理汇总了Python中openopt.NLP.minimize方法的典型用法代码示例。如果您正苦于以下问题:Python NLP.minimize方法的具体用法?Python NLP.minimize怎么用?Python NLP.minimize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在openopt.NLP的用法示例。


在下文中一共展示了NLP.minimize方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: f

# 需要导入模块: from openopt import NLP [as 别名]
# 或者: from openopt.NLP import minimize [as 别名]
# after the problem is assigned, you could turn the parameters,
# along with some other that have been set as defaults:

p.x0 = 0.15
p.plot = 0


def f(x):
    return x if x > 0 else x ** 2


p.f = f

# At last, you can modify any prob parameters in minimize/maximize/solve/manage functions:

r = p.minimize("ralg", x0=-1.5, iprint=-1, plot=1, color="r")
# or
# r = p.manage('ralg', start = False, iprint = 0, x0 = -1.5)

"""
Note that *any* kwarg passed to constructor will be assigned
e.g. 
p = NLP(f, x0, myName='JohnSmith')
is equivalent to 
p.myName='JohnSmith'
It can be very convenient for user-supplied callback functions 
(see /examples/userCallback.py)
(instead of using "global" as you have to do in MATLAB)

See also http://openopt.org/OOFrameworkDoc#Result_structure for result structure (r) fields 
"""
开发者ID:AlbertHolmes,项目名称:openopt,代码行数:33,代码来源:probAssign.py

示例2: sum

# 需要导入模块: from openopt import NLP [as 别名]
# 或者: from openopt.NLP import minimize [as 别名]
f = sum(x*a)**2 + sum(y*b) + c**4 + sum(x-1)**2 + sum(y)**2 + sum(y**2) + (z-5)**2
objective = 0.15 * mean(f+2*x) + sum(y) + sum(x) + z**2* std(c)  

constraints = [
               P(sum(a)**2 + sum(b**2) + sum(x) > 50*(z + sum(y))) < 0.5, # by default constraint tolerance is 10^-6
               mean(c + a[0] + b[1]+z) >= 15
               ]

startPoint = {
              x: [0]*10, # same to numpy.zeros(10); start point will have x[0]=0, x[1]=0, ..., x[9]=0
              y: [0]*4, z: 0,  
              a: [A]*10, b: [B]*4, c: C}

p = NLP(objective, startPoint, constraints = constraints)
solver = 'scipy_cobyla' 
r = p.minimize(solver, iprint = 5, maxDistributionSize=100)
''' output for Intel Atom 1.6 GHz:
------------------------- OpenOpt 0.39 -------------------------
solver: scipy_cobyla   problem: unnamed    type: NLP   goal: minimum
 iter   objFunVal   log10(maxResidual)   
    0  1.890e+01               1.00 
    5  6.791e+03              -1.65 
   10  6.790e+03              -4.46 
   15  6.790e+03              -6.07 
   17  6.790e+03             -14.75 
istop: 1000
Solver:   Time Elapsed = 45.84 	CPU Time Elapsed = 45.6
objFunValue: 6789.6809 (feasible, MaxResidual = 1.77636e-15)
'''
开发者ID:PythonCharmers,项目名称:OOSuite,代码行数:31,代码来源:sp3.py

示例3: oovars

# 需要导入模块: from openopt import NLP [as 别名]
# 或者: from openopt.NLP import minimize [as 别名]
from FuncDesigner import *
from openopt import NLP

a, b, c = oovars('a', 'b', 'c')
f = sum(a*[1, 2])**2+b**2+c**2
startPoint = {a:[100, 12], b:2, c:40} # however, you'd better use numpy arrays instead of Python lists
p = NLP(f, startPoint)
p.constraints = [(2*c+a-10)**2 < 1.5 + 0.1*b, (a-10)**2<1.5, a[0]>8.9, (a+b > [ 7.97999836, 7.8552538 ])('sum_a_b', tol=1.00000e-12), \
a < 9, ((c-2)**2 < 1), (b < -1.02), c > 1.01, ((b + c * log10(a).sum() - 1) ** 2==0)(tol=1e-6)]
r = p.minimize('ralg', plot=0, xtol=1e-7)
#r = p.solve('ralg') # for NLPs old-style (openopt 0.25 and below) p.solve() is same to p.minimize()
#r = p.maximize('ralg')
print(r.xf)
print(a(r.xf))
a_opt,  b_opt, c_opt = r(a, b, c)
# or any of the following: 
# a_opt,  b_opt, c_opt = r(a), r(b), r(c)
# r('a'), r('b'), r('c') (provided you have assigned the names to oovars as above)
# r('a', 'b', 'c')
# a(r), b(r), c(r)

"""
Expected output:
...
objFunValue: 717.75631 (feasible, max constraint =  7.44605e-07)
{a: array([ 8.99999792,  8.87525277]), b: array([-1.01999971]), c: array([ 1.0613562])}
"""
开发者ID:PythonCharmers,项目名称:OOSuite,代码行数:29,代码来源:nlp1.py

示例4: oovars

# 需要导入模块: from openopt import NLP [as 别名]
# 或者: from openopt.NLP import minimize [as 别名]
 
x = oovars(10, lb=5, ub=15)('x') # 10 unknowns
y = oovars(4, lb=10, ub=20) # 4 unknowns
z = oovar('z') # 1 unknown
 

f = 2*sum(x*a) + sum(y*b) + c**4 + sum(x-1)**2 + sum(y)**2 + sum(y**2) + (z-5)**2
objective = 0.15 * mean(f) + mean(z*c)+ 5*x[0]**4 + 10 * sum(x**2) + 3 * z**2

 
constraints = [
               P(sum(a)**2 + sum(b**2) + sum(x) > 7*(z + sum(y)), interpolate=True) < 0.5, # by default constraint tolerance is 10^-6
               mean(c + a[0] + b[1]+z) >= 15, 
               mean(z + a[0]) >= 15
               ]

startPoint = {
              x: [0]*10, # same to numpy.zeros(10); start point will have x[0]=0, x[1]=0, ..., x[9]=0
              y: [0]*4, z: 0,  
              a: [A]*10, b: [B]*4, c: C}

p = NLP(objective, startPoint, constraints = constraints)

#solver = 'scipy_cobyla' # ~ 20 sec
solver = 'algencan' # ~ 0.8 sec

r = p.minimize(solver, iprint = 1, maxDistributionSize=100, implicitBounds = 100, maxIter = 500)
#istop: 2 (|| gradient F(X[k]) || < gtol)
#Solver:   Time Elapsed = 0.82 	CPU Time Elapsed = 0.8
#objFunValue: 6764.5968 (feasible, MaxResidual = 1.71237e-09)
开发者ID:PythonCharmers,项目名称:OOSuite,代码行数:32,代码来源:sp4.py

示例5: oovar

# 需要导入模块: from openopt import NLP [as 别名]
# 或者: from openopt.NLP import minimize [as 别名]
from FuncDesigner import *
from openopt import NLP, oosolver
from numpy import arange
N = 10
a = oovar('a', size=N)
b = oovar('b', size=N)
f = (sum(abs(a*arange(1,N+1))**2.5)+sum(abs(b*arange(1,N+1))**1.5)) / 10000

startPoint = {a:cos(arange(N)), b:sin(arange(N))} # however, you'd better use numpy arrays instead of Python lists
p = NLP(f, startPoint)
#p.constraints = [(a>1)(tol=1e-5), a**2+b**2 < 0.1+a+b]# + [(a[i]**2+b[i]**2 < 3+abs(a[i])+abs(b[i])) for i in range(N)]

p.constraints = [a>1, 10*a>10]          # + [(a[i]**2+b[i]**2 < 3+abs(a[i])+abs(b[i])) for i in range(N)]
#p.constraints = []
#C = sum(a**2+b**2 - (0.1+a+b)
C = [ifThenElse(a[i]**2+b[i]**2 - (0.1+a[i]+b[i])<0,  0,  a[i]**2+b[i]**2 - (0.1+a[i]+b[i])) for i in range(N)]
p.constraints.append(sum(C)<0)
solver = 'ipopt'
solver = oosolver('ralg')
#solver = oosolver('ralg', approach='nqp')
r = p.minimize(solver, maxIter = 15000)
开发者ID:PythonCharmers,项目名称:OOSuite,代码行数:23,代码来源:nlp_mixed.py


注:本文中的openopt.NLP.minimize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。