Levmar配置

原文有些错误,在我的博客里已经改好了:http://blog.sina.com.cn/s/blog_45b747f70101he1t.html

如果6或者7自由度机器人的运动学逆解无法找到解析解,我们就要用数值解法来处理。这属于非线性超越方程的数值解法问题。LM算法,也成为阻尼最小二乘法。

本人编程能力太弱,只好借助于库函数。看到网上教程有用Levmar(FAQ)来做LM算法的。Levmar可用于C++, Matlab,Perl, Python, Haskell and Tcl。本人在VS2010下编程,所以下面只谈谈如何在VS2010下配置。

经过测试,win7与win8 64位系统下的VS2010/VS2013用下面的方法配置可以成功。

准备工具:

1. cmake-2.8.12.1-win32-x86.zip (选择Binarydistributions栏下的第二个)。

2. clapack-3.2.1-CMAKE.tgz(页面做的稍乱,找到同名的那个压缩包下载)。

3. levmar-2.6

步骤:

1.  先将上面下载的三个压缩包都解压。解压后的cmake-2.8.12.1-win32-x86是编译工具,建议放在常用的工具文件夹下(英文目录,不要有中文)。解压后的clapack-3.2.1-CMAKE和levmar-2.6等会要用到,而且编程的时候也用到其做包含目录,因此不建议放在桌面。

2.  针对clapack-3.2.1-CMAKE的操作。双击打开cmake-gui.exe(该文件在cmake-2.8.12.1-win32-x86文件夹里的bin文件夹下)首先要对clapack-3.2.1-CMAKE进行编译。如图:

对于箭头1,点击右边的BrowseSource...将文件夹定位到第一步解压出的clapack3.2.1-CMAKE,对于箭头2,Browse Build..则是你要编译到的位置,为了方便我选择C:\CLAPACK。选择完之后,我们点击左下角的configure按钮,弹出的框里我们选择Visual Studio 10 Win64,点Finish等几秒钟,Configure结束后画面中间会出来一些红色的条框,不管它,我们再点Configure右边的Generate按钮,根据下面提示Generate
done后,可以先关掉这个窗口了。现在我们到C:\CLAPACK下,找到CLAPACK.sln这个文件,并双击,这时候会在VS2010里出来一个工程,等待加载完成后,我们选择生成解决方案。这里需要花点时间,等程序跑完之后会提示成功了33个项目(这一步是为了生成4个lib文件)。下面我们可以先关掉VS2010。然后再次到C:\CLAPACK文件夹下,我们要寻找4个.lib文件(为了在levmar中要链接他们),这4个文件分别是:

l C:\Clapack\BLAS\SRC\Debug:       blas.lib

l C:\Clapack\F2CLIBS\libf2c\Debug:    libf2c.lib

l C:\Clapack\SRC\Debug:              lapack.lib

l C:\Clapack\Testing\MATGEN\Debug:     tmglib.lib

为了便于我们在后面链接他们,我们先在C:\CLAPACK下新建一个LIB文件夹,然后复制上面四个lib文件到这个文件夹下。到这里第二步基本完成了。

注意:复制libf2c.lib的时候我们将文件名改为f2c.lib(因为后面链接的默认名是f2c.lib)

3.   针对LEVMAR2.6部分接下去我们再次打开cmake-gui.exe,Browse Source...选择第一步解压出来的levmar-2.6,Browse Build..选择要编译到的位置,这里我选的是C:\lm26。然后点击Configure,选择Visual Studio 10 Win64,之后弹出如下所示:

将LAPACKBLAS_DIR 后面的usr/lib文件夹路径改成第二步中我们自己建的那个文件夹路径 C:\CLAPACK\LIB。然后再次点击左下角的Configure,红色变白色,之后再点击Generate。下面我们到 C:\lm26文件夹下找到LEVMAR.sln文件,运行它,点生成解决方案,全部成功后关掉。这一步是为里生成C:\lm26\Debug\levmar.lib文件。到这里配置部分基本完成了,下面讲如何在我们自己的程序里用。

4.  新建一个VS2010的c++工程lm_test,点顶部菜单栏的项目,再点属性,点左侧的配置属性,再点右侧的配置管理器,点活动平台解决方案的下拉菜单,点新建,然后把Itanium换成x64点确定,如图

这样,我们就将我们的工程配置成了64位的。

然后右键点击左侧解决方案资源管理器中lm_test,选择属性,弹出的窗口左侧点击配置属性下的VC++目录,然后在右侧的包含目录中添加我们在第一步中解压出的levmar2.6的文件夹,如图

确定后,我们再在库目录下再添加两个文件夹,一个是我们在第二步中自己新建的那个C:\CLAPACK\LIB,另一个是C:\lm26\Debug,为的就是这两个文件夹里面的lib文件,如图:

确定后我们点击左侧的链接器、输入,附加依赖项添加第二步生成4个lib文件和第三步生成的1个lib文件: lapack.lib, blas.lib, f2c.lib,tmglib.lib.levmar.lib。

现在把下面的程序拷贝进去运行看看吧。(可以对比 levmar-2.6 里面的 expfit.c  )

// testlevmar.cpp : Defines the entry pointfor the console application.

//

////////////////////////////////////////////////////////////////////////////////////

// Example program that shows how to use levmar in order to fit the three-

// parameter exponential model x_i = p[0]*exp(-p[1]*i) + p[2] to a set of

// data measurements; example is based on a similar one from GSL.

//

// Copyright (C) 2008-11  ManolisLourakis (lourakis at ics forth gr)

// Institute of Computer Science, Foundation for Research & Technology- Hellas

// Heraklion, Crete, Greece.

//

