谁知道怎么用matlab调用lingo

2024-11-05 18:27:38
推荐回答(3个)
回答1:

我现在也在学习matlab调用lingo,现在只是安装了API,然后在matlab上做了随matlab启动,简单的按照API上的测试了下,可以用,算了几个自带的例子,有的还运行不出来。但具体的参数设置还在学习。API的英文手册看起来有点费力,手册里面的例子大小写有时候不分,用起来起来也不方便复制到matlab中。

每次启动matlab时,workspace会加载API,然后输入命令窗输入mxlindo,有提示。

楼主先去www.lindo.com官网下载API吧,共同学习 

回答2:

这么大规模的0-1规划恐怕用lingo也不一定能解出来 不知道你说的无法写约束是什么意思 不过你不要对lingo抱太大希望 规模太大恐怕你最好换其它思路

回答3:

在MATLAB安装目录下新建一个名为works的文件夹,将Staff.m和STAFFPTR.lng文件放在这个文件目录下,然后把Lingd15.h和Lingd15.lib放在LINGO安装目录下,
1. STAFFPTR.lng
MODEL:
SETS:
DAYS / MON TUE WED THU FRI SAT SUN/:
NEEDS, START, ONDUTY;
ENDSETS

[OBJECTIVE] MIN = @SUM( DAYS( I): START( I));

@FOR( DAYS( TODAY):

! Calculate number on duty;
ONDUTY( TODAY) =
@SUM( DAYS( D)| D #LE# 5:
START( @WRAP( TODAY - D + 1, @SIZE( DAYS))));

! Enforce staffing requirement;
ONDUTY( TODAY) >= NEEDS( TODAY);

@GIN( START);
);

DATA:
NEEDS = @POINTER( 1);
@POINTER( 2) = START;
@POINTER( 3) = ONDUTY;
@POINTER( 4) = OBJECTIVE;
@POINTER( 5) = @STATUS();
ENDDATA
END

2. Staff.m
clear; clc;

% By default, Lingo API is in the d:\LINGO15 folder
addpath('D:\\LINGO15');

% Load the Lingo API
loadlibrary('Lingd15', 'Lingd15.h');
if ~libisloaded('Lingd15')
error('Cannot load LINGO library');
end

% Print out LINGO library functions
libfunctions('Lingd15', '-full');

% Create the Lingo environment
pLingo = calllib('Lingd15', 'LScreateEnvLng');
if ~( exist('pLingo', 'var') && isa(pLingo, 'lib.pointer') )
error('Cannot create LINGO environment object');
end

% Open a Lingo log file
nError = calllib('Lingd15', 'LSopenLogFileLng', pLingo, 'D:\\LINGO15\\testLingoLib15.log');
if ~strcmp( nError, 'LSERR_NO_ERROR_LNG')
error('Cannot open LINGO log file');
end

% Staffing needs
dNeeds = [1 4 3 7 2 5 3];

% Pointers to memory transfer areas
dNeedsPtr = libpointer('doublePtr', dNeeds);
dStartPtr = libpointer('doublePtr', zeros(1, 7));
dOnDutyPtr = libpointer('doublePtr', zeros(1, 7));
dStatusPtr = libpointer('doublePtr', -1);
dTotalPtr = libpointer('doublePtr', 0);
nPointerNowPtr = libpointer('int32Ptr', 0);

% Pass memory transfer pointers to LINGO
nError = calllib('Lingd15', 'LSsetPointerLng', pLingo, dNeedsPtr, nPointerNowPtr);
if ~strcmp( nError, 'LSERR_NO_ERROR_LNG')
error('Cannot assign pointer');
end
nError = calllib('Lingd15', 'LSsetPointerLng', pLingo, dStartPtr, nPointerNowPtr);
if ~strcmp( nError, 'LSERR_NO_ERROR_LNG')
error('Cannot assign pointer');
end
nError = calllib('Lingd15', 'LSsetPointerLng', pLingo, dOnDutyPtr, nPointerNowPtr);
if ~strcmp( nError, 'LSERR_NO_ERROR_LNG')
error('Cannot assign pointer');
end
nError = calllib('Lingd15', 'LSsetPointerLng', pLingo, dTotalPtr, nPointerNowPtr);
if ~strcmp( nError, 'LSERR_NO_ERROR_LNG')
error('Cannot assign pointer');
end
nError = calllib('Lingd15', 'LSsetPointerLng', pLingo, dStatusPtr, nPointerNowPtr);
if ~strcmp( nError, 'LSERR_NO_ERROR_LNG')
error('Cannot assign pointer');
end

% Set up the command script
csScript = 'SET ECHOIN 1 \n';
csScript = [csScript 'TAKE STAFFPTR.lng \n'];
csScript = [csScript 'GO \n'];
csScript = [csScript 'QUIT \n'];

% Run the script
nError = calllib('Lingd15', 'LSexecuteScriptLng', pLingo, sprintf(csScript));
if ~strcmp( nError, 'LSERR_NO_ERROR_LNG')
error('Cannot execute script');
end

% Close log file
calllib('Lingd15', 'LScloseLogFileLng', pLingo);

% Optimal?
if get(dStatusPtr, 'value')
error('Unable to solve!');
else
% Display solution
disp(['Start : ' num2str(dStartPtr.value)]);
disp(['Needs : ' num2str(dNeedsPtr.value)]);
disp(['On Duty: ' num2str(dOnDutyPtr.value)]);
end

% Free Lingo environment
calllib('Lingd15', 'LSdeleteEnvLng', pLingo);

3. 注意,MATLAB当前目录为先前创建的works文件夹,运行看结果:
MATLAB显示的:
Warning: The data type 'FcnPtr' used by function LSsetCallbackErrorLng does not exist.
> In loadlibrary (line 431)
In Staff (line 7)
Warning: The data type 'FcnPtr' used by function LSsetCallbackSolverLng does not exist.
> In loadlibrary (line 431)
In Staff (line 7)
Start : 0 4 1 0 0 0 2
Needs : 1 4 3 7 2 5 3
On Duty: 2 6 7 7 5 5 3