- 在线时间
- 1150 小时
- 最后登录
- 2017-1-14
- 注册时间
- 2013-1-3
- 听众数
- 97
- 收听数
- 0
- 能力
- 0 分
- 体力
- 7367 点
- 威望
- 20 点
- 阅读权限
- 150
- 积分
- 3110
- 相册
- 0
- 日志
- 0
- 记录
- 2
- 帖子
- 1299
- 主题
- 21
- 精华
- 2
- 分享
- 0
- 好友
- 298
TA的每日心情 | 奋斗 2014-3-2 00:26 |
---|
签到天数: 243 天 [LV.8]以坛为家I
|
你可以查下手册看看函数原型,三个参数都是指针类型,比如VC下一个简单的例子。
1. Simple.lng
model:
sets:
computers /standard, turbo/: profit, limit, labor, produce;
endsets
data:
! profit on a computer;
profit = @pointer( 1);
! limit on the number of computers that can be produced;
limit = @pointer( 2);
! labor required for production of a computer;
labor = @pointer( 3);
enddata
! maximize total profit;
[rObj] max = @sum ( computers(i) : profit(i) * produce(i));
! enforce production limit;
@for (computers(i) : produce(i) <= limit(i));
! labor constraint;
@sum (computers(i) : labor(i) * produce(i)) <= 160;
data:
@pointer(4) = rObj;
@pointer(5) = @status();
@pointer(6) = produce;
enddata
end
2. Simple.c
#include <stdlib.h>
#include "Lingd15.h"
/*
This code calls the LINGO DLL to solve the simple linear program:
MAX 100 STANDARD + 150 TURBO
SUBJECT TO
2] STANDARD <= 100
3] TURBO <= 120
4] STANDARD + 2 TURBO <= 160
END
*/
/////////////////////////////////////////////////////////////////////////////
int __stdcall MyCallback( void* pModel, int nReserved, void* pUserData)
{
// this callback function will be called periodically by the Lingo solver
int* pnCallbacks = (int*) pUserData;
++*pnCallbacks;
if ( !(*pnCallbacks % 10)) printf( "In Callback: %d\n", *pnCallbacks);
return( 0);
}
/////////////////////////////////////////////////////////////////////////////
int __stdcall MyErrorCallback( void* pModel, void* pUserData, int nErrorCode,
char* pcErrorText)
{
// this callback function will be called whenever Lingo hits an error
printf( "In Error Callback: Error %d:\n%s \n", nErrorCode, pcErrorText);
return( 0);
}
/////////////////////////////////////////////////////////////////////////////
int main()
{
char cScript[256];
char cComputers[64] = "STANDARD\nTURBO\n";
int nError=-1, nPointersNow;
int nCallbacks = 0;
double dObjective, dStatus=-1.;
double dProfit[] = {100., 150.};
double dLimit[] = {100., 120.};
double dLabor[] = {1., 2.};
double dProduce[ sizeof( dProfit) / sizeof( double)];
// create the LINGO environment object
pLSenvLINGO pLINGO;
pLINGO = LScreateEnvLng();
if ( !pLINGO)
{
printf( "Can''t create LINGO environment!\n");
goto FinalExit;
}
// Pass LINGO a pointer to our callback function
nCallbacks = 0;
nError = LSsetCallbackSolverLng( pLINGO, &MyCallback, &nCallbacks);
if ( nError) goto ErrorExit;
// Pass LINGO a pointer to our callback function
nCallbacks = 0;
nError = LSsetCallbackErrorLng( pLINGO, &MyErrorCallback, NULL);
if ( nError) goto ErrorExit;
// Open LINGO's log file
nError = LSopenLogFileLng( pLINGO, "LINGO.log");
if ( nError) goto ErrorExit;
// Pass memory transfer pointers to LINGO
// @POINTER(1)
nError = LSsetPointerLng( pLINGO, dProfit, &nPointersNow);
if ( nError) goto ErrorExit;
// @POINTER(2)
nError = LSsetPointerLng( pLINGO, dLimit, &nPointersNow);
if ( nError) goto ErrorExit;
// @POINTER(3)
nError = LSsetPointerLng( pLINGO, dLabor, &nPointersNow);
if ( nError) goto ErrorExit;
// @POINTER(4)
nError = LSsetPointerLng( pLINGO, &dObjective, &nPointersNow);
if ( nError) goto ErrorExit;
// @POINTER(5)
nError = LSsetPointerLng( pLINGO, &dStatus, &nPointersNow);
if ( nError) goto ErrorExit;
// @POINTER(6)
nError = LSsetPointerLng( pLINGO, dProduce, &nPointersNow);
if ( nError) goto ErrorExit;
// @POINTER(7)
nError = LSsetPointerLng( pLINGO, cComputers, &nPointersNow);
if ( nError) goto ErrorExit;
// Here is the script we want LINGO to run
strcpy( cScript, "SET ECHOIN 1 \n TAKE SIMPLE.LNG \n GO \n QUIT \n");
// Run the script
nError = LSexecuteScriptLng( pLINGO, cScript);
if ( nError) goto ErrorExit;
// Close the log file
LScloseLogFileLng( pLINGO);
// Any problems?
if ( nError || dStatus != LS_STATUS_GLOBAL_LNG)
{
// Had a problem
printf( "Unable to solve!");
} else {
// Everything went OK ... print results
printf( "\nStandards: %g \nTurbos: %g \nProfit: %g \n",
dProduce[0], dProduce[1], dObjective);
}
goto NormalExit;
ErrorExit:
printf("LINGO Error Code: %d\n", nError);
NormalExit:
LSdeleteEnvLng( pLINGO);
FinalExit: ;
}
3. Makefile
all : simple.obj simple.exe
simple.obj : simple.c
cl -c simple.c
simple.exe : Lingd64_15.lib simple.obj
cl simple.obj Lingd64_15.lib -Fesimple.exe
当然,把用到的头文件和lib文件放在当前目录下
4. 编译运行,结果:
: TAKE SIMPLE.LNG
: model:
? sets:
? computers /standard, turbo/: profit, limit, labor, produce;
? endsets
? data:
? ! profit on a computer;
? profit = @pointer( 1);
? ! limit on the number of computers that can be produced;
? limit = @pointer( 2);
? ! labor required for production of a computer;
? labor = @pointer( 3);
? enddata
? ! maximize total profit;
? [rObj] max = @sum ( computers(i) : profit(i) * produce(i));
? ! enforce production limit;
? @for (computers(i) : produce(i) <= limit(i));
? ! labor constraint;
? @sum (computers(i) : labor(i) * produce(i)) <= 160;
? data:
? @pointer(4) = rObj;
? @pointer(5) = @status();
? @pointer(6) = produce;
? enddata
? end
: GO
Compiling model ...
Structural analysis, pass 1 ...
Scalarizing model ...
Generating nonzero matrix ...
Solving ...
Global optimal solution found.
Objective value: 14500.0000000
Infeasibilities: 0.00000000000
Total solver iterations: 0
Elapsed runtime seconds: 0.00
Running output operations ...
Model Class: LP
Total variables: 2
Nonlinear variables: 0
Integer variables: 0
Total constraints: 4
Nonlinear constraints: 0
Total nonzeros: 6
Nonlinear nonzeros: 0
Variable Value Reduced Cost
PROFIT( STANDARD) 100.000000000 0.00000000000
PROFIT( TURBO) 150.000000000 0.00000000000
LIMIT( STANDARD) 100.000000000 0.00000000000
LIMIT( TURBO) 120.000000000 0.00000000000
LABOR( STANDARD) 1.00000000000 0.00000000000
LABOR( TURBO) 2.00000000000 0.00000000000
PRODUCE( STANDARD) 100.000000000 0.00000000000
PRODUCE( TURBO) 30.0000000000 0.00000000000
Row Slack or Surplus Dual Price
ROBJ 14500.0000000 1.00000000000
2 0.00000000000 25.0000000000
3 90.0000000000 0.00000000000
4 0.00000000000 75.0000000000
: QUIT
OK, have a try.
|
|