QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1553|回复: 0
打印 上一主题 下一主题

讲讲官方的例子,VC++调用Lingo

[复制链接]
字体大小: 正常 放大

21

主题

97

听众

3110

积分

  • TA的每日心情
    奋斗
    2014-3-2 00:26
  • 签到天数: 243 天

    [LV.8]以坛为家I

    跳转到指定楼层
    1#
    发表于 2015-4-5 18:55 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    以32位为例,64位是一样的,把Lingd15.h和Lingd15.lib这两个文件放到sack.lng和sack.c一个目录,这两个文件在Programming samples文件下,那闲话少说喽!

    1. sack.lng
    MODEL:

    SETS:
       ITEMS: INCLUDE, WEIGHT, RATING;
    ENDSETS

    DATA:
       ITEMS  = @POINTER( 1);
       WEIGHT = @POINTER( 2);
       RATING = @POINTER( 3);
       KNAPSACK_CAPACITY = @POINTER( 4);
    ENDDATA

    SUBMODEL SACK:
       MAX = @SUM( ITEMS: RATING * INCLUDE);

       @SUM( ITEMS: WEIGHT * INCLUDE) <=
        KNAPSACK_CAPACITY;

       @FOR( ITEMS: @BIN( INCLUDE));
    ENDSUBMODEL

    CALC:
       !keep output to a minimum;
       @SET( 'TERSEO', 1);
       !solve the model;
       @SOLVE( SACK);
       !fix the INCLUDE attribute to it's optimal value;
       @FOR( ITEMS( I): INCLUDE( I) = INCLUDE( I));
    ENDCALC

    SETS:
       !construct a set of the optimal items;
       ITEMSUSED( ITEMS) | INCLUDE( &1) #GT# .5:;
    ENDSETS

    DATA:
       !send optimal items set back to caller;
       @POINTER( 5) = ITEMSUSED;
       !along with the solver status;
       @POINTER( 6) = @STATUS();
    ENDDATA

    END


    2. sack.c
    #include <stdlib.h>
    #include <string.h>

    #include "Lingd15.h"

    /*
       Solves a simple knapsack problem, passing
       all data to Lingo and retrieving the optimal
       set of knapsack items for display
    */

    void main()
    {
       // input data for model:

       // potential items in knapsack
       char pcItems[256] = "ANT_REPEL \n BEER \n BLANKET \n"
        "BRATWURST \n BROWNIES \n FRISBEE \n SALAD \n"
        "WATERMELON";

       // and their weights
       double pdItemWeight[8] = { 1, 3, 4, 3, 3, 1, 5,10};

       // and their rankings
       double pdItemRank[8]   = { 2, 9, 3, 8,10, 6, 4,10};

       // knapsack size
       double dSackSize = 15;

       // other declarations
       int i, nPointersNow, nError;
       double dStatus=-1.0;
       char pcScript[256];
       char pcItemsSolution[256];

       // create the LINGO environment object
       pLSenvLINGO pLINGO;
       pLINGO = LScreateEnvLng();
       if ( !pLINGO)
       {
          printf( "Can''t create LINGO environment!\n");
          goto FinalExit;
       }

       // Open LINGO's log file  
       nError = LSopenLogFileLng( pLINGO, "LINGO.log");
       if ( nError) goto ErrorExit;

       // Pass memory transfer pointers to LINGO

       // @POINTER(1) - Items set
       nError = LSsetPointerLng( pLINGO, (void*) pcItems,
        &nPointersNow);
       if ( nError) goto ErrorExit;

       // @POINTER(2) - Item weights
       nError = LSsetPointerLng( pLINGO, (void*) pdItemWeight,
        &nPointersNow);
       if ( nError) goto ErrorExit;

       // @POINTER(3) - Item ranks
       nError = LSsetPointerLng( pLINGO, (void*) pdItemRank,
        &nPointersNow);
       if ( nError) goto ErrorExit;

       // @POINTER(4) - Sack size
       nError = LSsetPointerLng( pLINGO, (void*) &dSackSize,
        &nPointersNow);
       if ( nError) goto ErrorExit;

       // @POINTER(5) - Output region for optimal items set
       nError = LSsetPointerLng( pLINGO, (void*) pcItemsSolution,
        &nPointersNow);
       if ( nError) goto ErrorExit;

       // @POINTER(6) - Variable to receive solution status
       nError = LSsetPointerLng( pLINGO, &dStatus, &nPointersNow);
       if ( nError) goto ErrorExit;

       // Here is the script we want LINGO to run:
       //  Load the model, solve the model, exit.
       strcpy( pcScript, "TAKE SACK.LNG \n GO \n NONZ \n QUIT \n");

       // Run the script
       nError = LSexecuteScriptLng( pLINGO, pcScript);
       if ( nError) goto ErrorExit;

       // display solution status
       printf("\nSolution status (should be 0): %d\n", (int) dStatus);

       // display items in optimal sack
       printf("\nItems in optimal sack:\n%s\n", pcItemsSolution);

       // Close the log file
       LScloseLogFileLng( pLINGO);

       // All done
       goto NormalExit;

    ErrorExit:
       printf("LINGO Error Code: %d\n", nError);

    NormalExit:
       LSdeleteEnvLng( pLINGO);

    FinalExit: ;

    }

    3. Makefile
    all : sack.obj sack.exe

    sack.obj : sack.c
            cl -c sack.c

    sack.exe : Lingd15.lib sack.obj
            cl sack.obj Lingd15.lib -Fesack.exe

    4. compileKnap.bat(记得之前把vcvarsall.bat所在位置添加到系统变量path中)
    @echo off
    call vcvarsall.bat x86
    nmake /f Makefile
    sack.exe
    notepad LINGO.log

    5. 双击compileKnap.bat,结果:
      Global optimal solution found.
      Objective value:                        38.00000000000
      Objective bound:                        38.00000000000
      Infeasibilities:                        0.000000000000
      Extended solver steps:                               0
      Total solver iterations:                             0
      Elapsed runtime seconds:                          0.00

      Running output operations ...
      Global optimal solution found.
      Objective value:                        38.00000000000
      Objective bound:                        38.00000000000
      Infeasibilities:                        0.000000000000
      Extended solver steps:                               0
      Total solver iterations:                             0
      Elapsed runtime seconds:                          0.00

      Model Class:                                      PILP

      Total variables:                      8
      Nonlinear variables:                  0
      Integer variables:                    8

      Total constraints:                    2
      Nonlinear constraints:                0

      Total nonzeros:                      16
      Nonlinear nonzeros:                   0


                        Variable                 Value              Reduced Cost
               KNAPSACK_CAPACITY        15.00000000000            0.000000000000
             INCLUDE( ANT_REPEL)        1.000000000000            0.000000000000
                  INCLUDE( BEER)        1.000000000000            0.000000000000
               INCLUDE( BLANKET)        1.000000000000            0.000000000000
             INCLUDE( BRATWURST)        1.000000000000            0.000000000000
              INCLUDE( BROWNIES)        1.000000000000            0.000000000000
               INCLUDE( FRISBEE)        1.000000000000            0.000000000000
              WEIGHT( ANT_REPEL)        1.000000000000            0.000000000000
                   WEIGHT( BEER)        3.000000000000            0.000000000000
                WEIGHT( BLANKET)        4.000000000000            0.000000000000
              WEIGHT( BRATWURST)        3.000000000000            0.000000000000
               WEIGHT( BROWNIES)        3.000000000000            0.000000000000
                WEIGHT( FRISBEE)        1.000000000000            0.000000000000
                  WEIGHT( SALAD)        5.000000000000            0.000000000000
             WEIGHT( WATERMELON)        10.00000000000            0.000000000000
              RATING( ANT_REPEL)        2.000000000000            0.000000000000
                   RATING( BEER)        9.000000000000            0.000000000000
                RATING( BLANKET)        3.000000000000            0.000000000000
              RATING( BRATWURST)        8.000000000000            0.000000000000
               RATING( BROWNIES)        10.00000000000            0.000000000000
                RATING( FRISBEE)        6.000000000000            0.000000000000
                  RATING( SALAD)        4.000000000000            0.000000000000
             RATING( WATERMELON)        10.00000000000            0.000000000000

                             Row          Slack or Surplus            Dual Price
                               2        0.000000000000            0.000000000000

    Ha, done!

    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    有什么好说的
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2024-5-2 02:45 , Processed in 0.260451 second(s), 48 queries .

    回顶部