// This program is free software; you can redistribute it and/or modify

// it under the terms of the GNU General Public License as published by

// the Free Software Foundation; either version 2 of the License, or

// (at your option) any later version.

//

// This program is distributed in the hope that it will be useful,

// but WITHOUT ANY WARRANTY; without even the implied warranty of

// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

// GNU General Public License for more details.

//

////////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include "levmar.h"

#ifdef _cplusplus

extern "C"{

#include "f2c.h"

#include "clapack.h"

}

#endif

#ifndef LM_DBL_PREC

#error Example program assumes that levmarhas been compiled with double precision, see LM_DBL_PREC!

#endif

#define M_PI 3.14

#undef REPEATABLE_RANDOM

#define DBL_RAND_MAX (double)(RAND_MAX)

#ifdef _MSC_VER // MSVC

#include <process.h>

#define GETPID  _getpid

#elif defined(__GNUC__) // GCC

#include <sys/types.h>

#include <unistd.h>

#define GETPID  getpid

#else

#warning Do not know the name of thefunction returning the process id for your OS / compiler combination

#define GETPID  0

#endif

#ifdef REPEATABLE_RANDOM

#define INIT_RANDOM(seed) srandom(seed)

#else

#define INIT_RANDOM(seed)srandom((int)GETPID()) // seed unused

#endif

double gNoise(double m, double s)

{

doubler1, r2, val;

r1= ((double)rand()) / DBL_RAND_MAX;

r2= ((double)rand()) / DBL_RAND_MAX;

val= sqrt(-2.0*log(r1))*cos(2.0*M_PI*r2);

val= s*val + m;

returnval;

}

struct xtradata{

charmsg[128];

};

void expfunc(double *p, double *x, int m,int n, void *data)

{

registerint i;

structxtradata *dat;

dat= (struct xtradata *)data;

for(i = 0; i<n; ++i)

{

x[i]= p[0] * exp(-p[1] * i) + p[2];

}

}

/* Jacobian of expfunc() */

void jacexpfunc(double *p, double *jac, intm, int n, void *data)

{

registerint i, j;

structxtradata *dat;

dat= (struct xtradata *)data;

/*fill Jacobian row by row */

for(i = j = 0; i<n; ++i)

{

jac[j++]= exp(-p[1] * i);

jac[j++]= -p[0] * i*exp(-p[1] * i);

jac[j++] = 1.0;

}

}

int main()

{

constint n = 40, m = 3; // 40 measurements, 3 parameters

doublep[m], x[n], opts[LM_OPTS_SZ], info[LM_INFO_SZ];

registerint i;

intret;

structxtradata data;

/*generate some measurement using the exponential model with

*parameters (5.0, 0.1, 1.0), corrupted with zero-mean

*Gaussian noise of s=0.1

*/

//INIT_RANDOM(0);//这个地方源程序是没注释掉的

for(i = 0; i<n; ++i)

x[i]= (5.0*exp(-0.1*i) + 1.0) + gNoise(0.0, 0.1);

/*initial parameters estimate: (1.0, 0.0, 0.0) */

p[0]= 1.0; p[1] = 0.0; p[2] = 0.0;

/*optimization control parameters; passing to levmar NULL instead of opts revertsto defaults */

opts[0]= LM_INIT_MU; opts[1] = 1E-15; opts[2] = 1E-15; opts[3] = 1E-20;

opts[4]= LM_DIFF_DELTA; // relevant only if the finite difference Jacobian version isused

strcpy_s(data.msg,"Hello there!");

/*invoke the optimization function */

ret= dlevmar_der(expfunc, jacexpfunc, p, x, m, n, 1000, opts, info, NULL, NULL,(void *)&data); // with analytic Jacobian

//ret=dlevmar_dif(expfunc,p, x, m, n, 1000, opts, info, NULL, NULL, (void *)&data); // withoutJacobian

printf("Levenberg-Marquardtreturned in %g iter, reason %g, sumsq %g [%g]\n", info[5], info[6],info[1], info[0]);

printf("Bestfit parameters: %.7g %.7g %.7g\n", p[0], p[1], p[2]);

system("pause");

exit(0);

}

最新文章

  1. 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
  2. Lock的用法,为什么要用?
  3. SQL Server中@@ROWCOUNT的用法
  4. 关于sql语句的优化问题
  5. 01---Net基础加强
  6. HDU 5515 Game of Flying Circus 二分
  7. 关于mysql使用dbForge调试的问题:Object &#39;test.p_insertRoute&#39; does not exist.
  8. ASP.NET动态的网页增删查改
  9. 第三天学习内容 if--else
  10. FileStream:The process cannot access the file because it is being used by another process
  11. 编写高质量的JavaScript代码(一)
  12. javascript数组集锦
  13. 华为解锁BL
  14. Vue(服务端渲染)
  15. Linux运维工程师面试-部分题库
  16. textmate常用快捷键备忘
  17. PDF软件推荐——Sumatra PDF - imsoft.cnblogs
  18. 取消centOS7虚拟机锁屏
  19. 洛谷 P3698 [CQOI2017]小Q的棋盘 解题报告
  20. oc 中的.m和.mm文件区别

热门文章

  1. MT【44】抛物线不常见性质3
  2. 自学Linux Shell12.3-case命令
  3. SharePoint 错误集
  4. bzoj4817/luogu3703 树点涂色 (LCT+dfs序+线段树)
  5. python assert用法
  6. Azure vm 扩展脚本自动部署Elasticsearch集群
  7. [虚树模板] 洛谷P2495 消耗战
  8. 使用php与mysql构建我们的网站
  9. apigateway-kong(六)认证
  10. jdbc操作数据库(详细)