1.Motivation

最非线性问题研究,不可避免的需要求解雅克比,海森矩阵,这部分工作繁琐,易出错,费时。幸运的AMPL不仅具有快速建模的功能,同时还可以给出雅克比和海森矩阵,最近花了一些时间对AMPL建立出来的模型在matlab下求解雅克比和海森矩阵信息的软件库进行编译,相信这项工作可以大幅度减少以后研究工作的工作量。

2. AMPL简介

AMPL(A Mathematical Programming Language)是一种描述并求解大规模复杂数学问题的建模语言。AMPL支持世界上大部分的求解器,如CBC、CPLEX、FortMP、Gurobi、MINOS、IPOPT、SNOPT和KNITRO。AMPL语言的的一个主要的特点是对优化问题的数学表达式的简化,这使得简明地可读地定义优化问题成为可能。根据NEOS的统计AMPL是使用最为广泛的数学模型语言。
同时AMPL提供了用户自定义求解器的接口,用户可以对建立好的模型开发自己的求解器,这是AMPL最厉害的地方,这也是相对于GAMS,我选择AMPL的原因。

3. 准备资料

(我这列出我成功编译所需的软件)
Visual studio
Matlab 2012b(32位)
AMPL 在github 上的开源包:http://ampl.com/resources/hooking-your-solver-to-ampl/ https://github.com/ampl/mp

4. 编译

4.1 编译amplsolv.lib文件

将http://ampl.com/netlib/ampl/solvers/index.html 全部下载,该文件夹下包含makefile 文件,其中makefile.vc是针对visual studio编写的。打开Visual studio命令行,cd 到该目录下,输入nmake /f makefile.vc 会在目录下编译出amplsolv.lib。在C++/C语言,头文件加这个lib文件足够了。但是我们是在matlab下运行,还需要进行下一步。

4.2 编译amplfunc.mexw32文件

http://blog.csdn.net/ky25103378/article/details/50496203 博客中详述了如何在VS中编译mex文件的方法。其中几个注意地方:
1. 建立的事DLL工程
2. 打开项目属性配置页,C++ -> 附加包含目录 加入MATLAB安装目录下的 \extern\include 路径。
3. 链接器 -> 附加库目录 加入MATLAB的 \extern\lib\win32\microsoft 路径。
4. 链接器 -> 输入 -> 附加依赖项 输入libmx.lib libeng.lib libmat.lib libmex.lib 这四个lib文件。
5. 输出文件名改为*.mexw32
6. 我的VS是32位的,因此整个过程中都加入的32位source文件
将amplfunc.c加入到工程source文件夹下。点击build,如果一切顺利在解决方案debug目录下出现了amplfunc.mexw32的文件,将此文件拷贝到matlab工作路径下,可进行测试。
(amplfunc.c是针对稠密矩阵,要编译针对稀疏矩阵的需要使用spamfunc.c文件。)

5. 测试

.nl 文件的获得
.nl 文件是AMPL 模型生成的,生成的指令是 write “b+filename”;
如write “bOPFBRN”;会在mod文件所在目录下生成OPFBRN.nl文件,这个文件就可以作为matlab的测试文件了。

在example目录下,利用matlab运行代码

x = 0;
pname = 'OPFBRN'
[x,bl,bu,v,cl,cu] = spamfunc(pname);
[f,c] = spamfunc(x,0);
[g,Jac] = spamfunc(x,1);
W =(spamfunc([1; 1; 1]));
[g,A,B] = evalg(x)
W = evalw(1,[1 1])  

如果一切顺利会出现想要的结果。该编译出来的只能在matlab 32位条件下运行,64位会出错。

编译的结果存在了我的CSDN下,如何使用可以参考AMPL Hooking Your Solver to AMPL中的使用说明书。整个编译过程中的文件可以在:http://url.cn/5ywSGKN 下载(好像有效期是40天)。

如有问题欢迎相互讨论。

附:matlab 调用函数

