wujianjack2 发表于 2015-4-5 18:55

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

以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 = "ANT_REPEL \n BEER \n BLANKET \n"
    "BRATWURST \n BROWNIES \n FRISBEE \n SALAD \n"
    "WATERMELON";

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

   // and their rankings
   double pdItemRank   = { 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;
   char pcItemsSolution;

   // 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!

页: [1]
查看完整版本: 讲讲官方的例子,VC++调用Lingo