从 IPOPT Display Pyomo 获取价值
Getting Values from IPOPT Display Pyomo
这是我的 Rosenbrock 具体模型代码。
from pyomo.environ import *
from pyomo.opt import SolverFactory
import numpy as np
import math
import statistics
import time
m = ConcreteModel()
m.x = Var()
m.y = Var()
m.z = Var()
def rosenbrock(model):
return (1.0-m.x)2 + 100.0*(m.y - m.x2)2 + (1.0-m.y)2 + 100.0*(m.z - m.y2)2
m.obj = Objective(rule=rosenbrock, sense=minimize)
dist = 0.0
xval = yval = zval = error = times = []
for i in range(50):
m.x = np.random.uniform(low=-5.0, high=5.0)
m.y = np.random.uniform(low=-5.0, high=5.0)
m.z = np.random.uniform(low=-5.0, high=5.0)
solver = SolverFactory('ipopt')
t1 = time.time()
results = solver.solve(m, tee=True)
solver.solve 行通过 tee=True 时会打印出各种漂亮信息的漂亮显示。我想从 prinout 访问该信息并搜索了 Pyomo 和 IPOPT 文档,但似乎无法理解如何访问打印到屏幕上的值。我还包含了一个简短的打印输出示例,我想保存每个 运行 的值,以便我可以迭代并收集整个范围内的统计信息。
Number of nonzeros in equality constraint Jacobian...: 0
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 5
Total number of variables............................: 3
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
****省略****
Number of objective function evaluations = 45
Number of objective gradient evaluations = 23
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 0
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 22
Total CPU secs in IPOPT (w/o function evaluations) = 0.020
Total CPU secs in NLP function evaluations = 0.000
我需要这些值中的一些值,但我在搜索文档时发现没有可行的接口来访问它们,任何向导都知道如何做到这一点?谢谢。
查看贡献给 Pyomo 的 Ipopt 求解器包装器。它本质上是 Ipopt 输出日志的解析器,您应该能够 generalize/expand 它来收集当前未收集的任何值。
https://github.com/Pyomo/pyomo/blob/master/pyomo/contrib/parmest/ipopt_solver_wrapper.py
这是我的 Rosenbrock 具体模型代码。
from pyomo.environ import *
from pyomo.opt import SolverFactory
import numpy as np
import math
import statistics
import time
m = ConcreteModel()
m.x = Var()
m.y = Var()
m.z = Var()
def rosenbrock(model):
return (1.0-m.x)2 + 100.0*(m.y - m.x2)2 + (1.0-m.y)2 + 100.0*(m.z - m.y2)2
m.obj = Objective(rule=rosenbrock, sense=minimize)
dist = 0.0
xval = yval = zval = error = times = []
for i in range(50):
m.x = np.random.uniform(low=-5.0, high=5.0)
m.y = np.random.uniform(low=-5.0, high=5.0)
m.z = np.random.uniform(low=-5.0, high=5.0)
solver = SolverFactory('ipopt')
t1 = time.time()
results = solver.solve(m, tee=True)
solver.solve 行通过 tee=True 时会打印出各种漂亮信息的漂亮显示。我想从 prinout 访问该信息并搜索了 Pyomo 和 IPOPT 文档,但似乎无法理解如何访问打印到屏幕上的值。我还包含了一个简短的打印输出示例,我想保存每个 运行 的值,以便我可以迭代并收集整个范围内的统计信息。
Number of nonzeros in equality constraint Jacobian...: 0
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 5
Total number of variables............................: 3
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
****省略****
Number of objective function evaluations = 45
Number of objective gradient evaluations = 23
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 0
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 22
Total CPU secs in IPOPT (w/o function evaluations) = 0.020
Total CPU secs in NLP function evaluations = 0.000
我需要这些值中的一些值,但我在搜索文档时发现没有可行的接口来访问它们,任何向导都知道如何做到这一点?谢谢。
查看贡献给 Pyomo 的 Ipopt 求解器包装器。它本质上是 Ipopt 输出日志的解析器,您应该能够 generalize/expand 它来收集当前未收集的任何值。
https://github.com/Pyomo/pyomo/blob/master/pyomo/contrib/parmest/ipopt_solver_wrapper.py