Sample mex function (in MATLAB 5.x format) for getting functions,gradients, and dense Hessians from an AMPL .nl file.  Start with[x,bl,bu,v,cl,cu] = amplfunc('stub')or, for complementarity problems,[x,bl,bu,v,cl,cu,cv] = amplfunc('stub')to read in a problem (discarding the previous problem, if any).The return values are:x = primal initial guessv = dual initial guessbl, bu = lower and upper bounds on xcl, cu = lower and upper bounds on c (the constraint bodies).cv variables complementing constraints:  if cv(i) > 0, thenconstraint i complements x(cv(i)); otherwiseconstraint i is an ordinary constraint.Then[f,c] = amplfunc(x,0)gives the function (f) and constraint bodies (c) at x;[g,Jac] = amplfunc(x,1)gives the gradient g of f, the Jacobian matrix J of c, andW = amplfunc(v)gives the Hessian W of the Lagrangian function L = f + v*c(at the last x at which amplfunc(x,1) was called).After finding optimal values for x and v,amplfunc('solution message',x,v)to write a stub.sol file.Use with MATLAB
It is easy to use AMPL with MATLAB — with the help of a mex file that reads stub.nl files, writes
stub.sol files, and provides function, gradient, and Hessian values. Example file amplfunc.c is source
for an amplfunc.mex that looks at its left- and right-hand sides to determine what it should do and
works as follows:
[x,bl,bu,v,cl,cu] = amplfunc(’stub’)
reads stub.nl and sets
x = primal initial guess,
bl = lower bounds on the primal variables,
bu = upper bounds on the primal variables,
v = dual initial guess (often a vector of zeros),
cl = lower bounds on constraint bodies, and
cu = upper bounds on constraint bodies.
[f,c] = amplfunc(x,0)
sets
f = value of first objective at x and
c = values of constraint bodies at x.
[g,Jac] = amplfunc(x,1)
sets
g = gradient of first objective at x and
Jac = Jacobian matrix of constraints at x.
W = amplfunc(Y)
sets W to the Hessian of the Lagrangian (equation (*) in the section ‘‘Evaluating Nonlinear Functions’’
above) for the first objective at the point x at which the objective and constraint bodies were most recently
evaluated. Finally,
[] = amplfunc(msg,x,v)
calls write_sol(msg,x,v,0) to write the stub.sol file, with
msg = termination message (a string),
x = optimal primal variables, and
v = optimal dual variables.
It is often convenient to use .m files to massage problems to a desired form. To illustrate this, the
examples directory offers the following files (which are simplified forms of files used in joint work with
Michael Overton and Margaret Wright):
• init.m, which expects variable pname to have been assigned a stub (a string value), reads stub.nl,
and puts the problem into the form
minimize f(x)
s. t. c(x) = 0
and d(x) ≥ 0.
For simplicity, the example init.m assumes that the initial x yields d(x) > 0. A more elaborate version
of init.m is required in general.
• evalf.m, which provides [f,c,d] = evalf(x).
October 5, 2000
- 23 -
• evalg.m, which provides [g,A,B] = evalg(x), where A = c’(x) and B = d’(x) are the
Jacobian matrices of c and d.
• evalw.m, which computes the Lagrangian Hessian, W = evalw(y,z), in which y and z are vectors
of Lagrange multipliers for the constraints
c(x) = 0
and
d(x) ≥ 0,
respectively.
• enewt.m, which uses evalf.m, evalg.m and evalw.m in a simple, non-robust nonlinear
interior-point iteration that is meant mainly to illustrate setting up and solving an extended system involving
the constraint Jacobian and Lagrangian Hessian matrices.
• savesol.m, which writes file stub.sol to permit reading a computed solution into an AMPL session.
• hs100.amp, an AMPL model for test problem 100 of Hock and Schittkowski [13].
• hs100.nl, derived from hs100.amp. To solve this problem, start MATLAB and type
pname = ’hs100’;
init
enewt
savesol
Amplfunc.c provides dense Jacobian matrices and Lagrangian Hessians; spamfunc.c is a variant
that provides sparse Jacobian matrices and Lagrangian Hessians. To see an example of using
spamfunc, change all occurrences of ‘‘amplfunc’’ to ‘‘spamfunc’’ in the .m files.

