运筹系列38:优化软件测试包pyCUTEst

1. 介绍

pycutest是一个python接口,CUTEst,一个用于测试优化软件的fortran包。它基于interface originally developed for CUTErby Prof. Arpad Buermen。目前支持mac和linux。
首先安装cutest,mac系统里面用brew install cutest进行安装。然后创建pycutest_cache目录,在~/.bashrc文件里面加上:

export PYCUTEST_CACHE="/path/to/pycutest_cache"
export MYARCH=mac64.osx.gfo

执行后,使用pip install pycutest安装python接口。
详细安装方法可以参考这里

2. 问题分类

我们可以用problem_properties()函数来查看问题类型,以及使用find_problems() 函数来寻找问题。

分类标准参见 Hock and Schittkowski (Test Examples for Nonlinear Programming Codes, Lectures Notes in Economics and Mathematical Systems 187, Springer Verlag, 1981).
问题的分类格式为:XXXr-XX-n-m

  • 第一个字母:目标函数类型,包括:
    N:无目标函数
    C:常数目标函数
    L:线性目标函数
    Q:二次目标函数
    S:平方和目标函数(least square problem)
    O:其他
  • 第二个字母:约束条件类型,包括
    U:无约束
    X:仅有常量约束
    B:仅有bounds约束
    N:线性网络的邻接矩阵约束
    L:线性约束
    Q:二次约束
    O:其他
  • 第三个字母:问题连续性
    R:连续问题,且一阶导数和二阶导数存在(即Jacobbi和Hessian阵可计算)
    I:其他
  • 第四个数字:最多可微数目,为0, 1或2.
  • 第五个字母
    A:学术问题,构造出来用于测试算法
    M:没有实际意义的测试问题
    R:有实际应用的测试问题
  • 第六个字母表示是否包含隐变量,Y表示有,N表示没有
  • 第七个n表示变量的个数
  • 第八个m表示约束的个数(不包括bounds和固定变量)

3. 常用命令

在这里插入图片描述
下面是一个测试例子:

# Ensure compatibility with Python 2
from __future__ import print_function
import numpy as np
import pycutest

p = pycutest.import_problem('ROSENBR')

print("Rosenbrock function in %gD" % p.n)

iters = 0

x = p.x0
f, g = p.obj(x, gradient=True)  # objective and gradient
H = p.hess(x)  # Hessian

while iters < 100 and np.linalg.norm(g) > 1e-10:
    print("Iteration %g: objective value is %g with norm of gradient %g at x = %s" % (iters, f, np.linalg.norm(g), str(x)))
    s = np.linalg.solve(H, -g)  # Newton step
    x = x + s  # used fixed step length
    f, g = p.obj(x, gradient=True)
    H = p.hess(x)
    iters += 1

print("Found minimum x = %s after %g iterations" % (str(x), iters))
print("Done")

猜你喜欢

转载自blog.csdn.net/kittyzc/article/details/106146708
今日推荐