jiaqionghit的个人博客分享 http://blog.sciencenet.cn/u/jiaqionghit

博文

学会使用cplex的第一步——在Microsoft Visual C++使用cplex

已有 32008 次阅读 2011-11-25 16:21 |系统分类:科研笔记| cplex

CPLEX对学习OPERATION RESEARCH的人来说是必备工具,在解决最优化问题上依然是最知名的。
主要记录下cplex的初步使用,本文主要参考的英文网址在:http://docs.hpc.maths.unsw.edu.au/ilog/cplex/12.1/mvs/c_cpp.html
本文主要记录如何初次在cplex安装好后,在visual studio中运行cplex的例子。
对英文说明进行了解释,将自己以前遇到的问题也写出来,方便大家解决。
学会如何使用 Microsoft Visual Studio
1.安装并运行 cplex中的C and C++ examples  (see dotnet.html for the C# and VB examples).
2.将cplex和C or C++ project 链接。
下载cplex和安装

CPLEX现在出到了12.3免费试用装 官网下载地址:http://www-01.ibm.com/software/websphere/products/optimization/cplex-studio-preview-edition/?&S_TACT=109HE1MW&S_CMP=web_ibm_ws_ilg-opt_hero_cplexoptimizer-ov 

其实90天也够用了,也可以申请http://www-01.ibm.com/software/websphere/products/optimization/academic-initiative/index.html IBM的高级会员,享用长期免费的。但是需要经过他们的审核,要填写带自己名字的网页等等信息,可以试着申请申请。

300多M,虽然有些慢,下载完安装即可。

CPLEX 安装目录 记住最好安装到默认的C:ILOGCPLEX121 (the default location)

****我因为没有安装到这个目录,遇到的问题:“问题:将cplex安装在c:program filesIBM下,在vc2008的cmd窗户下运行例子时候,出现'C:Program' 不是内部或外部命令,也不是可运行的程序或批处理文件。....
原因:cplex安装的C:Program Files下,中间有空格,由于路径中有空格,所以,出了这个问题 ;
解决办法:
就用正常的路径,将路径全加上了双引号。其实,如果当初安装时候把cplex安装到c盘根目录,就不会出现这样的问题了。

Library Locations cplex的一些文件都在默认路径下得多个文件夹下。都是后缀为lib的文件。 下面来试试建立和运行第一个cplex例子吧 Building and Running CPLEX Examples
所有的例子都在默认目录下<CPLEXDIR>examplesx86_.net2005_8.0stat_mtaexamples.sln.
  1. 运行vc2005或者2008

  2. File-Open Project/Solution.---<CPLEXDIR>examplesx86_.net2005_8.0stat_mta.  选择例子,打开.
  3. 比如选择blend例子To build only one example (for instance, blend):
  • 选择该项目Select the blend project in the Solution Explorer window.
  • 建立项目From the Build menu, choose Build blend.
    项目完成Wait for the completion of the building process.

4.运行例子 run an example (for instance, blend):

怎么看结果呢?

方法:

打开vc中的command prompt window ,在vc菜单的tool下面的 Visual Studio 2005 Command prompt:

0:先要在cmd窗口中设置路径,得打出来Type set path C:ILOGCPLEX_Studio_Preview123cplexbinx86_win32 so that cplex121.dll is on the path.

0然后运行例子Type C:ILOGCPLEX_Studio_Preview123cplexexamplesx86_windows_vs2008stat_mtablend.

0然后就能看到结果了The result is then displayed.

附:在vc中debug那些例子的话,会出来这样的对话框:

debugging information for "iloplex.l.exe" can Not be found or does not match......

网上找到了解决方案,共享一下:

  1. 1.   打开项目属性,把   Configuration   Properties   ->   C/C++   ->   General   ->   Debug   Information   Format   设置成   "Program   Database   (/Zi) "   或   "Program   Database   for   Edit   &   Continue   (/ZI) ";
    2.   把   C/C++   ->   General   ->   Optimization   设置成   "Disabled   (/Od) ";
    3.   把   Linker   ->   Debugging   ->   Generate   Debug   Info   设置成   Yes   (/DEBUG);

  2. 这个问题似乎只在用   General/Empty   Project   模版出现,用   Win32/Win32   Console   Application   模版设置   Application   Settings/Empty   Project   就没有。

建立自己的cplex项目

下面新建的项目的设置很麻烦,其实有个很简单的方法,就是在已经有的例子的example某个例子下面直接复制一份,在该例子的文件里面写上自己的代码即可,省的下面新建的文件需要设置很多参数环境,还挺麻烦的说。

想建立一个test.exe : 包含一个源文件 test.cpp 使用 Concert Technology or test.c
  • a folder where this file is located and which, for the sake of simplicity, we'll refer to as <MYAPPDIR>.

建立一个 test.vcproj 项目

  1. 运行vc的 Microsoft Visual Studio 2005.
  2. 建立 test.sln solution.
    File ->New->Project....
  3. 依次按照下面步骤进行

    The New Project dialog box appears.

    • In the Project Types pane, select Visual C++ Projects.
    • In the Templates pane, select the Win32 Project icon.
    • Fill in the project name (test).
    • If necessary, correct the location of the project (to <MYAPPDIR>)
    • Click OK

    When the Win32 Application Wizard appears...

    • Click on Application Settings.
    • Select Console Application as Application type.
    • Make sure that Empty project is checked in Additional Options.
    • Click Finish.

    This creates a solution, test, with a single project, test. You can see the contents of the solution by selecting Solution Explorer in the View menu.

  4. Now you must add your source file to the project. From the Project menu, choose Add Existing Item...
    • Move to the folder <MYAPPDIR> and select test.cpp or test.c.
    • Click Open.

  5. Next, you have to set some options so that the project knows where to find the CPLEX and Concert include files and the CPLEX and Concert libraries.

    From the Project menu, choose Properties.
    The test Property Pages dialog box appears.

    In the Configuration drop-down list, select Release.

    Select C/C++ in the Configuration Properties tree.

    • Select General:
      • In the Additional Include Directories field, add the directories:
        • <CPLEXDIR>include.
        • <CONCERTDIR>include.
      • For Debug Information Format, choose Disabled (/Zd).
      • Choose No for Detect 64-bit Portability Issues.

    • Select Preprocessor:
      • Add IL_STD to the Preprocessor Definitions field. This defines the macro IL_STD which is needed to use the STL.

    • Select Code Generation:
      • Set Runtime Library to Multi-threaded (/MT).

    Select Linker in the Configuration Properties tree.
    • Select Input and then select Additional Dependencies. Add the files:
      • wsock32.lib
      • <CPLEXDIR>libx86_.net2005_8.0stat_mtacplex121.lib
      • <CPLEXDIR>libx86_.net2005_8.0stat_mtailocplex.lib
      • <CONCERTDIR>libx86_.net2005_8.0stat_mtaconcert.lib
      The latter two are only necessary if you are using Concert Technology.

    Click OK to close the test Property Pages dialog box.

  6. Next, you have to set the default project configuration.

    From the Build menu, select Configuration Manager...

    • Select Release in the Active Solution Configuration drop-down list.
    • Click Close.

  7. Finally, to build the project, from the Build menu, select Build Solution
After completion of the compiling and linking process, the target is created. The full path of the test.exe is <MYAPPDIR>testReleasetest.exe.

Remark:

From the Concert point of view, the only difference between the Win32 Release and Win32 Debug targets is:

  • the NDEBUG macro is defined for the Win32 Release target.
  • the NDEBUG macro is not defined for the Win32 Debug target.
This is why we have suggested using Release in the test.sln example, even though it is not the default proposed by Visual C++ .NET. Refer to the Visual C++ Reference Manual for full information on Release and Debug configurations.

The interaction of the NDEBUG macro and the Concert inline member functions is documented in the Concepts section of the CPLEX C++ API Reference Manual.



https://blog.sciencenet.cn/blog-543282-511626.html

上一篇:参数实验:taguchi方法及其他
下一篇:citespace闪退的原因和方法
收藏 IP: 163.152.28.*| 热度|

2 杨绪洪 张士伟

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-23 05:25

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部