AMPL Matlab 自动求导(AD)求解雅克比、海森矩阵相关推荐

  1. pytorch教程之自动求导机制(AUTOGRAD)-从梯度和Jacobian矩阵讲起

    文章目录 1. 梯度和Jacobian矩阵 2. pytorch求变量导数的过程 1. 梯度和Jacobian矩阵 设f(x)∈R1f(x)\in R^1f(x)∈R1是关于向量x∈Rnx\in R^ ...

  2. 迭代反投影法代码_Ceres求解直接法BA实现自动求导

    作者:郭田峰 来源:公众号@3D视觉工坊 BA,即Bundle Adjustment,通常译为光束法平差,束调整,捆绑调整等.但高翔博士觉得这些译名不如英文名称来得直观,所以保留英文名,简称BA. 所 ...

  3. Ceres求解直接法BA实现自动求导

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 BA,即Bundle Adjustment,通常译为光束法平差,束调整,捆绑调整等.但高翔博士觉得这些 ...

  4. 输出Ceres自动求导的雅克比

    输出Ceres自动求导的雅克比 ceres的自动求导是一个非常方便的工具,但为了保证实时性,有时需要自己求雅克比,可以用ceres自动求导求出的雅克比来验证自己求的雅克比对比,验证是否正确.ceres ...

  5. PyTorch基础(二)-----自动求导Autograd

    一.前言 上一篇文章中提到PyTorch提供了两个重要的高级功能,分别是: 具有强大的GPU加速的张量计算(如NumPy) 包含自动求导系统的的深度神经网络 第一个特性我们会在之后的学习中用到,这里暂 ...

  6. pytorch如何计算导数_Pytorch的自动求导机制与使用方法(一)

    本文以线性模型为例,讲解线性模型的求解的pytorch梯度实现方法. 要注意几个问题:在PyTorch 0.4.0版本之后,Variable类已经被禁用了,所有的torch.Tensor与torch. ...

  7. 深度学习(三十)——Deep Speech, 自动求导

    CTC 推断计算(续) 上图是一个Beam Width为3的Beam Search.Beam Search的细节可参见<机器学习(二十三)>. 由于语音的特殊性,我们实际上用的是Beam ...

  8. python二元函数求导_tensorflow的函数自动求导是如何实现的?

    最近在上关于 自动求导 (Automatic Differentiation, AD) 的课程 (CS207),正好来回答一下. 其实不只是 TensorFlow,Pytorch 这些为深度学习设计的 ...

  9. Pytorch Autograd (自动求导机制)

    Introduce Pytorch Autograd库 (自动求导机制) 是训练神经网络时,反向误差传播(BP)算法的核心. 本文通过logistic回归模型来介绍Pytorch的自动求导机制.首先, ...

  10. 自动求导-Automatic differentiation (自动微分

    在数学和计算机代数中,自动微分,也称为算法微分.计算微分.自动微分或简单的自动微分,是一组计算计算机程序指定函数导数的技术.Wikipedia 目录 前言 一.自动求导两种类别 二.计算图(无环的图) ...

最新文章

  1. 3.27课·········悬浮动态分层导航与隐藏导航
  2. spring框架学习笔记(一)
  3. WPF学习12:基于MVVM Light 制作图形编辑工具(3)
  4. 排序数字英文字母交错,由小到大
  5. android 通过图片url获取宽高_通过 URL 获取图片宽高优化
  6. Centos7 卸载自带的OpenJDK
  7. 【训练计划】--2019-05
  8. 七步学习法 —— 如何高效学习一项技能
  9. 央行最新公布2019支付牌照持牌机构公司列表,共255家(附清单)
  10. simplelink_cc13x0_sdk中GPIO的使用
  11. Hive 异常,长期更新帖
  12. 给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那两个整数,并返回它们的数组下标。
  13. asp.net强大工作流引擎,learun助力开发升级
  14. 0基础实现微信推送天气,生日等(女朋友快乐眼)
  15. 凡吸纳鲁宾逊微积分者,必须遵守“知识共享”授权许可
  16. 【收藏】2019届互联网大厂公司校招薪资汇总,基本年薪都在20万以上
  17. 关于树叶的活动设计_小学生“树叶探秘”主题活动方案
  18. c语言编程解三元一次方程组,三元一次方程组的解是 [] A.B.C.D
  19. [投资理念]沃伦-巴菲特的12条忠告
  20. WPF 实现自定义的笔迹橡皮擦

热门文章

  1. 完整的python项目实例-python完整项目
  2. 分享31个游戏源代码总有一个是你想要的
  3. 小米max android7 root,小米Max第三方Rom-Lineage OS
  4. c语言实现简易图书管理系统
  5. 日常小tip_Bat命令运行Java程序
  6. Java入门到进阶推荐书籍
  7. 简单易用的网络调试工具——NetAssist
  8. java语法让反编译报错_java 反编译
  9. vjc机器人灰度怎么编程_VJC1.5仿真版说明
  10. CSDN 缩进、目录、表格输入竖线或回车、字体及颜色设置